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

Fri, 20 Sep 2013 16:33:35 +0200

author
jlahoda
date
Fri, 20 Sep 2013 16:33:35 +0200
changeset 2042
41599b57d262
parent 2027
4932bb04c4b8
child 2525
2eb010b6cb22
child 3172
921a7d6ab90d
permissions
-rw-r--r--

8023835: TreeMaker.QualIdent() too leafy
Reviewed-by: jjg

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

mercurial