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

Fri, 29 May 2015 10:15:36 +0530

author
sadayapalam
date
Fri, 29 May 2015 10:15:36 +0530
changeset 2812
9ec429ab0e7e
parent 1945
f7f271bd74a2
child 2893
ca5783d9a597
permissions
-rw-r--r--

8080842: Using Lambda Expression with name clash results in ClassFormatError
Summary: Ensure ScopeImpl can cope properly with remove when a field and method share the name
Reviewed-by: mcimadamore, jlahoda

duke@1 1 /*
sadayapalam@2812 2 * Copyright (c) 1999, 2015, 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) {
mcimadamore@1945 202 enter(sym, s, s, false);
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 */
mcimadamore@1945 210 public void enter(Symbol sym, Scope s, Scope origin, boolean staticallyImported) {
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 }
mcimadamore@1945 220 Entry e = makeEntry(sym, old, elems, s, origin, staticallyImported);
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
mcimadamore@1945 230 Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin, boolean staticallyImported) {
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
sadayapalam@2812 244 /** Remove symbol from this scope.
duke@1 245 */
sadayapalam@2812 246 public void remove(final Symbol sym) {
jjg@816 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 });
duke@1 254 if (e.scope == null) return;
duke@1 255
duke@1 256 // remove e from table and shadowed list;
jjg@738 257 int i = getIndex(sym.name);
jjg@738 258 Entry te = table[i];
duke@1 259 if (te == e)
jjg@738 260 table[i] = e.shadowed;
duke@1 261 else while (true) {
duke@1 262 if (te.shadowed == e) {
duke@1 263 te.shadowed = e.shadowed;
duke@1 264 break;
duke@1 265 }
duke@1 266 te = te.shadowed;
duke@1 267 }
duke@1 268
duke@1 269 // remove e from elems and sibling list
duke@1 270 te = elems;
duke@1 271 if (te == e)
duke@1 272 elems = e.sibling;
duke@1 273 else while (true) {
duke@1 274 if (te.sibling == e) {
duke@1 275 te.sibling = e.sibling;
duke@1 276 break;
duke@1 277 }
duke@1 278 te = te.sibling;
duke@1 279 }
jjg@767 280
mcimadamore@877 281 //notify listeners
mcimadamore@877 282 for (List<ScopeListener> l = listeners; l.nonEmpty(); l = l.tail) {
mcimadamore@877 283 l.head.symbolRemoved(sym, this);
jjg@767 284 }
duke@1 285 }
duke@1 286
duke@1 287 /** Enter symbol sym in this scope if not already there.
duke@1 288 */
duke@1 289 public void enterIfAbsent(Symbol sym) {
jjg@816 290 Assert.check(shared == 0);
duke@1 291 Entry e = lookup(sym.name);
duke@1 292 while (e.scope == this && e.sym.kind != sym.kind) e = e.next();
duke@1 293 if (e.scope != this) enter(sym);
duke@1 294 }
duke@1 295
duke@1 296 /** Given a class, is there already a class with same fully
duke@1 297 * qualified name in this (import) scope?
duke@1 298 */
duke@1 299 public boolean includes(Symbol c) {
duke@1 300 for (Scope.Entry e = lookup(c.name);
duke@1 301 e.scope == this;
duke@1 302 e = e.next()) {
duke@1 303 if (e.sym == c) return true;
duke@1 304 }
duke@1 305 return false;
duke@1 306 }
duke@1 307
mcimadamore@673 308 static final Filter<Symbol> noFilter = new Filter<Symbol>() {
mcimadamore@673 309 public boolean accepts(Symbol s) {
mcimadamore@673 310 return true;
mcimadamore@673 311 }
mcimadamore@673 312 };
mcimadamore@673 313
duke@1 314 /** Return the entry associated with given name, starting in
duke@1 315 * this scope and proceeding outwards. If no entry was found,
duke@1 316 * return the sentinel, which is characterized by having a null in
duke@1 317 * both its scope and sym fields, whereas both fields are non-null
duke@1 318 * for regular entries.
duke@1 319 */
duke@1 320 public Entry lookup(Name name) {
mcimadamore@673 321 return lookup(name, noFilter);
mcimadamore@673 322 }
vromero@1886 323
mcimadamore@673 324 public Entry lookup(Name name, Filter<Symbol> sf) {
jjg@738 325 Entry e = table[getIndex(name)];
jjg@738 326 if (e == null || e == sentinel)
jjg@738 327 return sentinel;
mcimadamore@673 328 while (e.scope != null && (e.sym.name != name || !sf.accepts(e.sym)))
duke@1 329 e = e.shadowed;
duke@1 330 return e;
duke@1 331 }
duke@1 332
jjg@738 333 /*void dump (java.io.PrintStream out) {
jjg@738 334 out.println(this);
jjg@738 335 for (int l=0; l < table.length; l++) {
jjg@738 336 Entry le = table[l];
jjg@738 337 out.print("#"+l+": ");
jjg@738 338 if (le==sentinel) out.println("sentinel");
jjg@738 339 else if(le == null) out.println("null");
jjg@738 340 else out.println(""+le+" s:"+le.sym);
jjg@738 341 }
jjg@738 342 }*/
jjg@738 343
jjg@738 344 /** Look for slot in the table.
jjg@738 345 * We use open addressing with double hashing.
jjg@738 346 */
jjg@738 347 int getIndex (Name name) {
jjg@738 348 int h = name.hashCode();
jjg@738 349 int i = h & hashMask;
jjg@738 350 // The expression below is always odd, so it is guaranteed
jjg@767 351 // to be mutually prime with table.length, a power of 2.
jjg@738 352 int x = hashMask - ((h + (h >> 16)) << 1);
jjg@738 353 int d = -1; // Index of a deleted item.
jjg@738 354 for (;;) {
jjg@738 355 Entry e = table[i];
jjg@738 356 if (e == null)
jjg@738 357 return d >= 0 ? d : i;
jjg@738 358 if (e == sentinel) {
jjg@738 359 // We have to keep searching even if we see a deleted item.
jjg@738 360 // However, remember the index in case we fail to find the name.
jjg@738 361 if (d < 0)
jjg@738 362 d = i;
jjg@738 363 } else if (e.sym.name == name)
jjg@738 364 return i;
jjg@738 365 i = (i + x) & hashMask;
jjg@738 366 }
jjg@738 367 }
jjg@738 368
vromero@1886 369 public boolean anyMatch(Filter<Symbol> sf) {
vromero@1886 370 return getElements(sf).iterator().hasNext();
vromero@1886 371 }
vromero@1886 372
duke@1 373 public Iterable<Symbol> getElements() {
mcimadamore@673 374 return getElements(noFilter);
mcimadamore@673 375 }
mcimadamore@673 376
mcimadamore@673 377 public Iterable<Symbol> getElements(final Filter<Symbol> sf) {
duke@1 378 return new Iterable<Symbol>() {
duke@1 379 public Iterator<Symbol> iterator() {
duke@1 380 return new Iterator<Symbol>() {
duke@1 381 private Scope currScope = Scope.this;
duke@1 382 private Scope.Entry currEntry = elems;
duke@1 383 {
duke@1 384 update();
duke@1 385 }
duke@1 386
duke@1 387 public boolean hasNext() {
duke@1 388 return currEntry != null;
duke@1 389 }
duke@1 390
duke@1 391 public Symbol next() {
duke@1 392 Symbol sym = (currEntry == null ? null : currEntry.sym);
mcimadamore@673 393 if (currEntry != null) {
mcimadamore@673 394 currEntry = currEntry.sibling;
mcimadamore@673 395 }
duke@1 396 update();
duke@1 397 return sym;
duke@1 398 }
duke@1 399
duke@1 400 public void remove() {
duke@1 401 throw new UnsupportedOperationException();
duke@1 402 }
duke@1 403
duke@1 404 private void update() {
mcimadamore@673 405 skipToNextMatchingEntry();
duke@1 406 while (currEntry == null && currScope.next != null) {
duke@1 407 currScope = currScope.next;
duke@1 408 currEntry = currScope.elems;
mcimadamore@673 409 skipToNextMatchingEntry();
mcimadamore@673 410 }
mcimadamore@673 411 }
mcimadamore@673 412
mcimadamore@673 413 void skipToNextMatchingEntry() {
mcimadamore@673 414 while (currEntry != null && !sf.accepts(currEntry.sym)) {
mcimadamore@673 415 currEntry = currEntry.sibling;
duke@1 416 }
duke@1 417 }
duke@1 418 };
duke@1 419 }
duke@1 420 };
mcimadamore@877 421 }
duke@1 422
mcimadamore@877 423 public Iterable<Symbol> getElementsByName(Name name) {
mcimadamore@877 424 return getElementsByName(name, noFilter);
mcimadamore@877 425 }
mcimadamore@877 426
mcimadamore@877 427 public Iterable<Symbol> getElementsByName(final Name name, final Filter<Symbol> sf) {
mcimadamore@877 428 return new Iterable<Symbol>() {
mcimadamore@877 429 public Iterator<Symbol> iterator() {
mcimadamore@877 430 return new Iterator<Symbol>() {
mcimadamore@877 431 Scope.Entry currentEntry = lookup(name, sf);
mcimadamore@877 432
mcimadamore@877 433 public boolean hasNext() {
mcimadamore@877 434 return currentEntry.scope != null;
mcimadamore@877 435 }
mcimadamore@877 436 public Symbol next() {
mcimadamore@877 437 Scope.Entry prevEntry = currentEntry;
mcimadamore@877 438 currentEntry = currentEntry.next(sf);
mcimadamore@877 439 return prevEntry.sym;
mcimadamore@877 440 }
mcimadamore@877 441 public void remove() {
mcimadamore@877 442 throw new UnsupportedOperationException();
mcimadamore@877 443 }
mcimadamore@877 444 };
mcimadamore@877 445 }
mcimadamore@877 446 };
duke@1 447 }
duke@1 448
duke@1 449 public String toString() {
duke@1 450 StringBuilder result = new StringBuilder();
duke@1 451 result.append("Scope[");
duke@1 452 for (Scope s = this; s != null ; s = s.next) {
duke@1 453 if (s != this) result.append(" | ");
duke@1 454 for (Entry e = s.elems; e != null; e = e.sibling) {
duke@1 455 if (e != s.elems) result.append(", ");
duke@1 456 result.append(e.sym);
duke@1 457 }
duke@1 458 }
duke@1 459 result.append("]");
duke@1 460 return result.toString();
duke@1 461 }
duke@1 462
duke@1 463 /** A class for scope entries.
duke@1 464 */
duke@1 465 public static class Entry {
duke@1 466
duke@1 467 /** The referenced symbol.
duke@1 468 * sym == null iff this == sentinel
duke@1 469 */
duke@1 470 public Symbol sym;
duke@1 471
duke@1 472 /** An entry with the same hash code, or sentinel.
duke@1 473 */
duke@1 474 private Entry shadowed;
duke@1 475
duke@1 476 /** Next entry in same scope.
duke@1 477 */
duke@1 478 public Entry sibling;
duke@1 479
duke@1 480 /** The entry's scope.
duke@1 481 * scope == null iff this == sentinel
duke@1 482 * for an entry in an import scope, this is the scope
duke@1 483 * where the entry came from (i.e. was imported from).
duke@1 484 */
duke@1 485 public Scope scope;
duke@1 486
duke@1 487 public Entry(Symbol sym, Entry shadowed, Entry sibling, Scope scope) {
duke@1 488 this.sym = sym;
duke@1 489 this.shadowed = shadowed;
duke@1 490 this.sibling = sibling;
duke@1 491 this.scope = scope;
duke@1 492 }
duke@1 493
duke@1 494 /** Return next entry with the same name as this entry, proceeding
duke@1 495 * outwards if not found in this scope.
duke@1 496 */
duke@1 497 public Entry next() {
jjg@738 498 return shadowed;
duke@1 499 }
duke@1 500
mcimadamore@780 501 public Entry next(Filter<Symbol> sf) {
mcimadamore@780 502 if (shadowed.sym == null || sf.accepts(shadowed.sym)) return shadowed;
mcimadamore@780 503 else return shadowed.next(sf);
mcimadamore@780 504 }
mcimadamore@780 505
mcimadamore@1945 506 public boolean isStaticallyImported() {
mcimadamore@1945 507 return false;
mcimadamore@1945 508 }
mcimadamore@1945 509
duke@1 510 public Scope getOrigin() {
duke@1 511 // The origin is only recorded for import scopes. For all
duke@1 512 // other scope entries, the "enclosing" type is available
duke@1 513 // from other sources. See Attr.visitSelect and
duke@1 514 // Attr.visitIdent. Rather than throwing an assertion
duke@1 515 // error, we return scope which will be the same as origin
duke@1 516 // in many cases.
duke@1 517 return scope;
duke@1 518 }
duke@1 519 }
duke@1 520
duke@1 521 public static class ImportScope extends Scope {
duke@1 522
duke@1 523 public ImportScope(Symbol owner) {
duke@1 524 super(owner);
duke@1 525 }
duke@1 526
duke@1 527 @Override
mcimadamore@1945 528 Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope,
mcimadamore@1945 529 final Scope origin, final boolean staticallyImported) {
mcimadamore@1945 530 return new Entry(sym, shadowed, sibling, scope) {
mcimadamore@1945 531 @Override
mcimadamore@1945 532 public Scope getOrigin() {
mcimadamore@1945 533 return origin;
mcimadamore@1945 534 }
duke@1 535
mcimadamore@1945 536 @Override
mcimadamore@1945 537 public boolean isStaticallyImported() {
mcimadamore@1945 538 return staticallyImported;
mcimadamore@1945 539 }
mcimadamore@1945 540 };
jjg@767 541 }
jjg@767 542 }
jjg@738 543
mcimadamore@877 544 public static class StarImportScope extends ImportScope implements ScopeListener {
jjg@767 545
jjg@767 546 public StarImportScope(Symbol owner) {
jjg@767 547 super(owner);
jjg@767 548 }
jjg@767 549
jjg@767 550 public void importAll (Scope fromScope) {
jjg@767 551 for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
jjg@767 552 if (e.sym.kind == Kinds.TYP && !includes(e.sym))
jjg@767 553 enter(e.sym, fromScope);
jjg@767 554 }
jjg@767 555 // Register to be notified when imported items are removed
mcimadamore@877 556 fromScope.addScopeListener(this);
duke@1 557 }
mcimadamore@877 558
mcimadamore@877 559 public void symbolRemoved(Symbol sym, Scope s) {
mcimadamore@877 560 remove(sym);
mcimadamore@877 561 }
mcimadamore@877 562 public void symbolAdded(Symbol sym, Scope s) { }
duke@1 563 }
duke@1 564
duke@1 565 /** An empty scope, into which you can't place anything. Used for
duke@1 566 * the scope for a variable initializer.
duke@1 567 */
duke@1 568 public static class DelegatedScope extends Scope {
duke@1 569 Scope delegatee;
duke@1 570 public static final Entry[] emptyTable = new Entry[0];
duke@1 571
duke@1 572 public DelegatedScope(Scope outer) {
mcimadamore@858 573 super(outer, outer.owner, emptyTable);
duke@1 574 delegatee = outer;
duke@1 575 }
duke@1 576 public Scope dup() {
duke@1 577 return new DelegatedScope(next);
duke@1 578 }
duke@1 579 public Scope dupUnshared() {
duke@1 580 return new DelegatedScope(next);
duke@1 581 }
duke@1 582 public Scope leave() {
duke@1 583 return next;
duke@1 584 }
duke@1 585 public void enter(Symbol sym) {
duke@1 586 // only anonymous classes could be put here
duke@1 587 }
duke@1 588 public void enter(Symbol sym, Scope s) {
duke@1 589 // only anonymous classes could be put here
duke@1 590 }
duke@1 591 public void remove(Symbol sym) {
duke@1 592 throw new AssertionError(sym);
duke@1 593 }
duke@1 594 public Entry lookup(Name name) {
duke@1 595 return delegatee.lookup(name);
duke@1 596 }
duke@1 597 }
duke@1 598
mcimadamore@877 599 /** A class scope adds capabilities to keep track of changes in related
mcimadamore@877 600 * class scopes - this allows client to realize whether a class scope
mcimadamore@877 601 * has changed, either directly (because a new member has been added/removed
mcimadamore@877 602 * to this scope) or indirectly (i.e. because a new member has been
mcimadamore@877 603 * added/removed into a supertype scope)
mcimadamore@877 604 */
mcimadamore@877 605 public static class CompoundScope extends Scope implements ScopeListener {
mcimadamore@877 606
mcimadamore@877 607 public static final Entry[] emptyTable = new Entry[0];
mcimadamore@877 608
mcimadamore@877 609 private List<Scope> subScopes = List.nil();
mcimadamore@877 610 private int mark = 0;
mcimadamore@877 611
mcimadamore@877 612 public CompoundScope(Symbol owner) {
mcimadamore@877 613 super(null, owner, emptyTable);
mcimadamore@877 614 }
mcimadamore@877 615
mcimadamore@877 616 public void addSubScope(Scope that) {
mcimadamore@877 617 if (that != null) {
mcimadamore@877 618 subScopes = subScopes.prepend(that);
mcimadamore@877 619 that.addScopeListener(this);
mcimadamore@877 620 mark++;
mcimadamore@877 621 for (ScopeListener sl : listeners) {
mcimadamore@877 622 sl.symbolAdded(null, this); //propagate upwards in case of nested CompoundScopes
mcimadamore@877 623 }
mcimadamore@877 624 }
mcimadamore@877 625 }
mcimadamore@877 626
mcimadamore@877 627 public void symbolAdded(Symbol sym, Scope s) {
mcimadamore@877 628 mark++;
mcimadamore@877 629 for (ScopeListener sl : listeners) {
mcimadamore@877 630 sl.symbolAdded(sym, s);
mcimadamore@877 631 }
mcimadamore@877 632 }
mcimadamore@877 633
mcimadamore@877 634 public void symbolRemoved(Symbol sym, Scope s) {
mcimadamore@877 635 mark++;
mcimadamore@877 636 for (ScopeListener sl : listeners) {
mcimadamore@877 637 sl.symbolRemoved(sym, s);
mcimadamore@877 638 }
mcimadamore@877 639 }
mcimadamore@877 640
mcimadamore@877 641 public int getMark() {
mcimadamore@877 642 return mark;
mcimadamore@877 643 }
mcimadamore@877 644
mcimadamore@877 645 @Override
mcimadamore@877 646 public String toString() {
mcimadamore@877 647 StringBuilder buf = new StringBuilder();
mcimadamore@877 648 buf.append("CompoundScope{");
mcimadamore@877 649 String sep = "";
mcimadamore@877 650 for (Scope s : subScopes) {
mcimadamore@877 651 buf.append(sep);
mcimadamore@877 652 buf.append(s);
mcimadamore@877 653 sep = ",";
mcimadamore@877 654 }
mcimadamore@877 655 buf.append("}");
mcimadamore@877 656 return buf.toString();
mcimadamore@877 657 }
mcimadamore@877 658
mcimadamore@877 659 @Override
mcimadamore@877 660 public Iterable<Symbol> getElements(final Filter<Symbol> sf) {
mcimadamore@877 661 return new Iterable<Symbol>() {
mcimadamore@877 662 public Iterator<Symbol> iterator() {
mcimadamore@877 663 return new CompoundScopeIterator(subScopes) {
mcimadamore@877 664 Iterator<Symbol> nextIterator(Scope s) {
mcimadamore@982 665 return s.getElements(sf).iterator();
mcimadamore@877 666 }
mcimadamore@877 667 };
mcimadamore@877 668 }
mcimadamore@877 669 };
mcimadamore@877 670 }
mcimadamore@877 671
mcimadamore@877 672 @Override
mcimadamore@877 673 public Iterable<Symbol> getElementsByName(final Name name, final Filter<Symbol> sf) {
mcimadamore@877 674 return new Iterable<Symbol>() {
mcimadamore@877 675 public Iterator<Symbol> iterator() {
mcimadamore@877 676 return new CompoundScopeIterator(subScopes) {
mcimadamore@877 677 Iterator<Symbol> nextIterator(Scope s) {
mcimadamore@877 678 return s.getElementsByName(name, sf).iterator();
mcimadamore@877 679 }
mcimadamore@877 680 };
mcimadamore@877 681 }
mcimadamore@877 682 };
mcimadamore@877 683 }
mcimadamore@877 684
mcimadamore@877 685 abstract class CompoundScopeIterator implements Iterator<Symbol> {
mcimadamore@877 686
mcimadamore@877 687 private Iterator<Symbol> currentIterator;
mcimadamore@877 688 private List<Scope> scopesToScan;
mcimadamore@877 689
mcimadamore@877 690 public CompoundScopeIterator(List<Scope> scopesToScan) {
mcimadamore@877 691 this.scopesToScan = scopesToScan;
mcimadamore@877 692 update();
mcimadamore@877 693 }
mcimadamore@877 694
mcimadamore@877 695 abstract Iterator<Symbol> nextIterator(Scope s);
mcimadamore@877 696
mcimadamore@877 697 public boolean hasNext() {
mcimadamore@877 698 return currentIterator != null;
mcimadamore@877 699 }
mcimadamore@877 700
mcimadamore@877 701 public Symbol next() {
mcimadamore@877 702 Symbol sym = currentIterator.next();
mcimadamore@877 703 if (!currentIterator.hasNext()) {
mcimadamore@877 704 update();
mcimadamore@877 705 }
mcimadamore@877 706 return sym;
mcimadamore@877 707 }
mcimadamore@877 708
mcimadamore@877 709 public void remove() {
mcimadamore@877 710 throw new UnsupportedOperationException();
mcimadamore@877 711 }
mcimadamore@877 712
mcimadamore@877 713 private void update() {
mcimadamore@877 714 while (scopesToScan.nonEmpty()) {
mcimadamore@877 715 currentIterator = nextIterator(scopesToScan.head);
mcimadamore@877 716 scopesToScan = scopesToScan.tail;
mcimadamore@877 717 if (currentIterator.hasNext()) return;
mcimadamore@877 718 }
mcimadamore@877 719 currentIterator = null;
mcimadamore@877 720 }
mcimadamore@877 721 }
mcimadamore@877 722
mcimadamore@877 723 @Override
mcimadamore@877 724 public Entry lookup(Name name, Filter<Symbol> sf) {
mcimadamore@877 725 throw new UnsupportedOperationException();
mcimadamore@877 726 }
mcimadamore@877 727
mcimadamore@877 728 @Override
mcimadamore@877 729 public Scope dup(Symbol newOwner) {
mcimadamore@877 730 throw new UnsupportedOperationException();
mcimadamore@877 731 }
mcimadamore@877 732
mcimadamore@877 733 @Override
mcimadamore@1945 734 public void enter(Symbol sym, Scope s, Scope origin, boolean staticallyImported) {
mcimadamore@877 735 throw new UnsupportedOperationException();
mcimadamore@877 736 }
mcimadamore@877 737
mcimadamore@877 738 @Override
mcimadamore@877 739 public void remove(Symbol sym) {
mcimadamore@877 740 throw new UnsupportedOperationException();
mcimadamore@877 741 }
mcimadamore@877 742 }
mcimadamore@877 743
duke@1 744 /** An error scope, for which the owner should be an error symbol. */
duke@1 745 public static class ErrorScope extends Scope {
duke@1 746 ErrorScope(Scope next, Symbol errSymbol, Entry[] table) {
mcimadamore@858 747 super(next, /*owner=*/errSymbol, table);
duke@1 748 }
duke@1 749 public ErrorScope(Symbol errSymbol) {
duke@1 750 super(errSymbol);
duke@1 751 }
duke@1 752 public Scope dup() {
duke@1 753 return new ErrorScope(this, owner, table);
duke@1 754 }
duke@1 755 public Scope dupUnshared() {
duke@1 756 return new ErrorScope(this, owner, table.clone());
duke@1 757 }
duke@1 758 public Entry lookup(Name name) {
duke@1 759 Entry e = super.lookup(name);
duke@1 760 if (e.scope == null)
duke@1 761 return new Entry(owner, null, null, null);
duke@1 762 else
duke@1 763 return e;
duke@1 764 }
duke@1 765 }
duke@1 766 }

mercurial