src/share/classes/com/sun/tools/javac/code/Scope.java

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1357
c75be5bc5283
child 1886
79c3146e417b
permissions
-rw-r--r--

Merge

duke@1 1 /*
jjg@1357 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.code;
duke@1 27
jjg@1357 28 import java.util.Iterator;
jjg@1357 29
duke@1 30 import com.sun.tools.javac.util.*;
duke@1 31
duke@1 32 /** A scope represents an area of visibility in a Java program. The
duke@1 33 * Scope class is a container for symbols which provides
duke@1 34 * efficient access to symbols given their names. Scopes are implemented
jjg@738 35 * as hash tables with "open addressing" and "double hashing".
jjg@738 36 * Scopes can be nested; the next field of a scope points
duke@1 37 * to its next outer scope. Nested scopes can share their hash tables.
duke@1 38 *
jjg@581 39 * <p><b>This is NOT part of any supported API.
jjg@581 40 * If you write code that depends on this, you do so at your own risk.
duke@1 41 * This code and its internal interfaces are subject to change or
duke@1 42 * deletion without notice.</b>
duke@1 43 */
duke@1 44 public class Scope {
duke@1 45
duke@1 46 /** The number of scopes that share this scope's hash table.
duke@1 47 */
duke@1 48 private int shared;
duke@1 49
duke@1 50 /** Next enclosing scope (with whom this scope may share a hashtable)
duke@1 51 */
duke@1 52 public Scope next;
duke@1 53
duke@1 54 /** The scope's owner.
duke@1 55 */
duke@1 56 public Symbol owner;
duke@1 57
duke@1 58 /** A hash table for the scope's entries.
duke@1 59 */
jjg@738 60 Entry[] table;
duke@1 61
duke@1 62 /** Mask for hash codes, always equal to (table.length - 1).
duke@1 63 */
duke@1 64 int hashMask;
duke@1 65
duke@1 66 /** A linear list that also contains all entries in
duke@1 67 * reverse order of appearance (i.e later entries are pushed on top).
duke@1 68 */
duke@1 69 public Entry elems;
duke@1 70
duke@1 71 /** The number of elements in this scope.
jjg@738 72 * This includes deleted elements, whose value is the sentinel.
duke@1 73 */
jjg@738 74 int nelems = 0;
duke@1 75
jjg@767 76 /** A list of scopes to be notified if items are to be removed from this scope.
jjg@767 77 */
mcimadamore@877 78 List<ScopeListener> listeners = List.nil();
jjg@767 79
jjg@738 80 /** Use as a "not-found" result for lookup.
jjg@738 81 * Also used to mark deleted entries in the table.
duke@1 82 */
duke@1 83 private static final Entry sentinel = new Entry(null, null, null, null);
duke@1 84
duke@1 85 /** The hash table's initial size.
duke@1 86 */
duke@1 87 private static final int INITIAL_SIZE = 0x10;
duke@1 88
duke@1 89 /** A value for the empty scope.
duke@1 90 */
mcimadamore@858 91 public static final Scope emptyScope = new Scope(null, null, new Entry[]{});
duke@1 92
duke@1 93 /** Construct a new scope, within scope next, with given owner, using
duke@1 94 * given table. The table's length must be an exponent of 2.
duke@1 95 */
mcimadamore@858 96 private Scope(Scope next, Symbol owner, Entry[] table) {
duke@1 97 this.next = next;
jjg@816 98 Assert.check(emptyScope == null || owner != null);
duke@1 99 this.owner = owner;
duke@1 100 this.table = table;
duke@1 101 this.hashMask = table.length - 1;
duke@1 102 }
duke@1 103
jjg@738 104 /** Convenience constructor used for dup and dupUnshared. */
mcimadamore@858 105 private Scope(Scope next, Symbol owner, Entry[] table, int nelems) {
mcimadamore@858 106 this(next, owner, table);
mcimadamore@858 107 this.nelems = nelems;
jjg@738 108 }
jjg@738 109
duke@1 110 /** Construct a new scope, within scope next, with given owner,
duke@1 111 * using a fresh table of length INITIAL_SIZE.
duke@1 112 */
duke@1 113 public Scope(Symbol owner) {
mcimadamore@858 114 this(null, owner, new Entry[INITIAL_SIZE]);
duke@1 115 }
duke@1 116
duke@1 117 /** Construct a fresh scope within this scope, with same owner,
duke@1 118 * which shares its table with the outer scope. Used in connection with
duke@1 119 * method leave if scope access is stack-like in order to avoid allocation
duke@1 120 * of fresh tables.
duke@1 121 */
duke@1 122 public Scope dup() {
jjg@738 123 return dup(this.owner);
duke@1 124 }
duke@1 125
duke@1 126 /** Construct a fresh scope within this scope, with new owner,
duke@1 127 * which shares its table with the outer scope. Used in connection with
duke@1 128 * method leave if scope access is stack-like in order to avoid allocation
duke@1 129 * of fresh tables.
duke@1 130 */
duke@1 131 public Scope dup(Symbol newOwner) {
mcimadamore@858 132 Scope result = new Scope(this, newOwner, this.table, this.nelems);
duke@1 133 shared++;
duke@1 134 // System.out.println("====> duping scope " + this.hashCode() + " owned by " + newOwner + " to " + result.hashCode());
duke@1 135 // new Error().printStackTrace(System.out);
duke@1 136 return result;
duke@1 137 }
duke@1 138
duke@1 139 /** Construct a fresh scope within this scope, with same owner,
duke@1 140 * with a new hash table, whose contents initially are those of
duke@1 141 * the table of its outer scope.
duke@1 142 */
duke@1 143 public Scope dupUnshared() {
mcimadamore@858 144 return new Scope(this, this.owner, this.table.clone(), this.nelems);
duke@1 145 }
duke@1 146
duke@1 147 /** Remove all entries of this scope from its table, if shared
duke@1 148 * with next.
duke@1 149 */
duke@1 150 public Scope leave() {
jjg@816 151 Assert.check(shared == 0);
duke@1 152 if (table != next.table) return next;
duke@1 153 while (elems != null) {
jjg@738 154 int hash = getIndex(elems.sym.name);
duke@1 155 Entry e = table[hash];
jjg@816 156 Assert.check(e == elems, elems.sym);
duke@1 157 table[hash] = elems.shadowed;
duke@1 158 elems = elems.sibling;
duke@1 159 }
jjg@816 160 Assert.check(next.shared > 0);
duke@1 161 next.shared--;
jjg@738 162 next.nelems = nelems;
duke@1 163 // System.out.println("====> leaving scope " + this.hashCode() + " owned by " + this.owner + " to " + next.hashCode());
duke@1 164 // new Error().printStackTrace(System.out);
duke@1 165 return next;
duke@1 166 }
duke@1 167
duke@1 168 /** Double size of hash table.
duke@1 169 */
duke@1 170 private void dble() {
jjg@816 171 Assert.check(shared == 0);
duke@1 172 Entry[] oldtable = table;
duke@1 173 Entry[] newtable = new Entry[oldtable.length * 2];
duke@1 174 for (Scope s = this; s != null; s = s.next) {
duke@1 175 if (s.table == oldtable) {
jjg@816 176 Assert.check(s == this || s.shared != 0);
duke@1 177 s.table = newtable;
duke@1 178 s.hashMask = newtable.length - 1;
duke@1 179 }
duke@1 180 }
jjg@738 181 int n = 0;
jjg@738 182 for (int i = oldtable.length; --i >= 0; ) {
jjg@738 183 Entry e = oldtable[i];
jjg@767 184 if (e != null && e != sentinel) {
jjg@738 185 table[getIndex(e.sym.name)] = e;
jjg@738 186 n++;
jjg@738 187 }
duke@1 188 }
jjg@738 189 // We don't need to update nelems for shared inherited scopes,
jjg@738 190 // since that gets handled by leave().
jjg@738 191 nelems = n;
duke@1 192 }
duke@1 193
duke@1 194 /** Enter symbol sym in this scope.
duke@1 195 */
duke@1 196 public void enter(Symbol sym) {
jjg@816 197 Assert.check(shared == 0);
duke@1 198 enter(sym, this);
duke@1 199 }
duke@1 200
duke@1 201 public void enter(Symbol sym, Scope s) {
duke@1 202 enter(sym, s, s);
duke@1 203 }
duke@1 204
duke@1 205 /**
duke@1 206 * Enter symbol sym in this scope, but mark that it comes from
duke@1 207 * given scope `s' accessed through `origin'. The last two
duke@1 208 * arguments are only used in import scopes.
duke@1 209 */
duke@1 210 public void enter(Symbol sym, Scope s, Scope origin) {
jjg@816 211 Assert.check(shared == 0);
jjg@738 212 if (nelems * 3 >= hashMask * 2)
jjg@738 213 dble();
jjg@738 214 int hash = getIndex(sym.name);
jjg@738 215 Entry old = table[hash];
jjg@738 216 if (old == null) {
jjg@738 217 old = sentinel;
jjg@738 218 nelems++;
jjg@738 219 }
jjg@738 220 Entry e = makeEntry(sym, old, elems, s, origin);
duke@1 221 table[hash] = e;
duke@1 222 elems = e;
mcimadamore@877 223
mcimadamore@877 224 //notify listeners
mcimadamore@877 225 for (List<ScopeListener> l = listeners; l.nonEmpty(); l = l.tail) {
mcimadamore@877 226 l.head.symbolAdded(sym, this);
mcimadamore@877 227 }
duke@1 228 }
duke@1 229
duke@1 230 Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 231 return new Entry(sym, shadowed, sibling, scope);
duke@1 232 }
duke@1 233
mcimadamore@877 234
mcimadamore@877 235 public interface ScopeListener {
mcimadamore@877 236 public void symbolAdded(Symbol sym, Scope s);
mcimadamore@877 237 public void symbolRemoved(Symbol sym, Scope s);
mcimadamore@877 238 }
mcimadamore@877 239
mcimadamore@877 240 public void addScopeListener(ScopeListener sl) {
mcimadamore@877 241 listeners = listeners.prepend(sl);
mcimadamore@877 242 }
mcimadamore@877 243
duke@1 244 /** Remove symbol from this scope. Used when an inner class
duke@1 245 * attribute tells us that the class isn't a package member.
duke@1 246 */
duke@1 247 public void remove(Symbol sym) {
jjg@816 248 Assert.check(shared == 0);
duke@1 249 Entry e = lookup(sym.name);
duke@1 250 if (e.scope == null) return;
duke@1 251
duke@1 252 // remove e from table and shadowed list;
jjg@738 253 int i = getIndex(sym.name);
jjg@738 254 Entry te = table[i];
duke@1 255 if (te == e)
jjg@738 256 table[i] = e.shadowed;
duke@1 257 else while (true) {
duke@1 258 if (te.shadowed == e) {
duke@1 259 te.shadowed = e.shadowed;
duke@1 260 break;
duke@1 261 }
duke@1 262 te = te.shadowed;
duke@1 263 }
duke@1 264
duke@1 265 // remove e from elems and sibling list
duke@1 266 te = elems;
duke@1 267 if (te == e)
duke@1 268 elems = e.sibling;
duke@1 269 else while (true) {
duke@1 270 if (te.sibling == e) {
duke@1 271 te.sibling = e.sibling;
duke@1 272 break;
duke@1 273 }
duke@1 274 te = te.sibling;
duke@1 275 }
jjg@767 276
mcimadamore@877 277 //notify listeners
mcimadamore@877 278 for (List<ScopeListener> l = listeners; l.nonEmpty(); l = l.tail) {
mcimadamore@877 279 l.head.symbolRemoved(sym, this);
jjg@767 280 }
duke@1 281 }
duke@1 282
duke@1 283 /** Enter symbol sym in this scope if not already there.
duke@1 284 */
duke@1 285 public void enterIfAbsent(Symbol sym) {
jjg@816 286 Assert.check(shared == 0);
duke@1 287 Entry e = lookup(sym.name);
duke@1 288 while (e.scope == this && e.sym.kind != sym.kind) e = e.next();
duke@1 289 if (e.scope != this) enter(sym);
duke@1 290 }
duke@1 291
duke@1 292 /** Given a class, is there already a class with same fully
duke@1 293 * qualified name in this (import) scope?
duke@1 294 */
duke@1 295 public boolean includes(Symbol c) {
duke@1 296 for (Scope.Entry e = lookup(c.name);
duke@1 297 e.scope == this;
duke@1 298 e = e.next()) {
duke@1 299 if (e.sym == c) return true;
duke@1 300 }
duke@1 301 return false;
duke@1 302 }
duke@1 303
mcimadamore@673 304 static final Filter<Symbol> noFilter = new Filter<Symbol>() {
mcimadamore@673 305 public boolean accepts(Symbol s) {
mcimadamore@673 306 return true;
mcimadamore@673 307 }
mcimadamore@673 308 };
mcimadamore@673 309
duke@1 310 /** Return the entry associated with given name, starting in
duke@1 311 * this scope and proceeding outwards. If no entry was found,
duke@1 312 * return the sentinel, which is characterized by having a null in
duke@1 313 * both its scope and sym fields, whereas both fields are non-null
duke@1 314 * for regular entries.
duke@1 315 */
duke@1 316 public Entry lookup(Name name) {
mcimadamore@673 317 return lookup(name, noFilter);
mcimadamore@673 318 }
mcimadamore@673 319 public Entry lookup(Name name, Filter<Symbol> sf) {
jjg@738 320 Entry e = table[getIndex(name)];
jjg@738 321 if (e == null || e == sentinel)
jjg@738 322 return sentinel;
mcimadamore@673 323 while (e.scope != null && (e.sym.name != name || !sf.accepts(e.sym)))
duke@1 324 e = e.shadowed;
duke@1 325 return e;
duke@1 326 }
duke@1 327
jjg@738 328 /*void dump (java.io.PrintStream out) {
jjg@738 329 out.println(this);
jjg@738 330 for (int l=0; l < table.length; l++) {
jjg@738 331 Entry le = table[l];
jjg@738 332 out.print("#"+l+": ");
jjg@738 333 if (le==sentinel) out.println("sentinel");
jjg@738 334 else if(le == null) out.println("null");
jjg@738 335 else out.println(""+le+" s:"+le.sym);
jjg@738 336 }
jjg@738 337 }*/
jjg@738 338
jjg@738 339 /** Look for slot in the table.
jjg@738 340 * We use open addressing with double hashing.
jjg@738 341 */
jjg@738 342 int getIndex (Name name) {
jjg@738 343 int h = name.hashCode();
jjg@738 344 int i = h & hashMask;
jjg@738 345 // The expression below is always odd, so it is guaranteed
jjg@767 346 // to be mutually prime with table.length, a power of 2.
jjg@738 347 int x = hashMask - ((h + (h >> 16)) << 1);
jjg@738 348 int d = -1; // Index of a deleted item.
jjg@738 349 for (;;) {
jjg@738 350 Entry e = table[i];
jjg@738 351 if (e == null)
jjg@738 352 return d >= 0 ? d : i;
jjg@738 353 if (e == sentinel) {
jjg@738 354 // We have to keep searching even if we see a deleted item.
jjg@738 355 // However, remember the index in case we fail to find the name.
jjg@738 356 if (d < 0)
jjg@738 357 d = i;
jjg@738 358 } else if (e.sym.name == name)
jjg@738 359 return i;
jjg@738 360 i = (i + x) & hashMask;
jjg@738 361 }
jjg@738 362 }
jjg@738 363
duke@1 364 public Iterable<Symbol> getElements() {
mcimadamore@673 365 return getElements(noFilter);
mcimadamore@673 366 }
mcimadamore@673 367
mcimadamore@673 368 public Iterable<Symbol> getElements(final Filter<Symbol> sf) {
duke@1 369 return new Iterable<Symbol>() {
duke@1 370 public Iterator<Symbol> iterator() {
duke@1 371 return new Iterator<Symbol>() {
duke@1 372 private Scope currScope = Scope.this;
duke@1 373 private Scope.Entry currEntry = elems;
duke@1 374 {
duke@1 375 update();
duke@1 376 }
duke@1 377
duke@1 378 public boolean hasNext() {
duke@1 379 return currEntry != null;
duke@1 380 }
duke@1 381
duke@1 382 public Symbol next() {
duke@1 383 Symbol sym = (currEntry == null ? null : currEntry.sym);
mcimadamore@673 384 if (currEntry != null) {
mcimadamore@673 385 currEntry = currEntry.sibling;
mcimadamore@673 386 }
duke@1 387 update();
duke@1 388 return sym;
duke@1 389 }
duke@1 390
duke@1 391 public void remove() {
duke@1 392 throw new UnsupportedOperationException();
duke@1 393 }
duke@1 394
duke@1 395 private void update() {
mcimadamore@673 396 skipToNextMatchingEntry();
duke@1 397 while (currEntry == null && currScope.next != null) {
duke@1 398 currScope = currScope.next;
duke@1 399 currEntry = currScope.elems;
mcimadamore@673 400 skipToNextMatchingEntry();
mcimadamore@673 401 }
mcimadamore@673 402 }
mcimadamore@673 403
mcimadamore@673 404 void skipToNextMatchingEntry() {
mcimadamore@673 405 while (currEntry != null && !sf.accepts(currEntry.sym)) {
mcimadamore@673 406 currEntry = currEntry.sibling;
duke@1 407 }
duke@1 408 }
duke@1 409 };
duke@1 410 }
duke@1 411 };
mcimadamore@877 412 }
duke@1 413
mcimadamore@877 414 public Iterable<Symbol> getElementsByName(Name name) {
mcimadamore@877 415 return getElementsByName(name, noFilter);
mcimadamore@877 416 }
mcimadamore@877 417
mcimadamore@877 418 public Iterable<Symbol> getElementsByName(final Name name, final Filter<Symbol> sf) {
mcimadamore@877 419 return new Iterable<Symbol>() {
mcimadamore@877 420 public Iterator<Symbol> iterator() {
mcimadamore@877 421 return new Iterator<Symbol>() {
mcimadamore@877 422 Scope.Entry currentEntry = lookup(name, sf);
mcimadamore@877 423
mcimadamore@877 424 public boolean hasNext() {
mcimadamore@877 425 return currentEntry.scope != null;
mcimadamore@877 426 }
mcimadamore@877 427 public Symbol next() {
mcimadamore@877 428 Scope.Entry prevEntry = currentEntry;
mcimadamore@877 429 currentEntry = currentEntry.next(sf);
mcimadamore@877 430 return prevEntry.sym;
mcimadamore@877 431 }
mcimadamore@877 432 public void remove() {
mcimadamore@877 433 throw new UnsupportedOperationException();
mcimadamore@877 434 }
mcimadamore@877 435 };
mcimadamore@877 436 }
mcimadamore@877 437 };
duke@1 438 }
duke@1 439
duke@1 440 public String toString() {
duke@1 441 StringBuilder result = new StringBuilder();
duke@1 442 result.append("Scope[");
duke@1 443 for (Scope s = this; s != null ; s = s.next) {
duke@1 444 if (s != this) result.append(" | ");
duke@1 445 for (Entry e = s.elems; e != null; e = e.sibling) {
duke@1 446 if (e != s.elems) result.append(", ");
duke@1 447 result.append(e.sym);
duke@1 448 }
duke@1 449 }
duke@1 450 result.append("]");
duke@1 451 return result.toString();
duke@1 452 }
duke@1 453
duke@1 454 /** A class for scope entries.
duke@1 455 */
duke@1 456 public static class Entry {
duke@1 457
duke@1 458 /** The referenced symbol.
duke@1 459 * sym == null iff this == sentinel
duke@1 460 */
duke@1 461 public Symbol sym;
duke@1 462
duke@1 463 /** An entry with the same hash code, or sentinel.
duke@1 464 */
duke@1 465 private Entry shadowed;
duke@1 466
duke@1 467 /** Next entry in same scope.
duke@1 468 */
duke@1 469 public Entry sibling;
duke@1 470
duke@1 471 /** The entry's scope.
duke@1 472 * scope == null iff this == sentinel
duke@1 473 * for an entry in an import scope, this is the scope
duke@1 474 * where the entry came from (i.e. was imported from).
duke@1 475 */
duke@1 476 public Scope scope;
duke@1 477
duke@1 478 public Entry(Symbol sym, Entry shadowed, Entry sibling, Scope scope) {
duke@1 479 this.sym = sym;
duke@1 480 this.shadowed = shadowed;
duke@1 481 this.sibling = sibling;
duke@1 482 this.scope = scope;
duke@1 483 }
duke@1 484
duke@1 485 /** Return next entry with the same name as this entry, proceeding
duke@1 486 * outwards if not found in this scope.
duke@1 487 */
duke@1 488 public Entry next() {
jjg@738 489 return shadowed;
duke@1 490 }
duke@1 491
mcimadamore@780 492 public Entry next(Filter<Symbol> sf) {
mcimadamore@780 493 if (shadowed.sym == null || sf.accepts(shadowed.sym)) return shadowed;
mcimadamore@780 494 else return shadowed.next(sf);
mcimadamore@780 495 }
mcimadamore@780 496
duke@1 497 public Scope getOrigin() {
duke@1 498 // The origin is only recorded for import scopes. For all
duke@1 499 // other scope entries, the "enclosing" type is available
duke@1 500 // from other sources. See Attr.visitSelect and
duke@1 501 // Attr.visitIdent. Rather than throwing an assertion
duke@1 502 // error, we return scope which will be the same as origin
duke@1 503 // in many cases.
duke@1 504 return scope;
duke@1 505 }
duke@1 506 }
duke@1 507
duke@1 508 public static class ImportScope extends Scope {
duke@1 509
duke@1 510 public ImportScope(Symbol owner) {
duke@1 511 super(owner);
duke@1 512 }
duke@1 513
duke@1 514 @Override
duke@1 515 Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 516 return new ImportEntry(sym, shadowed, sibling, scope, origin);
duke@1 517 }
duke@1 518
duke@1 519 static class ImportEntry extends Entry {
duke@1 520 private Scope origin;
duke@1 521
duke@1 522 ImportEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 523 super(sym, shadowed, sibling, scope);
duke@1 524 this.origin = origin;
duke@1 525 }
duke@1 526
duke@1 527 @Override
duke@1 528 public Scope getOrigin() { return origin; }
jjg@767 529 }
jjg@767 530 }
jjg@738 531
mcimadamore@877 532 public static class StarImportScope extends ImportScope implements ScopeListener {
jjg@767 533
jjg@767 534 public StarImportScope(Symbol owner) {
jjg@767 535 super(owner);
jjg@767 536 }
jjg@767 537
jjg@767 538 public void importAll (Scope fromScope) {
jjg@767 539 for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
jjg@767 540 if (e.sym.kind == Kinds.TYP && !includes(e.sym))
jjg@767 541 enter(e.sym, fromScope);
jjg@767 542 }
jjg@767 543 // Register to be notified when imported items are removed
mcimadamore@877 544 fromScope.addScopeListener(this);
duke@1 545 }
mcimadamore@877 546
mcimadamore@877 547 public void symbolRemoved(Symbol sym, Scope s) {
mcimadamore@877 548 remove(sym);
mcimadamore@877 549 }
mcimadamore@877 550 public void symbolAdded(Symbol sym, Scope s) { }
duke@1 551 }
duke@1 552
duke@1 553 /** An empty scope, into which you can't place anything. Used for
duke@1 554 * the scope for a variable initializer.
duke@1 555 */
duke@1 556 public static class DelegatedScope extends Scope {
duke@1 557 Scope delegatee;
duke@1 558 public static final Entry[] emptyTable = new Entry[0];
duke@1 559
duke@1 560 public DelegatedScope(Scope outer) {
mcimadamore@858 561 super(outer, outer.owner, emptyTable);
duke@1 562 delegatee = outer;
duke@1 563 }
duke@1 564 public Scope dup() {
duke@1 565 return new DelegatedScope(next);
duke@1 566 }
duke@1 567 public Scope dupUnshared() {
duke@1 568 return new DelegatedScope(next);
duke@1 569 }
duke@1 570 public Scope leave() {
duke@1 571 return next;
duke@1 572 }
duke@1 573 public void enter(Symbol sym) {
duke@1 574 // only anonymous classes could be put here
duke@1 575 }
duke@1 576 public void enter(Symbol sym, Scope s) {
duke@1 577 // only anonymous classes could be put here
duke@1 578 }
duke@1 579 public void remove(Symbol sym) {
duke@1 580 throw new AssertionError(sym);
duke@1 581 }
duke@1 582 public Entry lookup(Name name) {
duke@1 583 return delegatee.lookup(name);
duke@1 584 }
duke@1 585 }
duke@1 586
mcimadamore@877 587 /** A class scope adds capabilities to keep track of changes in related
mcimadamore@877 588 * class scopes - this allows client to realize whether a class scope
mcimadamore@877 589 * has changed, either directly (because a new member has been added/removed
mcimadamore@877 590 * to this scope) or indirectly (i.e. because a new member has been
mcimadamore@877 591 * added/removed into a supertype scope)
mcimadamore@877 592 */
mcimadamore@877 593 public static class CompoundScope extends Scope implements ScopeListener {
mcimadamore@877 594
mcimadamore@877 595 public static final Entry[] emptyTable = new Entry[0];
mcimadamore@877 596
mcimadamore@877 597 private List<Scope> subScopes = List.nil();
mcimadamore@877 598 private int mark = 0;
mcimadamore@877 599
mcimadamore@877 600 public CompoundScope(Symbol owner) {
mcimadamore@877 601 super(null, owner, emptyTable);
mcimadamore@877 602 }
mcimadamore@877 603
mcimadamore@877 604 public void addSubScope(Scope that) {
mcimadamore@877 605 if (that != null) {
mcimadamore@877 606 subScopes = subScopes.prepend(that);
mcimadamore@877 607 that.addScopeListener(this);
mcimadamore@877 608 mark++;
mcimadamore@877 609 for (ScopeListener sl : listeners) {
mcimadamore@877 610 sl.symbolAdded(null, this); //propagate upwards in case of nested CompoundScopes
mcimadamore@877 611 }
mcimadamore@877 612 }
mcimadamore@877 613 }
mcimadamore@877 614
mcimadamore@877 615 public void symbolAdded(Symbol sym, Scope s) {
mcimadamore@877 616 mark++;
mcimadamore@877 617 for (ScopeListener sl : listeners) {
mcimadamore@877 618 sl.symbolAdded(sym, s);
mcimadamore@877 619 }
mcimadamore@877 620 }
mcimadamore@877 621
mcimadamore@877 622 public void symbolRemoved(Symbol sym, Scope s) {
mcimadamore@877 623 mark++;
mcimadamore@877 624 for (ScopeListener sl : listeners) {
mcimadamore@877 625 sl.symbolRemoved(sym, s);
mcimadamore@877 626 }
mcimadamore@877 627 }
mcimadamore@877 628
mcimadamore@877 629 public int getMark() {
mcimadamore@877 630 return mark;
mcimadamore@877 631 }
mcimadamore@877 632
mcimadamore@877 633 @Override
mcimadamore@877 634 public String toString() {
mcimadamore@877 635 StringBuilder buf = new StringBuilder();
mcimadamore@877 636 buf.append("CompoundScope{");
mcimadamore@877 637 String sep = "";
mcimadamore@877 638 for (Scope s : subScopes) {
mcimadamore@877 639 buf.append(sep);
mcimadamore@877 640 buf.append(s);
mcimadamore@877 641 sep = ",";
mcimadamore@877 642 }
mcimadamore@877 643 buf.append("}");
mcimadamore@877 644 return buf.toString();
mcimadamore@877 645 }
mcimadamore@877 646
mcimadamore@877 647 @Override
mcimadamore@877 648 public Iterable<Symbol> getElements(final Filter<Symbol> sf) {
mcimadamore@877 649 return new Iterable<Symbol>() {
mcimadamore@877 650 public Iterator<Symbol> iterator() {
mcimadamore@877 651 return new CompoundScopeIterator(subScopes) {
mcimadamore@877 652 Iterator<Symbol> nextIterator(Scope s) {
mcimadamore@982 653 return s.getElements(sf).iterator();
mcimadamore@877 654 }
mcimadamore@877 655 };
mcimadamore@877 656 }
mcimadamore@877 657 };
mcimadamore@877 658 }
mcimadamore@877 659
mcimadamore@877 660 @Override
mcimadamore@877 661 public Iterable<Symbol> getElementsByName(final Name name, final Filter<Symbol> sf) {
mcimadamore@877 662 return new Iterable<Symbol>() {
mcimadamore@877 663 public Iterator<Symbol> iterator() {
mcimadamore@877 664 return new CompoundScopeIterator(subScopes) {
mcimadamore@877 665 Iterator<Symbol> nextIterator(Scope s) {
mcimadamore@877 666 return s.getElementsByName(name, sf).iterator();
mcimadamore@877 667 }
mcimadamore@877 668 };
mcimadamore@877 669 }
mcimadamore@877 670 };
mcimadamore@877 671 }
mcimadamore@877 672
mcimadamore@877 673 abstract class CompoundScopeIterator implements Iterator<Symbol> {
mcimadamore@877 674
mcimadamore@877 675 private Iterator<Symbol> currentIterator;
mcimadamore@877 676 private List<Scope> scopesToScan;
mcimadamore@877 677
mcimadamore@877 678 public CompoundScopeIterator(List<Scope> scopesToScan) {
mcimadamore@877 679 this.scopesToScan = scopesToScan;
mcimadamore@877 680 update();
mcimadamore@877 681 }
mcimadamore@877 682
mcimadamore@877 683 abstract Iterator<Symbol> nextIterator(Scope s);
mcimadamore@877 684
mcimadamore@877 685 public boolean hasNext() {
mcimadamore@877 686 return currentIterator != null;
mcimadamore@877 687 }
mcimadamore@877 688
mcimadamore@877 689 public Symbol next() {
mcimadamore@877 690 Symbol sym = currentIterator.next();
mcimadamore@877 691 if (!currentIterator.hasNext()) {
mcimadamore@877 692 update();
mcimadamore@877 693 }
mcimadamore@877 694 return sym;
mcimadamore@877 695 }
mcimadamore@877 696
mcimadamore@877 697 public void remove() {
mcimadamore@877 698 throw new UnsupportedOperationException();
mcimadamore@877 699 }
mcimadamore@877 700
mcimadamore@877 701 private void update() {
mcimadamore@877 702 while (scopesToScan.nonEmpty()) {
mcimadamore@877 703 currentIterator = nextIterator(scopesToScan.head);
mcimadamore@877 704 scopesToScan = scopesToScan.tail;
mcimadamore@877 705 if (currentIterator.hasNext()) return;
mcimadamore@877 706 }
mcimadamore@877 707 currentIterator = null;
mcimadamore@877 708 }
mcimadamore@877 709 }
mcimadamore@877 710
mcimadamore@877 711 @Override
mcimadamore@877 712 public Entry lookup(Name name, Filter<Symbol> sf) {
mcimadamore@877 713 throw new UnsupportedOperationException();
mcimadamore@877 714 }
mcimadamore@877 715
mcimadamore@877 716 @Override
mcimadamore@877 717 public Scope dup(Symbol newOwner) {
mcimadamore@877 718 throw new UnsupportedOperationException();
mcimadamore@877 719 }
mcimadamore@877 720
mcimadamore@877 721 @Override
mcimadamore@877 722 public void enter(Symbol sym, Scope s, Scope origin) {
mcimadamore@877 723 throw new UnsupportedOperationException();
mcimadamore@877 724 }
mcimadamore@877 725
mcimadamore@877 726 @Override
mcimadamore@877 727 public void remove(Symbol sym) {
mcimadamore@877 728 throw new UnsupportedOperationException();
mcimadamore@877 729 }
mcimadamore@877 730 }
mcimadamore@877 731
duke@1 732 /** An error scope, for which the owner should be an error symbol. */
duke@1 733 public static class ErrorScope extends Scope {
duke@1 734 ErrorScope(Scope next, Symbol errSymbol, Entry[] table) {
mcimadamore@858 735 super(next, /*owner=*/errSymbol, table);
duke@1 736 }
duke@1 737 public ErrorScope(Symbol errSymbol) {
duke@1 738 super(errSymbol);
duke@1 739 }
duke@1 740 public Scope dup() {
duke@1 741 return new ErrorScope(this, owner, table);
duke@1 742 }
duke@1 743 public Scope dupUnshared() {
duke@1 744 return new ErrorScope(this, owner, table.clone());
duke@1 745 }
duke@1 746 public Entry lookup(Name name) {
duke@1 747 Entry e = super.lookup(name);
duke@1 748 if (e.scope == null)
duke@1 749 return new Entry(owner, null, null, null);
duke@1 750 else
duke@1 751 return e;
duke@1 752 }
duke@1 753 }
duke@1 754 }

mercurial