src/share/classes/com/sun/tools/javac/tree/TreeMaker.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 550
a6f2911a7c55
child 581
f2fdd52e4e87
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

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

mercurial