Mon, 03 Jun 2013 17:09:26 -0700
8006615: [doclint] move remaining messages into resource bundle
Reviewed-by: mcimadamore, vromero
1 /*
2 * Copyright (c) 1999, 2013, 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;
43 import static com.sun.tools.javac.comp.CompileStates.CompileState;
45 /** This pass translates Generic Java to conventional Java.
46 *
47 * <p><b>This is NOT part of any supported API.
48 * If you write code that depends on this, you do so at your own risk.
49 * This code and its internal interfaces are subject to change or
50 * deletion without notice.</b>
51 */
52 public class TransTypes extends TreeTranslator {
53 /** The context key for the TransTypes phase. */
54 protected static final Context.Key<TransTypes> transTypesKey =
55 new Context.Key<TransTypes>();
57 /** Get the instance for this context. */
58 public static TransTypes instance(Context context) {
59 TransTypes instance = context.get(transTypesKey);
60 if (instance == null)
61 instance = new TransTypes(context);
62 return instance;
63 }
65 private Names names;
66 private Log log;
67 private Symtab syms;
68 private TreeMaker make;
69 private Enter enter;
70 private boolean allowEnums;
71 private Types types;
72 private final Resolve resolve;
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 private final CompileStates compileStates;
83 protected TransTypes(Context context) {
84 context.put(transTypesKey, this);
85 compileStates = CompileStates.instance(context);
86 names = Names.instance(context);
87 log = Log.instance(context);
88 syms = Symtab.instance(context);
89 enter = Enter.instance(context);
90 overridden = new HashMap<MethodSymbol,MethodSymbol>();
91 Source source = Source.instance(context);
92 allowEnums = source.allowEnums();
93 addBridges = source.addBridges();
94 types = Types.instance(context);
95 make = TreeMaker.instance(context);
96 resolve = Resolve.instance(context);
97 }
99 /** A hashtable mapping bridge methods to the methods they override after
100 * type erasure.
101 */
102 Map<MethodSymbol,MethodSymbol> overridden;
104 /** Construct an attributed tree for a cast of expression to target type,
105 * unless it already has precisely that type.
106 * @param tree The expression tree.
107 * @param target The target type.
108 */
109 JCExpression cast(JCExpression tree, Type target) {
110 int oldpos = make.pos;
111 make.at(tree.pos);
112 if (!types.isSameType(tree.type, target)) {
113 if (!resolve.isAccessible(env, target.tsym))
114 resolve.logAccessErrorInternal(env, tree, target);
115 tree = make.TypeCast(make.Type(target), tree).setType(target);
116 }
117 make.pos = oldpos;
118 return tree;
119 }
121 /** Construct an attributed tree to coerce an expression to some erased
122 * target type, unless the expression is already assignable to that type.
123 * If target type is a constant type, use its base type instead.
124 * @param tree The expression tree.
125 * @param target The target type.
126 */
127 public JCExpression coerce(Env<AttrContext> env, JCExpression tree, Type target) {
128 Env<AttrContext> prevEnv = this.env;
129 try {
130 this.env = env;
131 return coerce(tree, target);
132 }
133 finally {
134 this.env = prevEnv;
135 }
136 }
137 JCExpression coerce(JCExpression tree, Type target) {
138 Type btarget = target.baseType();
139 if (tree.type.isPrimitive() == target.isPrimitive()) {
140 return types.isAssignable(tree.type, btarget, types.noWarnings)
141 ? tree
142 : cast(tree, btarget);
143 }
144 return tree;
145 }
147 /** Given an erased reference type, assume this type as the tree's type.
148 * Then, coerce to some given target type unless target type is null.
149 * This operation is used in situations like the following:
150 *
151 * <pre>{@code
152 * class Cell<A> { A value; }
153 * ...
154 * Cell<Integer> cell;
155 * Integer x = cell.value;
156 * }</pre>
157 *
158 * Since the erasure of Cell.value is Object, but the type
159 * of cell.value in the assignment is Integer, we need to
160 * adjust the original type of cell.value to Object, and insert
161 * a cast to Integer. That is, the last assignment becomes:
162 *
163 * <pre>{@code
164 * Integer x = (Integer)cell.value;
165 * }</pre>
166 *
167 * @param tree The expression tree whose type might need adjustment.
168 * @param erasedType The expression's type after erasure.
169 * @param target The target type, which is usually the erasure of the
170 * expression's original type.
171 */
172 JCExpression retype(JCExpression tree, Type erasedType, Type target) {
173 // System.err.println("retype " + tree + " to " + erasedType);//DEBUG
174 if (!erasedType.isPrimitive()) {
175 if (target != null && target.isPrimitive()) {
176 target = erasure(tree.type);
177 }
178 tree.type = erasedType;
179 if (target != null) {
180 return coerce(tree, target);
181 }
182 }
183 return tree;
184 }
186 /** Translate method argument list, casting each argument
187 * to its corresponding type in a list of target types.
188 * @param _args The method argument list.
189 * @param parameters The list of target types.
190 * @param varargsElement The erasure of the varargs element type,
191 * or null if translating a non-varargs invocation
192 */
193 <T extends JCTree> List<T> translateArgs(List<T> _args,
194 List<Type> parameters,
195 Type varargsElement) {
196 if (parameters.isEmpty()) return _args;
197 List<T> args = _args;
198 while (parameters.tail.nonEmpty()) {
199 args.head = translate(args.head, parameters.head);
200 args = args.tail;
201 parameters = parameters.tail;
202 }
203 Type parameter = parameters.head;
204 Assert.check(varargsElement != null || args.length() == 1);
205 if (varargsElement != null) {
206 while (args.nonEmpty()) {
207 args.head = translate(args.head, varargsElement);
208 args = args.tail;
209 }
210 } else {
211 args.head = translate(args.head, parameter);
212 }
213 return _args;
214 }
216 public <T extends JCTree> List<T> translateArgs(List<T> _args,
217 List<Type> parameters,
218 Type varargsElement,
219 Env<AttrContext> localEnv) {
220 Env<AttrContext> prevEnv = env;
221 try {
222 env = localEnv;
223 return translateArgs(_args, parameters, varargsElement);
224 }
225 finally {
226 env = prevEnv;
227 }
228 }
230 /** Add a bridge definition and enter corresponding method symbol in
231 * local scope of origin.
232 *
233 * @param pos The source code position to be used for the definition.
234 * @param meth The method for which a bridge needs to be added
235 * @param impl That method's implementation (possibly the method itself)
236 * @param origin The class to which the bridge will be added
237 * @param hypothetical
238 * True if the bridge method is not strictly necessary in the
239 * binary, but is represented in the symbol table to detect
240 * erasure clashes.
241 * @param bridges The list buffer to which the bridge will be added
242 */
243 void addBridge(DiagnosticPosition pos,
244 MethodSymbol meth,
245 MethodSymbol impl,
246 ClassSymbol origin,
247 boolean hypothetical,
248 ListBuffer<JCTree> bridges) {
249 make.at(pos);
250 Type origType = types.memberType(origin.type, meth);
251 Type origErasure = erasure(origType);
253 // Create a bridge method symbol and a bridge definition without a body.
254 Type bridgeType = meth.erasure(types);
255 long flags = impl.flags() & AccessFlags | SYNTHETIC | BRIDGE;
256 if (hypothetical) flags |= HYPOTHETICAL;
257 MethodSymbol bridge = new MethodSymbol(flags,
258 meth.name,
259 bridgeType,
260 origin);
261 /* once JDK-6996415 is solved it should be checked if this approach can
262 * be applied to method addOverrideBridgesIfNeeded
263 */
264 bridge.params = createBridgeParams(impl, bridge, bridgeType);
265 if (impl.annotations != null) {
266 bridge.annotations.setAttributes(impl.annotations);
267 }
269 if (!hypothetical) {
270 JCMethodDecl md = make.MethodDef(bridge, null);
272 // The bridge calls this.impl(..), if we have an implementation
273 // in the current class, super.impl(...) otherwise.
274 JCExpression receiver = (impl.owner == origin)
275 ? make.This(origin.erasure(types))
276 : make.Super(types.supertype(origin.type).tsym.erasure(types), origin);
278 // The type returned from the original method.
279 Type calltype = erasure(impl.type.getReturnType());
281 // Construct a call of this.impl(params), or super.impl(params),
282 // casting params and possibly results as needed.
283 JCExpression call =
284 make.Apply(
285 null,
286 make.Select(receiver, impl).setType(calltype),
287 translateArgs(make.Idents(md.params), origErasure.getParameterTypes(), null))
288 .setType(calltype);
289 JCStatement stat = (origErasure.getReturnType().hasTag(VOID))
290 ? make.Exec(call)
291 : make.Return(coerce(call, bridgeType.getReturnType()));
292 md.body = make.Block(0, List.of(stat));
294 // Add bridge to `bridges' buffer
295 bridges.append(md);
296 }
298 // Add bridge to scope of enclosing class and `overridden' table.
299 origin.members().enter(bridge);
300 overridden.put(bridge, meth);
301 }
303 private List<VarSymbol> createBridgeParams(MethodSymbol impl, MethodSymbol bridge,
304 Type bridgeType) {
305 List<VarSymbol> bridgeParams = null;
306 if (impl.params != null) {
307 bridgeParams = List.nil();
308 List<VarSymbol> implParams = impl.params;
309 Type.MethodType mType = (Type.MethodType)bridgeType;
310 List<Type> argTypes = mType.argtypes;
311 while (implParams.nonEmpty() && argTypes.nonEmpty()) {
312 VarSymbol param = new VarSymbol(implParams.head.flags() | SYNTHETIC,
313 implParams.head.name, argTypes.head, bridge);
314 if (implParams.head.annotations != null) {
315 param.annotations.setAttributes(implParams.head.annotations);
316 }
317 bridgeParams = bridgeParams.append(param);
318 implParams = implParams.tail;
319 argTypes = argTypes.tail;
320 }
321 }
322 return bridgeParams;
323 }
325 /** Add bridge if given symbol is a non-private, non-static member
326 * of the given class, which is either defined in the class or non-final
327 * inherited, and one of the two following conditions holds:
328 * 1. The method's type changes in the given class, as compared to the
329 * class where the symbol was defined, (in this case
330 * we have extended a parameterized class with non-trivial parameters).
331 * 2. The method has an implementation with a different erased return type.
332 * (in this case we have used co-variant returns).
333 * If a bridge already exists in some other class, no new bridge is added.
334 * Instead, it is checked that the bridge symbol overrides the method symbol.
335 * (Spec ???).
336 * todo: what about bridges for privates???
337 *
338 * @param pos The source code position to be used for the definition.
339 * @param sym The symbol for which a bridge might have to be added.
340 * @param origin The class in which the bridge would go.
341 * @param bridges The list buffer to which the bridge would be added.
342 */
343 void addBridgeIfNeeded(DiagnosticPosition pos,
344 Symbol sym,
345 ClassSymbol origin,
346 ListBuffer<JCTree> bridges) {
347 if (sym.kind == MTH &&
348 sym.name != names.init &&
349 (sym.flags() & (PRIVATE | STATIC)) == 0 &&
350 (sym.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC &&
351 sym.isMemberOf(origin, types))
352 {
353 MethodSymbol meth = (MethodSymbol)sym;
354 MethodSymbol bridge = meth.binaryImplementation(origin, types);
355 MethodSymbol impl = meth.implementation(origin, types, true, overrideBridgeFilter);
356 if (bridge == null ||
357 bridge == meth ||
358 (impl != null && !bridge.owner.isSubClass(impl.owner, types))) {
359 // No bridge was added yet.
360 if (impl != null && isBridgeNeeded(meth, impl, origin.type)) {
361 addBridge(pos, meth, impl, origin, bridge==impl, bridges);
362 } else if (impl == meth
363 && impl.owner != origin
364 && (impl.flags() & FINAL) == 0
365 && (meth.flags() & (ABSTRACT|PUBLIC)) == PUBLIC
366 && (origin.flags() & PUBLIC) > (impl.owner.flags() & PUBLIC)) {
367 // this is to work around a horrible but permanent
368 // reflection design error.
369 addBridge(pos, meth, impl, origin, false, bridges);
370 }
371 } else if ((bridge.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) == SYNTHETIC) {
372 MethodSymbol other = overridden.get(bridge);
373 if (other != null && other != meth) {
374 if (impl == null || !impl.overrides(other, origin, types, true)) {
375 // Bridge for other symbol pair was added
376 log.error(pos, "name.clash.same.erasure.no.override",
377 other, other.location(origin.type, types),
378 meth, meth.location(origin.type, types));
379 }
380 }
381 } else if (!bridge.overrides(meth, origin, types, true)) {
382 // Accidental binary override without source override.
383 if (bridge.owner == origin ||
384 types.asSuper(bridge.owner.type, meth.owner) == null)
385 // Don't diagnose the problem if it would already
386 // have been reported in the superclass
387 log.error(pos, "name.clash.same.erasure.no.override",
388 bridge, bridge.location(origin.type, types),
389 meth, meth.location(origin.type, types));
390 }
391 }
392 }
393 // where
394 Filter<Symbol> overrideBridgeFilter = new Filter<Symbol>() {
395 public boolean accepts(Symbol s) {
396 return (s.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC;
397 }
398 };
399 /**
400 * @param method The symbol for which a bridge might have to be added
401 * @param impl The implementation of method
402 * @param dest The type in which the bridge would go
403 */
404 private boolean isBridgeNeeded(MethodSymbol method,
405 MethodSymbol impl,
406 Type dest) {
407 if (impl != method) {
408 // If either method or impl have different erasures as
409 // members of dest, a bridge is needed.
410 Type method_erasure = method.erasure(types);
411 if (!isSameMemberWhenErased(dest, method, method_erasure))
412 return true;
413 Type impl_erasure = impl.erasure(types);
414 if (!isSameMemberWhenErased(dest, impl, impl_erasure))
415 return true;
417 // If the erasure of the return type is different, a
418 // bridge is needed.
419 return !types.isSameType(impl_erasure.getReturnType(),
420 method_erasure.getReturnType());
421 } else {
422 // method and impl are the same...
423 if ((method.flags() & ABSTRACT) != 0) {
424 // ...and abstract so a bridge is not needed.
425 // Concrete subclasses will bridge as needed.
426 return false;
427 }
429 // The erasure of the return type is always the same
430 // for the same symbol. Reducing the three tests in
431 // the other branch to just one:
432 return !isSameMemberWhenErased(dest, method, method.erasure(types));
433 }
434 }
435 /**
436 * Lookup the method as a member of the type. Compare the
437 * erasures.
438 * @param type the class where to look for the method
439 * @param method the method to look for in class
440 * @param erasure the erasure of method
441 */
442 private boolean isSameMemberWhenErased(Type type,
443 MethodSymbol method,
444 Type erasure) {
445 return types.isSameType(erasure(types.memberType(type, method)),
446 erasure);
447 }
449 void addBridges(DiagnosticPosition pos,
450 TypeSymbol i,
451 ClassSymbol origin,
452 ListBuffer<JCTree> bridges) {
453 for (Scope.Entry e = i.members().elems; e != null; e = e.sibling)
454 addBridgeIfNeeded(pos, e.sym, origin, bridges);
455 for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail)
456 addBridges(pos, l.head.tsym, origin, bridges);
457 }
459 /** Add all necessary bridges to some class appending them to list buffer.
460 * @param pos The source code position to be used for the bridges.
461 * @param origin The class in which the bridges go.
462 * @param bridges The list buffer to which the bridges are added.
463 */
464 void addBridges(DiagnosticPosition pos, ClassSymbol origin, ListBuffer<JCTree> bridges) {
465 Type st = types.supertype(origin.type);
466 while (st.hasTag(CLASS)) {
467 // if (isSpecialization(st))
468 addBridges(pos, st.tsym, origin, bridges);
469 st = types.supertype(st);
470 }
471 for (List<Type> l = types.interfaces(origin.type); l.nonEmpty(); l = l.tail)
472 // if (isSpecialization(l.head))
473 addBridges(pos, l.head.tsym, origin, bridges);
474 }
476 /* ************************************************************************
477 * Visitor methods
478 *************************************************************************/
480 /** Visitor argument: proto-type.
481 */
482 private Type pt;
484 /** Visitor method: perform a type translation on tree.
485 */
486 public <T extends JCTree> T translate(T tree, Type pt) {
487 Type prevPt = this.pt;
488 try {
489 this.pt = pt;
490 return translate(tree);
491 } finally {
492 this.pt = prevPt;
493 }
494 }
496 /** Visitor method: perform a type translation on list of trees.
497 */
498 public <T extends JCTree> List<T> translate(List<T> trees, Type pt) {
499 Type prevPt = this.pt;
500 List<T> res;
501 try {
502 this.pt = pt;
503 res = translate(trees);
504 } finally {
505 this.pt = prevPt;
506 }
507 return res;
508 }
510 public void visitClassDef(JCClassDecl tree) {
511 translateClass(tree.sym);
512 result = tree;
513 }
515 JCTree currentMethod = null;
516 public void visitMethodDef(JCMethodDecl tree) {
517 JCTree previousMethod = currentMethod;
518 try {
519 currentMethod = tree;
520 tree.restype = translate(tree.restype, null);
521 tree.typarams = List.nil();
522 tree.params = translateVarDefs(tree.params);
523 tree.recvparam = translate(tree.recvparam, null);
524 tree.thrown = translate(tree.thrown, null);
525 tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType());
526 tree.type = erasure(tree.type);
527 result = tree;
528 } finally {
529 currentMethod = previousMethod;
530 }
532 // Check that we do not introduce a name clash by erasing types.
533 for (Scope.Entry e = tree.sym.owner.members().lookup(tree.name);
534 e.sym != null;
535 e = e.next()) {
536 if (e.sym != tree.sym &&
537 types.isSameType(erasure(e.sym.type), tree.type)) {
538 log.error(tree.pos(),
539 "name.clash.same.erasure", tree.sym,
540 e.sym);
541 return;
542 }
543 }
544 }
546 public void visitVarDef(JCVariableDecl tree) {
547 tree.vartype = translate(tree.vartype, null);
548 tree.init = translate(tree.init, tree.sym.erasure(types));
549 tree.type = erasure(tree.type);
550 result = tree;
551 }
553 public void visitDoLoop(JCDoWhileLoop tree) {
554 tree.body = translate(tree.body);
555 tree.cond = translate(tree.cond, syms.booleanType);
556 result = tree;
557 }
559 public void visitWhileLoop(JCWhileLoop tree) {
560 tree.cond = translate(tree.cond, syms.booleanType);
561 tree.body = translate(tree.body);
562 result = tree;
563 }
565 public void visitForLoop(JCForLoop tree) {
566 tree.init = translate(tree.init, null);
567 if (tree.cond != null)
568 tree.cond = translate(tree.cond, syms.booleanType);
569 tree.step = translate(tree.step, null);
570 tree.body = translate(tree.body);
571 result = tree;
572 }
574 public void visitForeachLoop(JCEnhancedForLoop tree) {
575 tree.var = translate(tree.var, null);
576 Type iterableType = tree.expr.type;
577 tree.expr = translate(tree.expr, erasure(tree.expr.type));
578 if (types.elemtype(tree.expr.type) == null)
579 tree.expr.type = iterableType; // preserve type for Lower
580 tree.body = translate(tree.body);
581 result = tree;
582 }
584 public void visitLambda(JCLambda tree) {
585 JCTree prevMethod = currentMethod;
586 try {
587 currentMethod = null;
588 tree.params = translate(tree.params);
589 tree.body = translate(tree.body, null);
590 tree.type = erasure(tree.type);
591 result = tree;
592 }
593 finally {
594 currentMethod = prevMethod;
595 }
596 }
598 public void visitSwitch(JCSwitch tree) {
599 Type selsuper = types.supertype(tree.selector.type);
600 boolean enumSwitch = selsuper != null &&
601 selsuper.tsym == syms.enumSym;
602 Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType;
603 tree.selector = translate(tree.selector, target);
604 tree.cases = translateCases(tree.cases);
605 result = tree;
606 }
608 public void visitCase(JCCase tree) {
609 tree.pat = translate(tree.pat, null);
610 tree.stats = translate(tree.stats);
611 result = tree;
612 }
614 public void visitSynchronized(JCSynchronized tree) {
615 tree.lock = translate(tree.lock, erasure(tree.lock.type));
616 tree.body = translate(tree.body);
617 result = tree;
618 }
620 public void visitTry(JCTry tree) {
621 tree.resources = translate(tree.resources, syms.autoCloseableType);
622 tree.body = translate(tree.body);
623 tree.catchers = translateCatchers(tree.catchers);
624 tree.finalizer = translate(tree.finalizer);
625 result = tree;
626 }
628 public void visitConditional(JCConditional tree) {
629 tree.cond = translate(tree.cond, syms.booleanType);
630 tree.truepart = translate(tree.truepart, erasure(tree.type));
631 tree.falsepart = translate(tree.falsepart, erasure(tree.type));
632 tree.type = erasure(tree.type);
633 result = retype(tree, tree.type, pt);
634 }
636 public void visitIf(JCIf tree) {
637 tree.cond = translate(tree.cond, syms.booleanType);
638 tree.thenpart = translate(tree.thenpart);
639 tree.elsepart = translate(tree.elsepart);
640 result = tree;
641 }
643 public void visitExec(JCExpressionStatement tree) {
644 tree.expr = translate(tree.expr, null);
645 result = tree;
646 }
648 public void visitReturn(JCReturn tree) {
649 tree.expr = translate(tree.expr, currentMethod != null ? types.erasure(currentMethod.type).getReturnType() : null);
650 result = tree;
651 }
653 public void visitThrow(JCThrow tree) {
654 tree.expr = translate(tree.expr, erasure(tree.expr.type));
655 result = tree;
656 }
658 public void visitAssert(JCAssert tree) {
659 tree.cond = translate(tree.cond, syms.booleanType);
660 if (tree.detail != null)
661 tree.detail = translate(tree.detail, erasure(tree.detail.type));
662 result = tree;
663 }
665 public void visitApply(JCMethodInvocation tree) {
666 tree.meth = translate(tree.meth, null);
667 Symbol meth = TreeInfo.symbol(tree.meth);
668 Type mt = meth.erasure(types);
669 List<Type> argtypes = mt.getParameterTypes();
670 if (allowEnums &&
671 meth.name==names.init &&
672 meth.owner == syms.enumSym)
673 argtypes = argtypes.tail.tail;
674 if (tree.varargsElement != null)
675 tree.varargsElement = types.erasure(tree.varargsElement);
676 else
677 Assert.check(tree.args.length() == argtypes.length());
678 tree.args = translateArgs(tree.args, argtypes, tree.varargsElement);
680 tree.type = types.erasure(tree.type);
681 // Insert casts of method invocation results as needed.
682 result = retype(tree, mt.getReturnType(), pt);
683 }
685 public void visitNewClass(JCNewClass tree) {
686 if (tree.encl != null)
687 tree.encl = translate(tree.encl, erasure(tree.encl.type));
688 tree.clazz = translate(tree.clazz, null);
689 if (tree.varargsElement != null)
690 tree.varargsElement = types.erasure(tree.varargsElement);
691 tree.args = translateArgs(
692 tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement);
693 tree.def = translate(tree.def, null);
694 if (tree.constructorType != null)
695 tree.constructorType = erasure(tree.constructorType);
696 tree.type = erasure(tree.type);
697 result = tree;
698 }
700 public void visitNewArray(JCNewArray tree) {
701 tree.elemtype = translate(tree.elemtype, null);
702 translate(tree.dims, syms.intType);
703 if (tree.type != null) {
704 tree.elems = translate(tree.elems, erasure(types.elemtype(tree.type)));
705 tree.type = erasure(tree.type);
706 } else {
707 tree.elems = translate(tree.elems, null);
708 }
710 result = tree;
711 }
713 public void visitParens(JCParens tree) {
714 tree.expr = translate(tree.expr, pt);
715 tree.type = erasure(tree.type);
716 result = tree;
717 }
719 public void visitAssign(JCAssign tree) {
720 tree.lhs = translate(tree.lhs, null);
721 tree.rhs = translate(tree.rhs, erasure(tree.lhs.type));
722 tree.type = erasure(tree.lhs.type);
723 result = retype(tree, tree.type, pt);
724 }
726 public void visitAssignop(JCAssignOp tree) {
727 tree.lhs = translate(tree.lhs, null);
728 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
729 tree.type = erasure(tree.type);
730 result = tree;
731 }
733 public void visitUnary(JCUnary tree) {
734 tree.arg = translate(tree.arg, tree.operator.type.getParameterTypes().head);
735 result = tree;
736 }
738 public void visitBinary(JCBinary tree) {
739 tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head);
740 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
741 result = tree;
742 }
744 public void visitTypeCast(JCTypeCast tree) {
745 tree.clazz = translate(tree.clazz, null);
746 Type originalTarget = tree.type;
747 tree.type = erasure(tree.type);
748 tree.expr = translate(tree.expr, tree.type);
749 if (originalTarget.isCompound()) {
750 Type.IntersectionClassType ict = (Type.IntersectionClassType)originalTarget;
751 for (Type c : ict.getExplicitComponents()) {
752 Type ec = erasure(c);
753 if (!types.isSameType(ec, tree.type)) {
754 tree.expr = coerce(tree.expr, ec);
755 }
756 }
757 }
758 result = tree;
759 }
761 public void visitTypeTest(JCInstanceOf tree) {
762 tree.expr = translate(tree.expr, null);
763 tree.clazz = translate(tree.clazz, null);
764 result = tree;
765 }
767 public void visitIndexed(JCArrayAccess tree) {
768 tree.indexed = translate(tree.indexed, erasure(tree.indexed.type));
769 tree.index = translate(tree.index, syms.intType);
771 // Insert casts of indexed expressions as needed.
772 result = retype(tree, types.elemtype(tree.indexed.type), pt);
773 }
775 // There ought to be nothing to rewrite here;
776 // we don't generate code.
777 public void visitAnnotation(JCAnnotation tree) {
778 result = tree;
779 }
781 public void visitIdent(JCIdent tree) {
782 Type et = tree.sym.erasure(types);
784 // Map type variables to their bounds.
785 if (tree.sym.kind == TYP && tree.sym.type.hasTag(TYPEVAR)) {
786 result = make.at(tree.pos).Type(et);
787 } else
788 // Map constants expressions to themselves.
789 if (tree.type.constValue() != null) {
790 result = tree;
791 }
792 // Insert casts of variable uses as needed.
793 else if (tree.sym.kind == VAR) {
794 result = retype(tree, et, pt);
795 }
796 else {
797 tree.type = erasure(tree.type);
798 result = tree;
799 }
800 }
802 public void visitSelect(JCFieldAccess tree) {
803 Type t = tree.selected.type;
804 while (t.hasTag(TYPEVAR))
805 t = t.getUpperBound();
806 if (t.isCompound()) {
807 if ((tree.sym.flags() & IPROXY) != 0) {
808 tree.sym = ((MethodSymbol)tree.sym).
809 implemented((TypeSymbol)tree.sym.owner, types);
810 }
811 tree.selected = coerce(
812 translate(tree.selected, erasure(tree.selected.type)),
813 erasure(tree.sym.owner.type));
814 } else
815 tree.selected = translate(tree.selected, erasure(t));
817 // Map constants expressions to themselves.
818 if (tree.type.constValue() != null) {
819 result = tree;
820 }
821 // Insert casts of variable uses as needed.
822 else if (tree.sym.kind == VAR) {
823 result = retype(tree, tree.sym.erasure(types), pt);
824 }
825 else {
826 tree.type = erasure(tree.type);
827 result = tree;
828 }
829 }
831 public void visitReference(JCMemberReference tree) {
832 tree.expr = translate(tree.expr, null);
833 tree.type = erasure(tree.type);
834 result = tree;
835 }
837 public void visitTypeArray(JCArrayTypeTree tree) {
838 tree.elemtype = translate(tree.elemtype, null);
839 tree.type = erasure(tree.type);
840 result = tree;
841 }
843 /** Visitor method for parameterized types.
844 */
845 public void visitTypeApply(JCTypeApply tree) {
846 JCTree clazz = translate(tree.clazz, null);
847 result = clazz;
848 }
850 public void visitTypeIntersection(JCTypeIntersection tree) {
851 tree.bounds = translate(tree.bounds, null);
852 tree.type = erasure(tree.type);
853 result = tree;
854 }
856 /**************************************************************************
857 * utility methods
858 *************************************************************************/
860 private Type erasure(Type t) {
861 return types.erasure(t);
862 }
864 private boolean boundsRestricted(ClassSymbol c) {
865 Type st = types.supertype(c.type);
866 if (st.isParameterized()) {
867 List<Type> actuals = st.allparams();
868 List<Type> formals = st.tsym.type.allparams();
869 while (!actuals.isEmpty() && !formals.isEmpty()) {
870 Type actual = actuals.head;
871 Type formal = formals.head;
873 if (!types.isSameType(types.erasure(actual),
874 types.erasure(formal)))
875 return true;
877 actuals = actuals.tail;
878 formals = formals.tail;
879 }
880 }
881 return false;
882 }
884 private List<JCTree> addOverrideBridgesIfNeeded(DiagnosticPosition pos,
885 final ClassSymbol c) {
886 ListBuffer<JCTree> buf = ListBuffer.lb();
887 if (c.isInterface() || !boundsRestricted(c))
888 return buf.toList();
889 Type t = types.supertype(c.type);
890 Scope s = t.tsym.members();
891 if (s.elems != null) {
892 for (Symbol sym : s.getElements(new NeedsOverridBridgeFilter(c))) {
894 MethodSymbol m = (MethodSymbol)sym;
895 MethodSymbol member = (MethodSymbol)m.asMemberOf(c.type, types);
896 MethodSymbol impl = m.implementation(c, types, false);
898 if ((impl == null || impl.owner != c) &&
899 !types.isSameType(member.erasure(types), m.erasure(types))) {
900 addOverrideBridges(pos, m, member, c, buf);
901 }
902 }
903 }
904 return buf.toList();
905 }
906 // where
907 class NeedsOverridBridgeFilter implements Filter<Symbol> {
909 ClassSymbol c;
911 NeedsOverridBridgeFilter(ClassSymbol c) {
912 this.c = c;
913 }
914 public boolean accepts(Symbol s) {
915 return s.kind == MTH &&
916 !s.isConstructor() &&
917 s.isInheritedIn(c, types) &&
918 (s.flags() & FINAL) == 0 &&
919 (s.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC;
920 }
921 }
923 private void addOverrideBridges(DiagnosticPosition pos,
924 MethodSymbol impl,
925 MethodSymbol member,
926 ClassSymbol c,
927 ListBuffer<JCTree> bridges) {
928 Type implErasure = impl.erasure(types);
929 long flags = (impl.flags() & AccessFlags) | SYNTHETIC | BRIDGE | OVERRIDE_BRIDGE;
930 member = new MethodSymbol(flags, member.name, member.type, c);
931 JCMethodDecl md = make.MethodDef(member, null);
932 JCExpression receiver = make.Super(types.supertype(c.type).tsym.erasure(types), c);
933 Type calltype = erasure(impl.type.getReturnType());
934 JCExpression call =
935 make.Apply(null,
936 make.Select(receiver, impl).setType(calltype),
937 translateArgs(make.Idents(md.params),
938 implErasure.getParameterTypes(), null))
939 .setType(calltype);
940 JCStatement stat = (member.getReturnType().hasTag(VOID))
941 ? make.Exec(call)
942 : make.Return(coerce(call, member.erasure(types).getReturnType()));
943 md.body = make.Block(0, List.of(stat));
944 c.members().enter(member);
945 bridges.append(md);
946 }
948 /**************************************************************************
949 * main method
950 *************************************************************************/
952 private Env<AttrContext> env;
954 private static final String statePreviousToFlowAssertMsg =
955 "The current compile state [%s] of class %s is previous to FLOW";
957 void translateClass(ClassSymbol c) {
958 Type st = types.supertype(c.type);
959 // process superclass before derived
960 if (st.hasTag(CLASS)) {
961 translateClass((ClassSymbol)st.tsym);
962 }
964 Env<AttrContext> myEnv = enter.typeEnvs.remove(c);
965 if (myEnv == null) {
966 return;
967 }
969 /* The two assertions below are set for early detection of any attempt
970 * to translate a class that:
971 *
972 * 1) has no compile state being it the most outer class.
973 * We accept this condition for inner classes.
974 *
975 * 2) has a compile state which is previous to Flow state.
976 */
977 boolean envHasCompState = compileStates.get(myEnv) != null;
978 if (!envHasCompState && c.outermostClass() == c) {
979 Assert.error("No info for outermost class: " + myEnv.enclClass.sym);
980 }
982 if (envHasCompState &&
983 CompileState.FLOW.isAfter(compileStates.get(myEnv))) {
984 Assert.error(String.format(statePreviousToFlowAssertMsg,
985 compileStates.get(myEnv), myEnv.enclClass.sym));
986 }
988 Env<AttrContext> oldEnv = env;
989 try {
990 env = myEnv;
991 // class has not been translated yet
993 TreeMaker savedMake = make;
994 Type savedPt = pt;
995 make = make.forToplevel(env.toplevel);
996 pt = null;
997 try {
998 JCClassDecl tree = (JCClassDecl) env.tree;
999 tree.typarams = List.nil();
1000 super.visitClassDef(tree);
1001 make.at(tree.pos);
1002 if (addBridges) {
1003 ListBuffer<JCTree> bridges = new ListBuffer<JCTree>();
1004 if (false) //see CR: 6996415
1005 bridges.appendList(addOverrideBridgesIfNeeded(tree, c));
1006 if ((tree.sym.flags() & INTERFACE) == 0)
1007 addBridges(tree.pos(), tree.sym, bridges);
1008 tree.defs = bridges.toList().prependList(tree.defs);
1009 }
1010 tree.type = erasure(tree.type);
1011 } finally {
1012 make = savedMake;
1013 pt = savedPt;
1014 }
1015 } finally {
1016 env = oldEnv;
1017 }
1018 }
1020 /** Translate a toplevel class definition.
1021 * @param cdef The definition to be translated.
1022 */
1023 public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) {
1024 // note that this method does NOT support recursion.
1025 this.make = make;
1026 pt = null;
1027 return translate(cdef, null);
1028 }
1029 }