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
 
   9  *     IBM Corporation - initial API and implementation
 
  10  *******************************************************************************/
 
  11 package net.sourceforge.phpdt.internal.compiler.lookup;
 
  13 import net.sourceforge.phpdt.core.compiler.CharOperation;
 
  14 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
 
  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
 
  20         public static final FieldBinding LengthField = new FieldBinding(LENGTH,
 
  21                         IntBinding, AccPublic | AccFinal, null, Constant.NotAConstant);
 
  23         public TypeBinding leafComponentType;
 
  25         public int dimensions;
 
  27         char[] constantPoolName;
 
  29         public ArrayBinding(TypeBinding type, int dimensions) {
 
  30                 this.tagBits |= IsArrayType;
 
  31                 this.leafComponentType = type;
 
  32                 this.dimensions = dimensions;
 
  36          * Answer the receiver's constant pool name.
 
  38          * NOTE: This method should only be used during/after code gen.
 
  41         public char[] constantPoolName() /* [Ljava/lang/Object; */{
 
  42                 if (constantPoolName != null)
 
  43                         return constantPoolName;
 
  45                 char[] brackets = new char[dimensions];
 
  46                 for (int i = dimensions - 1; i >= 0; i--)
 
  48                 return constantPoolName = CharOperation.concat(brackets,
 
  49                                 leafComponentType.signature());
 
  53                 StringBuffer brackets = new StringBuffer(dimensions * 2);
 
  54                 for (int i = dimensions; --i >= 0;)
 
  55                         brackets.append("[]"); //$NON-NLS-1$
 
  56                 return leafComponentType.debugName() + brackets.toString();
 
  59         public int dimensions() {
 
  60                 return this.dimensions;
 
  64          * Answer an array whose dimension size is one less than the receiver.
 
  66          * When the receiver's dimension size is one then answer the leaf component
 
  70         public TypeBinding elementsType(Scope scope) {
 
  72                         return leafComponentType;
 
  74                         return scope.createArray(leafComponentType, dimensions - 1);
 
  77         public PackageBinding getPackage() {
 
  78                 return leafComponentType.getPackage();
 
  82          * Answer true if the receiver type can be assigned to the argument type
 
  86         public boolean isCompatibleWith(TypeBinding right) {
 
  91                 if (right.isArrayType()) {
 
  92                         ArrayBinding rightArray = (ArrayBinding) right;
 
  93                         if (rightArray.leafComponentType.isBaseType())
 
  94                                 return false; // relying on the fact that all equal arrays are
 
  96                         if (dimensions == rightArray.dimensions)
 
  97                                 return leafComponentType
 
  98                                                 .isCompatibleWith(rightArray.leafComponentType);
 
  99                         if (dimensions < rightArray.dimensions)
 
 100                                 return false; // cannot assign 'String[]' into 'Object[][]'
 
 101                                                                 // but can assign 'byte[][]' into 'Object[]'
 
 102                         rightName = ((ReferenceBinding) rightArray.leafComponentType).compoundName;
 
 104                         if (right.isBaseType())
 
 106                         rightName = ((ReferenceBinding) right).compoundName;
 
 108                 // Check dimensions - Java does not support explicitly sized dimensions
 
 110                 // However, if it did, the type checking support would go here.
 
 112                 if (CharOperation.equals(rightName, JAVA_LANG_OBJECT))
 
 114                 if (CharOperation.equals(rightName, JAVA_LANG_CLONEABLE))
 
 116                 if (CharOperation.equals(rightName, JAVA_IO_SERIALIZABLE))
 
 121         public TypeBinding leafComponentType() {
 
 122                 return leafComponentType;
 
 126          * API Answer the problem id associated with the receiver. NoError if the
 
 127          * receiver is a valid binding.
 
 130         public int problemId() {
 
 131                 return leafComponentType.problemId();
 
 135          * Answer the source name for the type. In the case of member types, as the
 
 136          * qualified name from its top level type. For example, for a member type N
 
 137          * defined inside M & A: "A.M.N".
 
 140         public char[] qualifiedSourceName() {
 
 141                 char[] brackets = new char[dimensions * 2];
 
 142                 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
 
 144                         brackets[i - 1] = '[';
 
 146                 return CharOperation.concat(leafComponentType.qualifiedSourceName(),
 
 150         public char[] readableName() /* java.lang.Object[] */{
 
 151                 char[] brackets = new char[dimensions * 2];
 
 152                 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
 
 154                         brackets[i - 1] = '[';
 
 156                 return CharOperation.concat(leafComponentType.readableName(), brackets);
 
 159         public char[] shortReadableName() {
 
 160                 char[] brackets = new char[dimensions * 2];
 
 161                 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
 
 163                         brackets[i - 1] = '[';
 
 165                 return CharOperation.concat(leafComponentType.shortReadableName(),
 
 169         public char[] sourceName() {
 
 170                 char[] brackets = new char[dimensions * 2];
 
 171                 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
 
 173                         brackets[i - 1] = '[';
 
 175                 return CharOperation.concat(leafComponentType.sourceName(), brackets);
 
 178         public String toString() {
 
 179                 return leafComponentType != null ? debugName() : "NULL TYPE ARRAY"; //$NON-NLS-1$