Sat, 13 Apr 2013 12:25:44 +0100
8010659: Javac Crashes while building OpenJFX
Reviewed-by: jjg
Contributed-by: maurizio.cimadamore@oracle.com
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 tree.type = erasedType;
178 if (target != null) return coerce(tree, target);
179 }
180 return tree;
181 }
183 /** Translate method argument list, casting each argument
184 * to its corresponding type in a list of target types.
185 * @param _args The method argument list.
186 * @param parameters The list of target types.
187 * @param varargsElement The erasure of the varargs element type,
188 * or null if translating a non-varargs invocation
189 */
190 <T extends JCTree> List<T> translateArgs(List<T> _args,
191 List<Type> parameters,
192 Type varargsElement) {
193 if (parameters.isEmpty()) return _args;
194 List<T> args = _args;
195 while (parameters.tail.nonEmpty()) {
196 args.head = translate(args.head, parameters.head);
197 args = args.tail;
198 parameters = parameters.tail;
199 }
200 Type parameter = parameters.head;
201 Assert.check(varargsElement != null || args.length() == 1);
202 if (varargsElement != null) {
203 while (args.nonEmpty()) {
204 args.head = translate(args.head, varargsElement);
205 args = args.tail;
206 }
207 } else {
208 args.head = translate(args.head, parameter);
209 }
210 return _args;
211 }
213 public <T extends JCTree> List<T> translateArgs(List<T> _args,
214 List<Type> parameters,
215 Type varargsElement,
216 Env<AttrContext> localEnv) {
217 Env<AttrContext> prevEnv = env;
218 try {
219 env = localEnv;
220 return translateArgs(_args, parameters, varargsElement);
221 }
222 finally {
223 env = prevEnv;
224 }
225 }
227 /** Add a bridge definition and enter corresponding method symbol in
228 * local scope of origin.
229 *
230 * @param pos The source code position to be used for the definition.
231 * @param meth The method for which a bridge needs to be added
232 * @param impl That method's implementation (possibly the method itself)
233 * @param origin The class to which the bridge will be added
234 * @param hypothetical
235 * True if the bridge method is not strictly necessary in the
236 * binary, but is represented in the symbol table to detect
237 * erasure clashes.
238 * @param bridges The list buffer to which the bridge will be added
239 */
240 void addBridge(DiagnosticPosition pos,
241 MethodSymbol meth,
242 MethodSymbol impl,
243 ClassSymbol origin,
244 boolean hypothetical,
245 ListBuffer<JCTree> bridges) {
246 make.at(pos);
247 Type origType = types.memberType(origin.type, meth);
248 Type origErasure = erasure(origType);
250 // Create a bridge method symbol and a bridge definition without a body.
251 Type bridgeType = meth.erasure(types);
252 long flags = impl.flags() & AccessFlags | SYNTHETIC | BRIDGE;
253 if (hypothetical) flags |= HYPOTHETICAL;
254 MethodSymbol bridge = new MethodSymbol(flags,
255 meth.name,
256 bridgeType,
257 origin);
258 if (!hypothetical) {
259 JCMethodDecl md = make.MethodDef(bridge, null);
261 // The bridge calls this.impl(..), if we have an implementation
262 // in the current class, super.impl(...) otherwise.
263 JCExpression receiver = (impl.owner == origin)
264 ? make.This(origin.erasure(types))
265 : make.Super(types.supertype(origin.type).tsym.erasure(types), origin);
267 // The type returned from the original method.
268 Type calltype = erasure(impl.type.getReturnType());
270 // Construct a call of this.impl(params), or super.impl(params),
271 // casting params and possibly results as needed.
272 JCExpression call =
273 make.Apply(
274 null,
275 make.Select(receiver, impl).setType(calltype),
276 translateArgs(make.Idents(md.params), origErasure.getParameterTypes(), null))
277 .setType(calltype);
278 JCStatement stat = (origErasure.getReturnType().hasTag(VOID))
279 ? make.Exec(call)
280 : make.Return(coerce(call, bridgeType.getReturnType()));
281 md.body = make.Block(0, List.of(stat));
283 // Add bridge to `bridges' buffer
284 bridges.append(md);
285 }
287 // Add bridge to scope of enclosing class and `overridden' table.
288 origin.members().enter(bridge);
289 overridden.put(bridge, meth);
290 }
292 /** Add bridge if given symbol is a non-private, non-static member
293 * of the given class, which is either defined in the class or non-final
294 * inherited, and one of the two following conditions holds:
295 * 1. The method's type changes in the given class, as compared to the
296 * class where the symbol was defined, (in this case
297 * we have extended a parameterized class with non-trivial parameters).
298 * 2. The method has an implementation with a different erased return type.
299 * (in this case we have used co-variant returns).
300 * If a bridge already exists in some other class, no new bridge is added.
301 * Instead, it is checked that the bridge symbol overrides the method symbol.
302 * (Spec ???).
303 * todo: what about bridges for privates???
304 *
305 * @param pos The source code position to be used for the definition.
306 * @param sym The symbol for which a bridge might have to be added.
307 * @param origin The class in which the bridge would go.
308 * @param bridges The list buffer to which the bridge would be added.
309 */
310 void addBridgeIfNeeded(DiagnosticPosition pos,
311 Symbol sym,
312 ClassSymbol origin,
313 ListBuffer<JCTree> bridges) {
314 if (sym.kind == MTH &&
315 sym.name != names.init &&
316 (sym.flags() & (PRIVATE | STATIC)) == 0 &&
317 (sym.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC &&
318 sym.isMemberOf(origin, types))
319 {
320 MethodSymbol meth = (MethodSymbol)sym;
321 MethodSymbol bridge = meth.binaryImplementation(origin, types);
322 MethodSymbol impl = meth.implementation(origin, types, true, overrideBridgeFilter);
323 if (bridge == null ||
324 bridge == meth ||
325 (impl != null && !bridge.owner.isSubClass(impl.owner, types))) {
326 // No bridge was added yet.
327 if (impl != null && isBridgeNeeded(meth, impl, origin.type)) {
328 addBridge(pos, meth, impl, origin, bridge==impl, bridges);
329 } else if (impl == meth
330 && impl.owner != origin
331 && (impl.flags() & FINAL) == 0
332 && (meth.flags() & (ABSTRACT|PUBLIC)) == PUBLIC
333 && (origin.flags() & PUBLIC) > (impl.owner.flags() & PUBLIC)) {
334 // this is to work around a horrible but permanent
335 // reflection design error.
336 addBridge(pos, meth, impl, origin, false, bridges);
337 }
338 } else if ((bridge.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) == SYNTHETIC) {
339 MethodSymbol other = overridden.get(bridge);
340 if (other != null && other != meth) {
341 if (impl == null || !impl.overrides(other, origin, types, true)) {
342 // Bridge for other symbol pair was added
343 log.error(pos, "name.clash.same.erasure.no.override",
344 other, other.location(origin.type, types),
345 meth, meth.location(origin.type, types));
346 }
347 }
348 } else if (!bridge.overrides(meth, origin, types, true)) {
349 // Accidental binary override without source override.
350 if (bridge.owner == origin ||
351 types.asSuper(bridge.owner.type, meth.owner) == null)
352 // Don't diagnose the problem if it would already
353 // have been reported in the superclass
354 log.error(pos, "name.clash.same.erasure.no.override",
355 bridge, bridge.location(origin.type, types),
356 meth, meth.location(origin.type, types));
357 }
358 }
359 }
360 // where
361 Filter<Symbol> overrideBridgeFilter = new Filter<Symbol>() {
362 public boolean accepts(Symbol s) {
363 return (s.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC;
364 }
365 };
366 /**
367 * @param method The symbol for which a bridge might have to be added
368 * @param impl The implementation of method
369 * @param dest The type in which the bridge would go
370 */
371 private boolean isBridgeNeeded(MethodSymbol method,
372 MethodSymbol impl,
373 Type dest) {
374 if (impl != method) {
375 // If either method or impl have different erasures as
376 // members of dest, a bridge is needed.
377 Type method_erasure = method.erasure(types);
378 if (!isSameMemberWhenErased(dest, method, method_erasure))
379 return true;
380 Type impl_erasure = impl.erasure(types);
381 if (!isSameMemberWhenErased(dest, impl, impl_erasure))
382 return true;
384 // If the erasure of the return type is different, a
385 // bridge is needed.
386 return !types.isSameType(impl_erasure.getReturnType(),
387 method_erasure.getReturnType());
388 } else {
389 // method and impl are the same...
390 if ((method.flags() & ABSTRACT) != 0) {
391 // ...and abstract so a bridge is not needed.
392 // Concrete subclasses will bridge as needed.
393 return false;
394 }
396 // The erasure of the return type is always the same
397 // for the same symbol. Reducing the three tests in
398 // the other branch to just one:
399 return !isSameMemberWhenErased(dest, method, method.erasure(types));
400 }
401 }
402 /**
403 * Lookup the method as a member of the type. Compare the
404 * erasures.
405 * @param type the class where to look for the method
406 * @param method the method to look for in class
407 * @param erasure the erasure of method
408 */
409 private boolean isSameMemberWhenErased(Type type,
410 MethodSymbol method,
411 Type erasure) {
412 return types.isSameType(erasure(types.memberType(type, method)),
413 erasure);
414 }
416 void addBridges(DiagnosticPosition pos,
417 TypeSymbol i,
418 ClassSymbol origin,
419 ListBuffer<JCTree> bridges) {
420 for (Scope.Entry e = i.members().elems; e != null; e = e.sibling)
421 addBridgeIfNeeded(pos, e.sym, origin, bridges);
422 for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail)
423 addBridges(pos, l.head.tsym, origin, bridges);
424 }
426 /** Add all necessary bridges to some class appending them to list buffer.
427 * @param pos The source code position to be used for the bridges.
428 * @param origin The class in which the bridges go.
429 * @param bridges The list buffer to which the bridges are added.
430 */
431 void addBridges(DiagnosticPosition pos, ClassSymbol origin, ListBuffer<JCTree> bridges) {
432 Type st = types.supertype(origin.type);
433 while (st.hasTag(CLASS)) {
434 // if (isSpecialization(st))
435 addBridges(pos, st.tsym, origin, bridges);
436 st = types.supertype(st);
437 }
438 for (List<Type> l = types.interfaces(origin.type); l.nonEmpty(); l = l.tail)
439 // if (isSpecialization(l.head))
440 addBridges(pos, l.head.tsym, origin, bridges);
441 }
443 /* ************************************************************************
444 * Visitor methods
445 *************************************************************************/
447 /** Visitor argument: proto-type.
448 */
449 private Type pt;
451 /** Visitor method: perform a type translation on tree.
452 */
453 public <T extends JCTree> T translate(T tree, Type pt) {
454 Type prevPt = this.pt;
455 try {
456 this.pt = pt;
457 return translate(tree);
458 } finally {
459 this.pt = prevPt;
460 }
461 }
463 /** Visitor method: perform a type translation on list of trees.
464 */
465 public <T extends JCTree> List<T> translate(List<T> trees, Type pt) {
466 Type prevPt = this.pt;
467 List<T> res;
468 try {
469 this.pt = pt;
470 res = translate(trees);
471 } finally {
472 this.pt = prevPt;
473 }
474 return res;
475 }
477 public void visitClassDef(JCClassDecl tree) {
478 translateClass(tree.sym);
479 result = tree;
480 }
482 JCTree currentMethod = null;
483 public void visitMethodDef(JCMethodDecl tree) {
484 JCTree previousMethod = currentMethod;
485 try {
486 currentMethod = tree;
487 tree.restype = translate(tree.restype, null);
488 tree.typarams = List.nil();
489 tree.params = translateVarDefs(tree.params);
490 tree.recvparam = translate(tree.recvparam, null);
491 tree.thrown = translate(tree.thrown, null);
492 tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType());
493 tree.type = erasure(tree.type);
494 result = tree;
495 } finally {
496 currentMethod = previousMethod;
497 }
499 // Check that we do not introduce a name clash by erasing types.
500 for (Scope.Entry e = tree.sym.owner.members().lookup(tree.name);
501 e.sym != null;
502 e = e.next()) {
503 if (e.sym != tree.sym &&
504 types.isSameType(erasure(e.sym.type), tree.type)) {
505 log.error(tree.pos(),
506 "name.clash.same.erasure", tree.sym,
507 e.sym);
508 return;
509 }
510 }
511 }
513 public void visitVarDef(JCVariableDecl tree) {
514 tree.vartype = translate(tree.vartype, null);
515 tree.init = translate(tree.init, tree.sym.erasure(types));
516 tree.type = erasure(tree.type);
517 result = tree;
518 }
520 public void visitDoLoop(JCDoWhileLoop tree) {
521 tree.body = translate(tree.body);
522 tree.cond = translate(tree.cond, syms.booleanType);
523 result = tree;
524 }
526 public void visitWhileLoop(JCWhileLoop tree) {
527 tree.cond = translate(tree.cond, syms.booleanType);
528 tree.body = translate(tree.body);
529 result = tree;
530 }
532 public void visitForLoop(JCForLoop tree) {
533 tree.init = translate(tree.init, null);
534 if (tree.cond != null)
535 tree.cond = translate(tree.cond, syms.booleanType);
536 tree.step = translate(tree.step, null);
537 tree.body = translate(tree.body);
538 result = tree;
539 }
541 public void visitForeachLoop(JCEnhancedForLoop tree) {
542 tree.var = translate(tree.var, null);
543 Type iterableType = tree.expr.type;
544 tree.expr = translate(tree.expr, erasure(tree.expr.type));
545 if (types.elemtype(tree.expr.type) == null)
546 tree.expr.type = iterableType; // preserve type for Lower
547 tree.body = translate(tree.body);
548 result = tree;
549 }
551 public void visitLambda(JCLambda tree) {
552 JCTree prevMethod = currentMethod;
553 try {
554 currentMethod = null;
555 tree.params = translate(tree.params);
556 tree.body = translate(tree.body, null);
557 tree.type = erasure(tree.type);
558 result = tree;
559 }
560 finally {
561 currentMethod = prevMethod;
562 }
563 }
565 public void visitSwitch(JCSwitch tree) {
566 Type selsuper = types.supertype(tree.selector.type);
567 boolean enumSwitch = selsuper != null &&
568 selsuper.tsym == syms.enumSym;
569 Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType;
570 tree.selector = translate(tree.selector, target);
571 tree.cases = translateCases(tree.cases);
572 result = tree;
573 }
575 public void visitCase(JCCase tree) {
576 tree.pat = translate(tree.pat, null);
577 tree.stats = translate(tree.stats);
578 result = tree;
579 }
581 public void visitSynchronized(JCSynchronized tree) {
582 tree.lock = translate(tree.lock, erasure(tree.lock.type));
583 tree.body = translate(tree.body);
584 result = tree;
585 }
587 public void visitTry(JCTry tree) {
588 tree.resources = translate(tree.resources, syms.autoCloseableType);
589 tree.body = translate(tree.body);
590 tree.catchers = translateCatchers(tree.catchers);
591 tree.finalizer = translate(tree.finalizer);
592 result = tree;
593 }
595 public void visitConditional(JCConditional tree) {
596 tree.cond = translate(tree.cond, syms.booleanType);
597 tree.truepart = translate(tree.truepart, erasure(tree.type));
598 tree.falsepart = translate(tree.falsepart, erasure(tree.type));
599 tree.type = erasure(tree.type);
600 result = retype(tree, tree.type, pt);
601 }
603 public void visitIf(JCIf tree) {
604 tree.cond = translate(tree.cond, syms.booleanType);
605 tree.thenpart = translate(tree.thenpart);
606 tree.elsepart = translate(tree.elsepart);
607 result = tree;
608 }
610 public void visitExec(JCExpressionStatement tree) {
611 tree.expr = translate(tree.expr, null);
612 result = tree;
613 }
615 public void visitReturn(JCReturn tree) {
616 tree.expr = translate(tree.expr, currentMethod != null ? types.erasure(currentMethod.type).getReturnType() : null);
617 result = tree;
618 }
620 public void visitThrow(JCThrow tree) {
621 tree.expr = translate(tree.expr, erasure(tree.expr.type));
622 result = tree;
623 }
625 public void visitAssert(JCAssert tree) {
626 tree.cond = translate(tree.cond, syms.booleanType);
627 if (tree.detail != null)
628 tree.detail = translate(tree.detail, erasure(tree.detail.type));
629 result = tree;
630 }
632 public void visitApply(JCMethodInvocation tree) {
633 tree.meth = translate(tree.meth, null);
634 Symbol meth = TreeInfo.symbol(tree.meth);
635 Type mt = meth.erasure(types);
636 List<Type> argtypes = mt.getParameterTypes();
637 if (allowEnums &&
638 meth.name==names.init &&
639 meth.owner == syms.enumSym)
640 argtypes = argtypes.tail.tail;
641 if (tree.varargsElement != null)
642 tree.varargsElement = types.erasure(tree.varargsElement);
643 else
644 Assert.check(tree.args.length() == argtypes.length());
645 tree.args = translateArgs(tree.args, argtypes, tree.varargsElement);
647 tree.type = types.erasure(tree.type);
648 // Insert casts of method invocation results as needed.
649 result = retype(tree, mt.getReturnType(), pt);
650 }
652 public void visitNewClass(JCNewClass tree) {
653 if (tree.encl != null)
654 tree.encl = translate(tree.encl, erasure(tree.encl.type));
655 tree.clazz = translate(tree.clazz, null);
656 if (tree.varargsElement != null)
657 tree.varargsElement = types.erasure(tree.varargsElement);
658 tree.args = translateArgs(
659 tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement);
660 tree.def = translate(tree.def, null);
661 if (tree.constructorType != null)
662 tree.constructorType = erasure(tree.constructorType);
663 tree.type = erasure(tree.type);
664 result = tree;
665 }
667 public void visitNewArray(JCNewArray tree) {
668 tree.elemtype = translate(tree.elemtype, null);
669 translate(tree.dims, syms.intType);
670 if (tree.type != null) {
671 tree.elems = translate(tree.elems, erasure(types.elemtype(tree.type)));
672 tree.type = erasure(tree.type);
673 } else {
674 tree.elems = translate(tree.elems, null);
675 }
677 result = tree;
678 }
680 public void visitParens(JCParens tree) {
681 tree.expr = translate(tree.expr, pt);
682 tree.type = erasure(tree.type);
683 result = tree;
684 }
686 public void visitAssign(JCAssign tree) {
687 tree.lhs = translate(tree.lhs, null);
688 tree.rhs = translate(tree.rhs, erasure(tree.lhs.type));
689 tree.type = erasure(tree.type);
690 result = tree;
691 }
693 public void visitAssignop(JCAssignOp tree) {
694 tree.lhs = translate(tree.lhs, null);
695 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
696 tree.type = erasure(tree.type);
697 result = tree;
698 }
700 public void visitUnary(JCUnary tree) {
701 tree.arg = translate(tree.arg, tree.operator.type.getParameterTypes().head);
702 result = tree;
703 }
705 public void visitBinary(JCBinary tree) {
706 tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head);
707 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
708 result = tree;
709 }
711 public void visitTypeCast(JCTypeCast tree) {
712 tree.clazz = translate(tree.clazz, null);
713 tree.type = erasure(tree.type);
714 tree.expr = translate(tree.expr, tree.type);
715 result = tree;
716 }
718 public void visitTypeTest(JCInstanceOf tree) {
719 tree.expr = translate(tree.expr, null);
720 tree.clazz = translate(tree.clazz, null);
721 result = tree;
722 }
724 public void visitIndexed(JCArrayAccess tree) {
725 tree.indexed = translate(tree.indexed, erasure(tree.indexed.type));
726 tree.index = translate(tree.index, syms.intType);
728 // Insert casts of indexed expressions as needed.
729 result = retype(tree, types.elemtype(tree.indexed.type), pt);
730 }
732 // There ought to be nothing to rewrite here;
733 // we don't generate code.
734 public void visitAnnotation(JCAnnotation tree) {
735 result = tree;
736 }
738 public void visitIdent(JCIdent tree) {
739 Type et = tree.sym.erasure(types);
741 // Map type variables to their bounds.
742 if (tree.sym.kind == TYP && tree.sym.type.hasTag(TYPEVAR)) {
743 result = make.at(tree.pos).Type(et);
744 } else
745 // Map constants expressions to themselves.
746 if (tree.type.constValue() != null) {
747 result = tree;
748 }
749 // Insert casts of variable uses as needed.
750 else if (tree.sym.kind == VAR) {
751 result = retype(tree, et, pt);
752 }
753 else {
754 tree.type = erasure(tree.type);
755 result = tree;
756 }
757 }
759 public void visitSelect(JCFieldAccess tree) {
760 Type t = tree.selected.type;
761 while (t.hasTag(TYPEVAR))
762 t = t.getUpperBound();
763 if (t.isCompound()) {
764 if ((tree.sym.flags() & IPROXY) != 0) {
765 tree.sym = ((MethodSymbol)tree.sym).
766 implemented((TypeSymbol)tree.sym.owner, types);
767 }
768 tree.selected = coerce(
769 translate(tree.selected, erasure(tree.selected.type)),
770 erasure(tree.sym.owner.type));
771 } else
772 tree.selected = translate(tree.selected, erasure(t));
774 // Map constants expressions to themselves.
775 if (tree.type.constValue() != null) {
776 result = tree;
777 }
778 // Insert casts of variable uses as needed.
779 else if (tree.sym.kind == VAR) {
780 result = retype(tree, tree.sym.erasure(types), pt);
781 }
782 else {
783 tree.type = erasure(tree.type);
784 result = tree;
785 }
786 }
788 public void visitReference(JCMemberReference tree) {
789 tree.expr = translate(tree.expr, null);
790 tree.type = erasure(tree.type);
791 result = tree;
792 }
794 public void visitTypeArray(JCArrayTypeTree tree) {
795 tree.elemtype = translate(tree.elemtype, null);
796 tree.type = erasure(tree.type);
797 result = tree;
798 }
800 /** Visitor method for parameterized types.
801 */
802 public void visitTypeApply(JCTypeApply tree) {
803 JCTree clazz = translate(tree.clazz, null);
804 result = clazz;
805 }
807 public void visitTypeIntersection(JCTypeIntersection tree) {
808 tree.bounds = translate(tree.bounds, null);
809 tree.type = erasure(tree.type);
810 result = tree;
811 }
813 /**************************************************************************
814 * utility methods
815 *************************************************************************/
817 private Type erasure(Type t) {
818 return types.erasure(t);
819 }
821 private boolean boundsRestricted(ClassSymbol c) {
822 Type st = types.supertype(c.type);
823 if (st.isParameterized()) {
824 List<Type> actuals = st.allparams();
825 List<Type> formals = st.tsym.type.allparams();
826 while (!actuals.isEmpty() && !formals.isEmpty()) {
827 Type actual = actuals.head;
828 Type formal = formals.head;
830 if (!types.isSameType(types.erasure(actual),
831 types.erasure(formal)))
832 return true;
834 actuals = actuals.tail;
835 formals = formals.tail;
836 }
837 }
838 return false;
839 }
841 private List<JCTree> addOverrideBridgesIfNeeded(DiagnosticPosition pos,
842 final ClassSymbol c) {
843 ListBuffer<JCTree> buf = ListBuffer.lb();
844 if (c.isInterface() || !boundsRestricted(c))
845 return buf.toList();
846 Type t = types.supertype(c.type);
847 Scope s = t.tsym.members();
848 if (s.elems != null) {
849 for (Symbol sym : s.getElements(new NeedsOverridBridgeFilter(c))) {
851 MethodSymbol m = (MethodSymbol)sym;
852 MethodSymbol member = (MethodSymbol)m.asMemberOf(c.type, types);
853 MethodSymbol impl = m.implementation(c, types, false);
855 if ((impl == null || impl.owner != c) &&
856 !types.isSameType(member.erasure(types), m.erasure(types))) {
857 addOverrideBridges(pos, m, member, c, buf);
858 }
859 }
860 }
861 return buf.toList();
862 }
863 // where
864 class NeedsOverridBridgeFilter implements Filter<Symbol> {
866 ClassSymbol c;
868 NeedsOverridBridgeFilter(ClassSymbol c) {
869 this.c = c;
870 }
871 public boolean accepts(Symbol s) {
872 return s.kind == MTH &&
873 !s.isConstructor() &&
874 s.isInheritedIn(c, types) &&
875 (s.flags() & FINAL) == 0 &&
876 (s.flags() & (SYNTHETIC | OVERRIDE_BRIDGE)) != SYNTHETIC;
877 }
878 }
880 private void addOverrideBridges(DiagnosticPosition pos,
881 MethodSymbol impl,
882 MethodSymbol member,
883 ClassSymbol c,
884 ListBuffer<JCTree> bridges) {
885 Type implErasure = impl.erasure(types);
886 long flags = (impl.flags() & AccessFlags) | SYNTHETIC | BRIDGE | OVERRIDE_BRIDGE;
887 member = new MethodSymbol(flags, member.name, member.type, c);
888 JCMethodDecl md = make.MethodDef(member, null);
889 JCExpression receiver = make.Super(types.supertype(c.type).tsym.erasure(types), c);
890 Type calltype = erasure(impl.type.getReturnType());
891 JCExpression call =
892 make.Apply(null,
893 make.Select(receiver, impl).setType(calltype),
894 translateArgs(make.Idents(md.params),
895 implErasure.getParameterTypes(), null))
896 .setType(calltype);
897 JCStatement stat = (member.getReturnType().hasTag(VOID))
898 ? make.Exec(call)
899 : make.Return(coerce(call, member.erasure(types).getReturnType()));
900 md.body = make.Block(0, List.of(stat));
901 c.members().enter(member);
902 bridges.append(md);
903 }
905 /**************************************************************************
906 * main method
907 *************************************************************************/
909 private Env<AttrContext> env;
911 private static final String statePreviousToFlowAssertMsg =
912 "The current compile state [%s] of class %s is previous to FLOW";
914 void translateClass(ClassSymbol c) {
915 Type st = types.supertype(c.type);
916 // process superclass before derived
917 if (st.hasTag(CLASS)) {
918 translateClass((ClassSymbol)st.tsym);
919 }
921 Env<AttrContext> myEnv = enter.typeEnvs.remove(c);
922 if (myEnv == null) {
923 return;
924 }
926 /* The two assertions below are set for early detection of any attempt
927 * to translate a class that:
928 *
929 * 1) has no compile state being it the most outer class.
930 * We accept this condition for inner classes.
931 *
932 * 2) has a compile state which is previous to Flow state.
933 */
934 boolean envHasCompState = compileStates.get(myEnv) != null;
935 if (!envHasCompState && c.outermostClass() == c) {
936 Assert.error("No info for outermost class: " + myEnv.enclClass.sym);
937 }
939 if (envHasCompState &&
940 CompileState.FLOW.isAfter(compileStates.get(myEnv))) {
941 Assert.error(String.format(statePreviousToFlowAssertMsg,
942 compileStates.get(myEnv), myEnv.enclClass.sym));
943 }
945 Env<AttrContext> oldEnv = env;
946 try {
947 env = myEnv;
948 // class has not been translated yet
950 TreeMaker savedMake = make;
951 Type savedPt = pt;
952 make = make.forToplevel(env.toplevel);
953 pt = null;
954 try {
955 JCClassDecl tree = (JCClassDecl) env.tree;
956 tree.typarams = List.nil();
957 super.visitClassDef(tree);
958 make.at(tree.pos);
959 if (addBridges) {
960 ListBuffer<JCTree> bridges = new ListBuffer<JCTree>();
961 if (false) //see CR: 6996415
962 bridges.appendList(addOverrideBridgesIfNeeded(tree, c));
963 if ((tree.sym.flags() & INTERFACE) == 0)
964 addBridges(tree.pos(), tree.sym, bridges);
965 tree.defs = bridges.toList().prependList(tree.defs);
966 }
967 tree.type = erasure(tree.type);
968 } finally {
969 make = savedMake;
970 pt = savedPt;
971 }
972 } finally {
973 env = oldEnv;
974 }
975 }
977 /** Translate a toplevel class definition.
978 * @param cdef The definition to be translated.
979 */
980 public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) {
981 // note that this method does NOT support recursion.
982 this.make = make;
983 pt = null;
984 return translate(cdef, null);
985 }
986 }