Mon, 21 Jan 2013 20:13:56 +0000
8005244: Implement overload resolution as per latest spec EDR
Summary: Add support for stuck expressions and provisional applicability
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1999, 2012, 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 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;
38 import static com.sun.tools.javac.code.Flags.*;
39 import static com.sun.tools.javac.code.Kinds.*;
40 import static com.sun.tools.javac.code.TypeTag.CLASS;
41 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
42 import static com.sun.tools.javac.code.TypeTag.VOID;
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;
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;
80 protected TransTypes(Context context) {
81 context.put(transTypesKey, this);
82 names = Names.instance(context);
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 }
95 /** A hashtable mapping bridge methods to the methods they override after
96 * type erasure.
97 */
98 Map<MethodSymbol,MethodSymbol> overridden;
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))
110 resolve.logAccessErrorInternal(env, tree, target);
111 tree = make.TypeCast(make.Type(target), tree).setType(target);
112 }
113 make.pos = oldpos;
114 return tree;
115 }
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 */
123 public JCExpression coerce(Env<AttrContext> env, JCExpression tree, Type target) {
124 Env<AttrContext> prevEnv = this.env;
125 try {
126 this.env = env;
127 return coerce(tree, target);
128 }
129 finally {
130 this.env = prevEnv;
131 }
132 }
133 JCExpression coerce(JCExpression tree, Type target) {
134 Type btarget = target.baseType();
135 if (tree.type.isPrimitive() == target.isPrimitive()) {
136 return types.isAssignable(tree.type, btarget, types.noWarnings)
137 ? tree
138 : cast(tree, btarget);
139 }
140 return tree;
141 }
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 *
147 * <pre>{@code
148 * class Cell<A> { A value; }
149 * ...
150 * Cell<Integer> cell;
151 * Integer x = cell.value;
152 * }</pre>
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 *
159 * <pre>{@code
160 * Integer x = (Integer)cell.value;
161 * }</pre>
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
170 if (!erasedType.isPrimitive()) {
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 }
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;
197 Assert.check(varargsElement != null || args.length() == 1);
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 }
209 public <T extends JCTree> List<T> translateArgs(List<T> _args,
210 List<Type> parameters,
211 Type varargsElement,
212 Env<AttrContext> localEnv) {
213 Env<AttrContext> prevEnv = env;
214 try {
215 env = localEnv;
216 return translateArgs(_args, parameters, varargsElement);
217 }
218 finally {
219 env = prevEnv;
220 }
221 }
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);
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);
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);
263 // The type returned from the original method.
264 Type calltype = erasure(impl.type.getReturnType());
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);
274 JCStatement stat = (origErasure.getReturnType().hasTag(VOID))
275 ? make.Exec(call)
276 : make.Return(coerce(call, bridgeType.getReturnType()));
277 md.body = make.Block(0, List.of(stat));
279 // Add bridge to `bridges' buffer
280 bridges.append(md);
281 }
283 // Add bridge to scope of enclosing class and `overridden' table.
284 origin.members().enter(bridge);
285 overridden.put(bridge, meth);
286 }
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 &&
312 (sym.flags() & (PRIVATE | STATIC)) == 0 &&
313 (sym.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC &&
314 sym.isMemberOf(origin, types))
315 {
316 MethodSymbol meth = (MethodSymbol)sym;
317 MethodSymbol bridge = meth.binaryImplementation(origin, types);
318 MethodSymbol impl = meth.implementation(origin, types, true, overrideBridgeFilter);
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 }
334 } else if ((bridge.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) == SYNTHETIC) {
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
357 Filter<Symbol> overrideBridgeFilter = new Filter<Symbol>() {
358 public boolean accepts(Symbol s) {
359 return (s.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC;
360 }
361 };
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;
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 }
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 }
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 }
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);
429 while (st.hasTag(CLASS)) {
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 }
439 /* ************************************************************************
440 * Visitor methods
441 *************************************************************************/
443 /** Visitor argument: proto-type.
444 */
445 private Type pt;
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 }
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 }
473 public void visitClassDef(JCClassDecl tree) {
474 translateClass(tree.sym);
475 result = tree;
476 }
478 JCTree currentMethod = null;
479 public void visitMethodDef(JCMethodDecl tree) {
480 JCTree previousMethod = currentMethod;
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 }
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 }
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 }
515 public void visitDoLoop(JCDoWhileLoop tree) {
516 tree.body = translate(tree.body);
517 tree.cond = translate(tree.cond, syms.booleanType);
518 result = tree;
519 }
521 public void visitWhileLoop(JCWhileLoop tree) {
522 tree.cond = translate(tree.cond, syms.booleanType);
523 tree.body = translate(tree.body);
524 result = tree;
525 }
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 }
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 }
546 public void visitLambda(JCLambda tree) {
547 JCTree prevMethod = currentMethod;
548 try {
549 currentMethod = null;
550 tree.params = translate(tree.params);
551 tree.body = translate(tree.body, null);
552 tree.type = erasure(tree.type);
553 result = tree;
554 }
555 finally {
556 currentMethod = prevMethod;
557 }
558 }
560 public void visitSwitch(JCSwitch tree) {
561 Type selsuper = types.supertype(tree.selector.type);
562 boolean enumSwitch = selsuper != null &&
563 selsuper.tsym == syms.enumSym;
564 Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType;
565 tree.selector = translate(tree.selector, target);
566 tree.cases = translateCases(tree.cases);
567 result = tree;
568 }
570 public void visitCase(JCCase tree) {
571 tree.pat = translate(tree.pat, null);
572 tree.stats = translate(tree.stats);
573 result = tree;
574 }
576 public void visitSynchronized(JCSynchronized tree) {
577 tree.lock = translate(tree.lock, erasure(tree.lock.type));
578 tree.body = translate(tree.body);
579 result = tree;
580 }
582 public void visitTry(JCTry tree) {
583 tree.resources = translate(tree.resources, syms.autoCloseableType);
584 tree.body = translate(tree.body);
585 tree.catchers = translateCatchers(tree.catchers);
586 tree.finalizer = translate(tree.finalizer);
587 result = tree;
588 }
590 public void visitConditional(JCConditional tree) {
591 tree.cond = translate(tree.cond, syms.booleanType);
592 tree.truepart = translate(tree.truepart, erasure(tree.type));
593 tree.falsepart = translate(tree.falsepart, erasure(tree.type));
594 tree.type = erasure(tree.type);
595 result = retype(tree, tree.type, pt);
596 }
598 public void visitIf(JCIf tree) {
599 tree.cond = translate(tree.cond, syms.booleanType);
600 tree.thenpart = translate(tree.thenpart);
601 tree.elsepart = translate(tree.elsepart);
602 result = tree;
603 }
605 public void visitExec(JCExpressionStatement tree) {
606 tree.expr = translate(tree.expr, null);
607 result = tree;
608 }
610 public void visitReturn(JCReturn tree) {
611 tree.expr = translate(tree.expr, currentMethod != null ? types.erasure(currentMethod.type).getReturnType() : null);
612 result = tree;
613 }
615 public void visitThrow(JCThrow tree) {
616 tree.expr = translate(tree.expr, erasure(tree.expr.type));
617 result = tree;
618 }
620 public void visitAssert(JCAssert tree) {
621 tree.cond = translate(tree.cond, syms.booleanType);
622 if (tree.detail != null)
623 tree.detail = translate(tree.detail, erasure(tree.detail.type));
624 result = tree;
625 }
627 public void visitApply(JCMethodInvocation tree) {
628 tree.meth = translate(tree.meth, null);
629 Symbol meth = TreeInfo.symbol(tree.meth);
630 Type mt = meth.erasure(types);
631 List<Type> argtypes = mt.getParameterTypes();
632 if (allowEnums &&
633 meth.name==names.init &&
634 meth.owner == syms.enumSym)
635 argtypes = argtypes.tail.tail;
636 if (tree.varargsElement != null)
637 tree.varargsElement = types.erasure(tree.varargsElement);
638 else
639 Assert.check(tree.args.length() == argtypes.length());
640 tree.args = translateArgs(tree.args, argtypes, tree.varargsElement);
642 tree.type = types.erasure(tree.type);
643 // Insert casts of method invocation results as needed.
644 result = retype(tree, mt.getReturnType(), pt);
645 }
647 public void visitNewClass(JCNewClass tree) {
648 if (tree.encl != null)
649 tree.encl = translate(tree.encl, erasure(tree.encl.type));
650 tree.clazz = translate(tree.clazz, null);
651 if (tree.varargsElement != null)
652 tree.varargsElement = types.erasure(tree.varargsElement);
653 tree.args = translateArgs(
654 tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement);
655 tree.def = translate(tree.def, null);
656 if (tree.constructorType != null)
657 tree.constructorType = erasure(tree.constructorType);
658 tree.type = erasure(tree.type);
659 result = tree;
660 }
662 public void visitNewArray(JCNewArray tree) {
663 tree.elemtype = translate(tree.elemtype, null);
664 translate(tree.dims, syms.intType);
665 if (tree.type != null) {
666 tree.elems = translate(tree.elems, erasure(types.elemtype(tree.type)));
667 tree.type = erasure(tree.type);
668 } else {
669 tree.elems = translate(tree.elems, null);
670 }
672 result = tree;
673 }
675 public void visitParens(JCParens tree) {
676 tree.expr = translate(tree.expr, pt);
677 tree.type = erasure(tree.type);
678 result = tree;
679 }
681 public void visitAssign(JCAssign tree) {
682 tree.lhs = translate(tree.lhs, null);
683 tree.rhs = translate(tree.rhs, erasure(tree.lhs.type));
684 tree.type = erasure(tree.type);
685 result = tree;
686 }
688 public void visitAssignop(JCAssignOp tree) {
689 tree.lhs = translate(tree.lhs, null);
690 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
691 tree.type = erasure(tree.type);
692 result = tree;
693 }
695 public void visitUnary(JCUnary tree) {
696 tree.arg = translate(tree.arg, tree.operator.type.getParameterTypes().head);
697 result = tree;
698 }
700 public void visitBinary(JCBinary tree) {
701 tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head);
702 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
703 result = tree;
704 }
706 public void visitTypeCast(JCTypeCast tree) {
707 tree.clazz = translate(tree.clazz, null);
708 tree.type = erasure(tree.type);
709 tree.expr = translate(tree.expr, tree.type);
710 result = tree;
711 }
713 public void visitTypeTest(JCInstanceOf tree) {
714 tree.expr = translate(tree.expr, null);
715 tree.clazz = translate(tree.clazz, null);
716 result = tree;
717 }
719 public void visitIndexed(JCArrayAccess tree) {
720 tree.indexed = translate(tree.indexed, erasure(tree.indexed.type));
721 tree.index = translate(tree.index, syms.intType);
723 // Insert casts of indexed expressions as needed.
724 result = retype(tree, types.elemtype(tree.indexed.type), pt);
725 }
727 // There ought to be nothing to rewrite here;
728 // we don't generate code.
729 public void visitAnnotation(JCAnnotation tree) {
730 result = tree;
731 }
733 public void visitIdent(JCIdent tree) {
734 Type et = tree.sym.erasure(types);
736 // Map type variables to their bounds.
737 if (tree.sym.kind == TYP && tree.sym.type.hasTag(TYPEVAR)) {
738 result = make.at(tree.pos).Type(et);
739 } else
740 // Map constants expressions to themselves.
741 if (tree.type.constValue() != null) {
742 result = tree;
743 }
744 // Insert casts of variable uses as needed.
745 else if (tree.sym.kind == VAR) {
746 result = retype(tree, et, pt);
747 }
748 else {
749 tree.type = erasure(tree.type);
750 result = tree;
751 }
752 }
754 public void visitSelect(JCFieldAccess tree) {
755 Type t = tree.selected.type;
756 while (t.hasTag(TYPEVAR))
757 t = t.getUpperBound();
758 if (t.isCompound()) {
759 if ((tree.sym.flags() & IPROXY) != 0) {
760 tree.sym = ((MethodSymbol)tree.sym).
761 implemented((TypeSymbol)tree.sym.owner, types);
762 }
763 tree.selected = coerce(
764 translate(tree.selected, erasure(tree.selected.type)),
765 erasure(tree.sym.owner.type));
766 } else
767 tree.selected = translate(tree.selected, erasure(t));
769 // Map constants expressions to themselves.
770 if (tree.type.constValue() != null) {
771 result = tree;
772 }
773 // Insert casts of variable uses as needed.
774 else if (tree.sym.kind == VAR) {
775 result = retype(tree, tree.sym.erasure(types), pt);
776 }
777 else {
778 tree.type = erasure(tree.type);
779 result = tree;
780 }
781 }
783 public void visitReference(JCMemberReference tree) {
784 tree.expr = translate(tree.expr, null);
785 tree.type = erasure(tree.type);
786 result = tree;
787 }
789 public void visitTypeArray(JCArrayTypeTree tree) {
790 tree.elemtype = translate(tree.elemtype, null);
791 tree.type = erasure(tree.type);
792 result = tree;
793 }
795 /** Visitor method for parameterized types.
796 */
797 public void visitTypeApply(JCTypeApply tree) {
798 JCTree clazz = translate(tree.clazz, null);
799 result = clazz;
800 }
802 public void visitTypeIntersection(JCTypeIntersection tree) {
803 tree.bounds = translate(tree.bounds, null);
804 tree.type = erasure(tree.type);
805 result = tree;
806 }
808 /**************************************************************************
809 * utility methods
810 *************************************************************************/
812 private Type erasure(Type t) {
813 return types.erasure(t);
814 }
816 private boolean boundsRestricted(ClassSymbol c) {
817 Type st = types.supertype(c.type);
818 if (st.isParameterized()) {
819 List<Type> actuals = st.allparams();
820 List<Type> formals = st.tsym.type.allparams();
821 while (!actuals.isEmpty() && !formals.isEmpty()) {
822 Type actual = actuals.head;
823 Type formal = formals.head;
825 if (!types.isSameType(types.erasure(actual),
826 types.erasure(formal)))
827 return true;
829 actuals = actuals.tail;
830 formals = formals.tail;
831 }
832 }
833 return false;
834 }
836 private List<JCTree> addOverrideBridgesIfNeeded(DiagnosticPosition pos,
837 final ClassSymbol c) {
838 ListBuffer<JCTree> buf = ListBuffer.lb();
839 if (c.isInterface() || !boundsRestricted(c))
840 return buf.toList();
841 Type t = types.supertype(c.type);
842 Scope s = t.tsym.members();
843 if (s.elems != null) {
844 for (Symbol sym : s.getElements(new NeedsOverridBridgeFilter(c))) {
846 MethodSymbol m = (MethodSymbol)sym;
847 MethodSymbol member = (MethodSymbol)m.asMemberOf(c.type, types);
848 MethodSymbol impl = m.implementation(c, types, false);
850 if ((impl == null || impl.owner != c) &&
851 !types.isSameType(member.erasure(types), m.erasure(types))) {
852 addOverrideBridges(pos, m, member, c, buf);
853 }
854 }
855 }
856 return buf.toList();
857 }
858 // where
859 class NeedsOverridBridgeFilter implements Filter<Symbol> {
861 ClassSymbol c;
863 NeedsOverridBridgeFilter(ClassSymbol c) {
864 this.c = c;
865 }
866 public boolean accepts(Symbol s) {
867 return s.kind == MTH &&
868 !s.isConstructor() &&
869 s.isInheritedIn(c, types) &&
870 (s.flags() & FINAL) == 0 &&
871 (s.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC;
872 }
873 }
875 private void addOverrideBridges(DiagnosticPosition pos,
876 MethodSymbol impl,
877 MethodSymbol member,
878 ClassSymbol c,
879 ListBuffer<JCTree> bridges) {
880 Type implErasure = impl.erasure(types);
881 long flags = (impl.flags() & AccessFlags) | SYNTHETIC | BRIDGE | OVERRIDE_BRIDGE;
882 member = new MethodSymbol(flags, member.name, member.type, c);
883 JCMethodDecl md = make.MethodDef(member, null);
884 JCExpression receiver = make.Super(types.supertype(c.type).tsym.erasure(types), c);
885 Type calltype = erasure(impl.type.getReturnType());
886 JCExpression call =
887 make.Apply(null,
888 make.Select(receiver, impl).setType(calltype),
889 translateArgs(make.Idents(md.params),
890 implErasure.getParameterTypes(), null))
891 .setType(calltype);
892 JCStatement stat = (member.getReturnType().hasTag(VOID))
893 ? make.Exec(call)
894 : make.Return(coerce(call, member.erasure(types).getReturnType()));
895 md.body = make.Block(0, List.of(stat));
896 c.members().enter(member);
897 bridges.append(md);
898 }
900 /**************************************************************************
901 * main method
902 *************************************************************************/
904 private Env<AttrContext> env;
906 void translateClass(ClassSymbol c) {
907 Type st = types.supertype(c.type);
909 // process superclass before derived
910 if (st.hasTag(CLASS))
911 translateClass((ClassSymbol)st.tsym);
913 Env<AttrContext> myEnv = enter.typeEnvs.remove(c);
914 if (myEnv == null)
915 return;
916 Env<AttrContext> oldEnv = env;
917 try {
918 env = myEnv;
919 // class has not been translated yet
921 TreeMaker savedMake = make;
922 Type savedPt = pt;
923 make = make.forToplevel(env.toplevel);
924 pt = null;
925 try {
926 JCClassDecl tree = (JCClassDecl) env.tree;
927 tree.typarams = List.nil();
928 super.visitClassDef(tree);
929 make.at(tree.pos);
930 if (addBridges) {
931 ListBuffer<JCTree> bridges = new ListBuffer<JCTree>();
932 if (false) //see CR: 6996415
933 bridges.appendList(addOverrideBridgesIfNeeded(tree, c));
934 if ((tree.sym.flags() & INTERFACE) == 0)
935 addBridges(tree.pos(), tree.sym, bridges);
936 tree.defs = bridges.toList().prependList(tree.defs);
937 }
938 tree.type = erasure(tree.type);
939 } finally {
940 make = savedMake;
941 pt = savedPt;
942 }
943 } finally {
944 env = oldEnv;
945 }
946 }
948 /** Translate a toplevel class definition.
949 * @param cdef The definition to be translated.
950 */
951 public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) {
952 // note that this method does NOT support recursion.
953 this.make = make;
954 pt = null;
955 return translate(cdef, null);
956 }
957 }