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

Tue, 07 Sep 2010 17:31:54 +0100

author
mcimadamore
date
Tue, 07 Sep 2010 17:31:54 +0100
changeset 673
7ae4016c5938
parent 581
f2fdd52e4e87
child 688
50f9ac2f4730
permissions
-rw-r--r--

6337171: javac should create bridge methods when type variable bounds restricted
Summary: javac should add synthetic overrides for inherited abstract methods in order to preserve binary compatibility
Reviewed-by: jjg

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
mcimadamore@673 275 static final Filter<Symbol> noFilter = new Filter<Symbol>() {
mcimadamore@673 276 public boolean accepts(Symbol s) {
mcimadamore@673 277 return true;
mcimadamore@673 278 }
mcimadamore@673 279 };
mcimadamore@673 280
duke@1 281 /** Return the entry associated with given name, starting in
duke@1 282 * this scope and proceeding outwards. If no entry was found,
duke@1 283 * return the sentinel, which is characterized by having a null in
duke@1 284 * both its scope and sym fields, whereas both fields are non-null
duke@1 285 * for regular entries.
duke@1 286 */
duke@1 287 public Entry lookup(Name name) {
mcimadamore@673 288 return lookup(name, noFilter);
mcimadamore@673 289 }
mcimadamore@673 290 public Entry lookup(Name name, Filter<Symbol> sf) {
jjg@113 291 Entry e = table[name.hashCode() & hashMask];
mcimadamore@673 292 while (e.scope != null && (e.sym.name != name || !sf.accepts(e.sym)))
duke@1 293 e = e.shadowed;
duke@1 294 return e;
duke@1 295 }
duke@1 296
duke@1 297 public Iterable<Symbol> getElements() {
mcimadamore@673 298 return getElements(noFilter);
mcimadamore@673 299 }
mcimadamore@673 300
mcimadamore@673 301 public Iterable<Symbol> getElements(final Filter<Symbol> sf) {
duke@1 302 return new Iterable<Symbol>() {
duke@1 303 public Iterator<Symbol> iterator() {
duke@1 304 return new Iterator<Symbol>() {
duke@1 305 private Scope currScope = Scope.this;
duke@1 306 private Scope.Entry currEntry = elems;
duke@1 307 {
duke@1 308 update();
duke@1 309 }
duke@1 310
duke@1 311 public boolean hasNext() {
duke@1 312 return currEntry != null;
duke@1 313 }
duke@1 314
duke@1 315 public Symbol next() {
duke@1 316 Symbol sym = (currEntry == null ? null : currEntry.sym);
mcimadamore@673 317 if (currEntry != null) {
mcimadamore@673 318 currEntry = currEntry.sibling;
mcimadamore@673 319 }
duke@1 320 update();
duke@1 321 return sym;
duke@1 322 }
duke@1 323
duke@1 324 public void remove() {
duke@1 325 throw new UnsupportedOperationException();
duke@1 326 }
duke@1 327
duke@1 328 private void update() {
mcimadamore@673 329 skipToNextMatchingEntry();
duke@1 330 while (currEntry == null && currScope.next != null) {
duke@1 331 currScope = currScope.next;
duke@1 332 currEntry = currScope.elems;
mcimadamore@673 333 skipToNextMatchingEntry();
mcimadamore@673 334 }
mcimadamore@673 335 }
mcimadamore@673 336
mcimadamore@673 337 void skipToNextMatchingEntry() {
mcimadamore@673 338 while (currEntry != null && !sf.accepts(currEntry.sym)) {
mcimadamore@673 339 currEntry = currEntry.sibling;
duke@1 340 }
duke@1 341 }
duke@1 342 };
duke@1 343 }
duke@1 344 };
duke@1 345
duke@1 346 }
duke@1 347
duke@1 348 public String toString() {
duke@1 349 StringBuilder result = new StringBuilder();
duke@1 350 result.append("Scope[");
duke@1 351 for (Scope s = this; s != null ; s = s.next) {
duke@1 352 if (s != this) result.append(" | ");
duke@1 353 for (Entry e = s.elems; e != null; e = e.sibling) {
duke@1 354 if (e != s.elems) result.append(", ");
duke@1 355 result.append(e.sym);
duke@1 356 }
duke@1 357 }
duke@1 358 result.append("]");
duke@1 359 return result.toString();
duke@1 360 }
duke@1 361
duke@1 362 /** A class for scope entries.
duke@1 363 */
duke@1 364 public static class Entry {
duke@1 365
duke@1 366 /** The referenced symbol.
duke@1 367 * sym == null iff this == sentinel
duke@1 368 */
duke@1 369 public Symbol sym;
duke@1 370
duke@1 371 /** An entry with the same hash code, or sentinel.
duke@1 372 */
duke@1 373 private Entry shadowed;
duke@1 374
duke@1 375 /** Next entry in same scope.
duke@1 376 */
duke@1 377 public Entry sibling;
duke@1 378
duke@1 379 /** The entry's scope.
duke@1 380 * scope == null iff this == sentinel
duke@1 381 * for an entry in an import scope, this is the scope
duke@1 382 * where the entry came from (i.e. was imported from).
duke@1 383 */
duke@1 384 public Scope scope;
duke@1 385
duke@1 386 public Entry(Symbol sym, Entry shadowed, Entry sibling, Scope scope) {
duke@1 387 this.sym = sym;
duke@1 388 this.shadowed = shadowed;
duke@1 389 this.sibling = sibling;
duke@1 390 this.scope = scope;
duke@1 391 }
duke@1 392
duke@1 393 /** Return next entry with the same name as this entry, proceeding
duke@1 394 * outwards if not found in this scope.
duke@1 395 */
duke@1 396 public Entry next() {
duke@1 397 Entry e = shadowed;
duke@1 398 while (e.scope != null && e.sym.name != sym.name)
duke@1 399 e = e.shadowed;
duke@1 400 return e;
duke@1 401 }
duke@1 402
duke@1 403 public Scope getOrigin() {
duke@1 404 // The origin is only recorded for import scopes. For all
duke@1 405 // other scope entries, the "enclosing" type is available
duke@1 406 // from other sources. See Attr.visitSelect and
duke@1 407 // Attr.visitIdent. Rather than throwing an assertion
duke@1 408 // error, we return scope which will be the same as origin
duke@1 409 // in many cases.
duke@1 410 return scope;
duke@1 411 }
duke@1 412 }
duke@1 413
duke@1 414 public static class ImportScope extends Scope {
duke@1 415
duke@1 416 public ImportScope(Symbol owner) {
duke@1 417 super(owner);
duke@1 418 }
duke@1 419
duke@1 420 @Override
duke@1 421 Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 422 return new ImportEntry(sym, shadowed, sibling, scope, origin);
duke@1 423 }
duke@1 424
duke@1 425 public Entry lookup(Name name) {
jjg@113 426 Entry e = table[name.hashCode() & hashMask];
duke@1 427 while (e.scope != null &&
duke@1 428 (e.sym.name != name ||
duke@1 429 /* Since an inner class will show up in package and
duke@1 430 * import scopes until its inner class attribute has
duke@1 431 * been processed, we have to weed it out here. This
duke@1 432 * is done by comparing the owners of the entry's
duke@1 433 * scope and symbol fields. The scope field's owner
duke@1 434 * points to where the class originally was imported
duke@1 435 * from. The symbol field's owner points to where the
duke@1 436 * class is situated now. This can change when an
duke@1 437 * inner class is read (see ClassReader.enterClass).
duke@1 438 * By comparing the two fields we make sure that we do
duke@1 439 * not accidentally import an inner class that started
duke@1 440 * life as a flat class in a package. */
duke@1 441 e.sym.owner != e.scope.owner))
duke@1 442 e = e.shadowed;
duke@1 443 return e;
duke@1 444 }
duke@1 445
duke@1 446 static class ImportEntry extends Entry {
duke@1 447 private Scope origin;
duke@1 448
duke@1 449 ImportEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) {
duke@1 450 super(sym, shadowed, sibling, scope);
duke@1 451 this.origin = origin;
duke@1 452 }
duke@1 453 public Entry next() {
duke@1 454 Entry e = super.shadowed;
duke@1 455 while (e.scope != null &&
duke@1 456 (e.sym.name != sym.name ||
duke@1 457 e.sym.owner != e.scope.owner)) // see lookup()
duke@1 458 e = e.shadowed;
duke@1 459 return e;
duke@1 460 }
duke@1 461
duke@1 462 @Override
duke@1 463 public Scope getOrigin() { return origin; }
duke@1 464 }
duke@1 465 }
duke@1 466
duke@1 467 /** An empty scope, into which you can't place anything. Used for
duke@1 468 * the scope for a variable initializer.
duke@1 469 */
duke@1 470 public static class DelegatedScope extends Scope {
duke@1 471 Scope delegatee;
duke@1 472 public static final Entry[] emptyTable = new Entry[0];
duke@1 473
duke@1 474 public DelegatedScope(Scope outer) {
duke@1 475 super(outer, outer.owner, emptyTable);
duke@1 476 delegatee = outer;
duke@1 477 }
duke@1 478 public Scope dup() {
duke@1 479 return new DelegatedScope(next);
duke@1 480 }
duke@1 481 public Scope dupUnshared() {
duke@1 482 return new DelegatedScope(next);
duke@1 483 }
duke@1 484 public Scope leave() {
duke@1 485 return next;
duke@1 486 }
duke@1 487 public void enter(Symbol sym) {
duke@1 488 // only anonymous classes could be put here
duke@1 489 }
duke@1 490 public void enter(Symbol sym, Scope s) {
duke@1 491 // only anonymous classes could be put here
duke@1 492 }
duke@1 493 public void remove(Symbol sym) {
duke@1 494 throw new AssertionError(sym);
duke@1 495 }
duke@1 496 public Entry lookup(Name name) {
duke@1 497 return delegatee.lookup(name);
duke@1 498 }
duke@1 499 }
duke@1 500
duke@1 501 /** An error scope, for which the owner should be an error symbol. */
duke@1 502 public static class ErrorScope extends Scope {
duke@1 503 ErrorScope(Scope next, Symbol errSymbol, Entry[] table) {
duke@1 504 super(next, /*owner=*/errSymbol, table);
duke@1 505 }
duke@1 506 public ErrorScope(Symbol errSymbol) {
duke@1 507 super(errSymbol);
duke@1 508 }
duke@1 509 public Scope dup() {
duke@1 510 return new ErrorScope(this, owner, table);
duke@1 511 }
duke@1 512 public Scope dupUnshared() {
duke@1 513 return new ErrorScope(this, owner, table.clone());
duke@1 514 }
duke@1 515 public Entry lookup(Name name) {
duke@1 516 Entry e = super.lookup(name);
duke@1 517 if (e.scope == null)
duke@1 518 return new Entry(owner, null, null, null);
duke@1 519 else
duke@1 520 return e;
duke@1 521 }
duke@1 522 }
duke@1 523 }

mercurial