author | mcimadamore |
Tue, 07 Sep 2010 17:32:27 +0100 | |
changeset 6592 | dc56420a69bc |
parent 6352 | 217d5a69681a |
child 6709 | ade773eb432d |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 2003, 2008, 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.comp; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
import java.util.Set; |
|
30 |
import javax.tools.JavaFileObject; |
|
31 |
||
32 |
import com.sun.tools.javac.code.*; |
|
33 |
import com.sun.tools.javac.jvm.*; |
|
34 |
import com.sun.tools.javac.tree.*; |
|
35 |
import com.sun.tools.javac.util.*; |
|
36 |
import com.sun.tools.javac.util.List; |
|
37 |
||
38 |
import com.sun.tools.javac.code.Type.*; |
|
39 |
import com.sun.tools.javac.code.Symbol.*; |
|
40 |
import com.sun.tools.javac.tree.JCTree.*; |
|
41 |
||
42 |
import static com.sun.tools.javac.code.Flags.*; |
|
43 |
import static com.sun.tools.javac.code.Kinds.*; |
|
44 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
45 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
46 |
||
47 |
/** This is the second phase of Enter, in which classes are completed |
|
48 |
* by entering their members into the class scope using |
|
49 |
* MemberEnter.complete(). See Enter for an overview. |
|
50 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
51 |
* <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
|
52 |
* If you write code that depends on this, you do so at your own risk. |
10 | 53 |
* This code and its internal interfaces are subject to change or |
54 |
* deletion without notice.</b> |
|
55 |
*/ |
|
56 |
public class MemberEnter extends JCTree.Visitor implements Completer { |
|
57 |
protected static final Context.Key<MemberEnter> memberEnterKey = |
|
58 |
new Context.Key<MemberEnter>(); |
|
59 |
||
60 |
/** A switch to determine whether we check for package/class conflicts |
|
61 |
*/ |
|
62 |
final static boolean checkClash = true; |
|
63 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1045
diff
changeset
|
64 |
private final Names names; |
10 | 65 |
private final Enter enter; |
66 |
private final Log log; |
|
67 |
private final Check chk; |
|
68 |
private final Attr attr; |
|
69 |
private final Symtab syms; |
|
70 |
private final TreeMaker make; |
|
71 |
private final ClassReader reader; |
|
72 |
private final Todo todo; |
|
73 |
private final Annotate annotate; |
|
74 |
private final Types types; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
75 |
private final JCDiagnostic.Factory diags; |
10 | 76 |
private final Target target; |
77 |
||
78 |
private final boolean skipAnnotations; |
|
79 |
||
80 |
public static MemberEnter instance(Context context) { |
|
81 |
MemberEnter instance = context.get(memberEnterKey); |
|
82 |
if (instance == null) |
|
83 |
instance = new MemberEnter(context); |
|
84 |
return instance; |
|
85 |
} |
|
86 |
||
87 |
protected MemberEnter(Context context) { |
|
88 |
context.put(memberEnterKey, this); |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1045
diff
changeset
|
89 |
names = Names.instance(context); |
10 | 90 |
enter = Enter.instance(context); |
91 |
log = Log.instance(context); |
|
92 |
chk = Check.instance(context); |
|
93 |
attr = Attr.instance(context); |
|
94 |
syms = Symtab.instance(context); |
|
95 |
make = TreeMaker.instance(context); |
|
96 |
reader = ClassReader.instance(context); |
|
97 |
todo = Todo.instance(context); |
|
98 |
annotate = Annotate.instance(context); |
|
99 |
types = Types.instance(context); |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
100 |
diags = JCDiagnostic.Factory.instance(context); |
10 | 101 |
target = Target.instance(context); |
3149 | 102 |
Options options = Options.instance(context); |
103 |
skipAnnotations = options.get("skipAnnotations") != null; |
|
10 | 104 |
} |
105 |
||
106 |
/** A queue for classes whose members still need to be entered into the |
|
107 |
* symbol table. |
|
108 |
*/ |
|
109 |
ListBuffer<Env<AttrContext>> halfcompleted = new ListBuffer<Env<AttrContext>>(); |
|
110 |
||
111 |
/** Set to true only when the first of a set of classes is |
|
112 |
* processed from the halfcompleted queue. |
|
113 |
*/ |
|
114 |
boolean isFirst = true; |
|
115 |
||
116 |
/** A flag to disable completion from time to time during member |
|
117 |
* enter, as we only need to look up types. This avoids |
|
118 |
* unnecessarily deep recursion. |
|
119 |
*/ |
|
120 |
boolean completionEnabled = true; |
|
121 |
||
122 |
/* ---------- Processing import clauses ---------------- |
|
123 |
*/ |
|
124 |
||
125 |
/** Import all classes of a class or package on demand. |
|
126 |
* @param pos Position to be used for error reporting. |
|
127 |
* @param tsym The class or package the members of which are imported. |
|
128 |
* @param toScope The (import) scope in which imported classes |
|
129 |
* are entered. |
|
130 |
*/ |
|
131 |
private void importAll(int pos, |
|
132 |
final TypeSymbol tsym, |
|
133 |
Env<AttrContext> env) { |
|
134 |
// Check that packages imported from exist (JLS ???). |
|
135 |
if (tsym.kind == PCK && tsym.members().elems == null && !tsym.exists()) { |
|
136 |
// If we can't find java.lang, exit immediately. |
|
137 |
if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) { |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
138 |
JCDiagnostic msg = diags.fragment("fatal.err.no.java.lang"); |
10 | 139 |
throw new FatalError(msg); |
140 |
} else { |
|
141 |
log.error(pos, "doesnt.exist", tsym); |
|
142 |
} |
|
143 |
} |
|
144 |
final Scope fromScope = tsym.members(); |
|
145 |
final Scope toScope = env.toplevel.starImportScope; |
|
146 |
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { |
|
147 |
if (e.sym.kind == TYP && !toScope.includes(e.sym)) |
|
148 |
toScope.enter(e.sym, fromScope); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
/** Import all static members of a class or package on demand. |
|
153 |
* @param pos Position to be used for error reporting. |
|
154 |
* @param tsym The class or package the members of which are imported. |
|
155 |
* @param toScope The (import) scope in which imported classes |
|
156 |
* are entered. |
|
157 |
*/ |
|
158 |
private void importStaticAll(int pos, |
|
159 |
final TypeSymbol tsym, |
|
160 |
Env<AttrContext> env) { |
|
161 |
final JavaFileObject sourcefile = env.toplevel.sourcefile; |
|
162 |
final Scope toScope = env.toplevel.starImportScope; |
|
163 |
final PackageSymbol packge = env.toplevel.packge; |
|
164 |
final TypeSymbol origin = tsym; |
|
165 |
||
166 |
// enter imported types immediately |
|
167 |
new Object() { |
|
168 |
Set<Symbol> processed = new HashSet<Symbol>(); |
|
169 |
void importFrom(TypeSymbol tsym) { |
|
170 |
if (tsym == null || !processed.add(tsym)) |
|
171 |
return; |
|
172 |
||
173 |
// also import inherited names |
|
174 |
importFrom(types.supertype(tsym.type).tsym); |
|
175 |
for (Type t : types.interfaces(tsym.type)) |
|
176 |
importFrom(t.tsym); |
|
177 |
||
178 |
final Scope fromScope = tsym.members(); |
|
179 |
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { |
|
180 |
Symbol sym = e.sym; |
|
181 |
if (sym.kind == TYP && |
|
182 |
(sym.flags() & STATIC) != 0 && |
|
183 |
staticImportAccessible(sym, packge) && |
|
184 |
sym.isMemberOf(origin, types) && |
|
185 |
!toScope.includes(sym)) |
|
186 |
toScope.enter(sym, fromScope, origin.members()); |
|
187 |
} |
|
188 |
} |
|
189 |
}.importFrom(tsym); |
|
190 |
||
191 |
// enter non-types before annotations that might use them |
|
192 |
annotate.earlier(new Annotate.Annotator() { |
|
193 |
Set<Symbol> processed = new HashSet<Symbol>(); |
|
194 |
||
195 |
public String toString() { |
|
196 |
return "import static " + tsym + ".*" + " in " + sourcefile; |
|
197 |
} |
|
198 |
void importFrom(TypeSymbol tsym) { |
|
199 |
if (tsym == null || !processed.add(tsym)) |
|
200 |
return; |
|
201 |
||
202 |
// also import inherited names |
|
203 |
importFrom(types.supertype(tsym.type).tsym); |
|
204 |
for (Type t : types.interfaces(tsym.type)) |
|
205 |
importFrom(t.tsym); |
|
206 |
||
207 |
final Scope fromScope = tsym.members(); |
|
208 |
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { |
|
209 |
Symbol sym = e.sym; |
|
210 |
if (sym.isStatic() && sym.kind != TYP && |
|
211 |
staticImportAccessible(sym, packge) && |
|
212 |
!toScope.includes(sym) && |
|
213 |
sym.isMemberOf(origin, types)) { |
|
214 |
toScope.enter(sym, fromScope, origin.members()); |
|
215 |
} |
|
216 |
} |
|
217 |
} |
|
218 |
public void enterAnnotation() { |
|
219 |
importFrom(tsym); |
|
220 |
} |
|
221 |
}); |
|
222 |
} |
|
223 |
||
224 |
// is the sym accessible everywhere in packge? |
|
225 |
boolean staticImportAccessible(Symbol sym, PackageSymbol packge) { |
|
226 |
int flags = (int)(sym.flags() & AccessFlags); |
|
227 |
switch (flags) { |
|
228 |
default: |
|
229 |
case PUBLIC: |
|
230 |
return true; |
|
231 |
case PRIVATE: |
|
232 |
return false; |
|
233 |
case 0: |
|
234 |
case PROTECTED: |
|
235 |
return sym.packge() == packge; |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
/** Import statics types of a given name. Non-types are handled in Attr. |
|
240 |
* @param pos Position to be used for error reporting. |
|
241 |
* @param tsym The class from which the name is imported. |
|
242 |
* @param name The (simple) name being imported. |
|
243 |
* @param env The environment containing the named import |
|
244 |
* scope to add to. |
|
245 |
*/ |
|
246 |
private void importNamedStatic(final DiagnosticPosition pos, |
|
247 |
final TypeSymbol tsym, |
|
248 |
final Name name, |
|
249 |
final Env<AttrContext> env) { |
|
250 |
if (tsym.kind != TYP) { |
|
251 |
log.error(pos, "static.imp.only.classes.and.interfaces"); |
|
252 |
return; |
|
253 |
} |
|
254 |
||
255 |
final Scope toScope = env.toplevel.namedImportScope; |
|
256 |
final PackageSymbol packge = env.toplevel.packge; |
|
257 |
final TypeSymbol origin = tsym; |
|
258 |
||
259 |
// enter imported types immediately |
|
260 |
new Object() { |
|
261 |
Set<Symbol> processed = new HashSet<Symbol>(); |
|
262 |
void importFrom(TypeSymbol tsym) { |
|
263 |
if (tsym == null || !processed.add(tsym)) |
|
264 |
return; |
|
265 |
||
266 |
// also import inherited names |
|
267 |
importFrom(types.supertype(tsym.type).tsym); |
|
268 |
for (Type t : types.interfaces(tsym.type)) |
|
269 |
importFrom(t.tsym); |
|
270 |
||
271 |
for (Scope.Entry e = tsym.members().lookup(name); |
|
272 |
e.scope != null; |
|
273 |
e = e.next()) { |
|
274 |
Symbol sym = e.sym; |
|
275 |
if (sym.isStatic() && |
|
276 |
sym.kind == TYP && |
|
277 |
staticImportAccessible(sym, packge) && |
|
278 |
sym.isMemberOf(origin, types) && |
|
279 |
chk.checkUniqueStaticImport(pos, sym, toScope)) |
|
280 |
toScope.enter(sym, sym.owner.members(), origin.members()); |
|
281 |
} |
|
282 |
} |
|
283 |
}.importFrom(tsym); |
|
284 |
||
285 |
// enter non-types before annotations that might use them |
|
286 |
annotate.earlier(new Annotate.Annotator() { |
|
287 |
Set<Symbol> processed = new HashSet<Symbol>(); |
|
288 |
boolean found = false; |
|
289 |
||
290 |
public String toString() { |
|
291 |
return "import static " + tsym + "." + name; |
|
292 |
} |
|
293 |
void importFrom(TypeSymbol tsym) { |
|
294 |
if (tsym == null || !processed.add(tsym)) |
|
295 |
return; |
|
296 |
||
297 |
// also import inherited names |
|
298 |
importFrom(types.supertype(tsym.type).tsym); |
|
299 |
for (Type t : types.interfaces(tsym.type)) |
|
300 |
importFrom(t.tsym); |
|
301 |
||
302 |
for (Scope.Entry e = tsym.members().lookup(name); |
|
303 |
e.scope != null; |
|
304 |
e = e.next()) { |
|
305 |
Symbol sym = e.sym; |
|
306 |
if (sym.isStatic() && |
|
307 |
staticImportAccessible(sym, packge) && |
|
308 |
sym.isMemberOf(origin, types)) { |
|
309 |
found = true; |
|
310 |
if (sym.kind == MTH || |
|
311 |
sym.kind != TYP && chk.checkUniqueStaticImport(pos, sym, toScope)) |
|
312 |
toScope.enter(sym, sym.owner.members(), origin.members()); |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
public void enterAnnotation() { |
|
317 |
JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
318 |
try { |
|
319 |
importFrom(tsym); |
|
320 |
if (!found) { |
|
321 |
log.error(pos, "cant.resolve.location", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
322 |
KindName.STATIC, |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
323 |
name, List.<Type>nil(), List.<Type>nil(), |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
324 |
Kinds.typeKindName(tsym.type), |
10 | 325 |
tsym.type); |
326 |
} |
|
327 |
} finally { |
|
328 |
log.useSource(prev); |
|
329 |
} |
|
330 |
} |
|
331 |
}); |
|
332 |
} |
|
333 |
/** Import given class. |
|
334 |
* @param pos Position to be used for error reporting. |
|
335 |
* @param tsym The class to be imported. |
|
336 |
* @param env The environment containing the named import |
|
337 |
* scope to add to. |
|
338 |
*/ |
|
339 |
private void importNamed(DiagnosticPosition pos, Symbol tsym, Env<AttrContext> env) { |
|
340 |
if (tsym.kind == TYP && |
|
341 |
chk.checkUniqueImport(pos, tsym, env.toplevel.namedImportScope)) |
|
342 |
env.toplevel.namedImportScope.enter(tsym, tsym.owner.members()); |
|
343 |
} |
|
344 |
||
345 |
/** Construct method type from method signature. |
|
346 |
* @param typarams The method's type parameters. |
|
347 |
* @param params The method's value parameters. |
|
348 |
* @param res The method's result type, |
|
349 |
* null if it is a constructor. |
|
350 |
* @param thrown The method's thrown exceptions. |
|
351 |
* @param env The method's (local) environment. |
|
352 |
*/ |
|
353 |
Type signature(List<JCTypeParameter> typarams, |
|
354 |
List<JCVariableDecl> params, |
|
355 |
JCTree res, |
|
356 |
List<JCExpression> thrown, |
|
357 |
Env<AttrContext> env) { |
|
358 |
||
359 |
// Enter and attribute type parameters. |
|
360 |
List<Type> tvars = enter.classEnter(typarams, env); |
|
361 |
attr.attribTypeVariables(typarams, env); |
|
362 |
||
363 |
// Enter and attribute value parameters. |
|
364 |
ListBuffer<Type> argbuf = new ListBuffer<Type>(); |
|
365 |
for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) { |
|
366 |
memberEnter(l.head, env); |
|
367 |
argbuf.append(l.head.vartype.type); |
|
368 |
} |
|
369 |
||
370 |
// Attribute result type, if one is given. |
|
371 |
Type restype = res == null ? syms.voidType : attr.attribType(res, env); |
|
372 |
||
373 |
// Attribute thrown exceptions. |
|
374 |
ListBuffer<Type> thrownbuf = new ListBuffer<Type>(); |
|
375 |
for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) { |
|
376 |
Type exc = attr.attribType(l.head, env); |
|
377 |
if (exc.tag != TYPEVAR) |
|
378 |
exc = chk.checkClassType(l.head.pos(), exc); |
|
379 |
thrownbuf.append(exc); |
|
380 |
} |
|
381 |
Type mtype = new MethodType(argbuf.toList(), |
|
382 |
restype, |
|
383 |
thrownbuf.toList(), |
|
384 |
syms.methodClass); |
|
385 |
return tvars.isEmpty() ? mtype : new ForAll(tvars, mtype); |
|
386 |
} |
|
387 |
||
388 |
/* ******************************************************************** |
|
389 |
* Visitor methods for member enter |
|
390 |
*********************************************************************/ |
|
391 |
||
392 |
/** Visitor argument: the current environment |
|
393 |
*/ |
|
394 |
protected Env<AttrContext> env; |
|
395 |
||
396 |
/** Enter field and method definitions and process import |
|
397 |
* clauses, catching any completion failure exceptions. |
|
398 |
*/ |
|
399 |
protected void memberEnter(JCTree tree, Env<AttrContext> env) { |
|
400 |
Env<AttrContext> prevEnv = this.env; |
|
401 |
try { |
|
402 |
this.env = env; |
|
403 |
tree.accept(this); |
|
404 |
} catch (CompletionFailure ex) { |
|
405 |
chk.completionError(tree.pos(), ex); |
|
406 |
} finally { |
|
407 |
this.env = prevEnv; |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
/** Enter members from a list of trees. |
|
412 |
*/ |
|
413 |
void memberEnter(List<? extends JCTree> trees, Env<AttrContext> env) { |
|
414 |
for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
|
415 |
memberEnter(l.head, env); |
|
416 |
} |
|
417 |
||
418 |
/** Enter members for a class. |
|
419 |
*/ |
|
420 |
void finishClass(JCClassDecl tree, Env<AttrContext> env) { |
|
421 |
if ((tree.mods.flags & Flags.ENUM) != 0 && |
|
422 |
(types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) { |
|
423 |
addEnumMembers(tree, env); |
|
424 |
} |
|
425 |
memberEnter(tree.defs, env); |
|
426 |
} |
|
427 |
||
428 |
/** Add the implicit members for an enum type |
|
429 |
* to the symbol table. |
|
430 |
*/ |
|
431 |
private void addEnumMembers(JCClassDecl tree, Env<AttrContext> env) { |
|
432 |
JCExpression valuesType = make.Type(new ArrayType(tree.sym.type, syms.arrayClass)); |
|
433 |
||
434 |
// public static T[] values() { return ???; } |
|
435 |
JCMethodDecl values = make. |
|
436 |
MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC), |
|
437 |
names.values, |
|
438 |
valuesType, |
|
439 |
List.<JCTypeParameter>nil(), |
|
440 |
List.<JCVariableDecl>nil(), |
|
441 |
List.<JCExpression>nil(), // thrown |
|
442 |
null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))), |
|
443 |
null); |
|
444 |
memberEnter(values, env); |
|
445 |
||
446 |
// public static T valueOf(String name) { return ???; } |
|
447 |
JCMethodDecl valueOf = make. |
|
448 |
MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC), |
|
449 |
names.valueOf, |
|
450 |
make.Type(tree.sym.type), |
|
451 |
List.<JCTypeParameter>nil(), |
|
452 |
List.of(make.VarDef(make.Modifiers(Flags.PARAMETER), |
|
453 |
names.fromString("name"), |
|
454 |
make.Type(syms.stringType), null)), |
|
455 |
List.<JCExpression>nil(), // thrown |
|
456 |
null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))), |
|
457 |
null); |
|
458 |
memberEnter(valueOf, env); |
|
459 |
||
460 |
// the remaining members are for bootstrapping only |
|
461 |
if (!target.compilerBootstrap(tree.sym)) return; |
|
462 |
||
463 |
// public final int ordinal() { return ???; } |
|
464 |
JCMethodDecl ordinal = make.at(tree.pos). |
|
465 |
MethodDef(make.Modifiers(Flags.PUBLIC|Flags.FINAL), |
|
466 |
names.ordinal, |
|
467 |
make.Type(syms.intType), |
|
468 |
List.<JCTypeParameter>nil(), |
|
469 |
List.<JCVariableDecl>nil(), |
|
470 |
List.<JCExpression>nil(), |
|
471 |
null, |
|
472 |
null); |
|
473 |
memberEnter(ordinal, env); |
|
474 |
||
475 |
// public final String name() { return ???; } |
|
476 |
JCMethodDecl name = make. |
|
477 |
MethodDef(make.Modifiers(Flags.PUBLIC|Flags.FINAL), |
|
478 |
names._name, |
|
479 |
make.Type(syms.stringType), |
|
480 |
List.<JCTypeParameter>nil(), |
|
481 |
List.<JCVariableDecl>nil(), |
|
482 |
List.<JCExpression>nil(), |
|
483 |
null, |
|
484 |
null); |
|
485 |
memberEnter(name, env); |
|
486 |
||
487 |
// public int compareTo(E other) { return ???; } |
|
488 |
MethodSymbol compareTo = new |
|
489 |
MethodSymbol(Flags.PUBLIC, |
|
490 |
names.compareTo, |
|
491 |
new MethodType(List.of(tree.sym.type), |
|
492 |
syms.intType, |
|
493 |
List.<Type>nil(), |
|
494 |
syms.methodClass), |
|
495 |
tree.sym); |
|
496 |
memberEnter(make.MethodDef(compareTo, null), env); |
|
497 |
} |
|
498 |
||
499 |
public void visitTopLevel(JCCompilationUnit tree) { |
|
500 |
if (tree.starImportScope.elems != null) { |
|
501 |
// we must have already processed this toplevel |
|
502 |
return; |
|
503 |
} |
|
504 |
||
505 |
// check that no class exists with same fully qualified name as |
|
506 |
// toplevel package |
|
507 |
if (checkClash && tree.pid != null) { |
|
508 |
Symbol p = tree.packge; |
|
509 |
while (p.owner != syms.rootPackage) { |
|
510 |
p.owner.complete(); // enter all class members of p |
|
511 |
if (syms.classes.get(p.getQualifiedName()) != null) { |
|
512 |
log.error(tree.pos, |
|
513 |
"pkg.clashes.with.class.of.same.name", |
|
514 |
p); |
|
515 |
} |
|
516 |
p = p.owner; |
|
517 |
} |
|
518 |
} |
|
519 |
||
520 |
// process package annotations |
|
521 |
annotateLater(tree.packageAnnotations, env, tree.packge); |
|
522 |
||
523 |
// Import-on-demand java.lang. |
|
524 |
importAll(tree.pos, reader.enterPackage(names.java_lang), env); |
|
525 |
||
526 |
// Process all import clauses. |
|
527 |
memberEnter(tree.defs, env); |
|
528 |
} |
|
529 |
||
530 |
// process the non-static imports and the static imports of types. |
|
531 |
public void visitImport(JCImport tree) { |
|
532 |
JCTree imp = tree.qualid; |
|
533 |
Name name = TreeInfo.name(imp); |
|
534 |
TypeSymbol p; |
|
535 |
||
536 |
// Create a local environment pointing to this tree to disable |
|
537 |
// effects of other imports in Resolve.findGlobalType |
|
538 |
Env<AttrContext> localEnv = env.dup(tree); |
|
539 |
||
540 |
// Attribute qualifying package or class. |
|
541 |
JCFieldAccess s = (JCFieldAccess) imp; |
|
542 |
p = attr. |
|
543 |
attribTree(s.selected, |
|
544 |
localEnv, |
|
545 |
tree.staticImport ? TYP : (TYP | PCK), |
|
546 |
Type.noType).tsym; |
|
547 |
if (name == names.asterisk) { |
|
548 |
// Import on demand. |
|
549 |
chk.checkCanonical(s.selected); |
|
550 |
if (tree.staticImport) |
|
551 |
importStaticAll(tree.pos, p, env); |
|
552 |
else |
|
553 |
importAll(tree.pos, p, env); |
|
554 |
} else { |
|
555 |
// Named type import. |
|
556 |
if (tree.staticImport) { |
|
557 |
importNamedStatic(tree.pos(), p, name, localEnv); |
|
558 |
chk.checkCanonical(s.selected); |
|
559 |
} else { |
|
560 |
TypeSymbol c = attribImportType(imp, localEnv).tsym; |
|
561 |
chk.checkCanonical(imp); |
|
562 |
importNamed(tree.pos(), c, env); |
|
563 |
} |
|
564 |
} |
|
565 |
} |
|
566 |
||
567 |
public void visitMethodDef(JCMethodDecl tree) { |
|
568 |
Scope enclScope = enter.enterScope(env); |
|
569 |
MethodSymbol m = new MethodSymbol(0, tree.name, null, enclScope.owner); |
|
570 |
m.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, m, tree); |
|
571 |
tree.sym = m; |
|
572 |
Env<AttrContext> localEnv = methodEnv(tree, env); |
|
573 |
||
574 |
// Compute the method type |
|
575 |
m.type = signature(tree.typarams, tree.params, |
|
576 |
tree.restype, tree.thrown, |
|
577 |
localEnv); |
|
578 |
||
579 |
// Set m.params |
|
580 |
ListBuffer<VarSymbol> params = new ListBuffer<VarSymbol>(); |
|
581 |
JCVariableDecl lastParam = null; |
|
582 |
for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
|
583 |
JCVariableDecl param = lastParam = l.head; |
|
584 |
assert param.sym != null; |
|
585 |
params.append(param.sym); |
|
586 |
} |
|
587 |
m.params = params.toList(); |
|
588 |
||
589 |
// mark the method varargs, if necessary |
|
590 |
if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0) |
|
591 |
m.flags_field |= Flags.VARARGS; |
|
592 |
||
593 |
localEnv.info.scope.leave(); |
|
594 |
if (chk.checkUnique(tree.pos(), m, enclScope)) { |
|
595 |
enclScope.enter(m); |
|
596 |
} |
|
597 |
annotateLater(tree.mods.annotations, localEnv, m); |
|
598 |
if (tree.defaultValue != null) |
|
599 |
annotateDefaultValueLater(tree.defaultValue, localEnv, m); |
|
600 |
} |
|
601 |
||
602 |
/** Create a fresh environment for method bodies. |
|
603 |
* @param tree The method definition. |
|
604 |
* @param env The environment current outside of the method definition. |
|
605 |
*/ |
|
606 |
Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) { |
|
607 |
Env<AttrContext> localEnv = |
|
608 |
env.dup(tree, env.info.dup(env.info.scope.dupUnshared())); |
|
609 |
localEnv.enclMethod = tree; |
|
610 |
localEnv.info.scope.owner = tree.sym; |
|
611 |
if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++; |
|
612 |
return localEnv; |
|
613 |
} |
|
614 |
||
615 |
public void visitVarDef(JCVariableDecl tree) { |
|
616 |
Env<AttrContext> localEnv = env; |
|
617 |
if ((tree.mods.flags & STATIC) != 0 || |
|
618 |
(env.info.scope.owner.flags() & INTERFACE) != 0) { |
|
619 |
localEnv = env.dup(tree, env.info.dup()); |
|
620 |
localEnv.info.staticLevel++; |
|
621 |
} |
|
622 |
attr.attribType(tree.vartype, localEnv); |
|
623 |
Scope enclScope = enter.enterScope(env); |
|
624 |
VarSymbol v = |
|
625 |
new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner); |
|
626 |
v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree); |
|
627 |
tree.sym = v; |
|
628 |
if (tree.init != null) { |
|
629 |
v.flags_field |= HASINIT; |
|
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1043
diff
changeset
|
630 |
if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) { |
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1043
diff
changeset
|
631 |
Env<AttrContext> initEnv = getInitEnv(tree, env); |
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1043
diff
changeset
|
632 |
initEnv.info.enclVar = v; |
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1043
diff
changeset
|
633 |
v.setLazyConstValue(initEnv(tree, initEnv), log, attr, tree.init); |
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1043
diff
changeset
|
634 |
} |
10 | 635 |
} |
636 |
if (chk.checkUnique(tree.pos(), v, enclScope)) { |
|
637 |
chk.checkTransparentVar(tree.pos(), v, enclScope); |
|
638 |
enclScope.enter(v); |
|
639 |
} |
|
640 |
annotateLater(tree.mods.annotations, localEnv, v); |
|
641 |
v.pos = tree.pos; |
|
642 |
} |
|
643 |
||
644 |
/** Create a fresh environment for a variable's initializer. |
|
645 |
* If the variable is a field, the owner of the environment's scope |
|
646 |
* is be the variable itself, otherwise the owner is the method |
|
647 |
* enclosing the variable definition. |
|
648 |
* |
|
649 |
* @param tree The variable definition. |
|
650 |
* @param env The environment current outside of the variable definition. |
|
651 |
*/ |
|
652 |
Env<AttrContext> initEnv(JCVariableDecl tree, Env<AttrContext> env) { |
|
653 |
Env<AttrContext> localEnv = env.dupto(new AttrContextEnv(tree, env.info.dup())); |
|
654 |
if (tree.sym.owner.kind == TYP) { |
|
655 |
localEnv.info.scope = new Scope.DelegatedScope(env.info.scope); |
|
656 |
localEnv.info.scope.owner = tree.sym; |
|
657 |
} |
|
658 |
if ((tree.mods.flags & STATIC) != 0 || |
|
659 |
(env.enclClass.sym.flags() & INTERFACE) != 0) |
|
660 |
localEnv.info.staticLevel++; |
|
661 |
return localEnv; |
|
662 |
} |
|
663 |
||
664 |
/** Default member enter visitor method: do nothing |
|
665 |
*/ |
|
666 |
public void visitTree(JCTree tree) { |
|
667 |
} |
|
668 |
||
669 |
||
670 |
public void visitErroneous(JCErroneous tree) { |
|
671 |
memberEnter(tree.errs, env); |
|
672 |
} |
|
673 |
||
674 |
public Env<AttrContext> getMethodEnv(JCMethodDecl tree, Env<AttrContext> env) { |
|
675 |
Env<AttrContext> mEnv = methodEnv(tree, env); |
|
676 |
mEnv.info.lint = mEnv.info.lint.augment(tree.sym.attributes_field, tree.sym.flags()); |
|
677 |
for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) |
|
678 |
mEnv.info.scope.enterIfAbsent(l.head.type.tsym); |
|
679 |
for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) |
|
680 |
mEnv.info.scope.enterIfAbsent(l.head.sym); |
|
681 |
return mEnv; |
|
682 |
} |
|
683 |
||
684 |
public Env<AttrContext> getInitEnv(JCVariableDecl tree, Env<AttrContext> env) { |
|
685 |
Env<AttrContext> iEnv = initEnv(tree, env); |
|
686 |
return iEnv; |
|
687 |
} |
|
688 |
||
689 |
/* ******************************************************************** |
|
690 |
* Type completion |
|
691 |
*********************************************************************/ |
|
692 |
||
693 |
Type attribImportType(JCTree tree, Env<AttrContext> env) { |
|
694 |
assert completionEnabled; |
|
695 |
try { |
|
696 |
// To prevent deep recursion, suppress completion of some |
|
697 |
// types. |
|
698 |
completionEnabled = false; |
|
699 |
return attr.attribType(tree, env); |
|
700 |
} finally { |
|
701 |
completionEnabled = true; |
|
702 |
} |
|
703 |
} |
|
704 |
||
705 |
/* ******************************************************************** |
|
706 |
* Annotation processing |
|
707 |
*********************************************************************/ |
|
708 |
||
709 |
/** Queue annotations for later processing. */ |
|
710 |
void annotateLater(final List<JCAnnotation> annotations, |
|
711 |
final Env<AttrContext> localEnv, |
|
712 |
final Symbol s) { |
|
713 |
if (annotations.isEmpty()) return; |
|
714 |
if (s.kind != PCK) s.attributes_field = null; // mark it incomplete for now |
|
715 |
annotate.later(new Annotate.Annotator() { |
|
716 |
public String toString() { |
|
717 |
return "annotate " + annotations + " onto " + s + " in " + s.owner; |
|
718 |
} |
|
719 |
public void enterAnnotation() { |
|
720 |
assert s.kind == PCK || s.attributes_field == null; |
|
721 |
JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
|
722 |
try { |
|
723 |
if (s.attributes_field != null && |
|
724 |
s.attributes_field.nonEmpty() && |
|
725 |
annotations.nonEmpty()) |
|
726 |
log.error(annotations.head.pos, |
|
727 |
"already.annotated", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
728 |
kindName(s), s); |
10 | 729 |
enterAnnotations(annotations, localEnv, s); |
730 |
} finally { |
|
731 |
log.useSource(prev); |
|
732 |
} |
|
733 |
} |
|
734 |
}); |
|
735 |
} |
|
736 |
||
737 |
/** |
|
738 |
* Check if a list of annotations contains a reference to |
|
739 |
* java.lang.Deprecated. |
|
740 |
**/ |
|
741 |
private boolean hasDeprecatedAnnotation(List<JCAnnotation> annotations) { |
|
742 |
for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) { |
|
743 |
JCAnnotation a = al.head; |
|
744 |
if (a.annotationType.type == syms.deprecatedType && a.args.isEmpty()) |
|
745 |
return true; |
|
746 |
} |
|
747 |
return false; |
|
748 |
} |
|
749 |
||
750 |
||
751 |
/** Enter a set of annotations. */ |
|
752 |
private void enterAnnotations(List<JCAnnotation> annotations, |
|
753 |
Env<AttrContext> env, |
|
754 |
Symbol s) { |
|
755 |
ListBuffer<Attribute.Compound> buf = |
|
756 |
new ListBuffer<Attribute.Compound>(); |
|
757 |
Set<TypeSymbol> annotated = new HashSet<TypeSymbol>(); |
|
758 |
if (!skipAnnotations) |
|
759 |
for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) { |
|
760 |
JCAnnotation a = al.head; |
|
761 |
Attribute.Compound c = annotate.enterAnnotation(a, |
|
762 |
syms.annotationType, |
|
763 |
env); |
|
764 |
if (c == null) continue; |
|
765 |
buf.append(c); |
|
766 |
// Note: @Deprecated has no effect on local variables and parameters |
|
767 |
if (!c.type.isErroneous() |
|
768 |
&& s.owner.kind != MTH |
|
769 |
&& types.isSameType(c.type, syms.deprecatedType)) |
|
770 |
s.flags_field |= Flags.DEPRECATED; |
|
5736
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4877
diff
changeset
|
771 |
// Internally to java.dyn, a @PolymorphicSignature annotation |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
772 |
// acts like a classfile attribute. |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
773 |
if (!c.type.isErroneous() && |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
774 |
types.isSameType(c.type, syms.polymorphicSignatureType)) { |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
775 |
if (!target.hasMethodHandles()) { |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
776 |
// Somebody is compiling JDK7 source code to a JDK6 target. |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
777 |
// Make it a strict warning, since it is unlikely but important. |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
778 |
log.strictWarning(env.tree.pos(), |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
779 |
"wrong.target.for.polymorphic.signature.definition", |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
780 |
target.name); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
781 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6352
diff
changeset
|
782 |
// Pull the flag through for better diagnostics, even on a bad target. |
5736
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4877
diff
changeset
|
783 |
s.flags_field |= Flags.POLYMORPHIC_SIGNATURE; |
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4877
diff
changeset
|
784 |
} |
10 | 785 |
if (!annotated.add(a.type.tsym)) |
786 |
log.error(a.pos, "duplicate.annotation"); |
|
787 |
} |
|
788 |
s.attributes_field = buf.toList(); |
|
789 |
} |
|
790 |
||
791 |
/** Queue processing of an attribute default value. */ |
|
792 |
void annotateDefaultValueLater(final JCExpression defaultValue, |
|
793 |
final Env<AttrContext> localEnv, |
|
794 |
final MethodSymbol m) { |
|
795 |
annotate.later(new Annotate.Annotator() { |
|
796 |
public String toString() { |
|
797 |
return "annotate " + m.owner + "." + |
|
798 |
m + " default " + defaultValue; |
|
799 |
} |
|
800 |
public void enterAnnotation() { |
|
801 |
JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
|
802 |
try { |
|
803 |
enterDefaultValue(defaultValue, localEnv, m); |
|
804 |
} finally { |
|
805 |
log.useSource(prev); |
|
806 |
} |
|
807 |
} |
|
808 |
}); |
|
809 |
} |
|
810 |
||
811 |
/** Enter a default value for an attribute method. */ |
|
812 |
private void enterDefaultValue(final JCExpression defaultValue, |
|
813 |
final Env<AttrContext> localEnv, |
|
814 |
final MethodSymbol m) { |
|
815 |
m.defaultValue = annotate.enterAttributeValue(m.type.getReturnType(), |
|
816 |
defaultValue, |
|
817 |
localEnv); |
|
818 |
} |
|
819 |
||
820 |
/* ******************************************************************** |
|
821 |
* Source completer |
|
822 |
*********************************************************************/ |
|
823 |
||
824 |
/** Complete entering a class. |
|
825 |
* @param sym The symbol of the class to be completed. |
|
826 |
*/ |
|
827 |
public void complete(Symbol sym) throws CompletionFailure { |
|
828 |
// Suppress some (recursive) MemberEnter invocations |
|
829 |
if (!completionEnabled) { |
|
830 |
// Re-install same completer for next time around and return. |
|
831 |
assert (sym.flags() & Flags.COMPOUND) == 0; |
|
832 |
sym.completer = this; |
|
833 |
return; |
|
834 |
} |
|
835 |
||
836 |
ClassSymbol c = (ClassSymbol)sym; |
|
837 |
ClassType ct = (ClassType)c.type; |
|
838 |
Env<AttrContext> env = enter.typeEnvs.get(c); |
|
839 |
JCClassDecl tree = (JCClassDecl)env.tree; |
|
840 |
boolean wasFirst = isFirst; |
|
841 |
isFirst = false; |
|
842 |
||
843 |
JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
844 |
try { |
|
845 |
// Save class environment for later member enter (2) processing. |
|
846 |
halfcompleted.append(env); |
|
847 |
||
1043
11ea4773b0af
6695838: javac does not detect cyclic inheritance involving static inner classes after import clause
mcimadamore
parents:
1040
diff
changeset
|
848 |
// Mark class as not yet attributed. |
11ea4773b0af
6695838: javac does not detect cyclic inheritance involving static inner classes after import clause
mcimadamore
parents:
1040
diff
changeset
|
849 |
c.flags_field |= UNATTRIBUTED; |
11ea4773b0af
6695838: javac does not detect cyclic inheritance involving static inner classes after import clause
mcimadamore
parents:
1040
diff
changeset
|
850 |
|
10 | 851 |
// If this is a toplevel-class, make sure any preceding import |
852 |
// clauses have been seen. |
|
853 |
if (c.owner.kind == PCK) { |
|
854 |
memberEnter(env.toplevel, env.enclosing(JCTree.TOPLEVEL)); |
|
855 |
todo.append(env); |
|
856 |
} |
|
857 |
||
858 |
if (c.owner.kind == TYP) |
|
859 |
c.owner.complete(); |
|
860 |
||
861 |
// create an environment for evaluating the base clauses |
|
862 |
Env<AttrContext> baseEnv = baseEnv(tree, env); |
|
863 |
||
864 |
// Determine supertype. |
|
865 |
Type supertype = |
|
866 |
(tree.extending != null) |
|
867 |
? attr.attribBase(tree.extending, baseEnv, true, false, true) |
|
868 |
: ((tree.mods.flags & Flags.ENUM) != 0 && !target.compilerBootstrap(c)) |
|
869 |
? attr.attribBase(enumBase(tree.pos, c), baseEnv, |
|
870 |
true, false, false) |
|
871 |
: (c.fullname == names.java_lang_Object) |
|
872 |
? Type.noType |
|
873 |
: syms.objectType; |
|
874 |
ct.supertype_field = supertype; |
|
875 |
||
876 |
// Determine interfaces. |
|
877 |
ListBuffer<Type> interfaces = new ListBuffer<Type>(); |
|
878 |
Set<Type> interfaceSet = new HashSet<Type>(); |
|
879 |
List<JCExpression> interfaceTrees = tree.implementing; |
|
880 |
if ((tree.mods.flags & Flags.ENUM) != 0 && target.compilerBootstrap(c)) { |
|
881 |
// add interface Comparable<T> |
|
882 |
interfaceTrees = |
|
883 |
interfaceTrees.prepend(make.Type(new ClassType(syms.comparableType.getEnclosingType(), |
|
884 |
List.of(c.type), |
|
885 |
syms.comparableType.tsym))); |
|
886 |
// add interface Serializable |
|
887 |
interfaceTrees = |
|
888 |
interfaceTrees.prepend(make.Type(syms.serializableType)); |
|
889 |
} |
|
890 |
for (JCExpression iface : interfaceTrees) { |
|
891 |
Type i = attr.attribBase(iface, baseEnv, false, true, true); |
|
892 |
if (i.tag == CLASS) { |
|
893 |
interfaces.append(i); |
|
894 |
chk.checkNotRepeated(iface.pos(), types.erasure(i), interfaceSet); |
|
895 |
} |
|
896 |
} |
|
897 |
if ((c.flags_field & ANNOTATION) != 0) |
|
898 |
ct.interfaces_field = List.of(syms.annotationType); |
|
899 |
else |
|
900 |
ct.interfaces_field = interfaces.toList(); |
|
901 |
||
902 |
if (c.fullname == names.java_lang_Object) { |
|
903 |
if (tree.extending != null) { |
|
904 |
chk.checkNonCyclic(tree.extending.pos(), |
|
905 |
supertype); |
|
906 |
ct.supertype_field = Type.noType; |
|
907 |
} |
|
908 |
else if (tree.implementing.nonEmpty()) { |
|
909 |
chk.checkNonCyclic(tree.implementing.head.pos(), |
|
910 |
ct.interfaces_field.head); |
|
911 |
ct.interfaces_field = List.nil(); |
|
912 |
} |
|
913 |
} |
|
914 |
||
915 |
// Annotations. |
|
916 |
// In general, we cannot fully process annotations yet, but we |
|
917 |
// can attribute the annotation types and then check to see if the |
|
918 |
// @Deprecated annotation is present. |
|
919 |
attr.attribAnnotationTypes(tree.mods.annotations, baseEnv); |
|
920 |
if (hasDeprecatedAnnotation(tree.mods.annotations)) |
|
921 |
c.flags_field |= DEPRECATED; |
|
922 |
annotateLater(tree.mods.annotations, baseEnv, c); |
|
3149 | 923 |
// class type parameters use baseEnv but everything uses env |
924 |
for (JCTypeParameter tp : tree.typarams) |
|
925 |
tp.accept(new TypeAnnotate(baseEnv)); |
|
926 |
tree.accept(new TypeAnnotate(env)); |
|
10 | 927 |
|
165
1875ac59896a
6663588: Compiler goes into infinite loop for Cyclic Inheritance test case
mcimadamore
parents:
10
diff
changeset
|
928 |
chk.checkNonCyclic(tree.pos(), c.type); |
10 | 929 |
|
165
1875ac59896a
6663588: Compiler goes into infinite loop for Cyclic Inheritance test case
mcimadamore
parents:
10
diff
changeset
|
930 |
attr.attribTypeVariables(tree.typarams, baseEnv); |
10 | 931 |
|
932 |
// Add default constructor if needed. |
|
933 |
if ((c.flags() & INTERFACE) == 0 && |
|
934 |
!TreeInfo.hasConstructors(tree.defs)) { |
|
935 |
List<Type> argtypes = List.nil(); |
|
936 |
List<Type> typarams = List.nil(); |
|
937 |
List<Type> thrown = List.nil(); |
|
938 |
long ctorFlags = 0; |
|
939 |
boolean based = false; |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1045
diff
changeset
|
940 |
if (c.name.isEmpty()) { |
10 | 941 |
JCNewClass nc = (JCNewClass)env.next.tree; |
942 |
if (nc.constructor != null) { |
|
943 |
Type superConstrType = types.memberType(c.type, |
|
944 |
nc.constructor); |
|
945 |
argtypes = superConstrType.getParameterTypes(); |
|
946 |
typarams = superConstrType.getTypeArguments(); |
|
947 |
ctorFlags = nc.constructor.flags() & VARARGS; |
|
948 |
if (nc.encl != null) { |
|
949 |
argtypes = argtypes.prepend(nc.encl.type); |
|
950 |
based = true; |
|
951 |
} |
|
952 |
thrown = superConstrType.getThrownTypes(); |
|
953 |
} |
|
954 |
} |
|
955 |
JCTree constrDef = DefaultConstructor(make.at(tree.pos), c, |
|
956 |
typarams, argtypes, thrown, |
|
957 |
ctorFlags, based); |
|
958 |
tree.defs = tree.defs.prepend(constrDef); |
|
959 |
} |
|
960 |
||
961 |
// If this is a class, enter symbols for this and super into |
|
962 |
// current scope. |
|
963 |
if ((c.flags_field & INTERFACE) == 0) { |
|
964 |
VarSymbol thisSym = |
|
965 |
new VarSymbol(FINAL | HASINIT, names._this, c.type, c); |
|
966 |
thisSym.pos = Position.FIRSTPOS; |
|
967 |
env.info.scope.enter(thisSym); |
|
968 |
if (ct.supertype_field.tag == CLASS) { |
|
969 |
VarSymbol superSym = |
|
970 |
new VarSymbol(FINAL | HASINIT, names._super, |
|
971 |
ct.supertype_field, c); |
|
972 |
superSym.pos = Position.FIRSTPOS; |
|
973 |
env.info.scope.enter(superSym); |
|
974 |
} |
|
975 |
} |
|
976 |
||
977 |
// check that no package exists with same fully qualified name, |
|
978 |
// but admit classes in the unnamed package which have the same |
|
979 |
// name as a top-level package. |
|
980 |
if (checkClash && |
|
981 |
c.owner.kind == PCK && c.owner != syms.unnamedPackage && |
|
982 |
reader.packageExists(c.fullname)) |
|
983 |
{ |
|
984 |
log.error(tree.pos, "clash.with.pkg.of.same.name", c); |
|
985 |
} |
|
986 |
||
987 |
} catch (CompletionFailure ex) { |
|
988 |
chk.completionError(tree.pos(), ex); |
|
989 |
} finally { |
|
990 |
log.useSource(prev); |
|
991 |
} |
|
992 |
||
993 |
// Enter all member fields and methods of a set of half completed |
|
994 |
// classes in a second phase. |
|
995 |
if (wasFirst) { |
|
996 |
try { |
|
997 |
while (halfcompleted.nonEmpty()) { |
|
998 |
finish(halfcompleted.next()); |
|
999 |
} |
|
1000 |
} finally { |
|
1001 |
isFirst = true; |
|
1002 |
} |
|
1003 |
||
1004 |
// commit pending annotations |
|
1005 |
annotate.flush(); |
|
1006 |
} |
|
1007 |
} |
|
1008 |
||
3149 | 1009 |
// A sub-phase that "compiles" annotations in annotated types. |
1010 |
private class TypeAnnotate extends TreeScanner { |
|
1011 |
private Env<AttrContext> env; |
|
1012 |
public TypeAnnotate(Env<AttrContext> env) { this.env = env; } |
|
1013 |
||
1014 |
private void enterTypeAnnotations(List<JCTypeAnnotation> annotations) { |
|
1015 |
Set<TypeSymbol> annotated = new HashSet<TypeSymbol>(); |
|
1016 |
if (!skipAnnotations) |
|
1017 |
for (List<JCTypeAnnotation> al = annotations; al.nonEmpty(); al = al.tail) { |
|
1018 |
JCTypeAnnotation a = al.head; |
|
1019 |
Attribute.Compound c = annotate.enterAnnotation(a, |
|
1020 |
syms.annotationType, |
|
1021 |
env); |
|
1022 |
if (c == null) continue; |
|
1023 |
Attribute.TypeCompound tc = new Attribute.TypeCompound(c.type, c.values, a.annotation_position); |
|
1024 |
a.attribute_field = tc; |
|
1025 |
// Note: @Deprecated has no effect on local variables and parameters |
|
1026 |
if (!annotated.add(a.type.tsym)) |
|
1027 |
log.error(a.pos, "duplicate.annotation"); |
|
1028 |
} |
|
1029 |
} |
|
1030 |
||
1031 |
// each class (including enclosed inner classes) should be visited |
|
1032 |
// separately through MemberEnter.complete(Symbol) |
|
1033 |
// this flag is used to prevent from visiting inner classes. |
|
1034 |
private boolean isEnclosingClass = false; |
|
1035 |
@Override |
|
1036 |
public void visitClassDef(final JCClassDecl tree) { |
|
1037 |
if (isEnclosingClass) |
|
1038 |
return; |
|
1039 |
isEnclosingClass = true; |
|
1040 |
scan(tree.mods); |
|
1041 |
// type parameter need to be visited with a separate env |
|
1042 |
// scan(tree.typarams); |
|
1043 |
scan(tree.extending); |
|
1044 |
scan(tree.implementing); |
|
1045 |
scan(tree.defs); |
|
1046 |
} |
|
1047 |
||
1048 |
private void annotate(final JCTree tree, final List<JCTypeAnnotation> annotations) { |
|
1049 |
annotate.later(new Annotate.Annotator() { |
|
1050 |
public String toString() { |
|
1051 |
return "annotate " + annotations + " onto " + tree; |
|
1052 |
} |
|
1053 |
public void enterAnnotation() { |
|
1054 |
JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
1055 |
try { |
|
1056 |
enterTypeAnnotations(annotations); |
|
1057 |
} finally { |
|
1058 |
log.useSource(prev); |
|
1059 |
} |
|
1060 |
} |
|
1061 |
}); |
|
1062 |
} |
|
1063 |
||
1064 |
@Override |
|
1065 |
public void visitAnnotatedType(final JCAnnotatedType tree) { |
|
1066 |
annotate(tree, tree.annotations); |
|
1067 |
super.visitAnnotatedType(tree); |
|
1068 |
} |
|
1069 |
@Override |
|
1070 |
public void visitTypeParameter(final JCTypeParameter tree) { |
|
1071 |
annotate(tree, tree.annotations); |
|
1072 |
super.visitTypeParameter(tree); |
|
1073 |
} |
|
1074 |
@Override |
|
1075 |
public void visitNewArray(final JCNewArray tree) { |
|
1076 |
annotate(tree, tree.annotations); |
|
1077 |
for (List<JCTypeAnnotation> dimAnnos : tree.dimAnnotations) |
|
1078 |
annotate(tree, dimAnnos); |
|
1079 |
super.visitNewArray(tree); |
|
1080 |
} |
|
1081 |
@Override |
|
1082 |
public void visitMethodDef(JCMethodDecl tree) { |
|
1083 |
annotate(tree, tree.receiverAnnotations); |
|
1084 |
super.visitMethodDef(tree); |
|
1085 |
} |
|
1086 |
} |
|
1087 |
||
1088 |
||
10 | 1089 |
private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) { |
6352
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1090 |
Scope baseScope = new Scope(tree.sym); |
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1091 |
//import already entered local classes into base scope |
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1092 |
for (Scope.Entry e = env.outer.info.scope.elems ; e != null ; e = e.sibling) { |
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1093 |
if (e.sym.isLocal()) { |
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1094 |
baseScope.enter(e.sym); |
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1095 |
} |
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1096 |
} |
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1097 |
//import current type-parameters into base scope |
10 | 1098 |
if (tree.typarams != null) |
1099 |
for (List<JCTypeParameter> typarams = tree.typarams; |
|
1100 |
typarams.nonEmpty(); |
|
1101 |
typarams = typarams.tail) |
|
6352
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1102 |
baseScope.enter(typarams.head.type.tsym); |
10 | 1103 |
Env<AttrContext> outer = env.outer; // the base clause can't see members of this class |
6352
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
5857
diff
changeset
|
1104 |
Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(baseScope)); |
10 | 1105 |
localEnv.baseClause = true; |
1106 |
localEnv.outer = outer; |
|
1107 |
localEnv.info.isSelfCall = false; |
|
1108 |
return localEnv; |
|
1109 |
} |
|
1110 |
||
1111 |
/** Enter member fields and methods of a class |
|
1112 |
* @param env the environment current for the class block. |
|
1113 |
*/ |
|
1114 |
private void finish(Env<AttrContext> env) { |
|
1115 |
JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
1116 |
try { |
|
1117 |
JCClassDecl tree = (JCClassDecl)env.tree; |
|
1118 |
finishClass(tree, env); |
|
1119 |
} finally { |
|
1120 |
log.useSource(prev); |
|
1121 |
} |
|
1122 |
} |
|
1123 |
||
1124 |
/** Generate a base clause for an enum type. |
|
1125 |
* @param pos The position for trees and diagnostics, if any |
|
1126 |
* @param c The class symbol of the enum |
|
1127 |
*/ |
|
1128 |
private JCExpression enumBase(int pos, ClassSymbol c) { |
|
1129 |
JCExpression result = make.at(pos). |
|
1130 |
TypeApply(make.QualIdent(syms.enumSym), |
|
1131 |
List.<JCExpression>of(make.Type(c.type))); |
|
1132 |
return result; |
|
1133 |
} |
|
1134 |
||
1135 |
/* *************************************************************************** |
|
1136 |
* tree building |
|
1137 |
****************************************************************************/ |
|
1138 |
||
1139 |
/** Generate default constructor for given class. For classes different |
|
1140 |
* from java.lang.Object, this is: |
|
1141 |
* |
|
1142 |
* c(argtype_0 x_0, ..., argtype_n x_n) throws thrown { |
|
1143 |
* super(x_0, ..., x_n) |
|
1144 |
* } |
|
1145 |
* |
|
1146 |
* or, if based == true: |
|
1147 |
* |
|
1148 |
* c(argtype_0 x_0, ..., argtype_n x_n) throws thrown { |
|
1149 |
* x_0.super(x_1, ..., x_n) |
|
1150 |
* } |
|
1151 |
* |
|
1152 |
* @param make The tree factory. |
|
1153 |
* @param c The class owning the default constructor. |
|
1154 |
* @param argtypes The parameter types of the constructor. |
|
1155 |
* @param thrown The thrown exceptions of the constructor. |
|
1156 |
* @param based Is first parameter a this$n? |
|
1157 |
*/ |
|
1158 |
JCTree DefaultConstructor(TreeMaker make, |
|
1159 |
ClassSymbol c, |
|
1160 |
List<Type> typarams, |
|
1161 |
List<Type> argtypes, |
|
1162 |
List<Type> thrown, |
|
1163 |
long flags, |
|
1164 |
boolean based) { |
|
1165 |
List<JCVariableDecl> params = make.Params(argtypes, syms.noSymbol); |
|
1166 |
List<JCStatement> stats = List.nil(); |
|
1167 |
if (c.type != syms.objectType) |
|
1168 |
stats = stats.prepend(SuperCall(make, typarams, params, based)); |
|
1169 |
if ((c.flags() & ENUM) != 0 && |
|
1170 |
(types.supertype(c.type).tsym == syms.enumSym || |
|
1171 |
target.compilerBootstrap(c))) { |
|
1172 |
// constructors of true enums are private |
|
1173 |
flags = (flags & ~AccessFlags) | PRIVATE | GENERATEDCONSTR; |
|
1174 |
} else |
|
1175 |
flags |= (c.flags() & AccessFlags) | GENERATEDCONSTR; |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1045
diff
changeset
|
1176 |
if (c.name.isEmpty()) flags |= ANONCONSTR; |
10 | 1177 |
JCTree result = make.MethodDef( |
1178 |
make.Modifiers(flags), |
|
1179 |
names.init, |
|
1180 |
null, |
|
1181 |
make.TypeParams(typarams), |
|
1182 |
params, |
|
1183 |
make.Types(thrown), |
|
1184 |
make.Block(0, stats), |
|
1185 |
null); |
|
1186 |
return result; |
|
1187 |
} |
|
1188 |
||
1189 |
/** Generate call to superclass constructor. This is: |
|
1190 |
* |
|
1191 |
* super(id_0, ..., id_n) |
|
1192 |
* |
|
1193 |
* or, if based == true |
|
1194 |
* |
|
1195 |
* id_0.super(id_1,...,id_n) |
|
1196 |
* |
|
1197 |
* where id_0, ..., id_n are the names of the given parameters. |
|
1198 |
* |
|
1199 |
* @param make The tree factory |
|
1200 |
* @param params The parameters that need to be passed to super |
|
1201 |
* @param typarams The type parameters that need to be passed to super |
|
1202 |
* @param based Is first parameter a this$n? |
|
1203 |
*/ |
|
1204 |
JCExpressionStatement SuperCall(TreeMaker make, |
|
1205 |
List<Type> typarams, |
|
1206 |
List<JCVariableDecl> params, |
|
1207 |
boolean based) { |
|
1208 |
JCExpression meth; |
|
1209 |
if (based) { |
|
1210 |
meth = make.Select(make.Ident(params.head), names._super); |
|
1211 |
params = params.tail; |
|
1212 |
} else { |
|
1213 |
meth = make.Ident(names._super); |
|
1214 |
} |
|
1215 |
List<JCExpression> typeargs = typarams.nonEmpty() ? make.Types(typarams) : null; |
|
1216 |
return make.Exec(make.Apply(typeargs, meth, make.Idents(params))); |
|
1217 |
} |
|
1218 |
} |