author | jjg |
Mon, 30 Aug 2010 18:03:35 -0700 | |
changeset 6582 | c7a4fb5a2f86 |
parent 6351 | 84c44db80d06 |
child 6587 | 344aa81a3b35 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 1999, 2009, 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.lang.model.element.ElementKind; |
|
31 |
import javax.tools.JavaFileObject; |
|
32 |
||
33 |
import com.sun.tools.javac.code.*; |
|
34 |
import com.sun.tools.javac.jvm.*; |
|
35 |
import com.sun.tools.javac.tree.*; |
|
36 |
import com.sun.tools.javac.util.*; |
|
37 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
38 |
import com.sun.tools.javac.util.List; |
|
39 |
||
40 |
import com.sun.tools.javac.jvm.Target; |
|
41 |
import com.sun.tools.javac.code.Symbol.*; |
|
42 |
import com.sun.tools.javac.tree.JCTree.*; |
|
43 |
import com.sun.tools.javac.code.Type.*; |
|
44 |
||
45 |
import com.sun.source.tree.IdentifierTree; |
|
46 |
import com.sun.source.tree.MemberSelectTree; |
|
47 |
import com.sun.source.tree.TreeVisitor; |
|
48 |
import com.sun.source.util.SimpleTreeVisitor; |
|
49 |
||
50 |
import static com.sun.tools.javac.code.Flags.*; |
|
51 |
import static com.sun.tools.javac.code.Kinds.*; |
|
52 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
53 |
||
54 |
/** This is the main context-dependent analysis phase in GJC. It |
|
55 |
* encompasses name resolution, type checking and constant folding as |
|
56 |
* subtasks. Some subtasks involve auxiliary classes. |
|
57 |
* @see Check |
|
58 |
* @see Resolve |
|
59 |
* @see ConstFold |
|
60 |
* @see Infer |
|
61 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
62 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
63 |
* If you write code that depends on this, you do so at your own risk. |
10 | 64 |
* This code and its internal interfaces are subject to change or |
65 |
* deletion without notice.</b> |
|
66 |
*/ |
|
67 |
public class Attr extends JCTree.Visitor { |
|
68 |
protected static final Context.Key<Attr> attrKey = |
|
69 |
new Context.Key<Attr>(); |
|
70 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
71 |
final Names names; |
10 | 72 |
final Log log; |
73 |
final Symtab syms; |
|
74 |
final Resolve rs; |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
75 |
final Infer infer; |
10 | 76 |
final Check chk; |
77 |
final MemberEnter memberEnter; |
|
78 |
final TreeMaker make; |
|
79 |
final ConstFold cfolder; |
|
80 |
final Enter enter; |
|
81 |
final Target target; |
|
82 |
final Types types; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
941
diff
changeset
|
83 |
final JCDiagnostic.Factory diags; |
10 | 84 |
final Annotate annotate; |
85 |
||
86 |
public static Attr instance(Context context) { |
|
87 |
Attr instance = context.get(attrKey); |
|
88 |
if (instance == null) |
|
89 |
instance = new Attr(context); |
|
90 |
return instance; |
|
91 |
} |
|
92 |
||
93 |
protected Attr(Context context) { |
|
94 |
context.put(attrKey, this); |
|
95 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
96 |
names = Names.instance(context); |
10 | 97 |
log = Log.instance(context); |
98 |
syms = Symtab.instance(context); |
|
99 |
rs = Resolve.instance(context); |
|
100 |
chk = Check.instance(context); |
|
101 |
memberEnter = MemberEnter.instance(context); |
|
102 |
make = TreeMaker.instance(context); |
|
103 |
enter = Enter.instance(context); |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
104 |
infer = Infer.instance(context); |
10 | 105 |
cfolder = ConstFold.instance(context); |
106 |
target = Target.instance(context); |
|
107 |
types = Types.instance(context); |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
941
diff
changeset
|
108 |
diags = JCDiagnostic.Factory.instance(context); |
10 | 109 |
annotate = Annotate.instance(context); |
110 |
||
111 |
Options options = Options.instance(context); |
|
112 |
||
113 |
Source source = Source.instance(context); |
|
114 |
allowGenerics = source.allowGenerics(); |
|
115 |
allowVarargs = source.allowVarargs(); |
|
116 |
allowEnums = source.allowEnums(); |
|
117 |
allowBoxing = source.allowBoxing(); |
|
118 |
allowCovariantReturns = source.allowCovariantReturns(); |
|
119 |
allowAnonOuterThis = source.allowAnonOuterThis(); |
|
4142 | 120 |
allowStringsInSwitch = source.allowStringsInSwitch(); |
121 |
sourceName = source.name; |
|
10 | 122 |
relax = (options.get("-retrofit") != null || |
123 |
options.get("-relax") != null); |
|
124 |
useBeforeDeclarationWarning = options.get("useBeforeDeclarationWarning") != null; |
|
3661 | 125 |
enableSunApiLintControl = options.get("enableSunApiLintControl") != null; |
10 | 126 |
} |
127 |
||
128 |
/** Switch: relax some constraints for retrofit mode. |
|
129 |
*/ |
|
130 |
boolean relax; |
|
131 |
||
132 |
/** Switch: support generics? |
|
133 |
*/ |
|
134 |
boolean allowGenerics; |
|
135 |
||
136 |
/** Switch: allow variable-arity methods. |
|
137 |
*/ |
|
138 |
boolean allowVarargs; |
|
139 |
||
140 |
/** Switch: support enums? |
|
141 |
*/ |
|
142 |
boolean allowEnums; |
|
143 |
||
144 |
/** Switch: support boxing and unboxing? |
|
145 |
*/ |
|
146 |
boolean allowBoxing; |
|
147 |
||
148 |
/** Switch: support covariant result types? |
|
149 |
*/ |
|
150 |
boolean allowCovariantReturns; |
|
151 |
||
152 |
/** Switch: allow references to surrounding object from anonymous |
|
153 |
* objects during constructor call? |
|
154 |
*/ |
|
155 |
boolean allowAnonOuterThis; |
|
156 |
||
157 |
/** |
|
158 |
* Switch: warn about use of variable before declaration? |
|
159 |
* RFE: 6425594 |
|
160 |
*/ |
|
161 |
boolean useBeforeDeclarationWarning; |
|
162 |
||
3661 | 163 |
/** |
5848
c5a4ce47e780
6960407: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5847
diff
changeset
|
164 |
* Switch: allow lint infrastructure to control proprietary |
3661 | 165 |
* API warnings. |
166 |
*/ |
|
167 |
boolean enableSunApiLintControl; |
|
168 |
||
4142 | 169 |
/** |
170 |
* Switch: allow strings in switch? |
|
171 |
*/ |
|
172 |
boolean allowStringsInSwitch; |
|
173 |
||
174 |
/** |
|
175 |
* Switch: name of source level; used for error reporting. |
|
176 |
*/ |
|
177 |
String sourceName; |
|
178 |
||
10 | 179 |
/** Check kind and type of given tree against protokind and prototype. |
180 |
* If check succeeds, store type in tree and return it. |
|
181 |
* If check fails, store errType in tree and return it. |
|
182 |
* No checks are performed if the prototype is a method type. |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
183 |
* It is not necessary in this case since we know that kind and type |
10 | 184 |
* are correct. |
185 |
* |
|
186 |
* @param tree The tree whose kind and type is checked |
|
187 |
* @param owntype The computed type of the tree |
|
188 |
* @param ownkind The computed kind of the tree |
|
189 |
* @param pkind The expected kind (or: protokind) of the tree |
|
190 |
* @param pt The expected type (or: prototype) of the tree |
|
191 |
*/ |
|
192 |
Type check(JCTree tree, Type owntype, int ownkind, int pkind, Type pt) { |
|
193 |
if (owntype.tag != ERROR && pt.tag != METHOD && pt.tag != FORALL) { |
|
194 |
if ((ownkind & ~pkind) == 0) { |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
195 |
owntype = chk.checkType(tree.pos(), owntype, pt, errKey); |
10 | 196 |
} else { |
197 |
log.error(tree.pos(), "unexpected.type", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
198 |
kindNames(pkind), |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
199 |
kindName(ownkind)); |
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
200 |
owntype = types.createErrorType(owntype); |
10 | 201 |
} |
202 |
} |
|
203 |
tree.type = owntype; |
|
204 |
return owntype; |
|
205 |
} |
|
206 |
||
207 |
/** Is given blank final variable assignable, i.e. in a scope where it |
|
208 |
* may be assigned to even though it is final? |
|
209 |
* @param v The blank final variable. |
|
210 |
* @param env The current environment. |
|
211 |
*/ |
|
212 |
boolean isAssignableAsBlankFinal(VarSymbol v, Env<AttrContext> env) { |
|
213 |
Symbol owner = env.info.scope.owner; |
|
214 |
// owner refers to the innermost variable, method or |
|
215 |
// initializer block declaration at this point. |
|
216 |
return |
|
217 |
v.owner == owner |
|
218 |
|| |
|
219 |
((owner.name == names.init || // i.e. we are in a constructor |
|
220 |
owner.kind == VAR || // i.e. we are in a variable initializer |
|
221 |
(owner.flags() & BLOCK) != 0) // i.e. we are in an initializer block |
|
222 |
&& |
|
223 |
v.owner == owner.owner |
|
224 |
&& |
|
225 |
((v.flags() & STATIC) != 0) == Resolve.isStatic(env)); |
|
226 |
} |
|
227 |
||
228 |
/** Check that variable can be assigned to. |
|
229 |
* @param pos The current source code position. |
|
230 |
* @param v The assigned varaible |
|
231 |
* @param base If the variable is referred to in a Select, the part |
|
232 |
* to the left of the `.', null otherwise. |
|
233 |
* @param env The current environment. |
|
234 |
*/ |
|
235 |
void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) { |
|
236 |
if ((v.flags() & FINAL) != 0 && |
|
237 |
((v.flags() & HASINIT) != 0 |
|
238 |
|| |
|
239 |
!((base == null || |
|
240 |
(base.getTag() == JCTree.IDENT && TreeInfo.name(base) == names._this)) && |
|
241 |
isAssignableAsBlankFinal(v, env)))) { |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
242 |
if (v.isResourceVariable()) { //TWR resource |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
243 |
log.error(pos, "twr.resource.may.not.be.assigned", v); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
244 |
} else { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
245 |
log.error(pos, "cant.assign.val.to.final.var", v); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
246 |
} |
10 | 247 |
} |
248 |
} |
|
249 |
||
250 |
/** Does tree represent a static reference to an identifier? |
|
251 |
* It is assumed that tree is either a SELECT or an IDENT. |
|
252 |
* We have to weed out selects from non-type names here. |
|
253 |
* @param tree The candidate tree. |
|
254 |
*/ |
|
255 |
boolean isStaticReference(JCTree tree) { |
|
256 |
if (tree.getTag() == JCTree.SELECT) { |
|
257 |
Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected); |
|
258 |
if (lsym == null || lsym.kind != TYP) { |
|
259 |
return false; |
|
260 |
} |
|
261 |
} |
|
262 |
return true; |
|
263 |
} |
|
264 |
||
265 |
/** Is this symbol a type? |
|
266 |
*/ |
|
267 |
static boolean isType(Symbol sym) { |
|
268 |
return sym != null && sym.kind == TYP; |
|
269 |
} |
|
270 |
||
271 |
/** The current `this' symbol. |
|
272 |
* @param env The current environment. |
|
273 |
*/ |
|
274 |
Symbol thisSym(DiagnosticPosition pos, Env<AttrContext> env) { |
|
275 |
return rs.resolveSelf(pos, env, env.enclClass.sym, names._this); |
|
276 |
} |
|
277 |
||
278 |
/** Attribute a parsed identifier. |
|
279 |
* @param tree Parsed identifier name |
|
280 |
* @param topLevel The toplevel to use |
|
281 |
*/ |
|
282 |
public Symbol attribIdent(JCTree tree, JCCompilationUnit topLevel) { |
|
283 |
Env<AttrContext> localEnv = enter.topLevelEnv(topLevel); |
|
284 |
localEnv.enclClass = make.ClassDef(make.Modifiers(0), |
|
285 |
syms.errSymbol.name, |
|
286 |
null, null, null, null); |
|
287 |
localEnv.enclClass.sym = syms.errSymbol; |
|
288 |
return tree.accept(identAttributer, localEnv); |
|
289 |
} |
|
290 |
// where |
|
291 |
private TreeVisitor<Symbol,Env<AttrContext>> identAttributer = new IdentAttributer(); |
|
292 |
private class IdentAttributer extends SimpleTreeVisitor<Symbol,Env<AttrContext>> { |
|
293 |
@Override |
|
294 |
public Symbol visitMemberSelect(MemberSelectTree node, Env<AttrContext> env) { |
|
295 |
Symbol site = visit(node.getExpression(), env); |
|
296 |
if (site.kind == ERR) |
|
297 |
return site; |
|
298 |
Name name = (Name)node.getIdentifier(); |
|
299 |
if (site.kind == PCK) { |
|
300 |
env.toplevel.packge = (PackageSymbol)site; |
|
301 |
return rs.findIdentInPackage(env, (TypeSymbol)site, name, TYP | PCK); |
|
302 |
} else { |
|
303 |
env.enclClass.sym = (ClassSymbol)site; |
|
304 |
return rs.findMemberType(env, site.asType(), name, (TypeSymbol)site); |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
@Override |
|
309 |
public Symbol visitIdentifier(IdentifierTree node, Env<AttrContext> env) { |
|
310 |
return rs.findIdent(env, (Name)node.getName(), TYP | PCK); |
|
311 |
} |
|
312 |
} |
|
313 |
||
314 |
public Type coerce(Type etype, Type ttype) { |
|
315 |
return cfolder.coerce(etype, ttype); |
|
316 |
} |
|
317 |
||
318 |
public Type attribType(JCTree node, TypeSymbol sym) { |
|
319 |
Env<AttrContext> env = enter.typeEnvs.get(sym); |
|
320 |
Env<AttrContext> localEnv = env.dup(node, env.info.dup()); |
|
321 |
return attribTree(node, localEnv, Kinds.TYP, Type.noType); |
|
322 |
} |
|
323 |
||
324 |
public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) { |
|
325 |
breakTree = tree; |
|
3144
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
2723
diff
changeset
|
326 |
JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
10 | 327 |
try { |
328 |
attribExpr(expr, env); |
|
329 |
} catch (BreakAttr b) { |
|
330 |
return b.env; |
|
331 |
} finally { |
|
332 |
breakTree = null; |
|
333 |
log.useSource(prev); |
|
334 |
} |
|
335 |
return env; |
|
336 |
} |
|
337 |
||
338 |
public Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) { |
|
339 |
breakTree = tree; |
|
3144
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
2723
diff
changeset
|
340 |
JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
10 | 341 |
try { |
342 |
attribStat(stmt, env); |
|
343 |
} catch (BreakAttr b) { |
|
344 |
return b.env; |
|
345 |
} finally { |
|
346 |
breakTree = null; |
|
347 |
log.useSource(prev); |
|
348 |
} |
|
349 |
return env; |
|
350 |
} |
|
351 |
||
352 |
private JCTree breakTree = null; |
|
353 |
||
354 |
private static class BreakAttr extends RuntimeException { |
|
355 |
static final long serialVersionUID = -6924771130405446405L; |
|
356 |
private Env<AttrContext> env; |
|
357 |
private BreakAttr(Env<AttrContext> env) { |
|
358 |
this.env = env; |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
||
363 |
/* ************************************************************************ |
|
364 |
* Visitor methods |
|
365 |
*************************************************************************/ |
|
366 |
||
367 |
/** Visitor argument: the current environment. |
|
368 |
*/ |
|
369 |
Env<AttrContext> env; |
|
370 |
||
371 |
/** Visitor argument: the currently expected proto-kind. |
|
372 |
*/ |
|
373 |
int pkind; |
|
374 |
||
375 |
/** Visitor argument: the currently expected proto-type. |
|
376 |
*/ |
|
377 |
Type pt; |
|
378 |
||
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
379 |
/** Visitor argument: the error key to be generated when a type error occurs |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
380 |
*/ |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
381 |
String errKey; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
382 |
|
10 | 383 |
/** Visitor result: the computed type. |
384 |
*/ |
|
385 |
Type result; |
|
386 |
||
387 |
/** Visitor method: attribute a tree, catching any completion failure |
|
388 |
* exceptions. Return the tree's type. |
|
389 |
* |
|
390 |
* @param tree The tree to be visited. |
|
391 |
* @param env The environment visitor argument. |
|
392 |
* @param pkind The protokind visitor argument. |
|
393 |
* @param pt The prototype visitor argument. |
|
394 |
*/ |
|
395 |
Type attribTree(JCTree tree, Env<AttrContext> env, int pkind, Type pt) { |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
396 |
return attribTree(tree, env, pkind, pt, "incompatible.types"); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
397 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
398 |
|
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
399 |
Type attribTree(JCTree tree, Env<AttrContext> env, int pkind, Type pt, String errKey) { |
10 | 400 |
Env<AttrContext> prevEnv = this.env; |
401 |
int prevPkind = this.pkind; |
|
402 |
Type prevPt = this.pt; |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
403 |
String prevErrKey = this.errKey; |
10 | 404 |
try { |
405 |
this.env = env; |
|
406 |
this.pkind = pkind; |
|
407 |
this.pt = pt; |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
408 |
this.errKey = errKey; |
10 | 409 |
tree.accept(this); |
410 |
if (tree == breakTree) |
|
411 |
throw new BreakAttr(env); |
|
412 |
return result; |
|
413 |
} catch (CompletionFailure ex) { |
|
414 |
tree.type = syms.errType; |
|
415 |
return chk.completionError(tree.pos(), ex); |
|
416 |
} finally { |
|
417 |
this.env = prevEnv; |
|
418 |
this.pkind = prevPkind; |
|
419 |
this.pt = prevPt; |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
420 |
this.errKey = prevErrKey; |
10 | 421 |
} |
422 |
} |
|
423 |
||
424 |
/** Derived visitor method: attribute an expression tree. |
|
425 |
*/ |
|
426 |
public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) { |
|
427 |
return attribTree(tree, env, VAL, pt.tag != ERROR ? pt : Type.noType); |
|
428 |
} |
|
429 |
||
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
430 |
public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt, String key) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
431 |
return attribTree(tree, env, VAL, pt.tag != ERROR ? pt : Type.noType, key); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
432 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
433 |
|
10 | 434 |
/** Derived visitor method: attribute an expression tree with |
435 |
* no constraints on the computed type. |
|
436 |
*/ |
|
437 |
Type attribExpr(JCTree tree, Env<AttrContext> env) { |
|
438 |
return attribTree(tree, env, VAL, Type.noType); |
|
439 |
} |
|
440 |
||
441 |
/** Derived visitor method: attribute a type tree. |
|
442 |
*/ |
|
443 |
Type attribType(JCTree tree, Env<AttrContext> env) { |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
444 |
Type result = attribType(tree, env, Type.noType); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
445 |
return result; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
446 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
447 |
|
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
448 |
/** Derived visitor method: attribute a type tree. |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
449 |
*/ |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
450 |
Type attribType(JCTree tree, Env<AttrContext> env, Type pt) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
451 |
Type result = attribTree(tree, env, TYP, pt); |
10 | 452 |
return result; |
453 |
} |
|
454 |
||
455 |
/** Derived visitor method: attribute a statement or definition tree. |
|
456 |
*/ |
|
457 |
public Type attribStat(JCTree tree, Env<AttrContext> env) { |
|
458 |
return attribTree(tree, env, NIL, Type.noType); |
|
459 |
} |
|
460 |
||
461 |
/** Attribute a list of expressions, returning a list of types. |
|
462 |
*/ |
|
463 |
List<Type> attribExprs(List<JCExpression> trees, Env<AttrContext> env, Type pt) { |
|
464 |
ListBuffer<Type> ts = new ListBuffer<Type>(); |
|
465 |
for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) |
|
466 |
ts.append(attribExpr(l.head, env, pt)); |
|
467 |
return ts.toList(); |
|
468 |
} |
|
469 |
||
470 |
/** Attribute a list of statements, returning nothing. |
|
471 |
*/ |
|
472 |
<T extends JCTree> void attribStats(List<T> trees, Env<AttrContext> env) { |
|
473 |
for (List<T> l = trees; l.nonEmpty(); l = l.tail) |
|
474 |
attribStat(l.head, env); |
|
475 |
} |
|
476 |
||
477 |
/** Attribute the arguments in a method call, returning a list of types. |
|
478 |
*/ |
|
479 |
List<Type> attribArgs(List<JCExpression> trees, Env<AttrContext> env) { |
|
480 |
ListBuffer<Type> argtypes = new ListBuffer<Type>(); |
|
481 |
for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) |
|
482 |
argtypes.append(chk.checkNonVoid( |
|
483 |
l.head.pos(), types.upperBound(attribTree(l.head, env, VAL, Infer.anyPoly)))); |
|
484 |
return argtypes.toList(); |
|
485 |
} |
|
486 |
||
487 |
/** Attribute a type argument list, returning a list of types. |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
488 |
* Caller is responsible for calling checkRefTypes. |
10 | 489 |
*/ |
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
490 |
List<Type> attribAnyTypes(List<JCExpression> trees, Env<AttrContext> env) { |
10 | 491 |
ListBuffer<Type> argtypes = new ListBuffer<Type>(); |
492 |
for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
493 |
argtypes.append(attribType(l.head, env)); |
10 | 494 |
return argtypes.toList(); |
495 |
} |
|
496 |
||
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
497 |
/** Attribute a type argument list, returning a list of types. |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
498 |
* Check that all the types are references. |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
499 |
*/ |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
500 |
List<Type> attribTypes(List<JCExpression> trees, Env<AttrContext> env) { |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
501 |
List<Type> types = attribAnyTypes(trees, env); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
502 |
return chk.checkRefTypes(trees, types); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
503 |
} |
10 | 504 |
|
505 |
/** |
|
506 |
* Attribute type variables (of generic classes or methods). |
|
507 |
* Compound types are attributed later in attribBounds. |
|
508 |
* @param typarams the type variables to enter |
|
509 |
* @param env the current environment |
|
510 |
*/ |
|
511 |
void attribTypeVariables(List<JCTypeParameter> typarams, Env<AttrContext> env) { |
|
512 |
for (JCTypeParameter tvar : typarams) { |
|
513 |
TypeVar a = (TypeVar)tvar.type; |
|
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
514 |
a.tsym.flags_field |= UNATTRIBUTED; |
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
515 |
a.bound = Type.noType; |
10 | 516 |
if (!tvar.bounds.isEmpty()) { |
517 |
List<Type> bounds = List.of(attribType(tvar.bounds.head, env)); |
|
518 |
for (JCExpression bound : tvar.bounds.tail) |
|
519 |
bounds = bounds.prepend(attribType(bound, env)); |
|
520 |
types.setBounds(a, bounds.reverse()); |
|
521 |
} else { |
|
522 |
// if no bounds are given, assume a single bound of |
|
523 |
// java.lang.Object. |
|
524 |
types.setBounds(a, List.of(syms.objectType)); |
|
525 |
} |
|
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
526 |
a.tsym.flags_field &= ~UNATTRIBUTED; |
10 | 527 |
} |
528 |
for (JCTypeParameter tvar : typarams) |
|
529 |
chk.checkNonCyclic(tvar.pos(), (TypeVar)tvar.type); |
|
530 |
attribStats(typarams, env); |
|
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
531 |
} |
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
532 |
|
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
533 |
void attribBounds(List<JCTypeParameter> typarams) { |
10 | 534 |
for (JCTypeParameter typaram : typarams) { |
535 |
Type bound = typaram.type.getUpperBound(); |
|
536 |
if (bound != null && bound.tsym instanceof ClassSymbol) { |
|
537 |
ClassSymbol c = (ClassSymbol)bound.tsym; |
|
538 |
if ((c.flags_field & COMPOUND) != 0) { |
|
539 |
assert (c.flags_field & UNATTRIBUTED) != 0 : c; |
|
540 |
attribClass(typaram.pos(), c); |
|
541 |
} |
|
542 |
} |
|
543 |
} |
|
544 |
} |
|
545 |
||
546 |
/** |
|
547 |
* Attribute the type references in a list of annotations. |
|
548 |
*/ |
|
549 |
void attribAnnotationTypes(List<JCAnnotation> annotations, |
|
550 |
Env<AttrContext> env) { |
|
551 |
for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) { |
|
552 |
JCAnnotation a = al.head; |
|
553 |
attribType(a.annotationType, env); |
|
554 |
} |
|
555 |
} |
|
556 |
||
557 |
/** Attribute type reference in an `extends' or `implements' clause. |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
558 |
* Supertypes of anonymous inner classes are usually already attributed. |
10 | 559 |
* |
560 |
* @param tree The tree making up the type reference. |
|
561 |
* @param env The environment current at the reference. |
|
562 |
* @param classExpected true if only a class is expected here. |
|
563 |
* @param interfaceExpected true if only an interface is expected here. |
|
564 |
*/ |
|
565 |
Type attribBase(JCTree tree, |
|
566 |
Env<AttrContext> env, |
|
567 |
boolean classExpected, |
|
568 |
boolean interfaceExpected, |
|
569 |
boolean checkExtensible) { |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
570 |
Type t = tree.type != null ? |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
571 |
tree.type : |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
572 |
attribType(tree, env); |
10 | 573 |
return checkBase(t, tree, env, classExpected, interfaceExpected, checkExtensible); |
574 |
} |
|
575 |
Type checkBase(Type t, |
|
576 |
JCTree tree, |
|
577 |
Env<AttrContext> env, |
|
578 |
boolean classExpected, |
|
579 |
boolean interfaceExpected, |
|
580 |
boolean checkExtensible) { |
|
6582
c7a4fb5a2f86
6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents:
6351
diff
changeset
|
581 |
if (t.isErroneous()) |
c7a4fb5a2f86
6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents:
6351
diff
changeset
|
582 |
return t; |
10 | 583 |
if (t.tag == TYPEVAR && !classExpected && !interfaceExpected) { |
584 |
// check that type variable is already visible |
|
585 |
if (t.getUpperBound() == null) { |
|
586 |
log.error(tree.pos(), "illegal.forward.ref"); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
587 |
return types.createErrorType(t); |
10 | 588 |
} |
589 |
} else { |
|
590 |
t = chk.checkClassType(tree.pos(), t, checkExtensible|!allowGenerics); |
|
591 |
} |
|
592 |
if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) { |
|
593 |
log.error(tree.pos(), "intf.expected.here"); |
|
594 |
// return errType is necessary since otherwise there might |
|
595 |
// be undetected cycles which cause attribution to loop |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
596 |
return types.createErrorType(t); |
10 | 597 |
} else if (checkExtensible && |
598 |
classExpected && |
|
599 |
(t.tsym.flags() & INTERFACE) != 0) { |
|
6582
c7a4fb5a2f86
6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents:
6351
diff
changeset
|
600 |
log.error(tree.pos(), "no.intf.expected.here"); |
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
601 |
return types.createErrorType(t); |
10 | 602 |
} |
603 |
if (checkExtensible && |
|
604 |
((t.tsym.flags() & FINAL) != 0)) { |
|
605 |
log.error(tree.pos(), |
|
606 |
"cant.inherit.from.final", t.tsym); |
|
607 |
} |
|
608 |
chk.checkNonCyclic(tree.pos(), t); |
|
609 |
return t; |
|
610 |
} |
|
611 |
||
612 |
public void visitClassDef(JCClassDecl tree) { |
|
613 |
// Local classes have not been entered yet, so we need to do it now: |
|
614 |
if ((env.info.scope.owner.kind & (VAR | MTH)) != 0) |
|
615 |
enter.classEnter(tree, env); |
|
616 |
||
617 |
ClassSymbol c = tree.sym; |
|
618 |
if (c == null) { |
|
619 |
// exit in case something drastic went wrong during enter. |
|
620 |
result = null; |
|
621 |
} else { |
|
622 |
// make sure class has been completed: |
|
623 |
c.complete(); |
|
624 |
||
625 |
// If this class appears as an anonymous class |
|
626 |
// in a superclass constructor call where |
|
627 |
// no explicit outer instance is given, |
|
628 |
// disable implicit outer instance from being passed. |
|
629 |
// (This would be an illegal access to "this before super"). |
|
630 |
if (env.info.isSelfCall && |
|
631 |
env.tree.getTag() == JCTree.NEWCLASS && |
|
632 |
((JCNewClass) env.tree).encl == null) |
|
633 |
{ |
|
634 |
c.flags_field |= NOOUTERTHIS; |
|
635 |
} |
|
636 |
attribClass(tree.pos(), c); |
|
637 |
result = tree.type = c.type; |
|
638 |
} |
|
639 |
} |
|
640 |
||
641 |
public void visitMethodDef(JCMethodDecl tree) { |
|
642 |
MethodSymbol m = tree.sym; |
|
643 |
||
644 |
Lint lint = env.info.lint.augment(m.attributes_field, m.flags()); |
|
645 |
Lint prevLint = chk.setLint(lint); |
|
646 |
try { |
|
647 |
chk.checkDeprecatedAnnotation(tree.pos(), m); |
|
648 |
||
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
649 |
attribBounds(tree.typarams); |
10 | 650 |
|
651 |
// If we override any other methods, check that we do so properly. |
|
652 |
// JLS ??? |
|
653 |
chk.checkOverride(tree, m); |
|
654 |
||
655 |
// Create a new environment with local scope |
|
656 |
// for attributing the method. |
|
657 |
Env<AttrContext> localEnv = memberEnter.methodEnv(tree, env); |
|
658 |
||
659 |
localEnv.info.lint = lint; |
|
660 |
||
661 |
// Enter all type parameters into the local method scope. |
|
662 |
for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) |
|
663 |
localEnv.info.scope.enterIfAbsent(l.head.type.tsym); |
|
664 |
||
665 |
ClassSymbol owner = env.enclClass.sym; |
|
666 |
if ((owner.flags() & ANNOTATION) != 0 && |
|
667 |
tree.params.nonEmpty()) |
|
668 |
log.error(tree.params.head.pos(), |
|
669 |
"intf.annotation.members.cant.have.params"); |
|
670 |
||
671 |
// Attribute all value parameters. |
|
672 |
for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
|
673 |
attribStat(l.head, localEnv); |
|
674 |
} |
|
675 |
||
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5654
diff
changeset
|
676 |
chk.checkVarargMethodDecl(tree); |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5654
diff
changeset
|
677 |
|
10 | 678 |
// Check that type parameters are well-formed. |
1358 | 679 |
chk.validate(tree.typarams, localEnv); |
10 | 680 |
|
681 |
// Check that result type is well-formed. |
|
1358 | 682 |
chk.validate(tree.restype, localEnv); |
6342
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
683 |
|
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
684 |
// annotation method checks |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
685 |
if ((owner.flags() & ANNOTATION) != 0) { |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
686 |
// annotation method cannot have throws clause |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
687 |
if (tree.thrown.nonEmpty()) { |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
688 |
log.error(tree.thrown.head.pos(), |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
689 |
"throws.not.allowed.in.intf.annotation"); |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
690 |
} |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
691 |
// annotation method cannot declare type-parameters |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
692 |
if (tree.typarams.nonEmpty()) { |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
693 |
log.error(tree.typarams.head.pos(), |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
694 |
"intf.annotation.members.cant.have.type.params"); |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
695 |
} |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
696 |
// validate annotation method's return type (could be an annotation type) |
10 | 697 |
chk.validateAnnotationType(tree.restype); |
6342
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
698 |
// ensure that annotation method does not clash with members of Object/Annotation |
10 | 699 |
chk.validateAnnotationMethod(tree.pos(), m); |
700 |
||
6347
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6344
diff
changeset
|
701 |
if (tree.defaultValue != null) { |
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6344
diff
changeset
|
702 |
// if default value is an annotation, check it is a well-formed |
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6344
diff
changeset
|
703 |
// annotation value (e.g. no duplicate values, no missing values, etc.) |
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6344
diff
changeset
|
704 |
chk.validateAnnotationTree(tree.defaultValue); |
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6344
diff
changeset
|
705 |
} |
6342
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
706 |
} |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6154
diff
changeset
|
707 |
|
10 | 708 |
for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail) |
709 |
chk.checkType(l.head.pos(), l.head.type, syms.throwableType); |
|
710 |
||
711 |
if (tree.body == null) { |
|
712 |
// Empty bodies are only allowed for |
|
713 |
// abstract, native, or interface methods, or for methods |
|
714 |
// in a retrofit signature class. |
|
715 |
if ((owner.flags() & INTERFACE) == 0 && |
|
716 |
(tree.mods.flags & (ABSTRACT | NATIVE)) == 0 && |
|
717 |
!relax) |
|
718 |
log.error(tree.pos(), "missing.meth.body.or.decl.abstract"); |
|
719 |
if (tree.defaultValue != null) { |
|
720 |
if ((owner.flags() & ANNOTATION) == 0) |
|
721 |
log.error(tree.pos(), |
|
722 |
"default.allowed.in.intf.annotation.member"); |
|
723 |
} |
|
724 |
} else if ((owner.flags() & INTERFACE) != 0) { |
|
725 |
log.error(tree.body.pos(), "intf.meth.cant.have.body"); |
|
726 |
} else if ((tree.mods.flags & ABSTRACT) != 0) { |
|
727 |
log.error(tree.pos(), "abstract.meth.cant.have.body"); |
|
728 |
} else if ((tree.mods.flags & NATIVE) != 0) { |
|
729 |
log.error(tree.pos(), "native.meth.cant.have.body"); |
|
730 |
} else { |
|
731 |
// Add an implicit super() call unless an explicit call to |
|
732 |
// super(...) or this(...) is given |
|
733 |
// or we are compiling class java.lang.Object. |
|
734 |
if (tree.name == names.init && owner.type != syms.objectType) { |
|
735 |
JCBlock body = tree.body; |
|
736 |
if (body.stats.isEmpty() || |
|
737 |
!TreeInfo.isSelfCall(body.stats.head)) { |
|
738 |
body.stats = body.stats. |
|
739 |
prepend(memberEnter.SuperCall(make.at(body.pos), |
|
740 |
List.<Type>nil(), |
|
741 |
List.<JCVariableDecl>nil(), |
|
742 |
false)); |
|
743 |
} else if ((env.enclClass.sym.flags() & ENUM) != 0 && |
|
744 |
(tree.mods.flags & GENERATEDCONSTR) == 0 && |
|
745 |
TreeInfo.isSuperCall(body.stats.head)) { |
|
746 |
// enum constructors are not allowed to call super |
|
747 |
// directly, so make sure there aren't any super calls |
|
748 |
// in enum constructors, except in the compiler |
|
749 |
// generated one. |
|
750 |
log.error(tree.body.stats.head.pos(), |
|
751 |
"call.to.super.not.allowed.in.enum.ctor", |
|
752 |
env.enclClass.sym); |
|
753 |
} |
|
754 |
} |
|
755 |
||
756 |
// Attribute method body. |
|
757 |
attribStat(tree.body, localEnv); |
|
758 |
} |
|
759 |
localEnv.info.scope.leave(); |
|
760 |
result = tree.type = m.type; |
|
761 |
chk.validateAnnotations(tree.mods.annotations, m); |
|
762 |
} |
|
763 |
finally { |
|
764 |
chk.setLint(prevLint); |
|
765 |
} |
|
766 |
} |
|
767 |
||
768 |
public void visitVarDef(JCVariableDecl tree) { |
|
769 |
// Local variables have not been entered yet, so we need to do it now: |
|
770 |
if (env.info.scope.owner.kind == MTH) { |
|
771 |
if (tree.sym != null) { |
|
772 |
// parameters have already been entered |
|
773 |
env.info.scope.enter(tree.sym); |
|
774 |
} else { |
|
775 |
memberEnter.memberEnter(tree, env); |
|
776 |
annotate.flush(); |
|
777 |
} |
|
778 |
} |
|
779 |
||
780 |
VarSymbol v = tree.sym; |
|
781 |
Lint lint = env.info.lint.augment(v.attributes_field, v.flags()); |
|
782 |
Lint prevLint = chk.setLint(lint); |
|
783 |
||
1538
3cbbc9424f43
6763518: Impossible to suppress raw-type warnings
mcimadamore
parents:
1534
diff
changeset
|
784 |
// Check that the variable's declared type is well-formed. |
3cbbc9424f43
6763518: Impossible to suppress raw-type warnings
mcimadamore
parents:
1534
diff
changeset
|
785 |
chk.validate(tree.vartype, env); |
3cbbc9424f43
6763518: Impossible to suppress raw-type warnings
mcimadamore
parents:
1534
diff
changeset
|
786 |
|
10 | 787 |
try { |
788 |
chk.checkDeprecatedAnnotation(tree.pos(), v); |
|
789 |
||
790 |
if (tree.init != null) { |
|
791 |
if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) { |
|
792 |
// In this case, `v' is final. Ensure that it's initializer is |
|
793 |
// evaluated. |
|
794 |
v.getConstValue(); // ensure initializer is evaluated |
|
795 |
} else { |
|
796 |
// Attribute initializer in a new environment |
|
797 |
// with the declared variable as owner. |
|
798 |
// Check that initializer conforms to variable's declared type. |
|
799 |
Env<AttrContext> initEnv = memberEnter.initEnv(tree, env); |
|
800 |
initEnv.info.lint = lint; |
|
801 |
// In order to catch self-references, we set the variable's |
|
802 |
// declaration position to maximal possible value, effectively |
|
803 |
// marking the variable as undefined. |
|
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
804 |
initEnv.info.enclVar = v; |
10 | 805 |
attribExpr(tree.init, initEnv, v.type); |
806 |
} |
|
807 |
} |
|
808 |
result = tree.type = v.type; |
|
809 |
chk.validateAnnotations(tree.mods.annotations, v); |
|
810 |
} |
|
811 |
finally { |
|
812 |
chk.setLint(prevLint); |
|
813 |
} |
|
814 |
} |
|
815 |
||
816 |
public void visitSkip(JCSkip tree) { |
|
817 |
result = null; |
|
818 |
} |
|
819 |
||
820 |
public void visitBlock(JCBlock tree) { |
|
821 |
if (env.info.scope.owner.kind == TYP) { |
|
822 |
// Block is a static or instance initializer; |
|
823 |
// let the owner of the environment be a freshly |
|
824 |
// created BLOCK-method. |
|
825 |
Env<AttrContext> localEnv = |
|
826 |
env.dup(tree, env.info.dup(env.info.scope.dupUnshared())); |
|
827 |
localEnv.info.scope.owner = |
|
828 |
new MethodSymbol(tree.flags | BLOCK, names.empty, null, |
|
829 |
env.info.scope.owner); |
|
830 |
if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++; |
|
831 |
attribStats(tree.stats, localEnv); |
|
832 |
} else { |
|
833 |
// Create a new local environment with a local scope. |
|
834 |
Env<AttrContext> localEnv = |
|
835 |
env.dup(tree, env.info.dup(env.info.scope.dup())); |
|
836 |
attribStats(tree.stats, localEnv); |
|
837 |
localEnv.info.scope.leave(); |
|
838 |
} |
|
839 |
result = null; |
|
840 |
} |
|
841 |
||
842 |
public void visitDoLoop(JCDoWhileLoop tree) { |
|
843 |
attribStat(tree.body, env.dup(tree)); |
|
844 |
attribExpr(tree.cond, env, syms.booleanType); |
|
845 |
result = null; |
|
846 |
} |
|
847 |
||
848 |
public void visitWhileLoop(JCWhileLoop tree) { |
|
849 |
attribExpr(tree.cond, env, syms.booleanType); |
|
850 |
attribStat(tree.body, env.dup(tree)); |
|
851 |
result = null; |
|
852 |
} |
|
853 |
||
854 |
public void visitForLoop(JCForLoop tree) { |
|
855 |
Env<AttrContext> loopEnv = |
|
856 |
env.dup(env.tree, env.info.dup(env.info.scope.dup())); |
|
857 |
attribStats(tree.init, loopEnv); |
|
858 |
if (tree.cond != null) attribExpr(tree.cond, loopEnv, syms.booleanType); |
|
859 |
loopEnv.tree = tree; // before, we were not in loop! |
|
860 |
attribStats(tree.step, loopEnv); |
|
861 |
attribStat(tree.body, loopEnv); |
|
862 |
loopEnv.info.scope.leave(); |
|
863 |
result = null; |
|
864 |
} |
|
865 |
||
866 |
public void visitForeachLoop(JCEnhancedForLoop tree) { |
|
867 |
Env<AttrContext> loopEnv = |
|
868 |
env.dup(env.tree, env.info.dup(env.info.scope.dup())); |
|
869 |
attribStat(tree.var, loopEnv); |
|
870 |
Type exprType = types.upperBound(attribExpr(tree.expr, loopEnv)); |
|
871 |
chk.checkNonVoid(tree.pos(), exprType); |
|
872 |
Type elemtype = types.elemtype(exprType); // perhaps expr is an array? |
|
873 |
if (elemtype == null) { |
|
874 |
// or perhaps expr implements Iterable<T>? |
|
875 |
Type base = types.asSuper(exprType, syms.iterableType.tsym); |
|
876 |
if (base == null) { |
|
877 |
log.error(tree.expr.pos(), "foreach.not.applicable.to.type"); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
878 |
elemtype = types.createErrorType(exprType); |
10 | 879 |
} else { |
880 |
List<Type> iterableParams = base.allparams(); |
|
881 |
elemtype = iterableParams.isEmpty() |
|
882 |
? syms.objectType |
|
883 |
: types.upperBound(iterableParams.head); |
|
884 |
} |
|
885 |
} |
|
886 |
chk.checkType(tree.expr.pos(), elemtype, tree.var.sym.type); |
|
887 |
loopEnv.tree = tree; // before, we were not in loop! |
|
888 |
attribStat(tree.body, loopEnv); |
|
889 |
loopEnv.info.scope.leave(); |
|
890 |
result = null; |
|
891 |
} |
|
892 |
||
893 |
public void visitLabelled(JCLabeledStatement tree) { |
|
894 |
// Check that label is not used in an enclosing statement |
|
895 |
Env<AttrContext> env1 = env; |
|
896 |
while (env1 != null && env1.tree.getTag() != JCTree.CLASSDEF) { |
|
897 |
if (env1.tree.getTag() == JCTree.LABELLED && |
|
898 |
((JCLabeledStatement) env1.tree).label == tree.label) { |
|
899 |
log.error(tree.pos(), "label.already.in.use", |
|
900 |
tree.label); |
|
901 |
break; |
|
902 |
} |
|
903 |
env1 = env1.next; |
|
904 |
} |
|
905 |
||
906 |
attribStat(tree.body, env.dup(tree)); |
|
907 |
result = null; |
|
908 |
} |
|
909 |
||
910 |
public void visitSwitch(JCSwitch tree) { |
|
911 |
Type seltype = attribExpr(tree.selector, env); |
|
912 |
||
913 |
Env<AttrContext> switchEnv = |
|
914 |
env.dup(tree, env.info.dup(env.info.scope.dup())); |
|
915 |
||
916 |
boolean enumSwitch = |
|
917 |
allowEnums && |
|
918 |
(seltype.tsym.flags() & Flags.ENUM) != 0; |
|
4142 | 919 |
boolean stringSwitch = false; |
920 |
if (types.isSameType(seltype, syms.stringType)) { |
|
921 |
if (allowStringsInSwitch) { |
|
922 |
stringSwitch = true; |
|
923 |
} else { |
|
924 |
log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName); |
|
925 |
} |
|
926 |
} |
|
927 |
if (!enumSwitch && !stringSwitch) |
|
10 | 928 |
seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType); |
929 |
||
930 |
// Attribute all cases and |
|
931 |
// check that there are no duplicate case labels or default clauses. |
|
932 |
Set<Object> labels = new HashSet<Object>(); // The set of case labels. |
|
933 |
boolean hasDefault = false; // Is there a default label? |
|
934 |
for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) { |
|
935 |
JCCase c = l.head; |
|
936 |
Env<AttrContext> caseEnv = |
|
937 |
switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup())); |
|
938 |
if (c.pat != null) { |
|
939 |
if (enumSwitch) { |
|
940 |
Symbol sym = enumConstant(c.pat, seltype); |
|
941 |
if (sym == null) { |
|
942 |
log.error(c.pat.pos(), "enum.const.req"); |
|
943 |
} else if (!labels.add(sym)) { |
|
944 |
log.error(c.pos(), "duplicate.case.label"); |
|
945 |
} |
|
946 |
} else { |
|
947 |
Type pattype = attribExpr(c.pat, switchEnv, seltype); |
|
948 |
if (pattype.tag != ERROR) { |
|
949 |
if (pattype.constValue() == null) { |
|
4142 | 950 |
log.error(c.pat.pos(), |
951 |
(stringSwitch ? "string.const.req" : "const.expr.req")); |
|
10 | 952 |
} else if (labels.contains(pattype.constValue())) { |
953 |
log.error(c.pos(), "duplicate.case.label"); |
|
954 |
} else { |
|
955 |
labels.add(pattype.constValue()); |
|
956 |
} |
|
957 |
} |
|
958 |
} |
|
959 |
} else if (hasDefault) { |
|
960 |
log.error(c.pos(), "duplicate.default.label"); |
|
961 |
} else { |
|
962 |
hasDefault = true; |
|
963 |
} |
|
964 |
attribStats(c.stats, caseEnv); |
|
965 |
caseEnv.info.scope.leave(); |
|
966 |
addVars(c.stats, switchEnv.info.scope); |
|
967 |
} |
|
968 |
||
969 |
switchEnv.info.scope.leave(); |
|
970 |
result = null; |
|
971 |
} |
|
972 |
// where |
|
973 |
/** Add any variables defined in stats to the switch scope. */ |
|
974 |
private static void addVars(List<JCStatement> stats, Scope switchScope) { |
|
975 |
for (;stats.nonEmpty(); stats = stats.tail) { |
|
976 |
JCTree stat = stats.head; |
|
977 |
if (stat.getTag() == JCTree.VARDEF) |
|
978 |
switchScope.enter(((JCVariableDecl) stat).sym); |
|
979 |
} |
|
980 |
} |
|
981 |
// where |
|
982 |
/** Return the selected enumeration constant symbol, or null. */ |
|
983 |
private Symbol enumConstant(JCTree tree, Type enumType) { |
|
984 |
if (tree.getTag() != JCTree.IDENT) { |
|
985 |
log.error(tree.pos(), "enum.label.must.be.unqualified.enum"); |
|
986 |
return syms.errSymbol; |
|
987 |
} |
|
988 |
JCIdent ident = (JCIdent)tree; |
|
989 |
Name name = ident.name; |
|
990 |
for (Scope.Entry e = enumType.tsym.members().lookup(name); |
|
991 |
e.scope != null; e = e.next()) { |
|
992 |
if (e.sym.kind == VAR) { |
|
993 |
Symbol s = ident.sym = e.sym; |
|
994 |
((VarSymbol)s).getConstValue(); // ensure initializer is evaluated |
|
995 |
ident.type = s.type; |
|
996 |
return ((s.flags_field & Flags.ENUM) == 0) |
|
997 |
? null : s; |
|
998 |
} |
|
999 |
} |
|
1000 |
return null; |
|
1001 |
} |
|
1002 |
||
1003 |
public void visitSynchronized(JCSynchronized tree) { |
|
1004 |
chk.checkRefType(tree.pos(), attribExpr(tree.lock, env)); |
|
1005 |
attribStat(tree.body, env); |
|
1006 |
result = null; |
|
1007 |
} |
|
1008 |
||
1009 |
public void visitTry(JCTry tree) { |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1010 |
// Create a new local environment with a local |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1011 |
Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup())); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1012 |
boolean isTryWithResource = tree.resources.nonEmpty(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1013 |
// Create a nested environment for attributing the try block if needed |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1014 |
Env<AttrContext> tryEnv = isTryWithResource ? |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1015 |
env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) : |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1016 |
localEnv; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1017 |
// Attribute resource declarations |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1018 |
for (JCTree resource : tree.resources) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1019 |
if (resource.getTag() == JCTree.VARDEF) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1020 |
attribStat(resource, tryEnv); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1021 |
chk.checkType(resource, resource.type, syms.autoCloseableType, "twr.not.applicable.to.type"); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1022 |
VarSymbol var = (VarSymbol)TreeInfo.symbolFor(resource); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1023 |
var.setData(ElementKind.RESOURCE_VARIABLE); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1024 |
} else { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1025 |
attribExpr(resource, tryEnv, syms.autoCloseableType, "twr.not.applicable.to.type"); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1026 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1027 |
} |
10 | 1028 |
// Attribute body |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1029 |
attribStat(tree.body, tryEnv); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1030 |
if (isTryWithResource) |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1031 |
tryEnv.info.scope.leave(); |
10 | 1032 |
|
1033 |
// Attribute catch clauses |
|
1034 |
for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { |
|
1035 |
JCCatch c = l.head; |
|
1036 |
Env<AttrContext> catchEnv = |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1037 |
localEnv.dup(c, localEnv.info.dup(localEnv.info.scope.dup())); |
10 | 1038 |
Type ctype = attribStat(c.param, catchEnv); |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
1039 |
if (TreeInfo.isMultiCatch(c)) { |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
1040 |
//check that multi-catch parameter is marked as final |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
1041 |
if ((c.param.sym.flags() & FINAL) == 0) { |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
1042 |
log.error(c.param.pos(), "multicatch.param.must.be.final", c.param.sym); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
1043 |
} |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
1044 |
c.param.sym.flags_field = c.param.sym.flags() | DISJOINT; |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
1045 |
} |
10 | 1046 |
if (c.param.type.tsym.kind == Kinds.VAR) { |
1047 |
c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER); |
|
1048 |
} |
|
1049 |
chk.checkType(c.param.vartype.pos(), |
|
1050 |
chk.checkClassType(c.param.vartype.pos(), ctype), |
|
1051 |
syms.throwableType); |
|
1052 |
attribStat(c.body, catchEnv); |
|
1053 |
catchEnv.info.scope.leave(); |
|
1054 |
} |
|
1055 |
||
1056 |
// Attribute finalizer |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1057 |
if (tree.finalizer != null) attribStat(tree.finalizer, localEnv); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1058 |
|
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
1059 |
localEnv.info.scope.leave(); |
10 | 1060 |
result = null; |
1061 |
} |
|
1062 |
||
1063 |
public void visitConditional(JCConditional tree) { |
|
1064 |
attribExpr(tree.cond, env, syms.booleanType); |
|
1065 |
attribExpr(tree.truepart, env); |
|
1066 |
attribExpr(tree.falsepart, env); |
|
1067 |
result = check(tree, |
|
1068 |
capture(condType(tree.pos(), tree.cond.type, |
|
1069 |
tree.truepart.type, tree.falsepart.type)), |
|
1070 |
VAL, pkind, pt); |
|
1071 |
} |
|
1072 |
//where |
|
1073 |
/** Compute the type of a conditional expression, after |
|
1074 |
* checking that it exists. See Spec 15.25. |
|
1075 |
* |
|
1076 |
* @param pos The source position to be used for |
|
1077 |
* error diagnostics. |
|
1078 |
* @param condtype The type of the expression's condition. |
|
1079 |
* @param thentype The type of the expression's then-part. |
|
1080 |
* @param elsetype The type of the expression's else-part. |
|
1081 |
*/ |
|
1082 |
private Type condType(DiagnosticPosition pos, |
|
1083 |
Type condtype, |
|
1084 |
Type thentype, |
|
1085 |
Type elsetype) { |
|
1086 |
Type ctype = condType1(pos, condtype, thentype, elsetype); |
|
1087 |
||
1088 |
// If condition and both arms are numeric constants, |
|
1089 |
// evaluate at compile-time. |
|
1090 |
return ((condtype.constValue() != null) && |
|
1091 |
(thentype.constValue() != null) && |
|
1092 |
(elsetype.constValue() != null)) |
|
1093 |
? cfolder.coerce(condtype.isTrue()?thentype:elsetype, ctype) |
|
1094 |
: ctype; |
|
1095 |
} |
|
1096 |
/** Compute the type of a conditional expression, after |
|
1097 |
* checking that it exists. Does not take into |
|
1098 |
* account the special case where condition and both arms |
|
1099 |
* are constants. |
|
1100 |
* |
|
1101 |
* @param pos The source position to be used for error |
|
1102 |
* diagnostics. |
|
1103 |
* @param condtype The type of the expression's condition. |
|
1104 |
* @param thentype The type of the expression's then-part. |
|
1105 |
* @param elsetype The type of the expression's else-part. |
|
1106 |
*/ |
|
1107 |
private Type condType1(DiagnosticPosition pos, Type condtype, |
|
1108 |
Type thentype, Type elsetype) { |
|
1109 |
// If same type, that is the result |
|
1110 |
if (types.isSameType(thentype, elsetype)) |
|
1111 |
return thentype.baseType(); |
|
1112 |
||
1113 |
Type thenUnboxed = (!allowBoxing || thentype.isPrimitive()) |
|
1114 |
? thentype : types.unboxedType(thentype); |
|
1115 |
Type elseUnboxed = (!allowBoxing || elsetype.isPrimitive()) |
|
1116 |
? elsetype : types.unboxedType(elsetype); |
|
1117 |
||
1118 |
// Otherwise, if both arms can be converted to a numeric |
|
1119 |
// type, return the least numeric type that fits both arms |
|
1120 |
// (i.e. return larger of the two, or return int if one |
|
1121 |
// arm is short, the other is char). |
|
1122 |
if (thenUnboxed.isPrimitive() && elseUnboxed.isPrimitive()) { |
|
1123 |
// If one arm has an integer subrange type (i.e., byte, |
|
1124 |
// short, or char), and the other is an integer constant |
|
1125 |
// that fits into the subrange, return the subrange type. |
|
1126 |
if (thenUnboxed.tag < INT && elseUnboxed.tag == INT && |
|
1127 |
types.isAssignable(elseUnboxed, thenUnboxed)) |
|
1128 |
return thenUnboxed.baseType(); |
|
1129 |
if (elseUnboxed.tag < INT && thenUnboxed.tag == INT && |
|
1130 |
types.isAssignable(thenUnboxed, elseUnboxed)) |
|
1131 |
return elseUnboxed.baseType(); |
|
1132 |
||
1133 |
for (int i = BYTE; i < VOID; i++) { |
|
1134 |
Type candidate = syms.typeOfTag[i]; |
|
1135 |
if (types.isSubtype(thenUnboxed, candidate) && |
|
1136 |
types.isSubtype(elseUnboxed, candidate)) |
|
1137 |
return candidate; |
|
1138 |
} |
|
1139 |
} |
|
1140 |
||
1141 |
// Those were all the cases that could result in a primitive |
|
1142 |
if (allowBoxing) { |
|
1143 |
if (thentype.isPrimitive()) |
|
1144 |
thentype = types.boxedClass(thentype).type; |
|
1145 |
if (elsetype.isPrimitive()) |
|
1146 |
elsetype = types.boxedClass(elsetype).type; |
|
1147 |
} |
|
1148 |
||
1149 |
if (types.isSubtype(thentype, elsetype)) |
|
1150 |
return elsetype.baseType(); |
|
1151 |
if (types.isSubtype(elsetype, thentype)) |
|
1152 |
return thentype.baseType(); |
|
1153 |
||
1154 |
if (!allowBoxing || thentype.tag == VOID || elsetype.tag == VOID) { |
|
1155 |
log.error(pos, "neither.conditional.subtype", |
|
1156 |
thentype, elsetype); |
|
1157 |
return thentype.baseType(); |
|
1158 |
} |
|
1159 |
||
1160 |
// both are known to be reference types. The result is |
|
1161 |
// lub(thentype,elsetype). This cannot fail, as it will |
|
1162 |
// always be possible to infer "Object" if nothing better. |
|
1163 |
return types.lub(thentype.baseType(), elsetype.baseType()); |
|
1164 |
} |
|
1165 |
||
1166 |
public void visitIf(JCIf tree) { |
|
1167 |
attribExpr(tree.cond, env, syms.booleanType); |
|
1168 |
attribStat(tree.thenpart, env); |
|
1169 |
if (tree.elsepart != null) |
|
1170 |
attribStat(tree.elsepart, env); |
|
1171 |
chk.checkEmptyIf(tree); |
|
1172 |
result = null; |
|
1173 |
} |
|
1174 |
||
1175 |
public void visitExec(JCExpressionStatement tree) { |
|
1176 |
attribExpr(tree.expr, env); |
|
1177 |
result = null; |
|
1178 |
} |
|
1179 |
||
1180 |
public void visitBreak(JCBreak tree) { |
|
1181 |
tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env); |
|
1182 |
result = null; |
|
1183 |
} |
|
1184 |
||
1185 |
public void visitContinue(JCContinue tree) { |
|
1186 |
tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env); |
|
1187 |
result = null; |
|
1188 |
} |
|
1189 |
//where |
|
1190 |
/** Return the target of a break or continue statement, if it exists, |
|
1191 |
* report an error if not. |
|
1192 |
* Note: The target of a labelled break or continue is the |
|
1193 |
* (non-labelled) statement tree referred to by the label, |
|
1194 |
* not the tree representing the labelled statement itself. |
|
1195 |
* |
|
1196 |
* @param pos The position to be used for error diagnostics |
|
1197 |
* @param tag The tag of the jump statement. This is either |
|
1198 |
* Tree.BREAK or Tree.CONTINUE. |
|
1199 |
* @param label The label of the jump statement, or null if no |
|
1200 |
* label is given. |
|
1201 |
* @param env The environment current at the jump statement. |
|
1202 |
*/ |
|
1203 |
private JCTree findJumpTarget(DiagnosticPosition pos, |
|
1204 |
int tag, |
|
1205 |
Name label, |
|
1206 |
Env<AttrContext> env) { |
|
1207 |
// Search environments outwards from the point of jump. |
|
1208 |
Env<AttrContext> env1 = env; |
|
1209 |
LOOP: |
|
1210 |
while (env1 != null) { |
|
1211 |
switch (env1.tree.getTag()) { |
|
1212 |
case JCTree.LABELLED: |
|
1213 |
JCLabeledStatement labelled = (JCLabeledStatement)env1.tree; |
|
1214 |
if (label == labelled.label) { |
|
1215 |
// If jump is a continue, check that target is a loop. |
|
1216 |
if (tag == JCTree.CONTINUE) { |
|
1217 |
if (labelled.body.getTag() != JCTree.DOLOOP && |
|
1218 |
labelled.body.getTag() != JCTree.WHILELOOP && |
|
1219 |
labelled.body.getTag() != JCTree.FORLOOP && |
|
1220 |
labelled.body.getTag() != JCTree.FOREACHLOOP) |
|
1221 |
log.error(pos, "not.loop.label", label); |
|
1222 |
// Found labelled statement target, now go inwards |
|
1223 |
// to next non-labelled tree. |
|
1224 |
return TreeInfo.referencedStatement(labelled); |
|
1225 |
} else { |
|
1226 |
return labelled; |
|
1227 |
} |
|
1228 |
} |
|
1229 |
break; |
|
1230 |
case JCTree.DOLOOP: |
|
1231 |
case JCTree.WHILELOOP: |
|
1232 |
case JCTree.FORLOOP: |
|
1233 |
case JCTree.FOREACHLOOP: |
|
1234 |
if (label == null) return env1.tree; |
|
1235 |
break; |
|
1236 |
case JCTree.SWITCH: |
|
1237 |
if (label == null && tag == JCTree.BREAK) return env1.tree; |
|
1238 |
break; |
|
1239 |
case JCTree.METHODDEF: |
|
1240 |
case JCTree.CLASSDEF: |
|
1241 |
break LOOP; |
|
1242 |
default: |
|
1243 |
} |
|
1244 |
env1 = env1.next; |
|
1245 |
} |
|
1246 |
if (label != null) |
|
1247 |
log.error(pos, "undef.label", label); |
|
1248 |
else if (tag == JCTree.CONTINUE) |
|
1249 |
log.error(pos, "cont.outside.loop"); |
|
1250 |
else |
|
1251 |
log.error(pos, "break.outside.switch.loop"); |
|
1252 |
return null; |
|
1253 |
} |
|
1254 |
||
1255 |
public void visitReturn(JCReturn tree) { |
|
1256 |
// Check that there is an enclosing method which is |
|
1257 |
// nested within than the enclosing class. |
|
1258 |
if (env.enclMethod == null || |
|
1259 |
env.enclMethod.sym.owner != env.enclClass.sym) { |
|
1260 |
log.error(tree.pos(), "ret.outside.meth"); |
|
1261 |
||
1262 |
} else { |
|
1263 |
// Attribute return expression, if it exists, and check that |
|
1264 |
// it conforms to result type of enclosing method. |
|
1265 |
Symbol m = env.enclMethod.sym; |
|
1266 |
if (m.type.getReturnType().tag == VOID) { |
|
1267 |
if (tree.expr != null) |
|
1268 |
log.error(tree.expr.pos(), |
|
1269 |
"cant.ret.val.from.meth.decl.void"); |
|
1270 |
} else if (tree.expr == null) { |
|
1271 |
log.error(tree.pos(), "missing.ret.val"); |
|
1272 |
} else { |
|
1273 |
attribExpr(tree.expr, env, m.type.getReturnType()); |
|
1274 |
} |
|
1275 |
} |
|
1276 |
result = null; |
|
1277 |
} |
|
1278 |
||
1279 |
public void visitThrow(JCThrow tree) { |
|
1280 |
attribExpr(tree.expr, env, syms.throwableType); |
|
1281 |
result = null; |
|
1282 |
} |
|
1283 |
||
1284 |
public void visitAssert(JCAssert tree) { |
|
1285 |
attribExpr(tree.cond, env, syms.booleanType); |
|
1286 |
if (tree.detail != null) { |
|
1287 |
chk.checkNonVoid(tree.detail.pos(), attribExpr(tree.detail, env)); |
|
1288 |
} |
|
1289 |
result = null; |
|
1290 |
} |
|
1291 |
||
1292 |
/** Visitor method for method invocations. |
|
1293 |
* NOTE: The method part of an application will have in its type field |
|
1294 |
* the return type of the method, not the method's type itself! |
|
1295 |
*/ |
|
1296 |
public void visitApply(JCMethodInvocation tree) { |
|
1297 |
// The local environment of a method application is |
|
1298 |
// a new environment nested in the current one. |
|
1299 |
Env<AttrContext> localEnv = env.dup(tree, env.info.dup()); |
|
1300 |
||
1301 |
// The types of the actual method arguments. |
|
1302 |
List<Type> argtypes; |
|
1303 |
||
1304 |
// The types of the actual method type arguments. |
|
1305 |
List<Type> typeargtypes = null; |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1306 |
boolean typeargtypesNonRefOK = false; |
10 | 1307 |
|
1308 |
Name methName = TreeInfo.name(tree.meth); |
|
1309 |
||
1310 |
boolean isConstructorCall = |
|
1311 |
methName == names._this || methName == names._super; |
|
1312 |
||
1313 |
if (isConstructorCall) { |
|
1314 |
// We are seeing a ...this(...) or ...super(...) call. |
|
1315 |
// Check that this is the first statement in a constructor. |
|
1316 |
if (checkFirstConstructorStat(tree, env)) { |
|
1317 |
||
1318 |
// Record the fact |
|
1319 |
// that this is a constructor call (using isSelfCall). |
|
1320 |
localEnv.info.isSelfCall = true; |
|
1321 |
||
1322 |
// Attribute arguments, yielding list of argument types. |
|
1323 |
argtypes = attribArgs(tree.args, localEnv); |
|
1324 |
typeargtypes = attribTypes(tree.typeargs, localEnv); |
|
1325 |
||
1326 |
// Variable `site' points to the class in which the called |
|
1327 |
// constructor is defined. |
|
1328 |
Type site = env.enclClass.sym.type; |
|
1329 |
if (methName == names._super) { |
|
1330 |
if (site == syms.objectType) { |
|
1331 |
log.error(tree.meth.pos(), "no.superclass", site); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
1332 |
site = types.createErrorType(syms.objectType); |
10 | 1333 |
} else { |
1334 |
site = types.supertype(site); |
|
1335 |
} |
|
1336 |
} |
|
1337 |
||
1338 |
if (site.tag == CLASS) { |
|
3559
58cfcc0f1aa9
6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents:
3556
diff
changeset
|
1339 |
Type encl = site.getEnclosingType(); |
58cfcc0f1aa9
6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents:
3556
diff
changeset
|
1340 |
while (encl != null && encl.tag == TYPEVAR) |
58cfcc0f1aa9
6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents:
3556
diff
changeset
|
1341 |
encl = encl.getUpperBound(); |
58cfcc0f1aa9
6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents:
3556
diff
changeset
|
1342 |
if (encl.tag == CLASS) { |
10 | 1343 |
// we are calling a nested class |
1344 |
||
1345 |
if (tree.meth.getTag() == JCTree.SELECT) { |
|
1346 |
JCTree qualifier = ((JCFieldAccess) tree.meth).selected; |
|
1347 |
||
1348 |
// We are seeing a prefixed call, of the form |
|
1349 |
// <expr>.super(...). |
|
1350 |
// Check that the prefix expression conforms |
|
1351 |
// to the outer instance type of the class. |
|
1352 |
chk.checkRefType(qualifier.pos(), |
|
1353 |
attribExpr(qualifier, localEnv, |
|
3559
58cfcc0f1aa9
6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents:
3556
diff
changeset
|
1354 |
encl)); |
10 | 1355 |
} else if (methName == names._super) { |
1356 |
// qualifier omitted; check for existence |
|
1357 |
// of an appropriate implicit qualifier. |
|
1358 |
rs.resolveImplicitThis(tree.meth.pos(), |
|
1359 |
localEnv, site); |
|
1360 |
} |
|
1361 |
} else if (tree.meth.getTag() == JCTree.SELECT) { |
|
1362 |
log.error(tree.meth.pos(), "illegal.qual.not.icls", |
|
1363 |
site.tsym); |
|
1364 |
} |
|
1365 |
||
1366 |
// if we're calling a java.lang.Enum constructor, |
|
1367 |
// prefix the implicit String and int parameters |
|
1368 |
if (site.tsym == syms.enumSym && allowEnums) |
|
1369 |
argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType); |
|
1370 |
||
1371 |
// Resolve the called constructor under the assumption |
|
1372 |
// that we are referring to a superclass instance of the |
|
1373 |
// current instance (JLS ???). |
|
1374 |
boolean selectSuperPrev = localEnv.info.selectSuper; |
|
1375 |
localEnv.info.selectSuper = true; |
|
1376 |
localEnv.info.varArgs = false; |
|
1377 |
Symbol sym = rs.resolveConstructor( |
|
1378 |
tree.meth.pos(), localEnv, site, argtypes, typeargtypes); |
|
1379 |
localEnv.info.selectSuper = selectSuperPrev; |
|
1380 |
||
1381 |
// Set method symbol to resolved constructor... |
|
1382 |
TreeInfo.setSymbol(tree.meth, sym); |
|
1383 |
||
1384 |
// ...and check that it is legal in the current context. |
|
1385 |
// (this will also set the tree's type) |
|
1386 |
Type mpt = newMethTemplate(argtypes, typeargtypes); |
|
1387 |
checkId(tree.meth, site, sym, localEnv, MTH, |
|
1388 |
mpt, tree.varargsElement != null); |
|
1389 |
} |
|
1390 |
// Otherwise, `site' is an error type and we do nothing |
|
1391 |
} |
|
1392 |
result = tree.type = syms.voidType; |
|
1393 |
} else { |
|
1394 |
// Otherwise, we are seeing a regular method call. |
|
1395 |
// Attribute the arguments, yielding list of argument types, ... |
|
1396 |
argtypes = attribArgs(tree.args, localEnv); |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1397 |
typeargtypes = attribAnyTypes(tree.typeargs, localEnv); |
10 | 1398 |
|
1399 |
// ... and attribute the method using as a prototype a methodtype |
|
1400 |
// whose formal argument types is exactly the list of actual |
|
1401 |
// arguments (this will also set the method symbol). |
|
1402 |
Type mpt = newMethTemplate(argtypes, typeargtypes); |
|
1403 |
localEnv.info.varArgs = false; |
|
1404 |
Type mtype = attribExpr(tree.meth, localEnv, mpt); |
|
1405 |
if (localEnv.info.varArgs) |
|
1406 |
assert mtype.isErroneous() || tree.varargsElement != null; |
|
1407 |
||
1408 |
// Compute the result type. |
|
1409 |
Type restype = mtype.getReturnType(); |
|
1410 |
assert restype.tag != WILDCARD : mtype; |
|
1411 |
||
1412 |
// as a special case, array.clone() has a result that is |
|
1413 |
// the same as static type of the array being cloned |
|
1414 |
if (tree.meth.getTag() == JCTree.SELECT && |
|
1415 |
allowCovariantReturns && |
|
1416 |
methName == names.clone && |
|
1417 |
types.isArray(((JCFieldAccess) tree.meth).selected.type)) |
|
1418 |
restype = ((JCFieldAccess) tree.meth).selected.type; |
|
1419 |
||
1420 |
// as a special case, x.getClass() has type Class<? extends |X|> |
|
1421 |
if (allowGenerics && |
|
1422 |
methName == names.getClass && tree.args.isEmpty()) { |
|
1423 |
Type qualifier = (tree.meth.getTag() == JCTree.SELECT) |
|
1424 |
? ((JCFieldAccess) tree.meth).selected.type |
|
1425 |
: env.enclClass.sym.type; |
|
1426 |
restype = new |
|
1427 |
ClassType(restype.getEnclosingType(), |
|
1428 |
List.<Type>of(new WildcardType(types.erasure(qualifier), |
|
1429 |
BoundKind.EXTENDS, |
|
1430 |
syms.boundClass)), |
|
1431 |
restype.tsym); |
|
1432 |
} |
|
1433 |
||
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1434 |
// as a special case, MethodHandle.<T>invoke(abc) and InvokeDynamic.<T>foo(abc) |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1435 |
// has type <T>, and T can be a primitive type. |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1436 |
if (tree.meth.getTag() == JCTree.SELECT && !typeargtypes.isEmpty()) { |
5736
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
5321
diff
changeset
|
1437 |
JCFieldAccess mfield = (JCFieldAccess) tree.meth; |
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
5321
diff
changeset
|
1438 |
if ((mfield.selected.type.tsym != null && |
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
5321
diff
changeset
|
1439 |
(mfield.selected.type.tsym.flags() & POLYMORPHIC_SIGNATURE) != 0) |
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
5321
diff
changeset
|
1440 |
|| |
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
5321
diff
changeset
|
1441 |
(mfield.sym != null && |
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
5321
diff
changeset
|
1442 |
(mfield.sym.flags() & POLYMORPHIC_SIGNATURE) != 0)) { |
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1443 |
assert types.isSameType(restype, typeargtypes.head) : mtype; |
5736
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
5321
diff
changeset
|
1444 |
assert mfield.selected.type == syms.methodHandleType |
ee0850472ca1
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
5321
diff
changeset
|
1445 |
|| mfield.selected.type == syms.invokeDynamicType; |
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1446 |
typeargtypesNonRefOK = true; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1447 |
} |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1448 |
} |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1449 |
|
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1450 |
if (!typeargtypesNonRefOK) { |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1451 |
chk.checkRefTypes(tree.typeargs, typeargtypes); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1452 |
} |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1453 |
|
10 | 1454 |
// Check that value of resulting type is admissible in the |
1455 |
// current context. Also, capture the return type |
|
5320
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
1456 |
result = check(tree, capture(restype), VAL, pkind, pt); |
10 | 1457 |
} |
1358 | 1458 |
chk.validate(tree.typeargs, localEnv); |
10 | 1459 |
} |
1460 |
//where |
|
1461 |
/** Check that given application node appears as first statement |
|
1462 |
* in a constructor call. |
|
1463 |
* @param tree The application node |
|
1464 |
* @param env The environment current at the application. |
|
1465 |
*/ |
|
1466 |
boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) { |
|
1467 |
JCMethodDecl enclMethod = env.enclMethod; |
|
1468 |
if (enclMethod != null && enclMethod.name == names.init) { |
|
1469 |
JCBlock body = enclMethod.body; |
|
1470 |
if (body.stats.head.getTag() == JCTree.EXEC && |
|
1471 |
((JCExpressionStatement) body.stats.head).expr == tree) |
|
1472 |
return true; |
|
1473 |
} |
|
1474 |
log.error(tree.pos(),"call.must.be.first.stmt.in.ctor", |
|
1475 |
TreeInfo.name(tree.meth)); |
|
1476 |
return false; |
|
1477 |
} |
|
1478 |
||
1479 |
/** Obtain a method type with given argument types. |
|
1480 |
*/ |
|
1481 |
Type newMethTemplate(List<Type> argtypes, List<Type> typeargtypes) { |
|
1482 |
MethodType mt = new MethodType(argtypes, null, null, syms.methodClass); |
|
1483 |
return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt); |
|
1484 |
} |
|
1485 |
||
1486 |
public void visitNewClass(JCNewClass tree) { |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
1487 |
Type owntype = types.createErrorType(tree.type); |
10 | 1488 |
|
1489 |
// The local environment of a class creation is |
|
1490 |
// a new environment nested in the current one. |
|
1491 |
Env<AttrContext> localEnv = env.dup(tree, env.info.dup()); |
|
1492 |
||
1493 |
// The anonymous inner class definition of the new expression, |
|
1494 |
// if one is defined by it. |
|
1495 |
JCClassDecl cdef = tree.def; |
|
1496 |
||
1497 |
// If enclosing class is given, attribute it, and |
|
1498 |
// complete class name to be fully qualified |
|
1499 |
JCExpression clazz = tree.clazz; // Class field following new |
|
1500 |
JCExpression clazzid = // Identifier in class field |
|
1501 |
(clazz.getTag() == JCTree.TYPEAPPLY) |
|
1502 |
? ((JCTypeApply) clazz).clazz |
|
1503 |
: clazz; |
|
1504 |
||
1505 |
JCExpression clazzid1 = clazzid; // The same in fully qualified form |
|
1506 |
||
1507 |
if (tree.encl != null) { |
|
1508 |
// We are seeing a qualified new, of the form |
|
1509 |
// <expr>.new C <...> (...) ... |
|
1510 |
// In this case, we let clazz stand for the name of the |
|
1511 |
// allocated class C prefixed with the type of the qualifier |
|
1512 |
// expression, so that we can |
|
1513 |
// resolve it with standard techniques later. I.e., if |
|
1514 |
// <expr> has type T, then <expr>.new C <...> (...) |
|
1515 |
// yields a clazz T.C. |
|
1516 |
Type encltype = chk.checkRefType(tree.encl.pos(), |
|
1517 |
attribExpr(tree.encl, env)); |
|
1518 |
clazzid1 = make.at(clazz.pos).Select(make.Type(encltype), |
|
1519 |
((JCIdent) clazzid).name); |
|
1520 |
if (clazz.getTag() == JCTree.TYPEAPPLY) |
|
1521 |
clazz = make.at(tree.pos). |
|
1522 |
TypeApply(clazzid1, |
|
1523 |
((JCTypeApply) clazz).arguments); |
|
1524 |
else |
|
1525 |
clazz = clazzid1; |
|
1526 |
} |
|
1527 |
||
1528 |
// Attribute clazz expression and store |
|
1529 |
// symbol + type back into the attributed tree. |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1530 |
Type clazztype = attribType(clazz, env); |
5649
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1531 |
Pair<Scope,Scope> mapping = getSyntheticScopeMapping(clazztype); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1532 |
if (!TreeInfo.isDiamond(tree)) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1533 |
clazztype = chk.checkClassType( |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1534 |
tree.clazz.pos(), clazztype, true); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1535 |
} |
1358 | 1536 |
chk.validate(clazz, localEnv); |
10 | 1537 |
if (tree.encl != null) { |
1538 |
// We have to work in this case to store |
|
1539 |
// symbol + type back into the attributed tree. |
|
1540 |
tree.clazz.type = clazztype; |
|
1541 |
TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1)); |
|
1542 |
clazzid.type = ((JCIdent) clazzid).sym.type; |
|
1543 |
if (!clazztype.isErroneous()) { |
|
1544 |
if (cdef != null && clazztype.tsym.isInterface()) { |
|
1545 |
log.error(tree.encl.pos(), "anon.class.impl.intf.no.qual.for.new"); |
|
1546 |
} else if (clazztype.tsym.isStatic()) { |
|
1547 |
log.error(tree.encl.pos(), "qualified.new.of.static.class", clazztype.tsym); |
|
1548 |
} |
|
1549 |
} |
|
1550 |
} else if (!clazztype.tsym.isInterface() && |
|
1551 |
clazztype.getEnclosingType().tag == CLASS) { |
|
1552 |
// Check for the existence of an apropos outer instance |
|
1553 |
rs.resolveImplicitThis(tree.pos(), env, clazztype); |
|
1554 |
} |
|
1555 |
||
1556 |
// Attribute constructor arguments. |
|
1557 |
List<Type> argtypes = attribArgs(tree.args, localEnv); |
|
1558 |
List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv); |
|
1559 |
||
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1560 |
if (TreeInfo.isDiamond(tree)) { |
6344
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1561 |
clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1562 |
clazz.type = clazztype; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1563 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1564 |
|
10 | 1565 |
// If we have made no mistakes in the class type... |
1566 |
if (clazztype.tag == CLASS) { |
|
1567 |
// Enums may not be instantiated except implicitly |
|
1568 |
if (allowEnums && |
|
1569 |
(clazztype.tsym.flags_field&Flags.ENUM) != 0 && |
|
1570 |
(env.tree.getTag() != JCTree.VARDEF || |
|
1571 |
(((JCVariableDecl) env.tree).mods.flags&Flags.ENUM) == 0 || |
|
1572 |
((JCVariableDecl) env.tree).init != tree)) |
|
1573 |
log.error(tree.pos(), "enum.cant.be.instantiated"); |
|
1574 |
// Check that class is not abstract |
|
1575 |
if (cdef == null && |
|
1576 |
(clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) { |
|
1577 |
log.error(tree.pos(), "abstract.cant.be.instantiated", |
|
1578 |
clazztype.tsym); |
|
1579 |
} else if (cdef != null && clazztype.tsym.isInterface()) { |
|
1580 |
// Check that no constructor arguments are given to |
|
1581 |
// anonymous classes implementing an interface |
|
1582 |
if (!argtypes.isEmpty()) |
|
1583 |
log.error(tree.args.head.pos(), "anon.class.impl.intf.no.args"); |
|
1584 |
||
1585 |
if (!typeargtypes.isEmpty()) |
|
1586 |
log.error(tree.typeargs.head.pos(), "anon.class.impl.intf.no.typeargs"); |
|
1587 |
||
1588 |
// Error recovery: pretend no arguments were supplied. |
|
1589 |
argtypes = List.nil(); |
|
1590 |
typeargtypes = List.nil(); |
|
1591 |
} |
|
1592 |
||
1593 |
// Resolve the called constructor under the assumption |
|
1594 |
// that we are referring to a superclass instance of the |
|
1595 |
// current instance (JLS ???). |
|
1596 |
else { |
|
1597 |
localEnv.info.selectSuper = cdef != null; |
|
1598 |
localEnv.info.varArgs = false; |
|
1599 |
tree.constructor = rs.resolveConstructor( |
|
1600 |
tree.pos(), localEnv, clazztype, argtypes, typeargtypes); |
|
6343
655c0db5643b
6857948: Calling a constructor with a doubly bogus argument causes an internal error
mcimadamore
parents:
6342
diff
changeset
|
1601 |
tree.constructorType = tree.constructor.type.isErroneous() ? |
655c0db5643b
6857948: Calling a constructor with a doubly bogus argument causes an internal error
mcimadamore
parents:
6342
diff
changeset
|
1602 |
syms.errType : |
655c0db5643b
6857948: Calling a constructor with a doubly bogus argument causes an internal error
mcimadamore
parents:
6342
diff
changeset
|
1603 |
checkMethod(clazztype, |
655c0db5643b
6857948: Calling a constructor with a doubly bogus argument causes an internal error
mcimadamore
parents:
6342
diff
changeset
|
1604 |
tree.constructor, |
655c0db5643b
6857948: Calling a constructor with a doubly bogus argument causes an internal error
mcimadamore
parents:
6342
diff
changeset
|
1605 |
localEnv, |
655c0db5643b
6857948: Calling a constructor with a doubly bogus argument causes an internal error
mcimadamore
parents:
6342
diff
changeset
|
1606 |
tree.args, |
655c0db5643b
6857948: Calling a constructor with a doubly bogus argument causes an internal error
mcimadamore
parents:
6342
diff
changeset
|
1607 |
argtypes, |
655c0db5643b
6857948: Calling a constructor with a doubly bogus argument causes an internal error
mcimadamore
parents:
6342
diff
changeset
|
1608 |
typeargtypes, |
655c0db5643b
6857948: Calling a constructor with a doubly bogus argument causes an internal error
mcimadamore
parents:
6342
diff
changeset
|
1609 |
localEnv.info.varArgs); |
10 | 1610 |
if (localEnv.info.varArgs) |
1791
d378f023c36d
6723444: javac fails to substitute type variables into a constructor's throws clause
mcimadamore
parents:
1538
diff
changeset
|
1611 |
assert tree.constructorType.isErroneous() || tree.varargsElement != null; |
10 | 1612 |
} |
1613 |
||
1614 |
if (cdef != null) { |
|
1615 |
// We are seeing an anonymous class instance creation. |
|
1616 |
// In this case, the class instance creation |
|
1617 |
// expression |
|
1618 |
// |
|
1619 |
// E.new <typeargs1>C<typargs2>(args) { ... } |
|
1620 |
// |
|
1621 |
// is represented internally as |
|
1622 |
// |
|
1623 |
// E . new <typeargs1>C<typargs2>(args) ( class <empty-name> { ... } ) . |
|
1624 |
// |
|
1625 |
// This expression is then *transformed* as follows: |
|
1626 |
// |
|
1627 |
// (1) add a STATIC flag to the class definition |
|
1628 |
// if the current environment is static |
|
1629 |
// (2) add an extends or implements clause |
|
1630 |
// (3) add a constructor. |
|
1631 |
// |
|
1632 |
// For instance, if C is a class, and ET is the type of E, |
|
1633 |
// the expression |
|
1634 |
// |
|
1635 |
// E.new <typeargs1>C<typargs2>(args) { ... } |
|
1636 |
// |
|
1637 |
// is translated to (where X is a fresh name and typarams is the |
|
1638 |
// parameter list of the super constructor): |
|
1639 |
// |
|
1640 |
// new <typeargs1>X(<*nullchk*>E, args) where |
|
1641 |
// X extends C<typargs2> { |
|
1642 |
// <typarams> X(ET e, args) { |
|
1643 |
// e.<typeargs1>super(args) |
|
1644 |
// } |
|
1645 |
// ... |
|
1646 |
// } |
|
1647 |
if (Resolve.isStatic(env)) cdef.mods.flags |= STATIC; |
|
5320
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
1648 |
|
10 | 1649 |
if (clazztype.tsym.isInterface()) { |
1650 |
cdef.implementing = List.of(clazz); |
|
1651 |
} else { |
|
1652 |
cdef.extending = clazz; |
|
1653 |
} |
|
1654 |
||
1655 |
attribStat(cdef, localEnv); |
|
1656 |
||
1657 |
// If an outer instance is given, |
|
1658 |
// prefix it to the constructor arguments |
|
1659 |
// and delete it from the new expression |
|
1660 |
if (tree.encl != null && !clazztype.tsym.isInterface()) { |
|
1661 |
tree.args = tree.args.prepend(makeNullCheck(tree.encl)); |
|
1662 |
argtypes = argtypes.prepend(tree.encl.type); |
|
1663 |
tree.encl = null; |
|
1664 |
} |
|
1665 |
||
1666 |
// Reassign clazztype and recompute constructor. |
|
1667 |
clazztype = cdef.sym.type; |
|
1668 |
Symbol sym = rs.resolveConstructor( |
|
1669 |
tree.pos(), localEnv, clazztype, argtypes, |
|
1670 |
typeargtypes, true, tree.varargsElement != null); |
|
1671 |
assert sym.kind < AMBIGUOUS || tree.constructor.type.isErroneous(); |
|
1672 |
tree.constructor = sym; |
|
3556
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1673 |
if (tree.constructor.kind > ERRONEOUS) { |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1674 |
tree.constructorType = syms.errType; |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1675 |
} |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1676 |
else { |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1677 |
tree.constructorType = checkMethod(clazztype, |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1678 |
tree.constructor, |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1679 |
localEnv, |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1680 |
tree.args, |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1681 |
argtypes, |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1682 |
typeargtypes, |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1683 |
localEnv.info.varArgs); |
08a404c6ff0f
6840059: regression: javac silently crashes when resolving erroneous anonymous inner constructor
mcimadamore
parents:
3149
diff
changeset
|
1684 |
} |
10 | 1685 |
} |
1686 |
||
1687 |
if (tree.constructor != null && tree.constructor.kind == MTH) |
|
1688 |
owntype = clazztype; |
|
1689 |
} |
|
1690 |
result = check(tree, owntype, VAL, pkind, pt); |
|
1358 | 1691 |
chk.validate(tree.typeargs, localEnv); |
10 | 1692 |
} |
1693 |
||
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1694 |
Type attribDiamond(Env<AttrContext> env, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1695 |
JCNewClass tree, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1696 |
Type clazztype, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1697 |
Pair<Scope, Scope> mapping, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1698 |
List<Type> argtypes, |
6344
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1699 |
List<Type> typeargtypes) { |
5649
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1700 |
if (clazztype.isErroneous() || mapping == erroneousMapping) { |
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1701 |
//if the type of the instance creation expression is erroneous, |
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1702 |
//or something prevented us to form a valid mapping, return the |
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1703 |
//(possibly erroneous) type unchanged |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1704 |
return clazztype; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1705 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1706 |
else if (clazztype.isInterface()) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1707 |
//if the type of the instance creation expression is an interface |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1708 |
//skip the method resolution step (JLS 15.12.2.7). The type to be |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1709 |
//inferred is of the kind <X1,X2, ... Xn>C<X1,X2, ... Xn> |
6154
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1710 |
clazztype = new ForAll(clazztype.tsym.type.allparams(), clazztype.tsym.type) { |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1711 |
@Override |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1712 |
public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) { |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1713 |
switch (ck) { |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1714 |
case EXTENDS: return types.getBounds(tv); |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1715 |
default: return List.nil(); |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1716 |
} |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1717 |
} |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1718 |
@Override |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1719 |
public Type inst(List<Type> inferred, Types types) throws Infer.NoInstanceException { |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1720 |
// check that inferred bounds conform to their bounds |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1721 |
infer.checkWithinBounds(tvars, |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1722 |
types.subst(tvars, tvars, inferred), Warner.noWarnings); |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1723 |
return super.inst(inferred, types); |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1724 |
} |
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
6151
diff
changeset
|
1725 |
}; |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1726 |
} else { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1727 |
//if the type of the instance creation expression is a class type |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1728 |
//apply method resolution inference (JLS 15.12.2.7). The return type |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1729 |
//of the resolved constructor will be a partially instantiated type |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1730 |
((ClassSymbol) clazztype.tsym).members_field = mapping.snd; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1731 |
Symbol constructor; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1732 |
try { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1733 |
constructor = rs.resolveDiamond(tree.pos(), |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1734 |
env, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1735 |
clazztype.tsym.type, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1736 |
argtypes, |
6344
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1737 |
typeargtypes); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1738 |
} finally { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1739 |
((ClassSymbol) clazztype.tsym).members_field = mapping.fst; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1740 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1741 |
if (constructor.kind == MTH) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1742 |
ClassType ct = new ClassType(clazztype.getEnclosingType(), |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1743 |
clazztype.tsym.type.getTypeArguments(), |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1744 |
clazztype.tsym); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1745 |
clazztype = checkMethod(ct, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1746 |
constructor, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1747 |
env, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1748 |
tree.args, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1749 |
argtypes, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1750 |
typeargtypes, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1751 |
env.info.varArgs).getReturnType(); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1752 |
} else { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1753 |
clazztype = syms.errType; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1754 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1755 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1756 |
if (clazztype.tag == FORALL && !pt.isErroneous()) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1757 |
//if the resolved constructor's return type has some uninferred |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1758 |
//type-variables, infer them using the expected type and declared |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1759 |
//bounds (JLS 15.12.2.8). |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1760 |
try { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1761 |
clazztype = infer.instantiateExpr((ForAll) clazztype, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1762 |
pt.tag == NONE ? syms.objectType : pt, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1763 |
Warner.noWarnings); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1764 |
} catch (Infer.InferenceException ex) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1765 |
//an error occurred while inferring uninstantiated type-variables |
6344
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1766 |
log.error(tree.clazz.pos(), |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1767 |
"cant.apply.diamond.1", |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1768 |
diags.fragment("diamond", clazztype.tsym), |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1769 |
ex.diagnostic); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1770 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1771 |
} |
6344
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1772 |
clazztype = chk.checkClassType(tree.clazz.pos(), |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1773 |
clazztype, |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1774 |
true); |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1775 |
if (clazztype.tag == CLASS) { |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1776 |
List<Type> invalidDiamondArgs = chk.checkDiamond((ClassType)clazztype); |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1777 |
if (!clazztype.isErroneous() && invalidDiamondArgs.nonEmpty()) { |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1778 |
//one or more types inferred in the previous steps is either a |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1779 |
//captured type or an intersection type --- we need to report an error. |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1780 |
String subkey = invalidDiamondArgs.size() > 1 ? |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1781 |
"diamond.invalid.args" : |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1782 |
"diamond.invalid.arg"; |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1783 |
//The error message is of the kind: |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1784 |
// |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1785 |
//cannot infer type arguments for {clazztype}<>; |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1786 |
//reason: {subkey} |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1787 |
// |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1788 |
//where subkey is a fragment of the kind: |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1789 |
// |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1790 |
//type argument(s) {invalidDiamondArgs} inferred for {clazztype}<> is not allowed in this context |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1791 |
log.error(tree.clazz.pos(), |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1792 |
"cant.apply.diamond.1", |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1793 |
diags.fragment("diamond", clazztype.tsym), |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1794 |
diags.fragment(subkey, |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1795 |
invalidDiamondArgs, |
f190fb7fdd2d
6975275: diamond implementation needs some cleanup
mcimadamore
parents:
6343
diff
changeset
|
1796 |
diags.fragment("diamond", clazztype.tsym))); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1797 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1798 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1799 |
return clazztype; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1800 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1801 |
|
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1802 |
/** Creates a synthetic scope containing fake generic constructors. |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1803 |
* Assuming that the original scope contains a constructor of the kind: |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1804 |
* Foo(X x, Y y), where X,Y are class type-variables declared in Foo, |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1805 |
* the synthetic scope is added a generic constructor of the kind: |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1806 |
* <X,Y>Foo<X,Y>(X x, Y y). This is crucial in order to enable diamond |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1807 |
* inference. The inferred return type of the synthetic constructor IS |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1808 |
* the inferred type for the diamond operator. |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1809 |
*/ |
5649
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1810 |
private Pair<Scope, Scope> getSyntheticScopeMapping(Type ctype) { |
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1811 |
if (ctype.tag != CLASS) { |
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1812 |
return erroneousMapping; |
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1813 |
} |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1814 |
Pair<Scope, Scope> mapping = |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1815 |
new Pair<Scope, Scope>(ctype.tsym.members(), new Scope(ctype.tsym)); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1816 |
List<Type> typevars = ctype.tsym.type.getTypeArguments(); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1817 |
for (Scope.Entry e = mapping.fst.lookup(names.init); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1818 |
e.scope != null; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1819 |
e = e.next()) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1820 |
MethodSymbol newConstr = (MethodSymbol) e.sym.clone(ctype.tsym); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1821 |
newConstr.name = names.init; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1822 |
List<Type> oldTypeargs = List.nil(); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1823 |
if (newConstr.type.tag == FORALL) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1824 |
oldTypeargs = ((ForAll) newConstr.type).tvars; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1825 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1826 |
newConstr.type = new MethodType(newConstr.type.getParameterTypes(), |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1827 |
new ClassType(ctype.getEnclosingType(), ctype.tsym.type.getTypeArguments(), ctype.tsym), |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1828 |
newConstr.type.getThrownTypes(), |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1829 |
syms.methodClass); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1830 |
newConstr.type = new ForAll(typevars.prependList(oldTypeargs), newConstr.type); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1831 |
mapping.snd.enter(newConstr); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1832 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1833 |
return mapping; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1834 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1835 |
|
5649
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1836 |
private final Pair<Scope,Scope> erroneousMapping = new Pair<Scope,Scope>(null, null); |
c9e7ce44b173
6946618: sqe test fails: javac/generics/NewOnTypeParm in pit jdk7 b91 in all platforms.
mcimadamore
parents:
5492
diff
changeset
|
1837 |
|
10 | 1838 |
/** Make an attributed null check tree. |
1839 |
*/ |
|
1840 |
public JCExpression makeNullCheck(JCExpression arg) { |
|
1841 |
// optimization: X.this is never null; skip null check |
|
1842 |
Name name = TreeInfo.name(arg); |
|
1843 |
if (name == names._this || name == names._super) return arg; |
|
1844 |
||
1845 |
int optag = JCTree.NULLCHK; |
|
1846 |
JCUnary tree = make.at(arg.pos).Unary(optag, arg); |
|
1847 |
tree.operator = syms.nullcheck; |
|
1848 |
tree.type = arg.type; |
|
1849 |
return tree; |
|
1850 |
} |
|
1851 |
||
1852 |
public void visitNewArray(JCNewArray tree) { |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
1853 |
Type owntype = types.createErrorType(tree.type); |
10 | 1854 |
Type elemtype; |
1855 |
if (tree.elemtype != null) { |
|
1856 |
elemtype = attribType(tree.elemtype, env); |
|
1358 | 1857 |
chk.validate(tree.elemtype, env); |
10 | 1858 |
owntype = elemtype; |
1859 |
for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) { |
|
1860 |
attribExpr(l.head, env, syms.intType); |
|
1861 |
owntype = new ArrayType(owntype, syms.arrayClass); |
|
1862 |
} |
|
1863 |
} else { |
|
1864 |
// we are seeing an untyped aggregate { ... } |
|
1865 |
// this is allowed only if the prototype is an array |
|
1866 |
if (pt.tag == ARRAY) { |
|
1867 |
elemtype = types.elemtype(pt); |
|
1868 |
} else { |
|
1869 |
if (pt.tag != ERROR) { |
|
1870 |
log.error(tree.pos(), "illegal.initializer.for.type", |
|
1871 |
pt); |
|
1872 |
} |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
1873 |
elemtype = types.createErrorType(pt); |
10 | 1874 |
} |
1875 |
} |
|
1876 |
if (tree.elems != null) { |
|
1877 |
attribExprs(tree.elems, env, elemtype); |
|
1878 |
owntype = new ArrayType(elemtype, syms.arrayClass); |
|
1879 |
} |
|
1880 |
if (!types.isReifiable(elemtype)) |
|
1881 |
log.error(tree.pos(), "generic.array.creation"); |
|
1882 |
result = check(tree, owntype, VAL, pkind, pt); |
|
1883 |
} |
|
1884 |
||
1885 |
public void visitParens(JCParens tree) { |
|
1886 |
Type owntype = attribTree(tree.expr, env, pkind, pt); |
|
1887 |
result = check(tree, owntype, pkind, pkind, pt); |
|
1888 |
Symbol sym = TreeInfo.symbol(tree); |
|
1889 |
if (sym != null && (sym.kind&(TYP|PCK)) != 0) |
|
1890 |
log.error(tree.pos(), "illegal.start.of.type"); |
|
1891 |
} |
|
1892 |
||
1893 |
public void visitAssign(JCAssign tree) { |
|
1894 |
Type owntype = attribTree(tree.lhs, env.dup(tree), VAR, Type.noType); |
|
1895 |
Type capturedType = capture(owntype); |
|
1896 |
attribExpr(tree.rhs, env, owntype); |
|
1897 |
result = check(tree, capturedType, VAL, pkind, pt); |
|
1898 |
} |
|
1899 |
||
1900 |
public void visitAssignop(JCAssignOp tree) { |
|
1901 |
// Attribute arguments. |
|
1902 |
Type owntype = attribTree(tree.lhs, env, VAR, Type.noType); |
|
1903 |
Type operand = attribExpr(tree.rhs, env); |
|
1904 |
// Find operator. |
|
1905 |
Symbol operator = tree.operator = rs.resolveBinaryOperator( |
|
1906 |
tree.pos(), tree.getTag() - JCTree.ASGOffset, env, |
|
1907 |
owntype, operand); |
|
1908 |
||
1909 |
if (operator.kind == MTH) { |
|
1910 |
chk.checkOperator(tree.pos(), |
|
1911 |
(OperatorSymbol)operator, |
|
1912 |
tree.getTag() - JCTree.ASGOffset, |
|
1913 |
owntype, |
|
1914 |
operand); |
|
166 | 1915 |
chk.checkDivZero(tree.rhs.pos(), operator, operand); |
1916 |
chk.checkCastable(tree.rhs.pos(), |
|
1917 |
operator.type.getReturnType(), |
|
1918 |
owntype); |
|
10 | 1919 |
} |
1920 |
result = check(tree, owntype, VAL, pkind, pt); |
|
1921 |
} |
|
1922 |
||
1923 |
public void visitUnary(JCUnary tree) { |
|
1924 |
// Attribute arguments. |
|
1925 |
Type argtype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC) |
|
1926 |
? attribTree(tree.arg, env, VAR, Type.noType) |
|
1927 |
: chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env)); |
|
1928 |
||
1929 |
// Find operator. |
|
1930 |
Symbol operator = tree.operator = |
|
1931 |
rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype); |
|
1932 |
||
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
1933 |
Type owntype = types.createErrorType(tree.type); |
10 | 1934 |
if (operator.kind == MTH) { |
1935 |
owntype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC) |
|
1936 |
? tree.arg.type |
|
1937 |
: operator.type.getReturnType(); |
|
1938 |
int opc = ((OperatorSymbol)operator).opcode; |
|
1939 |
||
1940 |
// If the argument is constant, fold it. |
|
1941 |
if (argtype.constValue() != null) { |
|
1942 |
Type ctype = cfolder.fold1(opc, argtype); |
|
1943 |
if (ctype != null) { |
|
1944 |
owntype = cfolder.coerce(ctype, owntype); |
|
1945 |
||
1946 |
// Remove constant types from arguments to |
|
1947 |
// conserve space. The parser will fold concatenations |
|
1948 |
// of string literals; the code here also |
|
1949 |
// gets rid of intermediate results when some of the |
|
1950 |
// operands are constant identifiers. |
|
1951 |
if (tree.arg.type.tsym == syms.stringType.tsym) { |
|
1952 |
tree.arg.type = syms.stringType; |
|
1953 |
} |
|
1954 |
} |
|
1955 |
} |
|
1956 |
} |
|
1957 |
result = check(tree, owntype, VAL, pkind, pt); |
|
1958 |
} |
|
1959 |
||
1960 |
public void visitBinary(JCBinary tree) { |
|
1961 |
// Attribute arguments. |
|
1962 |
Type left = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.lhs, env)); |
|
1963 |
Type right = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.rhs, env)); |
|
1964 |
||
1965 |
// Find operator. |
|
1966 |
Symbol operator = tree.operator = |
|
1967 |
rs.resolveBinaryOperator(tree.pos(), tree.getTag(), env, left, right); |
|
1968 |
||
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
1969 |
Type owntype = types.createErrorType(tree.type); |
10 | 1970 |
if (operator.kind == MTH) { |
1971 |
owntype = operator.type.getReturnType(); |
|
1972 |
int opc = chk.checkOperator(tree.lhs.pos(), |
|
1973 |
(OperatorSymbol)operator, |
|
1974 |
tree.getTag(), |
|
1975 |
left, |
|
1976 |
right); |
|
1977 |
||
1978 |
// If both arguments are constants, fold them. |
|
1979 |
if (left.constValue() != null && right.constValue() != null) { |
|
1980 |
Type ctype = cfolder.fold2(opc, left, right); |
|
1981 |
if (ctype != null) { |
|
1982 |
owntype = cfolder.coerce(ctype, owntype); |
|
1983 |
||
1984 |
// Remove constant types from arguments to |
|
1985 |
// conserve space. The parser will fold concatenations |
|
1986 |
// of string literals; the code here also |
|
1987 |
// gets rid of intermediate results when some of the |
|
1988 |
// operands are constant identifiers. |
|
1989 |
if (tree.lhs.type.tsym == syms.stringType.tsym) { |
|
1990 |
tree.lhs.type = syms.stringType; |
|
1991 |
} |
|
1992 |
if (tree.rhs.type.tsym == syms.stringType.tsym) { |
|
1993 |
tree.rhs.type = syms.stringType; |
|
1994 |
} |
|
1995 |
} |
|
1996 |
} |
|
1997 |
||
1998 |
// Check that argument types of a reference ==, != are |
|
1999 |
// castable to each other, (JLS???). |
|
2000 |
if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) { |
|
2001 |
if (!types.isCastable(left, right, new Warner(tree.pos()))) { |
|
2002 |
log.error(tree.pos(), "incomparable.types", left, right); |
|
2003 |
} |
|
2004 |
} |
|
2005 |
||
2006 |
chk.checkDivZero(tree.rhs.pos(), operator, right); |
|
2007 |
} |
|
2008 |
result = check(tree, owntype, VAL, pkind, pt); |
|
2009 |
} |
|
2010 |
||
2011 |
public void visitTypeCast(JCTypeCast tree) { |
|
2012 |
Type clazztype = attribType(tree.clazz, env); |
|
6351 | 2013 |
chk.validate(tree.clazz, env, false); |
10 | 2014 |
Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly); |
2015 |
Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype); |
|
2016 |
if (exprtype.constValue() != null) |
|
2017 |
owntype = cfolder.coerce(exprtype, owntype); |
|
2018 |
result = check(tree, capture(owntype), VAL, pkind, pt); |
|
2019 |
} |
|
2020 |
||
2021 |
public void visitTypeTest(JCInstanceOf tree) { |
|
2022 |
Type exprtype = chk.checkNullOrRefType( |
|
2023 |
tree.expr.pos(), attribExpr(tree.expr, env)); |
|
2024 |
Type clazztype = chk.checkReifiableReferenceType( |
|
2025 |
tree.clazz.pos(), attribType(tree.clazz, env)); |
|
6351 | 2026 |
chk.validate(tree.clazz, env, false); |
10 | 2027 |
chk.checkCastable(tree.expr.pos(), exprtype, clazztype); |
2028 |
result = check(tree, syms.booleanType, VAL, pkind, pt); |
|
2029 |
} |
|
2030 |
||
2031 |
public void visitIndexed(JCArrayAccess tree) { |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
2032 |
Type owntype = types.createErrorType(tree.type); |
10 | 2033 |
Type atype = attribExpr(tree.indexed, env); |
2034 |
attribExpr(tree.index, env, syms.intType); |
|
2035 |
if (types.isArray(atype)) |
|
2036 |
owntype = types.elemtype(atype); |
|
2037 |
else if (atype.tag != ERROR) |
|
2038 |
log.error(tree.pos(), "array.req.but.found", atype); |
|
2039 |
if ((pkind & VAR) == 0) owntype = capture(owntype); |
|
2040 |
result = check(tree, owntype, VAR, pkind, pt); |
|
2041 |
} |
|
2042 |
||
2043 |
public void visitIdent(JCIdent tree) { |
|
2044 |
Symbol sym; |
|
2045 |
boolean varArgs = false; |
|
2046 |
||
2047 |
// Find symbol |
|
2048 |
if (pt.tag == METHOD || pt.tag == FORALL) { |
|
2049 |
// If we are looking for a method, the prototype `pt' will be a |
|
2050 |
// method type with the type of the call's arguments as parameters. |
|
2051 |
env.info.varArgs = false; |
|
2052 |
sym = rs.resolveMethod(tree.pos(), env, tree.name, pt.getParameterTypes(), pt.getTypeArguments()); |
|
2053 |
varArgs = env.info.varArgs; |
|
2054 |
} else if (tree.sym != null && tree.sym.kind != VAR) { |
|
2055 |
sym = tree.sym; |
|
2056 |
} else { |
|
2057 |
sym = rs.resolveIdent(tree.pos(), env, tree.name, pkind); |
|
2058 |
} |
|
2059 |
tree.sym = sym; |
|
2060 |
||
2061 |
// (1) Also find the environment current for the class where |
|
2062 |
// sym is defined (`symEnv'). |
|
2063 |
// Only for pre-tiger versions (1.4 and earlier): |
|
2064 |
// (2) Also determine whether we access symbol out of an anonymous |
|
2065 |
// class in a this or super call. This is illegal for instance |
|
2066 |
// members since such classes don't carry a this$n link. |
|
2067 |
// (`noOuterThisPath'). |
|
2068 |
Env<AttrContext> symEnv = env; |
|
2069 |
boolean noOuterThisPath = false; |
|
2070 |
if (env.enclClass.sym.owner.kind != PCK && // we are in an inner class |
|
2071 |
(sym.kind & (VAR | MTH | TYP)) != 0 && |
|
2072 |
sym.owner.kind == TYP && |
|
2073 |
tree.name != names._this && tree.name != names._super) { |
|
2074 |
||
2075 |
// Find environment in which identifier is defined. |
|
2076 |
while (symEnv.outer != null && |
|
2077 |
!sym.isMemberOf(symEnv.enclClass.sym, types)) { |
|
2078 |
if ((symEnv.enclClass.sym.flags() & NOOUTERTHIS) != 0) |
|
2079 |
noOuterThisPath = !allowAnonOuterThis; |
|
2080 |
symEnv = symEnv.outer; |
|
2081 |
} |
|
2082 |
} |
|
2083 |
||
2084 |
// If symbol is a variable, ... |
|
2085 |
if (sym.kind == VAR) { |
|
2086 |
VarSymbol v = (VarSymbol)sym; |
|
2087 |
||
2088 |
// ..., evaluate its initializer, if it has one, and check for |
|
2089 |
// illegal forward reference. |
|
2090 |
checkInit(tree, env, v, false); |
|
2091 |
||
2092 |
// If symbol is a local variable accessed from an embedded |
|
2093 |
// inner class check that it is final. |
|
2094 |
if (v.owner.kind == MTH && |
|
2095 |
v.owner != env.info.scope.owner && |
|
2096 |
(v.flags_field & FINAL) == 0) { |
|
2097 |
log.error(tree.pos(), |
|
2098 |
"local.var.accessed.from.icls.needs.final", |
|
2099 |
v); |
|
2100 |
} |
|
2101 |
||
2102 |
// If we are expecting a variable (as opposed to a value), check |
|
2103 |
// that the variable is assignable in the current environment. |
|
2104 |
if (pkind == VAR) |
|
2105 |
checkAssignable(tree.pos(), v, null, env); |
|
2106 |
} |
|
2107 |
||
2108 |
// In a constructor body, |
|
2109 |
// if symbol is a field or instance method, check that it is |
|
2110 |
// not accessed before the supertype constructor is called. |
|
2111 |
if ((symEnv.info.isSelfCall || noOuterThisPath) && |
|
2112 |
(sym.kind & (VAR | MTH)) != 0 && |
|
2113 |
sym.owner.kind == TYP && |
|
2114 |
(sym.flags() & STATIC) == 0) { |
|
2115 |
chk.earlyRefError(tree.pos(), sym.kind == VAR ? sym : thisSym(tree.pos(), env)); |
|
2116 |
} |
|
2117 |
Env<AttrContext> env1 = env; |
|
512
53e498fa5c0e
6657499: javac 1.6.0 fails to compile class with inner class
mcimadamore
parents:
511
diff
changeset
|
2118 |
if (sym.kind != ERR && sym.kind != TYP && sym.owner != null && sym.owner != env1.enclClass.sym) { |
10 | 2119 |
// If the found symbol is inaccessible, then it is |
2120 |
// accessed through an enclosing instance. Locate this |
|
2121 |
// enclosing instance: |
|
2122 |
while (env1.outer != null && !rs.isAccessible(env, env1.enclClass.sym.type, sym)) |
|
2123 |
env1 = env1.outer; |
|
2124 |
} |
|
2125 |
result = checkId(tree, env1.enclClass.sym.type, sym, env, pkind, pt, varArgs); |
|
2126 |
} |
|
2127 |
||
2128 |
public void visitSelect(JCFieldAccess tree) { |
|
2129 |
// Determine the expected kind of the qualifier expression. |
|
2130 |
int skind = 0; |
|
2131 |
if (tree.name == names._this || tree.name == names._super || |
|
2132 |
tree.name == names._class) |
|
2133 |
{ |
|
2134 |
skind = TYP; |
|
2135 |
} else { |
|
2136 |
if ((pkind & PCK) != 0) skind = skind | PCK; |
|
2137 |
if ((pkind & TYP) != 0) skind = skind | TYP | PCK; |
|
2138 |
if ((pkind & (VAL | MTH)) != 0) skind = skind | VAL | TYP; |
|
2139 |
} |
|
2140 |
||
2141 |
// Attribute the qualifier expression, and determine its symbol (if any). |
|
2142 |
Type site = attribTree(tree.selected, env, skind, Infer.anyPoly); |
|
2143 |
if ((pkind & (PCK | TYP)) == 0) |
|
2144 |
site = capture(site); // Capture field access |
|
2145 |
||
2146 |
// don't allow T.class T[].class, etc |
|
2147 |
if (skind == TYP) { |
|
2148 |
Type elt = site; |
|
2149 |
while (elt.tag == ARRAY) |
|
2150 |
elt = ((ArrayType)elt).elemtype; |
|
2151 |
if (elt.tag == TYPEVAR) { |
|
2152 |
log.error(tree.pos(), "type.var.cant.be.deref"); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
2153 |
result = types.createErrorType(tree.type); |
10 | 2154 |
return; |
2155 |
} |
|
2156 |
} |
|
2157 |
||
2158 |
// If qualifier symbol is a type or `super', assert `selectSuper' |
|
2159 |
// for the selection. This is relevant for determining whether |
|
2160 |
// protected symbols are accessible. |
|
2161 |
Symbol sitesym = TreeInfo.symbol(tree.selected); |
|
2162 |
boolean selectSuperPrev = env.info.selectSuper; |
|
2163 |
env.info.selectSuper = |
|
2164 |
sitesym != null && |
|
2165 |
sitesym.name == names._super; |
|
2166 |
||
2167 |
// If selected expression is polymorphic, strip |
|
2168 |
// type parameters and remember in env.info.tvars, so that |
|
2169 |
// they can be added later (in Attr.checkId and Infer.instantiateMethod). |
|
2170 |
if (tree.selected.type.tag == FORALL) { |
|
2171 |
ForAll pstype = (ForAll)tree.selected.type; |
|
2172 |
env.info.tvars = pstype.tvars; |
|
2173 |
site = tree.selected.type = pstype.qtype; |
|
2174 |
} |
|
2175 |
||
2176 |
// Determine the symbol represented by the selection. |
|
2177 |
env.info.varArgs = false; |
|
2178 |
Symbol sym = selectSym(tree, site, env, pt, pkind); |
|
2179 |
if (sym.exists() && !isType(sym) && (pkind & (PCK | TYP)) != 0) { |
|
2180 |
site = capture(site); |
|
2181 |
sym = selectSym(tree, site, env, pt, pkind); |
|
2182 |
} |
|
2183 |
boolean varArgs = env.info.varArgs; |
|
2184 |
tree.sym = sym; |
|
2185 |
||
511
b3b5eadd2bca
6450290: Capture of nested wildcards causes type error
mcimadamore
parents:
325
diff
changeset
|
2186 |
if (site.tag == TYPEVAR && !isType(sym) && sym.kind != ERR) { |
b3b5eadd2bca
6450290: Capture of nested wildcards causes type error
mcimadamore
parents:
325
diff
changeset
|
2187 |
while (site.tag == TYPEVAR) site = site.getUpperBound(); |
b3b5eadd2bca
6450290: Capture of nested wildcards causes type error
mcimadamore
parents:
325
diff
changeset
|
2188 |
site = capture(site); |
b3b5eadd2bca
6450290: Capture of nested wildcards causes type error
mcimadamore
parents:
325
diff
changeset
|
2189 |
} |
10 | 2190 |
|
2191 |
// If that symbol is a variable, ... |
|
2192 |
if (sym.kind == VAR) { |
|
2193 |
VarSymbol v = (VarSymbol)sym; |
|
2194 |
||
2195 |
// ..., evaluate its initializer, if it has one, and check for |
|
2196 |
// illegal forward reference. |
|
2197 |
checkInit(tree, env, v, true); |
|
2198 |
||
2199 |
// If we are expecting a variable (as opposed to a value), check |
|
2200 |
// that the variable is assignable in the current environment. |
|
2201 |
if (pkind == VAR) |
|
2202 |
checkAssignable(tree.pos(), v, tree.selected, env); |
|
2203 |
} |
|
2204 |
||
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
2205 |
if (sitesym != null && |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
2206 |
sitesym.kind == VAR && |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
2207 |
((VarSymbol)sitesym).isResourceVariable() && |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
2208 |
sym.kind == MTH && |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
2209 |
sym.overrides(syms.autoCloseableClose, sitesym.type.tsym, types, true) && |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
2210 |
env.info.lint.isEnabled(Lint.LintCategory.ARM)) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
2211 |
log.warning(tree, "twr.explicit.close.call"); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
2212 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5857
diff
changeset
|
2213 |
|
10 | 2214 |
// Disallow selecting a type from an expression |
2215 |
if (isType(sym) && (sitesym==null || (sitesym.kind&(TYP|PCK)) == 0)) { |
|
2216 |
tree.type = check(tree.selected, pt, |
|
2217 |
sitesym == null ? VAL : sitesym.kind, TYP|PCK, pt); |
|
2218 |
} |
|
2219 |
||
2220 |
if (isType(sitesym)) { |
|
2221 |
if (sym.name == names._this) { |
|
2222 |
// If `C' is the currently compiled class, check that |
|
2223 |
// C.this' does not appear in a call to a super(...) |
|
2224 |
if (env.info.isSelfCall && |
|
2225 |
site.tsym == env.enclClass.sym) { |
|
2226 |
chk.earlyRefError(tree.pos(), sym); |
|
2227 |
} |
|
2228 |
} else { |
|
2229 |
// Check if type-qualified fields or methods are static (JLS) |
|
2230 |
if ((sym.flags() & STATIC) == 0 && |
|
2231 |
sym.name != names._super && |
|
2232 |
(sym.kind == VAR || sym.kind == MTH)) { |
|
2233 |
rs.access(rs.new StaticError(sym), |
|
2234 |
tree.pos(), site, sym.name, true); |
|
2235 |
} |
|
2236 |
} |
|
5002
12a9e8562200
4880220: Add a warning when accessing a static method via an reference
jjg
parents:
5001
diff
changeset
|
2237 |
} else if (sym.kind != ERR && (sym.flags() & STATIC) != 0 && sym.name != names._class) { |
12a9e8562200
4880220: Add a warning when accessing a static method via an reference
jjg
parents:
5001
diff
changeset
|
2238 |
// If the qualified item is not a type and the selected item is static, report |
12a9e8562200
4880220: Add a warning when accessing a static method via an reference
jjg
parents:
5001
diff
changeset
|
2239 |
// a warning. Make allowance for the class of an array type e.g. Object[].class) |
12a9e8562200
4880220: Add a warning when accessing a static method via an reference
jjg
parents:
5001
diff
changeset
|
2240 |
chk.warnStatic(tree, "static.not.qualified.by.type", Kinds.kindName(sym.kind), sym.owner); |
10 | 2241 |
} |
2242 |
||
2243 |
// If we are selecting an instance member via a `super', ... |
|
2244 |
if (env.info.selectSuper && (sym.flags() & STATIC) == 0) { |
|
2245 |
||
2246 |
// Check that super-qualified symbols are not abstract (JLS) |
|
2247 |
rs.checkNonAbstract(tree.pos(), sym); |
|
2248 |
||
2249 |
if (site.isRaw()) { |
|
2250 |
// Determine argument types for site. |
|
2251 |
Type site1 = types.asSuper(env.enclClass.sym.type, site.tsym); |
|
2252 |
if (site1 != null) site = site1; |
|
2253 |
} |
|
2254 |
} |
|
2255 |
||
2256 |
env.info.selectSuper = selectSuperPrev; |
|
2257 |
result = checkId(tree, site, sym, env, pkind, pt, varArgs); |
|
2258 |
env.info.tvars = List.nil(); |
|
2259 |
} |
|
2260 |
//where |
|
2261 |
/** Determine symbol referenced by a Select expression, |
|
2262 |
* |
|
2263 |
* @param tree The select tree. |
|
2264 |
* @param site The type of the selected expression, |
|
2265 |
* @param env The current environment. |
|
2266 |
* @param pt The current prototype. |
|
2267 |
* @param pkind The expected kind(s) of the Select expression. |
|
2268 |
*/ |
|
2269 |
private Symbol selectSym(JCFieldAccess tree, |
|
2270 |
Type site, |
|
2271 |
Env<AttrContext> env, |
|
2272 |
Type pt, |
|
2273 |
int pkind) { |
|
2274 |
DiagnosticPosition pos = tree.pos(); |
|
2275 |
Name name = tree.name; |
|
2276 |
||
2277 |
switch (site.tag) { |
|
2278 |
case PACKAGE: |
|
2279 |
return rs.access( |
|
2280 |
rs.findIdentInPackage(env, site.tsym, name, pkind), |
|
2281 |
pos, site, name, true); |
|
2282 |
case ARRAY: |
|
2283 |
case CLASS: |
|
2284 |
if (pt.tag == METHOD || pt.tag == FORALL) { |
|
2285 |
return rs.resolveQualifiedMethod( |
|
2286 |
pos, env, site, name, pt.getParameterTypes(), pt.getTypeArguments()); |
|
2287 |
} else if (name == names._this || name == names._super) { |
|
2288 |
return rs.resolveSelf(pos, env, site.tsym, name); |
|
2289 |
} else if (name == names._class) { |
|
2290 |
// In this case, we have already made sure in |
|
2291 |
// visitSelect that qualifier expression is a type. |
|
2292 |
Type t = syms.classType; |
|
2293 |
List<Type> typeargs = allowGenerics |
|
2294 |
? List.of(types.erasure(site)) |
|
2295 |
: List.<Type>nil(); |
|
2296 |
t = new ClassType(t.getEnclosingType(), typeargs, t.tsym); |
|
2297 |
return new VarSymbol( |
|
2298 |
STATIC | PUBLIC | FINAL, names._class, t, site.tsym); |
|
2299 |
} else { |
|
2300 |
// We are seeing a plain identifier as selector. |
|
2301 |
Symbol sym = rs.findIdentInType(env, site, name, pkind); |
|
2302 |
if ((pkind & ERRONEOUS) == 0) |
|
2303 |
sym = rs.access(sym, pos, site, name, true); |
|
2304 |
return sym; |
|
2305 |
} |
|
2306 |
case WILDCARD: |
|
2307 |
throw new AssertionError(tree); |
|
2308 |
case TYPEVAR: |
|
2309 |
// Normally, site.getUpperBound() shouldn't be null. |
|
2310 |
// It should only happen during memberEnter/attribBase |
|
2311 |
// when determining the super type which *must* be |
|
2312 |
// done before attributing the type variables. In |
|
2313 |
// other words, we are seeing this illegal program: |
|
2314 |
// class B<T> extends A<T.foo> {} |
|
2315 |
Symbol sym = (site.getUpperBound() != null) |
|
2316 |
? selectSym(tree, capture(site.getUpperBound()), env, pt, pkind) |
|
2317 |
: null; |
|
3559
58cfcc0f1aa9
6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents:
3556
diff
changeset
|
2318 |
if (sym == null) { |
10 | 2319 |
log.error(pos, "type.var.cant.be.deref"); |
2320 |
return syms.errSymbol; |
|
2321 |
} else { |
|
1528
441d4ec466de
6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents:
1358
diff
changeset
|
2322 |
Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ? |
441d4ec466de
6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents:
1358
diff
changeset
|
2323 |
rs.new AccessError(env, site, sym) : |
441d4ec466de
6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents:
1358
diff
changeset
|
2324 |
sym; |
441d4ec466de
6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents:
1358
diff
changeset
|
2325 |
rs.access(sym2, pos, site, name, true); |
10 | 2326 |
return sym; |
2327 |
} |
|
2328 |
case ERROR: |
|
2329 |
// preserve identifier names through errors |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
2330 |
return types.createErrorType(name, site.tsym, site).tsym; |
10 | 2331 |
default: |
2332 |
// The qualifier expression is of a primitive type -- only |
|
2333 |
// .class is allowed for these. |
|
2334 |
if (name == names._class) { |
|
2335 |
// In this case, we have already made sure in Select that |
|
2336 |
// qualifier expression is a type. |
|
2337 |
Type t = syms.classType; |
|
2338 |
Type arg = types.boxedClass(site).type; |
|
2339 |
t = new ClassType(t.getEnclosingType(), List.of(arg), t.tsym); |
|
2340 |
return new VarSymbol( |
|
2341 |
STATIC | PUBLIC | FINAL, names._class, t, site.tsym); |
|
2342 |
} else { |
|
2343 |
log.error(pos, "cant.deref", site); |
|
2344 |
return syms.errSymbol; |
|
2345 |
} |
|
2346 |
} |
|
2347 |
} |
|
2348 |
||
2349 |
/** Determine type of identifier or select expression and check that |
|
2350 |
* (1) the referenced symbol is not deprecated |
|
2351 |
* (2) the symbol's type is safe (@see checkSafe) |
|
2352 |
* (3) if symbol is a variable, check that its type and kind are |
|
2353 |
* compatible with the prototype and protokind. |
|
2354 |
* (4) if symbol is an instance field of a raw type, |
|
2355 |
* which is being assigned to, issue an unchecked warning if its |
|
2356 |
* type changes under erasure. |
|
2357 |
* (5) if symbol is an instance method of a raw type, issue an |
|
2358 |
* unchecked warning if its argument types change under erasure. |
|
2359 |
* If checks succeed: |
|
2360 |
* If symbol is a constant, return its constant type |
|
2361 |
* else if symbol is a method, return its result type |
|
2362 |
* otherwise return its type. |
|
2363 |
* Otherwise return errType. |
|
2364 |
* |
|
2365 |
* @param tree The syntax tree representing the identifier |
|
2366 |
* @param site If this is a select, the type of the selected |
|
2367 |
* expression, otherwise the type of the current class. |
|
2368 |
* @param sym The symbol representing the identifier. |
|
2369 |
* @param env The current environment. |
|
2370 |
* @param pkind The set of expected kinds. |
|
2371 |
* @param pt The expected type. |
|
2372 |
*/ |
|
2373 |
Type checkId(JCTree tree, |
|
2374 |
Type site, |
|
2375 |
Symbol sym, |
|
2376 |
Env<AttrContext> env, |
|
2377 |
int pkind, |
|
2378 |
Type pt, |
|
2379 |
boolean useVarargs) { |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
2380 |
if (pt.isErroneous()) return types.createErrorType(site); |
10 | 2381 |
Type owntype; // The computed type of this identifier occurrence. |
2382 |
switch (sym.kind) { |
|
2383 |
case TYP: |
|
2384 |
// For types, the computed type equals the symbol's type, |
|
2385 |
// except for two situations: |
|
2386 |
owntype = sym.type; |
|
2387 |
if (owntype.tag == CLASS) { |
|
2388 |
Type ownOuter = owntype.getEnclosingType(); |
|
2389 |
||
2390 |
// (a) If the symbol's type is parameterized, erase it |
|
2391 |
// because no type parameters were given. |
|
2392 |
// We recover generic outer type later in visitTypeApply. |
|
2393 |
if (owntype.tsym.type.getTypeArguments().nonEmpty()) { |
|
2394 |
owntype = types.erasure(owntype); |
|
2395 |
} |
|
2396 |
||
2397 |
// (b) If the symbol's type is an inner class, then |
|
2398 |
// we have to interpret its outer type as a superclass |
|
2399 |
// of the site type. Example: |
|
2400 |
// |
|
2401 |
// class Tree<A> { class Visitor { ... } } |
|
2402 |
// class PointTree extends Tree<Point> { ... } |
|
2403 |
// ...PointTree.Visitor... |
|
2404 |
// |
|
2405 |
// Then the type of the last expression above is |
|
2406 |
// Tree<Point>.Visitor. |
|
2407 |
else if (ownOuter.tag == CLASS && site != ownOuter) { |
|
2408 |
Type normOuter = site; |
|
2409 |
if (normOuter.tag == CLASS) |
|
2410 |
normOuter = types.asEnclosingSuper(site, ownOuter.tsym); |
|
2411 |
if (normOuter == null) // perhaps from an import |
|
2412 |
normOuter = types.erasure(ownOuter); |
|
2413 |
if (normOuter != ownOuter) |
|
2414 |
owntype = new ClassType( |
|
2415 |
normOuter, List.<Type>nil(), owntype.tsym); |
|
2416 |
} |
|
2417 |
} |
|
2418 |
break; |
|
2419 |
case VAR: |
|
2420 |
VarSymbol v = (VarSymbol)sym; |
|
2421 |
// Test (4): if symbol is an instance field of a raw type, |
|
2422 |
// which is being assigned to, issue an unchecked warning if |
|
2423 |
// its type changes under erasure. |
|
2424 |
if (allowGenerics && |
|
2425 |
pkind == VAR && |
|
2426 |
v.owner.kind == TYP && |
|
2427 |
(v.flags() & STATIC) == 0 && |
|
2428 |
(site.tag == CLASS || site.tag == TYPEVAR)) { |
|
2429 |
Type s = types.asOuterSuper(site, v.owner); |
|
2430 |
if (s != null && |
|
2431 |
s.isRaw() && |
|
2432 |
!types.isSameType(v.type, v.erasure(types))) { |
|
2433 |
chk.warnUnchecked(tree.pos(), |
|
2434 |
"unchecked.assign.to.var", |
|
2435 |
v, s); |
|
2436 |
} |
|
2437 |
} |
|
2438 |
// The computed type of a variable is the type of the |
|
2439 |
// variable symbol, taken as a member of the site type. |
|
2440 |
owntype = (sym.owner.kind == TYP && |
|
2441 |
sym.name != names._this && sym.name != names._super) |
|
2442 |
? types.memberType(site, sym) |
|
2443 |
: sym.type; |
|
2444 |
||
2445 |
if (env.info.tvars.nonEmpty()) { |
|
2446 |
Type owntype1 = new ForAll(env.info.tvars, owntype); |
|
2447 |
for (List<Type> l = env.info.tvars; l.nonEmpty(); l = l.tail) |
|
2448 |
if (!owntype.contains(l.head)) { |
|
2449 |
log.error(tree.pos(), "undetermined.type", owntype1); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
2450 |
owntype1 = types.createErrorType(owntype1); |
10 | 2451 |
} |
2452 |
owntype = owntype1; |
|
2453 |
} |
|
2454 |
||
2455 |
// If the variable is a constant, record constant value in |
|
2456 |
// computed type. |
|
2457 |
if (v.getConstValue() != null && isStaticReference(tree)) |
|
2458 |
owntype = owntype.constType(v.getConstValue()); |
|
2459 |
||
2460 |
if (pkind == VAL) { |
|
2461 |
owntype = capture(owntype); // capture "names as expressions" |
|
2462 |
} |
|
2463 |
break; |
|
2464 |
case MTH: { |
|
2465 |
JCMethodInvocation app = (JCMethodInvocation)env.tree; |
|
2466 |
owntype = checkMethod(site, sym, env, app.args, |
|
2467 |
pt.getParameterTypes(), pt.getTypeArguments(), |
|
2468 |
env.info.varArgs); |
|
2469 |
break; |
|
2470 |
} |
|
2471 |
case PCK: case ERR: |
|
2472 |
owntype = sym.type; |
|
2473 |
break; |
|
2474 |
default: |
|
2475 |
throw new AssertionError("unexpected kind: " + sym.kind + |
|
2476 |
" in tree " + tree); |
|
2477 |
} |
|
2478 |
||
2479 |
// Test (1): emit a `deprecation' warning if symbol is deprecated. |
|
2480 |
// (for constructors, the error was given when the constructor was |
|
2481 |
// resolved) |
|
2482 |
if (sym.name != names.init && |
|
2483 |
(sym.flags() & DEPRECATED) != 0 && |
|
2484 |
(env.info.scope.owner.flags() & DEPRECATED) == 0 && |
|
2485 |
sym.outermostClass() != env.info.scope.owner.outermostClass()) |
|
2486 |
chk.warnDeprecated(tree.pos(), sym); |
|
2487 |
||
3661 | 2488 |
if ((sym.flags() & PROPRIETARY) != 0) { |
2489 |
if (enableSunApiLintControl) |
|
2490 |
chk.warnSunApi(tree.pos(), "sun.proprietary", sym); |
|
2491 |
else |
|
2492 |
log.strictWarning(tree.pos(), "sun.proprietary", sym); |
|
2493 |
} |
|
10 | 2494 |
|
2495 |
// Test (3): if symbol is a variable, check that its type and |
|
2496 |
// kind are compatible with the prototype and protokind. |
|
2497 |
return check(tree, owntype, sym.kind, pkind, pt); |
|
2498 |
} |
|
2499 |
||
2500 |
/** Check that variable is initialized and evaluate the variable's |
|
2501 |
* initializer, if not yet done. Also check that variable is not |
|
2502 |
* referenced before it is defined. |
|
2503 |
* @param tree The tree making up the variable reference. |
|
2504 |
* @param env The current environment. |
|
2505 |
* @param v The variable's symbol. |
|
2506 |
*/ |
|
2507 |
private void checkInit(JCTree tree, |
|
2508 |
Env<AttrContext> env, |
|
2509 |
VarSymbol v, |
|
2510 |
boolean onlyWarning) { |
|
2511 |
// System.err.println(v + " " + ((v.flags() & STATIC) != 0) + " " + |
|
2512 |
// tree.pos + " " + v.pos + " " + |
|
2513 |
// Resolve.isStatic(env));//DEBUG |
|
2514 |
||
2515 |
// A forward reference is diagnosed if the declaration position |
|
2516 |
// of the variable is greater than the current tree position |
|
2517 |
// and the tree and variable definition occur in the same class |
|
2518 |
// definition. Note that writes don't count as references. |
|
2519 |
// This check applies only to class and instance |
|
2520 |
// variables. Local variables follow different scope rules, |
|
2521 |
// and are subject to definite assignment checking. |
|
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2522 |
if ((env.info.enclVar == v || v.pos > tree.pos) && |
10 | 2523 |
v.owner.kind == TYP && |
2524 |
canOwnInitializer(env.info.scope.owner) && |
|
2525 |
v.owner == env.info.scope.owner.enclClass() && |
|
2526 |
((v.flags() & STATIC) != 0) == Resolve.isStatic(env) && |
|
2527 |
(env.tree.getTag() != JCTree.ASSIGN || |
|
2528 |
TreeInfo.skipParens(((JCAssign) env.tree).lhs) != tree)) { |
|
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2529 |
String suffix = (env.info.enclVar == v) ? |
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2530 |
"self.ref" : "forward.ref"; |
325
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2531 |
if (!onlyWarning || isStaticEnumField(v)) { |
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2532 |
log.error(tree.pos(), "illegal." + suffix); |
10 | 2533 |
} else if (useBeforeDeclarationWarning) { |
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2534 |
log.warning(tree.pos(), suffix, v); |
10 | 2535 |
} |
2536 |
} |
|
2537 |
||
2538 |
v.getConstValue(); // ensure initializer is evaluated |
|
2539 |
||
2540 |
checkEnumInitializer(tree, env, v); |
|
2541 |
} |
|
2542 |
||
2543 |
/** |
|
2544 |
* Check for illegal references to static members of enum. In |
|
2545 |
* an enum type, constructors and initializers may not |
|
2546 |
* reference its static members unless they are constant. |
|
2547 |
* |
|
2548 |
* @param tree The tree making up the variable reference. |
|
2549 |
* @param env The current environment. |
|
2550 |
* @param v The variable's symbol. |
|
2551 |
* @see JLS 3rd Ed. (8.9 Enums) |
|
2552 |
*/ |
|
2553 |
private void checkEnumInitializer(JCTree tree, Env<AttrContext> env, VarSymbol v) { |
|
2554 |
// JLS 3rd Ed.: |
|
2555 |
// |
|
2556 |
// "It is a compile-time error to reference a static field |
|
2557 |
// of an enum type that is not a compile-time constant |
|
2558 |
// (15.28) from constructors, instance initializer blocks, |
|
2559 |
// or instance variable initializer expressions of that |
|
2560 |
// type. It is a compile-time error for the constructors, |
|
2561 |
// instance initializer blocks, or instance variable |
|
2562 |
// initializer expressions of an enum constant e to refer |
|
2563 |
// to itself or to an enum constant of the same type that |
|
2564 |
// is declared to the right of e." |
|
325
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2565 |
if (isStaticEnumField(v)) { |
10 | 2566 |
ClassSymbol enclClass = env.info.scope.owner.enclClass(); |
2567 |
||
2568 |
if (enclClass == null || enclClass.owner == null) |
|
2569 |
return; |
|
2570 |
||
2571 |
// See if the enclosing class is the enum (or a |
|
2572 |
// subclass thereof) declaring v. If not, this |
|
2573 |
// reference is OK. |
|
2574 |
if (v.owner != enclClass && !types.isSubtype(enclClass.type, v.owner.type)) |
|
2575 |
return; |
|
2576 |
||
2577 |
// If the reference isn't from an initializer, then |
|
2578 |
// the reference is OK. |
|
2579 |
if (!Resolve.isInitializer(env)) |
|
2580 |
return; |
|
2581 |
||
2582 |
log.error(tree.pos(), "illegal.enum.static.ref"); |
|
2583 |
} |
|
2584 |
} |
|
2585 |
||
325
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2586 |
/** Is the given symbol a static, non-constant field of an Enum? |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2587 |
* Note: enum literals should not be regarded as such |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2588 |
*/ |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2589 |
private boolean isStaticEnumField(VarSymbol v) { |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2590 |
return Flags.isEnum(v.owner) && |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2591 |
Flags.isStatic(v) && |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2592 |
!Flags.isConstant(v) && |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2593 |
v.name != names._class; |
10 | 2594 |
} |
2595 |
||
2596 |
/** Can the given symbol be the owner of code which forms part |
|
2597 |
* if class initialization? This is the case if the symbol is |
|
2598 |
* a type or field, or if the symbol is the synthetic method. |
|
2599 |
* owning a block. |
|
2600 |
*/ |
|
2601 |
private boolean canOwnInitializer(Symbol sym) { |
|
2602 |
return |
|
2603 |
(sym.kind & (VAR | TYP)) != 0 || |
|
2604 |
(sym.kind == MTH && (sym.flags() & BLOCK) != 0); |
|
2605 |
} |
|
2606 |
||
2607 |
Warner noteWarner = new Warner(); |
|
2608 |
||
2609 |
/** |
|
2610 |
* Check that method arguments conform to its instantation. |
|
2611 |
**/ |
|
2612 |
public Type checkMethod(Type site, |
|
2613 |
Symbol sym, |
|
2614 |
Env<AttrContext> env, |
|
2615 |
final List<JCExpression> argtrees, |
|
2616 |
List<Type> argtypes, |
|
2617 |
List<Type> typeargtypes, |
|
2618 |
boolean useVarargs) { |
|
2619 |
// Test (5): if symbol is an instance method of a raw type, issue |
|
2620 |
// an unchecked warning if its argument types change under erasure. |
|
2621 |
if (allowGenerics && |
|
2622 |
(sym.flags() & STATIC) == 0 && |
|
2623 |
(site.tag == CLASS || site.tag == TYPEVAR)) { |
|
2624 |
Type s = types.asOuterSuper(site, sym.owner); |
|
2625 |
if (s != null && s.isRaw() && |
|
2626 |
!types.isSameTypes(sym.type.getParameterTypes(), |
|
2627 |
sym.erasure(types).getParameterTypes())) { |
|
2628 |
chk.warnUnchecked(env.tree.pos(), |
|
2629 |
"unchecked.call.mbr.of.raw.type", |
|
2630 |
sym, s); |
|
2631 |
} |
|
2632 |
} |
|
2633 |
||
2634 |
// Compute the identifier's instantiated type. |
|
2635 |
// For methods, we need to compute the instance type by |
|
2636 |
// Resolve.instantiate from the symbol's type as well as |
|
2637 |
// any type arguments and value arguments. |
|
2638 |
noteWarner.warned = false; |
|
2639 |
Type owntype = rs.instantiate(env, |
|
2640 |
site, |
|
2641 |
sym, |
|
2642 |
argtypes, |
|
2643 |
typeargtypes, |
|
2644 |
true, |
|
2645 |
useVarargs, |
|
2646 |
noteWarner); |
|
2647 |
boolean warned = noteWarner.warned; |
|
2648 |
||
2649 |
// If this fails, something went wrong; we should not have |
|
2650 |
// found the identifier in the first place. |
|
2651 |
if (owntype == null) { |
|
2652 |
if (!pt.isErroneous()) |
|
2653 |
log.error(env.tree.pos(), |
|
2654 |
"internal.error.cant.instantiate", |
|
2655 |
sym, site, |
|
2656 |
Type.toString(pt.getParameterTypes())); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
2657 |
owntype = types.createErrorType(site); |
10 | 2658 |
} else { |
2659 |
// System.out.println("call : " + env.tree); |
|
2660 |
// System.out.println("method : " + owntype); |
|
2661 |
// System.out.println("actuals: " + argtypes); |
|
2662 |
List<Type> formals = owntype.getParameterTypes(); |
|
2663 |
Type last = useVarargs ? formals.last() : null; |
|
2664 |
if (sym.name==names.init && |
|
2665 |
sym.owner == syms.enumSym) |
|
2666 |
formals = formals.tail.tail; |
|
2667 |
List<JCExpression> args = argtrees; |
|
2668 |
while (formals.head != last) { |
|
2669 |
JCTree arg = args.head; |
|
2670 |
Warner warn = chk.convertWarner(arg.pos(), arg.type, formals.head); |
|
2671 |
assertConvertible(arg, arg.type, formals.head, warn); |
|
2672 |
warned |= warn.warned; |
|
2673 |
args = args.tail; |
|
2674 |
formals = formals.tail; |
|
2675 |
} |
|
2676 |
if (useVarargs) { |
|
2677 |
Type varArg = types.elemtype(last); |
|
2678 |
while (args.tail != null) { |
|
2679 |
JCTree arg = args.head; |
|
2680 |
Warner warn = chk.convertWarner(arg.pos(), arg.type, varArg); |
|
2681 |
assertConvertible(arg, arg.type, varArg, warn); |
|
2682 |
warned |= warn.warned; |
|
2683 |
args = args.tail; |
|
2684 |
} |
|
2685 |
} else if ((sym.flags() & VARARGS) != 0 && allowVarargs) { |
|
2686 |
// non-varargs call to varargs method |
|
2687 |
Type varParam = owntype.getParameterTypes().last(); |
|
2688 |
Type lastArg = argtypes.last(); |
|
2689 |
if (types.isSubtypeUnchecked(lastArg, types.elemtype(varParam)) && |
|
2690 |
!types.isSameType(types.erasure(varParam), types.erasure(lastArg))) |
|
2691 |
log.warning(argtrees.last().pos(), "inexact.non-varargs.call", |
|
2692 |
types.elemtype(varParam), |
|
2693 |
varParam); |
|
2694 |
} |
|
2695 |
||
2696 |
if (warned && sym.type.tag == FORALL) { |
|
2697 |
chk.warnUnchecked(env.tree.pos(), |
|
2698 |
"unchecked.meth.invocation.applied", |
|
1534
e923a41e84cc
6758789: Some method resolution diagnostic should be improved
mcimadamore
parents:
1532
diff
changeset
|
2699 |
kindName(sym), |
e923a41e84cc
6758789: Some method resolution diagnostic should be improved
mcimadamore
parents:
1532
diff
changeset
|
2700 |
sym.name, |
e923a41e84cc
6758789: Some method resolution diagnostic should be improved
mcimadamore
parents:
1532
diff
changeset
|
2701 |
rs.methodArguments(sym.type.getParameterTypes()), |
e923a41e84cc
6758789: Some method resolution diagnostic should be improved
mcimadamore
parents:
1532
diff
changeset
|
2702 |
rs.methodArguments(argtypes), |
e923a41e84cc
6758789: Some method resolution diagnostic should be improved
mcimadamore
parents:
1532
diff
changeset
|
2703 |
kindName(sym.location()), |
e923a41e84cc
6758789: Some method resolution diagnostic should be improved
mcimadamore
parents:
1532
diff
changeset
|
2704 |
sym.location()); |
10 | 2705 |
owntype = new MethodType(owntype.getParameterTypes(), |
2706 |
types.erasure(owntype.getReturnType()), |
|
2707 |
owntype.getThrownTypes(), |
|
2708 |
syms.methodClass); |
|
2709 |
} |
|
2710 |
if (useVarargs) { |
|
2711 |
JCTree tree = env.tree; |
|
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5654
diff
changeset
|
2712 |
Type argtype = owntype.getParameterTypes().last(); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
2713 |
if (owntype.getReturnType().tag != FORALL || warned) { |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5654
diff
changeset
|
2714 |
chk.checkVararg(env.tree.pos(), owntype.getParameterTypes(), sym, env); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
2715 |
} |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5654
diff
changeset
|
2716 |
Type elemtype = types.elemtype(argtype); |
10 | 2717 |
switch (tree.getTag()) { |
2718 |
case JCTree.APPLY: |
|
2719 |
((JCMethodInvocation) tree).varargsElement = elemtype; |
|
2720 |
break; |
|
2721 |
case JCTree.NEWCLASS: |
|
2722 |
((JCNewClass) tree).varargsElement = elemtype; |
|
2723 |
break; |
|
2724 |
default: |
|
2725 |
throw new AssertionError(""+tree); |
|
2726 |
} |
|
2727 |
} |
|
2728 |
} |
|
2729 |
return owntype; |
|
2730 |
} |
|
2731 |
||
2732 |
private void assertConvertible(JCTree tree, Type actual, Type formal, Warner warn) { |
|
2733 |
if (types.isConvertible(actual, formal, warn)) |
|
2734 |
return; |
|
2735 |
||
2736 |
if (formal.isCompound() |
|
2737 |
&& types.isSubtype(actual, types.supertype(formal)) |
|
2738 |
&& types.isSubtypeUnchecked(actual, types.interfaces(formal), warn)) |
|
2739 |
return; |
|
2740 |
||
2741 |
if (false) { |
|
2742 |
// TODO: make assertConvertible work |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
941
diff
changeset
|
2743 |
chk.typeError(tree.pos(), diags.fragment("incompatible.types"), actual, formal); |
10 | 2744 |
throw new AssertionError("Tree: " + tree |
2745 |
+ " actual:" + actual |
|
2746 |
+ " formal: " + formal); |
|
2747 |
} |
|
2748 |
} |
|
2749 |
||
2750 |
public void visitLiteral(JCLiteral tree) { |
|
2751 |
result = check( |
|
2752 |
tree, litType(tree.typetag).constType(tree.value), VAL, pkind, pt); |
|
2753 |
} |
|
2754 |
//where |
|
2755 |
/** Return the type of a literal with given type tag. |
|
2756 |
*/ |
|
2757 |
Type litType(int tag) { |
|
2758 |
return (tag == TypeTags.CLASS) ? syms.stringType : syms.typeOfTag[tag]; |
|
2759 |
} |
|
2760 |
||
2761 |
public void visitTypeIdent(JCPrimitiveTypeTree tree) { |
|
2762 |
result = check(tree, syms.typeOfTag[tree.typetag], TYP, pkind, pt); |
|
2763 |
} |
|
2764 |
||
2765 |
public void visitTypeArray(JCArrayTypeTree tree) { |
|
2766 |
Type etype = attribType(tree.elemtype, env); |
|
2767 |
Type type = new ArrayType(etype, syms.arrayClass); |
|
2768 |
result = check(tree, type, TYP, pkind, pt); |
|
2769 |
} |
|
2770 |
||
2771 |
/** Visitor method for parameterized types. |
|
2772 |
* Bound checking is left until later, since types are attributed |
|
2773 |
* before supertype structure is completely known |
|
2774 |
*/ |
|
2775 |
public void visitTypeApply(JCTypeApply tree) { |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
2776 |
Type owntype = types.createErrorType(tree.type); |
10 | 2777 |
|
2778 |
// Attribute functor part of application and make sure it's a class. |
|
2779 |
Type clazztype = chk.checkClassType(tree.clazz.pos(), attribType(tree.clazz, env)); |
|
2780 |
||
2781 |
// Attribute type parameters |
|
2782 |
List<Type> actuals = attribTypes(tree.arguments, env); |
|
2783 |
||
2784 |
if (clazztype.tag == CLASS) { |
|
2785 |
List<Type> formals = clazztype.tsym.type.getTypeArguments(); |
|
2786 |
||
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
2787 |
if (actuals.length() == formals.length() || actuals.length() == 0) { |
10 | 2788 |
List<Type> a = actuals; |
2789 |
List<Type> f = formals; |
|
2790 |
while (a.nonEmpty()) { |
|
2791 |
a.head = a.head.withTypeVar(f.head); |
|
2792 |
a = a.tail; |
|
2793 |
f = f.tail; |
|
2794 |
} |
|
2795 |
// Compute the proper generic outer |
|
2796 |
Type clazzOuter = clazztype.getEnclosingType(); |
|
2797 |
if (clazzOuter.tag == CLASS) { |
|
2798 |
Type site; |
|
3149 | 2799 |
JCExpression clazz = TreeInfo.typeIn(tree.clazz); |
2800 |
if (clazz.getTag() == JCTree.IDENT) { |
|
10 | 2801 |
site = env.enclClass.sym.type; |
3149 | 2802 |
} else if (clazz.getTag() == JCTree.SELECT) { |
2803 |
site = ((JCFieldAccess) clazz).selected.type; |
|
10 | 2804 |
} else throw new AssertionError(""+tree); |
2805 |
if (clazzOuter.tag == CLASS && site != clazzOuter) { |
|
2806 |
if (site.tag == CLASS) |
|
2807 |
site = types.asOuterSuper(site, clazzOuter.tsym); |
|
2808 |
if (site == null) |
|
2809 |
site = types.erasure(clazzOuter); |
|
2810 |
clazzOuter = site; |
|
2811 |
} |
|
2812 |
} |
|
5320
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
2813 |
owntype = new ClassType(clazzOuter, actuals, clazztype.tsym); |
10 | 2814 |
} else { |
2815 |
if (formals.length() != 0) { |
|
2816 |
log.error(tree.pos(), "wrong.number.type.args", |
|
2817 |
Integer.toString(formals.length())); |
|
2818 |
} else { |
|
2819 |
log.error(tree.pos(), "type.doesnt.take.params", clazztype.tsym); |
|
2820 |
} |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1045
diff
changeset
|
2821 |
owntype = types.createErrorType(tree.type); |
10 | 2822 |
} |
2823 |
} |
|
2824 |
result = check(tree, owntype, TYP, pkind, pt); |
|
2825 |
} |
|
2826 |
||
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
2827 |
public void visitTypeDisjoint(JCTypeDisjoint tree) { |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
2828 |
List<Type> componentTypes = attribTypes(tree.components, env); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
2829 |
tree.type = result = check(tree, types.lub(componentTypes), TYP, pkind, pt); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
2830 |
} |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5489
diff
changeset
|
2831 |
|
10 | 2832 |
public void visitTypeParameter(JCTypeParameter tree) { |
2833 |
TypeVar a = (TypeVar)tree.type; |
|
2834 |
Set<Type> boundSet = new HashSet<Type>(); |
|
2835 |
if (a.bound.isErroneous()) |
|
2836 |
return; |
|
2837 |
List<Type> bs = types.getBounds(a); |
|
2838 |
if (tree.bounds.nonEmpty()) { |
|
2839 |
// accept class or interface or typevar as first bound. |
|
2840 |
Type b = checkBase(bs.head, tree.bounds.head, env, false, false, false); |
|
2841 |
boundSet.add(types.erasure(b)); |
|
1532
ae41c47516e5
6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents:
1528
diff
changeset
|
2842 |
if (b.isErroneous()) { |
ae41c47516e5
6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents:
1528
diff
changeset
|
2843 |
a.bound = b; |
ae41c47516e5
6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents:
1528
diff
changeset
|
2844 |
} |
ae41c47516e5
6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents:
1528
diff
changeset
|
2845 |
else if (b.tag == TYPEVAR) { |
10 | 2846 |
// if first bound was a typevar, do not accept further bounds. |
2847 |
if (tree.bounds.tail.nonEmpty()) { |
|
2848 |
log.error(tree.bounds.tail.head.pos(), |
|
2849 |
"type.var.may.not.be.followed.by.other.bounds"); |
|
2850 |
tree.bounds = List.of(tree.bounds.head); |
|
164
c1e219636b4e
6608214: Exception throw while analysing a file with error
mcimadamore
parents:
163
diff
changeset
|
2851 |
a.bound = bs.head; |
10 | 2852 |
} |
2853 |
} else { |
|
2854 |
// if first bound was a class or interface, accept only interfaces |
|
2855 |
// as further bounds. |
|
2856 |
for (JCExpression bound : tree.bounds.tail) { |
|
2857 |
bs = bs.tail; |
|
2858 |
Type i = checkBase(bs.head, bound, env, false, true, false); |
|
1532
ae41c47516e5
6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents:
1528
diff
changeset
|
2859 |
if (i.isErroneous()) |
ae41c47516e5
6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents:
1528
diff
changeset
|
2860 |
a.bound = i; |
ae41c47516e5
6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents:
1528
diff
changeset
|
2861 |
else if (i.tag == CLASS) |
10 | 2862 |
chk.checkNotRepeated(bound.pos(), types.erasure(i), boundSet); |
2863 |
} |
|
2864 |
} |
|
2865 |
} |
|
2866 |
bs = types.getBounds(a); |
|
2867 |
||
2868 |
// in case of multiple bounds ... |
|
2869 |
if (bs.length() > 1) { |
|
2870 |
// ... the variable's bound is a class type flagged COMPOUND |
|
2871 |
// (see comment for TypeVar.bound). |
|
2872 |
// In this case, generate a class tree that represents the |
|
2873 |
// bound class, ... |
|
2874 |
JCTree extending; |
|
2875 |
List<JCExpression> implementing; |
|
2876 |
if ((bs.head.tsym.flags() & INTERFACE) == 0) { |
|
2877 |
extending = tree.bounds.head; |
|
2878 |
implementing = tree.bounds.tail; |
|
2879 |
} else { |
|
2880 |
extending = null; |
|
2881 |
implementing = tree.bounds; |
|
2882 |
} |
|
2883 |
JCClassDecl cd = make.at(tree.pos).ClassDef( |
|
2884 |
make.Modifiers(PUBLIC | ABSTRACT), |
|
2885 |
tree.name, List.<JCTypeParameter>nil(), |
|
2886 |
extending, implementing, List.<JCTree>nil()); |
|
2887 |
||
2888 |
ClassSymbol c = (ClassSymbol)a.getUpperBound().tsym; |
|
2889 |
assert (c.flags() & COMPOUND) != 0; |
|
2890 |
cd.sym = c; |
|
2891 |
c.sourcefile = env.toplevel.sourcefile; |
|
2892 |
||
2893 |
// ... and attribute the bound class |
|
2894 |
c.flags_field |= UNATTRIBUTED; |
|
2895 |
Env<AttrContext> cenv = enter.classEnv(cd, env); |
|
2896 |
enter.typeEnvs.put(c, cenv); |
|
2897 |
} |
|
2898 |
} |
|
2899 |
||
2900 |
||
2901 |
public void visitWildcard(JCWildcard tree) { |
|
2902 |
//- System.err.println("visitWildcard("+tree+");");//DEBUG |
|
2903 |
Type type = (tree.kind.kind == BoundKind.UNBOUND) |
|
2904 |
? syms.objectType |
|
2905 |
: attribType(tree.inner, env); |
|
2906 |
result = check(tree, new WildcardType(chk.checkRefType(tree.pos(), type), |
|
2907 |
tree.kind.kind, |
|
2908 |
syms.boundClass), |
|
2909 |
TYP, pkind, pt); |
|
2910 |
} |
|
2911 |
||
2912 |
public void visitAnnotation(JCAnnotation tree) { |
|
2913 |
log.error(tree.pos(), "annotation.not.valid.for.type", pt); |
|
2914 |
result = tree.type = syms.errType; |
|
2915 |
} |
|
2916 |
||
3149 | 2917 |
public void visitAnnotatedType(JCAnnotatedType tree) { |
2918 |
result = tree.type = attribType(tree.getUnderlyingType(), env); |
|
2919 |
} |
|
2920 |
||
10 | 2921 |
public void visitErroneous(JCErroneous tree) { |
2922 |
if (tree.errs != null) |
|
2923 |
for (JCTree err : tree.errs) |
|
2924 |
attribTree(err, env, ERR, pt); |
|
2925 |
result = tree.type = syms.errType; |
|
2926 |
} |
|
2927 |
||
2928 |
/** Default visitor method for all other trees. |
|
2929 |
*/ |
|
2930 |
public void visitTree(JCTree tree) { |
|
2931 |
throw new AssertionError(); |
|
2932 |
} |
|
2933 |
||
2934 |
/** Main method: attribute class definition associated with given class symbol. |
|
2935 |
* reporting completion failures at the given position. |
|
2936 |
* @param pos The source position at which completion errors are to be |
|
2937 |
* reported. |
|
2938 |
* @param c The class symbol whose definition will be attributed. |
|
2939 |
*/ |
|
2940 |
public void attribClass(DiagnosticPosition pos, ClassSymbol c) { |
|
2941 |
try { |
|
2942 |
annotate.flush(); |
|
2943 |
attribClass(c); |
|
2944 |
} catch (CompletionFailure ex) { |
|
2945 |
chk.completionError(pos, ex); |
|
2946 |
} |
|
2947 |
} |
|
2948 |
||
2949 |
/** Attribute class definition associated with given class symbol. |
|
2950 |
* @param c The class symbol whose definition will be attributed. |
|
2951 |
*/ |
|
2952 |
void attribClass(ClassSymbol c) throws CompletionFailure { |
|
2953 |
if (c.type.tag == ERROR) return; |
|
2954 |
||
2955 |
// Check for cycles in the inheritance graph, which can arise from |
|
2956 |
// ill-formed class files. |
|
2957 |
chk.checkNonCyclic(null, c.type); |
|
2958 |
||
2959 |
Type st = types.supertype(c.type); |
|
2960 |
if ((c.flags_field & Flags.COMPOUND) == 0) { |
|
2961 |
// First, attribute superclass. |
|
2962 |
if (st.tag == CLASS) |
|
2963 |
attribClass((ClassSymbol)st.tsym); |
|
2964 |
||
2965 |
// Next attribute owner, if it is a class. |
|
2966 |
if (c.owner.kind == TYP && c.owner.type.tag == CLASS) |
|
2967 |
attribClass((ClassSymbol)c.owner); |
|
2968 |
} |
|
2969 |
||
2970 |
// The previous operations might have attributed the current class |
|
2971 |
// if there was a cycle. So we test first whether the class is still |
|
2972 |
// UNATTRIBUTED. |
|
2973 |
if ((c.flags_field & UNATTRIBUTED) != 0) { |
|
2974 |
c.flags_field &= ~UNATTRIBUTED; |
|
2975 |
||
2976 |
// Get environment current at the point of class definition. |
|
2977 |
Env<AttrContext> env = enter.typeEnvs.get(c); |
|
2978 |
||
2979 |
// The info.lint field in the envs stored in enter.typeEnvs is deliberately uninitialized, |
|
2980 |
// because the annotations were not available at the time the env was created. Therefore, |
|
2981 |
// we look up the environment chain for the first enclosing environment for which the |
|
2982 |
// lint value is set. Typically, this is the parent env, but might be further if there |
|
2983 |
// are any envs created as a result of TypeParameter nodes. |
|
2984 |
Env<AttrContext> lintEnv = env; |
|
2985 |
while (lintEnv.info.lint == null) |
|
2986 |
lintEnv = lintEnv.next; |
|
2987 |
||
2988 |
// Having found the enclosing lint value, we can initialize the lint value for this class |
|
2989 |
env.info.lint = lintEnv.info.lint.augment(c.attributes_field, c.flags()); |
|
2990 |
||
2991 |
Lint prevLint = chk.setLint(env.info.lint); |
|
2992 |
JavaFileObject prev = log.useSource(c.sourcefile); |
|
2993 |
||
2994 |
try { |
|
2995 |
// java.lang.Enum may not be subclassed by a non-enum |
|
2996 |
if (st.tsym == syms.enumSym && |
|
2997 |
((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0)) |
|
2998 |
log.error(env.tree.pos(), "enum.no.subclassing"); |
|
2999 |
||
3000 |
// Enums may not be extended by source-level classes |
|
3001 |
if (st.tsym != null && |
|
3002 |
((st.tsym.flags_field & Flags.ENUM) != 0) && |
|
941
2589047a9c5d
6675483: Javac rejects multiple type-variable bound declarations starting with an enum type
mcimadamore
parents:
939
diff
changeset
|
3003 |
((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0) && |
10 | 3004 |
!target.compilerBootstrap(c)) { |
3005 |
log.error(env.tree.pos(), "enum.types.not.extensible"); |
|
3006 |
} |
|
3007 |
attribClassBody(env, c); |
|
3008 |
||
3009 |
chk.checkDeprecatedAnnotation(env.tree.pos(), c); |
|
3010 |
} finally { |
|
3011 |
log.useSource(prev); |
|
3012 |
chk.setLint(prevLint); |
|
3013 |
} |
|
3014 |
||
3015 |
} |
|
3016 |
} |
|
3017 |
||
3018 |
public void visitImport(JCImport tree) { |
|
3019 |
// nothing to do |
|
3020 |
} |
|
3021 |
||
3022 |
/** Finish the attribution of a class. */ |
|
3023 |
private void attribClassBody(Env<AttrContext> env, ClassSymbol c) { |
|
3024 |
JCClassDecl tree = (JCClassDecl)env.tree; |
|
3025 |
assert c == tree.sym; |
|
3026 |
||
3027 |
// Validate annotations |
|
3028 |
chk.validateAnnotations(tree.mods.annotations, c); |
|
3029 |
||
3030 |
// Validate type parameters, supertype and interfaces. |
|
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
3031 |
attribBounds(tree.typarams); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
3032 |
if (!c.isAnonymous()) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
3033 |
//already checked if anonymous |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
3034 |
chk.validate(tree.typarams, env); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
3035 |
chk.validate(tree.extending, env); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
3036 |
chk.validate(tree.implementing, env); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
3037 |
} |
10 | 3038 |
|
3039 |
// If this is a non-abstract class, check that it has no abstract |
|
3040 |
// methods or unimplemented methods of an implemented interface. |
|
3041 |
if ((c.flags() & (ABSTRACT | INTERFACE)) == 0) { |
|
3042 |
if (!relax) |
|
3043 |
chk.checkAllDefined(tree.pos(), c); |
|
3044 |
} |
|
3045 |
||
3046 |
if ((c.flags() & ANNOTATION) != 0) { |
|
3047 |
if (tree.implementing.nonEmpty()) |
|
3048 |
log.error(tree.implementing.head.pos(), |
|
3049 |
"cant.extend.intf.annotation"); |
|
3050 |
if (tree.typarams.nonEmpty()) |
|
3051 |
log.error(tree.typarams.head.pos(), |
|
3052 |
"intf.annotation.cant.have.type.params"); |
|
3053 |
} else { |
|
3054 |
// Check that all extended classes and interfaces |
|
3055 |
// are compatible (i.e. no two define methods with same arguments |
|
3056 |
// yet different return types). (JLS 8.4.6.3) |
|
3057 |
chk.checkCompatibleSupertypes(tree.pos(), c.type); |
|
3058 |
} |
|
3059 |
||
3060 |
// Check that class does not import the same parameterized interface |
|
3061 |
// with two different argument lists. |
|
3062 |
chk.checkClassBounds(tree.pos(), c.type); |
|
3063 |
||
3064 |
tree.type = c.type; |
|
3065 |
||
3066 |
boolean assertsEnabled = false; |
|
3067 |
assert assertsEnabled = true; |
|
3068 |
if (assertsEnabled) { |
|
3069 |
for (List<JCTypeParameter> l = tree.typarams; |
|
3070 |
l.nonEmpty(); l = l.tail) |
|
3071 |
assert env.info.scope.lookup(l.head.name).scope != null; |
|
3072 |
} |
|
3073 |
||
3074 |
// Check that a generic class doesn't extend Throwable |
|
3075 |
if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType)) |
|
3076 |
log.error(tree.extending.pos(), "generic.throwable"); |
|
3077 |
||
3078 |
// Check that all methods which implement some |
|
3079 |
// method conform to the method they implement. |
|
3080 |
chk.checkImplementations(tree); |
|
3081 |
||
3082 |
for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
|
3083 |
// Attribute declaration |
|
3084 |
attribStat(l.head, env); |
|
3085 |
// Check that declarations in inner classes are not static (JLS 8.1.2) |
|
3086 |
// Make an exception for static constants. |
|
3087 |
if (c.owner.kind != PCK && |
|
3088 |
((c.flags() & STATIC) == 0 || c.name == names.empty) && |
|
3089 |
(TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) { |
|
3090 |
Symbol sym = null; |
|
3091 |
if (l.head.getTag() == JCTree.VARDEF) sym = ((JCVariableDecl) l.head).sym; |
|
3092 |
if (sym == null || |
|
3093 |
sym.kind != VAR || |
|
3094 |
((VarSymbol) sym).getConstValue() == null) |
|
3095 |
log.error(l.head.pos(), "icls.cant.have.static.decl"); |
|
3096 |
} |
|
3097 |
} |
|
3098 |
||
3099 |
// Check for cycles among non-initial constructors. |
|
3100 |
chk.checkCyclicConstructors(tree); |
|
3101 |
||
3102 |
// Check for cycles among annotation elements. |
|
3103 |
chk.checkNonCyclicElements(tree); |
|
3104 |
||
3105 |
// Check for proper use of serialVersionUID |
|
3106 |
if (env.info.lint.isEnabled(Lint.LintCategory.SERIAL) && |
|
3107 |
isSerializable(c) && |
|
3108 |
(c.flags() & Flags.ENUM) == 0 && |
|
3109 |
(c.flags() & ABSTRACT) == 0) { |
|
3110 |
checkSerialVersionUID(tree, c); |
|
3111 |
} |
|
3149 | 3112 |
|
3113 |
// Check type annotations applicability rules |
|
3114 |
validateTypeAnnotations(tree); |
|
10 | 3115 |
} |
3116 |
// where |
|
3117 |
/** check if a class is a subtype of Serializable, if that is available. */ |
|
3118 |
private boolean isSerializable(ClassSymbol c) { |
|
3119 |
try { |
|
3120 |
syms.serializableType.complete(); |
|
3121 |
} |
|
3122 |
catch (CompletionFailure e) { |
|
3123 |
return false; |
|
3124 |
} |
|
3125 |
return types.isSubtype(c.type, syms.serializableType); |
|
3126 |
} |
|
3127 |
||
3128 |
/** Check that an appropriate serialVersionUID member is defined. */ |
|
3129 |
private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) { |
|
3130 |
||
3131 |
// check for presence of serialVersionUID |
|
3132 |
Scope.Entry e = c.members().lookup(names.serialVersionUID); |
|
3133 |
while (e.scope != null && e.sym.kind != VAR) e = e.next(); |
|
3134 |
if (e.scope == null) { |
|
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
3135 |
log.warning(Lint.LintCategory.SERIAL, |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
3136 |
tree.pos(), "missing.SVUID", c); |
10 | 3137 |
return; |
3138 |
} |
|
3139 |
||
3140 |
// check that it is static final |
|
3141 |
VarSymbol svuid = (VarSymbol)e.sym; |
|
3142 |
if ((svuid.flags() & (STATIC | FINAL)) != |
|
3143 |
(STATIC | FINAL)) |
|
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
3144 |
log.warning(Lint.LintCategory.SERIAL, |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
3145 |
TreeInfo.diagnosticPositionFor(svuid, tree), "improper.SVUID", c); |
10 | 3146 |
|
3147 |
// check that it is long |
|
3148 |
else if (svuid.type.tag != TypeTags.LONG) |
|
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
3149 |
log.warning(Lint.LintCategory.SERIAL, |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
3150 |
TreeInfo.diagnosticPositionFor(svuid, tree), "long.SVUID", c); |
10 | 3151 |
|
3152 |
// check constant |
|
3153 |
else if (svuid.getConstValue() == null) |
|
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
3154 |
log.warning(Lint.LintCategory.SERIAL, |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
3155 |
TreeInfo.diagnosticPositionFor(svuid, tree), "constant.SVUID", c); |
10 | 3156 |
} |
3157 |
||
3158 |
private Type capture(Type type) { |
|
3159 |
return types.capture(type); |
|
3160 |
} |
|
3149 | 3161 |
|
3162 |
private void validateTypeAnnotations(JCTree tree) { |
|
3163 |
tree.accept(typeAnnotationsValidator); |
|
3164 |
} |
|
3165 |
//where |
|
3166 |
private final JCTree.Visitor typeAnnotationsValidator = |
|
3167 |
new TreeScanner() { |
|
3168 |
public void visitAnnotation(JCAnnotation tree) { |
|
3169 |
if (tree instanceof JCTypeAnnotation) { |
|
3170 |
chk.validateTypeAnnotation((JCTypeAnnotation)tree, false); |
|
3171 |
} |
|
3172 |
super.visitAnnotation(tree); |
|
3173 |
} |
|
3174 |
public void visitTypeParameter(JCTypeParameter tree) { |
|
3175 |
chk.validateTypeAnnotations(tree.annotations, true); |
|
3176 |
// don't call super. skip type annotations |
|
3177 |
scan(tree.bounds); |
|
3178 |
} |
|
3179 |
public void visitMethodDef(JCMethodDecl tree) { |
|
3180 |
// need to check static methods |
|
3181 |
if ((tree.sym.flags() & Flags.STATIC) != 0) { |
|
3182 |
for (JCTypeAnnotation a : tree.receiverAnnotations) { |
|
3183 |
if (chk.isTypeAnnotation(a, false)) |
|
3184 |
log.error(a.pos(), "annotation.type.not.applicable"); |
|
3185 |
} |
|
3186 |
} |
|
3187 |
super.visitMethodDef(tree); |
|
3188 |
} |
|
3189 |
}; |
|
10 | 3190 |
} |