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