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