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