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