Tue, 02 Nov 2010 12:00:54 +0000
6996415: Override bridges causes compiler-generated code to end up with synthetic infinite loop
Summary: temporarily disable fix for override bridges (6337171)
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
26 package com.sun.tools.javac.comp;
28 import java.util.*;
30 import javax.lang.model.element.ElementKind;
32 import com.sun.tools.javac.code.*;
33 import com.sun.tools.javac.code.Symbol.*;
34 import com.sun.tools.javac.tree.*;
35 import com.sun.tools.javac.tree.JCTree.*;
36 import com.sun.tools.javac.util.*;
37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
38 import com.sun.tools.javac.util.List;
40 import static com.sun.tools.javac.code.Flags.*;
41 import static com.sun.tools.javac.code.Kinds.*;
42 import static com.sun.tools.javac.code.TypeTags.*;
44 /** This pass translates Generic Java to conventional Java.
45 *
46 * <p><b>This is NOT part of any supported API.
47 * If you write code that depends on this, you do so at your own risk.
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>();
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 }
64 private Names names;
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 private final TypeAnnotations typeAnnotations;
74 /**
75 * Flag to indicate whether or not to generate bridge methods.
76 * For pre-Tiger source there is no need for bridge methods, so it
77 * can be skipped to get better performance for -source 1.4 etc.
78 */
79 private final boolean addBridges;
81 protected TransTypes(Context context) {
82 context.put(transTypesKey, this);
83 names = Names.instance(context);
84 log = Log.instance(context);
85 syms = Symtab.instance(context);
86 enter = Enter.instance(context);
87 overridden = new HashMap<MethodSymbol,MethodSymbol>();
88 Source source = Source.instance(context);
89 allowEnums = source.allowEnums();
90 addBridges = source.addBridges();
91 types = Types.instance(context);
92 make = TreeMaker.instance(context);
93 resolve = Resolve.instance(context);
94 typeAnnotations = TypeAnnotations.instance(context);
95 }
97 /** A hashtable mapping bridge methods to the methods they override after
98 * type erasure.
99 */
100 Map<MethodSymbol,MethodSymbol> overridden;
102 /** Construct an attributed tree for a cast of expression to target type,
103 * unless it already has precisely that type.
104 * @param tree The expression tree.
105 * @param target The target type.
106 */
107 JCExpression cast(JCExpression tree, Type target) {
108 int oldpos = make.pos;
109 make.at(tree.pos);
110 if (!types.isSameType(tree.type, target)) {
111 if (!resolve.isAccessible(env, target.tsym))
112 resolve.logAccessError(env, tree, target);
113 tree = make.TypeCast(make.Type(target), tree).setType(target);
114 }
115 make.pos = oldpos;
116 return tree;
117 }
119 /** Construct an attributed tree to coerce an expression to some erased
120 * target type, unless the expression is already assignable to that type.
121 * If target type is a constant type, use its base type instead.
122 * @param tree The expression tree.
123 * @param target The target type.
124 */
125 JCExpression coerce(JCExpression tree, Type target) {
126 Type btarget = target.baseType();
127 if (tree.type.isPrimitive() == target.isPrimitive()) {
128 return types.isAssignable(tree.type, btarget, Warner.noWarnings)
129 ? tree
130 : cast(tree, btarget);
131 }
132 return tree;
133 }
135 /** Given an erased reference type, assume this type as the tree's type.
136 * Then, coerce to some given target type unless target type is null.
137 * This operation is used in situations like the following:
138 *
139 * class Cell<A> { A value; }
140 * ...
141 * Cell<Integer> cell;
142 * Integer x = cell.value;
143 *
144 * Since the erasure of Cell.value is Object, but the type
145 * of cell.value in the assignment is Integer, we need to
146 * adjust the original type of cell.value to Object, and insert
147 * a cast to Integer. That is, the last assignment becomes:
148 *
149 * Integer x = (Integer)cell.value;
150 *
151 * @param tree The expression tree whose type might need adjustment.
152 * @param erasedType The expression's type after erasure.
153 * @param target The target type, which is usually the erasure of the
154 * expression's original type.
155 */
156 JCExpression retype(JCExpression tree, Type erasedType, Type target) {
157 // System.err.println("retype " + tree + " to " + erasedType);//DEBUG
158 if (erasedType.tag > lastBaseTag) {
159 if (target != null && target.isPrimitive())
160 target = erasure(tree.type);
161 tree.type = erasedType;
162 if (target != null) return coerce(tree, target);
163 }
164 return tree;
165 }
167 /** Translate method argument list, casting each argument
168 * to its corresponding type in a list of target types.
169 * @param _args The method argument list.
170 * @param parameters The list of target types.
171 * @param varargsElement The erasure of the varargs element type,
172 * or null if translating a non-varargs invocation
173 */
174 <T extends JCTree> List<T> translateArgs(List<T> _args,
175 List<Type> parameters,
176 Type varargsElement) {
177 if (parameters.isEmpty()) return _args;
178 List<T> args = _args;
179 while (parameters.tail.nonEmpty()) {
180 args.head = translate(args.head, parameters.head);
181 args = args.tail;
182 parameters = parameters.tail;
183 }
184 Type parameter = parameters.head;
185 assert varargsElement != null || args.length() == 1;
186 if (varargsElement != null) {
187 while (args.nonEmpty()) {
188 args.head = translate(args.head, varargsElement);
189 args = args.tail;
190 }
191 } else {
192 args.head = translate(args.head, parameter);
193 }
194 return _args;
195 }
197 /** Add a bridge definition and enter corresponding method symbol in
198 * local scope of origin.
199 *
200 * @param pos The source code position to be used for the definition.
201 * @param meth The method for which a bridge needs to be added
202 * @param impl That method's implementation (possibly the method itself)
203 * @param origin The class to which the bridge will be added
204 * @param hypothetical
205 * True if the bridge method is not strictly necessary in the
206 * binary, but is represented in the symbol table to detect
207 * erasure clashes.
208 * @param bridges The list buffer to which the bridge will be added
209 */
210 void addBridge(DiagnosticPosition pos,
211 MethodSymbol meth,
212 MethodSymbol impl,
213 ClassSymbol origin,
214 boolean hypothetical,
215 ListBuffer<JCTree> bridges) {
216 make.at(pos);
217 Type origType = types.memberType(origin.type, meth);
218 Type origErasure = erasure(origType);
220 // Create a bridge method symbol and a bridge definition without a body.
221 Type bridgeType = meth.erasure(types);
222 long flags = impl.flags() & AccessFlags | SYNTHETIC | BRIDGE;
223 if (hypothetical) flags |= HYPOTHETICAL;
224 MethodSymbol bridge = new MethodSymbol(flags,
225 meth.name,
226 bridgeType,
227 origin);
228 if (!hypothetical) {
229 JCMethodDecl md = make.MethodDef(bridge, null);
231 // The bridge calls this.impl(..), if we have an implementation
232 // in the current class, super.impl(...) otherwise.
233 JCExpression receiver = (impl.owner == origin)
234 ? make.This(origin.erasure(types))
235 : make.Super(types.supertype(origin.type).tsym.erasure(types), origin);
237 // The type returned from the original method.
238 Type calltype = erasure(impl.type.getReturnType());
240 // Construct a call of this.impl(params), or super.impl(params),
241 // casting params and possibly results as needed.
242 JCExpression call =
243 make.Apply(
244 null,
245 make.Select(receiver, impl).setType(calltype),
246 translateArgs(make.Idents(md.params), origErasure.getParameterTypes(), null))
247 .setType(calltype);
248 JCStatement stat = (origErasure.getReturnType().tag == VOID)
249 ? make.Exec(call)
250 : make.Return(coerce(call, bridgeType.getReturnType()));
251 md.body = make.Block(0, List.of(stat));
253 // Add bridge to `bridges' buffer
254 bridges.append(md);
255 }
257 // Add bridge to scope of enclosing class and `overridden' table.
258 origin.members().enter(bridge);
259 overridden.put(bridge, meth);
260 }
262 /** Add bridge if given symbol is a non-private, non-static member
263 * of the given class, which is either defined in the class or non-final
264 * inherited, and one of the two following conditions holds:
265 * 1. The method's type changes in the given class, as compared to the
266 * class where the symbol was defined, (in this case
267 * we have extended a parameterized class with non-trivial parameters).
268 * 2. The method has an implementation with a different erased return type.
269 * (in this case we have used co-variant returns).
270 * If a bridge already exists in some other class, no new bridge is added.
271 * Instead, it is checked that the bridge symbol overrides the method symbol.
272 * (Spec ???).
273 * todo: what about bridges for privates???
274 *
275 * @param pos The source code position to be used for the definition.
276 * @param sym The symbol for which a bridge might have to be added.
277 * @param origin The class in which the bridge would go.
278 * @param bridges The list buffer to which the bridge would be added.
279 */
280 void addBridgeIfNeeded(DiagnosticPosition pos,
281 Symbol sym,
282 ClassSymbol origin,
283 ListBuffer<JCTree> bridges) {
284 if (sym.kind == MTH &&
285 sym.name != names.init &&
286 (sym.flags() & (PRIVATE | STATIC)) == 0 &&
287 (sym.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC &&
288 sym.isMemberOf(origin, types))
289 {
290 MethodSymbol meth = (MethodSymbol)sym;
291 MethodSymbol bridge = meth.binaryImplementation(origin, types);
292 MethodSymbol impl = meth.implementation(origin, types, true, overrideBridgeFilter);
293 if (bridge == null ||
294 bridge == meth ||
295 (impl != null && !bridge.owner.isSubClass(impl.owner, types))) {
296 // No bridge was added yet.
297 if (impl != null && isBridgeNeeded(meth, impl, origin.type)) {
298 addBridge(pos, meth, impl, origin, bridge==impl, bridges);
299 } else if (impl == meth
300 && impl.owner != origin
301 && (impl.flags() & FINAL) == 0
302 && (meth.flags() & (ABSTRACT|PUBLIC)) == PUBLIC
303 && (origin.flags() & PUBLIC) > (impl.owner.flags() & PUBLIC)) {
304 // this is to work around a horrible but permanent
305 // reflection design error.
306 addBridge(pos, meth, impl, origin, false, bridges);
307 }
308 } else if ((bridge.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) == SYNTHETIC) {
309 MethodSymbol other = overridden.get(bridge);
310 if (other != null && other != meth) {
311 if (impl == null || !impl.overrides(other, origin, types, true)) {
312 // Bridge for other symbol pair was added
313 log.error(pos, "name.clash.same.erasure.no.override",
314 other, other.location(origin.type, types),
315 meth, meth.location(origin.type, types));
316 }
317 }
318 } else if (!bridge.overrides(meth, origin, types, true)) {
319 // Accidental binary override without source override.
320 if (bridge.owner == origin ||
321 types.asSuper(bridge.owner.type, meth.owner) == null)
322 // Don't diagnose the problem if it would already
323 // have been reported in the superclass
324 log.error(pos, "name.clash.same.erasure.no.override",
325 bridge, bridge.location(origin.type, types),
326 meth, meth.location(origin.type, types));
327 }
328 }
329 }
330 // where
331 Filter<Symbol> overrideBridgeFilter = new Filter<Symbol>() {
332 public boolean accepts(Symbol s) {
333 return (s.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC;
334 }
335 };
336 /**
337 * @param method The symbol for which a bridge might have to be added
338 * @param impl The implementation of method
339 * @param dest The type in which the bridge would go
340 */
341 private boolean isBridgeNeeded(MethodSymbol method,
342 MethodSymbol impl,
343 Type dest) {
344 if (impl != method) {
345 // If either method or impl have different erasures as
346 // members of dest, a bridge is needed.
347 Type method_erasure = method.erasure(types);
348 if (!isSameMemberWhenErased(dest, method, method_erasure))
349 return true;
350 Type impl_erasure = impl.erasure(types);
351 if (!isSameMemberWhenErased(dest, impl, impl_erasure))
352 return true;
354 // If the erasure of the return type is different, a
355 // bridge is needed.
356 return !types.isSameType(impl_erasure.getReturnType(),
357 method_erasure.getReturnType());
358 } else {
359 // method and impl are the same...
360 if ((method.flags() & ABSTRACT) != 0) {
361 // ...and abstract so a bridge is not needed.
362 // Concrete subclasses will bridge as needed.
363 return false;
364 }
366 // The erasure of the return type is always the same
367 // for the same symbol. Reducing the three tests in
368 // the other branch to just one:
369 return !isSameMemberWhenErased(dest, method, method.erasure(types));
370 }
371 }
372 /**
373 * Lookup the method as a member of the type. Compare the
374 * erasures.
375 * @param type the class where to look for the method
376 * @param method the method to look for in class
377 * @param erasure the erasure of method
378 */
379 private boolean isSameMemberWhenErased(Type type,
380 MethodSymbol method,
381 Type erasure) {
382 return types.isSameType(erasure(types.memberType(type, method)),
383 erasure);
384 }
386 void addBridges(DiagnosticPosition pos,
387 TypeSymbol i,
388 ClassSymbol origin,
389 ListBuffer<JCTree> bridges) {
390 for (Scope.Entry e = i.members().elems; e != null; e = e.sibling)
391 addBridgeIfNeeded(pos, e.sym, origin, bridges);
392 for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail)
393 addBridges(pos, l.head.tsym, origin, bridges);
394 }
396 /** Add all necessary bridges to some class appending them to list buffer.
397 * @param pos The source code position to be used for the bridges.
398 * @param origin The class in which the bridges go.
399 * @param bridges The list buffer to which the bridges are added.
400 */
401 void addBridges(DiagnosticPosition pos, ClassSymbol origin, ListBuffer<JCTree> bridges) {
402 Type st = types.supertype(origin.type);
403 while (st.tag == CLASS) {
404 // if (isSpecialization(st))
405 addBridges(pos, st.tsym, origin, bridges);
406 st = types.supertype(st);
407 }
408 for (List<Type> l = types.interfaces(origin.type); l.nonEmpty(); l = l.tail)
409 // if (isSpecialization(l.head))
410 addBridges(pos, l.head.tsym, origin, bridges);
411 }
413 /* ************************************************************************
414 * Visitor methods
415 *************************************************************************/
417 /** Visitor argument: proto-type.
418 */
419 private Type pt;
421 /** Visitor method: perform a type translation on tree.
422 */
423 public <T extends JCTree> T translate(T tree, Type pt) {
424 Type prevPt = this.pt;
425 try {
426 this.pt = pt;
427 return translate(tree);
428 } finally {
429 this.pt = prevPt;
430 }
431 }
433 /** Visitor method: perform a type translation on list of trees.
434 */
435 public <T extends JCTree> List<T> translate(List<T> trees, Type pt) {
436 Type prevPt = this.pt;
437 List<T> res;
438 try {
439 this.pt = pt;
440 res = translate(trees);
441 } finally {
442 this.pt = prevPt;
443 }
444 return res;
445 }
447 public void visitClassDef(JCClassDecl tree) {
448 typeAnnotations.taFillAndLift(tree, true);
449 translateClass(tree.sym);
450 result = tree;
451 }
453 JCMethodDecl currentMethod = null;
454 public void visitMethodDef(JCMethodDecl tree) {
455 tree.sym.typeAnnotations = tree.sym.typeAnnotations;
456 JCMethodDecl previousMethod = currentMethod;
457 try {
458 currentMethod = tree;
459 tree.restype = translate(tree.restype, null);
460 tree.typarams = List.nil();
461 tree.params = translateVarDefs(tree.params);
462 tree.thrown = translate(tree.thrown, null);
463 tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType());
464 tree.type = erasure(tree.type);
465 result = tree;
466 } finally {
467 currentMethod = previousMethod;
468 }
470 // Check that we do not introduce a name clash by erasing types.
471 for (Scope.Entry e = tree.sym.owner.members().lookup(tree.name);
472 e.sym != null;
473 e = e.next()) {
474 if (e.sym != tree.sym &&
475 types.isSameType(erasure(e.sym.type), tree.type)) {
476 log.error(tree.pos(),
477 "name.clash.same.erasure", tree.sym,
478 e.sym);
479 return;
480 }
481 }
482 }
484 public void visitVarDef(JCVariableDecl tree) {
485 tree.vartype = translate(tree.vartype, null);
486 tree.init = translate(tree.init, tree.sym.erasure(types));
487 tree.type = erasure(tree.type);
488 result = tree;
489 }
491 public void visitDoLoop(JCDoWhileLoop tree) {
492 tree.body = translate(tree.body);
493 tree.cond = translate(tree.cond, syms.booleanType);
494 result = tree;
495 }
497 public void visitWhileLoop(JCWhileLoop tree) {
498 tree.cond = translate(tree.cond, syms.booleanType);
499 tree.body = translate(tree.body);
500 result = tree;
501 }
503 public void visitForLoop(JCForLoop tree) {
504 tree.init = translate(tree.init, null);
505 if (tree.cond != null)
506 tree.cond = translate(tree.cond, syms.booleanType);
507 tree.step = translate(tree.step, null);
508 tree.body = translate(tree.body);
509 result = tree;
510 }
512 public void visitForeachLoop(JCEnhancedForLoop tree) {
513 tree.var = translate(tree.var, null);
514 Type iterableType = tree.expr.type;
515 tree.expr = translate(tree.expr, erasure(tree.expr.type));
516 if (types.elemtype(tree.expr.type) == null)
517 tree.expr.type = iterableType; // preserve type for Lower
518 tree.body = translate(tree.body);
519 result = tree;
520 }
522 public void visitSwitch(JCSwitch tree) {
523 Type selsuper = types.supertype(tree.selector.type);
524 boolean enumSwitch = selsuper != null &&
525 selsuper.tsym == syms.enumSym;
526 Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType;
527 tree.selector = translate(tree.selector, target);
528 tree.cases = translateCases(tree.cases);
529 result = tree;
530 }
532 public void visitCase(JCCase tree) {
533 tree.pat = translate(tree.pat, null);
534 tree.stats = translate(tree.stats);
535 result = tree;
536 }
538 public void visitSynchronized(JCSynchronized tree) {
539 tree.lock = translate(tree.lock, erasure(tree.lock.type));
540 tree.body = translate(tree.body);
541 result = tree;
542 }
544 public void visitTry(JCTry tree) {
545 tree.resources = translate(tree.resources, syms.autoCloseableType);
546 tree.body = translate(tree.body);
547 tree.catchers = translateCatchers(tree.catchers);
548 tree.finalizer = translate(tree.finalizer);
549 result = tree;
550 }
552 public void visitConditional(JCConditional tree) {
553 tree.cond = translate(tree.cond, syms.booleanType);
554 tree.truepart = translate(tree.truepart, erasure(tree.type));
555 tree.falsepart = translate(tree.falsepart, erasure(tree.type));
556 tree.type = erasure(tree.type);
557 result = retype(tree, tree.type, pt);
558 }
560 public void visitIf(JCIf tree) {
561 tree.cond = translate(tree.cond, syms.booleanType);
562 tree.thenpart = translate(tree.thenpart);
563 tree.elsepart = translate(tree.elsepart);
564 result = tree;
565 }
567 public void visitExec(JCExpressionStatement tree) {
568 tree.expr = translate(tree.expr, null);
569 result = tree;
570 }
572 public void visitReturn(JCReturn tree) {
573 tree.expr = translate(tree.expr, currentMethod.sym.erasure(types).getReturnType());
574 result = tree;
575 }
577 public void visitThrow(JCThrow tree) {
578 tree.expr = translate(tree.expr, erasure(tree.expr.type));
579 result = tree;
580 }
582 public void visitAssert(JCAssert tree) {
583 tree.cond = translate(tree.cond, syms.booleanType);
584 if (tree.detail != null)
585 tree.detail = translate(tree.detail, erasure(tree.detail.type));
586 result = tree;
587 }
589 public void visitApply(JCMethodInvocation tree) {
590 tree.meth = translate(tree.meth, null);
591 Symbol meth = TreeInfo.symbol(tree.meth);
592 Type mt = meth.erasure(types);
593 List<Type> argtypes = mt.getParameterTypes();
594 if (allowEnums &&
595 meth.name==names.init &&
596 meth.owner == syms.enumSym)
597 argtypes = argtypes.tail.tail;
598 if (tree.varargsElement != null)
599 tree.varargsElement = types.erasure(tree.varargsElement);
600 else
601 assert tree.args.length() == argtypes.length();
602 tree.args = translateArgs(tree.args, argtypes, tree.varargsElement);
604 // Insert casts of method invocation results as needed.
605 result = retype(tree, mt.getReturnType(), pt);
606 }
608 public void visitNewClass(JCNewClass tree) {
609 if (tree.encl != null)
610 tree.encl = translate(tree.encl, erasure(tree.encl.type));
611 tree.clazz = translate(tree.clazz, null);
612 if (tree.varargsElement != null)
613 tree.varargsElement = types.erasure(tree.varargsElement);
614 tree.args = translateArgs(
615 tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement);
616 tree.def = translate(tree.def, null);
617 tree.type = erasure(tree.type);
618 result = tree;
619 }
621 public void visitNewArray(JCNewArray tree) {
622 tree.elemtype = translate(tree.elemtype, null);
623 translate(tree.dims, syms.intType);
624 if (tree.type != null) {
625 tree.elems = translate(tree.elems, erasure(types.elemtype(tree.type)));
626 tree.type = erasure(tree.type);
627 } else {
628 tree.elems = translate(tree.elems, null);
629 }
631 result = tree;
632 }
634 public void visitParens(JCParens tree) {
635 tree.expr = translate(tree.expr, pt);
636 tree.type = erasure(tree.type);
637 result = tree;
638 }
640 public void visitAssign(JCAssign tree) {
641 tree.lhs = translate(tree.lhs, null);
642 tree.rhs = translate(tree.rhs, erasure(tree.lhs.type));
643 tree.type = erasure(tree.type);
644 result = tree;
645 }
647 public void visitAssignop(JCAssignOp tree) {
648 tree.lhs = translate(tree.lhs, null);
649 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
650 tree.type = erasure(tree.type);
651 result = tree;
652 }
654 public void visitUnary(JCUnary tree) {
655 tree.arg = translate(tree.arg, tree.operator.type.getParameterTypes().head);
656 result = tree;
657 }
659 public void visitBinary(JCBinary tree) {
660 tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head);
661 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
662 result = tree;
663 }
665 public void visitTypeCast(JCTypeCast tree) {
666 tree.clazz = translate(tree.clazz, null);
667 tree.type = erasure(tree.type);
668 tree.expr = translate(tree.expr, tree.type);
669 result = tree;
670 }
672 public void visitTypeTest(JCInstanceOf tree) {
673 tree.expr = translate(tree.expr, null);
674 tree.clazz = translate(tree.clazz, null);
675 result = tree;
676 }
678 public void visitIndexed(JCArrayAccess tree) {
679 tree.indexed = translate(tree.indexed, erasure(tree.indexed.type));
680 tree.index = translate(tree.index, syms.intType);
682 // Insert casts of indexed expressions as needed.
683 result = retype(tree, types.elemtype(tree.indexed.type), pt);
684 }
686 // There ought to be nothing to rewrite here;
687 // we don't generate code.
688 public void visitAnnotation(JCAnnotation tree) {
689 result = tree;
690 }
692 public void visitIdent(JCIdent tree) {
693 Type et = tree.sym.erasure(types);
695 // Map type variables to their bounds.
696 if (tree.sym.kind == TYP && tree.sym.type.tag == TYPEVAR) {
697 result = make.at(tree.pos).Type(et);
698 } else
699 // Map constants expressions to themselves.
700 if (tree.type.constValue() != null) {
701 result = tree;
702 }
703 // Insert casts of variable uses as needed.
704 else if (tree.sym.kind == VAR) {
705 result = retype(tree, et, pt);
706 }
707 else {
708 tree.type = erasure(tree.type);
709 result = tree;
710 }
711 }
713 public void visitSelect(JCFieldAccess tree) {
714 Type t = tree.selected.type;
715 while (t.tag == TYPEVAR)
716 t = t.getUpperBound();
717 if (t.isCompound()) {
718 if ((tree.sym.flags() & IPROXY) != 0) {
719 tree.sym = ((MethodSymbol)tree.sym).
720 implemented((TypeSymbol)tree.sym.owner, types);
721 }
722 tree.selected = cast(
723 translate(tree.selected, erasure(tree.selected.type)),
724 erasure(tree.sym.owner.type));
725 } else
726 tree.selected = translate(tree.selected, erasure(t));
728 // Map constants expressions to themselves.
729 if (tree.type.constValue() != null) {
730 result = tree;
731 }
732 // Insert casts of variable uses as needed.
733 else if (tree.sym.kind == VAR) {
734 result = retype(tree, tree.sym.erasure(types), pt);
735 }
736 else {
737 tree.type = erasure(tree.type);
738 result = tree;
739 }
740 }
742 public void visitTypeArray(JCArrayTypeTree tree) {
743 tree.elemtype = translate(tree.elemtype, null);
744 tree.type = erasure(tree.type);
745 result = tree;
746 }
748 /** Visitor method for parameterized types.
749 */
750 public void visitTypeApply(JCTypeApply tree) {
751 JCTree clazz = translate(tree.clazz, null);
752 result = clazz;
753 }
755 /**************************************************************************
756 * utility methods
757 *************************************************************************/
759 private Type erasure(Type t) {
760 return types.erasure(t);
761 }
763 private boolean boundsRestricted(ClassSymbol c) {
764 Type st = types.supertype(c.type);
765 if (st.isParameterized()) {
766 List<Type> actuals = st.allparams();
767 List<Type> formals = st.tsym.type.allparams();
768 while (!actuals.isEmpty() && !formals.isEmpty()) {
769 Type actual = actuals.head;
770 Type formal = formals.head;
772 if (!types.isSameType(types.erasure(actual),
773 types.erasure(formal)))
774 return true;
776 actuals = actuals.tail;
777 formals = formals.tail;
778 }
779 }
780 return false;
781 }
783 private List<JCTree> addOverrideBridgesIfNeeded(DiagnosticPosition pos,
784 final ClassSymbol c) {
785 ListBuffer<JCTree> buf = ListBuffer.lb();
786 if (c.isInterface() || !boundsRestricted(c))
787 return buf.toList();
788 Type t = types.supertype(c.type);
789 Scope s = t.tsym.members();
790 if (s.elems != null) {
791 for (Symbol sym : s.getElements(new NeedsOverridBridgeFilter(c))) {
793 MethodSymbol m = (MethodSymbol)sym;
794 MethodSymbol member = (MethodSymbol)m.asMemberOf(c.type, types);
795 MethodSymbol impl = m.implementation(c, types, false);
797 if ((impl == null || impl.owner != c) &&
798 !types.isSameType(member.erasure(types), m.erasure(types))) {
799 addOverrideBridges(pos, m, member, c, buf);
800 }
801 }
802 }
803 return buf.toList();
804 }
805 // where
806 class NeedsOverridBridgeFilter implements Filter<Symbol> {
808 ClassSymbol c;
810 NeedsOverridBridgeFilter(ClassSymbol c) {
811 this.c = c;
812 }
813 public boolean accepts(Symbol s) {
814 return s.kind == MTH &&
815 !s.isConstructor() &&
816 s.isInheritedIn(c, types) &&
817 (s.flags() & FINAL) == 0 &&
818 (s.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC;
819 }
820 }
822 private void addOverrideBridges(DiagnosticPosition pos,
823 MethodSymbol impl,
824 MethodSymbol member,
825 ClassSymbol c,
826 ListBuffer<JCTree> bridges) {
827 Type implErasure = impl.erasure(types);
828 long flags = (impl.flags() & AccessFlags) | SYNTHETIC | BRIDGE | OVERRIDE_BRIDGE;
829 member = new MethodSymbol(flags, member.name, member.type, c);
830 JCMethodDecl md = make.MethodDef(member, null);
831 JCExpression receiver = make.Super(types.supertype(c.type).tsym.erasure(types), c);
832 Type calltype = erasure(impl.type.getReturnType());
833 JCExpression call =
834 make.Apply(null,
835 make.Select(receiver, impl).setType(calltype),
836 translateArgs(make.Idents(md.params),
837 implErasure.getParameterTypes(), null))
838 .setType(calltype);
839 JCStatement stat = (member.getReturnType().tag == VOID)
840 ? make.Exec(call)
841 : make.Return(coerce(call, member.erasure(types).getReturnType()));
842 md.body = make.Block(0, List.of(stat));
843 c.members().enter(member);
844 bridges.append(md);
845 }
847 /**************************************************************************
848 * main method
849 *************************************************************************/
851 private Env<AttrContext> env;
853 void translateClass(ClassSymbol c) {
854 Type st = types.supertype(c.type);
856 // process superclass before derived
857 if (st.tag == CLASS)
858 translateClass((ClassSymbol)st.tsym);
860 Env<AttrContext> myEnv = enter.typeEnvs.remove(c);
861 if (myEnv == null)
862 return;
863 Env<AttrContext> oldEnv = env;
864 try {
865 env = myEnv;
866 // class has not been translated yet
868 TreeMaker savedMake = make;
869 Type savedPt = pt;
870 make = make.forToplevel(env.toplevel);
871 pt = null;
872 try {
873 JCClassDecl tree = (JCClassDecl) env.tree;
874 tree.typarams = List.nil();
875 super.visitClassDef(tree);
876 make.at(tree.pos);
877 if (addBridges) {
878 ListBuffer<JCTree> bridges = new ListBuffer<JCTree>();
879 if (false) //see CR: 6996415
880 bridges.appendList(addOverrideBridgesIfNeeded(tree, c));
881 if ((tree.sym.flags() & INTERFACE) == 0)
882 addBridges(tree.pos(), tree.sym, bridges);
883 tree.defs = bridges.toList().prependList(tree.defs);
884 }
885 tree.type = erasure(tree.type);
886 } finally {
887 make = savedMake;
888 pt = savedPt;
889 }
890 } finally {
891 env = oldEnv;
892 }
893 }
895 /** Translate a toplevel class definition.
896 * @param cdef The definition to be translated.
897 */
898 public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) {
899 // note that this method does NOT support recursion.
900 this.make = make;
901 pt = null;
902 return translate(cdef, null);
903 }
904 }