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

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2812
9ec429ab0e7e
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

merge

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

mercurial