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

Sat, 06 Nov 2010 13:53:48 -0700

author
jjg
date
Sat, 06 Nov 2010 13:53:48 -0700
changeset 738
9427a3c795fc
parent 729
6ce6ee1b831a
child 751
abaceae7c9f8
permissions
-rw-r--r--

6998063: new Scope impl to fix Scope performance issues
Reviewed-by: jjg
Contributed-by: per.bothner@oracle.com

duke@1 1 /*
jjg@738 2 * Copyright (c) 1999, 2010, 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
duke@1 28 import com.sun.tools.javac.util.*;
duke@1 29 import java.util.Iterator;
duke@1 30
duke@1 31 /** A scope represents an area of visibility in a Java program. The
duke@1 32 * Scope class is a container for symbols which provides
duke@1 33 * efficient access to symbols given their names. Scopes are implemented
jjg@738 34 * as hash tables with "open addressing" and "double hashing".
jjg@738 35 * Scopes can be nested; the next field of a scope points
duke@1 36 * to its next outer scope. Nested scopes can share their hash tables.
duke@1 37 *
jjg@581 38 * <p><b>This is NOT part of any supported API.
jjg@581 39 * If you write code that depends on this, you do so at your own risk.
duke@1 40 * This code and its internal interfaces are subject to change or
duke@1 41 * deletion without notice.</b>
duke@1 42 */
duke@1 43 public class Scope {
duke@1 44
duke@1 45 /** The number of scopes that share this scope's hash table.
duke@1 46 */
duke@1 47 private int shared;
duke@1 48
duke@1 49 /** Next enclosing scope (with whom this scope may share a hashtable)
duke@1 50 */
duke@1 51 public Scope next;
duke@1 52
duke@1 53 /** The scope's owner.
duke@1 54 */
duke@1 55 public Symbol owner;
duke@1 56
duke@1 57 /** A hash table for the scope's entries.
duke@1 58 */
jjg@738 59 Entry[] table;
duke@1 60
duke@1 61 /** Mask for hash codes, always equal to (table.length - 1).
duke@1 62 */
duke@1 63 int hashMask;
duke@1 64
duke@1 65 /** A linear list that also contains all entries in
duke@1 66 * reverse order of appearance (i.e later entries are pushed on top).
duke@1 67 */
duke@1 68 public Entry elems;
duke@1 69
duke@1 70 /** The number of elements in this scope.
jjg@738 71 * This includes deleted elements, whose value is the sentinel.
duke@1 72 */
jjg@738 73 int nelems = 0;
duke@1 74
mcimadamore@688 75 /** A timestamp - useful to quickly check whether a scope has changed or not
mcimadamore@688 76 */
mcimadamore@688 77 public ScopeCounter scopeCounter;
mcimadamore@688 78
mcimadamore@688 79 static ScopeCounter dummyCounter = new ScopeCounter() {
mcimadamore@688 80 @Override
mcimadamore@688 81 public void inc() {
mcimadamore@688 82 //do nothing
mcimadamore@688 83 }
mcimadamore@688 84 };
mcimadamore@688 85
mcimadamore@688 86 public static class ScopeCounter {
mcimadamore@688 87 protected static final Context.Key<ScopeCounter> scopeCounterKey =
mcimadamore@688 88 new Context.Key<ScopeCounter>();
mcimadamore@688 89
mcimadamore@688 90 public static ScopeCounter instance(Context context) {
mcimadamore@688 91 ScopeCounter instance = context.get(scopeCounterKey);
mcimadamore@688 92 if (instance == null)
mcimadamore@688 93 instance = new ScopeCounter(context);
mcimadamore@688 94 return instance;
mcimadamore@688 95 }
mcimadamore@688 96
mcimadamore@688 97 protected ScopeCounter(Context context) {
mcimadamore@688 98 context.put(scopeCounterKey, this);
mcimadamore@688 99 }
mcimadamore@688 100
mcimadamore@688 101 private ScopeCounter() {};
mcimadamore@688 102
mcimadamore@688 103 private long val = 0;
mcimadamore@688 104
mcimadamore@688 105 public void inc() {
mcimadamore@688 106 val++;
mcimadamore@688 107 }
mcimadamore@688 108
mcimadamore@688 109 public long val() {
mcimadamore@688 110 return val;
mcimadamore@688 111 }
mcimadamore@688 112 }
mcimadamore@688 113
jjg@738 114 /** Use as a "not-found" result for lookup.
jjg@738 115 * Also used to mark deleted entries in the table.
duke@1 116 */
duke@1 117 private static final Entry sentinel = new Entry(null, null, null, null);
duke@1 118
duke@1 119 /** The hash table's initial size.
duke@1 120 */
duke@1 121 private static final int INITIAL_SIZE = 0x10;
duke@1 122
duke@1 123 /** A value for the empty scope.
duke@1 124 */
mcimadamore@688 125 public static final Scope emptyScope = new Scope(null, null, new Entry[]{}, dummyCounter);
duke@1 126
duke@1 127 /** Construct a new scope, within scope next, with given owner, using
duke@1 128 * given table. The table's length must be an exponent of 2.
duke@1 129 */
mcimadamore@688 130 private Scope(Scope next, Symbol owner, Entry[] table, ScopeCounter scopeCounter) {
duke@1 131 this.next = next;
duke@1 132 assert emptyScope == null || owner != null;
duke@1 133 this.owner = owner;
duke@1 134 this.table = table;
duke@1 135 this.hashMask = table.length - 1;
mcimadamore@688 136 this.scopeCounter = scopeCounter;
duke@1 137 }
duke@1 138
jjg@738 139 /** Convenience constructor used for dup and dupUnshared. */
jjg@738 140 private Scope(Scope next, Symbol owner, Entry[] table) {
jjg@738 141 this(next, owner, table, next.scopeCounter);
jjg@738 142 this.nelems = next.nelems;
jjg@738 143 }
jjg@738 144
duke@1 145 /** Construct a new scope, within scope next, with given owner,
duke@1 146 * using a fresh table of length INITIAL_SIZE.
duke@1 147 */
duke@1 148 public Scope(Symbol owner) {
mcimadamore@688 149 this(owner, dummyCounter);
mcimadamore@688 150 }
mcimadamore@688 151
mcimadamore@688 152 protected Scope(Symbol owner, ScopeCounter scopeCounter) {
mcimadamore@688 153 this(null, owner, new Entry[INITIAL_SIZE], scopeCounter);
duke@1 154 }
duke@1 155
duke@1 156 /** Construct a fresh scope within this scope, with same owner,
duke@1 157 * which shares its table with the outer scope. Used in connection with
duke@1 158 * method leave if scope access is stack-like in order to avoid allocation
duke@1 159 * of fresh tables.
duke@1 160 */
duke@1 161 public Scope dup() {
jjg@738 162 return dup(this.owner);
duke@1 163 }
duke@1 164
duke@1 165 /** Construct a fresh scope within this scope, with new owner,
duke@1 166 * which shares its table with the outer scope. Used in connection with
duke@1 167 * method leave if scope access is stack-like in order to avoid allocation
duke@1 168 * of fresh tables.
duke@1 169 */
duke@1 170 public Scope dup(Symbol newOwner) {
jjg@738 171 Scope result = new Scope(this, newOwner, this.table);
duke@1 172 shared++;
duke@1 173 // System.out.println("====> duping scope " + this.hashCode() + " owned by " + newOwner + " to " + result.hashCode());
duke@1 174 // new Error().printStackTrace(System.out);
duke@1 175 return result;
duke@1 176 }
duke@1 177
duke@1 178 /** Construct a fresh scope within this scope, with same owner,
duke@1 179 * with a new hash table, whose contents initially are those of
duke@1 180 * the table of its outer scope.
duke@1 181 */
duke@1 182 public Scope dupUnshared() {
jjg@738 183 return new Scope(this, this.owner, this.table.clone());
duke@1 184 }
duke@1 185
duke@1 186 /** Remove all entries of this scope from its table, if shared
duke@1 187 * with next.
duke@1 188 */
duke@1 189 public Scope leave() {
duke@1 190 assert shared == 0;
duke@1 191 if (table != next.table) return next;
duke@1 192 while (elems != null) {
jjg@738 193 int hash = getIndex(elems.sym.name);
duke@1 194 Entry e = table[hash];
duke@1 195 assert e == elems : elems.sym;
duke@1 196 table[hash] = elems.shadowed;
duke@1 197 elems = elems.sibling;
duke@1 198 }
duke@1 199 assert next.shared > 0;
duke@1 200 next.shared--;
jjg@738 201 next.nelems = nelems;
duke@1 202 // System.out.println("====> leaving scope " + this.hashCode() + " owned by " + this.owner + " to " + next.hashCode());
duke@1 203 // new Error().printStackTrace(System.out);
duke@1 204 return next;
duke@1 205 }
duke@1 206
duke@1 207 /** Double size of hash table.
duke@1 208 */
duke@1 209 private void dble() {
duke@1 210 assert shared == 0;
duke@1 211 Entry[] oldtable = table;
duke@1 212 Entry[] newtable = new Entry[oldtable.length * 2];
duke@1 213 for (Scope s = this; s != null; s = s.next) {
duke@1 214 if (s.table == oldtable) {
duke@1 215 assert s == this || s.shared != 0;
duke@1 216 s.table = newtable;
duke@1 217 s.hashMask = newtable.length - 1;
duke@1 218 }
duke@1 219 }
jjg@738 220 int n = 0;
jjg@738 221 for (int i = oldtable.length; --i >= 0; ) {
jjg@738 222 Entry e = oldtable[i];
jjg@738 223 if (e != null && e != sentinel && ! e.isBogus()) {
jjg@738 224 table[getIndex(e.sym.name)] = e;
jjg@738 225 n++;
jjg@738 226 }
duke@1 227 }
jjg@738 228 // We don't need to update nelems for shared inherited scopes,
jjg@738 229 // since that gets handled by leave().
jjg@738 230 nelems = n;
duke@1 231 }
duke@1 232
duke@1 233 /** Enter symbol sym in this scope.
duke@1 234 */
duke@1 235 public void enter(Symbol sym) {
duke@1 236 assert shared == 0;
duke@1 237 enter(sym, this);
duke@1 238 }
duke@1 239
duke@1 240 public void enter(Symbol sym, Scope s) {
duke@1 241 enter(sym, s, s);
duke@1 242 }
duke@1 243
duke@1 244 /**
duke@1 245 * Enter symbol sym in this scope, but mark that it comes from
duke@1 246 * given scope `s' accessed through `origin'. The last two
duke@1 247 * arguments are only used in import scopes.
duke@1 248 */
duke@1 249 public void enter(Symbol sym, Scope s, Scope origin) {
duke@1 250 assert shared == 0;
jjg@738 251 if (nelems * 3 >= hashMask * 2)
jjg@738 252 dble();
jjg@738 253 int hash = getIndex(sym.name);
jjg@738 254 Entry old = table[hash];
jjg@738 255 if (old == null) {
jjg@738 256 old = sentinel;
jjg@738 257 nelems++;
jjg@738 258 }
jjg@738 259 Entry e = makeEntry(sym, old, elems, s, origin);
duke@1 260 table[hash] = e;
duke@1 261 elems = e;
mcimadamore@688 262 scopeCounter.inc();
duke@1 263 }
duke@1 264
duke@1 265 Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 266 return new Entry(sym, shadowed, sibling, scope);
duke@1 267 }
duke@1 268
duke@1 269 /** Remove symbol from this scope. Used when an inner class
duke@1 270 * attribute tells us that the class isn't a package member.
duke@1 271 */
duke@1 272 public void remove(Symbol sym) {
duke@1 273 assert shared == 0;
duke@1 274 Entry e = lookup(sym.name);
duke@1 275 if (e.scope == null) return;
duke@1 276
mcimadamore@688 277 scopeCounter.inc();
mcimadamore@688 278
duke@1 279 // remove e from table and shadowed list;
jjg@738 280 int i = getIndex(sym.name);
jjg@738 281 Entry te = table[i];
duke@1 282 if (te == e)
jjg@738 283 table[i] = e.shadowed;
duke@1 284 else while (true) {
duke@1 285 if (te.shadowed == e) {
duke@1 286 te.shadowed = e.shadowed;
duke@1 287 break;
duke@1 288 }
duke@1 289 te = te.shadowed;
duke@1 290 }
duke@1 291
duke@1 292 // remove e from elems and sibling list
duke@1 293 te = elems;
duke@1 294 if (te == e)
duke@1 295 elems = e.sibling;
duke@1 296 else while (true) {
duke@1 297 if (te.sibling == e) {
duke@1 298 te.sibling = e.sibling;
duke@1 299 break;
duke@1 300 }
duke@1 301 te = te.sibling;
duke@1 302 }
duke@1 303 }
duke@1 304
duke@1 305 /** Enter symbol sym in this scope if not already there.
duke@1 306 */
duke@1 307 public void enterIfAbsent(Symbol sym) {
duke@1 308 assert shared == 0;
duke@1 309 Entry e = lookup(sym.name);
duke@1 310 while (e.scope == this && e.sym.kind != sym.kind) e = e.next();
duke@1 311 if (e.scope != this) enter(sym);
duke@1 312 }
duke@1 313
duke@1 314 /** Given a class, is there already a class with same fully
duke@1 315 * qualified name in this (import) scope?
duke@1 316 */
duke@1 317 public boolean includes(Symbol c) {
duke@1 318 for (Scope.Entry e = lookup(c.name);
duke@1 319 e.scope == this;
duke@1 320 e = e.next()) {
duke@1 321 if (e.sym == c) return true;
duke@1 322 }
duke@1 323 return false;
duke@1 324 }
duke@1 325
mcimadamore@673 326 static final Filter<Symbol> noFilter = new Filter<Symbol>() {
mcimadamore@673 327 public boolean accepts(Symbol s) {
mcimadamore@673 328 return true;
mcimadamore@673 329 }
mcimadamore@673 330 };
mcimadamore@673 331
duke@1 332 /** Return the entry associated with given name, starting in
duke@1 333 * this scope and proceeding outwards. If no entry was found,
duke@1 334 * return the sentinel, which is characterized by having a null in
duke@1 335 * both its scope and sym fields, whereas both fields are non-null
duke@1 336 * for regular entries.
duke@1 337 */
duke@1 338 public Entry lookup(Name name) {
mcimadamore@673 339 return lookup(name, noFilter);
mcimadamore@673 340 }
mcimadamore@673 341 public Entry lookup(Name name, Filter<Symbol> sf) {
jjg@738 342 Entry e = table[getIndex(name)];
jjg@738 343 if (e == null || e == sentinel)
jjg@738 344 return sentinel;
mcimadamore@673 345 while (e.scope != null && (e.sym.name != name || !sf.accepts(e.sym)))
duke@1 346 e = e.shadowed;
duke@1 347 return e;
duke@1 348 }
duke@1 349
jjg@738 350 /*void dump (java.io.PrintStream out) {
jjg@738 351 out.println(this);
jjg@738 352 for (int l=0; l < table.length; l++) {
jjg@738 353 Entry le = table[l];
jjg@738 354 out.print("#"+l+": ");
jjg@738 355 if (le==sentinel) out.println("sentinel");
jjg@738 356 else if(le == null) out.println("null");
jjg@738 357 else out.println(""+le+" s:"+le.sym);
jjg@738 358 }
jjg@738 359 }*/
jjg@738 360
jjg@738 361 /** Look for slot in the table.
jjg@738 362 * We use open addressing with double hashing.
jjg@738 363 */
jjg@738 364 int getIndex (Name name) {
jjg@738 365 int h = name.hashCode();
jjg@738 366 int i = h & hashMask;
jjg@738 367 // The expression below is always odd, so it is guaranteed
jjg@738 368 // be be mutually prime with table.length, a power of 2.
jjg@738 369 int x = hashMask - ((h + (h >> 16)) << 1);
jjg@738 370 int d = -1; // Index of a deleted item.
jjg@738 371 for (;;) {
jjg@738 372 Entry e = table[i];
jjg@738 373 if (e == null)
jjg@738 374 return d >= 0 ? d : i;
jjg@738 375 if (e == sentinel) {
jjg@738 376 // We have to keep searching even if we see a deleted item.
jjg@738 377 // However, remember the index in case we fail to find the name.
jjg@738 378 if (d < 0)
jjg@738 379 d = i;
jjg@738 380 } else if (e.sym.name == name)
jjg@738 381 return i;
jjg@738 382 i = (i + x) & hashMask;
jjg@738 383 }
jjg@738 384 }
jjg@738 385
duke@1 386 public Iterable<Symbol> getElements() {
mcimadamore@673 387 return getElements(noFilter);
mcimadamore@673 388 }
mcimadamore@673 389
mcimadamore@673 390 public Iterable<Symbol> getElements(final Filter<Symbol> sf) {
duke@1 391 return new Iterable<Symbol>() {
duke@1 392 public Iterator<Symbol> iterator() {
duke@1 393 return new Iterator<Symbol>() {
duke@1 394 private Scope currScope = Scope.this;
duke@1 395 private Scope.Entry currEntry = elems;
duke@1 396 {
duke@1 397 update();
duke@1 398 }
duke@1 399
duke@1 400 public boolean hasNext() {
duke@1 401 return currEntry != null;
duke@1 402 }
duke@1 403
duke@1 404 public Symbol next() {
duke@1 405 Symbol sym = (currEntry == null ? null : currEntry.sym);
mcimadamore@673 406 if (currEntry != null) {
mcimadamore@673 407 currEntry = currEntry.sibling;
mcimadamore@673 408 }
duke@1 409 update();
duke@1 410 return sym;
duke@1 411 }
duke@1 412
duke@1 413 public void remove() {
duke@1 414 throw new UnsupportedOperationException();
duke@1 415 }
duke@1 416
duke@1 417 private void update() {
mcimadamore@673 418 skipToNextMatchingEntry();
duke@1 419 while (currEntry == null && currScope.next != null) {
duke@1 420 currScope = currScope.next;
duke@1 421 currEntry = currScope.elems;
mcimadamore@673 422 skipToNextMatchingEntry();
mcimadamore@673 423 }
mcimadamore@673 424 }
mcimadamore@673 425
mcimadamore@673 426 void skipToNextMatchingEntry() {
mcimadamore@673 427 while (currEntry != null && !sf.accepts(currEntry.sym)) {
mcimadamore@673 428 currEntry = currEntry.sibling;
duke@1 429 }
duke@1 430 }
duke@1 431 };
duke@1 432 }
duke@1 433 };
duke@1 434
duke@1 435 }
duke@1 436
duke@1 437 public String toString() {
duke@1 438 StringBuilder result = new StringBuilder();
duke@1 439 result.append("Scope[");
duke@1 440 for (Scope s = this; s != null ; s = s.next) {
duke@1 441 if (s != this) result.append(" | ");
duke@1 442 for (Entry e = s.elems; e != null; e = e.sibling) {
duke@1 443 if (e != s.elems) result.append(", ");
duke@1 444 result.append(e.sym);
duke@1 445 }
duke@1 446 }
duke@1 447 result.append("]");
duke@1 448 return result.toString();
duke@1 449 }
duke@1 450
duke@1 451 /** A class for scope entries.
duke@1 452 */
duke@1 453 public static class Entry {
duke@1 454
duke@1 455 /** The referenced symbol.
duke@1 456 * sym == null iff this == sentinel
duke@1 457 */
duke@1 458 public Symbol sym;
duke@1 459
duke@1 460 /** An entry with the same hash code, or sentinel.
duke@1 461 */
duke@1 462 private Entry shadowed;
duke@1 463
duke@1 464 /** Next entry in same scope.
duke@1 465 */
duke@1 466 public Entry sibling;
duke@1 467
duke@1 468 /** The entry's scope.
duke@1 469 * scope == null iff this == sentinel
duke@1 470 * for an entry in an import scope, this is the scope
duke@1 471 * where the entry came from (i.e. was imported from).
duke@1 472 */
duke@1 473 public Scope scope;
duke@1 474
duke@1 475 public Entry(Symbol sym, Entry shadowed, Entry sibling, Scope scope) {
duke@1 476 this.sym = sym;
duke@1 477 this.shadowed = shadowed;
duke@1 478 this.sibling = sibling;
duke@1 479 this.scope = scope;
duke@1 480 }
duke@1 481
duke@1 482 /** Return next entry with the same name as this entry, proceeding
duke@1 483 * outwards if not found in this scope.
duke@1 484 */
duke@1 485 public Entry next() {
jjg@738 486 return shadowed;
duke@1 487 }
duke@1 488
duke@1 489 public Scope getOrigin() {
duke@1 490 // The origin is only recorded for import scopes. For all
duke@1 491 // other scope entries, the "enclosing" type is available
duke@1 492 // from other sources. See Attr.visitSelect and
duke@1 493 // Attr.visitIdent. Rather than throwing an assertion
duke@1 494 // error, we return scope which will be the same as origin
duke@1 495 // in many cases.
duke@1 496 return scope;
duke@1 497 }
jjg@738 498
jjg@738 499 protected boolean isBogus () { return false; }
duke@1 500 }
duke@1 501
duke@1 502 public static class ImportScope extends Scope {
duke@1 503
duke@1 504 public ImportScope(Symbol owner) {
duke@1 505 super(owner);
duke@1 506 }
duke@1 507
duke@1 508 @Override
duke@1 509 Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 510 return new ImportEntry(sym, shadowed, sibling, scope, origin);
duke@1 511 }
duke@1 512
duke@1 513 public Entry lookup(Name name) {
jjg@738 514 Entry e = table[getIndex(name)];
jjg@738 515 if (e == null)
jjg@738 516 return sentinel;
jjg@738 517 while (e.isBogus())
duke@1 518 e = e.shadowed;
duke@1 519 return e;
duke@1 520 }
duke@1 521
duke@1 522 static class ImportEntry extends Entry {
duke@1 523 private Scope origin;
duke@1 524
duke@1 525 ImportEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 526 super(sym, shadowed, sibling, scope);
duke@1 527 this.origin = origin;
duke@1 528 }
duke@1 529 public Entry next() {
duke@1 530 Entry e = super.shadowed;
jjg@738 531 while (isBogus())
duke@1 532 e = e.shadowed;
duke@1 533 return e;
duke@1 534 }
duke@1 535
duke@1 536 @Override
duke@1 537 public Scope getOrigin() { return origin; }
jjg@738 538
jjg@738 539 /**
jjg@738 540 * Is this a bogus inner-class import?
jjg@738 541 * An inner class {@code Outer$Inner.class} read from a class file
jjg@738 542 * starts out in a package scope under the name {@code Outer$Inner},
jjg@738 543 * which (if star-imported) gets copied to the import scope.
jjg@738 544 * When the InnerClasses attribute is processed, the ClassSymbol
jjg@738 545 * is renamed in place (to {@code Inner}), and the owner changed
jjg@738 546 * to the {@code Outer} class. The ImportScope still has the old
jjg@738 547 * Entry that was created and hashed as {@code "Outer$Inner"},
jjg@738 548 * but whose name was changed to {@code "Inner"}. This violates
jjg@738 549 * the invariants for the Scope hash table, and so is pretty bogus.
jjg@738 550 * When the symbol was renamed, it should have been removed from
jjg@738 551 * the import scope (and not just the package scope); however,
jjg@738 552 * doing so is difficult. A better fix would be to change
jjg@738 553 * import scopes to indirectly reference package symbols, rather
jjg@738 554 * than copy from them.
jjg@738 555 * Until then, we detect and skip the bogus entries using this test.
jjg@738 556 */
jjg@738 557 protected boolean isBogus () { return sym.owner != scope.owner; }
duke@1 558 }
duke@1 559 }
duke@1 560
duke@1 561 /** An empty scope, into which you can't place anything. Used for
duke@1 562 * the scope for a variable initializer.
duke@1 563 */
duke@1 564 public static class DelegatedScope extends Scope {
duke@1 565 Scope delegatee;
duke@1 566 public static final Entry[] emptyTable = new Entry[0];
duke@1 567
duke@1 568 public DelegatedScope(Scope outer) {
mcimadamore@688 569 super(outer, outer.owner, emptyTable, outer.scopeCounter);
duke@1 570 delegatee = outer;
duke@1 571 }
duke@1 572 public Scope dup() {
duke@1 573 return new DelegatedScope(next);
duke@1 574 }
duke@1 575 public Scope dupUnshared() {
duke@1 576 return new DelegatedScope(next);
duke@1 577 }
duke@1 578 public Scope leave() {
duke@1 579 return next;
duke@1 580 }
duke@1 581 public void enter(Symbol sym) {
duke@1 582 // only anonymous classes could be put here
duke@1 583 }
duke@1 584 public void enter(Symbol sym, Scope s) {
duke@1 585 // only anonymous classes could be put here
duke@1 586 }
duke@1 587 public void remove(Symbol sym) {
duke@1 588 throw new AssertionError(sym);
duke@1 589 }
duke@1 590 public Entry lookup(Name name) {
duke@1 591 return delegatee.lookup(name);
duke@1 592 }
duke@1 593 }
duke@1 594
mcimadamore@688 595 /** A class scope, for which a scope counter should be provided */
mcimadamore@688 596 public static class ClassScope extends Scope {
mcimadamore@688 597
mcimadamore@688 598 ClassScope(Scope next, Symbol owner, Entry[] table, ScopeCounter scopeCounter) {
mcimadamore@688 599 super(next, owner, table, scopeCounter);
mcimadamore@688 600 }
mcimadamore@688 601
mcimadamore@688 602 public ClassScope(Symbol owner, ScopeCounter scopeCounter) {
mcimadamore@688 603 super(owner, scopeCounter);
mcimadamore@688 604 }
mcimadamore@688 605 }
mcimadamore@688 606
duke@1 607 /** An error scope, for which the owner should be an error symbol. */
duke@1 608 public static class ErrorScope extends Scope {
duke@1 609 ErrorScope(Scope next, Symbol errSymbol, Entry[] table) {
mcimadamore@688 610 super(next, /*owner=*/errSymbol, table, dummyCounter);
duke@1 611 }
duke@1 612 public ErrorScope(Symbol errSymbol) {
duke@1 613 super(errSymbol);
duke@1 614 }
duke@1 615 public Scope dup() {
duke@1 616 return new ErrorScope(this, owner, table);
duke@1 617 }
duke@1 618 public Scope dupUnshared() {
duke@1 619 return new ErrorScope(this, owner, table.clone());
duke@1 620 }
duke@1 621 public Entry lookup(Name name) {
duke@1 622 Entry e = super.lookup(name);
duke@1 623 if (e.scope == null)
duke@1 624 return new Entry(owner, null, null, null);
duke@1 625 else
duke@1 626 return e;
duke@1 627 }
duke@1 628 }
duke@1 629 }

mercurial