1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.lookup;
13 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
14 import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
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);
21 public TypeBinding leafComponentType;
22 public int dimensions;
24 char[] constantPoolName;
25 public ArrayBinding(TypeBinding type, int dimensions) {
26 this.tagBits |= IsArrayType;
27 this.leafComponentType = type;
28 this.dimensions = dimensions;
30 /* Answer the receiver's constant pool name.
32 * NOTE: This method should only be used during/after code gen.
35 public char[] constantPoolName() /* [Ljava/lang/Object; */ {
36 if (constantPoolName != null)
37 return constantPoolName;
39 char[] brackets = new char[dimensions];
40 for (int i = dimensions - 1; i >= 0; i--)
42 return constantPoolName = CharOperation.concat(brackets, leafComponentType.signature());
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();
50 /* Answer an array whose dimension size is one less than the receiver.
52 * When the receiver's dimension size is one then answer the leaf component type.
55 public TypeBinding elementsType(Scope scope) {
57 return leafComponentType;
59 return scope.createArray(leafComponentType, dimensions - 1);
61 public PackageBinding getPackage() {
62 return leafComponentType.getPackage();
64 /* Answer true if the receiver type can be assigned to the argument type (right)
67 boolean isCompatibleWith(TypeBinding right) {
72 if (right.isArrayType()) {
73 ArrayBinding rightArray = (ArrayBinding) right;
74 if (rightArray.leafComponentType.isBaseType())
75 return false; // relying on the fact that all equal arrays are identical
76 if (dimensions == rightArray.dimensions)
77 return leafComponentType.isCompatibleWith(rightArray.leafComponentType);
78 if (dimensions < rightArray.dimensions)
79 return false; // cannot assign 'String[]' into 'Object[][]' but can assign 'byte[][]' into 'Object[]'
80 rightName = ((ReferenceBinding) rightArray.leafComponentType).compoundName;
82 if (right.isBaseType())
84 rightName = ((ReferenceBinding) right).compoundName;
86 //Check dimensions - Java does not support explicitly sized dimensions for types.
87 //However, if it did, the type checking support would go here.
89 if (CharOperation.equals(rightName, JAVA_LANG_OBJECT))
91 if (CharOperation.equals(rightName, JAVA_LANG_CLONEABLE))
93 if (CharOperation.equals(rightName, JAVA_IO_SERIALIZABLE))
98 public TypeBinding leafComponentType(){
99 return leafComponentType;
103 * Answer the problem id associated with the receiver.
104 * NoError if the receiver is a valid binding.
107 public int problemId() {
108 return leafComponentType.problemId();
111 * Answer the source name for the type.
112 * In the case of member types, as the qualified name from its top level type.
113 * For example, for a member type N defined inside M & A: "A.M.N".
116 public char[] qualifiedSourceName() {
117 char[] brackets = new char[dimensions * 2];
118 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
120 brackets[i - 1] = '[';
122 return CharOperation.concat(leafComponentType.qualifiedSourceName(), brackets);
124 public char[] readableName() /* java.lang.Object[] */ {
125 char[] brackets = new char[dimensions * 2];
126 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
128 brackets[i - 1] = '[';
130 return CharOperation.concat(leafComponentType.readableName(), brackets);
132 public char[] sourceName() {
133 char[] brackets = new char[dimensions * 2];
134 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
136 brackets[i - 1] = '[';
138 return CharOperation.concat(leafComponentType.sourceName(), brackets);
140 public String toString() {
141 return leafComponentType != null ? debugName() : "NULL TYPE ARRAY"; //$NON-NLS-1$