author | mcimadamore |
Wed, 13 Feb 2013 17:04:21 +0000 | |
changeset 15720 | e61b2f7a5148 |
parent 14258 | 8d2148961366 |
child 18899 | 2557b27d1f1c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14258 | 2 |
* Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.code; |
|
27 |
||
14258 | 28 |
import java.util.Iterator; |
29 |
||
10 | 30 |
import com.sun.tools.javac.util.*; |
31 |
||
32 |
/** A scope represents an area of visibility in a Java program. The |
|
33 |
* Scope class is a container for symbols which provides |
|
34 |
* efficient access to symbols given their names. Scopes are implemented |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
35 |
* as hash tables with "open addressing" and "double hashing". |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
36 |
* Scopes can be nested; the next field of a scope points |
10 | 37 |
* to its next outer scope. Nested scopes can share their hash tables. |
38 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
39 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
40 |
* If you write code that depends on this, you do so at your own risk. |
10 | 41 |
* This code and its internal interfaces are subject to change or |
42 |
* deletion without notice.</b> |
|
43 |
*/ |
|
44 |
public class Scope { |
|
45 |
||
46 |
/** The number of scopes that share this scope's hash table. |
|
47 |
*/ |
|
48 |
private int shared; |
|
49 |
||
50 |
/** Next enclosing scope (with whom this scope may share a hashtable) |
|
51 |
*/ |
|
52 |
public Scope next; |
|
53 |
||
54 |
/** The scope's owner. |
|
55 |
*/ |
|
56 |
public Symbol owner; |
|
57 |
||
58 |
/** A hash table for the scope's entries. |
|
59 |
*/ |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
60 |
Entry[] table; |
10 | 61 |
|
62 |
/** Mask for hash codes, always equal to (table.length - 1). |
|
63 |
*/ |
|
64 |
int hashMask; |
|
65 |
||
66 |
/** A linear list that also contains all entries in |
|
67 |
* reverse order of appearance (i.e later entries are pushed on top). |
|
68 |
*/ |
|
69 |
public Entry elems; |
|
70 |
||
71 |
/** The number of elements in this scope. |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
72 |
* This includes deleted elements, whose value is the sentinel. |
10 | 73 |
*/ |
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
74 |
int nelems = 0; |
10 | 75 |
|
7615 | 76 |
/** A list of scopes to be notified if items are to be removed from this scope. |
77 |
*/ |
|
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
78 |
List<ScopeListener> listeners = List.nil(); |
7615 | 79 |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
80 |
/** Use as a "not-found" result for lookup. |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
81 |
* Also used to mark deleted entries in the table. |
10 | 82 |
*/ |
83 |
private static final Entry sentinel = new Entry(null, null, null, null); |
|
84 |
||
85 |
/** The hash table's initial size. |
|
86 |
*/ |
|
87 |
private static final int INITIAL_SIZE = 0x10; |
|
88 |
||
89 |
/** A value for the empty scope. |
|
90 |
*/ |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
91 |
public static final Scope emptyScope = new Scope(null, null, new Entry[]{}); |
10 | 92 |
|
93 |
/** Construct a new scope, within scope next, with given owner, using |
|
94 |
* given table. The table's length must be an exponent of 2. |
|
95 |
*/ |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
96 |
private Scope(Scope next, Symbol owner, Entry[] table) { |
10 | 97 |
this.next = next; |
8032 | 98 |
Assert.check(emptyScope == null || owner != null); |
10 | 99 |
this.owner = owner; |
100 |
this.table = table; |
|
101 |
this.hashMask = table.length - 1; |
|
102 |
} |
|
103 |
||
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
104 |
/** Convenience constructor used for dup and dupUnshared. */ |
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
105 |
private Scope(Scope next, Symbol owner, Entry[] table, int nelems) { |
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
106 |
this(next, owner, table); |
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
107 |
this.nelems = nelems; |
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
108 |
} |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
109 |
|
10 | 110 |
/** Construct a new scope, within scope next, with given owner, |
111 |
* using a fresh table of length INITIAL_SIZE. |
|
112 |
*/ |
|
113 |
public Scope(Symbol owner) { |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
114 |
this(null, owner, new Entry[INITIAL_SIZE]); |
10 | 115 |
} |
116 |
||
117 |
/** Construct a fresh scope within this scope, with same owner, |
|
118 |
* which shares its table with the outer scope. Used in connection with |
|
119 |
* method leave if scope access is stack-like in order to avoid allocation |
|
120 |
* of fresh tables. |
|
121 |
*/ |
|
122 |
public Scope dup() { |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
123 |
return dup(this.owner); |
10 | 124 |
} |
125 |
||
126 |
/** Construct a fresh scope within this scope, with new owner, |
|
127 |
* which shares its table with the outer scope. Used in connection with |
|
128 |
* method leave if scope access is stack-like in order to avoid allocation |
|
129 |
* of fresh tables. |
|
130 |
*/ |
|
131 |
public Scope dup(Symbol newOwner) { |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
132 |
Scope result = new Scope(this, newOwner, this.table, this.nelems); |
10 | 133 |
shared++; |
134 |
// System.out.println("====> duping scope " + this.hashCode() + " owned by " + newOwner + " to " + result.hashCode()); |
|
135 |
// new Error().printStackTrace(System.out); |
|
136 |
return result; |
|
137 |
} |
|
138 |
||
139 |
/** Construct a fresh scope within this scope, with same owner, |
|
140 |
* with a new hash table, whose contents initially are those of |
|
141 |
* the table of its outer scope. |
|
142 |
*/ |
|
143 |
public Scope dupUnshared() { |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
144 |
return new Scope(this, this.owner, this.table.clone(), this.nelems); |
10 | 145 |
} |
146 |
||
147 |
/** Remove all entries of this scope from its table, if shared |
|
148 |
* with next. |
|
149 |
*/ |
|
150 |
public Scope leave() { |
|
8032 | 151 |
Assert.check(shared == 0); |
10 | 152 |
if (table != next.table) return next; |
153 |
while (elems != null) { |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
154 |
int hash = getIndex(elems.sym.name); |
10 | 155 |
Entry e = table[hash]; |
8032 | 156 |
Assert.check(e == elems, elems.sym); |
10 | 157 |
table[hash] = elems.shadowed; |
158 |
elems = elems.sibling; |
|
159 |
} |
|
8032 | 160 |
Assert.check(next.shared > 0); |
10 | 161 |
next.shared--; |
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
162 |
next.nelems = nelems; |
10 | 163 |
// System.out.println("====> leaving scope " + this.hashCode() + " owned by " + this.owner + " to " + next.hashCode()); |
164 |
// new Error().printStackTrace(System.out); |
|
165 |
return next; |
|
166 |
} |
|
167 |
||
168 |
/** Double size of hash table. |
|
169 |
*/ |
|
170 |
private void dble() { |
|
8032 | 171 |
Assert.check(shared == 0); |
10 | 172 |
Entry[] oldtable = table; |
173 |
Entry[] newtable = new Entry[oldtable.length * 2]; |
|
174 |
for (Scope s = this; s != null; s = s.next) { |
|
175 |
if (s.table == oldtable) { |
|
8032 | 176 |
Assert.check(s == this || s.shared != 0); |
10 | 177 |
s.table = newtable; |
178 |
s.hashMask = newtable.length - 1; |
|
179 |
} |
|
180 |
} |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
181 |
int n = 0; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
182 |
for (int i = oldtable.length; --i >= 0; ) { |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
183 |
Entry e = oldtable[i]; |
7615 | 184 |
if (e != null && e != sentinel) { |
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
185 |
table[getIndex(e.sym.name)] = e; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
186 |
n++; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
187 |
} |
10 | 188 |
} |
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
189 |
// We don't need to update nelems for shared inherited scopes, |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
190 |
// since that gets handled by leave(). |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
191 |
nelems = n; |
10 | 192 |
} |
193 |
||
194 |
/** Enter symbol sym in this scope. |
|
195 |
*/ |
|
196 |
public void enter(Symbol sym) { |
|
8032 | 197 |
Assert.check(shared == 0); |
10 | 198 |
enter(sym, this); |
199 |
} |
|
200 |
||
201 |
public void enter(Symbol sym, Scope s) { |
|
202 |
enter(sym, s, s); |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Enter symbol sym in this scope, but mark that it comes from |
|
207 |
* given scope `s' accessed through `origin'. The last two |
|
208 |
* arguments are only used in import scopes. |
|
209 |
*/ |
|
210 |
public void enter(Symbol sym, Scope s, Scope origin) { |
|
8032 | 211 |
Assert.check(shared == 0); |
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
212 |
if (nelems * 3 >= hashMask * 2) |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
213 |
dble(); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
214 |
int hash = getIndex(sym.name); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
215 |
Entry old = table[hash]; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
216 |
if (old == null) { |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
217 |
old = sentinel; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
218 |
nelems++; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
219 |
} |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
220 |
Entry e = makeEntry(sym, old, elems, s, origin); |
10 | 221 |
table[hash] = e; |
222 |
elems = e; |
|
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
223 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
224 |
//notify listeners |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
225 |
for (List<ScopeListener> l = listeners; l.nonEmpty(); l = l.tail) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
226 |
l.head.symbolAdded(sym, this); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
227 |
} |
10 | 228 |
} |
229 |
||
230 |
Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) { |
|
231 |
return new Entry(sym, shadowed, sibling, scope); |
|
232 |
} |
|
233 |
||
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
234 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
235 |
public interface ScopeListener { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
236 |
public void symbolAdded(Symbol sym, Scope s); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
237 |
public void symbolRemoved(Symbol sym, Scope s); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
238 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
239 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
240 |
public void addScopeListener(ScopeListener sl) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
241 |
listeners = listeners.prepend(sl); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
242 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
243 |
|
10 | 244 |
/** Remove symbol from this scope. Used when an inner class |
245 |
* attribute tells us that the class isn't a package member. |
|
246 |
*/ |
|
247 |
public void remove(Symbol sym) { |
|
8032 | 248 |
Assert.check(shared == 0); |
10 | 249 |
Entry e = lookup(sym.name); |
250 |
if (e.scope == null) return; |
|
251 |
||
252 |
// remove e from table and shadowed list; |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
253 |
int i = getIndex(sym.name); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
254 |
Entry te = table[i]; |
10 | 255 |
if (te == e) |
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
256 |
table[i] = e.shadowed; |
10 | 257 |
else while (true) { |
258 |
if (te.shadowed == e) { |
|
259 |
te.shadowed = e.shadowed; |
|
260 |
break; |
|
261 |
} |
|
262 |
te = te.shadowed; |
|
263 |
} |
|
264 |
||
265 |
// remove e from elems and sibling list |
|
266 |
te = elems; |
|
267 |
if (te == e) |
|
268 |
elems = e.sibling; |
|
269 |
else while (true) { |
|
270 |
if (te.sibling == e) { |
|
271 |
te.sibling = e.sibling; |
|
272 |
break; |
|
273 |
} |
|
274 |
te = te.sibling; |
|
275 |
} |
|
7615 | 276 |
|
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
277 |
//notify listeners |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
278 |
for (List<ScopeListener> l = listeners; l.nonEmpty(); l = l.tail) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
279 |
l.head.symbolRemoved(sym, this); |
7615 | 280 |
} |
10 | 281 |
} |
282 |
||
283 |
/** Enter symbol sym in this scope if not already there. |
|
284 |
*/ |
|
285 |
public void enterIfAbsent(Symbol sym) { |
|
8032 | 286 |
Assert.check(shared == 0); |
10 | 287 |
Entry e = lookup(sym.name); |
288 |
while (e.scope == this && e.sym.kind != sym.kind) e = e.next(); |
|
289 |
if (e.scope != this) enter(sym); |
|
290 |
} |
|
291 |
||
292 |
/** Given a class, is there already a class with same fully |
|
293 |
* qualified name in this (import) scope? |
|
294 |
*/ |
|
295 |
public boolean includes(Symbol c) { |
|
296 |
for (Scope.Entry e = lookup(c.name); |
|
297 |
e.scope == this; |
|
298 |
e = e.next()) { |
|
299 |
if (e.sym == c) return true; |
|
300 |
} |
|
301 |
return false; |
|
302 |
} |
|
303 |
||
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
304 |
static final Filter<Symbol> noFilter = new Filter<Symbol>() { |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
305 |
public boolean accepts(Symbol s) { |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
306 |
return true; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
307 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
308 |
}; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
309 |
|
10 | 310 |
/** Return the entry associated with given name, starting in |
311 |
* this scope and proceeding outwards. If no entry was found, |
|
312 |
* return the sentinel, which is characterized by having a null in |
|
313 |
* both its scope and sym fields, whereas both fields are non-null |
|
314 |
* for regular entries. |
|
315 |
*/ |
|
316 |
public Entry lookup(Name name) { |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
317 |
return lookup(name, noFilter); |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
318 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
319 |
public Entry lookup(Name name, Filter<Symbol> sf) { |
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
320 |
Entry e = table[getIndex(name)]; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
321 |
if (e == null || e == sentinel) |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
322 |
return sentinel; |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
323 |
while (e.scope != null && (e.sym.name != name || !sf.accepts(e.sym))) |
10 | 324 |
e = e.shadowed; |
325 |
return e; |
|
326 |
} |
|
327 |
||
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
328 |
/*void dump (java.io.PrintStream out) { |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
329 |
out.println(this); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
330 |
for (int l=0; l < table.length; l++) { |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
331 |
Entry le = table[l]; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
332 |
out.print("#"+l+": "); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
333 |
if (le==sentinel) out.println("sentinel"); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
334 |
else if(le == null) out.println("null"); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
335 |
else out.println(""+le+" s:"+le.sym); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
336 |
} |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
337 |
}*/ |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
338 |
|
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
339 |
/** Look for slot in the table. |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
340 |
* We use open addressing with double hashing. |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
341 |
*/ |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
342 |
int getIndex (Name name) { |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
343 |
int h = name.hashCode(); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
344 |
int i = h & hashMask; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
345 |
// The expression below is always odd, so it is guaranteed |
7615 | 346 |
// to be mutually prime with table.length, a power of 2. |
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
347 |
int x = hashMask - ((h + (h >> 16)) << 1); |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
348 |
int d = -1; // Index of a deleted item. |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
349 |
for (;;) { |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
350 |
Entry e = table[i]; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
351 |
if (e == null) |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
352 |
return d >= 0 ? d : i; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
353 |
if (e == sentinel) { |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
354 |
// We have to keep searching even if we see a deleted item. |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
355 |
// However, remember the index in case we fail to find the name. |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
356 |
if (d < 0) |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
357 |
d = i; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
358 |
} else if (e.sym.name == name) |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
359 |
return i; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
360 |
i = (i + x) & hashMask; |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
361 |
} |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
362 |
} |
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
363 |
|
10 | 364 |
public Iterable<Symbol> getElements() { |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
365 |
return getElements(noFilter); |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
366 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
367 |
|
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
368 |
public Iterable<Symbol> getElements(final Filter<Symbol> sf) { |
10 | 369 |
return new Iterable<Symbol>() { |
370 |
public Iterator<Symbol> iterator() { |
|
371 |
return new Iterator<Symbol>() { |
|
372 |
private Scope currScope = Scope.this; |
|
373 |
private Scope.Entry currEntry = elems; |
|
374 |
{ |
|
375 |
update(); |
|
376 |
} |
|
377 |
||
378 |
public boolean hasNext() { |
|
379 |
return currEntry != null; |
|
380 |
} |
|
381 |
||
382 |
public Symbol next() { |
|
383 |
Symbol sym = (currEntry == null ? null : currEntry.sym); |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
384 |
if (currEntry != null) { |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
385 |
currEntry = currEntry.sibling; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
386 |
} |
10 | 387 |
update(); |
388 |
return sym; |
|
389 |
} |
|
390 |
||
391 |
public void remove() { |
|
392 |
throw new UnsupportedOperationException(); |
|
393 |
} |
|
394 |
||
395 |
private void update() { |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
396 |
skipToNextMatchingEntry(); |
10 | 397 |
while (currEntry == null && currScope.next != null) { |
398 |
currScope = currScope.next; |
|
399 |
currEntry = currScope.elems; |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
400 |
skipToNextMatchingEntry(); |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
401 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
402 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
403 |
|
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
404 |
void skipToNextMatchingEntry() { |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
405 |
while (currEntry != null && !sf.accepts(currEntry.sym)) { |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
5847
diff
changeset
|
406 |
currEntry = currEntry.sibling; |
10 | 407 |
} |
408 |
} |
|
409 |
}; |
|
410 |
} |
|
411 |
}; |
|
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
412 |
} |
10 | 413 |
|
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
414 |
public Iterable<Symbol> getElementsByName(Name name) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
415 |
return getElementsByName(name, noFilter); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
416 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
417 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
418 |
public Iterable<Symbol> getElementsByName(final Name name, final Filter<Symbol> sf) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
419 |
return new Iterable<Symbol>() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
420 |
public Iterator<Symbol> iterator() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
421 |
return new Iterator<Symbol>() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
422 |
Scope.Entry currentEntry = lookup(name, sf); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
423 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
424 |
public boolean hasNext() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
425 |
return currentEntry.scope != null; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
426 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
427 |
public Symbol next() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
428 |
Scope.Entry prevEntry = currentEntry; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
429 |
currentEntry = currentEntry.next(sf); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
430 |
return prevEntry.sym; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
431 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
432 |
public void remove() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
433 |
throw new UnsupportedOperationException(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
434 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
435 |
}; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
436 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
437 |
}; |
10 | 438 |
} |
439 |
||
440 |
public String toString() { |
|
441 |
StringBuilder result = new StringBuilder(); |
|
442 |
result.append("Scope["); |
|
443 |
for (Scope s = this; s != null ; s = s.next) { |
|
444 |
if (s != this) result.append(" | "); |
|
445 |
for (Entry e = s.elems; e != null; e = e.sibling) { |
|
446 |
if (e != s.elems) result.append(", "); |
|
447 |
result.append(e.sym); |
|
448 |
} |
|
449 |
} |
|
450 |
result.append("]"); |
|
451 |
return result.toString(); |
|
452 |
} |
|
453 |
||
454 |
/** A class for scope entries. |
|
455 |
*/ |
|
456 |
public static class Entry { |
|
457 |
||
458 |
/** The referenced symbol. |
|
459 |
* sym == null iff this == sentinel |
|
460 |
*/ |
|
461 |
public Symbol sym; |
|
462 |
||
463 |
/** An entry with the same hash code, or sentinel. |
|
464 |
*/ |
|
465 |
private Entry shadowed; |
|
466 |
||
467 |
/** Next entry in same scope. |
|
468 |
*/ |
|
469 |
public Entry sibling; |
|
470 |
||
471 |
/** The entry's scope. |
|
472 |
* scope == null iff this == sentinel |
|
473 |
* for an entry in an import scope, this is the scope |
|
474 |
* where the entry came from (i.e. was imported from). |
|
475 |
*/ |
|
476 |
public Scope scope; |
|
477 |
||
478 |
public Entry(Symbol sym, Entry shadowed, Entry sibling, Scope scope) { |
|
479 |
this.sym = sym; |
|
480 |
this.shadowed = shadowed; |
|
481 |
this.sibling = sibling; |
|
482 |
this.scope = scope; |
|
483 |
} |
|
484 |
||
485 |
/** Return next entry with the same name as this entry, proceeding |
|
486 |
* outwards if not found in this scope. |
|
487 |
*/ |
|
488 |
public Entry next() { |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
489 |
return shadowed; |
10 | 490 |
} |
491 |
||
7628
e7baeb97d164
6476118: compiler bug causes runtime ClassCastException for generics overloading
mcimadamore
parents:
7615
diff
changeset
|
492 |
public Entry next(Filter<Symbol> sf) { |
e7baeb97d164
6476118: compiler bug causes runtime ClassCastException for generics overloading
mcimadamore
parents:
7615
diff
changeset
|
493 |
if (shadowed.sym == null || sf.accepts(shadowed.sym)) return shadowed; |
e7baeb97d164
6476118: compiler bug causes runtime ClassCastException for generics overloading
mcimadamore
parents:
7615
diff
changeset
|
494 |
else return shadowed.next(sf); |
e7baeb97d164
6476118: compiler bug causes runtime ClassCastException for generics overloading
mcimadamore
parents:
7615
diff
changeset
|
495 |
} |
e7baeb97d164
6476118: compiler bug causes runtime ClassCastException for generics overloading
mcimadamore
parents:
7615
diff
changeset
|
496 |
|
10 | 497 |
public Scope getOrigin() { |
498 |
// The origin is only recorded for import scopes. For all |
|
499 |
// other scope entries, the "enclosing" type is available |
|
500 |
// from other sources. See Attr.visitSelect and |
|
501 |
// Attr.visitIdent. Rather than throwing an assertion |
|
502 |
// error, we return scope which will be the same as origin |
|
503 |
// in many cases. |
|
504 |
return scope; |
|
505 |
} |
|
506 |
} |
|
507 |
||
508 |
public static class ImportScope extends Scope { |
|
509 |
||
510 |
public ImportScope(Symbol owner) { |
|
511 |
super(owner); |
|
512 |
} |
|
513 |
||
514 |
@Override |
|
515 |
Entry makeEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) { |
|
516 |
return new ImportEntry(sym, shadowed, sibling, scope, origin); |
|
517 |
} |
|
518 |
||
519 |
static class ImportEntry extends Entry { |
|
520 |
private Scope origin; |
|
521 |
||
522 |
ImportEntry(Symbol sym, Entry shadowed, Entry sibling, Scope scope, Scope origin) { |
|
523 |
super(sym, shadowed, sibling, scope); |
|
524 |
this.origin = origin; |
|
525 |
} |
|
526 |
||
527 |
@Override |
|
528 |
public Scope getOrigin() { return origin; } |
|
7615 | 529 |
} |
530 |
} |
|
7206
02edd110358f
6998063: new Scope impl to fix Scope performance issues
jjg
parents:
7079
diff
changeset
|
531 |
|
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
532 |
public static class StarImportScope extends ImportScope implements ScopeListener { |
7615 | 533 |
|
534 |
public StarImportScope(Symbol owner) { |
|
535 |
super(owner); |
|
536 |
} |
|
537 |
||
538 |
public void importAll (Scope fromScope) { |
|
539 |
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { |
|
540 |
if (e.sym.kind == Kinds.TYP && !includes(e.sym)) |
|
541 |
enter(e.sym, fromScope); |
|
542 |
} |
|
543 |
// Register to be notified when imported items are removed |
|
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
544 |
fromScope.addScopeListener(this); |
10 | 545 |
} |
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
546 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
547 |
public void symbolRemoved(Symbol sym, Scope s) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
548 |
remove(sym); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
549 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
550 |
public void symbolAdded(Symbol sym, Scope s) { } |
10 | 551 |
} |
552 |
||
553 |
/** An empty scope, into which you can't place anything. Used for |
|
554 |
* the scope for a variable initializer. |
|
555 |
*/ |
|
556 |
public static class DelegatedScope extends Scope { |
|
557 |
Scope delegatee; |
|
558 |
public static final Entry[] emptyTable = new Entry[0]; |
|
559 |
||
560 |
public DelegatedScope(Scope outer) { |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
561 |
super(outer, outer.owner, emptyTable); |
10 | 562 |
delegatee = outer; |
563 |
} |
|
564 |
public Scope dup() { |
|
565 |
return new DelegatedScope(next); |
|
566 |
} |
|
567 |
public Scope dupUnshared() { |
|
568 |
return new DelegatedScope(next); |
|
569 |
} |
|
570 |
public Scope leave() { |
|
571 |
return next; |
|
572 |
} |
|
573 |
public void enter(Symbol sym) { |
|
574 |
// only anonymous classes could be put here |
|
575 |
} |
|
576 |
public void enter(Symbol sym, Scope s) { |
|
577 |
// only anonymous classes could be put here |
|
578 |
} |
|
579 |
public void remove(Symbol sym) { |
|
580 |
throw new AssertionError(sym); |
|
581 |
} |
|
582 |
public Entry lookup(Name name) { |
|
583 |
return delegatee.lookup(name); |
|
584 |
} |
|
585 |
} |
|
586 |
||
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
587 |
/** A class scope adds capabilities to keep track of changes in related |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
588 |
* class scopes - this allows client to realize whether a class scope |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
589 |
* has changed, either directly (because a new member has been added/removed |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
590 |
* to this scope) or indirectly (i.e. because a new member has been |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
591 |
* added/removed into a supertype scope) |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
592 |
*/ |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
593 |
public static class CompoundScope extends Scope implements ScopeListener { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
594 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
595 |
public static final Entry[] emptyTable = new Entry[0]; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
596 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
597 |
private List<Scope> subScopes = List.nil(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
598 |
private int mark = 0; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
599 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
600 |
public CompoundScope(Symbol owner) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
601 |
super(null, owner, emptyTable); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
602 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
603 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
604 |
public void addSubScope(Scope that) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
605 |
if (that != null) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
606 |
subScopes = subScopes.prepend(that); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
607 |
that.addScopeListener(this); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
608 |
mark++; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
609 |
for (ScopeListener sl : listeners) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
610 |
sl.symbolAdded(null, this); //propagate upwards in case of nested CompoundScopes |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
611 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
612 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
613 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
614 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
615 |
public void symbolAdded(Symbol sym, Scope s) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
616 |
mark++; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
617 |
for (ScopeListener sl : listeners) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
618 |
sl.symbolAdded(sym, s); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
619 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
620 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
621 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
622 |
public void symbolRemoved(Symbol sym, Scope s) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
623 |
mark++; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
624 |
for (ScopeListener sl : listeners) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
625 |
sl.symbolRemoved(sym, s); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
626 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
627 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
628 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
629 |
public int getMark() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
630 |
return mark; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
631 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
632 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
633 |
@Override |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
634 |
public String toString() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
635 |
StringBuilder buf = new StringBuilder(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
636 |
buf.append("CompoundScope{"); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
637 |
String sep = ""; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
638 |
for (Scope s : subScopes) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
639 |
buf.append(sep); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
640 |
buf.append(s); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
641 |
sep = ","; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
642 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
643 |
buf.append("}"); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
644 |
return buf.toString(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
645 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
646 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
647 |
@Override |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
648 |
public Iterable<Symbol> getElements(final Filter<Symbol> sf) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
649 |
return new Iterable<Symbol>() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
650 |
public Iterator<Symbol> iterator() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
651 |
return new CompoundScopeIterator(subScopes) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
652 |
Iterator<Symbol> nextIterator(Scope s) { |
9593
f917c044e651
7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents:
8427
diff
changeset
|
653 |
return s.getElements(sf).iterator(); |
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
654 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
655 |
}; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
656 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
657 |
}; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
658 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
659 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
660 |
@Override |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
661 |
public Iterable<Symbol> getElementsByName(final Name name, final Filter<Symbol> sf) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
662 |
return new Iterable<Symbol>() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
663 |
public Iterator<Symbol> iterator() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
664 |
return new CompoundScopeIterator(subScopes) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
665 |
Iterator<Symbol> nextIterator(Scope s) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
666 |
return s.getElementsByName(name, sf).iterator(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
667 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
668 |
}; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
669 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
670 |
}; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
671 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
672 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
673 |
abstract class CompoundScopeIterator implements Iterator<Symbol> { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
674 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
675 |
private Iterator<Symbol> currentIterator; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
676 |
private List<Scope> scopesToScan; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
677 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
678 |
public CompoundScopeIterator(List<Scope> scopesToScan) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
679 |
this.scopesToScan = scopesToScan; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
680 |
update(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
681 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
682 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
683 |
abstract Iterator<Symbol> nextIterator(Scope s); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
684 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
685 |
public boolean hasNext() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
686 |
return currentIterator != null; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
687 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
688 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
689 |
public Symbol next() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
690 |
Symbol sym = currentIterator.next(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
691 |
if (!currentIterator.hasNext()) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
692 |
update(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
693 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
694 |
return sym; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
695 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
696 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
697 |
public void remove() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
698 |
throw new UnsupportedOperationException(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
699 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
700 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
701 |
private void update() { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
702 |
while (scopesToScan.nonEmpty()) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
703 |
currentIterator = nextIterator(scopesToScan.head); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
704 |
scopesToScan = scopesToScan.tail; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
705 |
if (currentIterator.hasNext()) return; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
706 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
707 |
currentIterator = null; |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
708 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
709 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
710 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
711 |
@Override |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
712 |
public Entry lookup(Name name, Filter<Symbol> sf) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
713 |
throw new UnsupportedOperationException(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
714 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
715 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
716 |
@Override |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
717 |
public Scope dup(Symbol newOwner) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
718 |
throw new UnsupportedOperationException(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
719 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
720 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
721 |
@Override |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
722 |
public void enter(Symbol sym, Scope s, Scope origin) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
723 |
throw new UnsupportedOperationException(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
724 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
725 |
|
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
726 |
@Override |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
727 |
public void remove(Symbol sym) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
728 |
throw new UnsupportedOperationException(); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
729 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
730 |
} |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
731 |
|
10 | 732 |
/** An error scope, for which the owner should be an error symbol. */ |
733 |
public static class ErrorScope extends Scope { |
|
734 |
ErrorScope(Scope next, Symbol errSymbol, Entry[] table) { |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8032
diff
changeset
|
735 |
super(next, /*owner=*/errSymbol, table); |
10 | 736 |
} |
737 |
public ErrorScope(Symbol errSymbol) { |
|
738 |
super(errSymbol); |
|
739 |
} |
|
740 |
public Scope dup() { |
|
741 |
return new ErrorScope(this, owner, table); |
|
742 |
} |
|
743 |
public Scope dupUnshared() { |
|
744 |
return new ErrorScope(this, owner, table.clone()); |
|
745 |
} |
|
746 |
public Entry lookup(Name name) { |
|
747 |
Entry e = super.lookup(name); |
|
748 |
if (e.scope == null) |
|
749 |
return new Entry(owner, null, null, null); |
|
750 |
else |
|
751 |
return e; |
|
752 |
} |
|
753 |
} |
|
754 |
} |