Refactored packagename to net.sourceforge.phpdt.internal.compiler.ast
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / lookup / LookupEnvironment.java
index 781189b..54e6de2 100644 (file)
@@ -1,36 +1,35 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials 
- * are made available under the terms of the Common Public License v0.5 
+ * are made available under the terms of the Common Public License v1.0
  * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/cpl-v05.html
+ * http://www.eclipse.org/legal/cpl-v10.html
  * 
  * Contributors:
  *     IBM Corporation - initial API and implementation
- ******************************************************************************/
+ *******************************************************************************/
 package net.sourceforge.phpdt.internal.compiler.lookup;
 
+import net.sourceforge.phpdt.core.compiler.CharOperation;
 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
 import net.sourceforge.phpdt.internal.compiler.env.IBinaryType;
 import net.sourceforge.phpdt.internal.compiler.env.INameEnvironment;
 import net.sourceforge.phpdt.internal.compiler.env.NameEnvironmentAnswer;
-import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
 import net.sourceforge.phpdt.internal.compiler.impl.ITypeRequestor;
 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
-import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
 import net.sourceforge.phpdt.internal.compiler.util.HashtableOfPackage;
 import net.sourceforge.phpdt.internal.compiler.util.Util;
 
 public class LookupEnvironment implements BaseTypes, ProblemReasons, TypeConstants {
-       public CompilerOptions options;
+//     public CompilerOptions options;
        public ProblemReporter problemReporter;
        public ITypeRequestor typeRequestor;
 
        PackageBinding defaultPackage;
        ImportBinding[] defaultImports;
        HashtableOfPackage knownPackages;
-       static final ProblemPackageBinding theNotFoundPackage = new ProblemPackageBinding(new char[0], NotFound);
-       static final ProblemReferenceBinding theNotFoundType = new ProblemReferenceBinding(new char[0], NotFound);
+       static final ProblemPackageBinding TheNotFoundPackage = new ProblemPackageBinding(CharOperation.NO_CHAR, NotFound);
+       static final ProblemReferenceBinding TheNotFoundType = new ProblemReferenceBinding(CharOperation.NO_CHAR, NotFound);
 
        private INameEnvironment nameEnvironment;
        private MethodVerifier verifier;
@@ -49,9 +48,10 @@ public class LookupEnvironment implements BaseTypes, ProblemReasons, TypeConstan
        final static int CHECK_AND_SET_IMPORTS = 2;
        final static int CONNECT_TYPE_HIERARCHY = 3;
        final static int BUILD_FIELDS_AND_METHODS = 4;
-public LookupEnvironment(ITypeRequestor typeRequestor, CompilerOptions options, ProblemReporter problemReporter, INameEnvironment nameEnvironment) {
+public LookupEnvironment(ITypeRequestor typeRequestor,ProblemReporter problemReporter, INameEnvironment nameEnvironment) {
+// CompilerOptions options, ProblemReporter problemReporter, INameEnvironment nameEnvironment) {
        this.typeRequestor = typeRequestor;
-       this.options = options;
+//     this.options = options;
        this.problemReporter = problemReporter;
        this.defaultPackage = new PackageBinding(this); // assume the default package always exists
        this.defaultImports = null;
@@ -161,18 +161,18 @@ public BinaryTypeBinding cacheBinaryType(IBinaryType binaryType, boolean needFie
 public void completeTypeBindings() {
        stepCompleted = BUILD_TYPE_HIERARCHY;
        
-       for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
-               units[i].scope.checkAndSetImports();
-       }
+//     for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
+//             units[i].scope.checkAndSetImports();
+//     }
        stepCompleted = CHECK_AND_SET_IMPORTS;
 
-       for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
-               units[i].scope.connectTypeHierarchy();
-       }
+//     for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
+//             units[i].scope.connectTypeHierarchy();
+//     }
        stepCompleted = CONNECT_TYPE_HIERARCHY;
 
        for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
-               units[i].scope.buildFieldsAndMethods();
+//             units[i].scope.buildFieldsAndMethods();
                units[i] = null; // release unnecessary reference to the parsed unit
        }
        stepCompleted = BUILD_FIELDS_AND_METHODS;
@@ -199,8 +199,8 @@ public void completeTypeBindings(CompilationUnitDeclaration parsedUnit) {
        } else {
                if (parsedUnit.scope == null) return; // parsing errors were too severe
 
-               if (stepCompleted >= CHECK_AND_SET_IMPORTS)
-                       parsedUnit.scope.checkAndSetImports();
+//             if (stepCompleted >= CHECK_AND_SET_IMPORTS)
+//                     parsedUnit.scope.checkAndSetImports();
 
                if (stepCompleted >= CONNECT_TYPE_HIERARCHY)
                        parsedUnit.scope.connectTypeHierarchy();
@@ -223,19 +223,20 @@ public void completeTypeBindings(CompilationUnitDeclaration parsedUnit, boolean
        if (buildFieldsAndMethods)
                parsedUnit.scope.buildFieldsAndMethods();
 }
+
 private PackageBinding computePackageFrom(char[][] constantPoolName) {
        if (constantPoolName.length == 1)
                return defaultPackage;
 
        PackageBinding packageBinding = getPackage0(constantPoolName[0]);
-       if (packageBinding == null || packageBinding == theNotFoundPackage) {
+       if (packageBinding == null || packageBinding == TheNotFoundPackage) {
                packageBinding = new PackageBinding(constantPoolName[0], this);
                knownPackages.put(constantPoolName[0], packageBinding);
        }
 
        for (int i = 1, length = constantPoolName.length - 1; i < length; i++) {
                PackageBinding parent = packageBinding;
-               if ((packageBinding = parent.getPackage0(constantPoolName[i])) == null || packageBinding == theNotFoundPackage) {
+               if ((packageBinding = parent.getPackage0(constantPoolName[i])) == null || packageBinding == TheNotFoundPackage) {
                        packageBinding = new PackageBinding(CharOperation.subarray(constantPoolName, 0, i + 1), parent, this);
                        parent.addPackage(packageBinding);
                }
@@ -246,6 +247,9 @@ private PackageBinding computePackageFrom(char[][] constantPoolName) {
 */
 
 ArrayBinding createArrayType(TypeBinding type, int dimensionCount) {
+       if (type instanceof LocalTypeBinding) // cache local type arrays with the local type itself
+               return ((LocalTypeBinding) type).createArrayType(dimensionCount);
+
        // find the array binding cache for this dimension
        int dimIndex = dimensionCount - 1;
        int length = uniqueArrayBindings.length;
@@ -305,35 +309,36 @@ public BinaryTypeBinding createBinaryTypeFrom(IBinaryType binaryType, PackageBin
 */
 
 PackageBinding createPackage(char[][] compoundName) {
-       PackageBinding packageBinding = getPackage0(compoundName[0]);
-       if (packageBinding == null || packageBinding == theNotFoundPackage) {
-               packageBinding = new PackageBinding(compoundName[0], this);
-               knownPackages.put(compoundName[0], packageBinding);
-       }
-
-       for (int i = 1, length = compoundName.length; i < length; i++) {
-               // check to see if it collides with a known type...
-               // this case can only happen if the package does not exist as a directory in the file system
-               // otherwise when the source type was defined, the correct error would have been reported
-               // unless its an unresolved type which is referenced from an inconsistent class file
-               ReferenceBinding type = packageBinding.getType0(compoundName[i]);
-               if (type != null && type != theNotFoundType && !(type instanceof UnresolvedReferenceBinding))
-                       return null;
-
-               PackageBinding parent = packageBinding;
-               if ((packageBinding = parent.getPackage0(compoundName[i])) == null || packageBinding == theNotFoundPackage) {
-                       // if the package is unknown, check to see if a type exists which would collide with the new package
-                       // catches the case of a package statement of: package java.lang.Object;
-                       // since the package can be added after a set of source files have already been compiled, we need
-                       // whenever a package statement is encountered
-                       if (nameEnvironment.findType(compoundName[i], parent.compoundName) != null)
-                               return null;
-
-                       packageBinding = new PackageBinding(CharOperation.subarray(compoundName, 0, i + 1), parent, this);
-                       parent.addPackage(packageBinding);
-               }
-       }
-       return packageBinding;
+    return null;
+//     PackageBinding packageBinding = getPackage0(compoundName[0]);
+//     if (packageBinding == null || packageBinding == TheNotFoundPackage) {
+//             packageBinding = new PackageBinding(compoundName[0], this);
+//             knownPackages.put(compoundName[0], packageBinding);
+//     }
+//
+//     for (int i = 1, length = compoundName.length; i < length; i++) {
+//             // check to see if it collides with a known type...
+//             // this case can only happen if the package does not exist as a directory in the file system
+//             // otherwise when the source type was defined, the correct error would have been reported
+//             // unless its an unresolved type which is referenced from an inconsistent class file
+//             ReferenceBinding type = packageBinding.getType0(compoundName[i]);
+//             if (type != null && type != TheNotFoundType && !(type instanceof UnresolvedReferenceBinding))
+//                     return null;
+//
+//             PackageBinding parent = packageBinding;
+//             if ((packageBinding = parent.getPackage0(compoundName[i])) == null || packageBinding == TheNotFoundPackage) {
+//                     // if the package is unknown, check to see if a type exists which would collide with the new package
+//                     // catches the case of a package statement of: package java.lang.Object;
+//                     // since the package can be added after a set of source files have already been compiled, we need
+//                     // whenever a package statement is encountered
+//                     if (nameEnvironment.findType(compoundName[i], parent.compoundName) != null)
+//                             return null;
+//
+//                     packageBinding = new PackageBinding(CharOperation.subarray(compoundName, 0, i + 1), parent, this);
+//                     parent.addPackage(packageBinding);
+//             }
+//     }
+//     return packageBinding;
 }
 /* Answer the type for the compoundName if it exists in the cache.
 * Answer theNotFoundType if it could not be resolved the first time
@@ -351,11 +356,11 @@ public ReferenceBinding getCachedType(char[][] compoundName) {
        }
 
        PackageBinding packageBinding = getPackage0(compoundName[0]);
-       if (packageBinding == null || packageBinding == theNotFoundPackage)
+       if (packageBinding == null || packageBinding == TheNotFoundPackage)
                return null;
 
        for (int i = 1, packageLength = compoundName.length - 1; i < packageLength; i++)
-               if ((packageBinding = packageBinding.getPackage0(compoundName[i])) == null || packageBinding == theNotFoundPackage)
+               if ((packageBinding = packageBinding.getPackage0(compoundName[i])) == null || packageBinding == TheNotFoundPackage)
                        return null;
        return packageBinding.getType0(compoundName[compoundName.length - 1]);
 }
@@ -378,7 +383,7 @@ PackageBinding getPackage0(char[] name) {
 PackageBinding getTopLevelPackage(char[] name) {
        PackageBinding packageBinding = getPackage0(name);
        if (packageBinding != null) {
-               if (packageBinding == theNotFoundPackage)
+               if (packageBinding == TheNotFoundPackage)
                        return null;
                else
                        return packageBinding;
@@ -389,7 +394,7 @@ PackageBinding getTopLevelPackage(char[] name) {
                return packageBinding;
        }
 
-       knownPackages.put(name, theNotFoundPackage); // saves asking the oracle next time
+       knownPackages.put(name, TheNotFoundPackage); // saves asking the oracle next time
        return null;
 }
 /* Answer the type corresponding to the compoundName.
@@ -406,20 +411,20 @@ public ReferenceBinding getType(char[][] compoundName) {
 
                if ((referenceBinding = defaultPackage.getType0(compoundName[0])) == null) {
                        PackageBinding packageBinding = getPackage0(compoundName[0]);
-                       if (packageBinding != null && packageBinding != theNotFoundPackage)
+                       if (packageBinding != null && packageBinding != TheNotFoundPackage)
                                return null; // collides with a known package... should not call this method in such a case
                        referenceBinding = askForType(defaultPackage, compoundName[0]);
                }
        } else {
                PackageBinding packageBinding = getPackage0(compoundName[0]);
-               if (packageBinding == theNotFoundPackage)
+               if (packageBinding == TheNotFoundPackage)
                        return null;
 
                if (packageBinding != null) {
                        for (int i = 1, packageLength = compoundName.length - 1; i < packageLength; i++) {
                                if ((packageBinding = packageBinding.getPackage0(compoundName[i])) == null)
                                        break;
-                               if (packageBinding == theNotFoundPackage)
+                               if (packageBinding == TheNotFoundPackage)
                                        return null;
                        }
                }
@@ -430,12 +435,12 @@ public ReferenceBinding getType(char[][] compoundName) {
                        referenceBinding = askForType(packageBinding, compoundName[compoundName.length - 1]);
        }
 
-       if (referenceBinding == null || referenceBinding == theNotFoundType)
+       if (referenceBinding == null || referenceBinding == TheNotFoundType)
                return null;
        if (referenceBinding instanceof UnresolvedReferenceBinding)
                referenceBinding = ((UnresolvedReferenceBinding) referenceBinding).resolve(this);
 
-       // compoundName refers to a nested type incorrectly (i.e. package1.A$B)
+       // compoundName refers to a nested type incorrectly (for example, package1.A$B)
        if (referenceBinding.isNestedType())
                return new ProblemReferenceBinding(compoundName, InternalNameProvided);
        else
@@ -452,7 +457,7 @@ public ReferenceBinding getType(char[][] compoundName) {
 
 ReferenceBinding getTypeFromConstantPoolName(char[] signature, int start, int end) {
        if (end == -1)
-               end = signature.length - 1;
+               end = signature.length;
 
        char[][] compoundName = CharOperation.splitOn('/', signature, start, end);
        ReferenceBinding binding = getCachedType(compoundName);
@@ -460,7 +465,7 @@ ReferenceBinding getTypeFromConstantPoolName(char[] signature, int start, int en
                PackageBinding packageBinding = computePackageFrom(compoundName);
                binding = new UnresolvedReferenceBinding(compoundName, packageBinding);
                packageBinding.addType(binding);
-       } else if (binding == theNotFoundType) {
+       } else if (binding == TheNotFoundType) {
                problemReporter.isClassPathCorrect(compoundName, null);
                return null; // will not get here since the above error aborts the compilation
        }
@@ -519,7 +524,7 @@ TypeBinding getTypeFromSignature(char[] signature, int start, int end) {
                                throw new Error(Util.bind("error.undefinedBaseType",String.valueOf(signature[start]))); //$NON-NLS-1$
                }
        } else {
-               binding = getTypeFromConstantPoolName(signature, start + 1, end - 1);
+               binding = getTypeFromConstantPoolName(signature, start + 1, end);
        }
 
        if (dimension == 0)