1 /* 2 * Copyright (c) 1999, 2008, 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 26 package com.sun.tools.javac.comp; 27 28 import java.util.*; 29 30 import javax.lang.model.element.ElementKind; 31 32 import com.sun.tools.javac.code.*; 33 import com.sun.tools.javac.code.Symbol.*; 34 import com.sun.tools.javac.tree.*; 35 import com.sun.tools.javac.tree.JCTree.*; 36 import com.sun.tools.javac.util.*; 37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 38 import com.sun.tools.javac.util.List; 39 40 import static com.sun.tools.javac.code.Flags.*; 41 import static com.sun.tools.javac.code.Kinds.*; 42 import static com.sun.tools.javac.code.TypeTags.*; 43 44 /** This pass translates Generic Java to conventional Java. 45 * 46 * <p><b>This is NOT part of any supported API. 47 * If you write code that depends on this, you do so at your own risk. 48 * This code and its internal interfaces are subject to change or 49 * deletion without notice.</b> 50 */ 51 public class TransTypes extends TreeTranslator { 52 /** The context key for the TransTypes phase. */ 53 protected static final Context.Key<TransTypes> transTypesKey = 54 new Context.Key<TransTypes>(); 55 56 /** Get the instance for this context. */ 57 public static TransTypes instance(Context context) { 58 TransTypes instance = context.get(transTypesKey); 59 if (instance == null) 60 instance = new TransTypes(context); 61 return instance; 62 } 63 64 private Names names; 65 private Log log; 66 private Symtab syms; 67 private TreeMaker make; 68 private Enter enter; 69 private boolean allowEnums; 70 private Types types; 71 private final Resolve resolve; 72 private final TypeAnnotations typeAnnotations; 73 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; 80 81 protected TransTypes(Context context) { 82 context.put(transTypesKey, this); 83 names = Names.instance(context); 84 log = Log.instance(context); 85 syms = Symtab.instance(context); 86 enter = Enter.instance(context); 87 overridden = new HashMap<MethodSymbol,MethodSymbol>(); 88 Source source = Source.instance(context); 89 allowEnums = source.allowEnums(); 90 addBridges = source.addBridges(); 91 types = Types.instance(context); 92 make = TreeMaker.instance(context); 93 resolve = Resolve.instance(context); 94 typeAnnotations = TypeAnnotations.instance(context); 95 } 96 97 /** A hashtable mapping bridge methods to the methods they override after 98 * type erasure. 99 */ 100 Map<MethodSymbol,MethodSymbol> overridden; 101 102 /** Construct an attributed tree for a cast of expression to target type, 103 * unless it already has precisely that type. 104 * @param tree The expression tree. 105 * @param target The target type. 106 */ 107 JCExpression cast(JCExpression tree, Type target) { 108 int oldpos = make.pos; 109 make.at(tree.pos); 110 if (!types.isSameType(tree.type, target)) { 111 if (!resolve.isAccessible(env, target.tsym)) 112 resolve.logAccessError(env, tree, target); 113 tree = make.TypeCast(make.Type(target), tree).setType(target); 114 } 115 make.pos = oldpos; 116 return tree; 117 } 118 119 /** Construct an attributed tree to coerce an expression to some erased 120 * target type, unless the expression is already assignable to that type. 121 * If target type is a constant type, use its base type instead. 122 * @param tree The expression tree. 123 * @param target The target type. 124 */ 125 JCExpression coerce(JCExpression tree, Type target) { 126 Type btarget = target.baseType(); 127 if (tree.type.isPrimitive() == target.isPrimitive()) { 128 return types.isAssignable(tree.type, btarget, Warner.noWarnings) 129 ? tree 130 : cast(tree, btarget); 131 } 132 return tree; 133 } 134 135 /** Given an erased reference type, assume this type as the tree's type. 136 * Then, coerce to some given target type unless target type is null. 137 * This operation is used in situations like the following: 138 * 139 * class Cell<A> { A value; } 140 * ... 141 * Cell<Integer> cell; 142 * Integer x = cell.value; 143 * 144 * Since the erasure of Cell.value is Object, but the type 145 * of cell.value in the assignment is Integer, we need to 146 * adjust the original type of cell.value to Object, and insert 147 * a cast to Integer. That is, the last assignment becomes: 148 * 149 * Integer x = (Integer)cell.value; 150 * 151 * @param tree The expression tree whose type might need adjustment. 152 * @param erasedType The expression's type after erasure. 153 * @param target The target type, which is usually the erasure of the 154 * expression's original type. 155 */ 156 JCExpression retype(JCExpression tree, Type erasedType, Type target) { 157 // System.err.println("retype " + tree + " to " + erasedType);//DEBUG 158 if (erasedType.tag > lastBaseTag) { 159 if (target != null && target.isPrimitive()) 160 target = erasure(tree.type); 161 tree.type = erasedType; 162 if (target != null) return coerce(tree, target); 163 } 164 return tree; 165 } 166 167 /** Translate method argument list, casting each argument 168 * to its corresponding type in a list of target types. 169 * @param _args The method argument list. 170 * @param parameters The list of target types. 171 * @param varargsElement The erasure of the varargs element type, 172 * or null if translating a non-varargs invocation 173 */ 174 <T extends JCTree> List<T> translateArgs(List<T> _args, 175 List<Type> parameters, 176 Type varargsElement) { 177 if (parameters.isEmpty()) return _args; 178 List<T> args = _args; 179 while (parameters.tail.nonEmpty()) { 180 args.head = translate(args.head, parameters.head); 181 args = args.tail; 182 parameters = parameters.tail; 183 } 184 Type parameter = parameters.head; 185 assert varargsElement != null || args.length() == 1; 186 if (varargsElement != null) { 187 while (args.nonEmpty()) { 188 args.head = translate(args.head, varargsElement); 189 args = args.tail; 190 } 191 } else { 192 args.head = translate(args.head, parameter); 193 } 194 return _args; 195 } 196 197 /** Add a bridge definition and enter corresponding method symbol in 198 * local scope of origin. 199 * 200 * @param pos The source code position to be used for the definition. 201 * @param meth The method for which a bridge needs to be added 202 * @param impl That method's implementation (possibly the method itself) 203 * @param origin The class to which the bridge will be added 204 * @param hypothetical 205 * True if the bridge method is not strictly necessary in the 206 * binary, but is represented in the symbol table to detect 207 * erasure clashes. 208 * @param bridges The list buffer to which the bridge will be added 209 */ 210 void addBridge(DiagnosticPosition pos, 211 MethodSymbol meth, 212 MethodSymbol impl, 213 ClassSymbol origin, 214 boolean hypothetical, 215 ListBuffer<JCTree> bridges) { 216 make.at(pos); 217 Type origType = types.memberType(origin.type, meth); 218 Type origErasure = erasure(origType); 219 220 // Create a bridge method symbol and a bridge definition without a body. 221 Type bridgeType = meth.erasure(types); 222 long flags = impl.flags() & AccessFlags | SYNTHETIC | BRIDGE; 223 if (hypothetical) flags |= HYPOTHETICAL; 224 MethodSymbol bridge = new MethodSymbol(flags, 225 meth.name, 226 bridgeType, 227 origin); 228 if (!hypothetical) { 229 JCMethodDecl md = make.MethodDef(bridge, null); 230 231 // The bridge calls this.impl(..), if we have an implementation 232 // in the current class, super.impl(...) otherwise. 233 JCExpression receiver = (impl.owner == origin) 234 ? make.This(origin.erasure(types)) 235 : make.Super(types.supertype(origin.type).tsym.erasure(types), origin); 236 237 // The type returned from the original method. 238 Type calltype = erasure(impl.type.getReturnType()); 239 240 // Construct a call of this.impl(params), or super.impl(params), 241 // casting params and possibly results as needed. 242 JCExpression call = 243 make.Apply( 244 null, 245 make.Select(receiver, impl).setType(calltype), 246 translateArgs(make.Idents(md.params), origErasure.getParameterTypes(), null)) 247 .setType(calltype); 248 JCStatement stat = (origErasure.getReturnType().tag == VOID) 249 ? make.Exec(call) 250 : make.Return(coerce(call, bridgeType.getReturnType())); 251 md.body = make.Block(0, List.of(stat)); 252 253 // Add bridge to `bridges' buffer 254 bridges.append(md); 255 } 256 257 // Add bridge to scope of enclosing class and `overridden' table. 258 origin.members().enter(bridge); 259 overridden.put(bridge, meth); 260 } 261 262 /** Add bridge if given symbol is a non-private, non-static member 263 * of the given class, which is either defined in the class or non-final 264 * inherited, and one of the two following conditions holds: 265 * 1. The method's type changes in the given class, as compared to the 266 * class where the symbol was defined, (in this case 267 * we have extended a parameterized class with non-trivial parameters). 268 * 2. The method has an implementation with a different erased return type. 269 * (in this case we have used co-variant returns). 270 * If a bridge already exists in some other class, no new bridge is added. 271 * Instead, it is checked that the bridge symbol overrides the method symbol. 272 * (Spec ???). 273 * todo: what about bridges for privates??? 274 * 275 * @param pos The source code position to be used for the definition. 276 * @param sym The symbol for which a bridge might have to be added. 277 * @param origin The class in which the bridge would go. 278 * @param bridges The list buffer to which the bridge would be added. 279 */ 280 void addBridgeIfNeeded(DiagnosticPosition pos, 281 Symbol sym, 282 ClassSymbol origin, 283 ListBuffer<JCTree> bridges) { 284 if (sym.kind == MTH && 285 sym.name != names.init && 286 (sym.flags() & (PRIVATE | SYNTHETIC | STATIC)) == 0 && 287 sym.isMemberOf(origin, types)) 288 { 289 MethodSymbol meth = (MethodSymbol)sym; 290 MethodSymbol bridge = meth.binaryImplementation(origin, types); 291 MethodSymbol impl = meth.implementation(origin, types, true); 292 if (bridge == null || 293 bridge == meth || 294 (impl != null && !bridge.owner.isSubClass(impl.owner, types))) { 295 // No bridge was added yet. 296 if (impl != null && isBridgeNeeded(meth, impl, origin.type)) { 297 addBridge(pos, meth, impl, origin, bridge==impl, bridges); 298 } else if (impl == meth 299 && impl.owner != origin 300 && (impl.flags() & FINAL) == 0 301 && (meth.flags() & (ABSTRACT|PUBLIC)) == PUBLIC 302 && (origin.flags() & PUBLIC) > (impl.owner.flags() & PUBLIC)) { 303 // this is to work around a horrible but permanent 304 // reflection design error. 305 addBridge(pos, meth, impl, origin, false, bridges); 306 } 307 } else if ((bridge.flags() & SYNTHETIC) != 0) { 308 MethodSymbol other = overridden.get(bridge); 309 if (other != null && other != meth) { 310 if (impl == null || !impl.overrides(other, origin, types, true)) { 311 // Bridge for other symbol pair was added 312 log.error(pos, "name.clash.same.erasure.no.override", 313 other, other.location(origin.type, types), 314 meth, meth.location(origin.type, types)); 315 } 316 } 317 } else if (!bridge.overrides(meth, origin, types, true)) { 318 // Accidental binary override without source override. 319 if (bridge.owner == origin || 320 types.asSuper(bridge.owner.type, meth.owner) == null) 321 // Don't diagnose the problem if it would already 322 // have been reported in the superclass 323 log.error(pos, "name.clash.same.erasure.no.override", 324 bridge, bridge.location(origin.type, types), 325 meth, meth.location(origin.type, types)); 326 } 327 } 328 } 329 // where 330 /** 331 * @param method The symbol for which a bridge might have to be added 332 * @param impl The implementation of method 333 * @param dest The type in which the bridge would go 334 */ 335 private boolean isBridgeNeeded(MethodSymbol method, 336 MethodSymbol impl, 337 Type dest) { 338 if (impl != method) { 339 // If either method or impl have different erasures as 340 // members of dest, a bridge is needed. 341 Type method_erasure = method.erasure(types); 342 if (!isSameMemberWhenErased(dest, method, method_erasure)) 343 return true; 344 Type impl_erasure = impl.erasure(types); 345 if (!isSameMemberWhenErased(dest, impl, impl_erasure)) 346 return true; 347 348 // If the erasure of the return type is different, a 349 // bridge is needed. 350 return !types.isSameType(impl_erasure.getReturnType(), 351 method_erasure.getReturnType()); 352 } else { 353 // method and impl are the same... 354 if ((method.flags() & ABSTRACT) != 0) { 355 // ...and abstract so a bridge is not needed. 356 // Concrete subclasses will bridge as needed. 357 return false; 358 } 359 360 // The erasure of the return type is always the same 361 // for the same symbol. Reducing the three tests in 362 // the other branch to just one: 363 return !isSameMemberWhenErased(dest, method, method.erasure(types)); 364 } 365 } 366 /** 367 * Lookup the method as a member of the type. Compare the 368 * erasures. 369 * @param type the class where to look for the method 370 * @param method the method to look for in class 371 * @param erasure the erasure of method 372 */ 373 private boolean isSameMemberWhenErased(Type type, 374 MethodSymbol method, 375 Type erasure) { 376 return types.isSameType(erasure(types.memberType(type, method)), 377 erasure); 378 } 379 380 void addBridges(DiagnosticPosition pos, 381 TypeSymbol i, 382 ClassSymbol origin, 383 ListBuffer<JCTree> bridges) { 384 for (Scope.Entry e = i.members().elems; e != null; e = e.sibling) 385 addBridgeIfNeeded(pos, e.sym, origin, bridges); 386 for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail) 387 addBridges(pos, l.head.tsym, origin, bridges); 388 } 389 390 /** Add all necessary bridges to some class appending them to list buffer. 391 * @param pos The source code position to be used for the bridges. 392 * @param origin The class in which the bridges go. 393 * @param bridges The list buffer to which the bridges are added. 394 */ 395 void addBridges(DiagnosticPosition pos, ClassSymbol origin, ListBuffer<JCTree> bridges) { 396 Type st = types.supertype(origin.type); 397 while (st.tag == CLASS) { 398 // if (isSpecialization(st)) 399 addBridges(pos, st.tsym, origin, bridges); 400 st = types.supertype(st); 401 } 402 for (List<Type> l = types.interfaces(origin.type); l.nonEmpty(); l = l.tail) 403 // if (isSpecialization(l.head)) 404 addBridges(pos, l.head.tsym, origin, bridges); 405 } 406 407 /* ************************************************************************ 408 * Visitor methods 409 *************************************************************************/ 410 411 /** Visitor argument: proto-type. 412 */ 413 private Type pt; 414 415 /** Visitor method: perform a type translation on tree. 416 */ 417 public <T extends JCTree> T translate(T tree, Type pt) { 418 Type prevPt = this.pt; 419 try { 420 this.pt = pt; 421 return translate(tree); 422 } finally { 423 this.pt = prevPt; 424 } 425 } 426 427 /** Visitor method: perform a type translation on list of trees. 428 */ 429 public <T extends JCTree> List<T> translate(List<T> trees, Type pt) { 430 Type prevPt = this.pt; 431 List<T> res; 432 try { 433 this.pt = pt; 434 res = translate(trees); 435 } finally { 436 this.pt = prevPt; 437 } 438 return res; 439 } 440 441 public void visitClassDef(JCClassDecl tree) { 442 typeAnnotations.taFillAndLift(tree, true); 443 translateClass(tree.sym); 444 result = tree; 445 } 446 447 JCMethodDecl currentMethod = null; 448 public void visitMethodDef(JCMethodDecl tree) { 449 tree.sym.typeAnnotations = tree.sym.typeAnnotations; 450 JCMethodDecl previousMethod = currentMethod; 451 try { 452 currentMethod = tree; 453 tree.restype = translate(tree.restype, null); 454 tree.typarams = List.nil(); 455 tree.params = translateVarDefs(tree.params); 456 tree.thrown = translate(tree.thrown, null); 457 tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType()); 458 tree.type = erasure(tree.type); 459 result = tree; 460 } finally { 461 currentMethod = previousMethod; 462 } 463 464 // Check that we do not introduce a name clash by erasing types. 465 for (Scope.Entry e = tree.sym.owner.members().lookup(tree.name); 466 e.sym != null; 467 e = e.next()) { 468 if (e.sym != tree.sym && 469 types.isSameType(erasure(e.sym.type), tree.type)) { 470 log.error(tree.pos(), 471 "name.clash.same.erasure", tree.sym, 472 e.sym); 473 return; 474 } 475 } 476 } 477 478 public void visitVarDef(JCVariableDecl tree) { 479 tree.vartype = translate(tree.vartype, null); 480 tree.init = translate(tree.init, tree.sym.erasure(types)); 481 tree.type = erasure(tree.type); 482 result = tree; 483 } 484 485 public void visitDoLoop(JCDoWhileLoop tree) { 486 tree.body = translate(tree.body); 487 tree.cond = translate(tree.cond, syms.booleanType); 488 result = tree; 489 } 490 491 public void visitWhileLoop(JCWhileLoop tree) { 492 tree.cond = translate(tree.cond, syms.booleanType); 493 tree.body = translate(tree.body); 494 result = tree; 495 } 496 497 public void visitForLoop(JCForLoop tree) { 498 tree.init = translate(tree.init, null); 499 if (tree.cond != null) 500 tree.cond = translate(tree.cond, syms.booleanType); 501 tree.step = translate(tree.step, null); 502 tree.body = translate(tree.body); 503 result = tree; 504 } 505 506 public void visitForeachLoop(JCEnhancedForLoop tree) { 507 tree.var = translate(tree.var, null); 508 Type iterableType = tree.expr.type; 509 tree.expr = translate(tree.expr, erasure(tree.expr.type)); 510 if (types.elemtype(tree.expr.type) == null) 511 tree.expr.type = iterableType; // preserve type for Lower 512 tree.body = translate(tree.body); 513 result = tree; 514 } 515 516 public void visitSwitch(JCSwitch tree) { 517 Type selsuper = types.supertype(tree.selector.type); 518 boolean enumSwitch = selsuper != null && 519 selsuper.tsym == syms.enumSym; 520 Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType; 521 tree.selector = translate(tree.selector, target); 522 tree.cases = translateCases(tree.cases); 523 result = tree; 524 } 525 526 public void visitCase(JCCase tree) { 527 tree.pat = translate(tree.pat, null); 528 tree.stats = translate(tree.stats); 529 result = tree; 530 } 531 532 public void visitSynchronized(JCSynchronized tree) { 533 tree.lock = translate(tree.lock, erasure(tree.lock.type)); 534 tree.body = translate(tree.body); 535 result = tree; 536 } 537 538 public void visitConditional(JCConditional tree) { 539 tree.cond = translate(tree.cond, syms.booleanType); 540 tree.truepart = translate(tree.truepart, erasure(tree.type)); 541 tree.falsepart = translate(tree.falsepart, erasure(tree.type)); 542 tree.type = erasure(tree.type); 543 result = retype(tree, tree.type, pt); 544 } 545 546 public void visitIf(JCIf tree) { 547 tree.cond = translate(tree.cond, syms.booleanType); 548 tree.thenpart = translate(tree.thenpart); 549 tree.elsepart = translate(tree.elsepart); 550 result = tree; 551 } 552 553 public void visitExec(JCExpressionStatement tree) { 554 tree.expr = translate(tree.expr, null); 555 result = tree; 556 } 557 558 public void visitReturn(JCReturn tree) { 559 tree.expr = translate(tree.expr, currentMethod.sym.erasure(types).getReturnType()); 560 result = tree; 561 } 562 563 public void visitThrow(JCThrow tree) { 564 tree.expr = translate(tree.expr, erasure(tree.expr.type)); 565 result = tree; 566 } 567 568 public void visitAssert(JCAssert tree) { 569 tree.cond = translate(tree.cond, syms.booleanType); 570 if (tree.detail != null) 571 tree.detail = translate(tree.detail, erasure(tree.detail.type)); 572 result = tree; 573 } 574 575 public void visitApply(JCMethodInvocation tree) { 576 tree.meth = translate(tree.meth, null); 577 Symbol meth = TreeInfo.symbol(tree.meth); 578 Type mt = meth.erasure(types); 579 List<Type> argtypes = mt.getParameterTypes(); 580 if (allowEnums && 581 meth.name==names.init && 582 meth.owner == syms.enumSym) 583 argtypes = argtypes.tail.tail; 584 if (tree.varargsElement != null) 585 tree.varargsElement = types.erasure(tree.varargsElement); 586 else 587 assert tree.args.length() == argtypes.length(); 588 tree.args = translateArgs(tree.args, argtypes, tree.varargsElement); 589 590 // Insert casts of method invocation results as needed. 591 result = retype(tree, mt.getReturnType(), pt); 592 } 593 594 public void visitNewClass(JCNewClass tree) { 595 if (tree.encl != null) 596 tree.encl = translate(tree.encl, erasure(tree.encl.type)); 597 tree.clazz = translate(tree.clazz, null); 598 if (tree.varargsElement != null) 599 tree.varargsElement = types.erasure(tree.varargsElement); 600 tree.args = translateArgs( 601 tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement); 602 tree.def = translate(tree.def, null); 603 tree.type = erasure(tree.type); 604 result = tree; 605 } 606 607 public void visitNewArray(JCNewArray tree) { 608 tree.elemtype = translate(tree.elemtype, null); 609 translate(tree.dims, syms.intType); 610 if (tree.type != null) { 611 tree.elems = translate(tree.elems, erasure(types.elemtype(tree.type))); 612 tree.type = erasure(tree.type); 613 } else { 614 tree.elems = translate(tree.elems, null); 615 } 616 617 result = tree; 618 } 619 620 public void visitParens(JCParens tree) { 621 tree.expr = translate(tree.expr, pt); 622 tree.type = erasure(tree.type); 623 result = tree; 624 } 625 626 public void visitAssign(JCAssign tree) { 627 tree.lhs = translate(tree.lhs, null); 628 tree.rhs = translate(tree.rhs, erasure(tree.lhs.type)); 629 tree.type = erasure(tree.type); 630 result = tree; 631 } 632 633 public void visitAssignop(JCAssignOp tree) { 634 tree.lhs = translate(tree.lhs, null); 635 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head); 636 tree.type = erasure(tree.type); 637 result = tree; 638 } 639 640 public void visitUnary(JCUnary tree) { 641 tree.arg = translate(tree.arg, tree.operator.type.getParameterTypes().head); 642 result = tree; 643 } 644 645 public void visitBinary(JCBinary tree) { 646 tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head); 647 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head); 648 result = tree; 649 } 650 651 public void visitTypeCast(JCTypeCast tree) { 652 tree.clazz = translate(tree.clazz, null); 653 tree.type = erasure(tree.type); 654 tree.expr = translate(tree.expr, tree.type); 655 result = tree; 656 } 657 658 public void visitTypeTest(JCInstanceOf tree) { 659 tree.expr = translate(tree.expr, null); 660 tree.clazz = translate(tree.clazz, null); 661 result = tree; 662 } 663 664 public void visitIndexed(JCArrayAccess tree) { 665 tree.indexed = translate(tree.indexed, erasure(tree.indexed.type)); 666 tree.index = translate(tree.index, syms.intType); 667 668 // Insert casts of indexed expressions as needed. 669 result = retype(tree, types.elemtype(tree.indexed.type), pt); 670 } 671 672 // There ought to be nothing to rewrite here; 673 // we don't generate code. 674 public void visitAnnotation(JCAnnotation tree) { 675 result = tree; 676 } 677 678 public void visitIdent(JCIdent tree) { 679 Type et = tree.sym.erasure(types); 680 681 // Map type variables to their bounds. 682 if (tree.sym.kind == TYP && tree.sym.type.tag == TYPEVAR) { 683 result = make.at(tree.pos).Type(et); 684 } else 685 // Map constants expressions to themselves. 686 if (tree.type.constValue() != null) { 687 result = tree; 688 } 689 // Insert casts of variable uses as needed. 690 else if (tree.sym.kind == VAR) { 691 result = retype(tree, et, pt); 692 } 693 else { 694 tree.type = erasure(tree.type); 695 result = tree; 696 } 697 } 698 699 public void visitSelect(JCFieldAccess tree) { 700 Type t = tree.selected.type; 701 while (t.tag == TYPEVAR) 702 t = t.getUpperBound(); 703 if (t.isCompound()) { 704 if ((tree.sym.flags() & IPROXY) != 0) { 705 tree.sym = ((MethodSymbol)tree.sym). 706 implemented((TypeSymbol)tree.sym.owner, types); 707 } 708 tree.selected = cast( 709 translate(tree.selected, erasure(tree.selected.type)), 710 erasure(tree.sym.owner.type)); 711 } else 712 tree.selected = translate(tree.selected, erasure(t)); 713 714 // Map constants expressions to themselves. 715 if (tree.type.constValue() != null) { 716 result = tree; 717 } 718 // Insert casts of variable uses as needed. 719 else if (tree.sym.kind == VAR) { 720 result = retype(tree, tree.sym.erasure(types), pt); 721 } 722 else { 723 tree.type = erasure(tree.type); 724 result = tree; 725 } 726 } 727 728 public void visitTypeArray(JCArrayTypeTree tree) { 729 tree.elemtype = translate(tree.elemtype, null); 730 tree.type = erasure(tree.type); 731 result = tree; 732 } 733 734 /** Visitor method for parameterized types. 735 */ 736 public void visitTypeApply(JCTypeApply tree) { 737 JCTree clazz = translate(tree.clazz, null); 738 result = clazz; 739 } 740 741 /************************************************************************** 742 * utility methods 743 *************************************************************************/ 744 745 private Type erasure(Type t) { 746 return types.erasure(t); 747 } 748 749 /************************************************************************** 750 * main method 751 *************************************************************************/ 752 753 private Env<AttrContext> env; 754 755 void translateClass(ClassSymbol c) { 756 Type st = types.supertype(c.type); 757 758 // process superclass before derived 759 if (st.tag == CLASS) 760 translateClass((ClassSymbol)st.tsym); 761 762 Env<AttrContext> myEnv = enter.typeEnvs.remove(c); 763 if (myEnv == null) 764 return; 765 Env<AttrContext> oldEnv = env; 766 try { 767 env = myEnv; 768 // class has not been translated yet 769 770 TreeMaker savedMake = make; 771 Type savedPt = pt; 772 make = make.forToplevel(env.toplevel); 773 pt = null; 774 try { 775 JCClassDecl tree = (JCClassDecl) env.tree; 776 tree.typarams = List.nil(); 777 super.visitClassDef(tree); 778 make.at(tree.pos); 779 if (addBridges) { 780 ListBuffer<JCTree> bridges = new ListBuffer<JCTree>(); 781 if ((tree.sym.flags() & INTERFACE) == 0) 782 addBridges(tree.pos(), tree.sym, bridges); 783 tree.defs = bridges.toList().prependList(tree.defs); 784 } 785 tree.type = erasure(tree.type); 786 } finally { 787 make = savedMake; 788 pt = savedPt; 789 } 790 } finally { 791 env = oldEnv; 792 } 793 } 794 795 /** Translate a toplevel class definition. 796 * @param cdef The definition to be translated. 797 */ 798 public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) { 799 // note that this method does NOT support recursion. 800 this.make = make; 801 pt = null; 802 return translate(cdef, null); 803 } 804 }