1 /* 2 * Copyright 1999-2008 Sun Microsystems, Inc. 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. Sun designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 22 * CA 95054 USA or visit www.sun.com if you need additional information or 23 * have any questions. 24 */ 25 26 package com.sun.tools.javac.tree; 27 28 import com.sun.tools.javac.code.*; 29 import com.sun.tools.javac.code.Symbol.*; 30 import com.sun.tools.javac.code.Type.*; 31 import com.sun.tools.javac.util.*; 32 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 33 34 import com.sun.tools.javac.tree.JCTree.*; 35 36 import static com.sun.tools.javac.code.Flags.*; 37 import static com.sun.tools.javac.code.Kinds.*; 38 import static com.sun.tools.javac.code.TypeTags.*; 39 40 /** Factory class for trees. 41 * 42 * <p><b>This is NOT part of any API supported by Sun Microsystems. If 43 * you write code that depends on this, you do so at your own risk. 44 * This code and its internal interfaces are subject to change or 45 * deletion without notice.</b> 46 */ 47 public class TreeMaker implements JCTree.Factory { 48 49 /** The context key for the tree factory. */ 50 protected static final Context.Key<TreeMaker> treeMakerKey = 51 new Context.Key<TreeMaker>(); 52 53 /** Get the TreeMaker instance. */ 54 public static TreeMaker instance(Context context) { 55 TreeMaker instance = context.get(treeMakerKey); 56 if (instance == null) 57 instance = new TreeMaker(context); 58 return instance; 59 } 60 61 /** The position at which subsequent trees will be created. 62 */ 63 public int pos = Position.NOPOS; 64 65 /** The toplevel tree to which created trees belong. 66 */ 67 public JCCompilationUnit toplevel; 68 69 /** The current name table. */ 70 Names names; 71 72 Types types; 73 74 /** The current symbol table. */ 75 Symtab syms; 76 77 /** Create a tree maker with null toplevel and NOPOS as initial position. 78 */ 79 protected TreeMaker(Context context) { 80 context.put(treeMakerKey, this); 81 this.pos = Position.NOPOS; 82 this.toplevel = null; 83 this.names = Names.instance(context); 84 this.syms = Symtab.instance(context); 85 this.types = Types.instance(context); 86 } 87 88 /** Create a tree maker with a given toplevel and FIRSTPOS as initial position. 89 */ 90 TreeMaker(JCCompilationUnit toplevel, Names names, Types types, Symtab syms) { 91 this.pos = Position.FIRSTPOS; 92 this.toplevel = toplevel; 93 this.names = names; 94 this.types = types; 95 this.syms = syms; 96 } 97 98 /** Create a new tree maker for a given toplevel. 99 */ 100 public TreeMaker forToplevel(JCCompilationUnit toplevel) { 101 return new TreeMaker(toplevel, names, types, syms); 102 } 103 104 /** Reassign current position. 105 */ 106 public TreeMaker at(int pos) { 107 this.pos = pos; 108 return this; 109 } 110 111 /** Reassign current position. 112 */ 113 public TreeMaker at(DiagnosticPosition pos) { 114 this.pos = (pos == null ? Position.NOPOS : pos.getStartPosition()); 115 return this; 116 } 117 118 /** 119 * Create given tree node at current position. 120 * @param defs a list of ClassDef, Import, and Skip 121 */ 122 public JCCompilationUnit TopLevel(List<JCAnnotation> packageAnnotations, 123 JCExpression pid, 124 List<JCTree> defs) { 125 assert packageAnnotations != null; 126 for (JCTree node : defs) 127 assert node instanceof JCClassDecl 128 || node instanceof JCImport 129 || node instanceof JCSkip 130 || node instanceof JCErroneous 131 || (node instanceof JCExpressionStatement 132 && ((JCExpressionStatement)node).expr instanceof JCErroneous) 133 : node.getClass().getSimpleName(); 134 JCCompilationUnit tree = new JCCompilationUnit(packageAnnotations, pid, defs, 135 null, null, null, null); 136 tree.pos = pos; 137 return tree; 138 } 139 140 public JCImport Import(JCTree qualid, boolean importStatic) { 141 JCImport tree = new JCImport(qualid, importStatic); 142 tree.pos = pos; 143 return tree; 144 } 145 146 public JCClassDecl ClassDef(JCModifiers mods, 147 Name name, 148 List<JCTypeParameter> typarams, 149 JCTree extending, 150 List<JCExpression> implementing, 151 List<JCTree> defs) 152 { 153 JCClassDecl tree = new JCClassDecl(mods, 154 name, 155 typarams, 156 extending, 157 implementing, 158 defs, 159 null); 160 tree.pos = pos; 161 return tree; 162 } 163 164 public JCMethodDecl MethodDef(JCModifiers mods, 165 Name name, 166 JCExpression restype, 167 List<JCTypeParameter> typarams, 168 List<JCVariableDecl> params, 169 List<JCExpression> thrown, 170 JCBlock body, 171 JCExpression defaultValue) { 172 return MethodDef( 173 mods, name, restype, typarams, params, 174 null, thrown, body, defaultValue); 175 } 176 177 public JCMethodDecl MethodDef(JCModifiers mods, 178 Name name, 179 JCExpression restype, 180 List<JCTypeParameter> typarams, 181 List<JCVariableDecl> params, 182 List<JCTypeAnnotation> receiver, 183 List<JCExpression> thrown, 184 JCBlock body, 185 JCExpression defaultValue) 186 { 187 JCMethodDecl tree = new JCMethodDecl(mods, 188 name, 189 restype, 190 typarams, 191 params, 192 receiver, 193 thrown, 194 body, 195 defaultValue, 196 null); 197 tree.pos = pos; 198 return tree; 199 } 200 201 public JCVariableDecl VarDef(JCModifiers mods, Name name, JCExpression vartype, JCExpression init) { 202 JCVariableDecl tree = new JCVariableDecl(mods, name, vartype, init, null); 203 tree.pos = pos; 204 return tree; 205 } 206 207 public JCSkip Skip() { 208 JCSkip tree = new JCSkip(); 209 tree.pos = pos; 210 return tree; 211 } 212 213 public JCBlock Block(long flags, List<JCStatement> stats) { 214 JCBlock tree = new JCBlock(flags, stats); 215 tree.pos = pos; 216 return tree; 217 } 218 219 public JCDoWhileLoop DoLoop(JCStatement body, JCExpression cond) { 220 JCDoWhileLoop tree = new JCDoWhileLoop(body, cond); 221 tree.pos = pos; 222 return tree; 223 } 224 225 public JCWhileLoop WhileLoop(JCExpression cond, JCStatement body) { 226 JCWhileLoop tree = new JCWhileLoop(cond, body); 227 tree.pos = pos; 228 return tree; 229 } 230 231 public JCForLoop ForLoop(List<JCStatement> init, 232 JCExpression cond, 233 List<JCExpressionStatement> step, 234 JCStatement body) 235 { 236 JCForLoop tree = new JCForLoop(init, cond, step, body); 237 tree.pos = pos; 238 return tree; 239 } 240 241 public JCEnhancedForLoop ForeachLoop(JCVariableDecl var, JCExpression expr, JCStatement body) { 242 JCEnhancedForLoop tree = new JCEnhancedForLoop(var, expr, body); 243 tree.pos = pos; 244 return tree; 245 } 246 247 public JCLabeledStatement Labelled(Name label, JCStatement body) { 248 JCLabeledStatement tree = new JCLabeledStatement(label, body); 249 tree.pos = pos; 250 return tree; 251 } 252 253 public JCSwitch Switch(JCExpression selector, List<JCCase> cases) { 254 JCSwitch tree = new JCSwitch(selector, cases); 255 tree.pos = pos; 256 return tree; 257 } 258 259 public JCCase Case(JCExpression pat, List<JCStatement> stats) { 260 JCCase tree = new JCCase(pat, stats); 261 tree.pos = pos; 262 return tree; 263 } 264 265 public JCSynchronized Synchronized(JCExpression lock, JCBlock body) { 266 JCSynchronized tree = new JCSynchronized(lock, body); 267 tree.pos = pos; 268 return tree; 269 } 270 271 public JCTry Try(JCBlock body, List<JCCatch> catchers, JCBlock finalizer) { 272 JCTry tree = new JCTry(body, catchers, finalizer); 273 tree.pos = pos; 274 return tree; 275 } 276 277 public JCCatch Catch(JCVariableDecl param, JCBlock body) { 278 JCCatch tree = new JCCatch(param, body); 279 tree.pos = pos; 280 return tree; 281 } 282 283 public JCConditional Conditional(JCExpression cond, 284 JCExpression thenpart, 285 JCExpression elsepart) 286 { 287 JCConditional tree = new JCConditional(cond, thenpart, elsepart); 288 tree.pos = pos; 289 return tree; 290 } 291 292 public JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart) { 293 JCIf tree = new JCIf(cond, thenpart, elsepart); 294 tree.pos = pos; 295 return tree; 296 } 297 298 public JCExpressionStatement Exec(JCExpression expr) { 299 JCExpressionStatement tree = new JCExpressionStatement(expr); 300 tree.pos = pos; 301 return tree; 302 } 303 304 public JCBreak Break(Name label) { 305 JCBreak tree = new JCBreak(label, null); 306 tree.pos = pos; 307 return tree; 308 } 309 310 public JCContinue Continue(Name label) { 311 JCContinue tree = new JCContinue(label, null); 312 tree.pos = pos; 313 return tree; 314 } 315 316 public JCReturn Return(JCExpression expr) { 317 JCReturn tree = new JCReturn(expr); 318 tree.pos = pos; 319 return tree; 320 } 321 322 public JCThrow Throw(JCTree expr) { 323 JCThrow tree = new JCThrow(expr); 324 tree.pos = pos; 325 return tree; 326 } 327 328 public JCAssert Assert(JCExpression cond, JCExpression detail) { 329 JCAssert tree = new JCAssert(cond, detail); 330 tree.pos = pos; 331 return tree; 332 } 333 334 public JCMethodInvocation Apply(List<JCExpression> typeargs, 335 JCExpression fn, 336 List<JCExpression> args) 337 { 338 JCMethodInvocation tree = new JCMethodInvocation(typeargs, fn, args); 339 tree.pos = pos; 340 return tree; 341 } 342 343 public JCNewClass NewClass(JCExpression encl, 344 List<JCExpression> typeargs, 345 JCExpression clazz, 346 List<JCExpression> args, 347 JCClassDecl def) 348 { 349 JCNewClass tree = new JCNewClass(encl, typeargs, clazz, args, def); 350 tree.pos = pos; 351 return tree; 352 } 353 354 public JCNewArray NewArray(JCExpression elemtype, 355 List<JCExpression> dims, 356 List<JCExpression> elems) 357 { 358 JCNewArray tree = new JCNewArray(elemtype, dims, elems); 359 tree.pos = pos; 360 return tree; 361 } 362 363 public JCParens Parens(JCExpression expr) { 364 JCParens tree = new JCParens(expr); 365 tree.pos = pos; 366 return tree; 367 } 368 369 public JCAssign Assign(JCExpression lhs, JCExpression rhs) { 370 JCAssign tree = new JCAssign(lhs, rhs); 371 tree.pos = pos; 372 return tree; 373 } 374 375 public JCAssignOp Assignop(int opcode, JCTree lhs, JCTree rhs) { 376 JCAssignOp tree = new JCAssignOp(opcode, lhs, rhs, null); 377 tree.pos = pos; 378 return tree; 379 } 380 381 public JCUnary Unary(int opcode, JCExpression arg) { 382 JCUnary tree = new JCUnary(opcode, arg); 383 tree.pos = pos; 384 return tree; 385 } 386 387 public JCBinary Binary(int opcode, JCExpression lhs, JCExpression rhs) { 388 JCBinary tree = new JCBinary(opcode, lhs, rhs, null); 389 tree.pos = pos; 390 return tree; 391 } 392 393 public JCTypeCast TypeCast(JCTree clazz, JCExpression expr) { 394 JCTypeCast tree = new JCTypeCast(clazz, expr); 395 tree.pos = pos; 396 return tree; 397 } 398 399 public JCInstanceOf TypeTest(JCExpression expr, JCTree clazz) { 400 JCInstanceOf tree = new JCInstanceOf(expr, clazz); 401 tree.pos = pos; 402 return tree; 403 } 404 405 public JCArrayAccess Indexed(JCExpression indexed, JCExpression index) { 406 JCArrayAccess tree = new JCArrayAccess(indexed, index); 407 tree.pos = pos; 408 return tree; 409 } 410 411 public JCFieldAccess Select(JCExpression selected, Name selector) { 412 JCFieldAccess tree = new JCFieldAccess(selected, selector, null); 413 tree.pos = pos; 414 return tree; 415 } 416 417 public JCIdent Ident(Name name) { 418 JCIdent tree = new JCIdent(name, null); 419 tree.pos = pos; 420 return tree; 421 } 422 423 public JCLiteral Literal(int tag, Object value) { 424 JCLiteral tree = new JCLiteral(tag, value); 425 tree.pos = pos; 426 return tree; 427 } 428 429 public JCPrimitiveTypeTree TypeIdent(int typetag) { 430 JCPrimitiveTypeTree tree = new JCPrimitiveTypeTree(typetag); 431 tree.pos = pos; 432 return tree; 433 } 434 435 public JCArrayTypeTree TypeArray(JCExpression elemtype) { 436 JCArrayTypeTree tree = new JCArrayTypeTree(elemtype); 437 tree.pos = pos; 438 return tree; 439 } 440 441 public JCTypeApply TypeApply(JCExpression clazz, List<JCExpression> arguments) { 442 JCTypeApply tree = new JCTypeApply(clazz, arguments); 443 tree.pos = pos; 444 return tree; 445 } 446 447 public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds) { 448 return TypeParameter(name, bounds, List.<JCTypeAnnotation>nil()); 449 } 450 451 public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds, List<JCTypeAnnotation> annos) { 452 JCTypeParameter tree = new JCTypeParameter(name, bounds, annos); 453 tree.pos = pos; 454 return tree; 455 } 456 457 public JCWildcard Wildcard(TypeBoundKind kind, JCTree type) { 458 JCWildcard tree = new JCWildcard(kind, type); 459 tree.pos = pos; 460 return tree; 461 } 462 463 public TypeBoundKind TypeBoundKind(BoundKind kind) { 464 TypeBoundKind tree = new TypeBoundKind(kind); 465 tree.pos = pos; 466 return tree; 467 } 468 469 public JCAnnotation Annotation(JCTree annotationType, List<JCExpression> args) { 470 JCAnnotation tree = new JCAnnotation(annotationType, args); 471 tree.pos = pos; 472 return tree; 473 } 474 475 public JCTypeAnnotation TypeAnnotation(JCTree annotationType, List<JCExpression> args) { 476 JCTypeAnnotation tree = new JCTypeAnnotation(annotationType, args); 477 tree.pos = pos; 478 return tree; 479 } 480 481 public JCModifiers Modifiers(long flags, List<JCAnnotation> annotations) { 482 JCModifiers tree = new JCModifiers(flags, annotations); 483 boolean noFlags = (flags & Flags.StandardFlags) == 0; 484 tree.pos = (noFlags && annotations.isEmpty()) ? Position.NOPOS : pos; 485 return tree; 486 } 487 488 public JCModifiers Modifiers(long flags) { 489 return Modifiers(flags, List.<JCAnnotation>nil()); 490 } 491 492 public JCAnnotatedType AnnotatedType(List<JCTypeAnnotation> annotations, JCExpression underlyingType) { 493 JCAnnotatedType tree = new JCAnnotatedType(annotations, underlyingType); 494 tree.pos = pos; 495 return tree; 496 } 497 498 public JCErroneous Erroneous() { 499 return Erroneous(List.<JCTree>nil()); 500 } 501 502 public JCErroneous Erroneous(List<? extends JCTree> errs) { 503 JCErroneous tree = new JCErroneous(errs); 504 tree.pos = pos; 505 return tree; 506 } 507 508 public LetExpr LetExpr(List<JCVariableDecl> defs, JCTree expr) { 509 LetExpr tree = new LetExpr(defs, expr); 510 tree.pos = pos; 511 return tree; 512 } 513 514 /* *************************************************************************** 515 * Derived building blocks. 516 ****************************************************************************/ 517 518 public JCClassDecl AnonymousClassDef(JCModifiers mods, 519 List<JCTree> defs) 520 { 521 return ClassDef(mods, 522 names.empty, 523 List.<JCTypeParameter>nil(), 524 null, 525 List.<JCExpression>nil(), 526 defs); 527 } 528 529 public LetExpr LetExpr(JCVariableDecl def, JCTree expr) { 530 LetExpr tree = new LetExpr(List.of(def), expr); 531 tree.pos = pos; 532 return tree; 533 } 534 535 /** Create an identifier from a symbol. 536 */ 537 public JCIdent Ident(Symbol sym) { 538 return (JCIdent)new JCIdent((sym.name != names.empty) 539 ? sym.name 540 : sym.flatName(), sym) 541 .setPos(pos) 542 .setType(sym.type); 543 } 544 545 /** Create a selection node from a qualifier tree and a symbol. 546 * @param base The qualifier tree. 547 */ 548 public JCExpression Select(JCExpression base, Symbol sym) { 549 return new JCFieldAccess(base, sym.name, sym).setPos(pos).setType(sym.type); 550 } 551 552 /** Create a qualified identifier from a symbol, adding enough qualifications 553 * to make the reference unique. 554 */ 555 public JCExpression QualIdent(Symbol sym) { 556 return isUnqualifiable(sym) 557 ? Ident(sym) 558 : Select(QualIdent(sym.owner), sym); 559 } 560 561 /** Create an identifier that refers to the variable declared in given variable 562 * declaration. 563 */ 564 public JCExpression Ident(JCVariableDecl param) { 565 return Ident(param.sym); 566 } 567 568 /** Create a list of identifiers referring to the variables declared 569 * in given list of variable declarations. 570 */ 571 public List<JCExpression> Idents(List<JCVariableDecl> params) { 572 ListBuffer<JCExpression> ids = new ListBuffer<JCExpression>(); 573 for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) 574 ids.append(Ident(l.head)); 575 return ids.toList(); 576 } 577 578 /** Create a tree representing `this', given its type. 579 */ 580 public JCExpression This(Type t) { 581 return Ident(new VarSymbol(FINAL, names._this, t, t.tsym)); 582 } 583 584 /** Create a tree representing a class literal. 585 */ 586 public JCExpression ClassLiteral(ClassSymbol clazz) { 587 return ClassLiteral(clazz.type); 588 } 589 590 /** Create a tree representing a class literal. 591 */ 592 public JCExpression ClassLiteral(Type t) { 593 VarSymbol lit = new VarSymbol(STATIC | PUBLIC | FINAL, 594 names._class, 595 t, 596 t.tsym); 597 return Select(Type(t), lit); 598 } 599 600 /** Create a tree representing `super', given its type and owner. 601 */ 602 public JCIdent Super(Type t, TypeSymbol owner) { 603 return Ident(new VarSymbol(FINAL, names._super, t, owner)); 604 } 605 606 /** 607 * Create a method invocation from a method tree and a list of 608 * argument trees. 609 */ 610 public JCMethodInvocation App(JCExpression meth, List<JCExpression> args) { 611 return Apply(null, meth, args).setType(meth.type.getReturnType()); 612 } 613 614 /** 615 * Create a no-arg method invocation from a method tree 616 */ 617 public JCMethodInvocation App(JCExpression meth) { 618 return Apply(null, meth, List.<JCExpression>nil()).setType(meth.type.getReturnType()); 619 } 620 621 /** Create a method invocation from a method tree and a list of argument trees. 622 */ 623 public JCExpression Create(Symbol ctor, List<JCExpression> args) { 624 Type t = ctor.owner.erasure(types); 625 JCNewClass newclass = NewClass(null, null, Type(t), args, null); 626 newclass.constructor = ctor; 627 newclass.setType(t); 628 return newclass; 629 } 630 631 /** Create a tree representing given type. 632 */ 633 public JCExpression Type(Type t) { 634 if (t == null) return null; 635 JCExpression tp; 636 switch (t.tag) { 637 case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT: 638 case DOUBLE: case BOOLEAN: case VOID: 639 tp = TypeIdent(t.tag); 640 break; 641 case TYPEVAR: 642 tp = Ident(t.tsym); 643 break; 644 case WILDCARD: { 645 WildcardType a = ((WildcardType) t); 646 tp = Wildcard(TypeBoundKind(a.kind), Type(a.type)); 647 break; 648 } 649 case CLASS: 650 Type outer = t.getEnclosingType(); 651 JCExpression clazz = outer.tag == CLASS && t.tsym.owner.kind == TYP 652 ? Select(Type(outer), t.tsym) 653 : QualIdent(t.tsym); 654 tp = t.getTypeArguments().isEmpty() 655 ? clazz 656 : TypeApply(clazz, Types(t.getTypeArguments())); 657 break; 658 case ARRAY: 659 tp = TypeArray(Type(types.elemtype(t))); 660 break; 661 case ERROR: 662 tp = TypeIdent(ERROR); 663 break; 664 default: 665 throw new AssertionError("unexpected type: " + t); 666 } 667 return tp.setType(t); 668 } 669 //where 670 private JCExpression Selectors(JCExpression base, Symbol sym, Symbol limit) { 671 if (sym == limit) return base; 672 else return Select(Selectors(base, sym.owner, limit), sym); 673 } 674 675 /** Create a list of trees representing given list of types. 676 */ 677 public List<JCExpression> Types(List<Type> ts) { 678 ListBuffer<JCExpression> types = new ListBuffer<JCExpression>(); 679 for (List<Type> l = ts; l.nonEmpty(); l = l.tail) 680 types.append(Type(l.head)); 681 return types.toList(); 682 } 683 684 /** Create a variable definition from a variable symbol and an initializer 685 * expression. 686 */ 687 public JCVariableDecl VarDef(VarSymbol v, JCExpression init) { 688 return (JCVariableDecl) 689 new JCVariableDecl( 690 Modifiers(v.flags(), Annotations(v.getAnnotationMirrors())), 691 v.name, 692 Type(v.type), 693 init, 694 v).setPos(pos).setType(v.type); 695 } 696 697 /** Create annotation trees from annotations. 698 */ 699 public List<JCAnnotation> Annotations(List<Attribute.Compound> attributes) { 700 if (attributes == null) return List.nil(); 701 ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>(); 702 for (List<Attribute.Compound> i = attributes; i.nonEmpty(); i=i.tail) { 703 Attribute a = i.head; 704 result.append(Annotation(a)); 705 } 706 return result.toList(); 707 } 708 709 public JCLiteral Literal(Object value) { 710 JCLiteral result = null; 711 if (value instanceof String) { 712 result = Literal(CLASS, value). 713 setType(syms.stringType.constType(value)); 714 } else if (value instanceof Integer) { 715 result = Literal(INT, value). 716 setType(syms.intType.constType(value)); 717 } else if (value instanceof Long) { 718 result = Literal(LONG, value). 719 setType(syms.longType.constType(value)); 720 } else if (value instanceof Byte) { 721 result = Literal(BYTE, value). 722 setType(syms.byteType.constType(value)); 723 } else if (value instanceof Character) { 724 result = Literal(CHAR, value). 725 setType(syms.charType.constType(value)); 726 } else if (value instanceof Double) { 727 result = Literal(DOUBLE, value). 728 setType(syms.doubleType.constType(value)); 729 } else if (value instanceof Float) { 730 result = Literal(FLOAT, value). 731 setType(syms.floatType.constType(value)); 732 } else if (value instanceof Short) { 733 result = Literal(SHORT, value). 734 setType(syms.shortType.constType(value)); 735 } else { 736 throw new AssertionError(value); 737 } 738 return result; 739 } 740 741 class AnnotationBuilder implements Attribute.Visitor { 742 JCExpression result = null; 743 public void visitConstant(Attribute.Constant v) { 744 result = Literal(v.value); 745 } 746 public void visitClass(Attribute.Class clazz) { 747 result = ClassLiteral(clazz.type).setType(syms.classType); 748 } 749 public void visitEnum(Attribute.Enum e) { 750 result = QualIdent(e.value); 751 } 752 public void visitError(Attribute.Error e) { 753 result = Erroneous(); 754 } 755 public void visitCompound(Attribute.Compound compound) { 756 result = visitCompoundInternal(compound); 757 } 758 public JCAnnotation visitCompoundInternal(Attribute.Compound compound) { 759 ListBuffer<JCExpression> args = new ListBuffer<JCExpression>(); 760 for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) { 761 Pair<MethodSymbol,Attribute> pair = values.head; 762 JCExpression valueTree = translate(pair.snd); 763 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type)); 764 } 765 return Annotation(Type(compound.type), args.toList()); 766 } 767 public void visitArray(Attribute.Array array) { 768 ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>(); 769 for (int i = 0; i < array.values.length; i++) 770 elems.append(translate(array.values[i])); 771 result = NewArray(null, List.<JCExpression>nil(), elems.toList()).setType(array.type); 772 } 773 JCExpression translate(Attribute a) { 774 a.accept(this); 775 return result; 776 } 777 JCAnnotation translate(Attribute.Compound a) { 778 return visitCompoundInternal(a); 779 } 780 } 781 AnnotationBuilder annotationBuilder = new AnnotationBuilder(); 782 783 /** Create an annotation tree from an attribute. 784 */ 785 public JCAnnotation Annotation(Attribute a) { 786 return annotationBuilder.translate((Attribute.Compound)a); 787 } 788 789 /** Create a method definition from a method symbol and a method body. 790 */ 791 public JCMethodDecl MethodDef(MethodSymbol m, JCBlock body) { 792 return MethodDef(m, m.type, body); 793 } 794 795 /** Create a method definition from a method symbol, method type 796 * and a method body. 797 */ 798 public JCMethodDecl MethodDef(MethodSymbol m, Type mtype, JCBlock body) { 799 return (JCMethodDecl) 800 new JCMethodDecl( 801 Modifiers(m.flags(), Annotations(m.getAnnotationMirrors())), 802 m.name, 803 Type(mtype.getReturnType()), 804 TypeParams(mtype.getTypeArguments()), 805 Params(mtype.getParameterTypes(), m), 806 null, 807 Types(mtype.getThrownTypes()), 808 body, 809 null, 810 m).setPos(pos).setType(mtype); 811 } 812 813 /** Create a type parameter tree from its name and type. 814 */ 815 public JCTypeParameter TypeParam(Name name, TypeVar tvar) { 816 return (JCTypeParameter) 817 TypeParameter(name, Types(types.getBounds(tvar))).setPos(pos).setType(tvar); 818 } 819 820 /** Create a list of type parameter trees from a list of type variables. 821 */ 822 public List<JCTypeParameter> TypeParams(List<Type> typarams) { 823 ListBuffer<JCTypeParameter> tparams = new ListBuffer<JCTypeParameter>(); 824 int i = 0; 825 for (List<Type> l = typarams; l.nonEmpty(); l = l.tail) 826 tparams.append(TypeParam(l.head.tsym.name, (TypeVar)l.head)); 827 return tparams.toList(); 828 } 829 830 /** Create a value parameter tree from its name, type, and owner. 831 */ 832 public JCVariableDecl Param(Name name, Type argtype, Symbol owner) { 833 return VarDef(new VarSymbol(0, name, argtype, owner), null); 834 } 835 836 /** Create a a list of value parameter trees x0, ..., xn from a list of 837 * their types and an their owner. 838 */ 839 public List<JCVariableDecl> Params(List<Type> argtypes, Symbol owner) { 840 ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>(); 841 MethodSymbol mth = (owner.kind == MTH) ? ((MethodSymbol)owner) : null; 842 if (mth != null && mth.params != null && argtypes.length() == mth.params.length()) { 843 for (VarSymbol param : ((MethodSymbol)owner).params) 844 params.append(VarDef(param, null)); 845 } else { 846 int i = 0; 847 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail) 848 params.append(Param(paramName(i++), l.head, owner)); 849 } 850 return params.toList(); 851 } 852 853 /** Wrap a method invocation in an expression statement or return statement, 854 * depending on whether the method invocation expression's type is void. 855 */ 856 public JCStatement Call(JCExpression apply) { 857 return apply.type.tag == VOID ? Exec(apply) : Return(apply); 858 } 859 860 /** Construct an assignment from a variable symbol and a right hand side. 861 */ 862 public JCStatement Assignment(Symbol v, JCExpression rhs) { 863 return Exec(Assign(Ident(v), rhs).setType(v.type)); 864 } 865 866 /** Construct an index expression from a variable and an expression. 867 */ 868 public JCArrayAccess Indexed(Symbol v, JCExpression index) { 869 JCArrayAccess tree = new JCArrayAccess(QualIdent(v), index); 870 tree.type = ((ArrayType)v.type).elemtype; 871 return tree; 872 } 873 874 /** Make an attributed type cast expression. 875 */ 876 public JCTypeCast TypeCast(Type type, JCExpression expr) { 877 return (JCTypeCast)TypeCast(Type(type), expr).setType(type); 878 } 879 880 /* *************************************************************************** 881 * Helper methods. 882 ****************************************************************************/ 883 884 /** Can given symbol be referred to in unqualified form? 885 */ 886 boolean isUnqualifiable(Symbol sym) { 887 if (sym.name == names.empty || 888 sym.owner == null || 889 sym.owner.kind == MTH || sym.owner.kind == VAR) { 890 return true; 891 } else if (sym.kind == TYP && toplevel != null) { 892 Scope.Entry e; 893 e = toplevel.namedImportScope.lookup(sym.name); 894 if (e.scope != null) { 895 return 896 e.sym == sym && 897 e.next().scope == null; 898 } 899 e = toplevel.packge.members().lookup(sym.name); 900 if (e.scope != null) { 901 return 902 e.sym == sym && 903 e.next().scope == null; 904 } 905 e = toplevel.starImportScope.lookup(sym.name); 906 if (e.scope != null) { 907 return 908 e.sym == sym && 909 e.next().scope == null; 910 } 911 } 912 return false; 913 } 914 915 /** The name of synthetic parameter number `i'. 916 */ 917 public Name paramName(int i) { return names.fromString("x" + i); } 918 919 /** The name of synthetic type parameter number `i'. 920 */ 921 public Name typaramName(int i) { return names.fromString("A" + i); } 922 }