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

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 554
9d9f26857129
child 673
7ae4016c5938
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 1999, 2008, 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
duke@1 34 * as hash tables. Scopes can be nested; the next field of a scope points
duke@1 35 * to its next outer scope. Nested scopes can share their hash tables.
duke@1 36 *
jjg@581 37 * <p><b>This is NOT part of any supported API.
jjg@581 38 * If you write code that depends on this, you do so at your own risk.
duke@1 39 * This code and its internal interfaces are subject to change or
duke@1 40 * deletion without notice.</b>
duke@1 41 */
duke@1 42 public class Scope {
duke@1 43
duke@1 44 /** The number of scopes that share this scope's hash table.
duke@1 45 */
duke@1 46 private int shared;
duke@1 47
duke@1 48 /** Next enclosing scope (with whom this scope may share a hashtable)
duke@1 49 */
duke@1 50 public Scope next;
duke@1 51
duke@1 52 /** The scope's owner.
duke@1 53 */
duke@1 54 public Symbol owner;
duke@1 55
duke@1 56 /** A hash table for the scope's entries.
duke@1 57 */
duke@1 58 public Entry[] table;
duke@1 59
duke@1 60 /** Mask for hash codes, always equal to (table.length - 1).
duke@1 61 */
duke@1 62 int hashMask;
duke@1 63
duke@1 64 /** A linear list that also contains all entries in
duke@1 65 * reverse order of appearance (i.e later entries are pushed on top).
duke@1 66 */
duke@1 67 public Entry elems;
duke@1 68
duke@1 69 /** The number of elements in this scope.
duke@1 70 */
duke@1 71 public int nelems = 0;
duke@1 72
duke@1 73 /** Every hash bucket is a list of Entry's which ends in sentinel.
duke@1 74 */
duke@1 75 private static final Entry sentinel = new Entry(null, null, null, null);
duke@1 76
duke@1 77 /** The hash table's initial size.
duke@1 78 */
duke@1 79 private static final int INITIAL_SIZE = 0x10;
duke@1 80
duke@1 81 /** A value for the empty scope.
duke@1 82 */
duke@1 83 public static final Scope emptyScope = new Scope(null, null, new Entry[]{});
duke@1 84
duke@1 85 /** Construct a new scope, within scope next, with given owner, using
duke@1 86 * given table. The table's length must be an exponent of 2.
duke@1 87 */
duke@1 88 Scope(Scope next, Symbol owner, Entry[] table) {
duke@1 89 this.next = next;
duke@1 90 assert emptyScope == null || owner != null;
duke@1 91 this.owner = owner;
duke@1 92 this.table = table;
duke@1 93 this.hashMask = table.length - 1;
duke@1 94 this.elems = null;
duke@1 95 this.nelems = 0;
duke@1 96 this.shared = 0;
duke@1 97 }
duke@1 98
duke@1 99 /** Construct a new scope, within scope next, with given owner,
duke@1 100 * using a fresh table of length INITIAL_SIZE.
duke@1 101 */
duke@1 102 public Scope(Symbol owner) {
duke@1 103 this(null, owner, new Entry[INITIAL_SIZE]);
duke@1 104 for (int i = 0; i < INITIAL_SIZE; i++) table[i] = sentinel;
duke@1 105 }
duke@1 106
duke@1 107 /** Construct a fresh scope within this scope, with same owner,
duke@1 108 * which shares its table with the outer scope. Used in connection with
duke@1 109 * method leave if scope access is stack-like in order to avoid allocation
duke@1 110 * of fresh tables.
duke@1 111 */
duke@1 112 public Scope dup() {
duke@1 113 Scope result = new Scope(this, this.owner, this.table);
duke@1 114 shared++;
duke@1 115 // System.out.println("====> duping scope " + this.hashCode() + " owned by " + this.owner + " to " + result.hashCode());
duke@1 116 // new Error().printStackTrace(System.out);
duke@1 117 return result;
duke@1 118 }
duke@1 119
duke@1 120 /** Construct a fresh scope within this scope, with new owner,
duke@1 121 * which shares its table with the outer scope. Used in connection with
duke@1 122 * method leave if scope access is stack-like in order to avoid allocation
duke@1 123 * of fresh tables.
duke@1 124 */
duke@1 125 public Scope dup(Symbol newOwner) {
duke@1 126 Scope result = new Scope(this, newOwner, this.table);
duke@1 127 shared++;
duke@1 128 // System.out.println("====> duping scope " + this.hashCode() + " owned by " + newOwner + " to " + result.hashCode());
duke@1 129 // new Error().printStackTrace(System.out);
duke@1 130 return result;
duke@1 131 }
duke@1 132
duke@1 133 /** Construct a fresh scope within this scope, with same owner,
duke@1 134 * with a new hash table, whose contents initially are those of
duke@1 135 * the table of its outer scope.
duke@1 136 */
duke@1 137 public Scope dupUnshared() {
duke@1 138 return new Scope(this, this.owner, this.table.clone());
duke@1 139 }
duke@1 140
duke@1 141 /** Remove all entries of this scope from its table, if shared
duke@1 142 * with next.
duke@1 143 */
duke@1 144 public Scope leave() {
duke@1 145 assert shared == 0;
duke@1 146 if (table != next.table) return next;
duke@1 147 while (elems != null) {
jjg@113 148 int hash = elems.sym.name.hashCode() & hashMask;
duke@1 149 Entry e = table[hash];
duke@1 150 assert e == elems : elems.sym;
duke@1 151 table[hash] = elems.shadowed;
duke@1 152 elems = elems.sibling;
duke@1 153 }
duke@1 154 assert next.shared > 0;
duke@1 155 next.shared--;
duke@1 156 // System.out.println("====> leaving scope " + this.hashCode() + " owned by " + this.owner + " to " + next.hashCode());
duke@1 157 // new Error().printStackTrace(System.out);
duke@1 158 return next;
duke@1 159 }
duke@1 160
duke@1 161 /** Double size of hash table.
duke@1 162 */
duke@1 163 private void dble() {
duke@1 164 assert shared == 0;
duke@1 165 Entry[] oldtable = table;
duke@1 166 Entry[] newtable = new Entry[oldtable.length * 2];
duke@1 167 for (Scope s = this; s != null; s = s.next) {
duke@1 168 if (s.table == oldtable) {
duke@1 169 assert s == this || s.shared != 0;
duke@1 170 s.table = newtable;
duke@1 171 s.hashMask = newtable.length - 1;
duke@1 172 }
duke@1 173 }
duke@1 174 for (int i = 0; i < newtable.length; i++) newtable[i] = sentinel;
duke@1 175 for (int i = 0; i < oldtable.length; i++) copy(oldtable[i]);
duke@1 176 }
duke@1 177
duke@1 178 /** Copy the given entry and all entries shadowed by it to table
duke@1 179 */
duke@1 180 private void copy(Entry e) {
duke@1 181 if (e.sym != null) {
duke@1 182 copy(e.shadowed);
jjg@113 183 int hash = e.sym.name.hashCode() & hashMask;
duke@1 184 e.shadowed = table[hash];
duke@1 185 table[hash] = e;
duke@1 186 }
duke@1 187 }
duke@1 188
duke@1 189 /** Enter symbol sym in this scope.
duke@1 190 */
duke@1 191 public void enter(Symbol sym) {
duke@1 192 assert shared == 0;
duke@1 193 enter(sym, this);
duke@1 194 }
duke@1 195
duke@1 196 public void enter(Symbol sym, Scope s) {
duke@1 197 enter(sym, s, s);
duke@1 198 }
duke@1 199
duke@1 200 /**
duke@1 201 * Enter symbol sym in this scope, but mark that it comes from
duke@1 202 * given scope `s' accessed through `origin'. The last two
duke@1 203 * arguments are only used in import scopes.
duke@1 204 */
duke@1 205 public void enter(Symbol sym, Scope s, Scope origin) {
duke@1 206 assert shared == 0;
duke@1 207 // Temporarily disabled (bug 6460352):
duke@1 208 // if (nelems * 3 >= hashMask * 2) dble();
jjg@113 209 int hash = sym.name.hashCode() & hashMask;
duke@1 210 Entry e = makeEntry(sym, table[hash], elems, s, origin);
duke@1 211 table[hash] = e;
duke@1 212 elems = e;
duke@1 213 nelems++;
duke@1 214 }
duke@1 215
duke@1 216 Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 217 return new Entry(sym, shadowed, sibling, scope);
duke@1 218 }
duke@1 219
duke@1 220 /** Remove symbol from this scope. Used when an inner class
duke@1 221 * attribute tells us that the class isn't a package member.
duke@1 222 */
duke@1 223 public void remove(Symbol sym) {
duke@1 224 assert shared == 0;
duke@1 225 Entry e = lookup(sym.name);
duke@1 226 while (e.scope == this && e.sym != sym) e = e.next();
duke@1 227 if (e.scope == null) return;
duke@1 228
duke@1 229 // remove e from table and shadowed list;
jjg@113 230 Entry te = table[sym.name.hashCode() & hashMask];
duke@1 231 if (te == e)
jjg@113 232 table[sym.name.hashCode() & hashMask] = e.shadowed;
duke@1 233 else while (true) {
duke@1 234 if (te.shadowed == e) {
duke@1 235 te.shadowed = e.shadowed;
duke@1 236 break;
duke@1 237 }
duke@1 238 te = te.shadowed;
duke@1 239 }
duke@1 240
duke@1 241 // remove e from elems and sibling list
duke@1 242 te = elems;
duke@1 243 if (te == e)
duke@1 244 elems = e.sibling;
duke@1 245 else while (true) {
duke@1 246 if (te.sibling == e) {
duke@1 247 te.sibling = e.sibling;
duke@1 248 break;
duke@1 249 }
duke@1 250 te = te.sibling;
duke@1 251 }
duke@1 252 }
duke@1 253
duke@1 254 /** Enter symbol sym in this scope if not already there.
duke@1 255 */
duke@1 256 public void enterIfAbsent(Symbol sym) {
duke@1 257 assert shared == 0;
duke@1 258 Entry e = lookup(sym.name);
duke@1 259 while (e.scope == this && e.sym.kind != sym.kind) e = e.next();
duke@1 260 if (e.scope != this) enter(sym);
duke@1 261 }
duke@1 262
duke@1 263 /** Given a class, is there already a class with same fully
duke@1 264 * qualified name in this (import) scope?
duke@1 265 */
duke@1 266 public boolean includes(Symbol c) {
duke@1 267 for (Scope.Entry e = lookup(c.name);
duke@1 268 e.scope == this;
duke@1 269 e = e.next()) {
duke@1 270 if (e.sym == c) return true;
duke@1 271 }
duke@1 272 return false;
duke@1 273 }
duke@1 274
duke@1 275 /** Return the entry associated with given name, starting in
duke@1 276 * this scope and proceeding outwards. If no entry was found,
duke@1 277 * return the sentinel, which is characterized by having a null in
duke@1 278 * both its scope and sym fields, whereas both fields are non-null
duke@1 279 * for regular entries.
duke@1 280 */
duke@1 281 public Entry lookup(Name name) {
jjg@113 282 Entry e = table[name.hashCode() & hashMask];
duke@1 283 while (e.scope != null && e.sym.name != name)
duke@1 284 e = e.shadowed;
duke@1 285 return e;
duke@1 286 }
duke@1 287
duke@1 288 public Iterable<Symbol> getElements() {
duke@1 289 return new Iterable<Symbol>() {
duke@1 290 public Iterator<Symbol> iterator() {
duke@1 291 return new Iterator<Symbol>() {
duke@1 292 private Scope currScope = Scope.this;
duke@1 293 private Scope.Entry currEntry = elems;
duke@1 294 {
duke@1 295 update();
duke@1 296 }
duke@1 297
duke@1 298 public boolean hasNext() {
duke@1 299 return currEntry != null;
duke@1 300 }
duke@1 301
duke@1 302 public Symbol next() {
duke@1 303 Symbol sym = (currEntry == null ? null : currEntry.sym);
duke@1 304 currEntry = currEntry.sibling;
duke@1 305 update();
duke@1 306 return sym;
duke@1 307 }
duke@1 308
duke@1 309 public void remove() {
duke@1 310 throw new UnsupportedOperationException();
duke@1 311 }
duke@1 312
duke@1 313 private void update() {
duke@1 314 while (currEntry == null && currScope.next != null) {
duke@1 315 currScope = currScope.next;
duke@1 316 currEntry = currScope.elems;
duke@1 317 }
duke@1 318 }
duke@1 319 };
duke@1 320 }
duke@1 321 };
duke@1 322
duke@1 323 }
duke@1 324
duke@1 325 public String toString() {
duke@1 326 StringBuilder result = new StringBuilder();
duke@1 327 result.append("Scope[");
duke@1 328 for (Scope s = this; s != null ; s = s.next) {
duke@1 329 if (s != this) result.append(" | ");
duke@1 330 for (Entry e = s.elems; e != null; e = e.sibling) {
duke@1 331 if (e != s.elems) result.append(", ");
duke@1 332 result.append(e.sym);
duke@1 333 }
duke@1 334 }
duke@1 335 result.append("]");
duke@1 336 return result.toString();
duke@1 337 }
duke@1 338
duke@1 339 /** A class for scope entries.
duke@1 340 */
duke@1 341 public static class Entry {
duke@1 342
duke@1 343 /** The referenced symbol.
duke@1 344 * sym == null iff this == sentinel
duke@1 345 */
duke@1 346 public Symbol sym;
duke@1 347
duke@1 348 /** An entry with the same hash code, or sentinel.
duke@1 349 */
duke@1 350 private Entry shadowed;
duke@1 351
duke@1 352 /** Next entry in same scope.
duke@1 353 */
duke@1 354 public Entry sibling;
duke@1 355
duke@1 356 /** The entry's scope.
duke@1 357 * scope == null iff this == sentinel
duke@1 358 * for an entry in an import scope, this is the scope
duke@1 359 * where the entry came from (i.e. was imported from).
duke@1 360 */
duke@1 361 public Scope scope;
duke@1 362
duke@1 363 public Entry(Symbol sym, Entry shadowed, Entry sibling, Scope scope) {
duke@1 364 this.sym = sym;
duke@1 365 this.shadowed = shadowed;
duke@1 366 this.sibling = sibling;
duke@1 367 this.scope = scope;
duke@1 368 }
duke@1 369
duke@1 370 /** Return next entry with the same name as this entry, proceeding
duke@1 371 * outwards if not found in this scope.
duke@1 372 */
duke@1 373 public Entry next() {
duke@1 374 Entry e = shadowed;
duke@1 375 while (e.scope != null && e.sym.name != sym.name)
duke@1 376 e = e.shadowed;
duke@1 377 return e;
duke@1 378 }
duke@1 379
duke@1 380 public Scope getOrigin() {
duke@1 381 // The origin is only recorded for import scopes. For all
duke@1 382 // other scope entries, the "enclosing" type is available
duke@1 383 // from other sources. See Attr.visitSelect and
duke@1 384 // Attr.visitIdent. Rather than throwing an assertion
duke@1 385 // error, we return scope which will be the same as origin
duke@1 386 // in many cases.
duke@1 387 return scope;
duke@1 388 }
duke@1 389 }
duke@1 390
duke@1 391 public static class ImportScope extends Scope {
duke@1 392
duke@1 393 public ImportScope(Symbol owner) {
duke@1 394 super(owner);
duke@1 395 }
duke@1 396
duke@1 397 @Override
duke@1 398 Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 399 return new ImportEntry(sym, shadowed, sibling, scope, origin);
duke@1 400 }
duke@1 401
duke@1 402 public Entry lookup(Name name) {
jjg@113 403 Entry e = table[name.hashCode() & hashMask];
duke@1 404 while (e.scope != null &&
duke@1 405 (e.sym.name != name ||
duke@1 406 /* Since an inner class will show up in package and
duke@1 407 * import scopes until its inner class attribute has
duke@1 408 * been processed, we have to weed it out here. This
duke@1 409 * is done by comparing the owners of the entry's
duke@1 410 * scope and symbol fields. The scope field's owner
duke@1 411 * points to where the class originally was imported
duke@1 412 * from. The symbol field's owner points to where the
duke@1 413 * class is situated now. This can change when an
duke@1 414 * inner class is read (see ClassReader.enterClass).
duke@1 415 * By comparing the two fields we make sure that we do
duke@1 416 * not accidentally import an inner class that started
duke@1 417 * life as a flat class in a package. */
duke@1 418 e.sym.owner != e.scope.owner))
duke@1 419 e = e.shadowed;
duke@1 420 return e;
duke@1 421 }
duke@1 422
duke@1 423 static class ImportEntry extends Entry {
duke@1 424 private Scope origin;
duke@1 425
duke@1 426 ImportEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 427 super(sym, shadowed, sibling, scope);
duke@1 428 this.origin = origin;
duke@1 429 }
duke@1 430 public Entry next() {
duke@1 431 Entry e = super.shadowed;
duke@1 432 while (e.scope != null &&
duke@1 433 (e.sym.name != sym.name ||
duke@1 434 e.sym.owner != e.scope.owner)) // see lookup()
duke@1 435 e = e.shadowed;
duke@1 436 return e;
duke@1 437 }
duke@1 438
duke@1 439 @Override
duke@1 440 public Scope getOrigin() { return origin; }
duke@1 441 }
duke@1 442 }
duke@1 443
duke@1 444 /** An empty scope, into which you can't place anything. Used for
duke@1 445 * the scope for a variable initializer.
duke@1 446 */
duke@1 447 public static class DelegatedScope extends Scope {
duke@1 448 Scope delegatee;
duke@1 449 public static final Entry[] emptyTable = new Entry[0];
duke@1 450
duke@1 451 public DelegatedScope(Scope outer) {
duke@1 452 super(outer, outer.owner, emptyTable);
duke@1 453 delegatee = outer;
duke@1 454 }
duke@1 455 public Scope dup() {
duke@1 456 return new DelegatedScope(next);
duke@1 457 }
duke@1 458 public Scope dupUnshared() {
duke@1 459 return new DelegatedScope(next);
duke@1 460 }
duke@1 461 public Scope leave() {
duke@1 462 return next;
duke@1 463 }
duke@1 464 public void enter(Symbol sym) {
duke@1 465 // only anonymous classes could be put here
duke@1 466 }
duke@1 467 public void enter(Symbol sym, Scope s) {
duke@1 468 // only anonymous classes could be put here
duke@1 469 }
duke@1 470 public void remove(Symbol sym) {
duke@1 471 throw new AssertionError(sym);
duke@1 472 }
duke@1 473 public Entry lookup(Name name) {
duke@1 474 return delegatee.lookup(name);
duke@1 475 }
duke@1 476 }
duke@1 477
duke@1 478 /** An error scope, for which the owner should be an error symbol. */
duke@1 479 public static class ErrorScope extends Scope {
duke@1 480 ErrorScope(Scope next, Symbol errSymbol, Entry[] table) {
duke@1 481 super(next, /*owner=*/errSymbol, table);
duke@1 482 }
duke@1 483 public ErrorScope(Symbol errSymbol) {
duke@1 484 super(errSymbol);
duke@1 485 }
duke@1 486 public Scope dup() {
duke@1 487 return new ErrorScope(this, owner, table);
duke@1 488 }
duke@1 489 public Scope dupUnshared() {
duke@1 490 return new ErrorScope(this, owner, table.clone());
duke@1 491 }
duke@1 492 public Entry lookup(Name name) {
duke@1 493 Entry e = super.lookup(name);
duke@1 494 if (e.scope == null)
duke@1 495 return new Entry(owner, null, null, null);
duke@1 496 else
duke@1 497 return e;
duke@1 498 }
duke@1 499 }
duke@1 500 }

mercurial