langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java
changeset 25443 9187d77f2c64
parent 25435 928866794984
child 25445 603f0c93d5c9
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Tue Jul 08 18:26:34 2014 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Jul 09 16:32:05 2014 +0200
@@ -28,6 +28,7 @@
 import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
 import com.sun.tools.javac.api.Formattable.LocalizedString;
 import com.sun.tools.javac.code.*;
+import com.sun.tools.javac.code.Scope.WriteableScope;
 import com.sun.tools.javac.code.Symbol.*;
 import com.sun.tools.javac.code.Type.*;
 import com.sun.tools.javac.comp.Attr.ResultInfo;
@@ -100,7 +101,7 @@
     private final boolean compactMethodDiags;
     final EnumSet<VerboseResolutionMode> verboseResolutionMode;
 
-    Scope polymorphicSignatureScope;
+    WriteableScope polymorphicSignatureScope;
 
     protected Resolve(Context context) {
         context.put(resolveKey, this);
@@ -139,7 +140,7 @@
         allowFunctionalInterfaceMostSpecific = source.allowFunctionalInterfaceMostSpecific();
         checkVarargsAccessAfterResolution =
                 source.allowPostApplicabilityVarargsAccessCheck();
-        polymorphicSignatureScope = new Scope(syms.noSymbol);
+        polymorphicSignatureScope = WriteableScope.create(syms.noSymbol);
 
         inapplicableMethodException = new InapplicableMethodException(diags);
     }
@@ -1299,13 +1300,11 @@
             c = c.type.getUpperBound().tsym;
         Symbol bestSoFar = varNotFound;
         Symbol sym;
-        Scope.Entry e = c.members().lookup(name);
-        while (e.scope != null) {
-            if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
-                return isAccessible(env, site, e.sym)
-                    ? e.sym : new AccessError(env, site, e.sym);
+        for (Symbol s : c.members().getSymbolsByName(name)) {
+            if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
+                return isAccessible(env, site, s)
+                    ? s : new AccessError(env, site, s);
             }
-            e = e.next();
         }
         Type st = types.supertype(c.type);
         if (st != null && (st.hasTag(CLASS) || st.hasTag(TYPEVAR))) {
@@ -1348,20 +1347,20 @@
      */
     Symbol findVar(Env<AttrContext> env, Name name) {
         Symbol bestSoFar = varNotFound;
-        Symbol sym;
         Env<AttrContext> env1 = env;
         boolean staticOnly = false;
         while (env1.outer != null) {
+            Symbol sym = null;
             if (isStatic(env1)) staticOnly = true;
-            Scope.Entry e = env1.info.scope.lookup(name);
-            while (e.scope != null &&
-                   (e.sym.kind != VAR ||
-                    (e.sym.flags_field & SYNTHETIC) != 0))
-                e = e.next();
-            sym = (e.scope != null)
-                ? e.sym
-                : findField(
-                    env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
+            for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
+                if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
+                    sym = s;
+                    break;
+                }
+            }
+            if (sym == null) {
+                sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
+            }
             if (sym.exists()) {
                 if (staticOnly &&
                     sym.kind == VAR &&
@@ -1378,7 +1377,7 @@
             env1 = env1.outer;
         }
 
-        sym = findField(env, syms.predefClass.type, name, syms.predefClass);
+        Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
         if (sym.exists())
             return sym;
         if (bestSoFar.exists())
@@ -1386,18 +1385,16 @@
 
         Symbol origin = null;
         for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
-            Scope.Entry e = sc.lookup(name);
-            for (; e.scope != null; e = e.next()) {
-                sym = e.sym;
-                if (sym.kind != VAR)
+            for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
+                if (currentSymbol.kind != VAR)
                     continue;
                 // invariant: sym.kind == VAR
-                if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
-                    return new AmbiguityError(bestSoFar, sym);
+                if (bestSoFar.kind < AMBIGUOUS && currentSymbol.owner != bestSoFar.owner)
+                    return new AmbiguityError(bestSoFar, currentSymbol);
                 else if (bestSoFar.kind >= VAR) {
-                    origin = e.getOrigin().owner;
-                    bestSoFar = isAccessible(env, origin.type, sym)
-                        ? sym : new AccessError(env, origin.type, sym);
+                    origin = sc.getOrigin(currentSymbol).owner;
+                    bestSoFar = isAccessible(env, origin.type, currentSymbol)
+                        ? currentSymbol : new AccessError(env, origin.type, currentSymbol);
                 }
             }
             if (bestSoFar.exists()) break;
@@ -1627,7 +1624,7 @@
             boolean useVarargs,
             boolean operator,
             boolean abstractok) {
-        for (Symbol s : sc.getElementsByName(name, new LookupFilter(abstractok))) {
+        for (Symbol s : sc.getSymbolsByName(name, new LookupFilter(abstractok))) {
             bestSoFar = selectBest(env, site, argtypes, typeargtypes, s,
                     bestSoFar, allowBoxing, useVarargs, operator);
         }
@@ -1827,12 +1824,11 @@
                    List<Type> argtypes, List<Type> typeargtypes,
                    boolean allowBoxing, boolean useVarargs) {
         Symbol bestSoFar = methodNotFound;
-        Symbol sym;
         Env<AttrContext> env1 = env;
         boolean staticOnly = false;
         while (env1.outer != null) {
             if (isStatic(env1)) staticOnly = true;
-            sym = findMethod(
+            Symbol sym = findMethod(
                 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
                 allowBoxing, useVarargs, false);
             if (sym.exists()) {
@@ -1848,41 +1844,37 @@
             env1 = env1.outer;
         }
 
-        sym = findMethod(env, syms.predefClass.type, name, argtypes,
-                         typeargtypes, allowBoxing, useVarargs, false);
+        Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
+                                typeargtypes, allowBoxing, useVarargs, false);
         if (sym.exists())
             return sym;
 
-        Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
-        for (; e.scope != null; e = e.next()) {
-            sym = e.sym;
-            Type origin = e.getOrigin().owner.type;
-            if (sym.kind == MTH) {
-                if (e.sym.owner.type != origin)
-                    sym = sym.clone(e.getOrigin().owner);
-                if (!isAccessible(env, origin, sym))
-                    sym = new AccessError(env, origin, sym);
-                bestSoFar = selectBest(env, origin,
+        for (Symbol currentSym : env.toplevel.namedImportScope.getSymbolsByName(name)) {
+            Symbol origin = env.toplevel.namedImportScope.getOrigin(currentSym).owner;
+            if (currentSym.kind == MTH) {
+                if (currentSym.owner.type != origin.type)
+                    currentSym = currentSym.clone(origin);
+                if (!isAccessible(env, origin.type, currentSym))
+                    currentSym = new AccessError(env, origin.type, currentSym);
+                bestSoFar = selectBest(env, origin.type,
                                        argtypes, typeargtypes,
-                                       sym, bestSoFar,
+                                       currentSym, bestSoFar,
                                        allowBoxing, useVarargs, false);
             }
         }
         if (bestSoFar.exists())
             return bestSoFar;
 
-        e = env.toplevel.starImportScope.lookup(name);
-        for (; e.scope != null; e = e.next()) {
-            sym = e.sym;
-            Type origin = e.getOrigin().owner.type;
-            if (sym.kind == MTH) {
-                if (e.sym.owner.type != origin)
-                    sym = sym.clone(e.getOrigin().owner);
-                if (!isAccessible(env, origin, sym))
-                    sym = new AccessError(env, origin, sym);
-                bestSoFar = selectBest(env, origin,
+        for (Symbol currentSym : env.toplevel.starImportScope.getSymbolsByName(name)) {
+            Symbol origin = env.toplevel.starImportScope.getOrigin(currentSym).owner;
+            if (currentSym.kind == MTH) {
+                if (currentSym.owner.type != origin.type)
+                    currentSym = currentSym.clone(origin);
+                if (!isAccessible(env, origin.type, currentSym))
+                    currentSym = new AccessError(env, origin.type, currentSym);
+                bestSoFar = selectBest(env, origin.type,
                                        argtypes, typeargtypes,
-                                       sym, bestSoFar,
+                                       currentSym, bestSoFar,
                                        allowBoxing, useVarargs, false);
             }
         }
@@ -1921,14 +1913,12 @@
                                    Type site,
                                    Name name,
                                    TypeSymbol c) {
-        Scope.Entry e = c.members().lookup(name);
-        while (e.scope != null) {
-            if (e.sym.kind == TYP) {
-                return isAccessible(env, site, e.sym)
-                    ? e.sym
-                    : new AccessError(env, site, e.sym);
+        for (Symbol sym : c.members().getSymbolsByName(name)) {
+            if (sym.kind == TYP) {
+                return isAccessible(env, site, sym)
+                    ? sym
+                    : new AccessError(env, site, sym);
             }
-            e = e.next();
         }
         return typeNotFound;
     }
@@ -1995,8 +1985,8 @@
      */
     Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
         Symbol bestSoFar = typeNotFound;
-        for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
-            Symbol sym = loadClass(env, e.sym.flatName());
+        for (Symbol s : scope.getSymbolsByName(name)) {
+            Symbol sym = loadClass(env, s.flatName());
             if (bestSoFar.kind == TYP && sym.kind == TYP &&
                 bestSoFar != sym)
                 return new AmbiguityError(bestSoFar, sym);
@@ -2007,15 +1997,13 @@
     }
 
     Symbol findTypeVar(Env<AttrContext> env, Name name, boolean staticOnly) {
-        for (Scope.Entry e = env.info.scope.lookup(name);
-             e.scope != null;
-             e = e.next()) {
-            if (e.sym.kind == TYP) {
+        for (Symbol sym : env.info.scope.getSymbolsByName(name)) {
+            if (sym.kind == TYP) {
                 if (staticOnly &&
-                    e.sym.type.hasTag(TYPEVAR) &&
-                    e.sym.owner.kind == TYP)
-                    return new StaticError(e.sym);
-                return e.sym;
+                    sym.type.hasTag(TYPEVAR) &&
+                    sym.owner.kind == TYP)
+                    return new StaticError(sym);
+                return sym;
             }
         }
         return typeNotFound;
@@ -2323,42 +2311,6 @@
     }
 
 /* ***************************************************************************
- *  Debugging
- ****************************************************************************/
-
-    /** print all scopes starting with scope s and proceeding outwards.
-     *  used for debugging.
-     */
-    public void printscopes(Scope s) {
-        while (s != null) {
-            if (s.owner != null)
-                System.err.print(s.owner + ": ");
-            for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
-                if ((e.sym.flags() & ABSTRACT) != 0)
-                    System.err.print("abstract ");
-                System.err.print(e.sym + " ");
-            }
-            System.err.println();
-            s = s.next;
-        }
-    }
-
-    void printscopes(Env<AttrContext> env) {
-        while (env.outer != null) {
-            System.err.println("------------------------------");
-            printscopes(env.info.scope);
-            env = env.outer;
-        }
-    }
-
-    public void printscopes(Type t) {
-        while (t.hasTag(CLASS)) {
-            printscopes(t.tsym.members());
-            t = types.supertype(t);
-        }
-    }
-
-/* ***************************************************************************
  *  Name resolution
  *  Naming conventions are as for symbol lookup
  *  Unlike the find... methods these methods will report access errors
@@ -2456,7 +2408,7 @@
                                             List<Type> argtypes) {
         Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
                 (MethodSymbol)spMethod, currentResolutionContext, argtypes);
-        for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) {
+        for (Symbol sym : polymorphicSignatureScope.getSymbolsByName(spMethod.name)) {
             if (types.isSameType(mtype, sym.type)) {
                return sym;
             }
@@ -2629,14 +2581,11 @@
                               boolean allowBoxing,
                               boolean useVarargs) {
         Symbol bestSoFar = methodNotFound;
-        for (Scope.Entry e = site.tsym.members().lookup(names.init);
-             e.scope != null;
-             e = e.next()) {
-            final Symbol sym = e.sym;
+        for (final Symbol sym : site.tsym.members().getSymbolsByName(names.init)) {
             //- System.out.println(" e " + e.sym);
             if (sym.kind == MTH &&
                 (sym.flags_field & SYNTHETIC) == 0) {
-                    List<Type> oldParams = e.sym.type.hasTag(FORALL) ?
+                    List<Type> oldParams = sym.type.hasTag(FORALL) ?
                             ((ForAll)sym.type).tvars :
                             List.<Type>nil();
                     Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
@@ -3252,7 +3201,7 @@
 
         @Override
         protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
-            Scope sc = new Scope(syms.arrayClass);
+            WriteableScope sc = WriteableScope.create(syms.arrayClass);
             MethodSymbol arrayConstr = new MethodSymbol(PUBLIC, name, null, site.tsym);
             arrayConstr.type = new MethodType(List.<Type>of(syms.intType), site, List.<Type>nil(), syms.methodClass);
             sc.enter(arrayConstr);
@@ -3362,7 +3311,7 @@
         while (env1.outer != null) {
             if (isStatic(env1)) staticOnly = true;
             if (env1.enclClass.sym == c) {
-                Symbol sym = env1.info.scope.lookup(name).sym;
+                Symbol sym = env1.info.scope.findFirst(name);
                 if (sym != null) {
                     if (staticOnly) sym = new StaticError(sym);
                     return accessBase(sym, pos, env.enclClass.sym.type,
@@ -3449,7 +3398,7 @@
             while (env1 != null && env1.outer != null) {
                 if (isStatic(env1)) staticOnly = true;
                 if (env1.enclClass.sym.isSubClass(member.owner, types)) {
-                    Symbol sym = env1.info.scope.lookup(name).sym;
+                    Symbol sym = env1.info.scope.findFirst(name);
                     if (sym != null) {
                         if (staticOnly) sym = new StaticError(sym);
                         return sym;