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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1521
71f35e4b93a5
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

Merge

duke@1 1 /*
jjg@1521 2 * Copyright (c) 1999, 2013, 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.*;
jjg@1374 38 import static com.sun.tools.javac.code.TypeTag.*;
duke@1 39
duke@1 40 /** Factory class for trees.
duke@1 41 *
jjg@581 42 * <p><b>This is NOT part of any supported API.
jjg@581 43 * If 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 */
ksrini@1327 90 protected 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) {
jjg@816 125 Assert.checkNonNull(packageAnnotations);
duke@1 126 for (JCTree node : defs)
jjg@816 127 Assert.check(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
jjg@816 132 && ((JCExpressionStatement)node).expr instanceof JCErroneous),
jjg@816 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,
jjg@904 149 JCExpression 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@1521 172 return MethodDef(
jjg@1521 173 mods, name, restype, typarams, null, params,
jjg@1521 174 thrown, body, defaultValue);
jjg@1521 175 }
jjg@1521 176
jjg@1521 177 public JCMethodDecl MethodDef(JCModifiers mods,
jjg@1521 178 Name name,
jjg@1521 179 JCExpression restype,
jjg@1521 180 List<JCTypeParameter> typarams,
jjg@1521 181 JCVariableDecl recvparam,
jjg@1521 182 List<JCVariableDecl> params,
jjg@1521 183 List<JCExpression> thrown,
jjg@1521 184 JCBlock body,
jjg@1521 185 JCExpression defaultValue)
jjg@1521 186 {
duke@1 187 JCMethodDecl tree = new JCMethodDecl(mods,
duke@1 188 name,
duke@1 189 restype,
duke@1 190 typarams,
jjg@1521 191 recvparam,
duke@1 192 params,
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) {
darcy@609 272 return Try(List.<JCTree>nil(), body, catchers, finalizer);
darcy@609 273 }
darcy@609 274
darcy@609 275 public JCTry Try(List<JCTree> resources,
darcy@609 276 JCBlock body,
darcy@609 277 List<JCCatch> catchers,
darcy@609 278 JCBlock finalizer) {
darcy@609 279 JCTry tree = new JCTry(resources, body, catchers, finalizer);
duke@1 280 tree.pos = pos;
duke@1 281 return tree;
duke@1 282 }
duke@1 283
duke@1 284 public JCCatch Catch(JCVariableDecl param, JCBlock body) {
duke@1 285 JCCatch tree = new JCCatch(param, body);
duke@1 286 tree.pos = pos;
duke@1 287 return tree;
duke@1 288 }
duke@1 289
duke@1 290 public JCConditional Conditional(JCExpression cond,
duke@1 291 JCExpression thenpart,
duke@1 292 JCExpression elsepart)
duke@1 293 {
duke@1 294 JCConditional tree = new JCConditional(cond, thenpart, elsepart);
duke@1 295 tree.pos = pos;
duke@1 296 return tree;
duke@1 297 }
duke@1 298
duke@1 299 public JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart) {
duke@1 300 JCIf tree = new JCIf(cond, thenpart, elsepart);
duke@1 301 tree.pos = pos;
duke@1 302 return tree;
duke@1 303 }
duke@1 304
duke@1 305 public JCExpressionStatement Exec(JCExpression expr) {
duke@1 306 JCExpressionStatement tree = new JCExpressionStatement(expr);
duke@1 307 tree.pos = pos;
duke@1 308 return tree;
duke@1 309 }
duke@1 310
duke@1 311 public JCBreak Break(Name label) {
duke@1 312 JCBreak tree = new JCBreak(label, null);
duke@1 313 tree.pos = pos;
duke@1 314 return tree;
duke@1 315 }
duke@1 316
duke@1 317 public JCContinue Continue(Name label) {
duke@1 318 JCContinue tree = new JCContinue(label, null);
duke@1 319 tree.pos = pos;
duke@1 320 return tree;
duke@1 321 }
duke@1 322
duke@1 323 public JCReturn Return(JCExpression expr) {
duke@1 324 JCReturn tree = new JCReturn(expr);
duke@1 325 tree.pos = pos;
duke@1 326 return tree;
duke@1 327 }
duke@1 328
duke@1 329 public JCThrow Throw(JCTree expr) {
duke@1 330 JCThrow tree = new JCThrow(expr);
duke@1 331 tree.pos = pos;
duke@1 332 return tree;
duke@1 333 }
duke@1 334
duke@1 335 public JCAssert Assert(JCExpression cond, JCExpression detail) {
duke@1 336 JCAssert tree = new JCAssert(cond, detail);
duke@1 337 tree.pos = pos;
duke@1 338 return tree;
duke@1 339 }
duke@1 340
duke@1 341 public JCMethodInvocation Apply(List<JCExpression> typeargs,
duke@1 342 JCExpression fn,
duke@1 343 List<JCExpression> args)
duke@1 344 {
duke@1 345 JCMethodInvocation tree = new JCMethodInvocation(typeargs, fn, args);
duke@1 346 tree.pos = pos;
duke@1 347 return tree;
duke@1 348 }
duke@1 349
duke@1 350 public JCNewClass NewClass(JCExpression encl,
duke@1 351 List<JCExpression> typeargs,
duke@1 352 JCExpression clazz,
duke@1 353 List<JCExpression> args,
duke@1 354 JCClassDecl def)
duke@1 355 {
duke@1 356 JCNewClass tree = new JCNewClass(encl, typeargs, clazz, args, def);
duke@1 357 tree.pos = pos;
duke@1 358 return tree;
duke@1 359 }
duke@1 360
duke@1 361 public JCNewArray NewArray(JCExpression elemtype,
duke@1 362 List<JCExpression> dims,
duke@1 363 List<JCExpression> elems)
duke@1 364 {
duke@1 365 JCNewArray tree = new JCNewArray(elemtype, dims, elems);
duke@1 366 tree.pos = pos;
duke@1 367 return tree;
duke@1 368 }
duke@1 369
mcimadamore@1142 370 public JCLambda Lambda(List<JCVariableDecl> params,
mcimadamore@1142 371 JCTree body)
mcimadamore@1142 372 {
mcimadamore@1142 373 JCLambda tree = new JCLambda(params, body);
mcimadamore@1142 374 tree.pos = pos;
mcimadamore@1142 375 return tree;
mcimadamore@1142 376 }
mcimadamore@1142 377
duke@1 378 public JCParens Parens(JCExpression expr) {
duke@1 379 JCParens tree = new JCParens(expr);
duke@1 380 tree.pos = pos;
duke@1 381 return tree;
duke@1 382 }
duke@1 383
duke@1 384 public JCAssign Assign(JCExpression lhs, JCExpression rhs) {
duke@1 385 JCAssign tree = new JCAssign(lhs, rhs);
duke@1 386 tree.pos = pos;
duke@1 387 return tree;
duke@1 388 }
duke@1 389
jjg@1127 390 public JCAssignOp Assignop(JCTree.Tag opcode, JCTree lhs, JCTree rhs) {
duke@1 391 JCAssignOp tree = new JCAssignOp(opcode, lhs, rhs, null);
duke@1 392 tree.pos = pos;
duke@1 393 return tree;
duke@1 394 }
duke@1 395
jjg@1127 396 public JCUnary Unary(JCTree.Tag opcode, JCExpression arg) {
duke@1 397 JCUnary tree = new JCUnary(opcode, arg);
duke@1 398 tree.pos = pos;
duke@1 399 return tree;
duke@1 400 }
duke@1 401
jjg@1127 402 public JCBinary Binary(JCTree.Tag opcode, JCExpression lhs, JCExpression rhs) {
duke@1 403 JCBinary tree = new JCBinary(opcode, lhs, rhs, null);
duke@1 404 tree.pos = pos;
duke@1 405 return tree;
duke@1 406 }
duke@1 407
duke@1 408 public JCTypeCast TypeCast(JCTree clazz, JCExpression expr) {
duke@1 409 JCTypeCast tree = new JCTypeCast(clazz, expr);
duke@1 410 tree.pos = pos;
duke@1 411 return tree;
duke@1 412 }
duke@1 413
duke@1 414 public JCInstanceOf TypeTest(JCExpression expr, JCTree clazz) {
duke@1 415 JCInstanceOf tree = new JCInstanceOf(expr, clazz);
duke@1 416 tree.pos = pos;
duke@1 417 return tree;
duke@1 418 }
duke@1 419
duke@1 420 public JCArrayAccess Indexed(JCExpression indexed, JCExpression index) {
duke@1 421 JCArrayAccess tree = new JCArrayAccess(indexed, index);
duke@1 422 tree.pos = pos;
duke@1 423 return tree;
duke@1 424 }
duke@1 425
duke@1 426 public JCFieldAccess Select(JCExpression selected, Name selector) {
duke@1 427 JCFieldAccess tree = new JCFieldAccess(selected, selector, null);
duke@1 428 tree.pos = pos;
duke@1 429 return tree;
duke@1 430 }
duke@1 431
mcimadamore@1143 432 public JCMemberReference Reference(JCMemberReference.ReferenceMode mode, Name name,
mcimadamore@1143 433 JCExpression expr, List<JCExpression> typeargs) {
mcimadamore@1143 434 JCMemberReference tree = new JCMemberReference(mode, name, expr, typeargs);
mcimadamore@1143 435 tree.pos = pos;
mcimadamore@1143 436 return tree;
mcimadamore@1143 437 }
mcimadamore@1143 438
duke@1 439 public JCIdent Ident(Name name) {
duke@1 440 JCIdent tree = new JCIdent(name, null);
duke@1 441 tree.pos = pos;
duke@1 442 return tree;
duke@1 443 }
duke@1 444
jjg@1374 445 public JCLiteral Literal(TypeTag tag, Object value) {
duke@1 446 JCLiteral tree = new JCLiteral(tag, value);
duke@1 447 tree.pos = pos;
duke@1 448 return tree;
duke@1 449 }
duke@1 450
jjg@1374 451 public JCPrimitiveTypeTree TypeIdent(TypeTag typetag) {
duke@1 452 JCPrimitiveTypeTree tree = new JCPrimitiveTypeTree(typetag);
duke@1 453 tree.pos = pos;
duke@1 454 return tree;
duke@1 455 }
duke@1 456
duke@1 457 public JCArrayTypeTree TypeArray(JCExpression elemtype) {
duke@1 458 JCArrayTypeTree tree = new JCArrayTypeTree(elemtype);
duke@1 459 tree.pos = pos;
duke@1 460 return tree;
duke@1 461 }
duke@1 462
duke@1 463 public JCTypeApply TypeApply(JCExpression clazz, List<JCExpression> arguments) {
duke@1 464 JCTypeApply tree = new JCTypeApply(clazz, arguments);
duke@1 465 tree.pos = pos;
duke@1 466 return tree;
duke@1 467 }
duke@1 468
darcy@969 469 public JCTypeUnion TypeUnion(List<JCExpression> components) {
darcy@969 470 JCTypeUnion tree = new JCTypeUnion(components);
mcimadamore@550 471 tree.pos = pos;
mcimadamore@550 472 return tree;
mcimadamore@550 473 }
mcimadamore@550 474
mcimadamore@1436 475 public JCTypeIntersection TypeIntersection(List<JCExpression> components) {
mcimadamore@1436 476 JCTypeIntersection tree = new JCTypeIntersection(components);
mcimadamore@1436 477 tree.pos = pos;
mcimadamore@1436 478 return tree;
mcimadamore@1436 479 }
mcimadamore@1436 480
duke@1 481 public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds) {
jjg@1521 482 return TypeParameter(name, bounds, List.<JCAnnotation>nil());
jjg@1521 483 }
jjg@1521 484
jjg@1521 485 public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds, List<JCAnnotation> annos) {
jjg@1521 486 JCTypeParameter tree = new JCTypeParameter(name, bounds, annos);
duke@1 487 tree.pos = pos;
duke@1 488 return tree;
duke@1 489 }
duke@1 490
duke@1 491 public JCWildcard Wildcard(TypeBoundKind kind, JCTree type) {
duke@1 492 JCWildcard tree = new JCWildcard(kind, type);
duke@1 493 tree.pos = pos;
duke@1 494 return tree;
duke@1 495 }
duke@1 496
duke@1 497 public TypeBoundKind TypeBoundKind(BoundKind kind) {
duke@1 498 TypeBoundKind tree = new TypeBoundKind(kind);
duke@1 499 tree.pos = pos;
duke@1 500 return tree;
duke@1 501 }
duke@1 502
duke@1 503 public JCAnnotation Annotation(JCTree annotationType, List<JCExpression> args) {
jjg@1521 504 JCAnnotation tree = new JCAnnotation(Tag.ANNOTATION, annotationType, args);
jjg@1521 505 tree.pos = pos;
jjg@1521 506 return tree;
jjg@1521 507 }
jjg@1521 508
jjg@1521 509 public JCAnnotation TypeAnnotation(JCTree annotationType, List<JCExpression> args) {
jjg@1521 510 JCAnnotation tree = new JCAnnotation(Tag.TYPE_ANNOTATION, annotationType, args);
duke@1 511 tree.pos = pos;
duke@1 512 return tree;
duke@1 513 }
duke@1 514
duke@1 515 public JCModifiers Modifiers(long flags, List<JCAnnotation> annotations) {
duke@1 516 JCModifiers tree = new JCModifiers(flags, annotations);
jjg@613 517 boolean noFlags = (flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0;
duke@1 518 tree.pos = (noFlags && annotations.isEmpty()) ? Position.NOPOS : pos;
duke@1 519 return tree;
duke@1 520 }
duke@1 521
duke@1 522 public JCModifiers Modifiers(long flags) {
duke@1 523 return Modifiers(flags, List.<JCAnnotation>nil());
duke@1 524 }
duke@1 525
jjg@1521 526 public JCAnnotatedType AnnotatedType(List<JCAnnotation> annotations, JCExpression underlyingType) {
jjg@1521 527 JCAnnotatedType tree = new JCAnnotatedType(annotations, underlyingType);
jjg@1521 528 tree.pos = pos;
jjg@1521 529 return tree;
jjg@1521 530 }
jjg@1521 531
duke@1 532 public JCErroneous Erroneous() {
duke@1 533 return Erroneous(List.<JCTree>nil());
duke@1 534 }
duke@1 535
duke@1 536 public JCErroneous Erroneous(List<? extends JCTree> errs) {
duke@1 537 JCErroneous tree = new JCErroneous(errs);
duke@1 538 tree.pos = pos;
duke@1 539 return tree;
duke@1 540 }
duke@1 541
duke@1 542 public LetExpr LetExpr(List<JCVariableDecl> defs, JCTree expr) {
duke@1 543 LetExpr tree = new LetExpr(defs, expr);
duke@1 544 tree.pos = pos;
duke@1 545 return tree;
duke@1 546 }
duke@1 547
duke@1 548 /* ***************************************************************************
duke@1 549 * Derived building blocks.
duke@1 550 ****************************************************************************/
duke@1 551
duke@1 552 public JCClassDecl AnonymousClassDef(JCModifiers mods,
duke@1 553 List<JCTree> defs)
duke@1 554 {
duke@1 555 return ClassDef(mods,
duke@1 556 names.empty,
duke@1 557 List.<JCTypeParameter>nil(),
duke@1 558 null,
duke@1 559 List.<JCExpression>nil(),
duke@1 560 defs);
duke@1 561 }
duke@1 562
duke@1 563 public LetExpr LetExpr(JCVariableDecl def, JCTree expr) {
duke@1 564 LetExpr tree = new LetExpr(List.of(def), expr);
duke@1 565 tree.pos = pos;
duke@1 566 return tree;
duke@1 567 }
duke@1 568
duke@1 569 /** Create an identifier from a symbol.
duke@1 570 */
duke@1 571 public JCIdent Ident(Symbol sym) {
duke@1 572 return (JCIdent)new JCIdent((sym.name != names.empty)
duke@1 573 ? sym.name
duke@1 574 : sym.flatName(), sym)
duke@1 575 .setPos(pos)
duke@1 576 .setType(sym.type);
duke@1 577 }
duke@1 578
duke@1 579 /** Create a selection node from a qualifier tree and a symbol.
duke@1 580 * @param base The qualifier tree.
duke@1 581 */
duke@1 582 public JCExpression Select(JCExpression base, Symbol sym) {
duke@1 583 return new JCFieldAccess(base, sym.name, sym).setPos(pos).setType(sym.type);
duke@1 584 }
duke@1 585
duke@1 586 /** Create a qualified identifier from a symbol, adding enough qualifications
duke@1 587 * to make the reference unique.
duke@1 588 */
duke@1 589 public JCExpression QualIdent(Symbol sym) {
duke@1 590 return isUnqualifiable(sym)
duke@1 591 ? Ident(sym)
duke@1 592 : Select(QualIdent(sym.owner), sym);
duke@1 593 }
duke@1 594
duke@1 595 /** Create an identifier that refers to the variable declared in given variable
duke@1 596 * declaration.
duke@1 597 */
duke@1 598 public JCExpression Ident(JCVariableDecl param) {
duke@1 599 return Ident(param.sym);
duke@1 600 }
duke@1 601
duke@1 602 /** Create a list of identifiers referring to the variables declared
duke@1 603 * in given list of variable declarations.
duke@1 604 */
duke@1 605 public List<JCExpression> Idents(List<JCVariableDecl> params) {
duke@1 606 ListBuffer<JCExpression> ids = new ListBuffer<JCExpression>();
duke@1 607 for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail)
duke@1 608 ids.append(Ident(l.head));
duke@1 609 return ids.toList();
duke@1 610 }
duke@1 611
duke@1 612 /** Create a tree representing `this', given its type.
duke@1 613 */
duke@1 614 public JCExpression This(Type t) {
duke@1 615 return Ident(new VarSymbol(FINAL, names._this, t, t.tsym));
duke@1 616 }
duke@1 617
duke@1 618 /** Create a tree representing a class literal.
duke@1 619 */
duke@1 620 public JCExpression ClassLiteral(ClassSymbol clazz) {
duke@1 621 return ClassLiteral(clazz.type);
duke@1 622 }
duke@1 623
duke@1 624 /** Create a tree representing a class literal.
duke@1 625 */
duke@1 626 public JCExpression ClassLiteral(Type t) {
duke@1 627 VarSymbol lit = new VarSymbol(STATIC | PUBLIC | FINAL,
duke@1 628 names._class,
duke@1 629 t,
duke@1 630 t.tsym);
duke@1 631 return Select(Type(t), lit);
duke@1 632 }
duke@1 633
duke@1 634 /** Create a tree representing `super', given its type and owner.
duke@1 635 */
duke@1 636 public JCIdent Super(Type t, TypeSymbol owner) {
duke@1 637 return Ident(new VarSymbol(FINAL, names._super, t, owner));
duke@1 638 }
duke@1 639
duke@1 640 /**
duke@1 641 * Create a method invocation from a method tree and a list of
duke@1 642 * argument trees.
duke@1 643 */
duke@1 644 public JCMethodInvocation App(JCExpression meth, List<JCExpression> args) {
duke@1 645 return Apply(null, meth, args).setType(meth.type.getReturnType());
duke@1 646 }
duke@1 647
duke@1 648 /**
duke@1 649 * Create a no-arg method invocation from a method tree
duke@1 650 */
duke@1 651 public JCMethodInvocation App(JCExpression meth) {
duke@1 652 return Apply(null, meth, List.<JCExpression>nil()).setType(meth.type.getReturnType());
duke@1 653 }
duke@1 654
duke@1 655 /** Create a method invocation from a method tree and a list of argument trees.
duke@1 656 */
duke@1 657 public JCExpression Create(Symbol ctor, List<JCExpression> args) {
duke@1 658 Type t = ctor.owner.erasure(types);
duke@1 659 JCNewClass newclass = NewClass(null, null, Type(t), args, null);
duke@1 660 newclass.constructor = ctor;
duke@1 661 newclass.setType(t);
duke@1 662 return newclass;
duke@1 663 }
duke@1 664
duke@1 665 /** Create a tree representing given type.
duke@1 666 */
duke@1 667 public JCExpression Type(Type t) {
duke@1 668 if (t == null) return null;
duke@1 669 JCExpression tp;
jjg@1374 670 switch (t.getTag()) {
duke@1 671 case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
duke@1 672 case DOUBLE: case BOOLEAN: case VOID:
jjg@1374 673 tp = TypeIdent(t.getTag());
duke@1 674 break;
duke@1 675 case TYPEVAR:
duke@1 676 tp = Ident(t.tsym);
duke@1 677 break;
duke@1 678 case WILDCARD: {
duke@1 679 WildcardType a = ((WildcardType) t);
duke@1 680 tp = Wildcard(TypeBoundKind(a.kind), Type(a.type));
duke@1 681 break;
duke@1 682 }
duke@1 683 case CLASS:
duke@1 684 Type outer = t.getEnclosingType();
jjg@1374 685 JCExpression clazz = outer.hasTag(CLASS) && t.tsym.owner.kind == TYP
duke@1 686 ? Select(Type(outer), t.tsym)
duke@1 687 : QualIdent(t.tsym);
duke@1 688 tp = t.getTypeArguments().isEmpty()
duke@1 689 ? clazz
duke@1 690 : TypeApply(clazz, Types(t.getTypeArguments()));
duke@1 691 break;
duke@1 692 case ARRAY:
duke@1 693 tp = TypeArray(Type(types.elemtype(t)));
duke@1 694 break;
duke@1 695 case ERROR:
duke@1 696 tp = TypeIdent(ERROR);
duke@1 697 break;
duke@1 698 default:
duke@1 699 throw new AssertionError("unexpected type: " + t);
duke@1 700 }
duke@1 701 return tp.setType(t);
duke@1 702 }
duke@1 703
duke@1 704 /** Create a list of trees representing given list of types.
duke@1 705 */
duke@1 706 public List<JCExpression> Types(List<Type> ts) {
jjg@816 707 ListBuffer<JCExpression> lb = new ListBuffer<JCExpression>();
duke@1 708 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
jjg@816 709 lb.append(Type(l.head));
jjg@816 710 return lb.toList();
duke@1 711 }
duke@1 712
duke@1 713 /** Create a variable definition from a variable symbol and an initializer
duke@1 714 * expression.
duke@1 715 */
duke@1 716 public JCVariableDecl VarDef(VarSymbol v, JCExpression init) {
duke@1 717 return (JCVariableDecl)
duke@1 718 new JCVariableDecl(
jfranck@1464 719 Modifiers(v.flags(), Annotations(v.getRawAttributes())),
duke@1 720 v.name,
duke@1 721 Type(v.type),
duke@1 722 init,
duke@1 723 v).setPos(pos).setType(v.type);
duke@1 724 }
duke@1 725
duke@1 726 /** Create annotation trees from annotations.
duke@1 727 */
duke@1 728 public List<JCAnnotation> Annotations(List<Attribute.Compound> attributes) {
duke@1 729 if (attributes == null) return List.nil();
duke@1 730 ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>();
duke@1 731 for (List<Attribute.Compound> i = attributes; i.nonEmpty(); i=i.tail) {
duke@1 732 Attribute a = i.head;
duke@1 733 result.append(Annotation(a));
duke@1 734 }
duke@1 735 return result.toList();
duke@1 736 }
duke@1 737
duke@1 738 public JCLiteral Literal(Object value) {
duke@1 739 JCLiteral result = null;
duke@1 740 if (value instanceof String) {
duke@1 741 result = Literal(CLASS, value).
duke@1 742 setType(syms.stringType.constType(value));
duke@1 743 } else if (value instanceof Integer) {
duke@1 744 result = Literal(INT, value).
duke@1 745 setType(syms.intType.constType(value));
duke@1 746 } else if (value instanceof Long) {
duke@1 747 result = Literal(LONG, value).
duke@1 748 setType(syms.longType.constType(value));
duke@1 749 } else if (value instanceof Byte) {
duke@1 750 result = Literal(BYTE, value).
duke@1 751 setType(syms.byteType.constType(value));
duke@1 752 } else if (value instanceof Character) {
jjg@788 753 int v = (int) (((Character) value).toString().charAt(0));
duke@1 754 result = Literal(CHAR, value).
jjg@788 755 setType(syms.charType.constType(v));
duke@1 756 } else if (value instanceof Double) {
duke@1 757 result = Literal(DOUBLE, value).
duke@1 758 setType(syms.doubleType.constType(value));
duke@1 759 } else if (value instanceof Float) {
duke@1 760 result = Literal(FLOAT, value).
duke@1 761 setType(syms.floatType.constType(value));
duke@1 762 } else if (value instanceof Short) {
duke@1 763 result = Literal(SHORT, value).
duke@1 764 setType(syms.shortType.constType(value));
jjg@788 765 } else if (value instanceof Boolean) {
jjg@788 766 int v = ((Boolean) value) ? 1 : 0;
jjg@788 767 result = Literal(BOOLEAN, v).
jjg@788 768 setType(syms.booleanType.constType(v));
duke@1 769 } else {
duke@1 770 throw new AssertionError(value);
duke@1 771 }
duke@1 772 return result;
duke@1 773 }
duke@1 774
duke@1 775 class AnnotationBuilder implements Attribute.Visitor {
duke@1 776 JCExpression result = null;
duke@1 777 public void visitConstant(Attribute.Constant v) {
duke@1 778 result = Literal(v.value);
duke@1 779 }
duke@1 780 public void visitClass(Attribute.Class clazz) {
jfranck@1313 781 result = ClassLiteral(clazz.classType).setType(syms.classType);
duke@1 782 }
duke@1 783 public void visitEnum(Attribute.Enum e) {
duke@1 784 result = QualIdent(e.value);
duke@1 785 }
duke@1 786 public void visitError(Attribute.Error e) {
duke@1 787 result = Erroneous();
duke@1 788 }
duke@1 789 public void visitCompound(Attribute.Compound compound) {
jjg@1521 790 if (compound instanceof Attribute.TypeCompound) {
jjg@1521 791 result = visitTypeCompoundInternal((Attribute.TypeCompound) compound);
jjg@1521 792 } else {
jjg@1521 793 result = visitCompoundInternal(compound);
jjg@1521 794 }
duke@1 795 }
duke@1 796 public JCAnnotation visitCompoundInternal(Attribute.Compound compound) {
duke@1 797 ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
duke@1 798 for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
duke@1 799 Pair<MethodSymbol,Attribute> pair = values.head;
duke@1 800 JCExpression valueTree = translate(pair.snd);
duke@1 801 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
duke@1 802 }
duke@1 803 return Annotation(Type(compound.type), args.toList());
duke@1 804 }
jjg@1521 805 public JCAnnotation visitTypeCompoundInternal(Attribute.TypeCompound compound) {
jjg@1521 806 ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
jjg@1521 807 for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
jjg@1521 808 Pair<MethodSymbol,Attribute> pair = values.head;
jjg@1521 809 JCExpression valueTree = translate(pair.snd);
jjg@1521 810 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
jjg@1521 811 }
jjg@1521 812 return TypeAnnotation(Type(compound.type), args.toList());
jjg@1521 813 }
duke@1 814 public void visitArray(Attribute.Array array) {
duke@1 815 ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>();
duke@1 816 for (int i = 0; i < array.values.length; i++)
duke@1 817 elems.append(translate(array.values[i]));
duke@1 818 result = NewArray(null, List.<JCExpression>nil(), elems.toList()).setType(array.type);
duke@1 819 }
duke@1 820 JCExpression translate(Attribute a) {
duke@1 821 a.accept(this);
duke@1 822 return result;
duke@1 823 }
duke@1 824 JCAnnotation translate(Attribute.Compound a) {
duke@1 825 return visitCompoundInternal(a);
duke@1 826 }
jjg@1521 827 JCAnnotation translate(Attribute.TypeCompound a) {
jjg@1521 828 return visitTypeCompoundInternal(a);
jjg@1521 829 }
duke@1 830 }
jjg@1521 831
duke@1 832 AnnotationBuilder annotationBuilder = new AnnotationBuilder();
duke@1 833
duke@1 834 /** Create an annotation tree from an attribute.
duke@1 835 */
duke@1 836 public JCAnnotation Annotation(Attribute a) {
duke@1 837 return annotationBuilder.translate((Attribute.Compound)a);
duke@1 838 }
duke@1 839
jjg@1521 840 public JCAnnotation TypeAnnotation(Attribute a) {
jjg@1521 841 return annotationBuilder.translate((Attribute.TypeCompound) a);
jjg@1521 842 }
jjg@1521 843
duke@1 844 /** Create a method definition from a method symbol and a method body.
duke@1 845 */
duke@1 846 public JCMethodDecl MethodDef(MethodSymbol m, JCBlock body) {
duke@1 847 return MethodDef(m, m.type, body);
duke@1 848 }
duke@1 849
duke@1 850 /** Create a method definition from a method symbol, method type
duke@1 851 * and a method body.
duke@1 852 */
duke@1 853 public JCMethodDecl MethodDef(MethodSymbol m, Type mtype, JCBlock body) {
duke@1 854 return (JCMethodDecl)
duke@1 855 new JCMethodDecl(
jfranck@1464 856 Modifiers(m.flags(), Annotations(m.getRawAttributes())),
duke@1 857 m.name,
duke@1 858 Type(mtype.getReturnType()),
duke@1 859 TypeParams(mtype.getTypeArguments()),
jjg@1521 860 null, // receiver type
duke@1 861 Params(mtype.getParameterTypes(), m),
duke@1 862 Types(mtype.getThrownTypes()),
duke@1 863 body,
duke@1 864 null,
duke@1 865 m).setPos(pos).setType(mtype);
duke@1 866 }
duke@1 867
duke@1 868 /** Create a type parameter tree from its name and type.
duke@1 869 */
duke@1 870 public JCTypeParameter TypeParam(Name name, TypeVar tvar) {
duke@1 871 return (JCTypeParameter)
duke@1 872 TypeParameter(name, Types(types.getBounds(tvar))).setPos(pos).setType(tvar);
duke@1 873 }
duke@1 874
duke@1 875 /** Create a list of type parameter trees from a list of type variables.
duke@1 876 */
duke@1 877 public List<JCTypeParameter> TypeParams(List<Type> typarams) {
duke@1 878 ListBuffer<JCTypeParameter> tparams = new ListBuffer<JCTypeParameter>();
duke@1 879 for (List<Type> l = typarams; l.nonEmpty(); l = l.tail)
duke@1 880 tparams.append(TypeParam(l.head.tsym.name, (TypeVar)l.head));
duke@1 881 return tparams.toList();
duke@1 882 }
duke@1 883
duke@1 884 /** Create a value parameter tree from its name, type, and owner.
duke@1 885 */
duke@1 886 public JCVariableDecl Param(Name name, Type argtype, Symbol owner) {
duke@1 887 return VarDef(new VarSymbol(0, name, argtype, owner), null);
duke@1 888 }
duke@1 889
duke@1 890 /** Create a a list of value parameter trees x0, ..., xn from a list of
duke@1 891 * their types and an their owner.
duke@1 892 */
duke@1 893 public List<JCVariableDecl> Params(List<Type> argtypes, Symbol owner) {
duke@1 894 ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
duke@1 895 MethodSymbol mth = (owner.kind == MTH) ? ((MethodSymbol)owner) : null;
duke@1 896 if (mth != null && mth.params != null && argtypes.length() == mth.params.length()) {
duke@1 897 for (VarSymbol param : ((MethodSymbol)owner).params)
duke@1 898 params.append(VarDef(param, null));
duke@1 899 } else {
duke@1 900 int i = 0;
duke@1 901 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
duke@1 902 params.append(Param(paramName(i++), l.head, owner));
duke@1 903 }
duke@1 904 return params.toList();
duke@1 905 }
duke@1 906
duke@1 907 /** Wrap a method invocation in an expression statement or return statement,
duke@1 908 * depending on whether the method invocation expression's type is void.
duke@1 909 */
duke@1 910 public JCStatement Call(JCExpression apply) {
jjg@1374 911 return apply.type.hasTag(VOID) ? Exec(apply) : Return(apply);
duke@1 912 }
duke@1 913
duke@1 914 /** Construct an assignment from a variable symbol and a right hand side.
duke@1 915 */
duke@1 916 public JCStatement Assignment(Symbol v, JCExpression rhs) {
duke@1 917 return Exec(Assign(Ident(v), rhs).setType(v.type));
duke@1 918 }
duke@1 919
duke@1 920 /** Construct an index expression from a variable and an expression.
duke@1 921 */
duke@1 922 public JCArrayAccess Indexed(Symbol v, JCExpression index) {
duke@1 923 JCArrayAccess tree = new JCArrayAccess(QualIdent(v), index);
duke@1 924 tree.type = ((ArrayType)v.type).elemtype;
duke@1 925 return tree;
duke@1 926 }
duke@1 927
duke@1 928 /** Make an attributed type cast expression.
duke@1 929 */
duke@1 930 public JCTypeCast TypeCast(Type type, JCExpression expr) {
duke@1 931 return (JCTypeCast)TypeCast(Type(type), expr).setType(type);
duke@1 932 }
duke@1 933
duke@1 934 /* ***************************************************************************
duke@1 935 * Helper methods.
duke@1 936 ****************************************************************************/
duke@1 937
duke@1 938 /** Can given symbol be referred to in unqualified form?
duke@1 939 */
duke@1 940 boolean isUnqualifiable(Symbol sym) {
duke@1 941 if (sym.name == names.empty ||
duke@1 942 sym.owner == null ||
duke@1 943 sym.owner.kind == MTH || sym.owner.kind == VAR) {
duke@1 944 return true;
duke@1 945 } else if (sym.kind == TYP && toplevel != null) {
duke@1 946 Scope.Entry e;
duke@1 947 e = toplevel.namedImportScope.lookup(sym.name);
duke@1 948 if (e.scope != null) {
duke@1 949 return
duke@1 950 e.sym == sym &&
duke@1 951 e.next().scope == null;
duke@1 952 }
duke@1 953 e = toplevel.packge.members().lookup(sym.name);
duke@1 954 if (e.scope != null) {
duke@1 955 return
duke@1 956 e.sym == sym &&
duke@1 957 e.next().scope == null;
duke@1 958 }
duke@1 959 e = toplevel.starImportScope.lookup(sym.name);
duke@1 960 if (e.scope != null) {
duke@1 961 return
duke@1 962 e.sym == sym &&
duke@1 963 e.next().scope == null;
duke@1 964 }
duke@1 965 }
duke@1 966 return false;
duke@1 967 }
duke@1 968
duke@1 969 /** The name of synthetic parameter number `i'.
duke@1 970 */
duke@1 971 public Name paramName(int i) { return names.fromString("x" + i); }
duke@1 972
duke@1 973 /** The name of synthetic type parameter number `i'.
duke@1 974 */
duke@1 975 public Name typaramName(int i) { return names.fromString("A" + i); }
duke@1 976 }

mercurial