aoqi@0: /*
sadayapalam@2812: * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.javac.code;
aoqi@0:
aoqi@0: import java.util.Iterator;
aoqi@0:
aoqi@0: import com.sun.tools.javac.util.*;
aoqi@0:
aoqi@0: /** A scope represents an area of visibility in a Java program. The
aoqi@0: * Scope class is a container for symbols which provides
aoqi@0: * efficient access to symbols given their names. Scopes are implemented
aoqi@0: * as hash tables with "open addressing" and "double hashing".
aoqi@0: * Scopes can be nested; the next field of a scope points
aoqi@0: * to its next outer scope. Nested scopes can share their hash tables.
aoqi@0: *
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own risk.
aoqi@0: * This code and its internal interfaces are subject to change or
aoqi@0: * deletion without notice.
aoqi@0: */
aoqi@0: public class Scope {
aoqi@0:
aoqi@0: /** The number of scopes that share this scope's hash table.
aoqi@0: */
aoqi@0: private int shared;
aoqi@0:
aoqi@0: /** Next enclosing scope (with whom this scope may share a hashtable)
aoqi@0: */
aoqi@0: public Scope next;
aoqi@0:
aoqi@0: /** The scope's owner.
aoqi@0: */
aoqi@0: public Symbol owner;
aoqi@0:
aoqi@0: /** A hash table for the scope's entries.
aoqi@0: */
aoqi@0: Entry[] table;
aoqi@0:
aoqi@0: /** Mask for hash codes, always equal to (table.length - 1).
aoqi@0: */
aoqi@0: int hashMask;
aoqi@0:
aoqi@0: /** A linear list that also contains all entries in
aoqi@0: * reverse order of appearance (i.e later entries are pushed on top).
aoqi@0: */
aoqi@0: public Entry elems;
aoqi@0:
aoqi@0: /** The number of elements in this scope.
aoqi@0: * This includes deleted elements, whose value is the sentinel.
aoqi@0: */
aoqi@0: int nelems = 0;
aoqi@0:
aoqi@0: /** A list of scopes to be notified if items are to be removed from this scope.
aoqi@0: */
aoqi@0: List listeners = List.nil();
aoqi@0:
aoqi@0: /** Use as a "not-found" result for lookup.
aoqi@0: * Also used to mark deleted entries in the table.
aoqi@0: */
aoqi@0: private static final Entry sentinel = new Entry(null, null, null, null);
aoqi@0:
aoqi@0: /** The hash table's initial size.
aoqi@0: */
aoqi@0: private static final int INITIAL_SIZE = 0x10;
aoqi@0:
aoqi@0: /** A value for the empty scope.
aoqi@0: */
aoqi@0: public static final Scope emptyScope = new Scope(null, null, new Entry[]{});
aoqi@0:
aoqi@0: /** Construct a new scope, within scope next, with given owner, using
aoqi@0: * given table. The table's length must be an exponent of 2.
aoqi@0: */
aoqi@0: private Scope(Scope next, Symbol owner, Entry[] table) {
aoqi@0: this.next = next;
aoqi@0: Assert.check(emptyScope == null || owner != null);
aoqi@0: this.owner = owner;
aoqi@0: this.table = table;
aoqi@0: this.hashMask = table.length - 1;
aoqi@0: }
aoqi@0:
aoqi@0: /** Convenience constructor used for dup and dupUnshared. */
aoqi@0: private Scope(Scope next, Symbol owner, Entry[] table, int nelems) {
aoqi@0: this(next, owner, table);
aoqi@0: this.nelems = nelems;
aoqi@0: }
aoqi@0:
aoqi@0: /** Construct a new scope, within scope next, with given owner,
aoqi@0: * using a fresh table of length INITIAL_SIZE.
aoqi@0: */
aoqi@0: public Scope(Symbol owner) {
aoqi@0: this(null, owner, new Entry[INITIAL_SIZE]);
aoqi@0: }
aoqi@0:
aoqi@0: /** Construct a fresh scope within this scope, with same owner,
aoqi@0: * which shares its table with the outer scope. Used in connection with
aoqi@0: * method leave if scope access is stack-like in order to avoid allocation
aoqi@0: * of fresh tables.
aoqi@0: */
aoqi@0: public Scope dup() {
aoqi@0: return dup(this.owner);
aoqi@0: }
aoqi@0:
aoqi@0: /** Construct a fresh scope within this scope, with new owner,
aoqi@0: * which shares its table with the outer scope. Used in connection with
aoqi@0: * method leave if scope access is stack-like in order to avoid allocation
aoqi@0: * of fresh tables.
aoqi@0: */
aoqi@0: public Scope dup(Symbol newOwner) {
aoqi@0: Scope result = new Scope(this, newOwner, this.table, this.nelems);
aoqi@0: shared++;
aoqi@0: // System.out.println("====> duping scope " + this.hashCode() + " owned by " + newOwner + " to " + result.hashCode());
aoqi@0: // new Error().printStackTrace(System.out);
aoqi@0: return result;
aoqi@0: }
aoqi@0:
aoqi@0: /** Construct a fresh scope within this scope, with same owner,
aoqi@0: * with a new hash table, whose contents initially are those of
aoqi@0: * the table of its outer scope.
aoqi@0: */
aoqi@0: public Scope dupUnshared() {
aoqi@0: return new Scope(this, this.owner, this.table.clone(), this.nelems);
aoqi@0: }
aoqi@0:
aoqi@0: /** Remove all entries of this scope from its table, if shared
aoqi@0: * with next.
aoqi@0: */
aoqi@0: public Scope leave() {
aoqi@0: Assert.check(shared == 0);
aoqi@0: if (table != next.table) return next;
aoqi@0: while (elems != null) {
aoqi@0: int hash = getIndex(elems.sym.name);
aoqi@0: Entry e = table[hash];
aoqi@0: Assert.check(e == elems, elems.sym);
aoqi@0: table[hash] = elems.shadowed;
aoqi@0: elems = elems.sibling;
aoqi@0: }
aoqi@0: Assert.check(next.shared > 0);
aoqi@0: next.shared--;
aoqi@0: next.nelems = nelems;
aoqi@0: // System.out.println("====> leaving scope " + this.hashCode() + " owned by " + this.owner + " to " + next.hashCode());
aoqi@0: // new Error().printStackTrace(System.out);
aoqi@0: return next;
aoqi@0: }
aoqi@0:
aoqi@0: /** Double size of hash table.
aoqi@0: */
aoqi@0: private void dble() {
aoqi@0: Assert.check(shared == 0);
aoqi@0: Entry[] oldtable = table;
aoqi@0: Entry[] newtable = new Entry[oldtable.length * 2];
aoqi@0: for (Scope s = this; s != null; s = s.next) {
aoqi@0: if (s.table == oldtable) {
aoqi@0: Assert.check(s == this || s.shared != 0);
aoqi@0: s.table = newtable;
aoqi@0: s.hashMask = newtable.length - 1;
aoqi@0: }
aoqi@0: }
aoqi@0: int n = 0;
aoqi@0: for (int i = oldtable.length; --i >= 0; ) {
aoqi@0: Entry e = oldtable[i];
aoqi@0: if (e != null && e != sentinel) {
aoqi@0: table[getIndex(e.sym.name)] = e;
aoqi@0: n++;
aoqi@0: }
aoqi@0: }
aoqi@0: // We don't need to update nelems for shared inherited scopes,
aoqi@0: // since that gets handled by leave().
aoqi@0: nelems = n;
aoqi@0: }
aoqi@0:
aoqi@0: /** Enter symbol sym in this scope.
aoqi@0: */
aoqi@0: public void enter(Symbol sym) {
aoqi@0: Assert.check(shared == 0);
aoqi@0: enter(sym, this);
aoqi@0: }
aoqi@0:
aoqi@0: public void enter(Symbol sym, Scope s) {
aoqi@0: enter(sym, s, s, false);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Enter symbol sym in this scope, but mark that it comes from
aoqi@0: * given scope `s' accessed through `origin'. The last two
aoqi@0: * arguments are only used in import scopes.
aoqi@0: */
aoqi@0: public void enter(Symbol sym, Scope s, Scope origin, boolean staticallyImported) {
aoqi@0: Assert.check(shared == 0);
aoqi@0: if (nelems * 3 >= hashMask * 2)
aoqi@0: dble();
aoqi@0: int hash = getIndex(sym.name);
aoqi@0: Entry old = table[hash];
aoqi@0: if (old == null) {
aoqi@0: old = sentinel;
aoqi@0: nelems++;
aoqi@0: }
aoqi@0: Entry e = makeEntry(sym, old, elems, s, origin, staticallyImported);
aoqi@0: table[hash] = e;
aoqi@0: elems = e;
aoqi@0:
aoqi@0: //notify listeners
aoqi@0: for (List l = listeners; l.nonEmpty(); l = l.tail) {
aoqi@0: l.head.symbolAdded(sym, this);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin, boolean staticallyImported) {
aoqi@0: return new Entry(sym, shadowed, sibling, scope);
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: public interface ScopeListener {
aoqi@0: public void symbolAdded(Symbol sym, Scope s);
aoqi@0: public void symbolRemoved(Symbol sym, Scope s);
aoqi@0: }
aoqi@0:
aoqi@0: public void addScopeListener(ScopeListener sl) {
aoqi@0: listeners = listeners.prepend(sl);
aoqi@0: }
aoqi@0:
sadayapalam@2812: /** Remove symbol from this scope.
aoqi@0: */
sadayapalam@2812: public void remove(final Symbol sym) {
aoqi@0: Assert.check(shared == 0);
sadayapalam@2812: Entry e = lookup(sym.name, new Filter() {
sadayapalam@2812: @Override
sadayapalam@2812: public boolean accepts(Symbol candidate) {
sadayapalam@2812: return candidate == sym;
sadayapalam@2812: }
sadayapalam@2812: });
aoqi@0: if (e.scope == null) return;
aoqi@0:
aoqi@0: // remove e from table and shadowed list;
aoqi@0: int i = getIndex(sym.name);
aoqi@0: Entry te = table[i];
aoqi@0: if (te == e)
aoqi@0: table[i] = e.shadowed;
aoqi@0: else while (true) {
aoqi@0: if (te.shadowed == e) {
aoqi@0: te.shadowed = e.shadowed;
aoqi@0: break;
aoqi@0: }
aoqi@0: te = te.shadowed;
aoqi@0: }
aoqi@0:
aoqi@0: // remove e from elems and sibling list
aoqi@0: te = elems;
aoqi@0: if (te == e)
aoqi@0: elems = e.sibling;
aoqi@0: else while (true) {
aoqi@0: if (te.sibling == e) {
aoqi@0: te.sibling = e.sibling;
aoqi@0: break;
aoqi@0: }
aoqi@0: te = te.sibling;
aoqi@0: }
aoqi@0:
aoqi@0: //notify listeners
aoqi@0: for (List l = listeners; l.nonEmpty(); l = l.tail) {
aoqi@0: l.head.symbolRemoved(sym, this);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Enter symbol sym in this scope if not already there.
aoqi@0: */
aoqi@0: public void enterIfAbsent(Symbol sym) {
aoqi@0: Assert.check(shared == 0);
aoqi@0: Entry e = lookup(sym.name);
aoqi@0: while (e.scope == this && e.sym.kind != sym.kind) e = e.next();
aoqi@0: if (e.scope != this) enter(sym);
aoqi@0: }
aoqi@0:
aoqi@0: /** Given a class, is there already a class with same fully
aoqi@0: * qualified name in this (import) scope?
aoqi@0: */
aoqi@0: public boolean includes(Symbol c) {
aoqi@0: for (Scope.Entry e = lookup(c.name);
aoqi@0: e.scope == this;
aoqi@0: e = e.next()) {
aoqi@0: if (e.sym == c) return true;
aoqi@0: }
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: static final Filter noFilter = new Filter() {
aoqi@0: public boolean accepts(Symbol s) {
aoqi@0: return true;
aoqi@0: }
aoqi@0: };
aoqi@0:
aoqi@0: /** Return the entry associated with given name, starting in
aoqi@0: * this scope and proceeding outwards. If no entry was found,
aoqi@0: * return the sentinel, which is characterized by having a null in
aoqi@0: * both its scope and sym fields, whereas both fields are non-null
aoqi@0: * for regular entries.
aoqi@0: */
aoqi@0: public Entry lookup(Name name) {
aoqi@0: return lookup(name, noFilter);
aoqi@0: }
aoqi@0:
aoqi@0: public Entry lookup(Name name, Filter sf) {
aoqi@0: Entry e = table[getIndex(name)];
aoqi@0: if (e == null || e == sentinel)
aoqi@0: return sentinel;
aoqi@0: while (e.scope != null && (e.sym.name != name || !sf.accepts(e.sym)))
aoqi@0: e = e.shadowed;
aoqi@0: return e;
aoqi@0: }
aoqi@0:
aoqi@0: /*void dump (java.io.PrintStream out) {
aoqi@0: out.println(this);
aoqi@0: for (int l=0; l < table.length; l++) {
aoqi@0: Entry le = table[l];
aoqi@0: out.print("#"+l+": ");
aoqi@0: if (le==sentinel) out.println("sentinel");
aoqi@0: else if(le == null) out.println("null");
aoqi@0: else out.println(""+le+" s:"+le.sym);
aoqi@0: }
aoqi@0: }*/
aoqi@0:
aoqi@0: /** Look for slot in the table.
aoqi@0: * We use open addressing with double hashing.
aoqi@0: */
aoqi@0: int getIndex (Name name) {
aoqi@0: int h = name.hashCode();
aoqi@0: int i = h & hashMask;
aoqi@0: // The expression below is always odd, so it is guaranteed
aoqi@0: // to be mutually prime with table.length, a power of 2.
aoqi@0: int x = hashMask - ((h + (h >> 16)) << 1);
aoqi@0: int d = -1; // Index of a deleted item.
aoqi@0: for (;;) {
aoqi@0: Entry e = table[i];
aoqi@0: if (e == null)
aoqi@0: return d >= 0 ? d : i;
aoqi@0: if (e == sentinel) {
aoqi@0: // We have to keep searching even if we see a deleted item.
aoqi@0: // However, remember the index in case we fail to find the name.
aoqi@0: if (d < 0)
aoqi@0: d = i;
aoqi@0: } else if (e.sym.name == name)
aoqi@0: return i;
aoqi@0: i = (i + x) & hashMask;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public boolean anyMatch(Filter sf) {
aoqi@0: return getElements(sf).iterator().hasNext();
aoqi@0: }
aoqi@0:
aoqi@0: public Iterable getElements() {
aoqi@0: return getElements(noFilter);
aoqi@0: }
aoqi@0:
aoqi@0: public Iterable getElements(final Filter sf) {
aoqi@0: return new Iterable() {
aoqi@0: public Iterator iterator() {
aoqi@0: return new Iterator() {
aoqi@0: private Scope currScope = Scope.this;
aoqi@0: private Scope.Entry currEntry = elems;
aoqi@0: {
aoqi@0: update();
aoqi@0: }
aoqi@0:
aoqi@0: public boolean hasNext() {
aoqi@0: return currEntry != null;
aoqi@0: }
aoqi@0:
aoqi@0: public Symbol next() {
aoqi@0: Symbol sym = (currEntry == null ? null : currEntry.sym);
aoqi@0: if (currEntry != null) {
aoqi@0: currEntry = currEntry.sibling;
aoqi@0: }
aoqi@0: update();
aoqi@0: return sym;
aoqi@0: }
aoqi@0:
aoqi@0: public void remove() {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: private void update() {
aoqi@0: skipToNextMatchingEntry();
aoqi@0: while (currEntry == null && currScope.next != null) {
aoqi@0: currScope = currScope.next;
aoqi@0: currEntry = currScope.elems;
aoqi@0: skipToNextMatchingEntry();
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: void skipToNextMatchingEntry() {
aoqi@0: while (currEntry != null && !sf.accepts(currEntry.sym)) {
aoqi@0: currEntry = currEntry.sibling;
aoqi@0: }
aoqi@0: }
aoqi@0: };
aoqi@0: }
aoqi@0: };
aoqi@0: }
aoqi@0:
aoqi@0: public Iterable getElementsByName(Name name) {
aoqi@0: return getElementsByName(name, noFilter);
aoqi@0: }
aoqi@0:
aoqi@0: public Iterable getElementsByName(final Name name, final Filter sf) {
aoqi@0: return new Iterable() {
aoqi@0: public Iterator iterator() {
aoqi@0: return new Iterator() {
aoqi@0: Scope.Entry currentEntry = lookup(name, sf);
aoqi@0:
aoqi@0: public boolean hasNext() {
aoqi@0: return currentEntry.scope != null;
aoqi@0: }
aoqi@0: public Symbol next() {
aoqi@0: Scope.Entry prevEntry = currentEntry;
aoqi@0: currentEntry = currentEntry.next(sf);
aoqi@0: return prevEntry.sym;
aoqi@0: }
aoqi@0: public void remove() {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0: };
aoqi@0: }
aoqi@0: };
aoqi@0: }
aoqi@0:
aoqi@0: public String toString() {
aoqi@0: StringBuilder result = new StringBuilder();
aoqi@0: result.append("Scope[");
aoqi@0: for (Scope s = this; s != null ; s = s.next) {
aoqi@0: if (s != this) result.append(" | ");
aoqi@0: for (Entry e = s.elems; e != null; e = e.sibling) {
aoqi@0: if (e != s.elems) result.append(", ");
aoqi@0: result.append(e.sym);
aoqi@0: }
aoqi@0: }
aoqi@0: result.append("]");
aoqi@0: return result.toString();
aoqi@0: }
aoqi@0:
aoqi@0: /** A class for scope entries.
aoqi@0: */
aoqi@0: public static class Entry {
aoqi@0:
aoqi@0: /** The referenced symbol.
aoqi@0: * sym == null iff this == sentinel
aoqi@0: */
aoqi@0: public Symbol sym;
aoqi@0:
aoqi@0: /** An entry with the same hash code, or sentinel.
aoqi@0: */
aoqi@0: private Entry shadowed;
aoqi@0:
aoqi@0: /** Next entry in same scope.
aoqi@0: */
aoqi@0: public Entry sibling;
aoqi@0:
aoqi@0: /** The entry's scope.
aoqi@0: * scope == null iff this == sentinel
aoqi@0: * for an entry in an import scope, this is the scope
aoqi@0: * where the entry came from (i.e. was imported from).
aoqi@0: */
aoqi@0: public Scope scope;
aoqi@0:
aoqi@0: public Entry(Symbol sym, Entry shadowed, Entry sibling, Scope scope) {
aoqi@0: this.sym = sym;
aoqi@0: this.shadowed = shadowed;
aoqi@0: this.sibling = sibling;
aoqi@0: this.scope = scope;
aoqi@0: }
aoqi@0:
aoqi@0: /** Return next entry with the same name as this entry, proceeding
aoqi@0: * outwards if not found in this scope.
aoqi@0: */
aoqi@0: public Entry next() {
aoqi@0: return shadowed;
aoqi@0: }
aoqi@0:
aoqi@0: public Entry next(Filter sf) {
aoqi@0: if (shadowed.sym == null || sf.accepts(shadowed.sym)) return shadowed;
aoqi@0: else return shadowed.next(sf);
aoqi@0: }
aoqi@0:
aoqi@0: public boolean isStaticallyImported() {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: public Scope getOrigin() {
aoqi@0: // The origin is only recorded for import scopes. For all
aoqi@0: // other scope entries, the "enclosing" type is available
aoqi@0: // from other sources. See Attr.visitSelect and
aoqi@0: // Attr.visitIdent. Rather than throwing an assertion
aoqi@0: // error, we return scope which will be the same as origin
aoqi@0: // in many cases.
aoqi@0: return scope;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public static class ImportScope extends Scope {
aoqi@0:
aoqi@0: public ImportScope(Symbol owner) {
aoqi@0: super(owner);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope,
aoqi@0: final Scope origin, final boolean staticallyImported) {
aoqi@0: return new Entry(sym, shadowed, sibling, scope) {
aoqi@0: @Override
aoqi@0: public Scope getOrigin() {
aoqi@0: return origin;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public boolean isStaticallyImported() {
aoqi@0: return staticallyImported;
aoqi@0: }
aoqi@0: };
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public static class StarImportScope extends ImportScope implements ScopeListener {
aoqi@0:
aoqi@0: public StarImportScope(Symbol owner) {
aoqi@0: super(owner);
aoqi@0: }
aoqi@0:
aoqi@0: public void importAll (Scope fromScope) {
aoqi@0: for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
aoqi@0: if (e.sym.kind == Kinds.TYP && !includes(e.sym))
aoqi@0: enter(e.sym, fromScope);
aoqi@0: }
aoqi@0: // Register to be notified when imported items are removed
aoqi@0: fromScope.addScopeListener(this);
aoqi@0: }
aoqi@0:
aoqi@0: public void symbolRemoved(Symbol sym, Scope s) {
aoqi@0: remove(sym);
aoqi@0: }
aoqi@0: public void symbolAdded(Symbol sym, Scope s) { }
aoqi@0: }
aoqi@0:
aoqi@0: /** An empty scope, into which you can't place anything. Used for
aoqi@0: * the scope for a variable initializer.
aoqi@0: */
aoqi@0: public static class DelegatedScope extends Scope {
aoqi@0: Scope delegatee;
aoqi@0: public static final Entry[] emptyTable = new Entry[0];
aoqi@0:
aoqi@0: public DelegatedScope(Scope outer) {
aoqi@0: super(outer, outer.owner, emptyTable);
aoqi@0: delegatee = outer;
aoqi@0: }
aoqi@0: public Scope dup() {
aoqi@0: return new DelegatedScope(next);
aoqi@0: }
aoqi@0: public Scope dupUnshared() {
aoqi@0: return new DelegatedScope(next);
aoqi@0: }
aoqi@0: public Scope leave() {
aoqi@0: return next;
aoqi@0: }
aoqi@0: public void enter(Symbol sym) {
aoqi@0: // only anonymous classes could be put here
aoqi@0: }
aoqi@0: public void enter(Symbol sym, Scope s) {
aoqi@0: // only anonymous classes could be put here
aoqi@0: }
aoqi@0: public void remove(Symbol sym) {
aoqi@0: throw new AssertionError(sym);
aoqi@0: }
aoqi@0: public Entry lookup(Name name) {
aoqi@0: return delegatee.lookup(name);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** A class scope adds capabilities to keep track of changes in related
aoqi@0: * class scopes - this allows client to realize whether a class scope
aoqi@0: * has changed, either directly (because a new member has been added/removed
aoqi@0: * to this scope) or indirectly (i.e. because a new member has been
aoqi@0: * added/removed into a supertype scope)
aoqi@0: */
aoqi@0: public static class CompoundScope extends Scope implements ScopeListener {
aoqi@0:
aoqi@0: public static final Entry[] emptyTable = new Entry[0];
aoqi@0:
aoqi@0: private List subScopes = List.nil();
aoqi@0: private int mark = 0;
aoqi@0:
aoqi@0: public CompoundScope(Symbol owner) {
aoqi@0: super(null, owner, emptyTable);
aoqi@0: }
aoqi@0:
aoqi@0: public void addSubScope(Scope that) {
aoqi@0: if (that != null) {
aoqi@0: subScopes = subScopes.prepend(that);
aoqi@0: that.addScopeListener(this);
aoqi@0: mark++;
aoqi@0: for (ScopeListener sl : listeners) {
aoqi@0: sl.symbolAdded(null, this); //propagate upwards in case of nested CompoundScopes
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public void symbolAdded(Symbol sym, Scope s) {
aoqi@0: mark++;
aoqi@0: for (ScopeListener sl : listeners) {
aoqi@0: sl.symbolAdded(sym, s);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public void symbolRemoved(Symbol sym, Scope s) {
aoqi@0: mark++;
aoqi@0: for (ScopeListener sl : listeners) {
aoqi@0: sl.symbolRemoved(sym, s);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public int getMark() {
aoqi@0: return mark;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: StringBuilder buf = new StringBuilder();
aoqi@0: buf.append("CompoundScope{");
aoqi@0: String sep = "";
aoqi@0: for (Scope s : subScopes) {
aoqi@0: buf.append(sep);
aoqi@0: buf.append(s);
aoqi@0: sep = ",";
aoqi@0: }
aoqi@0: buf.append("}");
aoqi@0: return buf.toString();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Iterable getElements(final Filter sf) {
aoqi@0: return new Iterable() {
aoqi@0: public Iterator iterator() {
aoqi@0: return new CompoundScopeIterator(subScopes) {
aoqi@0: Iterator nextIterator(Scope s) {
aoqi@0: return s.getElements(sf).iterator();
aoqi@0: }
aoqi@0: };
aoqi@0: }
aoqi@0: };
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Iterable getElementsByName(final Name name, final Filter sf) {
aoqi@0: return new Iterable() {
aoqi@0: public Iterator iterator() {
aoqi@0: return new CompoundScopeIterator(subScopes) {
aoqi@0: Iterator nextIterator(Scope s) {
aoqi@0: return s.getElementsByName(name, sf).iterator();
aoqi@0: }
aoqi@0: };
aoqi@0: }
aoqi@0: };
aoqi@0: }
aoqi@0:
aoqi@0: abstract class CompoundScopeIterator implements Iterator {
aoqi@0:
aoqi@0: private Iterator currentIterator;
aoqi@0: private List scopesToScan;
aoqi@0:
aoqi@0: public CompoundScopeIterator(List scopesToScan) {
aoqi@0: this.scopesToScan = scopesToScan;
aoqi@0: update();
aoqi@0: }
aoqi@0:
aoqi@0: abstract Iterator nextIterator(Scope s);
aoqi@0:
aoqi@0: public boolean hasNext() {
aoqi@0: return currentIterator != null;
aoqi@0: }
aoqi@0:
aoqi@0: public Symbol next() {
aoqi@0: Symbol sym = currentIterator.next();
aoqi@0: if (!currentIterator.hasNext()) {
aoqi@0: update();
aoqi@0: }
aoqi@0: return sym;
aoqi@0: }
aoqi@0:
aoqi@0: public void remove() {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: private void update() {
aoqi@0: while (scopesToScan.nonEmpty()) {
aoqi@0: currentIterator = nextIterator(scopesToScan.head);
aoqi@0: scopesToScan = scopesToScan.tail;
aoqi@0: if (currentIterator.hasNext()) return;
aoqi@0: }
aoqi@0: currentIterator = null;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Entry lookup(Name name, Filter sf) {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Scope dup(Symbol newOwner) {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void enter(Symbol sym, Scope s, Scope origin, boolean staticallyImported) {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void remove(Symbol sym) {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** An error scope, for which the owner should be an error symbol. */
aoqi@0: public static class ErrorScope extends Scope {
aoqi@0: ErrorScope(Scope next, Symbol errSymbol, Entry[] table) {
aoqi@0: super(next, /*owner=*/errSymbol, table);
aoqi@0: }
aoqi@0: public ErrorScope(Symbol errSymbol) {
aoqi@0: super(errSymbol);
aoqi@0: }
aoqi@0: public Scope dup() {
aoqi@0: return new ErrorScope(this, owner, table);
aoqi@0: }
aoqi@0: public Scope dupUnshared() {
aoqi@0: return new ErrorScope(this, owner, table.clone());
aoqi@0: }
aoqi@0: public Entry lookup(Name name) {
aoqi@0: Entry e = super.lookup(name);
aoqi@0: if (e.scope == null)
aoqi@0: return new Entry(owner, null, null, null);
aoqi@0: else
aoqi@0: return e;
aoqi@0: }
aoqi@0: }
aoqi@0: }