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