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