removed Makefile; lifted repo/org.ibex.tool/src/ to src/
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / lookup / ArrayBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.lookup;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.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 ArrayLength = 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 /**
31  * Answer the receiver's constant pool name.
32  * NOTE: This method should only be used during/after code gen.
33  * e.g. '[Ljava/lang/Object;'
34  */
35
36 public char[] constantPoolName() {
37         if (constantPoolName != null)
38                 return constantPoolName;
39
40         char[] brackets = new char[dimensions];
41         for (int i = dimensions - 1; i >= 0; i--) 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)    return leafComponentType;
61         return scope.createArray(leafComponentType, dimensions - 1);
62 }
63 public PackageBinding getPackage() {
64         return leafComponentType.getPackage();
65 }
66 /* Answer true if the receiver type can be assigned to the argument type (right)
67 */
68
69 public boolean isCompatibleWith(TypeBinding right) {
70         if (this == right)
71                 return true;
72
73         if (right.isArrayType()) {
74                 ArrayBinding rightArray = (ArrayBinding) right;
75                 if (rightArray.leafComponentType.isBaseType())
76                         return false; // relying on the fact that all equal arrays are identical
77                 if (dimensions == rightArray.dimensions)
78                         return leafComponentType.isCompatibleWith(rightArray.leafComponentType);
79                 if (dimensions < rightArray.dimensions)
80                         return false; // cannot assign 'String[]' into 'Object[][]' but can assign 'byte[][]' into 'Object[]'
81         } else {
82                 if (right.isBaseType())
83                         return false;
84         }
85         //Check dimensions - Java does not support explicitly sized dimensions for types.
86         //However, if it did, the type checking support would go here.
87         switch (right.leafComponentType().id) {
88             case T_JavaLangObject :
89             case T_JavaLangCloneable :
90             case T_JavaIoSerializable :
91                 return true;
92         }
93         return false;
94 }
95
96 public TypeBinding leafComponentType(){
97         return leafComponentType;
98 }
99
100 /* API
101 * Answer the problem id associated with the receiver.
102 * NoError if the receiver is a valid binding.
103 */
104
105 public int problemId() {
106         return leafComponentType.problemId();
107 }
108 /**
109 * Answer the source name for the type.
110 * In the case of member types, as the qualified name from its top level type.
111 * For example, for a member type N defined inside M & A: "A.M.N".
112 */
113
114 public char[] qualifiedSourceName() {
115         char[] brackets = new char[dimensions * 2];
116         for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
117                 brackets[i] = ']';
118                 brackets[i - 1] = '[';
119         }
120         return CharOperation.concat(leafComponentType.qualifiedSourceName(), brackets);
121 }
122 public char[] readableName() /* java.lang.Object[] */ {
123         char[] brackets = new char[dimensions * 2];
124         for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
125                 brackets[i] = ']';
126                 brackets[i - 1] = '[';
127         }
128         return CharOperation.concat(leafComponentType.readableName(), brackets);
129 }
130 public char[] shortReadableName(){
131         char[] brackets = new char[dimensions * 2];
132         for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
133                 brackets[i] = ']';
134                 brackets[i - 1] = '[';
135         }
136         return CharOperation.concat(leafComponentType.shortReadableName(), brackets);
137 }
138 public char[] sourceName() {
139         char[] brackets = new char[dimensions * 2];
140         for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
141                 brackets[i] = ']';
142                 brackets[i - 1] = '[';
143         }
144         return CharOperation.concat(leafComponentType.sourceName(), brackets);
145 }
146 public String toString() {
147         return leafComponentType != null ? debugName() : "NULL TYPE ARRAY"; //$NON-NLS-1$
148 }
149 }