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