duke@1: /* jjg@1357: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.code; duke@1: jjg@1357: import java.util.Iterator; jjg@1357: duke@1: import com.sun.tools.javac.util.*; duke@1: duke@1: /** A scope represents an area of visibility in a Java program. The duke@1: * Scope class is a container for symbols which provides duke@1: * efficient access to symbols given their names. Scopes are implemented jjg@738: * as hash tables with "open addressing" and "double hashing". jjg@738: * Scopes can be nested; the next field of a scope points duke@1: * to its next outer scope. Nested scopes can share their hash tables. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class Scope { duke@1: duke@1: /** The number of scopes that share this scope's hash table. duke@1: */ duke@1: private int shared; duke@1: duke@1: /** Next enclosing scope (with whom this scope may share a hashtable) duke@1: */ duke@1: public Scope next; duke@1: duke@1: /** The scope's owner. duke@1: */ duke@1: public Symbol owner; duke@1: duke@1: /** A hash table for the scope's entries. duke@1: */ jjg@738: Entry[] table; duke@1: duke@1: /** Mask for hash codes, always equal to (table.length - 1). duke@1: */ duke@1: int hashMask; duke@1: duke@1: /** A linear list that also contains all entries in duke@1: * reverse order of appearance (i.e later entries are pushed on top). duke@1: */ duke@1: public Entry elems; duke@1: duke@1: /** The number of elements in this scope. jjg@738: * This includes deleted elements, whose value is the sentinel. duke@1: */ jjg@738: int nelems = 0; duke@1: jjg@767: /** A list of scopes to be notified if items are to be removed from this scope. jjg@767: */ mcimadamore@877: List listeners = List.nil(); jjg@767: jjg@738: /** Use as a "not-found" result for lookup. jjg@738: * Also used to mark deleted entries in the table. duke@1: */ duke@1: private static final Entry sentinel = new Entry(null, null, null, null); duke@1: duke@1: /** The hash table's initial size. duke@1: */ duke@1: private static final int INITIAL_SIZE = 0x10; duke@1: duke@1: /** A value for the empty scope. duke@1: */ mcimadamore@858: public static final Scope emptyScope = new Scope(null, null, new Entry[]{}); duke@1: duke@1: /** Construct a new scope, within scope next, with given owner, using duke@1: * given table. The table's length must be an exponent of 2. duke@1: */ mcimadamore@858: private Scope(Scope next, Symbol owner, Entry[] table) { duke@1: this.next = next; jjg@816: Assert.check(emptyScope == null || owner != null); duke@1: this.owner = owner; duke@1: this.table = table; duke@1: this.hashMask = table.length - 1; duke@1: } duke@1: jjg@738: /** Convenience constructor used for dup and dupUnshared. */ mcimadamore@858: private Scope(Scope next, Symbol owner, Entry[] table, int nelems) { mcimadamore@858: this(next, owner, table); mcimadamore@858: this.nelems = nelems; jjg@738: } jjg@738: duke@1: /** Construct a new scope, within scope next, with given owner, duke@1: * using a fresh table of length INITIAL_SIZE. duke@1: */ duke@1: public Scope(Symbol owner) { mcimadamore@858: this(null, owner, new Entry[INITIAL_SIZE]); duke@1: } duke@1: duke@1: /** Construct a fresh scope within this scope, with same owner, duke@1: * which shares its table with the outer scope. Used in connection with duke@1: * method leave if scope access is stack-like in order to avoid allocation duke@1: * of fresh tables. duke@1: */ duke@1: public Scope dup() { jjg@738: return dup(this.owner); duke@1: } duke@1: duke@1: /** Construct a fresh scope within this scope, with new owner, duke@1: * which shares its table with the outer scope. Used in connection with duke@1: * method leave if scope access is stack-like in order to avoid allocation duke@1: * of fresh tables. duke@1: */ duke@1: public Scope dup(Symbol newOwner) { mcimadamore@858: Scope result = new Scope(this, newOwner, this.table, this.nelems); duke@1: shared++; duke@1: // System.out.println("====> duping scope " + this.hashCode() + " owned by " + newOwner + " to " + result.hashCode()); duke@1: // new Error().printStackTrace(System.out); duke@1: return result; duke@1: } duke@1: duke@1: /** Construct a fresh scope within this scope, with same owner, duke@1: * with a new hash table, whose contents initially are those of duke@1: * the table of its outer scope. duke@1: */ duke@1: public Scope dupUnshared() { mcimadamore@858: return new Scope(this, this.owner, this.table.clone(), this.nelems); duke@1: } duke@1: duke@1: /** Remove all entries of this scope from its table, if shared duke@1: * with next. duke@1: */ duke@1: public Scope leave() { jjg@816: Assert.check(shared == 0); duke@1: if (table != next.table) return next; duke@1: while (elems != null) { jjg@738: int hash = getIndex(elems.sym.name); duke@1: Entry e = table[hash]; jjg@816: Assert.check(e == elems, elems.sym); duke@1: table[hash] = elems.shadowed; duke@1: elems = elems.sibling; duke@1: } jjg@816: Assert.check(next.shared > 0); duke@1: next.shared--; jjg@738: next.nelems = nelems; duke@1: // System.out.println("====> leaving scope " + this.hashCode() + " owned by " + this.owner + " to " + next.hashCode()); duke@1: // new Error().printStackTrace(System.out); duke@1: return next; duke@1: } duke@1: duke@1: /** Double size of hash table. duke@1: */ duke@1: private void dble() { jjg@816: Assert.check(shared == 0); duke@1: Entry[] oldtable = table; duke@1: Entry[] newtable = new Entry[oldtable.length * 2]; duke@1: for (Scope s = this; s != null; s = s.next) { duke@1: if (s.table == oldtable) { jjg@816: Assert.check(s == this || s.shared != 0); duke@1: s.table = newtable; duke@1: s.hashMask = newtable.length - 1; duke@1: } duke@1: } jjg@738: int n = 0; jjg@738: for (int i = oldtable.length; --i >= 0; ) { jjg@738: Entry e = oldtable[i]; jjg@767: if (e != null && e != sentinel) { jjg@738: table[getIndex(e.sym.name)] = e; jjg@738: n++; jjg@738: } duke@1: } jjg@738: // We don't need to update nelems for shared inherited scopes, jjg@738: // since that gets handled by leave(). jjg@738: nelems = n; duke@1: } duke@1: duke@1: /** Enter symbol sym in this scope. duke@1: */ duke@1: public void enter(Symbol sym) { jjg@816: Assert.check(shared == 0); duke@1: enter(sym, this); duke@1: } duke@1: duke@1: public void enter(Symbol sym, Scope s) { duke@1: enter(sym, s, s); duke@1: } duke@1: duke@1: /** duke@1: * Enter symbol sym in this scope, but mark that it comes from duke@1: * given scope `s' accessed through `origin'. The last two duke@1: * arguments are only used in import scopes. duke@1: */ duke@1: public void enter(Symbol sym, Scope s, Scope origin) { jjg@816: Assert.check(shared == 0); jjg@738: if (nelems * 3 >= hashMask * 2) jjg@738: dble(); jjg@738: int hash = getIndex(sym.name); jjg@738: Entry old = table[hash]; jjg@738: if (old == null) { jjg@738: old = sentinel; jjg@738: nelems++; jjg@738: } jjg@738: Entry e = makeEntry(sym, old, elems, s, origin); duke@1: table[hash] = e; duke@1: elems = e; mcimadamore@877: mcimadamore@877: //notify listeners mcimadamore@877: for (List l = listeners; l.nonEmpty(); l = l.tail) { mcimadamore@877: l.head.symbolAdded(sym, this); mcimadamore@877: } duke@1: } duke@1: duke@1: Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) { duke@1: return new Entry(sym, shadowed, sibling, scope); duke@1: } duke@1: mcimadamore@877: mcimadamore@877: public interface ScopeListener { mcimadamore@877: public void symbolAdded(Symbol sym, Scope s); mcimadamore@877: public void symbolRemoved(Symbol sym, Scope s); mcimadamore@877: } mcimadamore@877: mcimadamore@877: public void addScopeListener(ScopeListener sl) { mcimadamore@877: listeners = listeners.prepend(sl); mcimadamore@877: } mcimadamore@877: duke@1: /** Remove symbol from this scope. Used when an inner class duke@1: * attribute tells us that the class isn't a package member. duke@1: */ duke@1: public void remove(Symbol sym) { jjg@816: Assert.check(shared == 0); duke@1: Entry e = lookup(sym.name); duke@1: if (e.scope == null) return; duke@1: duke@1: // remove e from table and shadowed list; jjg@738: int i = getIndex(sym.name); jjg@738: Entry te = table[i]; duke@1: if (te == e) jjg@738: table[i] = e.shadowed; duke@1: else while (true) { duke@1: if (te.shadowed == e) { duke@1: te.shadowed = e.shadowed; duke@1: break; duke@1: } duke@1: te = te.shadowed; duke@1: } duke@1: duke@1: // remove e from elems and sibling list duke@1: te = elems; duke@1: if (te == e) duke@1: elems = e.sibling; duke@1: else while (true) { duke@1: if (te.sibling == e) { duke@1: te.sibling = e.sibling; duke@1: break; duke@1: } duke@1: te = te.sibling; duke@1: } jjg@767: mcimadamore@877: //notify listeners mcimadamore@877: for (List l = listeners; l.nonEmpty(); l = l.tail) { mcimadamore@877: l.head.symbolRemoved(sym, this); jjg@767: } duke@1: } duke@1: duke@1: /** Enter symbol sym in this scope if not already there. duke@1: */ duke@1: public void enterIfAbsent(Symbol sym) { jjg@816: Assert.check(shared == 0); duke@1: Entry e = lookup(sym.name); duke@1: while (e.scope == this && e.sym.kind != sym.kind) e = e.next(); duke@1: if (e.scope != this) enter(sym); duke@1: } duke@1: duke@1: /** Given a class, is there already a class with same fully duke@1: * qualified name in this (import) scope? duke@1: */ duke@1: public boolean includes(Symbol c) { duke@1: for (Scope.Entry e = lookup(c.name); duke@1: e.scope == this; duke@1: e = e.next()) { duke@1: if (e.sym == c) return true; duke@1: } duke@1: return false; duke@1: } duke@1: mcimadamore@673: static final Filter noFilter = new Filter() { mcimadamore@673: public boolean accepts(Symbol s) { mcimadamore@673: return true; mcimadamore@673: } mcimadamore@673: }; mcimadamore@673: duke@1: /** Return the entry associated with given name, starting in duke@1: * this scope and proceeding outwards. If no entry was found, duke@1: * return the sentinel, which is characterized by having a null in duke@1: * both its scope and sym fields, whereas both fields are non-null duke@1: * for regular entries. duke@1: */ duke@1: public Entry lookup(Name name) { mcimadamore@673: return lookup(name, noFilter); mcimadamore@673: } mcimadamore@673: public Entry lookup(Name name, Filter sf) { jjg@738: Entry e = table[getIndex(name)]; jjg@738: if (e == null || e == sentinel) jjg@738: return sentinel; mcimadamore@673: while (e.scope != null && (e.sym.name != name || !sf.accepts(e.sym))) duke@1: e = e.shadowed; duke@1: return e; duke@1: } duke@1: jjg@738: /*void dump (java.io.PrintStream out) { jjg@738: out.println(this); jjg@738: for (int l=0; l < table.length; l++) { jjg@738: Entry le = table[l]; jjg@738: out.print("#"+l+": "); jjg@738: if (le==sentinel) out.println("sentinel"); jjg@738: else if(le == null) out.println("null"); jjg@738: else out.println(""+le+" s:"+le.sym); jjg@738: } jjg@738: }*/ jjg@738: jjg@738: /** Look for slot in the table. jjg@738: * We use open addressing with double hashing. jjg@738: */ jjg@738: int getIndex (Name name) { jjg@738: int h = name.hashCode(); jjg@738: int i = h & hashMask; jjg@738: // The expression below is always odd, so it is guaranteed jjg@767: // to be mutually prime with table.length, a power of 2. jjg@738: int x = hashMask - ((h + (h >> 16)) << 1); jjg@738: int d = -1; // Index of a deleted item. jjg@738: for (;;) { jjg@738: Entry e = table[i]; jjg@738: if (e == null) jjg@738: return d >= 0 ? d : i; jjg@738: if (e == sentinel) { jjg@738: // We have to keep searching even if we see a deleted item. jjg@738: // However, remember the index in case we fail to find the name. jjg@738: if (d < 0) jjg@738: d = i; jjg@738: } else if (e.sym.name == name) jjg@738: return i; jjg@738: i = (i + x) & hashMask; jjg@738: } jjg@738: } jjg@738: duke@1: public Iterable getElements() { mcimadamore@673: return getElements(noFilter); mcimadamore@673: } mcimadamore@673: mcimadamore@673: public Iterable getElements(final Filter sf) { duke@1: return new Iterable() { duke@1: public Iterator iterator() { duke@1: return new Iterator() { duke@1: private Scope currScope = Scope.this; duke@1: private Scope.Entry currEntry = elems; duke@1: { duke@1: update(); duke@1: } duke@1: duke@1: public boolean hasNext() { duke@1: return currEntry != null; duke@1: } duke@1: duke@1: public Symbol next() { duke@1: Symbol sym = (currEntry == null ? null : currEntry.sym); mcimadamore@673: if (currEntry != null) { mcimadamore@673: currEntry = currEntry.sibling; mcimadamore@673: } duke@1: update(); duke@1: return sym; duke@1: } duke@1: duke@1: public void remove() { duke@1: throw new UnsupportedOperationException(); duke@1: } duke@1: duke@1: private void update() { mcimadamore@673: skipToNextMatchingEntry(); duke@1: while (currEntry == null && currScope.next != null) { duke@1: currScope = currScope.next; duke@1: currEntry = currScope.elems; mcimadamore@673: skipToNextMatchingEntry(); mcimadamore@673: } mcimadamore@673: } mcimadamore@673: mcimadamore@673: void skipToNextMatchingEntry() { mcimadamore@673: while (currEntry != null && !sf.accepts(currEntry.sym)) { mcimadamore@673: currEntry = currEntry.sibling; duke@1: } duke@1: } duke@1: }; duke@1: } duke@1: }; mcimadamore@877: } duke@1: mcimadamore@877: public Iterable getElementsByName(Name name) { mcimadamore@877: return getElementsByName(name, noFilter); mcimadamore@877: } mcimadamore@877: mcimadamore@877: public Iterable getElementsByName(final Name name, final Filter sf) { mcimadamore@877: return new Iterable() { mcimadamore@877: public Iterator iterator() { mcimadamore@877: return new Iterator() { mcimadamore@877: Scope.Entry currentEntry = lookup(name, sf); mcimadamore@877: mcimadamore@877: public boolean hasNext() { mcimadamore@877: return currentEntry.scope != null; mcimadamore@877: } mcimadamore@877: public Symbol next() { mcimadamore@877: Scope.Entry prevEntry = currentEntry; mcimadamore@877: currentEntry = currentEntry.next(sf); mcimadamore@877: return prevEntry.sym; mcimadamore@877: } mcimadamore@877: public void remove() { mcimadamore@877: throw new UnsupportedOperationException(); mcimadamore@877: } mcimadamore@877: }; mcimadamore@877: } mcimadamore@877: }; duke@1: } duke@1: duke@1: public String toString() { duke@1: StringBuilder result = new StringBuilder(); duke@1: result.append("Scope["); duke@1: for (Scope s = this; s != null ; s = s.next) { duke@1: if (s != this) result.append(" | "); duke@1: for (Entry e = s.elems; e != null; e = e.sibling) { duke@1: if (e != s.elems) result.append(", "); duke@1: result.append(e.sym); duke@1: } duke@1: } duke@1: result.append("]"); duke@1: return result.toString(); duke@1: } duke@1: duke@1: /** A class for scope entries. duke@1: */ duke@1: public static class Entry { duke@1: duke@1: /** The referenced symbol. duke@1: * sym == null iff this == sentinel duke@1: */ duke@1: public Symbol sym; duke@1: duke@1: /** An entry with the same hash code, or sentinel. duke@1: */ duke@1: private Entry shadowed; duke@1: duke@1: /** Next entry in same scope. duke@1: */ duke@1: public Entry sibling; duke@1: duke@1: /** The entry's scope. duke@1: * scope == null iff this == sentinel duke@1: * for an entry in an import scope, this is the scope duke@1: * where the entry came from (i.e. was imported from). duke@1: */ duke@1: public Scope scope; duke@1: duke@1: public Entry(Symbol sym, Entry shadowed, Entry sibling, Scope scope) { duke@1: this.sym = sym; duke@1: this.shadowed = shadowed; duke@1: this.sibling = sibling; duke@1: this.scope = scope; duke@1: } duke@1: duke@1: /** Return next entry with the same name as this entry, proceeding duke@1: * outwards if not found in this scope. duke@1: */ duke@1: public Entry next() { jjg@738: return shadowed; duke@1: } duke@1: mcimadamore@780: public Entry next(Filter sf) { mcimadamore@780: if (shadowed.sym == null || sf.accepts(shadowed.sym)) return shadowed; mcimadamore@780: else return shadowed.next(sf); mcimadamore@780: } mcimadamore@780: duke@1: public Scope getOrigin() { duke@1: // The origin is only recorded for import scopes. For all duke@1: // other scope entries, the "enclosing" type is available duke@1: // from other sources. See Attr.visitSelect and duke@1: // Attr.visitIdent. Rather than throwing an assertion duke@1: // error, we return scope which will be the same as origin duke@1: // in many cases. duke@1: return scope; duke@1: } duke@1: } duke@1: duke@1: public static class ImportScope extends Scope { duke@1: duke@1: public ImportScope(Symbol owner) { duke@1: super(owner); duke@1: } duke@1: duke@1: @Override duke@1: Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) { duke@1: return new ImportEntry(sym, shadowed, sibling, scope, origin); duke@1: } duke@1: duke@1: static class ImportEntry extends Entry { duke@1: private Scope origin; duke@1: duke@1: ImportEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) { duke@1: super(sym, shadowed, sibling, scope); duke@1: this.origin = origin; duke@1: } duke@1: duke@1: @Override duke@1: public Scope getOrigin() { return origin; } jjg@767: } jjg@767: } jjg@738: mcimadamore@877: public static class StarImportScope extends ImportScope implements ScopeListener { jjg@767: jjg@767: public StarImportScope(Symbol owner) { jjg@767: super(owner); jjg@767: } jjg@767: jjg@767: public void importAll (Scope fromScope) { jjg@767: for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { jjg@767: if (e.sym.kind == Kinds.TYP && !includes(e.sym)) jjg@767: enter(e.sym, fromScope); jjg@767: } jjg@767: // Register to be notified when imported items are removed mcimadamore@877: fromScope.addScopeListener(this); duke@1: } mcimadamore@877: mcimadamore@877: public void symbolRemoved(Symbol sym, Scope s) { mcimadamore@877: remove(sym); mcimadamore@877: } mcimadamore@877: public void symbolAdded(Symbol sym, Scope s) { } duke@1: } duke@1: duke@1: /** An empty scope, into which you can't place anything. Used for duke@1: * the scope for a variable initializer. duke@1: */ duke@1: public static class DelegatedScope extends Scope { duke@1: Scope delegatee; duke@1: public static final Entry[] emptyTable = new Entry[0]; duke@1: duke@1: public DelegatedScope(Scope outer) { mcimadamore@858: super(outer, outer.owner, emptyTable); duke@1: delegatee = outer; duke@1: } duke@1: public Scope dup() { duke@1: return new DelegatedScope(next); duke@1: } duke@1: public Scope dupUnshared() { duke@1: return new DelegatedScope(next); duke@1: } duke@1: public Scope leave() { duke@1: return next; duke@1: } duke@1: public void enter(Symbol sym) { duke@1: // only anonymous classes could be put here duke@1: } duke@1: public void enter(Symbol sym, Scope s) { duke@1: // only anonymous classes could be put here duke@1: } duke@1: public void remove(Symbol sym) { duke@1: throw new AssertionError(sym); duke@1: } duke@1: public Entry lookup(Name name) { duke@1: return delegatee.lookup(name); duke@1: } duke@1: } duke@1: mcimadamore@877: /** A class scope adds capabilities to keep track of changes in related mcimadamore@877: * class scopes - this allows client to realize whether a class scope mcimadamore@877: * has changed, either directly (because a new member has been added/removed mcimadamore@877: * to this scope) or indirectly (i.e. because a new member has been mcimadamore@877: * added/removed into a supertype scope) mcimadamore@877: */ mcimadamore@877: public static class CompoundScope extends Scope implements ScopeListener { mcimadamore@877: mcimadamore@877: public static final Entry[] emptyTable = new Entry[0]; mcimadamore@877: mcimadamore@877: private List subScopes = List.nil(); mcimadamore@877: private int mark = 0; mcimadamore@877: mcimadamore@877: public CompoundScope(Symbol owner) { mcimadamore@877: super(null, owner, emptyTable); mcimadamore@877: } mcimadamore@877: mcimadamore@877: public void addSubScope(Scope that) { mcimadamore@877: if (that != null) { mcimadamore@877: subScopes = subScopes.prepend(that); mcimadamore@877: that.addScopeListener(this); mcimadamore@877: mark++; mcimadamore@877: for (ScopeListener sl : listeners) { mcimadamore@877: sl.symbolAdded(null, this); //propagate upwards in case of nested CompoundScopes mcimadamore@877: } mcimadamore@877: } mcimadamore@877: } mcimadamore@877: mcimadamore@877: public void symbolAdded(Symbol sym, Scope s) { mcimadamore@877: mark++; mcimadamore@877: for (ScopeListener sl : listeners) { mcimadamore@877: sl.symbolAdded(sym, s); mcimadamore@877: } mcimadamore@877: } mcimadamore@877: mcimadamore@877: public void symbolRemoved(Symbol sym, Scope s) { mcimadamore@877: mark++; mcimadamore@877: for (ScopeListener sl : listeners) { mcimadamore@877: sl.symbolRemoved(sym, s); mcimadamore@877: } mcimadamore@877: } mcimadamore@877: mcimadamore@877: public int getMark() { mcimadamore@877: return mark; mcimadamore@877: } mcimadamore@877: mcimadamore@877: @Override mcimadamore@877: public String toString() { mcimadamore@877: StringBuilder buf = new StringBuilder(); mcimadamore@877: buf.append("CompoundScope{"); mcimadamore@877: String sep = ""; mcimadamore@877: for (Scope s : subScopes) { mcimadamore@877: buf.append(sep); mcimadamore@877: buf.append(s); mcimadamore@877: sep = ","; mcimadamore@877: } mcimadamore@877: buf.append("}"); mcimadamore@877: return buf.toString(); mcimadamore@877: } mcimadamore@877: mcimadamore@877: @Override mcimadamore@877: public Iterable getElements(final Filter sf) { mcimadamore@877: return new Iterable() { mcimadamore@877: public Iterator iterator() { mcimadamore@877: return new CompoundScopeIterator(subScopes) { mcimadamore@877: Iterator nextIterator(Scope s) { mcimadamore@982: return s.getElements(sf).iterator(); mcimadamore@877: } mcimadamore@877: }; mcimadamore@877: } mcimadamore@877: }; mcimadamore@877: } mcimadamore@877: mcimadamore@877: @Override mcimadamore@877: public Iterable getElementsByName(final Name name, final Filter sf) { mcimadamore@877: return new Iterable() { mcimadamore@877: public Iterator iterator() { mcimadamore@877: return new CompoundScopeIterator(subScopes) { mcimadamore@877: Iterator nextIterator(Scope s) { mcimadamore@877: return s.getElementsByName(name, sf).iterator(); mcimadamore@877: } mcimadamore@877: }; mcimadamore@877: } mcimadamore@877: }; mcimadamore@877: } mcimadamore@877: mcimadamore@877: abstract class CompoundScopeIterator implements Iterator { mcimadamore@877: mcimadamore@877: private Iterator currentIterator; mcimadamore@877: private List scopesToScan; mcimadamore@877: mcimadamore@877: public CompoundScopeIterator(List scopesToScan) { mcimadamore@877: this.scopesToScan = scopesToScan; mcimadamore@877: update(); mcimadamore@877: } mcimadamore@877: mcimadamore@877: abstract Iterator nextIterator(Scope s); mcimadamore@877: mcimadamore@877: public boolean hasNext() { mcimadamore@877: return currentIterator != null; mcimadamore@877: } mcimadamore@877: mcimadamore@877: public Symbol next() { mcimadamore@877: Symbol sym = currentIterator.next(); mcimadamore@877: if (!currentIterator.hasNext()) { mcimadamore@877: update(); mcimadamore@877: } mcimadamore@877: return sym; mcimadamore@877: } mcimadamore@877: mcimadamore@877: public void remove() { mcimadamore@877: throw new UnsupportedOperationException(); mcimadamore@877: } mcimadamore@877: mcimadamore@877: private void update() { mcimadamore@877: while (scopesToScan.nonEmpty()) { mcimadamore@877: currentIterator = nextIterator(scopesToScan.head); mcimadamore@877: scopesToScan = scopesToScan.tail; mcimadamore@877: if (currentIterator.hasNext()) return; mcimadamore@877: } mcimadamore@877: currentIterator = null; mcimadamore@877: } mcimadamore@877: } mcimadamore@877: mcimadamore@877: @Override mcimadamore@877: public Entry lookup(Name name, Filter sf) { mcimadamore@877: throw new UnsupportedOperationException(); mcimadamore@877: } mcimadamore@877: mcimadamore@877: @Override mcimadamore@877: public Scope dup(Symbol newOwner) { mcimadamore@877: throw new UnsupportedOperationException(); mcimadamore@877: } mcimadamore@877: mcimadamore@877: @Override mcimadamore@877: public void enter(Symbol sym, Scope s, Scope origin) { mcimadamore@877: throw new UnsupportedOperationException(); mcimadamore@877: } mcimadamore@877: mcimadamore@877: @Override mcimadamore@877: public void remove(Symbol sym) { mcimadamore@877: throw new UnsupportedOperationException(); mcimadamore@877: } mcimadamore@877: } mcimadamore@877: duke@1: /** An error scope, for which the owner should be an error symbol. */ duke@1: public static class ErrorScope extends Scope { duke@1: ErrorScope(Scope next, Symbol errSymbol, Entry[] table) { mcimadamore@858: super(next, /*owner=*/errSymbol, table); duke@1: } duke@1: public ErrorScope(Symbol errSymbol) { duke@1: super(errSymbol); duke@1: } duke@1: public Scope dup() { duke@1: return new ErrorScope(this, owner, table); duke@1: } duke@1: public Scope dupUnshared() { duke@1: return new ErrorScope(this, owner, table.clone()); duke@1: } duke@1: public Entry lookup(Name name) { duke@1: Entry e = super.lookup(name); duke@1: if (e.scope == null) duke@1: return new Entry(owner, null, null, null); duke@1: else duke@1: return e; duke@1: } duke@1: } duke@1: }