Rename HTML editor to Smarty editor
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / lookup / ArrayBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.lookup;
12
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
15
16 public final class ArrayBinding extends TypeBinding {
17         // creation and initialization of the length field
18         // the declaringClass of this field is intentionally set to null so it can be distinguished.
19         public static final FieldBinding LengthField = new FieldBinding(LENGTH, IntBinding, AccPublic | AccFinal, null, Constant.NotAConstant);
20
21         public TypeBinding leafComponentType;
22         public int dimensions;
23
24         char[] constantPoolName;
25 public ArrayBinding(TypeBinding type, int dimensions) {
26         this.tagBits |= IsArrayType;
27         this.leafComponentType = type;
28         this.dimensions = dimensions;
29 }
30 /* Answer the receiver's constant pool name.
31 *
32 * NOTE: This method should only be used during/after code gen.
33 */
34
35 public char[] constantPoolName() /*     [Ljava/lang/Object; */ {
36         if (constantPoolName != null)
37                 return constantPoolName;
38
39         char[] brackets = new char[dimensions];
40         for (int i = dimensions - 1; i >= 0; i--)
41                 brackets[i] = '[';
42         return constantPoolName = CharOperation.concat(brackets, leafComponentType.signature());
43 }
44 String debugName() {
45         StringBuffer brackets = new StringBuffer(dimensions * 2);
46         for (int i = dimensions; --i >= 0;)
47                 brackets.append("[]"); //$NON-NLS-1$
48         return leafComponentType.debugName() + brackets.toString();
49 }
50 public int dimensions() {
51         return this.dimensions;
52 }
53
54 /* Answer an array whose dimension size is one less than the receiver.
55 *
56 * When the receiver's dimension size is one then answer the leaf component type.
57 */
58
59 public TypeBinding elementsType(Scope scope) {
60         if (dimensions == 1)
61                 return leafComponentType;
62         else
63                 return scope.createArray(leafComponentType, dimensions - 1);
64 }
65 public PackageBinding getPackage() {
66         return leafComponentType.getPackage();
67 }
68 /* Answer true if the receiver type can be assigned to the argument type (right)
69 */
70
71 public boolean isCompatibleWith(TypeBinding right) {
72         if (this == right)
73                 return true;
74
75         char[][] rightName;
76         if (right.isArrayType()) {
77                 ArrayBinding rightArray = (ArrayBinding) right;
78                 if (rightArray.leafComponentType.isBaseType())
79                         return false; // relying on the fact that all equal arrays are identical
80                 if (dimensions == rightArray.dimensions)
81                         return leafComponentType.isCompatibleWith(rightArray.leafComponentType);
82                 if (dimensions < rightArray.dimensions)
83                         return false; // cannot assign 'String[]' into 'Object[][]' but can assign 'byte[][]' into 'Object[]'
84                 rightName = ((ReferenceBinding) rightArray.leafComponentType).compoundName;
85         } else {
86                 if (right.isBaseType())
87                         return false;
88                 rightName = ((ReferenceBinding) right).compoundName;
89         }
90         //Check dimensions - Java does not support explicitly sized dimensions for types.
91         //However, if it did, the type checking support would go here.
92
93         if (CharOperation.equals(rightName, JAVA_LANG_OBJECT))
94                 return true;
95         if (CharOperation.equals(rightName, JAVA_LANG_CLONEABLE))
96                 return true;
97         if (CharOperation.equals(rightName, JAVA_IO_SERIALIZABLE))
98                 return true;
99         return false;
100 }
101
102 public TypeBinding leafComponentType(){
103         return leafComponentType;
104 }
105
106 /* API
107 * Answer the problem id associated with the receiver.
108 * NoError if the receiver is a valid binding.
109 */
110
111 public int problemId() {
112         return leafComponentType.problemId();
113 }
114 /**
115 * Answer the source name for the type.
116 * In the case of member types, as the qualified name from its top level type.
117 * For example, for a member type N defined inside M & A: "A.M.N".
118 */
119
120 public char[] qualifiedSourceName() {
121         char[] brackets = new char[dimensions * 2];
122         for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
123                 brackets[i] = ']';
124                 brackets[i - 1] = '[';
125         }
126         return CharOperation.concat(leafComponentType.qualifiedSourceName(), brackets);
127 }
128 public char[] readableName() /* java.lang.Object[] */ {
129         char[] brackets = new char[dimensions * 2];
130         for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
131                 brackets[i] = ']';
132                 brackets[i - 1] = '[';
133         }
134         return CharOperation.concat(leafComponentType.readableName(), brackets);
135 }
136 public char[] shortReadableName(){
137         char[] brackets = new char[dimensions * 2];
138         for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
139                 brackets[i] = ']';
140                 brackets[i - 1] = '[';
141         }
142         return CharOperation.concat(leafComponentType.shortReadableName(), brackets);
143 }
144 public char[] sourceName() {
145         char[] brackets = new char[dimensions * 2];
146         for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
147                 brackets[i] = ']';
148                 brackets[i - 1] = '[';
149         }
150         return CharOperation.concat(leafComponentType.sourceName(), brackets);
151 }
152 public String toString() {
153         return leafComponentType != null ? debugName() : "NULL TYPE ARRAY"; //$NON-NLS-1$
154 }
155 }