author | jlahoda |
Wed, 09 Jul 2014 16:32:05 +0200 | |
changeset 25443 | 9187d77f2c64 |
parent 25306 | 6c8fc76bf383 |
child 25445 | 603f0c93d5c9 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
23127
5aba53c2b9b2
8034048: javac crash with method references plus lambda plus var args
jjg
parents:
22168
diff
changeset
|
2 |
* Copyright (c) 1999, 2014, 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 |
||
30 |
import com.sun.tools.javac.code.*; |
|
31 |
import com.sun.tools.javac.code.Symbol.*; |
|
32 |
import com.sun.tools.javac.tree.*; |
|
33 |
import com.sun.tools.javac.tree.JCTree.*; |
|
34 |
import com.sun.tools.javac.util.*; |
|
35 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
36 |
import com.sun.tools.javac.util.List; |
|
37 |
||
38 |
import static com.sun.tools.javac.code.Flags.*; |
|
39 |
import static com.sun.tools.javac.code.Kinds.*; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25306
diff
changeset
|
40 |
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE; |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
41 |
import static com.sun.tools.javac.code.TypeTag.CLASS; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
42 |
import static com.sun.tools.javac.code.TypeTag.TYPEVAR; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
43 |
import static com.sun.tools.javac.code.TypeTag.VOID; |
16968 | 44 |
import static com.sun.tools.javac.comp.CompileStates.CompileState; |
10 | 45 |
|
46 |
/** This pass translates Generic Java to conventional Java. |
|
47 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
48 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
49 |
* If you write code that depends on this, you do so at your own risk. |
10 | 50 |
* This code and its internal interfaces are subject to change or |
51 |
* deletion without notice.</b> |
|
52 |
*/ |
|
53 |
public class TransTypes extends TreeTranslator { |
|
54 |
/** The context key for the TransTypes phase. */ |
|
22163 | 55 |
protected static final Context.Key<TransTypes> transTypesKey = new Context.Key<>(); |
10 | 56 |
|
57 |
/** Get the instance for this context. */ |
|
58 |
public static TransTypes instance(Context context) { |
|
59 |
TransTypes instance = context.get(transTypesKey); |
|
60 |
if (instance == null) |
|
61 |
instance = new TransTypes(context); |
|
62 |
return instance; |
|
63 |
} |
|
64 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
735
diff
changeset
|
65 |
private Names names; |
10 | 66 |
private Log log; |
67 |
private Symtab syms; |
|
68 |
private TreeMaker make; |
|
69 |
private Enter enter; |
|
70 |
private boolean allowEnums; |
|
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
71 |
private boolean allowInterfaceBridges; |
10 | 72 |
private Types types; |
73 |
private final Resolve resolve; |
|
74 |
||
75 |
/** |
|
76 |
* Flag to indicate whether or not to generate bridge methods. |
|
77 |
* For pre-Tiger source there is no need for bridge methods, so it |
|
78 |
* can be skipped to get better performance for -source 1.4 etc. |
|
79 |
*/ |
|
80 |
private final boolean addBridges; |
|
81 |
||
16968 | 82 |
private final CompileStates compileStates; |
83 |
||
10 | 84 |
protected TransTypes(Context context) { |
85 |
context.put(transTypesKey, this); |
|
16968 | 86 |
compileStates = CompileStates.instance(context); |
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
735
diff
changeset
|
87 |
names = Names.instance(context); |
10 | 88 |
log = Log.instance(context); |
89 |
syms = Symtab.instance(context); |
|
90 |
enter = Enter.instance(context); |
|
22163 | 91 |
overridden = new HashMap<>(); |
10 | 92 |
Source source = Source.instance(context); |
93 |
allowEnums = source.allowEnums(); |
|
94 |
addBridges = source.addBridges(); |
|
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
95 |
allowInterfaceBridges = source.allowDefaultMethods(); |
10 | 96 |
types = Types.instance(context); |
97 |
make = TreeMaker.instance(context); |
|
98 |
resolve = Resolve.instance(context); |
|
99 |
} |
|
100 |
||
101 |
/** A hashtable mapping bridge methods to the methods they override after |
|
102 |
* type erasure. |
|
103 |
*/ |
|
104 |
Map<MethodSymbol,MethodSymbol> overridden; |
|
105 |
||
106 |
/** Construct an attributed tree for a cast of expression to target type, |
|
107 |
* unless it already has precisely that type. |
|
108 |
* @param tree The expression tree. |
|
109 |
* @param target The target type. |
|
110 |
*/ |
|
111 |
JCExpression cast(JCExpression tree, Type target) { |
|
112 |
int oldpos = make.pos; |
|
113 |
make.at(tree.pos); |
|
114 |
if (!types.isSameType(tree.type, target)) { |
|
115 |
if (!resolve.isAccessible(env, target.tsym)) |
|
12082
32bdf6ef6388
7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents:
8032
diff
changeset
|
116 |
resolve.logAccessErrorInternal(env, tree, target); |
10 | 117 |
tree = make.TypeCast(make.Type(target), tree).setType(target); |
118 |
} |
|
119 |
make.pos = oldpos; |
|
120 |
return tree; |
|
121 |
} |
|
122 |
||
123 |
/** Construct an attributed tree to coerce an expression to some erased |
|
124 |
* target type, unless the expression is already assignable to that type. |
|
125 |
* If target type is a constant type, use its base type instead. |
|
126 |
* @param tree The expression tree. |
|
127 |
* @param target The target type. |
|
128 |
*/ |
|
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
129 |
public JCExpression coerce(Env<AttrContext> env, JCExpression tree, Type target) { |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
130 |
Env<AttrContext> prevEnv = this.env; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
131 |
try { |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
132 |
this.env = env; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
133 |
return coerce(tree, target); |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
134 |
} |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
135 |
finally { |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
136 |
this.env = prevEnv; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
137 |
} |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
138 |
} |
10 | 139 |
JCExpression coerce(JCExpression tree, Type target) { |
140 |
Type btarget = target.baseType(); |
|
141 |
if (tree.type.isPrimitive() == target.isPrimitive()) { |
|
14547 | 142 |
return types.isAssignable(tree.type, btarget, types.noWarnings) |
10 | 143 |
? tree |
144 |
: cast(tree, btarget); |
|
145 |
} |
|
146 |
return tree; |
|
147 |
} |
|
148 |
||
149 |
/** Given an erased reference type, assume this type as the tree's type. |
|
150 |
* Then, coerce to some given target type unless target type is null. |
|
151 |
* This operation is used in situations like the following: |
|
152 |
* |
|
14259 | 153 |
* <pre>{@code |
10 | 154 |
* class Cell<A> { A value; } |
155 |
* ... |
|
156 |
* Cell<Integer> cell; |
|
157 |
* Integer x = cell.value; |
|
14259 | 158 |
* }</pre> |
10 | 159 |
* |
160 |
* Since the erasure of Cell.value is Object, but the type |
|
161 |
* of cell.value in the assignment is Integer, we need to |
|
162 |
* adjust the original type of cell.value to Object, and insert |
|
163 |
* a cast to Integer. That is, the last assignment becomes: |
|
164 |
* |
|
14259 | 165 |
* <pre>{@code |
10 | 166 |
* Integer x = (Integer)cell.value; |
14259 | 167 |
* }</pre> |
10 | 168 |
* |
169 |
* @param tree The expression tree whose type might need adjustment. |
|
170 |
* @param erasedType The expression's type after erasure. |
|
171 |
* @param target The target type, which is usually the erasure of the |
|
172 |
* expression's original type. |
|
173 |
*/ |
|
174 |
JCExpression retype(JCExpression tree, Type erasedType, Type target) { |
|
175 |
// System.err.println("retype " + tree + " to " + erasedType);//DEBUG |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
176 |
if (!erasedType.isPrimitive()) { |
17810
db9f3e9cd760
7053059: VerifyError with double Assignment using a Generic Member of a Superclass
vromero
parents:
16974
diff
changeset
|
177 |
if (target != null && target.isPrimitive()) { |
10 | 178 |
target = erasure(tree.type); |
17810
db9f3e9cd760
7053059: VerifyError with double Assignment using a Generic Member of a Superclass
vromero
parents:
16974
diff
changeset
|
179 |
} |
10 | 180 |
tree.type = erasedType; |
17810
db9f3e9cd760
7053059: VerifyError with double Assignment using a Generic Member of a Superclass
vromero
parents:
16974
diff
changeset
|
181 |
if (target != null) { |
db9f3e9cd760
7053059: VerifyError with double Assignment using a Generic Member of a Superclass
vromero
parents:
16974
diff
changeset
|
182 |
return coerce(tree, target); |
db9f3e9cd760
7053059: VerifyError with double Assignment using a Generic Member of a Superclass
vromero
parents:
16974
diff
changeset
|
183 |
} |
10 | 184 |
} |
185 |
return tree; |
|
186 |
} |
|
187 |
||
188 |
/** Translate method argument list, casting each argument |
|
189 |
* to its corresponding type in a list of target types. |
|
190 |
* @param _args The method argument list. |
|
191 |
* @param parameters The list of target types. |
|
192 |
* @param varargsElement The erasure of the varargs element type, |
|
193 |
* or null if translating a non-varargs invocation |
|
194 |
*/ |
|
195 |
<T extends JCTree> List<T> translateArgs(List<T> _args, |
|
196 |
List<Type> parameters, |
|
197 |
Type varargsElement) { |
|
198 |
if (parameters.isEmpty()) return _args; |
|
199 |
List<T> args = _args; |
|
200 |
while (parameters.tail.nonEmpty()) { |
|
201 |
args.head = translate(args.head, parameters.head); |
|
202 |
args = args.tail; |
|
203 |
parameters = parameters.tail; |
|
204 |
} |
|
205 |
Type parameter = parameters.head; |
|
8032 | 206 |
Assert.check(varargsElement != null || args.length() == 1); |
10 | 207 |
if (varargsElement != null) { |
208 |
while (args.nonEmpty()) { |
|
209 |
args.head = translate(args.head, varargsElement); |
|
210 |
args = args.tail; |
|
211 |
} |
|
212 |
} else { |
|
213 |
args.head = translate(args.head, parameter); |
|
214 |
} |
|
215 |
return _args; |
|
216 |
} |
|
217 |
||
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
218 |
public <T extends JCTree> List<T> translateArgs(List<T> _args, |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
219 |
List<Type> parameters, |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
220 |
Type varargsElement, |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
221 |
Env<AttrContext> localEnv) { |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
222 |
Env<AttrContext> prevEnv = env; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
223 |
try { |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
224 |
env = localEnv; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
225 |
return translateArgs(_args, parameters, varargsElement); |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
226 |
} |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
227 |
finally { |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
228 |
env = prevEnv; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
229 |
} |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
230 |
} |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
231 |
|
10 | 232 |
/** Add a bridge definition and enter corresponding method symbol in |
233 |
* local scope of origin. |
|
234 |
* |
|
235 |
* @param pos The source code position to be used for the definition. |
|
236 |
* @param meth The method for which a bridge needs to be added |
|
237 |
* @param impl That method's implementation (possibly the method itself) |
|
238 |
* @param origin The class to which the bridge will be added |
|
239 |
* @param hypothetical |
|
240 |
* True if the bridge method is not strictly necessary in the |
|
241 |
* binary, but is represented in the symbol table to detect |
|
242 |
* erasure clashes. |
|
243 |
* @param bridges The list buffer to which the bridge will be added |
|
244 |
*/ |
|
245 |
void addBridge(DiagnosticPosition pos, |
|
246 |
MethodSymbol meth, |
|
247 |
MethodSymbol impl, |
|
248 |
ClassSymbol origin, |
|
249 |
boolean hypothetical, |
|
250 |
ListBuffer<JCTree> bridges) { |
|
251 |
make.at(pos); |
|
252 |
Type origType = types.memberType(origin.type, meth); |
|
253 |
Type origErasure = erasure(origType); |
|
254 |
||
255 |
// Create a bridge method symbol and a bridge definition without a body. |
|
256 |
Type bridgeType = meth.erasure(types); |
|
24793
bb58a9f1ae10
8044487: Fix for 8042785 causes regression tests to fail with java.lang.VerifyError
vromero
parents:
24792
diff
changeset
|
257 |
long flags = impl.flags() & AccessFlags | SYNTHETIC | BRIDGE | |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
258 |
(origin.isInterface() ? DEFAULT : 0); |
10 | 259 |
if (hypothetical) flags |= HYPOTHETICAL; |
260 |
MethodSymbol bridge = new MethodSymbol(flags, |
|
261 |
meth.name, |
|
262 |
bridgeType, |
|
263 |
origin); |
|
18000
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
264 |
/* once JDK-6996415 is solved it should be checked if this approach can |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
265 |
* be applied to method addOverrideBridgesIfNeeded |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
266 |
*/ |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
267 |
bridge.params = createBridgeParams(impl, bridge, bridgeType); |
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
18000
diff
changeset
|
268 |
bridge.setAttributes(impl); |
18000
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
269 |
|
10 | 270 |
if (!hypothetical) { |
271 |
JCMethodDecl md = make.MethodDef(bridge, null); |
|
272 |
||
273 |
// The bridge calls this.impl(..), if we have an implementation |
|
274 |
// in the current class, super.impl(...) otherwise. |
|
275 |
JCExpression receiver = (impl.owner == origin) |
|
276 |
? make.This(origin.erasure(types)) |
|
277 |
: make.Super(types.supertype(origin.type).tsym.erasure(types), origin); |
|
278 |
||
279 |
// The type returned from the original method. |
|
280 |
Type calltype = erasure(impl.type.getReturnType()); |
|
281 |
||
282 |
// Construct a call of this.impl(params), or super.impl(params), |
|
283 |
// casting params and possibly results as needed. |
|
284 |
JCExpression call = |
|
285 |
make.Apply( |
|
286 |
null, |
|
287 |
make.Select(receiver, impl).setType(calltype), |
|
288 |
translateArgs(make.Idents(md.params), origErasure.getParameterTypes(), null)) |
|
289 |
.setType(calltype); |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
290 |
JCStatement stat = (origErasure.getReturnType().hasTag(VOID)) |
10 | 291 |
? make.Exec(call) |
292 |
: make.Return(coerce(call, bridgeType.getReturnType())); |
|
293 |
md.body = make.Block(0, List.of(stat)); |
|
294 |
||
295 |
// Add bridge to `bridges' buffer |
|
296 |
bridges.append(md); |
|
297 |
} |
|
298 |
||
299 |
// Add bridge to scope of enclosing class and `overridden' table. |
|
300 |
origin.members().enter(bridge); |
|
301 |
overridden.put(bridge, meth); |
|
302 |
} |
|
303 |
||
18000
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
304 |
private List<VarSymbol> createBridgeParams(MethodSymbol impl, MethodSymbol bridge, |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
305 |
Type bridgeType) { |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
306 |
List<VarSymbol> bridgeParams = null; |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
307 |
if (impl.params != null) { |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
308 |
bridgeParams = List.nil(); |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
309 |
List<VarSymbol> implParams = impl.params; |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
310 |
Type.MethodType mType = (Type.MethodType)bridgeType; |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
311 |
List<Type> argTypes = mType.argtypes; |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
312 |
while (implParams.nonEmpty() && argTypes.nonEmpty()) { |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19939
diff
changeset
|
313 |
VarSymbol param = new VarSymbol(implParams.head.flags() | SYNTHETIC | PARAMETER, |
18000
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
314 |
implParams.head.name, argTypes.head, bridge); |
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
18000
diff
changeset
|
315 |
param.setAttributes(implParams.head); |
18000
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
316 |
bridgeParams = bridgeParams.append(param); |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
317 |
implParams = implParams.tail; |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
318 |
argTypes = argTypes.tail; |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
319 |
} |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
320 |
} |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
321 |
return bridgeParams; |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
322 |
} |
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17810
diff
changeset
|
323 |
|
10 | 324 |
/** Add bridge if given symbol is a non-private, non-static member |
325 |
* of the given class, which is either defined in the class or non-final |
|
326 |
* inherited, and one of the two following conditions holds: |
|
327 |
* 1. The method's type changes in the given class, as compared to the |
|
328 |
* class where the symbol was defined, (in this case |
|
329 |
* we have extended a parameterized class with non-trivial parameters). |
|
330 |
* 2. The method has an implementation with a different erased return type. |
|
331 |
* (in this case we have used co-variant returns). |
|
332 |
* If a bridge already exists in some other class, no new bridge is added. |
|
333 |
* Instead, it is checked that the bridge symbol overrides the method symbol. |
|
334 |
* (Spec ???). |
|
335 |
* todo: what about bridges for privates??? |
|
336 |
* |
|
337 |
* @param pos The source code position to be used for the definition. |
|
338 |
* @param sym The symbol for which a bridge might have to be added. |
|
339 |
* @param origin The class in which the bridge would go. |
|
340 |
* @param bridges The list buffer to which the bridge would be added. |
|
341 |
*/ |
|
342 |
void addBridgeIfNeeded(DiagnosticPosition pos, |
|
343 |
Symbol sym, |
|
344 |
ClassSymbol origin, |
|
345 |
ListBuffer<JCTree> bridges) { |
|
346 |
if (sym.kind == MTH && |
|
347 |
sym.name != names.init && |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6148
diff
changeset
|
348 |
(sym.flags() & (PRIVATE | STATIC)) == 0 && |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6148
diff
changeset
|
349 |
(sym.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC && |
10 | 350 |
sym.isMemberOf(origin, types)) |
351 |
{ |
|
352 |
MethodSymbol meth = (MethodSymbol)sym; |
|
353 |
MethodSymbol bridge = meth.binaryImplementation(origin, types); |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6148
diff
changeset
|
354 |
MethodSymbol impl = meth.implementation(origin, types, true, overrideBridgeFilter); |
10 | 355 |
if (bridge == null || |
356 |
bridge == meth || |
|
357 |
(impl != null && !bridge.owner.isSubClass(impl.owner, types))) { |
|
358 |
// No bridge was added yet. |
|
359 |
if (impl != null && isBridgeNeeded(meth, impl, origin.type)) { |
|
360 |
addBridge(pos, meth, impl, origin, bridge==impl, bridges); |
|
361 |
} else if (impl == meth |
|
362 |
&& impl.owner != origin |
|
363 |
&& (impl.flags() & FINAL) == 0 |
|
364 |
&& (meth.flags() & (ABSTRACT|PUBLIC)) == PUBLIC |
|
365 |
&& (origin.flags() & PUBLIC) > (impl.owner.flags() & PUBLIC)) { |
|
366 |
// this is to work around a horrible but permanent |
|
367 |
// reflection design error. |
|
368 |
addBridge(pos, meth, impl, origin, false, bridges); |
|
369 |
} |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6148
diff
changeset
|
370 |
} else if ((bridge.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) == SYNTHETIC) { |
10 | 371 |
MethodSymbol other = overridden.get(bridge); |
372 |
if (other != null && other != meth) { |
|
373 |
if (impl == null || !impl.overrides(other, origin, types, true)) { |
|
374 |
// Bridge for other symbol pair was added |
|
375 |
log.error(pos, "name.clash.same.erasure.no.override", |
|
376 |
other, other.location(origin.type, types), |
|
377 |
meth, meth.location(origin.type, types)); |
|
378 |
} |
|
379 |
} |
|
380 |
} else if (!bridge.overrides(meth, origin, types, true)) { |
|
381 |
// Accidental binary override without source override. |
|
382 |
if (bridge.owner == origin || |
|
383 |
types.asSuper(bridge.owner.type, meth.owner) == null) |
|
384 |
// Don't diagnose the problem if it would already |
|
385 |
// have been reported in the superclass |
|
386 |
log.error(pos, "name.clash.same.erasure.no.override", |
|
387 |
bridge, bridge.location(origin.type, types), |
|
388 |
meth, meth.location(origin.type, types)); |
|
389 |
} |
|
390 |
} |
|
391 |
} |
|
392 |
// where |
|
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
393 |
private Filter<Symbol> overrideBridgeFilter = new Filter<Symbol>() { |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6148
diff
changeset
|
394 |
public boolean accepts(Symbol s) { |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6148
diff
changeset
|
395 |
return (s.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6148
diff
changeset
|
396 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6148
diff
changeset
|
397 |
}; |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
398 |
|
10 | 399 |
/** |
400 |
* @param method The symbol for which a bridge might have to be added |
|
401 |
* @param impl The implementation of method |
|
402 |
* @param dest The type in which the bridge would go |
|
403 |
*/ |
|
404 |
private boolean isBridgeNeeded(MethodSymbol method, |
|
405 |
MethodSymbol impl, |
|
406 |
Type dest) { |
|
407 |
if (impl != method) { |
|
408 |
// If either method or impl have different erasures as |
|
409 |
// members of dest, a bridge is needed. |
|
410 |
Type method_erasure = method.erasure(types); |
|
411 |
if (!isSameMemberWhenErased(dest, method, method_erasure)) |
|
412 |
return true; |
|
413 |
Type impl_erasure = impl.erasure(types); |
|
414 |
if (!isSameMemberWhenErased(dest, impl, impl_erasure)) |
|
415 |
return true; |
|
416 |
||
417 |
// If the erasure of the return type is different, a |
|
418 |
// bridge is needed. |
|
419 |
return !types.isSameType(impl_erasure.getReturnType(), |
|
420 |
method_erasure.getReturnType()); |
|
421 |
} else { |
|
422 |
// method and impl are the same... |
|
423 |
if ((method.flags() & ABSTRACT) != 0) { |
|
424 |
// ...and abstract so a bridge is not needed. |
|
425 |
// Concrete subclasses will bridge as needed. |
|
426 |
return false; |
|
427 |
} |
|
428 |
||
429 |
// The erasure of the return type is always the same |
|
430 |
// for the same symbol. Reducing the three tests in |
|
431 |
// the other branch to just one: |
|
432 |
return !isSameMemberWhenErased(dest, method, method.erasure(types)); |
|
433 |
} |
|
434 |
} |
|
435 |
/** |
|
436 |
* Lookup the method as a member of the type. Compare the |
|
437 |
* erasures. |
|
438 |
* @param type the class where to look for the method |
|
439 |
* @param method the method to look for in class |
|
440 |
* @param erasure the erasure of method |
|
441 |
*/ |
|
442 |
private boolean isSameMemberWhenErased(Type type, |
|
443 |
MethodSymbol method, |
|
444 |
Type erasure) { |
|
445 |
return types.isSameType(erasure(types.memberType(type, method)), |
|
446 |
erasure); |
|
447 |
} |
|
448 |
||
449 |
void addBridges(DiagnosticPosition pos, |
|
450 |
TypeSymbol i, |
|
451 |
ClassSymbol origin, |
|
452 |
ListBuffer<JCTree> bridges) { |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25306
diff
changeset
|
453 |
for (Symbol sym : i.members().getSymbols(NON_RECURSIVE)) |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25306
diff
changeset
|
454 |
addBridgeIfNeeded(pos, sym, origin, bridges); |
10 | 455 |
for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail) |
456 |
addBridges(pos, l.head.tsym, origin, bridges); |
|
457 |
} |
|
458 |
||
459 |
/** Add all necessary bridges to some class appending them to list buffer. |
|
460 |
* @param pos The source code position to be used for the bridges. |
|
461 |
* @param origin The class in which the bridges go. |
|
462 |
* @param bridges The list buffer to which the bridges are added. |
|
463 |
*/ |
|
464 |
void addBridges(DiagnosticPosition pos, ClassSymbol origin, ListBuffer<JCTree> bridges) { |
|
465 |
Type st = types.supertype(origin.type); |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
466 |
while (st.hasTag(CLASS)) { |
10 | 467 |
// if (isSpecialization(st)) |
468 |
addBridges(pos, st.tsym, origin, bridges); |
|
469 |
st = types.supertype(st); |
|
470 |
} |
|
471 |
for (List<Type> l = types.interfaces(origin.type); l.nonEmpty(); l = l.tail) |
|
472 |
// if (isSpecialization(l.head)) |
|
473 |
addBridges(pos, l.head.tsym, origin, bridges); |
|
474 |
} |
|
475 |
||
476 |
/* ************************************************************************ |
|
477 |
* Visitor methods |
|
478 |
*************************************************************************/ |
|
479 |
||
480 |
/** Visitor argument: proto-type. |
|
481 |
*/ |
|
482 |
private Type pt; |
|
483 |
||
484 |
/** Visitor method: perform a type translation on tree. |
|
485 |
*/ |
|
486 |
public <T extends JCTree> T translate(T tree, Type pt) { |
|
487 |
Type prevPt = this.pt; |
|
488 |
try { |
|
489 |
this.pt = pt; |
|
490 |
return translate(tree); |
|
491 |
} finally { |
|
492 |
this.pt = prevPt; |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
/** Visitor method: perform a type translation on list of trees. |
|
497 |
*/ |
|
498 |
public <T extends JCTree> List<T> translate(List<T> trees, Type pt) { |
|
499 |
Type prevPt = this.pt; |
|
500 |
List<T> res; |
|
501 |
try { |
|
502 |
this.pt = pt; |
|
503 |
res = translate(trees); |
|
504 |
} finally { |
|
505 |
this.pt = prevPt; |
|
506 |
} |
|
507 |
return res; |
|
508 |
} |
|
509 |
||
510 |
public void visitClassDef(JCClassDecl tree) { |
|
511 |
translateClass(tree.sym); |
|
512 |
result = tree; |
|
513 |
} |
|
514 |
||
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
515 |
JCTree currentMethod = null; |
10 | 516 |
public void visitMethodDef(JCMethodDecl tree) { |
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
517 |
JCTree previousMethod = currentMethod; |
10 | 518 |
try { |
519 |
currentMethod = tree; |
|
520 |
tree.restype = translate(tree.restype, null); |
|
521 |
tree.typarams = List.nil(); |
|
522 |
tree.params = translateVarDefs(tree.params); |
|
15385 | 523 |
tree.recvparam = translate(tree.recvparam, null); |
10 | 524 |
tree.thrown = translate(tree.thrown, null); |
525 |
tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType()); |
|
526 |
tree.type = erasure(tree.type); |
|
527 |
result = tree; |
|
528 |
} finally { |
|
529 |
currentMethod = previousMethod; |
|
530 |
} |
|
531 |
||
532 |
// Check that we do not introduce a name clash by erasing types. |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25306
diff
changeset
|
533 |
for (Symbol sym : tree.sym.owner.members().getSymbolsByName(tree.name)) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25306
diff
changeset
|
534 |
if (sym != tree.sym && |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25306
diff
changeset
|
535 |
types.isSameType(erasure(sym.type), tree.type)) { |
10 | 536 |
log.error(tree.pos(), |
537 |
"name.clash.same.erasure", tree.sym, |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25306
diff
changeset
|
538 |
sym); |
10 | 539 |
return; |
540 |
} |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
public void visitVarDef(JCVariableDecl tree) { |
|
545 |
tree.vartype = translate(tree.vartype, null); |
|
546 |
tree.init = translate(tree.init, tree.sym.erasure(types)); |
|
547 |
tree.type = erasure(tree.type); |
|
548 |
result = tree; |
|
549 |
} |
|
550 |
||
551 |
public void visitDoLoop(JCDoWhileLoop tree) { |
|
552 |
tree.body = translate(tree.body); |
|
553 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
554 |
result = tree; |
|
555 |
} |
|
556 |
||
557 |
public void visitWhileLoop(JCWhileLoop tree) { |
|
558 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
559 |
tree.body = translate(tree.body); |
|
560 |
result = tree; |
|
561 |
} |
|
562 |
||
563 |
public void visitForLoop(JCForLoop tree) { |
|
564 |
tree.init = translate(tree.init, null); |
|
565 |
if (tree.cond != null) |
|
566 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
567 |
tree.step = translate(tree.step, null); |
|
568 |
tree.body = translate(tree.body); |
|
569 |
result = tree; |
|
570 |
} |
|
571 |
||
572 |
public void visitForeachLoop(JCEnhancedForLoop tree) { |
|
573 |
tree.var = translate(tree.var, null); |
|
574 |
Type iterableType = tree.expr.type; |
|
575 |
tree.expr = translate(tree.expr, erasure(tree.expr.type)); |
|
576 |
if (types.elemtype(tree.expr.type) == null) |
|
577 |
tree.expr.type = iterableType; // preserve type for Lower |
|
578 |
tree.body = translate(tree.body); |
|
579 |
result = tree; |
|
580 |
} |
|
581 |
||
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
582 |
public void visitLambda(JCLambda tree) { |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
583 |
JCTree prevMethod = currentMethod; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
584 |
try { |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
585 |
currentMethod = null; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
586 |
tree.params = translate(tree.params); |
22168
a48fe7ec473d
8030626: java.lang.VerifyError: Bad return type when lambda's body is in parentheses
rfield
parents:
22163
diff
changeset
|
587 |
tree.body = translate(tree.body, tree.body.type==null? null : erasure(tree.body.type)); |
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
588 |
tree.type = erasure(tree.type); |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
589 |
result = tree; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
590 |
} |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
591 |
finally { |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
592 |
currentMethod = prevMethod; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
593 |
} |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
594 |
} |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
595 |
|
10 | 596 |
public void visitSwitch(JCSwitch tree) { |
597 |
Type selsuper = types.supertype(tree.selector.type); |
|
598 |
boolean enumSwitch = selsuper != null && |
|
599 |
selsuper.tsym == syms.enumSym; |
|
600 |
Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType; |
|
601 |
tree.selector = translate(tree.selector, target); |
|
602 |
tree.cases = translateCases(tree.cases); |
|
603 |
result = tree; |
|
604 |
} |
|
605 |
||
606 |
public void visitCase(JCCase tree) { |
|
607 |
tree.pat = translate(tree.pat, null); |
|
608 |
tree.stats = translate(tree.stats); |
|
609 |
result = tree; |
|
610 |
} |
|
611 |
||
612 |
public void visitSynchronized(JCSynchronized tree) { |
|
613 |
tree.lock = translate(tree.lock, erasure(tree.lock.type)); |
|
614 |
tree.body = translate(tree.body); |
|
615 |
result = tree; |
|
616 |
} |
|
617 |
||
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5847
diff
changeset
|
618 |
public void visitTry(JCTry tree) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5847
diff
changeset
|
619 |
tree.resources = translate(tree.resources, syms.autoCloseableType); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5847
diff
changeset
|
620 |
tree.body = translate(tree.body); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5847
diff
changeset
|
621 |
tree.catchers = translateCatchers(tree.catchers); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5847
diff
changeset
|
622 |
tree.finalizer = translate(tree.finalizer); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5847
diff
changeset
|
623 |
result = tree; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5847
diff
changeset
|
624 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5847
diff
changeset
|
625 |
|
10 | 626 |
public void visitConditional(JCConditional tree) { |
627 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
628 |
tree.truepart = translate(tree.truepart, erasure(tree.type)); |
|
629 |
tree.falsepart = translate(tree.falsepart, erasure(tree.type)); |
|
630 |
tree.type = erasure(tree.type); |
|
1356
9f40f16e543b
6500343: compiler generates bad code when translating conditional expressions
mcimadamore
parents:
1260
diff
changeset
|
631 |
result = retype(tree, tree.type, pt); |
10 | 632 |
} |
633 |
||
634 |
public void visitIf(JCIf tree) { |
|
635 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
636 |
tree.thenpart = translate(tree.thenpart); |
|
637 |
tree.elsepart = translate(tree.elsepart); |
|
638 |
result = tree; |
|
639 |
} |
|
640 |
||
641 |
public void visitExec(JCExpressionStatement tree) { |
|
642 |
tree.expr = translate(tree.expr, null); |
|
643 |
result = tree; |
|
644 |
} |
|
645 |
||
646 |
public void visitReturn(JCReturn tree) { |
|
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
647 |
tree.expr = translate(tree.expr, currentMethod != null ? types.erasure(currentMethod.type).getReturnType() : null); |
10 | 648 |
result = tree; |
649 |
} |
|
650 |
||
651 |
public void visitThrow(JCThrow tree) { |
|
652 |
tree.expr = translate(tree.expr, erasure(tree.expr.type)); |
|
653 |
result = tree; |
|
654 |
} |
|
655 |
||
656 |
public void visitAssert(JCAssert tree) { |
|
657 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
658 |
if (tree.detail != null) |
|
659 |
tree.detail = translate(tree.detail, erasure(tree.detail.type)); |
|
660 |
result = tree; |
|
661 |
} |
|
662 |
||
663 |
public void visitApply(JCMethodInvocation tree) { |
|
664 |
tree.meth = translate(tree.meth, null); |
|
665 |
Symbol meth = TreeInfo.symbol(tree.meth); |
|
666 |
Type mt = meth.erasure(types); |
|
667 |
List<Type> argtypes = mt.getParameterTypes(); |
|
668 |
if (allowEnums && |
|
669 |
meth.name==names.init && |
|
670 |
meth.owner == syms.enumSym) |
|
671 |
argtypes = argtypes.tail.tail; |
|
672 |
if (tree.varargsElement != null) |
|
673 |
tree.varargsElement = types.erasure(tree.varargsElement); |
|
674 |
else |
|
19133
aa085efaaa2e
8013179: assertion failure in javac when compiling with -source 1.6 -target 1.6
vromero
parents:
18730
diff
changeset
|
675 |
if (tree.args.length() != argtypes.length()) { |
aa085efaaa2e
8013179: assertion failure in javac when compiling with -source 1.6 -target 1.6
vromero
parents:
18730
diff
changeset
|
676 |
log.error(tree.pos(), |
aa085efaaa2e
8013179: assertion failure in javac when compiling with -source 1.6 -target 1.6
vromero
parents:
18730
diff
changeset
|
677 |
"method.invoked.with.incorrect.number.arguments", |
aa085efaaa2e
8013179: assertion failure in javac when compiling with -source 1.6 -target 1.6
vromero
parents:
18730
diff
changeset
|
678 |
tree.args.length(), argtypes.length()); |
aa085efaaa2e
8013179: assertion failure in javac when compiling with -source 1.6 -target 1.6
vromero
parents:
18730
diff
changeset
|
679 |
} |
10 | 680 |
tree.args = translateArgs(tree.args, argtypes, tree.varargsElement); |
681 |
||
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
682 |
tree.type = types.erasure(tree.type); |
10 | 683 |
// Insert casts of method invocation results as needed. |
684 |
result = retype(tree, mt.getReturnType(), pt); |
|
685 |
} |
|
686 |
||
687 |
public void visitNewClass(JCNewClass tree) { |
|
688 |
if (tree.encl != null) |
|
689 |
tree.encl = translate(tree.encl, erasure(tree.encl.type)); |
|
690 |
tree.clazz = translate(tree.clazz, null); |
|
691 |
if (tree.varargsElement != null) |
|
692 |
tree.varargsElement = types.erasure(tree.varargsElement); |
|
693 |
tree.args = translateArgs( |
|
694 |
tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement); |
|
695 |
tree.def = translate(tree.def, null); |
|
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
696 |
if (tree.constructorType != null) |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
697 |
tree.constructorType = erasure(tree.constructorType); |
10 | 698 |
tree.type = erasure(tree.type); |
699 |
result = tree; |
|
700 |
} |
|
701 |
||
702 |
public void visitNewArray(JCNewArray tree) { |
|
703 |
tree.elemtype = translate(tree.elemtype, null); |
|
704 |
translate(tree.dims, syms.intType); |
|
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4865
diff
changeset
|
705 |
if (tree.type != null) { |
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4865
diff
changeset
|
706 |
tree.elems = translate(tree.elems, erasure(types.elemtype(tree.type))); |
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4865
diff
changeset
|
707 |
tree.type = erasure(tree.type); |
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4865
diff
changeset
|
708 |
} else { |
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4865
diff
changeset
|
709 |
tree.elems = translate(tree.elems, null); |
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4865
diff
changeset
|
710 |
} |
10 | 711 |
|
712 |
result = tree; |
|
713 |
} |
|
714 |
||
715 |
public void visitParens(JCParens tree) { |
|
716 |
tree.expr = translate(tree.expr, pt); |
|
717 |
tree.type = erasure(tree.type); |
|
718 |
result = tree; |
|
719 |
} |
|
720 |
||
721 |
public void visitAssign(JCAssign tree) { |
|
722 |
tree.lhs = translate(tree.lhs, null); |
|
723 |
tree.rhs = translate(tree.rhs, erasure(tree.lhs.type)); |
|
17810
db9f3e9cd760
7053059: VerifyError with double Assignment using a Generic Member of a Superclass
vromero
parents:
16974
diff
changeset
|
724 |
tree.type = erasure(tree.lhs.type); |
db9f3e9cd760
7053059: VerifyError with double Assignment using a Generic Member of a Superclass
vromero
parents:
16974
diff
changeset
|
725 |
result = retype(tree, tree.type, pt); |
10 | 726 |
} |
727 |
||
728 |
public void visitAssignop(JCAssignOp tree) { |
|
3558
56be54d60f36
6869075: regression: javac crashes when compiling compound string assignment with generics
mcimadamore
parents:
3542
diff
changeset
|
729 |
tree.lhs = translate(tree.lhs, null); |
1468
19d266637353
6751514: Unary post-increment with type variables crash javac during lowering
mcimadamore
parents:
1356
diff
changeset
|
730 |
tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head); |
10 | 731 |
tree.type = erasure(tree.type); |
732 |
result = tree; |
|
733 |
} |
|
734 |
||
735 |
public void visitUnary(JCUnary tree) { |
|
736 |
tree.arg = translate(tree.arg, tree.operator.type.getParameterTypes().head); |
|
737 |
result = tree; |
|
738 |
} |
|
739 |
||
740 |
public void visitBinary(JCBinary tree) { |
|
741 |
tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head); |
|
742 |
tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head); |
|
743 |
result = tree; |
|
744 |
} |
|
745 |
||
746 |
public void visitTypeCast(JCTypeCast tree) { |
|
747 |
tree.clazz = translate(tree.clazz, null); |
|
16974
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
748 |
Type originalTarget = tree.type; |
10 | 749 |
tree.type = erasure(tree.type); |
23812
a53aa842f5cf
8015499: javac, Gen is generating extra checkcast instructions in some corner cases
pgovereau
parents:
23127
diff
changeset
|
750 |
tree.expr = translate(tree.expr, erasure(tree.expr.type)); |
16974
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
751 |
if (originalTarget.isCompound()) { |
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
752 |
Type.IntersectionClassType ict = (Type.IntersectionClassType)originalTarget; |
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
753 |
for (Type c : ict.getExplicitComponents()) { |
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
754 |
Type ec = erasure(c); |
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
755 |
if (!types.isSameType(ec, tree.type)) { |
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
756 |
tree.expr = coerce(tree.expr, ec); |
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
757 |
} |
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
758 |
} |
0c9f2cc31ae7
8011392: Missing checkcast when casting to intersection type
mcimadamore
parents:
16968
diff
changeset
|
759 |
} |
10 | 760 |
result = tree; |
761 |
} |
|
762 |
||
763 |
public void visitTypeTest(JCInstanceOf tree) { |
|
764 |
tree.expr = translate(tree.expr, null); |
|
765 |
tree.clazz = translate(tree.clazz, null); |
|
766 |
result = tree; |
|
767 |
} |
|
768 |
||
769 |
public void visitIndexed(JCArrayAccess tree) { |
|
770 |
tree.indexed = translate(tree.indexed, erasure(tree.indexed.type)); |
|
771 |
tree.index = translate(tree.index, syms.intType); |
|
772 |
||
773 |
// Insert casts of indexed expressions as needed. |
|
774 |
result = retype(tree, types.elemtype(tree.indexed.type), pt); |
|
775 |
} |
|
776 |
||
777 |
// There ought to be nothing to rewrite here; |
|
778 |
// we don't generate code. |
|
779 |
public void visitAnnotation(JCAnnotation tree) { |
|
780 |
result = tree; |
|
781 |
} |
|
782 |
||
783 |
public void visitIdent(JCIdent tree) { |
|
784 |
Type et = tree.sym.erasure(types); |
|
785 |
||
786 |
// Map type variables to their bounds. |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
787 |
if (tree.sym.kind == TYP && tree.sym.type.hasTag(TYPEVAR)) { |
10 | 788 |
result = make.at(tree.pos).Type(et); |
789 |
} else |
|
790 |
// Map constants expressions to themselves. |
|
791 |
if (tree.type.constValue() != null) { |
|
792 |
result = tree; |
|
793 |
} |
|
794 |
// Insert casts of variable uses as needed. |
|
795 |
else if (tree.sym.kind == VAR) { |
|
796 |
result = retype(tree, et, pt); |
|
797 |
} |
|
798 |
else { |
|
799 |
tree.type = erasure(tree.type); |
|
800 |
result = tree; |
|
801 |
} |
|
802 |
} |
|
803 |
||
804 |
public void visitSelect(JCFieldAccess tree) { |
|
805 |
Type t = tree.selected.type; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
806 |
while (t.hasTag(TYPEVAR)) |
507
bdd92cc35ac4
6531075: Missing synthetic casts when accessing fields/methods of intersection types including type variables
mcimadamore
parents:
10
diff
changeset
|
807 |
t = t.getUpperBound(); |
bdd92cc35ac4
6531075: Missing synthetic casts when accessing fields/methods of intersection types including type variables
mcimadamore
parents:
10
diff
changeset
|
808 |
if (t.isCompound()) { |
10 | 809 |
if ((tree.sym.flags() & IPROXY) != 0) { |
810 |
tree.sym = ((MethodSymbol)tree.sym). |
|
811 |
implemented((TypeSymbol)tree.sym.owner, types); |
|
812 |
} |
|
7617
6d4424dd7256
6956758: NPE in com.sun.tools.javac.code.Symbol - isSubClass
mcimadamore
parents:
7080
diff
changeset
|
813 |
tree.selected = coerce( |
507
bdd92cc35ac4
6531075: Missing synthetic casts when accessing fields/methods of intersection types including type variables
mcimadamore
parents:
10
diff
changeset
|
814 |
translate(tree.selected, erasure(tree.selected.type)), |
10 | 815 |
erasure(tree.sym.owner.type)); |
816 |
} else |
|
817 |
tree.selected = translate(tree.selected, erasure(t)); |
|
818 |
||
819 |
// Map constants expressions to themselves. |
|
820 |
if (tree.type.constValue() != null) { |
|
821 |
result = tree; |
|
822 |
} |
|
823 |
// Insert casts of variable uses as needed. |
|
824 |
else if (tree.sym.kind == VAR) { |
|
825 |
result = retype(tree, tree.sym.erasure(types), pt); |
|
826 |
} |
|
827 |
else { |
|
828 |
tree.type = erasure(tree.type); |
|
829 |
result = tree; |
|
830 |
} |
|
831 |
} |
|
832 |
||
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
833 |
public void visitReference(JCMemberReference tree) { |
19939
ab90fbc7b74e
8023558: Javac creates invalid bootstrap methods for complex lambda/methodref case
vromero
parents:
19133
diff
changeset
|
834 |
tree.expr = translate(tree.expr, erasure(tree.expr.type)); |
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
835 |
tree.type = erasure(tree.type); |
23127
5aba53c2b9b2
8034048: javac crash with method references plus lambda plus var args
jjg
parents:
22168
diff
changeset
|
836 |
if (tree.varargsElement != null) |
5aba53c2b9b2
8034048: javac crash with method references plus lambda plus var args
jjg
parents:
22168
diff
changeset
|
837 |
tree.varargsElement = erasure(tree.varargsElement); |
14365
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
838 |
result = tree; |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
839 |
} |
20f388573215
8000694: Add generation of lambda implementation code: invokedynamic call, lambda method, adaptor methods
rfield
parents:
14359
diff
changeset
|
840 |
|
10 | 841 |
public void visitTypeArray(JCArrayTypeTree tree) { |
842 |
tree.elemtype = translate(tree.elemtype, null); |
|
843 |
tree.type = erasure(tree.type); |
|
844 |
result = tree; |
|
845 |
} |
|
846 |
||
847 |
/** Visitor method for parameterized types. |
|
848 |
*/ |
|
849 |
public void visitTypeApply(JCTypeApply tree) { |
|
3149 | 850 |
JCTree clazz = translate(tree.clazz, null); |
851 |
result = clazz; |
|
10 | 852 |
} |
853 |
||
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
854 |
public void visitTypeIntersection(JCTypeIntersection tree) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
855 |
tree.bounds = translate(tree.bounds, null); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
856 |
tree.type = erasure(tree.type); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
857 |
result = tree; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
858 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
859 |
|
10 | 860 |
/************************************************************************** |
861 |
* utility methods |
|
862 |
*************************************************************************/ |
|
863 |
||
864 |
private Type erasure(Type t) { |
|
865 |
return types.erasure(t); |
|
866 |
} |
|
867 |
||
868 |
/************************************************************************** |
|
869 |
* main method |
|
870 |
*************************************************************************/ |
|
871 |
||
872 |
private Env<AttrContext> env; |
|
873 |
||
16968 | 874 |
private static final String statePreviousToFlowAssertMsg = |
875 |
"The current compile state [%s] of class %s is previous to FLOW"; |
|
876 |
||
10 | 877 |
void translateClass(ClassSymbol c) { |
878 |
Type st = types.supertype(c.type); |
|
879 |
// process superclass before derived |
|
16968 | 880 |
if (st.hasTag(CLASS)) { |
10 | 881 |
translateClass((ClassSymbol)st.tsym); |
16968 | 882 |
} |
10 | 883 |
|
25007 | 884 |
Env<AttrContext> myEnv = enter.getEnv(c); |
885 |
if (myEnv == null || (c.flags_field & TYPE_TRANSLATED) != 0) { |
|
10 | 886 |
return; |
16968 | 887 |
} |
25007 | 888 |
c.flags_field |= TYPE_TRANSLATED; |
16968 | 889 |
|
890 |
/* The two assertions below are set for early detection of any attempt |
|
891 |
* to translate a class that: |
|
892 |
* |
|
893 |
* 1) has no compile state being it the most outer class. |
|
894 |
* We accept this condition for inner classes. |
|
895 |
* |
|
896 |
* 2) has a compile state which is previous to Flow state. |
|
897 |
*/ |
|
898 |
boolean envHasCompState = compileStates.get(myEnv) != null; |
|
899 |
if (!envHasCompState && c.outermostClass() == c) { |
|
900 |
Assert.error("No info for outermost class: " + myEnv.enclClass.sym); |
|
901 |
} |
|
902 |
||
903 |
if (envHasCompState && |
|
904 |
CompileState.FLOW.isAfter(compileStates.get(myEnv))) { |
|
905 |
Assert.error(String.format(statePreviousToFlowAssertMsg, |
|
906 |
compileStates.get(myEnv), myEnv.enclClass.sym)); |
|
907 |
} |
|
908 |
||
10 | 909 |
Env<AttrContext> oldEnv = env; |
910 |
try { |
|
911 |
env = myEnv; |
|
912 |
// class has not been translated yet |
|
913 |
||
914 |
TreeMaker savedMake = make; |
|
915 |
Type savedPt = pt; |
|
916 |
make = make.forToplevel(env.toplevel); |
|
917 |
pt = null; |
|
918 |
try { |
|
919 |
JCClassDecl tree = (JCClassDecl) env.tree; |
|
920 |
tree.typarams = List.nil(); |
|
921 |
super.visitClassDef(tree); |
|
922 |
make.at(tree.pos); |
|
923 |
if (addBridges) { |
|
22163 | 924 |
ListBuffer<JCTree> bridges = new ListBuffer<>(); |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
925 |
if (allowInterfaceBridges || (tree.sym.flags() & INTERFACE) == 0) { |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
926 |
addBridges(tree.pos(), c, bridges); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
927 |
} |
10 | 928 |
tree.defs = bridges.toList().prependList(tree.defs); |
929 |
} |
|
930 |
tree.type = erasure(tree.type); |
|
931 |
} finally { |
|
932 |
make = savedMake; |
|
933 |
pt = savedPt; |
|
934 |
} |
|
935 |
} finally { |
|
936 |
env = oldEnv; |
|
937 |
} |
|
938 |
} |
|
939 |
||
940 |
/** Translate a toplevel class definition. |
|
941 |
* @param cdef The definition to be translated. |
|
942 |
*/ |
|
943 |
public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) { |
|
944 |
// note that this method does NOT support recursion. |
|
945 |
this.make = make; |
|
946 |
pt = null; |
|
947 |
return translate(cdef, null); |
|
948 |
} |
|
949 |
} |