author | jjg |
Tue, 19 Oct 2010 15:02:48 -0700 | |
changeset 7072 | 4863847e93a5 |
parent 6716 | 71df48777dd1 |
child 7074 | 0183c3f9614e |
permissions | -rw-r--r-- |
10 | 1 |
/* |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
6585
diff
changeset
|
2 |
* Copyright (c) 1999, 2010, 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.parser; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
||
30 |
import com.sun.tools.javac.tree.*; |
|
31 |
import com.sun.tools.javac.code.*; |
|
32 |
import com.sun.tools.javac.util.*; |
|
33 |
import com.sun.tools.javac.util.List; |
|
34 |
import static com.sun.tools.javac.util.ListBuffer.lb; |
|
35 |
||
36 |
import com.sun.tools.javac.tree.JCTree.*; |
|
37 |
||
38 |
import static com.sun.tools.javac.parser.Token.*; |
|
39 |
||
40 |
/** The parser maps a token sequence into an abstract syntax |
|
41 |
* tree. It operates by recursive descent, with code derived |
|
42 |
* systematically from an LL(1) grammar. For efficiency reasons, an |
|
43 |
* operator precedence scheme is used for parsing binary operation |
|
44 |
* expressions. |
|
45 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
46 |
* <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
|
47 |
* If you write code that depends on this, you do so at your own risk. |
10 | 48 |
* This code and its internal interfaces are subject to change or |
49 |
* deletion without notice.</b> |
|
50 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
51 |
public class JavacParser implements Parser { |
10 | 52 |
|
53 |
/** The number of precedence levels of infix operators. |
|
54 |
*/ |
|
55 |
private static final int infixPrecedenceLevels = 10; |
|
56 |
||
57 |
/** The scanner used for lexical analysis. |
|
58 |
*/ |
|
59 |
private Lexer S; |
|
60 |
||
61 |
/** The factory to be used for abstract syntax tree construction. |
|
62 |
*/ |
|
63 |
protected TreeMaker F; |
|
64 |
||
65 |
/** The log to be used for error diagnostics. |
|
66 |
*/ |
|
67 |
private Log log; |
|
68 |
||
69 |
/** The keyword table. */ |
|
70 |
private Keywords keywords; |
|
71 |
||
72 |
/** The Source language setting. */ |
|
73 |
private Source source; |
|
74 |
||
75 |
/** The name table. */ |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1258
diff
changeset
|
76 |
private Names names; |
10 | 77 |
|
78 |
/** Construct a parser from a given scanner, tree factory and log. |
|
79 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
80 |
protected JavacParser(ParserFactory fac, |
10 | 81 |
Lexer S, |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
82 |
boolean keepDocComments, |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
83 |
boolean keepLineMap) { |
10 | 84 |
this.S = S; |
85 |
S.nextToken(); // prime the pump |
|
86 |
this.F = fac.F; |
|
87 |
this.log = fac.log; |
|
88 |
this.names = fac.names; |
|
89 |
this.keywords = fac.keywords; |
|
90 |
this.source = fac.source; |
|
91 |
this.allowGenerics = source.allowGenerics(); |
|
92 |
this.allowVarargs = source.allowVarargs(); |
|
93 |
this.allowAsserts = source.allowAsserts(); |
|
94 |
this.allowEnums = source.allowEnums(); |
|
95 |
this.allowForeach = source.allowForeach(); |
|
96 |
this.allowStaticImport = source.allowStaticImport(); |
|
97 |
this.allowAnnotations = source.allowAnnotations(); |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
98 |
this.allowTWR = source.allowTryWithResources(); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
99 |
this.allowDiamond = source.allowDiamond(); |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
100 |
this.allowMulticatch = source.allowMulticatch(); |
10 | 101 |
this.keepDocComments = keepDocComments; |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
102 |
if (keepDocComments) |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
103 |
docComments = new HashMap<JCTree,String>(); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
104 |
this.keepLineMap = keepLineMap; |
10 | 105 |
this.errorTree = F.Erroneous(); |
106 |
} |
|
107 |
||
108 |
/** Switch: Should generics be recognized? |
|
109 |
*/ |
|
110 |
boolean allowGenerics; |
|
111 |
||
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
112 |
/** Switch: Should diamond operator be recognized? |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
113 |
*/ |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
114 |
boolean allowDiamond; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
115 |
|
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
116 |
/** Switch: Should multicatch clause be accepted? |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
117 |
*/ |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
118 |
boolean allowMulticatch; |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
119 |
|
10 | 120 |
/** Switch: Should varargs be recognized? |
121 |
*/ |
|
122 |
boolean allowVarargs; |
|
123 |
||
124 |
/** Switch: should we recognize assert statements, or just give a warning? |
|
125 |
*/ |
|
126 |
boolean allowAsserts; |
|
127 |
||
128 |
/** Switch: should we recognize enums, or just give a warning? |
|
129 |
*/ |
|
130 |
boolean allowEnums; |
|
131 |
||
132 |
/** Switch: should we recognize foreach? |
|
133 |
*/ |
|
134 |
boolean allowForeach; |
|
135 |
||
136 |
/** Switch: should we recognize foreach? |
|
137 |
*/ |
|
138 |
boolean allowStaticImport; |
|
139 |
||
140 |
/** Switch: should we recognize annotations? |
|
141 |
*/ |
|
142 |
boolean allowAnnotations; |
|
143 |
||
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
144 |
/** Switch: should we recognize automatic resource management? |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
145 |
*/ |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
146 |
boolean allowTWR; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
147 |
|
10 | 148 |
/** Switch: should we keep docComments? |
149 |
*/ |
|
150 |
boolean keepDocComments; |
|
151 |
||
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
152 |
/** Switch: should we keep line table? |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
153 |
*/ |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
154 |
boolean keepLineMap; |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
155 |
|
10 | 156 |
/** When terms are parsed, the mode determines which is expected: |
157 |
* mode = EXPR : an expression |
|
158 |
* mode = TYPE : a type |
|
159 |
* mode = NOPARAMS : no parameters allowed for type |
|
160 |
* mode = TYPEARG : type argument |
|
161 |
*/ |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
162 |
static final int EXPR = 0x1; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
163 |
static final int TYPE = 0x2; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
164 |
static final int NOPARAMS = 0x4; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
165 |
static final int TYPEARG = 0x8; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
166 |
static final int DIAMOND = 0x10; |
10 | 167 |
|
168 |
/** The current mode. |
|
169 |
*/ |
|
170 |
private int mode = 0; |
|
171 |
||
172 |
/** The mode of the term that was parsed last. |
|
173 |
*/ |
|
174 |
private int lastmode = 0; |
|
175 |
||
176 |
/* ---------- error recovery -------------- */ |
|
177 |
||
178 |
private JCErroneous errorTree; |
|
179 |
||
180 |
/** Skip forward until a suitable stop token is found. |
|
181 |
*/ |
|
182 |
private void skip(boolean stopAtImport, boolean stopAtMemberDecl, boolean stopAtIdentifier, boolean stopAtStatement) { |
|
183 |
while (true) { |
|
184 |
switch (S.token()) { |
|
185 |
case SEMI: |
|
186 |
S.nextToken(); |
|
187 |
return; |
|
188 |
case PUBLIC: |
|
189 |
case FINAL: |
|
190 |
case ABSTRACT: |
|
191 |
case MONKEYS_AT: |
|
192 |
case EOF: |
|
193 |
case CLASS: |
|
194 |
case INTERFACE: |
|
195 |
case ENUM: |
|
196 |
return; |
|
197 |
case IMPORT: |
|
198 |
if (stopAtImport) |
|
199 |
return; |
|
200 |
break; |
|
201 |
case LBRACE: |
|
202 |
case RBRACE: |
|
203 |
case PRIVATE: |
|
204 |
case PROTECTED: |
|
205 |
case STATIC: |
|
206 |
case TRANSIENT: |
|
207 |
case NATIVE: |
|
208 |
case VOLATILE: |
|
209 |
case SYNCHRONIZED: |
|
210 |
case STRICTFP: |
|
211 |
case LT: |
|
212 |
case BYTE: |
|
213 |
case SHORT: |
|
214 |
case CHAR: |
|
215 |
case INT: |
|
216 |
case LONG: |
|
217 |
case FLOAT: |
|
218 |
case DOUBLE: |
|
219 |
case BOOLEAN: |
|
220 |
case VOID: |
|
221 |
if (stopAtMemberDecl) |
|
222 |
return; |
|
223 |
break; |
|
224 |
case IDENTIFIER: |
|
225 |
if (stopAtIdentifier) |
|
226 |
return; |
|
227 |
break; |
|
228 |
case CASE: |
|
229 |
case DEFAULT: |
|
230 |
case IF: |
|
231 |
case FOR: |
|
232 |
case WHILE: |
|
233 |
case DO: |
|
234 |
case TRY: |
|
235 |
case SWITCH: |
|
236 |
case RETURN: |
|
237 |
case THROW: |
|
238 |
case BREAK: |
|
239 |
case CONTINUE: |
|
240 |
case ELSE: |
|
241 |
case FINALLY: |
|
242 |
case CATCH: |
|
243 |
if (stopAtStatement) |
|
244 |
return; |
|
245 |
break; |
|
246 |
} |
|
247 |
S.nextToken(); |
|
248 |
} |
|
249 |
} |
|
250 |
||
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
251 |
private JCErroneous syntaxError(int pos, String key, Token... args) { |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
252 |
return syntaxError(pos, null, key, args); |
10 | 253 |
} |
254 |
||
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
255 |
private JCErroneous syntaxError(int pos, List<JCTree> errs, String key, Token... args) { |
10 | 256 |
setErrorEndPos(pos); |
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
257 |
reportSyntaxError(pos, key, (Object[])args); |
10 | 258 |
return toP(F.at(pos).Erroneous(errs)); |
259 |
} |
|
260 |
||
261 |
private int errorPos = Position.NOPOS; |
|
262 |
/** |
|
263 |
* Report a syntax error at given position using the given |
|
264 |
* argument unless one was already reported at the same position. |
|
265 |
*/ |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
266 |
private void reportSyntaxError(int pos, String key, Object... args) { |
10 | 267 |
if (pos > S.errPos() || pos == Position.NOPOS) { |
268 |
if (S.token() == EOF) |
|
269 |
log.error(pos, "premature.eof"); |
|
270 |
else |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
271 |
log.error(pos, key, args); |
10 | 272 |
} |
273 |
S.errPos(pos); |
|
274 |
if (S.pos() == errorPos) |
|
275 |
S.nextToken(); // guarantee progress |
|
276 |
errorPos = S.pos(); |
|
277 |
} |
|
278 |
||
279 |
||
280 |
/** Generate a syntax error at current position unless one was already |
|
281 |
* reported at the same position. |
|
282 |
*/ |
|
283 |
private JCErroneous syntaxError(String key) { |
|
284 |
return syntaxError(S.pos(), key); |
|
285 |
} |
|
286 |
||
287 |
/** Generate a syntax error at current position unless one was |
|
288 |
* already reported at the same position. |
|
289 |
*/ |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
290 |
private JCErroneous syntaxError(String key, Token arg) { |
10 | 291 |
return syntaxError(S.pos(), key, arg); |
292 |
} |
|
293 |
||
294 |
/** If next input token matches given token, skip it, otherwise report |
|
295 |
* an error. |
|
296 |
*/ |
|
297 |
public void accept(Token token) { |
|
298 |
if (S.token() == token) { |
|
299 |
S.nextToken(); |
|
300 |
} else { |
|
301 |
setErrorEndPos(S.pos()); |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
302 |
reportSyntaxError(S.prevEndPos(), "expected", token); |
10 | 303 |
} |
304 |
} |
|
305 |
||
306 |
/** Report an illegal start of expression/type error at given position. |
|
307 |
*/ |
|
308 |
JCExpression illegal(int pos) { |
|
309 |
setErrorEndPos(S.pos()); |
|
310 |
if ((mode & EXPR) != 0) |
|
311 |
return syntaxError(pos, "illegal.start.of.expr"); |
|
312 |
else |
|
313 |
return syntaxError(pos, "illegal.start.of.type"); |
|
314 |
||
315 |
} |
|
316 |
||
317 |
/** Report an illegal start of expression/type error at current position. |
|
318 |
*/ |
|
319 |
JCExpression illegal() { |
|
320 |
return illegal(S.pos()); |
|
321 |
} |
|
322 |
||
323 |
/** Diagnose a modifier flag from the set, if any. */ |
|
324 |
void checkNoMods(long mods) { |
|
325 |
if (mods != 0) { |
|
326 |
long lowestMod = mods & -mods; |
|
327 |
log.error(S.pos(), "mod.not.allowed.here", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
328 |
Flags.asFlagSet(lowestMod)); |
10 | 329 |
} |
330 |
} |
|
331 |
||
332 |
/* ---------- doc comments --------- */ |
|
333 |
||
334 |
/** A hashtable to store all documentation comments |
|
335 |
* indexed by the tree nodes they refer to. |
|
336 |
* defined only if option flag keepDocComment is set. |
|
337 |
*/ |
|
338 |
Map<JCTree, String> docComments; |
|
339 |
||
340 |
/** Make an entry into docComments hashtable, |
|
341 |
* provided flag keepDocComments is set and given doc comment is non-null. |
|
342 |
* @param tree The tree to be used as index in the hashtable |
|
343 |
* @param dc The doc comment to associate with the tree, or null. |
|
344 |
*/ |
|
345 |
void attach(JCTree tree, String dc) { |
|
346 |
if (keepDocComments && dc != null) { |
|
347 |
// System.out.println("doc comment = ");System.out.println(dc);//DEBUG |
|
348 |
docComments.put(tree, dc); |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
/* -------- source positions ------- */ |
|
353 |
||
354 |
private int errorEndPos = -1; |
|
355 |
||
356 |
private void setErrorEndPos(int errPos) { |
|
357 |
if (errPos > errorEndPos) |
|
358 |
errorEndPos = errPos; |
|
359 |
} |
|
360 |
||
361 |
protected int getErrorEndPos() { |
|
362 |
return errorEndPos; |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* Store ending position for a tree. |
|
367 |
* @param tree The tree. |
|
368 |
* @param endpos The ending position to associate with the tree. |
|
369 |
*/ |
|
370 |
protected void storeEnd(JCTree tree, int endpos) {} |
|
371 |
||
372 |
/** |
|
373 |
* Store ending position for a tree. The ending position should |
|
374 |
* be the ending position of the current token. |
|
375 |
* @param t The tree. |
|
376 |
*/ |
|
377 |
protected <T extends JCTree> T to(T t) { return t; } |
|
378 |
||
379 |
/** |
|
380 |
* Store ending position for a tree. The ending position should |
|
381 |
* be greater of the ending position of the previous token and errorEndPos. |
|
382 |
* @param t The tree. |
|
383 |
*/ |
|
384 |
protected <T extends JCTree> T toP(T t) { return t; } |
|
385 |
||
386 |
/** Get the start position for a tree node. The start position is |
|
387 |
* defined to be the position of the first character of the first |
|
388 |
* token of the node's source text. |
|
389 |
* @param tree The tree node |
|
390 |
*/ |
|
391 |
public int getStartPos(JCTree tree) { |
|
392 |
return TreeInfo.getStartPos(tree); |
|
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* Get the end position for a tree node. The end position is |
|
397 |
* defined to be the position of the last character of the last |
|
398 |
* token of the node's source text. Returns Position.NOPOS if end |
|
399 |
* positions are not generated or the position is otherwise not |
|
400 |
* found. |
|
401 |
* @param tree The tree node |
|
402 |
*/ |
|
403 |
public int getEndPos(JCTree tree) { |
|
404 |
return Position.NOPOS; |
|
405 |
} |
|
406 |
||
407 |
||
408 |
||
409 |
/* ---------- parsing -------------- */ |
|
410 |
||
411 |
/** |
|
412 |
* Ident = IDENTIFIER |
|
413 |
*/ |
|
414 |
Name ident() { |
|
415 |
if (S.token() == IDENTIFIER) { |
|
416 |
Name name = S.name(); |
|
417 |
S.nextToken(); |
|
418 |
return name; |
|
419 |
} else if (S.token() == ASSERT) { |
|
420 |
if (allowAsserts) { |
|
421 |
log.error(S.pos(), "assert.as.identifier"); |
|
422 |
S.nextToken(); |
|
423 |
return names.error; |
|
424 |
} else { |
|
425 |
log.warning(S.pos(), "assert.as.identifier"); |
|
426 |
Name name = S.name(); |
|
427 |
S.nextToken(); |
|
428 |
return name; |
|
429 |
} |
|
430 |
} else if (S.token() == ENUM) { |
|
431 |
if (allowEnums) { |
|
432 |
log.error(S.pos(), "enum.as.identifier"); |
|
433 |
S.nextToken(); |
|
434 |
return names.error; |
|
435 |
} else { |
|
436 |
log.warning(S.pos(), "enum.as.identifier"); |
|
437 |
Name name = S.name(); |
|
438 |
S.nextToken(); |
|
439 |
return name; |
|
440 |
} |
|
441 |
} else { |
|
442 |
accept(IDENTIFIER); |
|
443 |
return names.error; |
|
444 |
} |
|
445 |
} |
|
446 |
||
447 |
/** |
|
448 |
* Qualident = Ident { DOT Ident } |
|
449 |
*/ |
|
450 |
public JCExpression qualident() { |
|
451 |
JCExpression t = toP(F.at(S.pos()).Ident(ident())); |
|
452 |
while (S.token() == DOT) { |
|
453 |
int pos = S.pos(); |
|
454 |
S.nextToken(); |
|
455 |
t = toP(F.at(pos).Select(t, ident())); |
|
456 |
} |
|
457 |
return t; |
|
458 |
} |
|
459 |
||
460 |
/** |
|
461 |
* Literal = |
|
462 |
* INTLITERAL |
|
463 |
* | LONGLITERAL |
|
464 |
* | FLOATLITERAL |
|
465 |
* | DOUBLELITERAL |
|
466 |
* | CHARLITERAL |
|
467 |
* | STRINGLITERAL |
|
468 |
* | TRUE |
|
469 |
* | FALSE |
|
470 |
* | NULL |
|
471 |
*/ |
|
472 |
JCExpression literal(Name prefix) { |
|
473 |
int pos = S.pos(); |
|
474 |
JCExpression t = errorTree; |
|
475 |
switch (S.token()) { |
|
476 |
case INTLITERAL: |
|
477 |
try { |
|
478 |
t = F.at(pos).Literal( |
|
479 |
TypeTags.INT, |
|
480 |
Convert.string2int(strval(prefix), S.radix())); |
|
481 |
} catch (NumberFormatException ex) { |
|
482 |
log.error(S.pos(), "int.number.too.large", strval(prefix)); |
|
483 |
} |
|
484 |
break; |
|
485 |
case LONGLITERAL: |
|
486 |
try { |
|
487 |
t = F.at(pos).Literal( |
|
488 |
TypeTags.LONG, |
|
489 |
new Long(Convert.string2long(strval(prefix), S.radix()))); |
|
490 |
} catch (NumberFormatException ex) { |
|
491 |
log.error(S.pos(), "int.number.too.large", strval(prefix)); |
|
492 |
} |
|
493 |
break; |
|
494 |
case FLOATLITERAL: { |
|
495 |
String proper = (S.radix() == 16 ? ("0x"+ S.stringVal()) : S.stringVal()); |
|
496 |
Float n; |
|
497 |
try { |
|
498 |
n = Float.valueOf(proper); |
|
499 |
} catch (NumberFormatException ex) { |
|
500 |
// error already repoted in scanner |
|
501 |
n = Float.NaN; |
|
502 |
} |
|
503 |
if (n.floatValue() == 0.0f && !isZero(proper)) |
|
504 |
log.error(S.pos(), "fp.number.too.small"); |
|
505 |
else if (n.floatValue() == Float.POSITIVE_INFINITY) |
|
506 |
log.error(S.pos(), "fp.number.too.large"); |
|
507 |
else |
|
508 |
t = F.at(pos).Literal(TypeTags.FLOAT, n); |
|
509 |
break; |
|
510 |
} |
|
511 |
case DOUBLELITERAL: { |
|
512 |
String proper = (S.radix() == 16 ? ("0x"+ S.stringVal()) : S.stringVal()); |
|
513 |
Double n; |
|
514 |
try { |
|
515 |
n = Double.valueOf(proper); |
|
516 |
} catch (NumberFormatException ex) { |
|
517 |
// error already reported in scanner |
|
518 |
n = Double.NaN; |
|
519 |
} |
|
520 |
if (n.doubleValue() == 0.0d && !isZero(proper)) |
|
521 |
log.error(S.pos(), "fp.number.too.small"); |
|
522 |
else if (n.doubleValue() == Double.POSITIVE_INFINITY) |
|
523 |
log.error(S.pos(), "fp.number.too.large"); |
|
524 |
else |
|
525 |
t = F.at(pos).Literal(TypeTags.DOUBLE, n); |
|
526 |
break; |
|
527 |
} |
|
528 |
case CHARLITERAL: |
|
529 |
t = F.at(pos).Literal( |
|
530 |
TypeTags.CHAR, |
|
531 |
S.stringVal().charAt(0) + 0); |
|
532 |
break; |
|
533 |
case STRINGLITERAL: |
|
534 |
t = F.at(pos).Literal( |
|
535 |
TypeTags.CLASS, |
|
536 |
S.stringVal()); |
|
537 |
break; |
|
538 |
case TRUE: case FALSE: |
|
539 |
t = F.at(pos).Literal( |
|
540 |
TypeTags.BOOLEAN, |
|
541 |
(S.token() == TRUE ? 1 : 0)); |
|
542 |
break; |
|
543 |
case NULL: |
|
544 |
t = F.at(pos).Literal( |
|
545 |
TypeTags.BOT, |
|
546 |
null); |
|
547 |
break; |
|
548 |
default: |
|
549 |
assert false; |
|
550 |
} |
|
551 |
if (t == errorTree) |
|
552 |
t = F.at(pos).Erroneous(); |
|
553 |
storeEnd(t, S.endPos()); |
|
554 |
S.nextToken(); |
|
555 |
return t; |
|
556 |
} |
|
557 |
//where |
|
558 |
boolean isZero(String s) { |
|
559 |
char[] cs = s.toCharArray(); |
|
3894 | 560 |
int base = ((cs.length > 1 && Character.toLowerCase(cs[1]) == 'x') ? 16 : 10); |
10 | 561 |
int i = ((base==16) ? 2 : 0); |
562 |
while (i < cs.length && (cs[i] == '0' || cs[i] == '.')) i++; |
|
563 |
return !(i < cs.length && (Character.digit(cs[i], base) > 0)); |
|
564 |
} |
|
565 |
||
566 |
String strval(Name prefix) { |
|
567 |
String s = S.stringVal(); |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1258
diff
changeset
|
568 |
return prefix.isEmpty() ? s : prefix + s; |
10 | 569 |
} |
570 |
||
571 |
/** terms can be either expressions or types. |
|
572 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
573 |
public JCExpression parseExpression() { |
10 | 574 |
return term(EXPR); |
575 |
} |
|
576 |
||
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
577 |
public JCExpression parseType() { |
10 | 578 |
return term(TYPE); |
579 |
} |
|
580 |
||
581 |
JCExpression term(int newmode) { |
|
582 |
int prevmode = mode; |
|
583 |
mode = newmode; |
|
584 |
JCExpression t = term(); |
|
585 |
lastmode = mode; |
|
586 |
mode = prevmode; |
|
587 |
return t; |
|
588 |
} |
|
589 |
||
590 |
/** |
|
591 |
* Expression = Expression1 [ExpressionRest] |
|
592 |
* ExpressionRest = [AssignmentOperator Expression1] |
|
593 |
* AssignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" | |
|
594 |
* "&=" | "|=" | "^=" | |
|
595 |
* "%=" | "<<=" | ">>=" | ">>>=" |
|
596 |
* Type = Type1 |
|
597 |
* TypeNoParams = TypeNoParams1 |
|
598 |
* StatementExpression = Expression |
|
599 |
* ConstantExpression = Expression |
|
600 |
*/ |
|
601 |
JCExpression term() { |
|
602 |
JCExpression t = term1(); |
|
603 |
if ((mode & EXPR) != 0 && |
|
604 |
S.token() == EQ || PLUSEQ.compareTo(S.token()) <= 0 && S.token().compareTo(GTGTGTEQ) <= 0) |
|
605 |
return termRest(t); |
|
606 |
else |
|
607 |
return t; |
|
608 |
} |
|
609 |
||
610 |
JCExpression termRest(JCExpression t) { |
|
611 |
switch (S.token()) { |
|
612 |
case EQ: { |
|
613 |
int pos = S.pos(); |
|
614 |
S.nextToken(); |
|
615 |
mode = EXPR; |
|
616 |
JCExpression t1 = term(); |
|
617 |
return toP(F.at(pos).Assign(t, t1)); |
|
618 |
} |
|
619 |
case PLUSEQ: |
|
620 |
case SUBEQ: |
|
621 |
case STAREQ: |
|
622 |
case SLASHEQ: |
|
623 |
case PERCENTEQ: |
|
624 |
case AMPEQ: |
|
625 |
case BAREQ: |
|
626 |
case CARETEQ: |
|
627 |
case LTLTEQ: |
|
628 |
case GTGTEQ: |
|
629 |
case GTGTGTEQ: |
|
630 |
int pos = S.pos(); |
|
631 |
Token token = S.token(); |
|
632 |
S.nextToken(); |
|
633 |
mode = EXPR; |
|
634 |
JCExpression t1 = term(); |
|
635 |
return F.at(pos).Assignop(optag(token), t, t1); |
|
636 |
default: |
|
637 |
return t; |
|
638 |
} |
|
639 |
} |
|
640 |
||
641 |
/** Expression1 = Expression2 [Expression1Rest] |
|
642 |
* Type1 = Type2 |
|
643 |
* TypeNoParams1 = TypeNoParams2 |
|
644 |
*/ |
|
645 |
JCExpression term1() { |
|
646 |
JCExpression t = term2(); |
|
647 |
if ((mode & EXPR) != 0 && S.token() == QUES) { |
|
648 |
mode = EXPR; |
|
649 |
return term1Rest(t); |
|
650 |
} else { |
|
651 |
return t; |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
/** Expression1Rest = ["?" Expression ":" Expression1] |
|
656 |
*/ |
|
657 |
JCExpression term1Rest(JCExpression t) { |
|
658 |
if (S.token() == QUES) { |
|
659 |
int pos = S.pos(); |
|
660 |
S.nextToken(); |
|
661 |
JCExpression t1 = term(); |
|
662 |
accept(COLON); |
|
663 |
JCExpression t2 = term1(); |
|
664 |
return F.at(pos).Conditional(t, t1, t2); |
|
665 |
} else { |
|
666 |
return t; |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
/** Expression2 = Expression3 [Expression2Rest] |
|
671 |
* Type2 = Type3 |
|
672 |
* TypeNoParams2 = TypeNoParams3 |
|
673 |
*/ |
|
674 |
JCExpression term2() { |
|
675 |
JCExpression t = term3(); |
|
676 |
if ((mode & EXPR) != 0 && prec(S.token()) >= TreeInfo.orPrec) { |
|
677 |
mode = EXPR; |
|
678 |
return term2Rest(t, TreeInfo.orPrec); |
|
679 |
} else { |
|
680 |
return t; |
|
681 |
} |
|
682 |
} |
|
683 |
||
684 |
/* Expression2Rest = {infixop Expression3} |
|
685 |
* | Expression3 instanceof Type |
|
686 |
* infixop = "||" |
|
687 |
* | "&&" |
|
688 |
* | "|" |
|
689 |
* | "^" |
|
690 |
* | "&" |
|
691 |
* | "==" | "!=" |
|
692 |
* | "<" | ">" | "<=" | ">=" |
|
693 |
* | "<<" | ">>" | ">>>" |
|
694 |
* | "+" | "-" |
|
695 |
* | "*" | "/" | "%" |
|
696 |
*/ |
|
697 |
JCExpression term2Rest(JCExpression t, int minprec) { |
|
698 |
List<JCExpression[]> savedOd = odStackSupply.elems; |
|
699 |
JCExpression[] odStack = newOdStack(); |
|
700 |
List<Token[]> savedOp = opStackSupply.elems; |
|
701 |
Token[] opStack = newOpStack(); |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
702 |
List<int[]> savedPos = posStackSupply.elems; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
703 |
int[] posStack = newPosStack(); |
10 | 704 |
// optimization, was odStack = new Tree[...]; opStack = new Tree[...]; |
705 |
int top = 0; |
|
706 |
odStack[0] = t; |
|
707 |
int startPos = S.pos(); |
|
708 |
Token topOp = ERROR; |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
709 |
int topOpPos = Position.NOPOS; |
10 | 710 |
while (prec(S.token()) >= minprec) { |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
711 |
posStack[top] = topOpPos; |
10 | 712 |
opStack[top] = topOp; |
713 |
top++; |
|
714 |
topOp = S.token(); |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
715 |
topOpPos = S.pos(); |
10 | 716 |
S.nextToken(); |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
717 |
odStack[top] = (topOp == INSTANCEOF) ? parseType() : term3(); |
10 | 718 |
while (top > 0 && prec(topOp) >= prec(S.token())) { |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
719 |
odStack[top-1] = makeOp(topOpPos, topOp, odStack[top-1], |
10 | 720 |
odStack[top]); |
721 |
top--; |
|
722 |
topOp = opStack[top]; |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
723 |
topOpPos = posStack[top]; |
10 | 724 |
} |
725 |
} |
|
726 |
assert top == 0; |
|
727 |
t = odStack[0]; |
|
728 |
||
729 |
if (t.getTag() == JCTree.PLUS) { |
|
730 |
StringBuffer buf = foldStrings(t); |
|
731 |
if (buf != null) { |
|
732 |
t = toP(F.at(startPos).Literal(TypeTags.CLASS, buf.toString())); |
|
733 |
} |
|
734 |
} |
|
735 |
||
736 |
odStackSupply.elems = savedOd; // optimization |
|
737 |
opStackSupply.elems = savedOp; // optimization |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
738 |
posStackSupply.elems = savedPos; // optimization |
10 | 739 |
return t; |
740 |
} |
|
741 |
//where |
|
742 |
/** Construct a binary or type test node. |
|
743 |
*/ |
|
744 |
private JCExpression makeOp(int pos, |
|
745 |
Token topOp, |
|
746 |
JCExpression od1, |
|
747 |
JCExpression od2) |
|
748 |
{ |
|
749 |
if (topOp == INSTANCEOF) { |
|
750 |
return F.at(pos).TypeTest(od1, od2); |
|
751 |
} else { |
|
752 |
return F.at(pos).Binary(optag(topOp), od1, od2); |
|
753 |
} |
|
754 |
} |
|
755 |
/** If tree is a concatenation of string literals, replace it |
|
756 |
* by a single literal representing the concatenated string. |
|
757 |
*/ |
|
758 |
protected StringBuffer foldStrings(JCTree tree) { |
|
759 |
List<String> buf = List.nil(); |
|
760 |
while (true) { |
|
761 |
if (tree.getTag() == JCTree.LITERAL) { |
|
762 |
JCLiteral lit = (JCLiteral) tree; |
|
763 |
if (lit.typetag == TypeTags.CLASS) { |
|
764 |
StringBuffer sbuf = |
|
765 |
new StringBuffer((String)lit.value); |
|
766 |
while (buf.nonEmpty()) { |
|
767 |
sbuf.append(buf.head); |
|
768 |
buf = buf.tail; |
|
769 |
} |
|
770 |
return sbuf; |
|
771 |
} |
|
772 |
} else if (tree.getTag() == JCTree.PLUS) { |
|
773 |
JCBinary op = (JCBinary)tree; |
|
774 |
if (op.rhs.getTag() == JCTree.LITERAL) { |
|
775 |
JCLiteral lit = (JCLiteral) op.rhs; |
|
776 |
if (lit.typetag == TypeTags.CLASS) { |
|
777 |
buf = buf.prepend((String) lit.value); |
|
778 |
tree = op.lhs; |
|
779 |
continue; |
|
780 |
} |
|
781 |
} |
|
782 |
} |
|
783 |
return null; |
|
784 |
} |
|
785 |
} |
|
786 |
||
787 |
/** optimization: To save allocating a new operand/operator stack |
|
788 |
* for every binary operation, we use supplys. |
|
789 |
*/ |
|
790 |
ListBuffer<JCExpression[]> odStackSupply = new ListBuffer<JCExpression[]>(); |
|
791 |
ListBuffer<Token[]> opStackSupply = new ListBuffer<Token[]>(); |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
792 |
ListBuffer<int[]> posStackSupply = new ListBuffer<int[]>(); |
10 | 793 |
|
794 |
private JCExpression[] newOdStack() { |
|
795 |
if (odStackSupply.elems == odStackSupply.last) |
|
796 |
odStackSupply.append(new JCExpression[infixPrecedenceLevels + 1]); |
|
797 |
JCExpression[] odStack = odStackSupply.elems.head; |
|
798 |
odStackSupply.elems = odStackSupply.elems.tail; |
|
799 |
return odStack; |
|
800 |
} |
|
801 |
||
802 |
private Token[] newOpStack() { |
|
803 |
if (opStackSupply.elems == opStackSupply.last) |
|
804 |
opStackSupply.append(new Token[infixPrecedenceLevels + 1]); |
|
805 |
Token[] opStack = opStackSupply.elems.head; |
|
806 |
opStackSupply.elems = opStackSupply.elems.tail; |
|
807 |
return opStack; |
|
808 |
} |
|
809 |
||
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
810 |
private int[] newPosStack() { |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
811 |
if (posStackSupply.elems == posStackSupply.last) |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
812 |
posStackSupply.append(new int[infixPrecedenceLevels + 1]); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
813 |
int[] posStack = posStackSupply.elems.head; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
814 |
posStackSupply.elems = posStackSupply.elems.tail; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
815 |
return posStack; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
816 |
} |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
817 |
|
10 | 818 |
/** Expression3 = PrefixOp Expression3 |
819 |
* | "(" Expr | TypeNoParams ")" Expression3 |
|
820 |
* | Primary {Selector} {PostfixOp} |
|
821 |
* Primary = "(" Expression ")" |
|
822 |
* | Literal |
|
823 |
* | [TypeArguments] THIS [Arguments] |
|
824 |
* | [TypeArguments] SUPER SuperSuffix |
|
825 |
* | NEW [TypeArguments] Creator |
|
7072 | 826 |
* | Ident { "." Ident } |
827 |
* [ "[" ( "]" BracketsOpt "." CLASS | Expression "]" ) |
|
10 | 828 |
* | Arguments |
829 |
* | "." ( CLASS | THIS | [TypeArguments] SUPER Arguments | NEW [TypeArguments] InnerCreator ) |
|
830 |
* ] |
|
831 |
* | BasicType BracketsOpt "." CLASS |
|
832 |
* PrefixOp = "++" | "--" | "!" | "~" | "+" | "-" |
|
833 |
* PostfixOp = "++" | "--" |
|
834 |
* Type3 = Ident { "." Ident } [TypeArguments] {TypeSelector} BracketsOpt |
|
835 |
* | BasicType |
|
836 |
* TypeNoParams3 = Ident { "." Ident } BracketsOpt |
|
837 |
* Selector = "." [TypeArguments] Ident [Arguments] |
|
838 |
* | "." THIS |
|
839 |
* | "." [TypeArguments] SUPER SuperSuffix |
|
840 |
* | "." NEW [TypeArguments] InnerCreator |
|
841 |
* | "[" Expression "]" |
|
842 |
* TypeSelector = "." Ident [TypeArguments] |
|
843 |
* SuperSuffix = Arguments | "." Ident [Arguments] |
|
844 |
*/ |
|
845 |
protected JCExpression term3() { |
|
846 |
int pos = S.pos(); |
|
847 |
JCExpression t; |
|
848 |
List<JCExpression> typeArgs = typeArgumentsOpt(EXPR); |
|
849 |
switch (S.token()) { |
|
850 |
case QUES: |
|
851 |
if ((mode & TYPE) != 0 && (mode & (TYPEARG|NOPARAMS)) == TYPEARG) { |
|
852 |
mode = TYPE; |
|
853 |
return typeArgument(); |
|
854 |
} else |
|
855 |
return illegal(); |
|
856 |
case PLUSPLUS: case SUBSUB: case BANG: case TILDE: case PLUS: case SUB: |
|
857 |
if (typeArgs == null && (mode & EXPR) != 0) { |
|
858 |
Token token = S.token(); |
|
859 |
S.nextToken(); |
|
860 |
mode = EXPR; |
|
861 |
if (token == SUB && |
|
862 |
(S.token() == INTLITERAL || S.token() == LONGLITERAL) && |
|
863 |
S.radix() == 10) { |
|
864 |
mode = EXPR; |
|
865 |
t = literal(names.hyphen); |
|
866 |
} else { |
|
867 |
t = term3(); |
|
868 |
return F.at(pos).Unary(unoptag(token), t); |
|
869 |
} |
|
870 |
} else return illegal(); |
|
871 |
break; |
|
872 |
case LPAREN: |
|
873 |
if (typeArgs == null && (mode & EXPR) != 0) { |
|
874 |
S.nextToken(); |
|
875 |
mode = EXPR | TYPE | NOPARAMS; |
|
876 |
t = term3(); |
|
877 |
if ((mode & TYPE) != 0 && S.token() == LT) { |
|
878 |
// Could be a cast to a parameterized type |
|
879 |
int op = JCTree.LT; |
|
880 |
int pos1 = S.pos(); |
|
881 |
S.nextToken(); |
|
882 |
mode &= (EXPR | TYPE); |
|
883 |
mode |= TYPEARG; |
|
884 |
JCExpression t1 = term3(); |
|
885 |
if ((mode & TYPE) != 0 && |
|
886 |
(S.token() == COMMA || S.token() == GT)) { |
|
887 |
mode = TYPE; |
|
888 |
ListBuffer<JCExpression> args = new ListBuffer<JCExpression>(); |
|
889 |
args.append(t1); |
|
890 |
while (S.token() == COMMA) { |
|
891 |
S.nextToken(); |
|
892 |
args.append(typeArgument()); |
|
893 |
} |
|
894 |
accept(GT); |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
895 |
t = toP(F.at(pos1).TypeApply(t, args.toList())); |
10 | 896 |
checkGenerics(); |
1790
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1260
diff
changeset
|
897 |
while (S.token() == DOT) { |
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1260
diff
changeset
|
898 |
S.nextToken(); |
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1260
diff
changeset
|
899 |
mode = TYPE; |
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1260
diff
changeset
|
900 |
t = toP(F.at(S.pos()).Select(t, ident())); |
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1260
diff
changeset
|
901 |
t = typeArgumentsOpt(t); |
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1260
diff
changeset
|
902 |
} |
1867
0a3af3ae0d8e
6795580: parser confused by square brackets in qualified generic cast
mcimadamore
parents:
1790
diff
changeset
|
903 |
t = bracketsOpt(toP(t)); |
10 | 904 |
} else if ((mode & EXPR) != 0) { |
905 |
mode = EXPR; |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
906 |
JCExpression e = term2Rest(t1, TreeInfo.shiftPrec); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
907 |
t = F.at(pos1).Binary(op, t, e); |
10 | 908 |
t = termRest(term1Rest(term2Rest(t, TreeInfo.orPrec))); |
909 |
} else { |
|
910 |
accept(GT); |
|
911 |
} |
|
1790
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1260
diff
changeset
|
912 |
} |
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1260
diff
changeset
|
913 |
else { |
10 | 914 |
t = termRest(term1Rest(term2Rest(t, TreeInfo.orPrec))); |
915 |
} |
|
916 |
accept(RPAREN); |
|
917 |
lastmode = mode; |
|
918 |
mode = EXPR; |
|
919 |
if ((lastmode & EXPR) == 0) { |
|
920 |
JCExpression t1 = term3(); |
|
921 |
return F.at(pos).TypeCast(t, t1); |
|
922 |
} else if ((lastmode & TYPE) != 0) { |
|
923 |
switch (S.token()) { |
|
924 |
/*case PLUSPLUS: case SUBSUB: */ |
|
925 |
case BANG: case TILDE: |
|
926 |
case LPAREN: case THIS: case SUPER: |
|
927 |
case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: |
|
928 |
case DOUBLELITERAL: case CHARLITERAL: case STRINGLITERAL: |
|
929 |
case TRUE: case FALSE: case NULL: |
|
930 |
case NEW: case IDENTIFIER: case ASSERT: case ENUM: |
|
931 |
case BYTE: case SHORT: case CHAR: case INT: |
|
932 |
case LONG: case FLOAT: case DOUBLE: case BOOLEAN: case VOID: |
|
933 |
JCExpression t1 = term3(); |
|
934 |
return F.at(pos).TypeCast(t, t1); |
|
935 |
} |
|
936 |
} |
|
937 |
} else return illegal(); |
|
938 |
t = toP(F.at(pos).Parens(t)); |
|
939 |
break; |
|
940 |
case THIS: |
|
941 |
if ((mode & EXPR) != 0) { |
|
942 |
mode = EXPR; |
|
943 |
t = to(F.at(pos).Ident(names._this)); |
|
944 |
S.nextToken(); |
|
945 |
if (typeArgs == null) |
|
946 |
t = argumentsOpt(null, t); |
|
947 |
else |
|
948 |
t = arguments(typeArgs, t); |
|
949 |
typeArgs = null; |
|
950 |
} else return illegal(); |
|
951 |
break; |
|
952 |
case SUPER: |
|
953 |
if ((mode & EXPR) != 0) { |
|
954 |
mode = EXPR; |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
955 |
t = to(F.at(pos).Ident(names._super)); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
956 |
t = superSuffix(typeArgs, t); |
10 | 957 |
typeArgs = null; |
958 |
} else return illegal(); |
|
959 |
break; |
|
960 |
case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: case DOUBLELITERAL: |
|
961 |
case CHARLITERAL: case STRINGLITERAL: |
|
962 |
case TRUE: case FALSE: case NULL: |
|
963 |
if (typeArgs == null && (mode & EXPR) != 0) { |
|
964 |
mode = EXPR; |
|
965 |
t = literal(names.empty); |
|
966 |
} else return illegal(); |
|
967 |
break; |
|
968 |
case NEW: |
|
969 |
if (typeArgs != null) return illegal(); |
|
970 |
if ((mode & EXPR) != 0) { |
|
971 |
mode = EXPR; |
|
972 |
S.nextToken(); |
|
973 |
if (S.token() == LT) typeArgs = typeArguments(); |
|
974 |
t = creator(pos, typeArgs); |
|
975 |
typeArgs = null; |
|
976 |
} else return illegal(); |
|
977 |
break; |
|
978 |
case IDENTIFIER: case ASSERT: case ENUM: |
|
979 |
if (typeArgs != null) return illegal(); |
|
980 |
t = toP(F.at(S.pos()).Ident(ident())); |
|
981 |
loop: while (true) { |
|
982 |
pos = S.pos(); |
|
983 |
switch (S.token()) { |
|
984 |
case LBRACKET: |
|
985 |
S.nextToken(); |
|
986 |
if (S.token() == RBRACKET) { |
|
987 |
S.nextToken(); |
|
7072 | 988 |
t = bracketsOpt(t); |
10 | 989 |
t = toP(F.at(pos).TypeArray(t)); |
990 |
t = bracketsSuffix(t); |
|
991 |
} else { |
|
992 |
if ((mode & EXPR) != 0) { |
|
993 |
mode = EXPR; |
|
994 |
JCExpression t1 = term(); |
|
995 |
t = to(F.at(pos).Indexed(t, t1)); |
|
996 |
} |
|
997 |
accept(RBRACKET); |
|
998 |
} |
|
999 |
break loop; |
|
1000 |
case LPAREN: |
|
1001 |
if ((mode & EXPR) != 0) { |
|
1002 |
mode = EXPR; |
|
1003 |
t = arguments(typeArgs, t); |
|
1004 |
typeArgs = null; |
|
1005 |
} |
|
1006 |
break loop; |
|
1007 |
case DOT: |
|
1008 |
S.nextToken(); |
|
510
8d0b2c7d60a1
6481655: Parser confused by combination of parens and explicit type args
mcimadamore
parents:
10
diff
changeset
|
1009 |
int oldmode = mode; |
8d0b2c7d60a1
6481655: Parser confused by combination of parens and explicit type args
mcimadamore
parents:
10
diff
changeset
|
1010 |
mode &= ~NOPARAMS; |
10 | 1011 |
typeArgs = typeArgumentsOpt(EXPR); |
510
8d0b2c7d60a1
6481655: Parser confused by combination of parens and explicit type args
mcimadamore
parents:
10
diff
changeset
|
1012 |
mode = oldmode; |
10 | 1013 |
if ((mode & EXPR) != 0) { |
1014 |
switch (S.token()) { |
|
1015 |
case CLASS: |
|
1016 |
if (typeArgs != null) return illegal(); |
|
1017 |
mode = EXPR; |
|
1018 |
t = to(F.at(pos).Select(t, names._class)); |
|
1019 |
S.nextToken(); |
|
1020 |
break loop; |
|
1021 |
case THIS: |
|
1022 |
if (typeArgs != null) return illegal(); |
|
1023 |
mode = EXPR; |
|
1024 |
t = to(F.at(pos).Select(t, names._this)); |
|
1025 |
S.nextToken(); |
|
1026 |
break loop; |
|
1027 |
case SUPER: |
|
1028 |
mode = EXPR; |
|
1029 |
t = to(F.at(pos).Select(t, names._super)); |
|
1030 |
t = superSuffix(typeArgs, t); |
|
1031 |
typeArgs = null; |
|
1032 |
break loop; |
|
1033 |
case NEW: |
|
1034 |
if (typeArgs != null) return illegal(); |
|
1035 |
mode = EXPR; |
|
1036 |
int pos1 = S.pos(); |
|
1037 |
S.nextToken(); |
|
1038 |
if (S.token() == LT) typeArgs = typeArguments(); |
|
1039 |
t = innerCreator(pos1, typeArgs, t); |
|
1040 |
typeArgs = null; |
|
1041 |
break loop; |
|
1042 |
} |
|
1043 |
} |
|
1044 |
// typeArgs saved for next loop iteration. |
|
1045 |
t = toP(F.at(pos).Select(t, ident())); |
|
1046 |
break; |
|
1047 |
default: |
|
1048 |
break loop; |
|
1049 |
} |
|
1050 |
} |
|
1051 |
if (typeArgs != null) illegal(); |
|
1052 |
t = typeArgumentsOpt(t); |
|
1053 |
break; |
|
1054 |
case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT: |
|
1055 |
case DOUBLE: case BOOLEAN: |
|
1056 |
if (typeArgs != null) illegal(); |
|
1057 |
t = bracketsSuffix(bracketsOpt(basicType())); |
|
1058 |
break; |
|
1059 |
case VOID: |
|
1060 |
if (typeArgs != null) illegal(); |
|
1061 |
if ((mode & EXPR) != 0) { |
|
1062 |
S.nextToken(); |
|
1063 |
if (S.token() == DOT) { |
|
1064 |
JCPrimitiveTypeTree ti = toP(F.at(pos).TypeIdent(TypeTags.VOID)); |
|
1065 |
t = bracketsSuffix(ti); |
|
1066 |
} else { |
|
1067 |
return illegal(pos); |
|
1068 |
} |
|
1069 |
} else { |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1070 |
// Support the corner case of myMethodHandle.<void>invoke() by passing |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1071 |
// a void type (like other primitive types) to the next phase. |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1072 |
// The error will be reported in Attr.attribTypes or Attr.visitApply. |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1073 |
JCPrimitiveTypeTree ti = to(F.at(pos).TypeIdent(TypeTags.VOID)); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1074 |
S.nextToken(); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1075 |
return ti; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1076 |
//return illegal(); |
10 | 1077 |
} |
1078 |
break; |
|
1079 |
default: |
|
1080 |
return illegal(); |
|
1081 |
} |
|
1082 |
if (typeArgs != null) illegal(); |
|
1083 |
while (true) { |
|
1084 |
int pos1 = S.pos(); |
|
1085 |
if (S.token() == LBRACKET) { |
|
1086 |
S.nextToken(); |
|
1087 |
if ((mode & TYPE) != 0) { |
|
1088 |
int oldmode = mode; |
|
1089 |
mode = TYPE; |
|
1090 |
if (S.token() == RBRACKET) { |
|
1091 |
S.nextToken(); |
|
7072 | 1092 |
t = bracketsOpt(t); |
10 | 1093 |
t = toP(F.at(pos1).TypeArray(t)); |
1094 |
return t; |
|
1095 |
} |
|
1096 |
mode = oldmode; |
|
1097 |
} |
|
1098 |
if ((mode & EXPR) != 0) { |
|
1099 |
mode = EXPR; |
|
1100 |
JCExpression t1 = term(); |
|
1101 |
t = to(F.at(pos1).Indexed(t, t1)); |
|
1102 |
} |
|
1103 |
accept(RBRACKET); |
|
1104 |
} else if (S.token() == DOT) { |
|
1105 |
S.nextToken(); |
|
1106 |
typeArgs = typeArgumentsOpt(EXPR); |
|
1107 |
if (S.token() == SUPER && (mode & EXPR) != 0) { |
|
1108 |
mode = EXPR; |
|
1109 |
t = to(F.at(pos1).Select(t, names._super)); |
|
1110 |
S.nextToken(); |
|
1111 |
t = arguments(typeArgs, t); |
|
1112 |
typeArgs = null; |
|
1113 |
} else if (S.token() == NEW && (mode & EXPR) != 0) { |
|
1114 |
if (typeArgs != null) return illegal(); |
|
1115 |
mode = EXPR; |
|
1116 |
int pos2 = S.pos(); |
|
1117 |
S.nextToken(); |
|
1118 |
if (S.token() == LT) typeArgs = typeArguments(); |
|
1119 |
t = innerCreator(pos2, typeArgs, t); |
|
1120 |
typeArgs = null; |
|
1121 |
} else { |
|
1122 |
t = toP(F.at(pos1).Select(t, ident())); |
|
1123 |
t = argumentsOpt(typeArgs, typeArgumentsOpt(t)); |
|
1124 |
typeArgs = null; |
|
1125 |
} |
|
1126 |
} else { |
|
1127 |
break; |
|
1128 |
} |
|
1129 |
} |
|
1130 |
while ((S.token() == PLUSPLUS || S.token() == SUBSUB) && (mode & EXPR) != 0) { |
|
1131 |
mode = EXPR; |
|
1132 |
t = to(F.at(S.pos()).Unary( |
|
1133 |
S.token() == PLUSPLUS ? JCTree.POSTINC : JCTree.POSTDEC, t)); |
|
1134 |
S.nextToken(); |
|
1135 |
} |
|
1136 |
return toP(t); |
|
1137 |
} |
|
1138 |
||
1139 |
/** SuperSuffix = Arguments | "." [TypeArguments] Ident [Arguments] |
|
1140 |
*/ |
|
1141 |
JCExpression superSuffix(List<JCExpression> typeArgs, JCExpression t) { |
|
1142 |
S.nextToken(); |
|
1143 |
if (S.token() == LPAREN || typeArgs != null) { |
|
1144 |
t = arguments(typeArgs, t); |
|
1145 |
} else { |
|
1146 |
int pos = S.pos(); |
|
1147 |
accept(DOT); |
|
1148 |
typeArgs = (S.token() == LT) ? typeArguments() : null; |
|
1149 |
t = toP(F.at(pos).Select(t, ident())); |
|
1150 |
t = argumentsOpt(typeArgs, t); |
|
1151 |
} |
|
1152 |
return t; |
|
1153 |
} |
|
1154 |
||
1155 |
/** BasicType = BYTE | SHORT | CHAR | INT | LONG | FLOAT | DOUBLE | BOOLEAN |
|
1156 |
*/ |
|
1157 |
JCPrimitiveTypeTree basicType() { |
|
1158 |
JCPrimitiveTypeTree t = to(F.at(S.pos()).TypeIdent(typetag(S.token()))); |
|
1159 |
S.nextToken(); |
|
1160 |
return t; |
|
1161 |
} |
|
1162 |
||
1163 |
/** ArgumentsOpt = [ Arguments ] |
|
1164 |
*/ |
|
1165 |
JCExpression argumentsOpt(List<JCExpression> typeArgs, JCExpression t) { |
|
1166 |
if ((mode & EXPR) != 0 && S.token() == LPAREN || typeArgs != null) { |
|
1167 |
mode = EXPR; |
|
1168 |
return arguments(typeArgs, t); |
|
1169 |
} else { |
|
1170 |
return t; |
|
1171 |
} |
|
1172 |
} |
|
1173 |
||
1174 |
/** Arguments = "(" [Expression { COMMA Expression }] ")" |
|
1175 |
*/ |
|
1176 |
List<JCExpression> arguments() { |
|
1177 |
ListBuffer<JCExpression> args = lb(); |
|
1178 |
if (S.token() == LPAREN) { |
|
1179 |
S.nextToken(); |
|
1180 |
if (S.token() != RPAREN) { |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1181 |
args.append(parseExpression()); |
10 | 1182 |
while (S.token() == COMMA) { |
1183 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1184 |
args.append(parseExpression()); |
10 | 1185 |
} |
1186 |
} |
|
1187 |
accept(RPAREN); |
|
1188 |
} else { |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
1189 |
syntaxError(S.pos(), "expected", LPAREN); |
10 | 1190 |
} |
1191 |
return args.toList(); |
|
1192 |
} |
|
1193 |
||
1194 |
JCMethodInvocation arguments(List<JCExpression> typeArgs, JCExpression t) { |
|
1195 |
int pos = S.pos(); |
|
1196 |
List<JCExpression> args = arguments(); |
|
1197 |
return toP(F.at(pos).Apply(typeArgs, t, args)); |
|
1198 |
} |
|
1199 |
||
1200 |
/** TypeArgumentsOpt = [ TypeArguments ] |
|
1201 |
*/ |
|
1202 |
JCExpression typeArgumentsOpt(JCExpression t) { |
|
1203 |
if (S.token() == LT && |
|
1204 |
(mode & TYPE) != 0 && |
|
1205 |
(mode & NOPARAMS) == 0) { |
|
1206 |
mode = TYPE; |
|
1207 |
checkGenerics(); |
|
1208 |
return typeArguments(t); |
|
1209 |
} else { |
|
1210 |
return t; |
|
1211 |
} |
|
1212 |
} |
|
1213 |
List<JCExpression> typeArgumentsOpt() { |
|
1214 |
return typeArgumentsOpt(TYPE); |
|
1215 |
} |
|
1216 |
||
1217 |
List<JCExpression> typeArgumentsOpt(int useMode) { |
|
1218 |
if (S.token() == LT) { |
|
1219 |
checkGenerics(); |
|
1220 |
if ((mode & useMode) == 0 || |
|
1221 |
(mode & NOPARAMS) != 0) { |
|
1222 |
illegal(); |
|
1223 |
} |
|
1224 |
mode = useMode; |
|
1225 |
return typeArguments(); |
|
1226 |
} |
|
1227 |
return null; |
|
1228 |
} |
|
1229 |
||
1230 |
/** TypeArguments = "<" TypeArgument {"," TypeArgument} ">" |
|
1231 |
*/ |
|
1232 |
List<JCExpression> typeArguments() { |
|
1233 |
ListBuffer<JCExpression> args = lb(); |
|
1234 |
if (S.token() == LT) { |
|
1235 |
S.nextToken(); |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1236 |
if (S.token() == GT && (mode & DIAMOND) != 0) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1237 |
checkDiamond(); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1238 |
S.nextToken(); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1239 |
return List.nil(); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1240 |
} |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1241 |
args.append(((mode & EXPR) == 0) ? typeArgument() : parseType()); |
10 | 1242 |
while (S.token() == COMMA) { |
1243 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1244 |
args.append(((mode & EXPR) == 0) ? typeArgument() : parseType()); |
10 | 1245 |
} |
1246 |
switch (S.token()) { |
|
1247 |
case GTGTGTEQ: |
|
1248 |
S.token(GTGTEQ); |
|
1249 |
break; |
|
1250 |
case GTGTEQ: |
|
1251 |
S.token(GTEQ); |
|
1252 |
break; |
|
1253 |
case GTEQ: |
|
1254 |
S.token(EQ); |
|
1255 |
break; |
|
1256 |
case GTGTGT: |
|
1257 |
S.token(GTGT); |
|
1258 |
break; |
|
1259 |
case GTGT: |
|
1260 |
S.token(GT); |
|
1261 |
break; |
|
1262 |
default: |
|
1263 |
accept(GT); |
|
1264 |
break; |
|
1265 |
} |
|
1266 |
} else { |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
1267 |
syntaxError(S.pos(), "expected", LT); |
10 | 1268 |
} |
1269 |
return args.toList(); |
|
1270 |
} |
|
1271 |
||
1272 |
/** TypeArgument = Type |
|
7072 | 1273 |
* | "?" |
1274 |
* | "?" EXTENDS Type {"&" Type} |
|
1275 |
* | "?" SUPER Type |
|
10 | 1276 |
*/ |
1277 |
JCExpression typeArgument() { |
|
7072 | 1278 |
if (S.token() != QUES) return parseType(); |
10 | 1279 |
int pos = S.pos(); |
1280 |
S.nextToken(); |
|
1281 |
if (S.token() == EXTENDS) { |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1282 |
TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.EXTENDS)); |
10 | 1283 |
S.nextToken(); |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1284 |
JCExpression bound = parseType(); |
7072 | 1285 |
return F.at(pos).Wildcard(t, bound); |
10 | 1286 |
} else if (S.token() == SUPER) { |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1287 |
TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.SUPER)); |
10 | 1288 |
S.nextToken(); |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1289 |
JCExpression bound = parseType(); |
7072 | 1290 |
return F.at(pos).Wildcard(t, bound); |
10 | 1291 |
} else if (S.token() == IDENTIFIER) { |
1292 |
//error recovery |
|
1293 |
reportSyntaxError(S.prevEndPos(), "expected3", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
1294 |
GT, EXTENDS, SUPER); |
10 | 1295 |
TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND); |
1296 |
JCExpression wc = toP(F.at(pos).Wildcard(t, null)); |
|
1297 |
JCIdent id = toP(F.at(S.pos()).Ident(ident())); |
|
7072 | 1298 |
return F.at(pos).Erroneous(List.<JCTree>of(wc, id)); |
10 | 1299 |
} else { |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1300 |
TypeBoundKind t = toP(F.at(pos).TypeBoundKind(BoundKind.UNBOUND)); |
7072 | 1301 |
return toP(F.at(pos).Wildcard(t, null)); |
10 | 1302 |
} |
1303 |
} |
|
1304 |
||
1305 |
JCTypeApply typeArguments(JCExpression t) { |
|
1306 |
int pos = S.pos(); |
|
1307 |
List<JCExpression> args = typeArguments(); |
|
1308 |
return toP(F.at(pos).TypeApply(t, args)); |
|
1309 |
} |
|
1310 |
||
7072 | 1311 |
/** BracketsOpt = {"[" "]"} |
10 | 1312 |
*/ |
7072 | 1313 |
private JCExpression bracketsOpt(JCExpression t) { |
10 | 1314 |
if (S.token() == LBRACKET) { |
1315 |
int pos = S.pos(); |
|
1316 |
S.nextToken(); |
|
7072 | 1317 |
t = bracketsOptCont(t, pos); |
1318 |
F.at(pos); |
|
10 | 1319 |
} |
1320 |
return t; |
|
1321 |
} |
|
1322 |
||
7072 | 1323 |
private JCArrayTypeTree bracketsOptCont(JCExpression t, int pos) { |
10 | 1324 |
accept(RBRACKET); |
7072 | 1325 |
t = bracketsOpt(t); |
10 | 1326 |
return toP(F.at(pos).TypeArray(t)); |
1327 |
} |
|
1328 |
||
1329 |
/** BracketsSuffixExpr = "." CLASS |
|
1330 |
* BracketsSuffixType = |
|
1331 |
*/ |
|
1332 |
JCExpression bracketsSuffix(JCExpression t) { |
|
1333 |
if ((mode & EXPR) != 0 && S.token() == DOT) { |
|
1334 |
mode = EXPR; |
|
1335 |
int pos = S.pos(); |
|
1336 |
S.nextToken(); |
|
1337 |
accept(CLASS); |
|
1338 |
if (S.pos() == errorEndPos) { |
|
1339 |
// error recovery |
|
1340 |
Name name = null; |
|
1341 |
if (S.token() == IDENTIFIER) { |
|
1342 |
name = S.name(); |
|
1343 |
S.nextToken(); |
|
1344 |
} else { |
|
1345 |
name = names.error; |
|
1346 |
} |
|
1347 |
t = F.at(pos).Erroneous(List.<JCTree>of(toP(F.at(pos).Select(t, name)))); |
|
1348 |
} else { |
|
1349 |
t = toP(F.at(pos).Select(t, names._class)); |
|
1350 |
} |
|
1351 |
} else if ((mode & TYPE) != 0) { |
|
1352 |
mode = TYPE; |
|
1353 |
} else { |
|
1354 |
syntaxError(S.pos(), "dot.class.expected"); |
|
1355 |
} |
|
1356 |
return t; |
|
1357 |
} |
|
1358 |
||
7072 | 1359 |
/** Creator = Qualident [TypeArguments] ( ArrayCreatorRest | ClassCreatorRest ) |
10 | 1360 |
*/ |
1361 |
JCExpression creator(int newpos, List<JCExpression> typeArgs) { |
|
1362 |
switch (S.token()) { |
|
1363 |
case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT: |
|
1364 |
case DOUBLE: case BOOLEAN: |
|
7072 | 1365 |
if (typeArgs == null) |
1366 |
return arrayCreatorRest(newpos, basicType()); |
|
10 | 1367 |
break; |
1368 |
default: |
|
1369 |
} |
|
1370 |
JCExpression t = qualident(); |
|
1371 |
int oldmode = mode; |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1372 |
mode = TYPE | DIAMOND; |
10 | 1373 |
if (S.token() == LT) { |
1374 |
checkGenerics(); |
|
1375 |
t = typeArguments(t); |
|
1376 |
} |
|
1377 |
while (S.token() == DOT) { |
|
1378 |
int pos = S.pos(); |
|
1379 |
S.nextToken(); |
|
1380 |
t = toP(F.at(pos).Select(t, ident())); |
|
1381 |
if (S.token() == LT) { |
|
1382 |
checkGenerics(); |
|
1383 |
t = typeArguments(t); |
|
1384 |
} |
|
1385 |
} |
|
1386 |
mode = oldmode; |
|
7072 | 1387 |
if (S.token() == LBRACKET) { |
10 | 1388 |
JCExpression e = arrayCreatorRest(newpos, t); |
1389 |
if (typeArgs != null) { |
|
1390 |
int pos = newpos; |
|
1391 |
if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) { |
|
1392 |
// note: this should always happen but we should |
|
1393 |
// not rely on this as the parser is continuously |
|
1394 |
// modified to improve error recovery. |
|
1395 |
pos = typeArgs.head.pos; |
|
1396 |
} |
|
1397 |
setErrorEndPos(S.prevEndPos()); |
|
1398 |
reportSyntaxError(pos, "cannot.create.array.with.type.arguments"); |
|
1399 |
return toP(F.at(newpos).Erroneous(typeArgs.prepend(e))); |
|
1400 |
} |
|
1401 |
return e; |
|
1402 |
} else if (S.token() == LPAREN) { |
|
7072 | 1403 |
return classCreatorRest(newpos, null, typeArgs, t); |
10 | 1404 |
} else { |
1405 |
reportSyntaxError(S.pos(), "expected2", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
1406 |
LPAREN, LBRACKET); |
10 | 1407 |
t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.<JCExpression>nil(), null)); |
1408 |
return toP(F.at(newpos).Erroneous(List.<JCTree>of(t))); |
|
1409 |
} |
|
1410 |
} |
|
1411 |
||
1412 |
/** InnerCreator = Ident [TypeArguments] ClassCreatorRest |
|
1413 |
*/ |
|
1414 |
JCExpression innerCreator(int newpos, List<JCExpression> typeArgs, JCExpression encl) { |
|
1415 |
JCExpression t = toP(F.at(S.pos()).Ident(ident())); |
|
1416 |
if (S.token() == LT) { |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1417 |
int oldmode = mode; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1418 |
mode |= DIAMOND; |
10 | 1419 |
checkGenerics(); |
1420 |
t = typeArguments(t); |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1421 |
mode = oldmode; |
10 | 1422 |
} |
1423 |
return classCreatorRest(newpos, encl, typeArgs, t); |
|
1424 |
} |
|
1425 |
||
7072 | 1426 |
/** ArrayCreatorRest = "[" ( "]" BracketsOpt ArrayInitializer |
1427 |
* | Expression "]" {"[" Expression "]"} BracketsOpt ) |
|
10 | 1428 |
*/ |
1429 |
JCExpression arrayCreatorRest(int newpos, JCExpression elemtype) { |
|
1430 |
accept(LBRACKET); |
|
1431 |
if (S.token() == RBRACKET) { |
|
1432 |
accept(RBRACKET); |
|
7072 | 1433 |
elemtype = bracketsOpt(elemtype); |
10 | 1434 |
if (S.token() == LBRACE) { |
7072 | 1435 |
return arrayInitializer(newpos, elemtype); |
10 | 1436 |
} else { |
1437 |
return syntaxError(S.pos(), "array.dimension.missing"); |
|
1438 |
} |
|
1439 |
} else { |
|
1440 |
ListBuffer<JCExpression> dims = new ListBuffer<JCExpression>(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1441 |
dims.append(parseExpression()); |
10 | 1442 |
accept(RBRACKET); |
7072 | 1443 |
while (S.token() == LBRACKET) { |
10 | 1444 |
int pos = S.pos(); |
1445 |
S.nextToken(); |
|
1446 |
if (S.token() == RBRACKET) { |
|
7072 | 1447 |
elemtype = bracketsOptCont(elemtype, pos); |
10 | 1448 |
} else { |
7072 | 1449 |
dims.append(parseExpression()); |
1450 |
accept(RBRACKET); |
|
10 | 1451 |
} |
1452 |
} |
|
7072 | 1453 |
return toP(F.at(newpos).NewArray(elemtype, dims.toList(), null)); |
10 | 1454 |
} |
1455 |
} |
|
1456 |
||
1457 |
/** ClassCreatorRest = Arguments [ClassBody] |
|
1458 |
*/ |
|
3149 | 1459 |
JCNewClass classCreatorRest(int newpos, |
10 | 1460 |
JCExpression encl, |
1461 |
List<JCExpression> typeArgs, |
|
1462 |
JCExpression t) |
|
1463 |
{ |
|
1464 |
List<JCExpression> args = arguments(); |
|
1465 |
JCClassDecl body = null; |
|
1466 |
if (S.token() == LBRACE) { |
|
1467 |
int pos = S.pos(); |
|
1468 |
List<JCTree> defs = classOrInterfaceBody(names.empty, false); |
|
1469 |
JCModifiers mods = F.at(Position.NOPOS).Modifiers(0); |
|
1470 |
body = toP(F.at(pos).AnonymousClassDef(mods, defs)); |
|
1471 |
} |
|
1472 |
return toP(F.at(newpos).NewClass(encl, typeArgs, t, args, body)); |
|
1473 |
} |
|
1474 |
||
1475 |
/** ArrayInitializer = "{" [VariableInitializer {"," VariableInitializer}] [","] "}" |
|
1476 |
*/ |
|
1477 |
JCExpression arrayInitializer(int newpos, JCExpression t) { |
|
1478 |
accept(LBRACE); |
|
1479 |
ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>(); |
|
1480 |
if (S.token() == COMMA) { |
|
1481 |
S.nextToken(); |
|
1482 |
} else if (S.token() != RBRACE) { |
|
1483 |
elems.append(variableInitializer()); |
|
1484 |
while (S.token() == COMMA) { |
|
1485 |
S.nextToken(); |
|
1486 |
if (S.token() == RBRACE) break; |
|
1487 |
elems.append(variableInitializer()); |
|
1488 |
} |
|
1489 |
} |
|
1490 |
accept(RBRACE); |
|
1491 |
return toP(F.at(newpos).NewArray(t, List.<JCExpression>nil(), elems.toList())); |
|
1492 |
} |
|
1493 |
||
1494 |
/** VariableInitializer = ArrayInitializer | Expression |
|
1495 |
*/ |
|
1496 |
public JCExpression variableInitializer() { |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1497 |
return S.token() == LBRACE ? arrayInitializer(S.pos(), null) : parseExpression(); |
10 | 1498 |
} |
1499 |
||
1500 |
/** ParExpression = "(" Expression ")" |
|
1501 |
*/ |
|
1502 |
JCExpression parExpression() { |
|
1503 |
accept(LPAREN); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1504 |
JCExpression t = parseExpression(); |
10 | 1505 |
accept(RPAREN); |
1506 |
return t; |
|
1507 |
} |
|
1508 |
||
1509 |
/** Block = "{" BlockStatements "}" |
|
1510 |
*/ |
|
1511 |
JCBlock block(int pos, long flags) { |
|
1512 |
accept(LBRACE); |
|
1513 |
List<JCStatement> stats = blockStatements(); |
|
1514 |
JCBlock t = F.at(pos).Block(flags, stats); |
|
1515 |
while (S.token() == CASE || S.token() == DEFAULT) { |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
1516 |
syntaxError("orphaned", S.token()); |
10 | 1517 |
switchBlockStatementGroups(); |
1518 |
} |
|
1519 |
// the Block node has a field "endpos" for first char of last token, which is |
|
1520 |
// usually but not necessarily the last char of the last token. |
|
1521 |
t.endpos = S.pos(); |
|
1522 |
accept(RBRACE); |
|
1523 |
return toP(t); |
|
1524 |
} |
|
1525 |
||
1526 |
public JCBlock block() { |
|
1527 |
return block(S.pos(), 0); |
|
1528 |
} |
|
1529 |
||
1530 |
/** BlockStatements = { BlockStatement } |
|
1531 |
* BlockStatement = LocalVariableDeclarationStatement |
|
1532 |
* | ClassOrInterfaceOrEnumDeclaration |
|
1533 |
* | [Ident ":"] Statement |
|
1534 |
* LocalVariableDeclarationStatement |
|
1535 |
* = { FINAL | '@' Annotation } Type VariableDeclarators ";" |
|
1536 |
*/ |
|
1537 |
@SuppressWarnings("fallthrough") |
|
1538 |
List<JCStatement> blockStatements() { |
|
1539 |
//todo: skip to anchor on error(?) |
|
1540 |
int lastErrPos = -1; |
|
1541 |
ListBuffer<JCStatement> stats = new ListBuffer<JCStatement>(); |
|
1542 |
while (true) { |
|
1543 |
int pos = S.pos(); |
|
1544 |
switch (S.token()) { |
|
1545 |
case RBRACE: case CASE: case DEFAULT: case EOF: |
|
1546 |
return stats.toList(); |
|
1547 |
case LBRACE: case IF: case FOR: case WHILE: case DO: case TRY: |
|
1548 |
case SWITCH: case SYNCHRONIZED: case RETURN: case THROW: case BREAK: |
|
1549 |
case CONTINUE: case SEMI: case ELSE: case FINALLY: case CATCH: |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1550 |
stats.append(parseStatement()); |
10 | 1551 |
break; |
1552 |
case MONKEYS_AT: |
|
1553 |
case FINAL: { |
|
1554 |
String dc = S.docComment(); |
|
1555 |
JCModifiers mods = modifiersOpt(); |
|
1556 |
if (S.token() == INTERFACE || |
|
1557 |
S.token() == CLASS || |
|
1558 |
allowEnums && S.token() == ENUM) { |
|
1559 |
stats.append(classOrInterfaceOrEnumDeclaration(mods, dc)); |
|
1560 |
} else { |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1561 |
JCExpression t = parseType(); |
10 | 1562 |
stats.appendList(variableDeclarators(mods, t, |
1563 |
new ListBuffer<JCStatement>())); |
|
1564 |
// A "LocalVariableDeclarationStatement" subsumes the terminating semicolon |
|
1565 |
storeEnd(stats.elems.last(), S.endPos()); |
|
1566 |
accept(SEMI); |
|
1567 |
} |
|
1568 |
break; |
|
1569 |
} |
|
1570 |
case ABSTRACT: case STRICTFP: { |
|
1571 |
String dc = S.docComment(); |
|
1572 |
JCModifiers mods = modifiersOpt(); |
|
1573 |
stats.append(classOrInterfaceOrEnumDeclaration(mods, dc)); |
|
1574 |
break; |
|
1575 |
} |
|
1576 |
case INTERFACE: |
|
1577 |
case CLASS: |
|
1578 |
stats.append(classOrInterfaceOrEnumDeclaration(modifiersOpt(), |
|
1579 |
S.docComment())); |
|
1580 |
break; |
|
1581 |
case ENUM: |
|
1582 |
case ASSERT: |
|
1583 |
if (allowEnums && S.token() == ENUM) { |
|
1584 |
log.error(S.pos(), "local.enum"); |
|
1585 |
stats. |
|
1586 |
append(classOrInterfaceOrEnumDeclaration(modifiersOpt(), |
|
1587 |
S.docComment())); |
|
1588 |
break; |
|
1589 |
} else if (allowAsserts && S.token() == ASSERT) { |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1590 |
stats.append(parseStatement()); |
10 | 1591 |
break; |
1592 |
} |
|
1593 |
/* fall through to default */ |
|
1594 |
default: |
|
1595 |
Name name = S.name(); |
|
1596 |
JCExpression t = term(EXPR | TYPE); |
|
1597 |
if (S.token() == COLON && t.getTag() == JCTree.IDENT) { |
|
1598 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1599 |
JCStatement stat = parseStatement(); |
10 | 1600 |
stats.append(F.at(pos).Labelled(name, stat)); |
1601 |
} else if ((lastmode & TYPE) != 0 && |
|
1602 |
(S.token() == IDENTIFIER || |
|
1603 |
S.token() == ASSERT || |
|
1604 |
S.token() == ENUM)) { |
|
1605 |
pos = S.pos(); |
|
1606 |
JCModifiers mods = F.at(Position.NOPOS).Modifiers(0); |
|
1607 |
F.at(pos); |
|
1608 |
stats.appendList(variableDeclarators(mods, t, |
|
1609 |
new ListBuffer<JCStatement>())); |
|
1610 |
// A "LocalVariableDeclarationStatement" subsumes the terminating semicolon |
|
1611 |
storeEnd(stats.elems.last(), S.endPos()); |
|
1612 |
accept(SEMI); |
|
1613 |
} else { |
|
1614 |
// This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon |
|
1615 |
stats.append(to(F.at(pos).Exec(checkExprStat(t)))); |
|
1616 |
accept(SEMI); |
|
1617 |
} |
|
1618 |
} |
|
1619 |
||
1620 |
// error recovery |
|
1621 |
if (S.pos() == lastErrPos) |
|
1622 |
return stats.toList(); |
|
1623 |
if (S.pos() <= errorEndPos) { |
|
1624 |
skip(false, true, true, true); |
|
1625 |
lastErrPos = S.pos(); |
|
1626 |
} |
|
1627 |
||
1628 |
// ensure no dangling /** @deprecated */ active |
|
1629 |
S.resetDeprecatedFlag(); |
|
1630 |
} |
|
1631 |
} |
|
1632 |
||
1633 |
/** Statement = |
|
1634 |
* Block |
|
1635 |
* | IF ParExpression Statement [ELSE Statement] |
|
1636 |
* | FOR "(" ForInitOpt ";" [Expression] ";" ForUpdateOpt ")" Statement |
|
1637 |
* | FOR "(" FormalParameter : Expression ")" Statement |
|
1638 |
* | WHILE ParExpression Statement |
|
1639 |
* | DO Statement WHILE ParExpression ";" |
|
1640 |
* | TRY Block ( Catches | [Catches] FinallyPart ) |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1641 |
* | TRY "(" ResourceSpecification ")" Block [Catches] [FinallyPart] |
10 | 1642 |
* | SWITCH ParExpression "{" SwitchBlockStatementGroups "}" |
1643 |
* | SYNCHRONIZED ParExpression Block |
|
1644 |
* | RETURN [Expression] ";" |
|
1645 |
* | THROW Expression ";" |
|
1646 |
* | BREAK [Ident] ";" |
|
1647 |
* | CONTINUE [Ident] ";" |
|
1648 |
* | ASSERT Expression [ ":" Expression ] ";" |
|
1649 |
* | ";" |
|
1650 |
* | ExpressionStatement |
|
1651 |
* | Ident ":" Statement |
|
1652 |
*/ |
|
1653 |
@SuppressWarnings("fallthrough") |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1654 |
public JCStatement parseStatement() { |
10 | 1655 |
int pos = S.pos(); |
1656 |
switch (S.token()) { |
|
1657 |
case LBRACE: |
|
1658 |
return block(); |
|
1659 |
case IF: { |
|
1660 |
S.nextToken(); |
|
1661 |
JCExpression cond = parExpression(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1662 |
JCStatement thenpart = parseStatement(); |
10 | 1663 |
JCStatement elsepart = null; |
1664 |
if (S.token() == ELSE) { |
|
1665 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1666 |
elsepart = parseStatement(); |
10 | 1667 |
} |
1668 |
return F.at(pos).If(cond, thenpart, elsepart); |
|
1669 |
} |
|
1670 |
case FOR: { |
|
1671 |
S.nextToken(); |
|
1672 |
accept(LPAREN); |
|
1673 |
List<JCStatement> inits = S.token() == SEMI ? List.<JCStatement>nil() : forInit(); |
|
1674 |
if (inits.length() == 1 && |
|
1675 |
inits.head.getTag() == JCTree.VARDEF && |
|
1676 |
((JCVariableDecl) inits.head).init == null && |
|
1677 |
S.token() == COLON) { |
|
1678 |
checkForeach(); |
|
1679 |
JCVariableDecl var = (JCVariableDecl)inits.head; |
|
1680 |
accept(COLON); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1681 |
JCExpression expr = parseExpression(); |
10 | 1682 |
accept(RPAREN); |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1683 |
JCStatement body = parseStatement(); |
10 | 1684 |
return F.at(pos).ForeachLoop(var, expr, body); |
1685 |
} else { |
|
1686 |
accept(SEMI); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1687 |
JCExpression cond = S.token() == SEMI ? null : parseExpression(); |
10 | 1688 |
accept(SEMI); |
1689 |
List<JCExpressionStatement> steps = S.token() == RPAREN ? List.<JCExpressionStatement>nil() : forUpdate(); |
|
1690 |
accept(RPAREN); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1691 |
JCStatement body = parseStatement(); |
10 | 1692 |
return F.at(pos).ForLoop(inits, cond, steps, body); |
1693 |
} |
|
1694 |
} |
|
1695 |
case WHILE: { |
|
1696 |
S.nextToken(); |
|
1697 |
JCExpression cond = parExpression(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1698 |
JCStatement body = parseStatement(); |
10 | 1699 |
return F.at(pos).WhileLoop(cond, body); |
1700 |
} |
|
1701 |
case DO: { |
|
1702 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1703 |
JCStatement body = parseStatement(); |
10 | 1704 |
accept(WHILE); |
1705 |
JCExpression cond = parExpression(); |
|
1706 |
JCDoWhileLoop t = to(F.at(pos).DoLoop(body, cond)); |
|
1707 |
accept(SEMI); |
|
1708 |
return t; |
|
1709 |
} |
|
1710 |
case TRY: { |
|
1711 |
S.nextToken(); |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1712 |
List<JCTree> resources = List.<JCTree>nil(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1713 |
if (S.token() == LPAREN) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1714 |
checkAutomaticResourceManagement(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1715 |
S.nextToken(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1716 |
resources = resources(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1717 |
accept(RPAREN); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1718 |
} |
10 | 1719 |
JCBlock body = block(); |
1720 |
ListBuffer<JCCatch> catchers = new ListBuffer<JCCatch>(); |
|
1721 |
JCBlock finalizer = null; |
|
1722 |
if (S.token() == CATCH || S.token() == FINALLY) { |
|
1723 |
while (S.token() == CATCH) catchers.append(catchClause()); |
|
1724 |
if (S.token() == FINALLY) { |
|
1725 |
S.nextToken(); |
|
1726 |
finalizer = block(); |
|
1727 |
} |
|
1728 |
} else { |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1729 |
if (allowTWR) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1730 |
if (resources.isEmpty()) |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1731 |
log.error(pos, "try.without.catch.finally.or.resource.decls"); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1732 |
} else |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1733 |
log.error(pos, "try.without.catch.or.finally"); |
10 | 1734 |
} |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
1735 |
return F.at(pos).Try(resources, body, catchers.toList(), finalizer); |
10 | 1736 |
} |
1737 |
case SWITCH: { |
|
1738 |
S.nextToken(); |
|
1739 |
JCExpression selector = parExpression(); |
|
1740 |
accept(LBRACE); |
|
1741 |
List<JCCase> cases = switchBlockStatementGroups(); |
|
1742 |
JCSwitch t = to(F.at(pos).Switch(selector, cases)); |
|
1743 |
accept(RBRACE); |
|
1744 |
return t; |
|
1745 |
} |
|
1746 |
case SYNCHRONIZED: { |
|
1747 |
S.nextToken(); |
|
1748 |
JCExpression lock = parExpression(); |
|
1749 |
JCBlock body = block(); |
|
1750 |
return F.at(pos).Synchronized(lock, body); |
|
1751 |
} |
|
1752 |
case RETURN: { |
|
1753 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1754 |
JCExpression result = S.token() == SEMI ? null : parseExpression(); |
10 | 1755 |
JCReturn t = to(F.at(pos).Return(result)); |
1756 |
accept(SEMI); |
|
1757 |
return t; |
|
1758 |
} |
|
1759 |
case THROW: { |
|
1760 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1761 |
JCExpression exc = parseExpression(); |
10 | 1762 |
JCThrow t = to(F.at(pos).Throw(exc)); |
1763 |
accept(SEMI); |
|
1764 |
return t; |
|
1765 |
} |
|
1766 |
case BREAK: { |
|
1767 |
S.nextToken(); |
|
1768 |
Name label = (S.token() == IDENTIFIER || S.token() == ASSERT || S.token() == ENUM) ? ident() : null; |
|
1769 |
JCBreak t = to(F.at(pos).Break(label)); |
|
1770 |
accept(SEMI); |
|
1771 |
return t; |
|
1772 |
} |
|
1773 |
case CONTINUE: { |
|
1774 |
S.nextToken(); |
|
1775 |
Name label = (S.token() == IDENTIFIER || S.token() == ASSERT || S.token() == ENUM) ? ident() : null; |
|
1776 |
JCContinue t = to(F.at(pos).Continue(label)); |
|
1777 |
accept(SEMI); |
|
1778 |
return t; |
|
1779 |
} |
|
1780 |
case SEMI: |
|
1781 |
S.nextToken(); |
|
1782 |
return toP(F.at(pos).Skip()); |
|
1783 |
case ELSE: |
|
1784 |
return toP(F.Exec(syntaxError("else.without.if"))); |
|
1785 |
case FINALLY: |
|
1786 |
return toP(F.Exec(syntaxError("finally.without.try"))); |
|
1787 |
case CATCH: |
|
1788 |
return toP(F.Exec(syntaxError("catch.without.try"))); |
|
1789 |
case ASSERT: { |
|
1790 |
if (allowAsserts && S.token() == ASSERT) { |
|
1791 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1792 |
JCExpression assertion = parseExpression(); |
10 | 1793 |
JCExpression message = null; |
1794 |
if (S.token() == COLON) { |
|
1795 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1796 |
message = parseExpression(); |
10 | 1797 |
} |
1798 |
JCAssert t = to(F.at(pos).Assert(assertion, message)); |
|
1799 |
accept(SEMI); |
|
1800 |
return t; |
|
1801 |
} |
|
1802 |
/* else fall through to default case */ |
|
1803 |
} |
|
1804 |
case ENUM: |
|
1805 |
default: |
|
1806 |
Name name = S.name(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1807 |
JCExpression expr = parseExpression(); |
10 | 1808 |
if (S.token() == COLON && expr.getTag() == JCTree.IDENT) { |
1809 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1810 |
JCStatement stat = parseStatement(); |
10 | 1811 |
return F.at(pos).Labelled(name, stat); |
1812 |
} else { |
|
1813 |
// This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon |
|
1814 |
JCExpressionStatement stat = to(F.at(pos).Exec(checkExprStat(expr))); |
|
1815 |
accept(SEMI); |
|
1816 |
return stat; |
|
1817 |
} |
|
1818 |
} |
|
1819 |
} |
|
1820 |
||
1821 |
/** CatchClause = CATCH "(" FormalParameter ")" Block |
|
1822 |
*/ |
|
1823 |
JCCatch catchClause() { |
|
1824 |
int pos = S.pos(); |
|
1825 |
accept(CATCH); |
|
1826 |
accept(LPAREN); |
|
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1827 |
JCModifiers mods = optFinal(Flags.PARAMETER); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1828 |
List<JCExpression> catchTypes = catchTypes(); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1829 |
JCExpression paramType = catchTypes.size() > 1 ? |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1830 |
toP(F.at(catchTypes.head.getStartPosition()).TypeDisjoint(catchTypes)) : |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1831 |
catchTypes.head; |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1832 |
JCVariableDecl formal = variableDeclaratorId(mods, paramType); |
10 | 1833 |
accept(RPAREN); |
1834 |
JCBlock body = block(); |
|
1835 |
return F.at(pos).Catch(formal, body); |
|
1836 |
} |
|
1837 |
||
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1838 |
List<JCExpression> catchTypes() { |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1839 |
ListBuffer<JCExpression> catchTypes = ListBuffer.lb(); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1840 |
catchTypes.add(parseType()); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1841 |
while (S.token() == BAR) { |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1842 |
checkMulticatch(); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1843 |
S.nextToken(); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1844 |
catchTypes.add(qualident()); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1845 |
} |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1846 |
return catchTypes.toList(); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1847 |
} |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
1848 |
|
10 | 1849 |
/** SwitchBlockStatementGroups = { SwitchBlockStatementGroup } |
1850 |
* SwitchBlockStatementGroup = SwitchLabel BlockStatements |
|
1851 |
* SwitchLabel = CASE ConstantExpression ":" | DEFAULT ":" |
|
1852 |
*/ |
|
1853 |
List<JCCase> switchBlockStatementGroups() { |
|
1854 |
ListBuffer<JCCase> cases = new ListBuffer<JCCase>(); |
|
1855 |
while (true) { |
|
1856 |
int pos = S.pos(); |
|
1857 |
switch (S.token()) { |
|
1858 |
case CASE: { |
|
1859 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1860 |
JCExpression pat = parseExpression(); |
10 | 1861 |
accept(COLON); |
1862 |
List<JCStatement> stats = blockStatements(); |
|
1863 |
JCCase c = F.at(pos).Case(pat, stats); |
|
1864 |
if (stats.isEmpty()) |
|
1865 |
storeEnd(c, S.prevEndPos()); |
|
1866 |
cases.append(c); |
|
1867 |
break; |
|
1868 |
} |
|
1869 |
case DEFAULT: { |
|
1870 |
S.nextToken(); |
|
1871 |
accept(COLON); |
|
1872 |
List<JCStatement> stats = blockStatements(); |
|
1873 |
JCCase c = F.at(pos).Case(null, stats); |
|
1874 |
if (stats.isEmpty()) |
|
1875 |
storeEnd(c, S.prevEndPos()); |
|
1876 |
cases.append(c); |
|
1877 |
break; |
|
1878 |
} |
|
1879 |
case RBRACE: case EOF: |
|
1880 |
return cases.toList(); |
|
1881 |
default: |
|
1882 |
S.nextToken(); // to ensure progress |
|
1883 |
syntaxError(pos, "expected3", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
1884 |
CASE, DEFAULT, RBRACE); |
10 | 1885 |
} |
1886 |
} |
|
1887 |
} |
|
1888 |
||
1889 |
/** MoreStatementExpressions = { COMMA StatementExpression } |
|
1890 |
*/ |
|
1891 |
<T extends ListBuffer<? super JCExpressionStatement>> T moreStatementExpressions(int pos, |
|
1892 |
JCExpression first, |
|
1893 |
T stats) { |
|
1894 |
// This Exec is a "StatementExpression"; it subsumes no terminating token |
|
1895 |
stats.append(toP(F.at(pos).Exec(checkExprStat(first)))); |
|
1896 |
while (S.token() == COMMA) { |
|
1897 |
S.nextToken(); |
|
1898 |
pos = S.pos(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1899 |
JCExpression t = parseExpression(); |
10 | 1900 |
// This Exec is a "StatementExpression"; it subsumes no terminating token |
1901 |
stats.append(toP(F.at(pos).Exec(checkExprStat(t)))); |
|
1902 |
} |
|
1903 |
return stats; |
|
1904 |
} |
|
1905 |
||
1906 |
/** ForInit = StatementExpression MoreStatementExpressions |
|
1907 |
* | { FINAL | '@' Annotation } Type VariableDeclarators |
|
1908 |
*/ |
|
1909 |
List<JCStatement> forInit() { |
|
1910 |
ListBuffer<JCStatement> stats = lb(); |
|
1911 |
int pos = S.pos(); |
|
1912 |
if (S.token() == FINAL || S.token() == MONKEYS_AT) { |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1913 |
return variableDeclarators(optFinal(0), parseType(), stats).toList(); |
10 | 1914 |
} else { |
1915 |
JCExpression t = term(EXPR | TYPE); |
|
1916 |
if ((lastmode & TYPE) != 0 && |
|
1917 |
(S.token() == IDENTIFIER || S.token() == ASSERT || S.token() == ENUM)) |
|
1918 |
return variableDeclarators(modifiersOpt(), t, stats).toList(); |
|
1919 |
else |
|
1920 |
return moreStatementExpressions(pos, t, stats).toList(); |
|
1921 |
} |
|
1922 |
} |
|
1923 |
||
1924 |
/** ForUpdate = StatementExpression MoreStatementExpressions |
|
1925 |
*/ |
|
1926 |
List<JCExpressionStatement> forUpdate() { |
|
1927 |
return moreStatementExpressions(S.pos(), |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1928 |
parseExpression(), |
10 | 1929 |
new ListBuffer<JCExpressionStatement>()).toList(); |
1930 |
} |
|
1931 |
||
1932 |
/** AnnotationsOpt = { '@' Annotation } |
|
1933 |
*/ |
|
7072 | 1934 |
List<JCAnnotation> annotationsOpt() { |
10 | 1935 |
if (S.token() != MONKEYS_AT) return List.nil(); // optimization |
1936 |
ListBuffer<JCAnnotation> buf = new ListBuffer<JCAnnotation>(); |
|
1937 |
while (S.token() == MONKEYS_AT) { |
|
1938 |
int pos = S.pos(); |
|
1939 |
S.nextToken(); |
|
7072 | 1940 |
buf.append(annotation(pos)); |
10 | 1941 |
} |
7072 | 1942 |
return buf.toList(); |
10 | 1943 |
} |
1944 |
||
1945 |
/** ModifiersOpt = { Modifier } |
|
1946 |
* Modifier = PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL |
|
1947 |
* | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE | "@" |
|
1948 |
* | "@" Annotation |
|
1949 |
*/ |
|
1950 |
JCModifiers modifiersOpt() { |
|
1951 |
return modifiersOpt(null); |
|
1952 |
} |
|
1953 |
JCModifiers modifiersOpt(JCModifiers partial) { |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1954 |
long flags; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1955 |
ListBuffer<JCAnnotation> annotations = new ListBuffer<JCAnnotation>(); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1956 |
int pos; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1957 |
if (partial == null) { |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1958 |
flags = 0; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1959 |
pos = S.pos(); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1960 |
} else { |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1961 |
flags = partial.flags; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1962 |
annotations.appendList(partial.annotations); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1963 |
pos = partial.pos; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1964 |
} |
10 | 1965 |
if (S.deprecatedFlag()) { |
1966 |
flags |= Flags.DEPRECATED; |
|
1967 |
S.resetDeprecatedFlag(); |
|
1968 |
} |
|
1969 |
int lastPos = Position.NOPOS; |
|
1970 |
loop: |
|
1971 |
while (true) { |
|
1972 |
long flag; |
|
1973 |
switch (S.token()) { |
|
1974 |
case PRIVATE : flag = Flags.PRIVATE; break; |
|
1975 |
case PROTECTED : flag = Flags.PROTECTED; break; |
|
1976 |
case PUBLIC : flag = Flags.PUBLIC; break; |
|
1977 |
case STATIC : flag = Flags.STATIC; break; |
|
1978 |
case TRANSIENT : flag = Flags.TRANSIENT; break; |
|
1979 |
case FINAL : flag = Flags.FINAL; break; |
|
1980 |
case ABSTRACT : flag = Flags.ABSTRACT; break; |
|
1981 |
case NATIVE : flag = Flags.NATIVE; break; |
|
1982 |
case VOLATILE : flag = Flags.VOLATILE; break; |
|
1983 |
case SYNCHRONIZED: flag = Flags.SYNCHRONIZED; break; |
|
1984 |
case STRICTFP : flag = Flags.STRICTFP; break; |
|
1985 |
case MONKEYS_AT : flag = Flags.ANNOTATION; break; |
|
1986 |
default: break loop; |
|
1987 |
} |
|
1988 |
if ((flags & flag) != 0) log.error(S.pos(), "repeated.modifier"); |
|
1989 |
lastPos = S.pos(); |
|
1990 |
S.nextToken(); |
|
1991 |
if (flag == Flags.ANNOTATION) { |
|
1992 |
checkAnnotations(); |
|
1993 |
if (S.token() != INTERFACE) { |
|
7072 | 1994 |
JCAnnotation ann = annotation(lastPos); |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1995 |
// if first modifier is an annotation, set pos to annotation's. |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1996 |
if (flags == 0 && annotations.isEmpty()) |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1997 |
pos = ann.pos; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1998 |
annotations.append(ann); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1999 |
lastPos = ann.pos; |
10 | 2000 |
flag = 0; |
2001 |
} |
|
2002 |
} |
|
2003 |
flags |= flag; |
|
2004 |
} |
|
2005 |
switch (S.token()) { |
|
2006 |
case ENUM: flags |= Flags.ENUM; break; |
|
2007 |
case INTERFACE: flags |= Flags.INTERFACE; break; |
|
2008 |
default: break; |
|
2009 |
} |
|
2010 |
||
2011 |
/* A modifiers tree with no modifier tokens or annotations |
|
2012 |
* has no text position. */ |
|
6152
111b884a19a7
6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
6148
diff
changeset
|
2013 |
if ((flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0 && annotations.isEmpty()) |
10 | 2014 |
pos = Position.NOPOS; |
2015 |
||
2016 |
JCModifiers mods = F.at(pos).Modifiers(flags, annotations.toList()); |
|
2017 |
if (pos != Position.NOPOS) |
|
2018 |
storeEnd(mods, S.prevEndPos()); |
|
2019 |
return mods; |
|
2020 |
} |
|
2021 |
||
2022 |
/** Annotation = "@" Qualident [ "(" AnnotationFieldValues ")" ] |
|
2023 |
* @param pos position of "@" token |
|
2024 |
*/ |
|
7072 | 2025 |
JCAnnotation annotation(int pos) { |
10 | 2026 |
// accept(AT); // AT consumed by caller |
2027 |
checkAnnotations(); |
|
2028 |
JCTree ident = qualident(); |
|
2029 |
List<JCExpression> fieldValues = annotationFieldValuesOpt(); |
|
7072 | 2030 |
JCAnnotation ann = F.at(pos).Annotation(ident, fieldValues); |
10 | 2031 |
storeEnd(ann, S.prevEndPos()); |
2032 |
return ann; |
|
2033 |
} |
|
2034 |
||
2035 |
List<JCExpression> annotationFieldValuesOpt() { |
|
2036 |
return (S.token() == LPAREN) ? annotationFieldValues() : List.<JCExpression>nil(); |
|
2037 |
} |
|
2038 |
||
2039 |
/** AnnotationFieldValues = "(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */ |
|
2040 |
List<JCExpression> annotationFieldValues() { |
|
2041 |
accept(LPAREN); |
|
2042 |
ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>(); |
|
2043 |
if (S.token() != RPAREN) { |
|
2044 |
buf.append(annotationFieldValue()); |
|
2045 |
while (S.token() == COMMA) { |
|
2046 |
S.nextToken(); |
|
2047 |
buf.append(annotationFieldValue()); |
|
2048 |
} |
|
2049 |
} |
|
2050 |
accept(RPAREN); |
|
2051 |
return buf.toList(); |
|
2052 |
} |
|
2053 |
||
2054 |
/** AnnotationFieldValue = AnnotationValue |
|
2055 |
* | Identifier "=" AnnotationValue |
|
2056 |
*/ |
|
2057 |
JCExpression annotationFieldValue() { |
|
2058 |
if (S.token() == IDENTIFIER) { |
|
2059 |
mode = EXPR; |
|
2060 |
JCExpression t1 = term1(); |
|
2061 |
if (t1.getTag() == JCTree.IDENT && S.token() == EQ) { |
|
2062 |
int pos = S.pos(); |
|
2063 |
accept(EQ); |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2064 |
JCExpression v = annotationValue(); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2065 |
return toP(F.at(pos).Assign(t1, v)); |
10 | 2066 |
} else { |
2067 |
return t1; |
|
2068 |
} |
|
2069 |
} |
|
2070 |
return annotationValue(); |
|
2071 |
} |
|
2072 |
||
2073 |
/* AnnotationValue = ConditionalExpression |
|
2074 |
* | Annotation |
|
3997
35250de1d4ea
6337964: should ignore last comma in annotation array
darcy
parents:
3894
diff
changeset
|
2075 |
* | "{" [ AnnotationValue { "," AnnotationValue } ] [","] "}" |
10 | 2076 |
*/ |
2077 |
JCExpression annotationValue() { |
|
2078 |
int pos; |
|
2079 |
switch (S.token()) { |
|
2080 |
case MONKEYS_AT: |
|
2081 |
pos = S.pos(); |
|
2082 |
S.nextToken(); |
|
7072 | 2083 |
return annotation(pos); |
10 | 2084 |
case LBRACE: |
2085 |
pos = S.pos(); |
|
2086 |
accept(LBRACE); |
|
2087 |
ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>(); |
|
2088 |
if (S.token() != RBRACE) { |
|
2089 |
buf.append(annotationValue()); |
|
2090 |
while (S.token() == COMMA) { |
|
2091 |
S.nextToken(); |
|
3997
35250de1d4ea
6337964: should ignore last comma in annotation array
darcy
parents:
3894
diff
changeset
|
2092 |
if (S.token() == RBRACE) break; |
10 | 2093 |
buf.append(annotationValue()); |
2094 |
} |
|
2095 |
} |
|
2096 |
accept(RBRACE); |
|
2097 |
return toP(F.at(pos).NewArray(null, List.<JCExpression>nil(), buf.toList())); |
|
2098 |
default: |
|
2099 |
mode = EXPR; |
|
2100 |
return term1(); |
|
2101 |
} |
|
2102 |
} |
|
2103 |
||
2104 |
/** VariableDeclarators = VariableDeclarator { "," VariableDeclarator } |
|
2105 |
*/ |
|
2106 |
public <T extends ListBuffer<? super JCVariableDecl>> T variableDeclarators(JCModifiers mods, |
|
2107 |
JCExpression type, |
|
2108 |
T vdefs) |
|
2109 |
{ |
|
2110 |
return variableDeclaratorsRest(S.pos(), mods, type, ident(), false, null, vdefs); |
|
2111 |
} |
|
2112 |
||
2113 |
/** VariableDeclaratorsRest = VariableDeclaratorRest { "," VariableDeclarator } |
|
2114 |
* ConstantDeclaratorsRest = ConstantDeclaratorRest { "," ConstantDeclarator } |
|
2115 |
* |
|
2116 |
* @param reqInit Is an initializer always required? |
|
2117 |
* @param dc The documentation comment for the variable declarations, or null. |
|
2118 |
*/ |
|
2119 |
<T extends ListBuffer<? super JCVariableDecl>> T variableDeclaratorsRest(int pos, |
|
2120 |
JCModifiers mods, |
|
2121 |
JCExpression type, |
|
2122 |
Name name, |
|
2123 |
boolean reqInit, |
|
2124 |
String dc, |
|
2125 |
T vdefs) |
|
2126 |
{ |
|
2127 |
vdefs.append(variableDeclaratorRest(pos, mods, type, name, reqInit, dc)); |
|
2128 |
while (S.token() == COMMA) { |
|
2129 |
// All but last of multiple declarators subsume a comma |
|
2130 |
storeEnd((JCTree)vdefs.elems.last(), S.endPos()); |
|
2131 |
S.nextToken(); |
|
2132 |
vdefs.append(variableDeclarator(mods, type, reqInit, dc)); |
|
2133 |
} |
|
2134 |
return vdefs; |
|
2135 |
} |
|
2136 |
||
2137 |
/** VariableDeclarator = Ident VariableDeclaratorRest |
|
2138 |
* ConstantDeclarator = Ident ConstantDeclaratorRest |
|
2139 |
*/ |
|
2140 |
JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, String dc) { |
|
2141 |
return variableDeclaratorRest(S.pos(), mods, type, ident(), reqInit, dc); |
|
2142 |
} |
|
2143 |
||
2144 |
/** VariableDeclaratorRest = BracketsOpt ["=" VariableInitializer] |
|
2145 |
* ConstantDeclaratorRest = BracketsOpt "=" VariableInitializer |
|
2146 |
* |
|
2147 |
* @param reqInit Is an initializer always required? |
|
2148 |
* @param dc The documentation comment for the variable declarations, or null. |
|
2149 |
*/ |
|
2150 |
JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name, |
|
2151 |
boolean reqInit, String dc) { |
|
2152 |
type = bracketsOpt(type); |
|
2153 |
JCExpression init = null; |
|
2154 |
if (S.token() == EQ) { |
|
2155 |
S.nextToken(); |
|
2156 |
init = variableInitializer(); |
|
2157 |
} |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
2158 |
else if (reqInit) syntaxError(S.pos(), "expected", EQ); |
10 | 2159 |
JCVariableDecl result = |
2160 |
toP(F.at(pos).VarDef(mods, name, type, init)); |
|
2161 |
attach(result, dc); |
|
2162 |
return result; |
|
2163 |
} |
|
2164 |
||
2165 |
/** VariableDeclaratorId = Ident BracketsOpt |
|
2166 |
*/ |
|
2167 |
JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type) { |
|
2168 |
int pos = S.pos(); |
|
2169 |
Name name = ident(); |
|
2170 |
if ((mods.flags & Flags.VARARGS) == 0) |
|
2171 |
type = bracketsOpt(type); |
|
2172 |
return toP(F.at(pos).VarDef(mods, name, type, null)); |
|
2173 |
} |
|
2174 |
||
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2175 |
/** Resources = Resource { ";" Resources } |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2176 |
*/ |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2177 |
List<JCTree> resources() { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2178 |
ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2179 |
defs.append(resource()); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2180 |
while (S.token() == SEMI) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2181 |
// All but last of multiple declarators subsume a semicolon |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2182 |
storeEnd(defs.elems.last(), S.endPos()); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2183 |
S.nextToken(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2184 |
defs.append(resource()); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2185 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2186 |
return defs.toList(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2187 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2188 |
|
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2189 |
/** Resource = |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2190 |
* VariableModifiers Type VariableDeclaratorId = Expression |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2191 |
* | Expression |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2192 |
*/ |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2193 |
JCTree resource() { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2194 |
int pos = S.pos(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2195 |
if (S.token() == FINAL || S.token() == MONKEYS_AT) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2196 |
return variableDeclaratorRest(pos, optFinal(0), parseType(), |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2197 |
ident(), true, null); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2198 |
} else { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2199 |
JCExpression t = term(EXPR | TYPE); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2200 |
if ((lastmode & TYPE) != 0 && S.token() == IDENTIFIER) |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2201 |
return variableDeclaratorRest(pos, toP(F.at(pos).Modifiers(Flags.FINAL)), t, |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2202 |
ident(), true, null); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2203 |
else |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2204 |
return t; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2205 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2206 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2207 |
|
10 | 2208 |
/** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration} |
2209 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2210 |
public JCTree.JCCompilationUnit parseCompilationUnit() { |
10 | 2211 |
int pos = S.pos(); |
2212 |
JCExpression pid = null; |
|
2213 |
String dc = S.docComment(); |
|
2214 |
JCModifiers mods = null; |
|
2215 |
List<JCAnnotation> packageAnnotations = List.nil(); |
|
2216 |
if (S.token() == MONKEYS_AT) |
|
2217 |
mods = modifiersOpt(); |
|
2218 |
||
2219 |
if (S.token() == PACKAGE) { |
|
2220 |
if (mods != null) { |
|
2221 |
checkNoMods(mods.flags); |
|
2222 |
packageAnnotations = mods.annotations; |
|
2223 |
mods = null; |
|
2224 |
} |
|
2225 |
S.nextToken(); |
|
2226 |
pid = qualident(); |
|
2227 |
accept(SEMI); |
|
2228 |
} |
|
2229 |
ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2230 |
boolean checkForImports = true; |
10 | 2231 |
while (S.token() != EOF) { |
2232 |
if (S.pos() <= errorEndPos) { |
|
2233 |
// error recovery |
|
2234 |
skip(checkForImports, false, false, false); |
|
2235 |
if (S.token() == EOF) |
|
2236 |
break; |
|
2237 |
} |
|
2238 |
if (checkForImports && mods == null && S.token() == IMPORT) { |
|
2239 |
defs.append(importDeclaration()); |
|
2240 |
} else { |
|
2241 |
JCTree def = typeDeclaration(mods); |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
6585
diff
changeset
|
2242 |
if (keepDocComments && dc != null && docComments.get(def) == dc) { |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
6585
diff
changeset
|
2243 |
// If the first type declaration has consumed the first doc |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
6585
diff
changeset
|
2244 |
// comment, then don't use it for the top level comment as well. |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
6585
diff
changeset
|
2245 |
dc = null; |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
6585
diff
changeset
|
2246 |
} |
10 | 2247 |
if (def instanceof JCExpressionStatement) |
2248 |
def = ((JCExpressionStatement)def).expr; |
|
2249 |
defs.append(def); |
|
2250 |
if (def instanceof JCClassDecl) |
|
2251 |
checkForImports = false; |
|
2252 |
mods = null; |
|
2253 |
} |
|
2254 |
} |
|
2255 |
JCTree.JCCompilationUnit toplevel = F.at(pos).TopLevel(packageAnnotations, pid, defs.toList()); |
|
2256 |
attach(toplevel, dc); |
|
2257 |
if (defs.elems.isEmpty()) |
|
2258 |
storeEnd(toplevel, S.prevEndPos()); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2259 |
if (keepDocComments) |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2260 |
toplevel.docComments = docComments; |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2261 |
if (keepLineMap) |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2262 |
toplevel.lineMap = S.getLineMap(); |
10 | 2263 |
return toplevel; |
2264 |
} |
|
2265 |
||
2266 |
/** ImportDeclaration = IMPORT [ STATIC ] Ident { "." Ident } [ "." "*" ] ";" |
|
2267 |
*/ |
|
2268 |
JCTree importDeclaration() { |
|
2269 |
int pos = S.pos(); |
|
2270 |
S.nextToken(); |
|
2271 |
boolean importStatic = false; |
|
2272 |
if (S.token() == STATIC) { |
|
2273 |
checkStaticImports(); |
|
2274 |
importStatic = true; |
|
2275 |
S.nextToken(); |
|
2276 |
} |
|
2277 |
JCExpression pid = toP(F.at(S.pos()).Ident(ident())); |
|
2278 |
do { |
|
2279 |
int pos1 = S.pos(); |
|
2280 |
accept(DOT); |
|
2281 |
if (S.token() == STAR) { |
|
2282 |
pid = to(F.at(pos1).Select(pid, names.asterisk)); |
|
2283 |
S.nextToken(); |
|
2284 |
break; |
|
2285 |
} else { |
|
2286 |
pid = toP(F.at(pos1).Select(pid, ident())); |
|
2287 |
} |
|
2288 |
} while (S.token() == DOT); |
|
2289 |
accept(SEMI); |
|
2290 |
return toP(F.at(pos).Import(pid, importStatic)); |
|
2291 |
} |
|
2292 |
||
2293 |
/** TypeDeclaration = ClassOrInterfaceOrEnumDeclaration |
|
2294 |
* | ";" |
|
2295 |
*/ |
|
2296 |
JCTree typeDeclaration(JCModifiers mods) { |
|
2297 |
int pos = S.pos(); |
|
2298 |
if (mods == null && S.token() == SEMI) { |
|
2299 |
S.nextToken(); |
|
2300 |
return toP(F.at(pos).Skip()); |
|
2301 |
} else { |
|
2302 |
String dc = S.docComment(); |
|
2303 |
return classOrInterfaceOrEnumDeclaration(modifiersOpt(mods), dc); |
|
2304 |
} |
|
2305 |
} |
|
2306 |
||
2307 |
/** ClassOrInterfaceOrEnumDeclaration = ModifiersOpt |
|
2308 |
* (ClassDeclaration | InterfaceDeclaration | EnumDeclaration) |
|
2309 |
* @param mods Any modifiers starting the class or interface declaration |
|
2310 |
* @param dc The documentation comment for the class, or null. |
|
2311 |
*/ |
|
2312 |
JCStatement classOrInterfaceOrEnumDeclaration(JCModifiers mods, String dc) { |
|
2313 |
if (S.token() == CLASS) { |
|
2314 |
return classDeclaration(mods, dc); |
|
2315 |
} else if (S.token() == INTERFACE) { |
|
2316 |
return interfaceDeclaration(mods, dc); |
|
2317 |
} else if (allowEnums) { |
|
2318 |
if (S.token() == ENUM) { |
|
2319 |
return enumDeclaration(mods, dc); |
|
2320 |
} else { |
|
2321 |
int pos = S.pos(); |
|
2322 |
List<JCTree> errs; |
|
2323 |
if (S.token() == IDENTIFIER) { |
|
2324 |
errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident()))); |
|
2325 |
setErrorEndPos(S.pos()); |
|
2326 |
} else { |
|
2327 |
errs = List.<JCTree>of(mods); |
|
2328 |
} |
|
2329 |
return toP(F.Exec(syntaxError(pos, errs, "expected3", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
2330 |
CLASS, INTERFACE, ENUM))); |
10 | 2331 |
} |
2332 |
} else { |
|
2333 |
if (S.token() == ENUM) { |
|
2334 |
log.error(S.pos(), "enums.not.supported.in.source", source.name); |
|
2335 |
allowEnums = true; |
|
2336 |
return enumDeclaration(mods, dc); |
|
2337 |
} |
|
2338 |
int pos = S.pos(); |
|
2339 |
List<JCTree> errs; |
|
2340 |
if (S.token() == IDENTIFIER) { |
|
2341 |
errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident()))); |
|
2342 |
setErrorEndPos(S.pos()); |
|
2343 |
} else { |
|
2344 |
errs = List.<JCTree>of(mods); |
|
2345 |
} |
|
2346 |
return toP(F.Exec(syntaxError(pos, errs, "expected2", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
2347 |
CLASS, INTERFACE))); |
10 | 2348 |
} |
2349 |
} |
|
2350 |
||
2351 |
/** ClassDeclaration = CLASS Ident TypeParametersOpt [EXTENDS Type] |
|
2352 |
* [IMPLEMENTS TypeList] ClassBody |
|
2353 |
* @param mods The modifiers starting the class declaration |
|
2354 |
* @param dc The documentation comment for the class, or null. |
|
2355 |
*/ |
|
2356 |
JCClassDecl classDeclaration(JCModifiers mods, String dc) { |
|
2357 |
int pos = S.pos(); |
|
2358 |
accept(CLASS); |
|
2359 |
Name name = ident(); |
|
2360 |
||
2361 |
List<JCTypeParameter> typarams = typeParametersOpt(); |
|
2362 |
||
2363 |
JCTree extending = null; |
|
2364 |
if (S.token() == EXTENDS) { |
|
2365 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2366 |
extending = parseType(); |
10 | 2367 |
} |
2368 |
List<JCExpression> implementing = List.nil(); |
|
2369 |
if (S.token() == IMPLEMENTS) { |
|
2370 |
S.nextToken(); |
|
2371 |
implementing = typeList(); |
|
2372 |
} |
|
2373 |
List<JCTree> defs = classOrInterfaceBody(name, false); |
|
2374 |
JCClassDecl result = toP(F.at(pos).ClassDef( |
|
2375 |
mods, name, typarams, extending, implementing, defs)); |
|
2376 |
attach(result, dc); |
|
2377 |
return result; |
|
2378 |
} |
|
2379 |
||
2380 |
/** InterfaceDeclaration = INTERFACE Ident TypeParametersOpt |
|
2381 |
* [EXTENDS TypeList] InterfaceBody |
|
2382 |
* @param mods The modifiers starting the interface declaration |
|
2383 |
* @param dc The documentation comment for the interface, or null. |
|
2384 |
*/ |
|
2385 |
JCClassDecl interfaceDeclaration(JCModifiers mods, String dc) { |
|
2386 |
int pos = S.pos(); |
|
2387 |
accept(INTERFACE); |
|
2388 |
Name name = ident(); |
|
2389 |
||
2390 |
List<JCTypeParameter> typarams = typeParametersOpt(); |
|
2391 |
||
2392 |
List<JCExpression> extending = List.nil(); |
|
2393 |
if (S.token() == EXTENDS) { |
|
2394 |
S.nextToken(); |
|
2395 |
extending = typeList(); |
|
2396 |
} |
|
2397 |
List<JCTree> defs = classOrInterfaceBody(name, true); |
|
2398 |
JCClassDecl result = toP(F.at(pos).ClassDef( |
|
2399 |
mods, name, typarams, null, extending, defs)); |
|
2400 |
attach(result, dc); |
|
2401 |
return result; |
|
2402 |
} |
|
2403 |
||
2404 |
/** EnumDeclaration = ENUM Ident [IMPLEMENTS TypeList] EnumBody |
|
2405 |
* @param mods The modifiers starting the enum declaration |
|
2406 |
* @param dc The documentation comment for the enum, or null. |
|
2407 |
*/ |
|
2408 |
JCClassDecl enumDeclaration(JCModifiers mods, String dc) { |
|
2409 |
int pos = S.pos(); |
|
2410 |
accept(ENUM); |
|
2411 |
Name name = ident(); |
|
2412 |
||
2413 |
List<JCExpression> implementing = List.nil(); |
|
2414 |
if (S.token() == IMPLEMENTS) { |
|
2415 |
S.nextToken(); |
|
2416 |
implementing = typeList(); |
|
2417 |
} |
|
2418 |
||
2419 |
List<JCTree> defs = enumBody(name); |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2420 |
mods.flags |= Flags.ENUM; |
10 | 2421 |
JCClassDecl result = toP(F.at(pos). |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2422 |
ClassDef(mods, name, List.<JCTypeParameter>nil(), |
10 | 2423 |
null, implementing, defs)); |
2424 |
attach(result, dc); |
|
2425 |
return result; |
|
2426 |
} |
|
2427 |
||
2428 |
/** EnumBody = "{" { EnumeratorDeclarationList } [","] |
|
2429 |
* [ ";" {ClassBodyDeclaration} ] "}" |
|
2430 |
*/ |
|
2431 |
List<JCTree> enumBody(Name enumName) { |
|
2432 |
accept(LBRACE); |
|
2433 |
ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); |
|
2434 |
if (S.token() == COMMA) { |
|
2435 |
S.nextToken(); |
|
2436 |
} else if (S.token() != RBRACE && S.token() != SEMI) { |
|
2437 |
defs.append(enumeratorDeclaration(enumName)); |
|
2438 |
while (S.token() == COMMA) { |
|
2439 |
S.nextToken(); |
|
2440 |
if (S.token() == RBRACE || S.token() == SEMI) break; |
|
2441 |
defs.append(enumeratorDeclaration(enumName)); |
|
2442 |
} |
|
2443 |
if (S.token() != SEMI && S.token() != RBRACE) { |
|
2444 |
defs.append(syntaxError(S.pos(), "expected3", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
2445 |
COMMA, RBRACE, SEMI)); |
10 | 2446 |
S.nextToken(); |
2447 |
} |
|
2448 |
} |
|
2449 |
if (S.token() == SEMI) { |
|
2450 |
S.nextToken(); |
|
2451 |
while (S.token() != RBRACE && S.token() != EOF) { |
|
2452 |
defs.appendList(classOrInterfaceBodyDeclaration(enumName, |
|
2453 |
false)); |
|
2454 |
if (S.pos() <= errorEndPos) { |
|
2455 |
// error recovery |
|
2456 |
skip(false, true, true, false); |
|
2457 |
} |
|
2458 |
} |
|
2459 |
} |
|
2460 |
accept(RBRACE); |
|
2461 |
return defs.toList(); |
|
2462 |
} |
|
2463 |
||
2464 |
/** EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ] |
|
2465 |
*/ |
|
2466 |
JCTree enumeratorDeclaration(Name enumName) { |
|
2467 |
String dc = S.docComment(); |
|
2468 |
int flags = Flags.PUBLIC|Flags.STATIC|Flags.FINAL|Flags.ENUM; |
|
2469 |
if (S.deprecatedFlag()) { |
|
2470 |
flags |= Flags.DEPRECATED; |
|
2471 |
S.resetDeprecatedFlag(); |
|
2472 |
} |
|
2473 |
int pos = S.pos(); |
|
7072 | 2474 |
List<JCAnnotation> annotations = annotationsOpt(); |
10 | 2475 |
JCModifiers mods = F.at(annotations.isEmpty() ? Position.NOPOS : pos).Modifiers(flags, annotations); |
2476 |
List<JCExpression> typeArgs = typeArgumentsOpt(); |
|
2477 |
int identPos = S.pos(); |
|
2478 |
Name name = ident(); |
|
2479 |
int createPos = S.pos(); |
|
2480 |
List<JCExpression> args = (S.token() == LPAREN) |
|
2481 |
? arguments() : List.<JCExpression>nil(); |
|
2482 |
JCClassDecl body = null; |
|
2483 |
if (S.token() == LBRACE) { |
|
2484 |
JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM | Flags.STATIC); |
|
2485 |
List<JCTree> defs = classOrInterfaceBody(names.empty, false); |
|
2486 |
body = toP(F.at(identPos).AnonymousClassDef(mods1, defs)); |
|
2487 |
} |
|
2488 |
if (args.isEmpty() && body == null) |
|
4704
5206047418c2
6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents:
3997
diff
changeset
|
2489 |
createPos = identPos; |
5206047418c2
6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents:
3997
diff
changeset
|
2490 |
JCIdent ident = F.at(identPos).Ident(enumName); |
10 | 2491 |
JCNewClass create = F.at(createPos).NewClass(null, typeArgs, ident, args, body); |
4704
5206047418c2
6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents:
3997
diff
changeset
|
2492 |
if (createPos != identPos) |
10 | 2493 |
storeEnd(create, S.prevEndPos()); |
4704
5206047418c2
6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents:
3997
diff
changeset
|
2494 |
ident = F.at(identPos).Ident(enumName); |
10 | 2495 |
JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create)); |
2496 |
attach(result, dc); |
|
2497 |
return result; |
|
2498 |
} |
|
2499 |
||
2500 |
/** TypeList = Type {"," Type} |
|
2501 |
*/ |
|
2502 |
List<JCExpression> typeList() { |
|
2503 |
ListBuffer<JCExpression> ts = new ListBuffer<JCExpression>(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2504 |
ts.append(parseType()); |
10 | 2505 |
while (S.token() == COMMA) { |
2506 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2507 |
ts.append(parseType()); |
10 | 2508 |
} |
2509 |
return ts.toList(); |
|
2510 |
} |
|
2511 |
||
2512 |
/** ClassBody = "{" {ClassBodyDeclaration} "}" |
|
2513 |
* InterfaceBody = "{" {InterfaceBodyDeclaration} "}" |
|
2514 |
*/ |
|
2515 |
List<JCTree> classOrInterfaceBody(Name className, boolean isInterface) { |
|
2516 |
accept(LBRACE); |
|
2517 |
if (S.pos() <= errorEndPos) { |
|
2518 |
// error recovery |
|
2519 |
skip(false, true, false, false); |
|
2520 |
if (S.token() == LBRACE) |
|
2521 |
S.nextToken(); |
|
2522 |
} |
|
2523 |
ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); |
|
2524 |
while (S.token() != RBRACE && S.token() != EOF) { |
|
2525 |
defs.appendList(classOrInterfaceBodyDeclaration(className, isInterface)); |
|
2526 |
if (S.pos() <= errorEndPos) { |
|
2527 |
// error recovery |
|
2528 |
skip(false, true, true, false); |
|
2529 |
} |
|
2530 |
} |
|
2531 |
accept(RBRACE); |
|
2532 |
return defs.toList(); |
|
2533 |
} |
|
2534 |
||
2535 |
/** ClassBodyDeclaration = |
|
2536 |
* ";" |
|
2537 |
* | [STATIC] Block |
|
2538 |
* | ModifiersOpt |
|
2539 |
* ( Type Ident |
|
2540 |
* ( VariableDeclaratorsRest ";" | MethodDeclaratorRest ) |
|
2541 |
* | VOID Ident MethodDeclaratorRest |
|
2542 |
* | TypeParameters (Type | VOID) Ident MethodDeclaratorRest |
|
2543 |
* | Ident ConstructorDeclaratorRest |
|
2544 |
* | TypeParameters Ident ConstructorDeclaratorRest |
|
2545 |
* | ClassOrInterfaceOrEnumDeclaration |
|
2546 |
* ) |
|
2547 |
* InterfaceBodyDeclaration = |
|
2548 |
* ";" |
|
2549 |
* | ModifiersOpt Type Ident |
|
2550 |
* ( ConstantDeclaratorsRest | InterfaceMethodDeclaratorRest ";" ) |
|
2551 |
*/ |
|
2552 |
List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) { |
|
2553 |
if (S.token() == SEMI) { |
|
2554 |
S.nextToken(); |
|
6585
189f53daed52
6921495: spurious semicolons in class def cause empty NOPOS blocks
jjg
parents:
6152
diff
changeset
|
2555 |
return List.<JCTree>nil(); |
10 | 2556 |
} else { |
2557 |
String dc = S.docComment(); |
|
2558 |
int pos = S.pos(); |
|
2559 |
JCModifiers mods = modifiersOpt(); |
|
2560 |
if (S.token() == CLASS || |
|
2561 |
S.token() == INTERFACE || |
|
2562 |
allowEnums && S.token() == ENUM) { |
|
2563 |
return List.<JCTree>of(classOrInterfaceOrEnumDeclaration(mods, dc)); |
|
2564 |
} else if (S.token() == LBRACE && !isInterface && |
|
2565 |
(mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 && |
|
2566 |
mods.annotations.isEmpty()) { |
|
2567 |
return List.<JCTree>of(block(pos, mods.flags)); |
|
2568 |
} else { |
|
2569 |
pos = S.pos(); |
|
2570 |
List<JCTypeParameter> typarams = typeParametersOpt(); |
|
2571 |
Name name = S.name(); |
|
2572 |
pos = S.pos(); |
|
2573 |
JCExpression type; |
|
2574 |
boolean isVoid = S.token() == VOID; |
|
2575 |
if (isVoid) { |
|
2576 |
type = to(F.at(pos).TypeIdent(TypeTags.VOID)); |
|
2577 |
S.nextToken(); |
|
2578 |
} else { |
|
7072 | 2579 |
type = parseType(); |
10 | 2580 |
} |
2581 |
if (S.token() == LPAREN && !isInterface && type.getTag() == JCTree.IDENT) { |
|
2582 |
if (isInterface || name != className) |
|
2583 |
log.error(pos, "invalid.meth.decl.ret.type.req"); |
|
2584 |
return List.of(methodDeclaratorRest( |
|
2585 |
pos, mods, null, names.init, typarams, |
|
2586 |
isInterface, true, dc)); |
|
2587 |
} else { |
|
2588 |
pos = S.pos(); |
|
2589 |
name = ident(); |
|
2590 |
if (S.token() == LPAREN) { |
|
2591 |
return List.of(methodDeclaratorRest( |
|
2592 |
pos, mods, type, name, typarams, |
|
2593 |
isInterface, isVoid, dc)); |
|
2594 |
} else if (!isVoid && typarams.isEmpty()) { |
|
2595 |
List<JCTree> defs = |
|
2596 |
variableDeclaratorsRest(pos, mods, type, name, isInterface, dc, |
|
2597 |
new ListBuffer<JCTree>()).toList(); |
|
2598 |
storeEnd(defs.last(), S.endPos()); |
|
2599 |
accept(SEMI); |
|
2600 |
return defs; |
|
2601 |
} else { |
|
2602 |
pos = S.pos(); |
|
2603 |
List<JCTree> err = isVoid |
|
2604 |
? List.<JCTree>of(toP(F.at(pos).MethodDef(mods, name, type, typarams, |
|
2605 |
List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null))) |
|
2606 |
: null; |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
2607 |
return List.<JCTree>of(syntaxError(S.pos(), err, "expected", LPAREN)); |
10 | 2608 |
} |
2609 |
} |
|
2610 |
} |
|
2611 |
} |
|
2612 |
} |
|
2613 |
||
2614 |
/** MethodDeclaratorRest = |
|
7072 | 2615 |
* FormalParameters BracketsOpt [Throws TypeList] ( MethodBody | [DEFAULT AnnotationValue] ";") |
10 | 2616 |
* VoidMethodDeclaratorRest = |
7072 | 2617 |
* FormalParameters [Throws TypeList] ( MethodBody | ";") |
10 | 2618 |
* InterfaceMethodDeclaratorRest = |
7072 | 2619 |
* FormalParameters BracketsOpt [THROWS TypeList] ";" |
10 | 2620 |
* VoidInterfaceMethodDeclaratorRest = |
7072 | 2621 |
* FormalParameters [THROWS TypeList] ";" |
10 | 2622 |
* ConstructorDeclaratorRest = |
7072 | 2623 |
* "(" FormalParameterListOpt ")" [THROWS TypeList] MethodBody |
10 | 2624 |
*/ |
2625 |
JCTree methodDeclaratorRest(int pos, |
|
2626 |
JCModifiers mods, |
|
2627 |
JCExpression type, |
|
2628 |
Name name, |
|
2629 |
List<JCTypeParameter> typarams, |
|
2630 |
boolean isInterface, boolean isVoid, |
|
2631 |
String dc) { |
|
2632 |
List<JCVariableDecl> params = formalParameters(); |
|
7072 | 2633 |
if (!isVoid) type = bracketsOpt(type); |
10 | 2634 |
List<JCExpression> thrown = List.nil(); |
2635 |
if (S.token() == THROWS) { |
|
2636 |
S.nextToken(); |
|
2637 |
thrown = qualidentList(); |
|
2638 |
} |
|
2639 |
JCBlock body = null; |
|
2640 |
JCExpression defaultValue; |
|
2641 |
if (S.token() == LBRACE) { |
|
2642 |
body = block(); |
|
2643 |
defaultValue = null; |
|
2644 |
} else { |
|
2645 |
if (S.token() == DEFAULT) { |
|
2646 |
accept(DEFAULT); |
|
2647 |
defaultValue = annotationValue(); |
|
2648 |
} else { |
|
2649 |
defaultValue = null; |
|
2650 |
} |
|
2651 |
accept(SEMI); |
|
2652 |
if (S.pos() <= errorEndPos) { |
|
2653 |
// error recovery |
|
2654 |
skip(false, true, false, false); |
|
2655 |
if (S.token() == LBRACE) { |
|
2656 |
body = block(); |
|
2657 |
} |
|
2658 |
} |
|
2659 |
} |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2660 |
|
10 | 2661 |
JCMethodDecl result = |
2662 |
toP(F.at(pos).MethodDef(mods, name, type, typarams, |
|
7072 | 2663 |
params, thrown, |
10 | 2664 |
body, defaultValue)); |
2665 |
attach(result, dc); |
|
2666 |
return result; |
|
2667 |
} |
|
2668 |
||
7072 | 2669 |
/** QualidentList = Qualident {"," Qualident} |
10 | 2670 |
*/ |
2671 |
List<JCExpression> qualidentList() { |
|
2672 |
ListBuffer<JCExpression> ts = new ListBuffer<JCExpression>(); |
|
7072 | 2673 |
ts.append(qualident()); |
10 | 2674 |
while (S.token() == COMMA) { |
2675 |
S.nextToken(); |
|
7072 | 2676 |
ts.append(qualident()); |
10 | 2677 |
} |
2678 |
return ts.toList(); |
|
2679 |
} |
|
2680 |
||
2681 |
/** TypeParametersOpt = ["<" TypeParameter {"," TypeParameter} ">"] |
|
2682 |
*/ |
|
2683 |
List<JCTypeParameter> typeParametersOpt() { |
|
2684 |
if (S.token() == LT) { |
|
2685 |
checkGenerics(); |
|
2686 |
ListBuffer<JCTypeParameter> typarams = new ListBuffer<JCTypeParameter>(); |
|
2687 |
S.nextToken(); |
|
2688 |
typarams.append(typeParameter()); |
|
2689 |
while (S.token() == COMMA) { |
|
2690 |
S.nextToken(); |
|
2691 |
typarams.append(typeParameter()); |
|
2692 |
} |
|
2693 |
accept(GT); |
|
2694 |
return typarams.toList(); |
|
2695 |
} else { |
|
2696 |
return List.nil(); |
|
2697 |
} |
|
2698 |
} |
|
2699 |
||
7072 | 2700 |
/** TypeParameter = TypeVariable [TypeParameterBound] |
10 | 2701 |
* TypeParameterBound = EXTENDS Type {"&" Type} |
2702 |
* TypeVariable = Ident |
|
2703 |
*/ |
|
2704 |
JCTypeParameter typeParameter() { |
|
2705 |
int pos = S.pos(); |
|
2706 |
Name name = ident(); |
|
2707 |
ListBuffer<JCExpression> bounds = new ListBuffer<JCExpression>(); |
|
2708 |
if (S.token() == EXTENDS) { |
|
2709 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2710 |
bounds.append(parseType()); |
10 | 2711 |
while (S.token() == AMP) { |
2712 |
S.nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2713 |
bounds.append(parseType()); |
10 | 2714 |
} |
2715 |
} |
|
7072 | 2716 |
return toP(F.at(pos).TypeParameter(name, bounds.toList())); |
10 | 2717 |
} |
2718 |
||
2719 |
/** FormalParameters = "(" [ FormalParameterList ] ")" |
|
2720 |
* FormalParameterList = [ FormalParameterListNovarargs , ] LastFormalParameter |
|
2721 |
* FormalParameterListNovarargs = [ FormalParameterListNovarargs , ] FormalParameter |
|
2722 |
*/ |
|
2723 |
List<JCVariableDecl> formalParameters() { |
|
2724 |
ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>(); |
|
2725 |
JCVariableDecl lastParam = null; |
|
2726 |
accept(LPAREN); |
|
2727 |
if (S.token() != RPAREN) { |
|
2728 |
params.append(lastParam = formalParameter()); |
|
2729 |
while ((lastParam.mods.flags & Flags.VARARGS) == 0 && S.token() == COMMA) { |
|
2730 |
S.nextToken(); |
|
2731 |
params.append(lastParam = formalParameter()); |
|
2732 |
} |
|
2733 |
} |
|
2734 |
accept(RPAREN); |
|
2735 |
return params.toList(); |
|
2736 |
} |
|
2737 |
||
2738 |
JCModifiers optFinal(long flags) { |
|
2739 |
JCModifiers mods = modifiersOpt(); |
|
2740 |
checkNoMods(mods.flags & ~(Flags.FINAL | Flags.DEPRECATED)); |
|
2741 |
mods.flags |= flags; |
|
2742 |
return mods; |
|
2743 |
} |
|
2744 |
||
2745 |
/** FormalParameter = { FINAL | '@' Annotation } Type VariableDeclaratorId |
|
2746 |
* LastFormalParameter = { FINAL | '@' Annotation } Type '...' Ident | FormalParameter |
|
2747 |
*/ |
|
2748 |
JCVariableDecl formalParameter() { |
|
2749 |
JCModifiers mods = optFinal(Flags.PARAMETER); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2750 |
JCExpression type = parseType(); |
10 | 2751 |
if (S.token() == ELLIPSIS) { |
2752 |
checkVarargs(); |
|
2753 |
mods.flags |= Flags.VARARGS; |
|
2754 |
type = to(F.at(S.pos()).TypeArray(type)); |
|
2755 |
S.nextToken(); |
|
2756 |
} |
|
2757 |
return variableDeclaratorId(mods, type); |
|
2758 |
} |
|
2759 |
||
2760 |
/* ---------- auxiliary methods -------------- */ |
|
2761 |
||
2762 |
/** Check that given tree is a legal expression statement. |
|
2763 |
*/ |
|
2764 |
protected JCExpression checkExprStat(JCExpression t) { |
|
2765 |
switch(t.getTag()) { |
|
2766 |
case JCTree.PREINC: case JCTree.PREDEC: |
|
2767 |
case JCTree.POSTINC: case JCTree.POSTDEC: |
|
2768 |
case JCTree.ASSIGN: |
|
2769 |
case JCTree.BITOR_ASG: case JCTree.BITXOR_ASG: case JCTree.BITAND_ASG: |
|
2770 |
case JCTree.SL_ASG: case JCTree.SR_ASG: case JCTree.USR_ASG: |
|
2771 |
case JCTree.PLUS_ASG: case JCTree.MINUS_ASG: |
|
2772 |
case JCTree.MUL_ASG: case JCTree.DIV_ASG: case JCTree.MOD_ASG: |
|
2773 |
case JCTree.APPLY: case JCTree.NEWCLASS: |
|
2774 |
case JCTree.ERRONEOUS: |
|
2775 |
return t; |
|
2776 |
default: |
|
2777 |
log.error(t.pos, "not.stmt"); |
|
2778 |
return F.at(t.pos).Erroneous(List.<JCTree>of(t)); |
|
2779 |
} |
|
2780 |
} |
|
2781 |
||
2782 |
/** Return precedence of operator represented by token, |
|
2783 |
* -1 if token is not a binary operator. @see TreeInfo.opPrec |
|
2784 |
*/ |
|
2785 |
static int prec(Token token) { |
|
2786 |
int oc = optag(token); |
|
2787 |
return (oc >= 0) ? TreeInfo.opPrec(oc) : -1; |
|
2788 |
} |
|
2789 |
||
5013
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2790 |
/** |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2791 |
* Return the lesser of two positions, making allowance for either one |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2792 |
* being unset. |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2793 |
*/ |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2794 |
static int earlier(int pos1, int pos2) { |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2795 |
if (pos1 == Position.NOPOS) |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2796 |
return pos2; |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2797 |
if (pos2 == Position.NOPOS) |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2798 |
return pos1; |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2799 |
return (pos1 < pos2 ? pos1 : pos2); |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2800 |
} |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
2801 |
|
10 | 2802 |
/** Return operation tag of binary operator represented by token, |
2803 |
* -1 if token is not a binary operator. |
|
2804 |
*/ |
|
2805 |
static int optag(Token token) { |
|
2806 |
switch (token) { |
|
2807 |
case BARBAR: |
|
2808 |
return JCTree.OR; |
|
2809 |
case AMPAMP: |
|
2810 |
return JCTree.AND; |
|
2811 |
case BAR: |
|
2812 |
return JCTree.BITOR; |
|
2813 |
case BAREQ: |
|
2814 |
return JCTree.BITOR_ASG; |
|
2815 |
case CARET: |
|
2816 |
return JCTree.BITXOR; |
|
2817 |
case CARETEQ: |
|
2818 |
return JCTree.BITXOR_ASG; |
|
2819 |
case AMP: |
|
2820 |
return JCTree.BITAND; |
|
2821 |
case AMPEQ: |
|
2822 |
return JCTree.BITAND_ASG; |
|
2823 |
case EQEQ: |
|
2824 |
return JCTree.EQ; |
|
2825 |
case BANGEQ: |
|
2826 |
return JCTree.NE; |
|
2827 |
case LT: |
|
2828 |
return JCTree.LT; |
|
2829 |
case GT: |
|
2830 |
return JCTree.GT; |
|
2831 |
case LTEQ: |
|
2832 |
return JCTree.LE; |
|
2833 |
case GTEQ: |
|
2834 |
return JCTree.GE; |
|
2835 |
case LTLT: |
|
2836 |
return JCTree.SL; |
|
2837 |
case LTLTEQ: |
|
2838 |
return JCTree.SL_ASG; |
|
2839 |
case GTGT: |
|
2840 |
return JCTree.SR; |
|
2841 |
case GTGTEQ: |
|
2842 |
return JCTree.SR_ASG; |
|
2843 |
case GTGTGT: |
|
2844 |
return JCTree.USR; |
|
2845 |
case GTGTGTEQ: |
|
2846 |
return JCTree.USR_ASG; |
|
2847 |
case PLUS: |
|
2848 |
return JCTree.PLUS; |
|
2849 |
case PLUSEQ: |
|
2850 |
return JCTree.PLUS_ASG; |
|
2851 |
case SUB: |
|
2852 |
return JCTree.MINUS; |
|
2853 |
case SUBEQ: |
|
2854 |
return JCTree.MINUS_ASG; |
|
2855 |
case STAR: |
|
2856 |
return JCTree.MUL; |
|
2857 |
case STAREQ: |
|
2858 |
return JCTree.MUL_ASG; |
|
2859 |
case SLASH: |
|
2860 |
return JCTree.DIV; |
|
2861 |
case SLASHEQ: |
|
2862 |
return JCTree.DIV_ASG; |
|
2863 |
case PERCENT: |
|
2864 |
return JCTree.MOD; |
|
2865 |
case PERCENTEQ: |
|
2866 |
return JCTree.MOD_ASG; |
|
2867 |
case INSTANCEOF: |
|
2868 |
return JCTree.TYPETEST; |
|
2869 |
default: |
|
2870 |
return -1; |
|
2871 |
} |
|
2872 |
} |
|
2873 |
||
2874 |
/** Return operation tag of unary operator represented by token, |
|
2875 |
* -1 if token is not a binary operator. |
|
2876 |
*/ |
|
2877 |
static int unoptag(Token token) { |
|
2878 |
switch (token) { |
|
2879 |
case PLUS: |
|
2880 |
return JCTree.POS; |
|
2881 |
case SUB: |
|
2882 |
return JCTree.NEG; |
|
2883 |
case BANG: |
|
2884 |
return JCTree.NOT; |
|
2885 |
case TILDE: |
|
2886 |
return JCTree.COMPL; |
|
2887 |
case PLUSPLUS: |
|
2888 |
return JCTree.PREINC; |
|
2889 |
case SUBSUB: |
|
2890 |
return JCTree.PREDEC; |
|
2891 |
default: |
|
2892 |
return -1; |
|
2893 |
} |
|
2894 |
} |
|
2895 |
||
2896 |
/** Return type tag of basic type represented by token, |
|
2897 |
* -1 if token is not a basic type identifier. |
|
2898 |
*/ |
|
2899 |
static int typetag(Token token) { |
|
2900 |
switch (token) { |
|
2901 |
case BYTE: |
|
2902 |
return TypeTags.BYTE; |
|
2903 |
case CHAR: |
|
2904 |
return TypeTags.CHAR; |
|
2905 |
case SHORT: |
|
2906 |
return TypeTags.SHORT; |
|
2907 |
case INT: |
|
2908 |
return TypeTags.INT; |
|
2909 |
case LONG: |
|
2910 |
return TypeTags.LONG; |
|
2911 |
case FLOAT: |
|
2912 |
return TypeTags.FLOAT; |
|
2913 |
case DOUBLE: |
|
2914 |
return TypeTags.DOUBLE; |
|
2915 |
case BOOLEAN: |
|
2916 |
return TypeTags.BOOLEAN; |
|
2917 |
default: |
|
2918 |
return -1; |
|
2919 |
} |
|
2920 |
} |
|
2921 |
||
2922 |
void checkGenerics() { |
|
2923 |
if (!allowGenerics) { |
|
2924 |
log.error(S.pos(), "generics.not.supported.in.source", source.name); |
|
2925 |
allowGenerics = true; |
|
2926 |
} |
|
2927 |
} |
|
2928 |
void checkVarargs() { |
|
2929 |
if (!allowVarargs) { |
|
2930 |
log.error(S.pos(), "varargs.not.supported.in.source", source.name); |
|
2931 |
allowVarargs = true; |
|
2932 |
} |
|
2933 |
} |
|
2934 |
void checkForeach() { |
|
2935 |
if (!allowForeach) { |
|
2936 |
log.error(S.pos(), "foreach.not.supported.in.source", source.name); |
|
2937 |
allowForeach = true; |
|
2938 |
} |
|
2939 |
} |
|
2940 |
void checkStaticImports() { |
|
2941 |
if (!allowStaticImport) { |
|
2942 |
log.error(S.pos(), "static.import.not.supported.in.source", source.name); |
|
2943 |
allowStaticImport = true; |
|
2944 |
} |
|
2945 |
} |
|
2946 |
void checkAnnotations() { |
|
2947 |
if (!allowAnnotations) { |
|
2948 |
log.error(S.pos(), "annotations.not.supported.in.source", source.name); |
|
2949 |
allowAnnotations = true; |
|
2950 |
} |
|
2951 |
} |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
2952 |
void checkDiamond() { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
2953 |
if (!allowDiamond) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
2954 |
log.error(S.pos(), "diamond.not.supported.in.source", source.name); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
2955 |
allowDiamond = true; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
2956 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
2957 |
} |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2958 |
void checkMulticatch() { |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2959 |
if (!allowMulticatch) { |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2960 |
log.error(S.pos(), "multicatch.not.supported.in.source", source.name); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2961 |
allowMulticatch = true; |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2962 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2963 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2964 |
void checkAutomaticResourceManagement() { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2965 |
if (!allowTWR) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2966 |
log.error(S.pos(), "automatic.resource.management.not.supported.in.source", source.name); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2967 |
allowTWR = true; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2968 |
} |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2969 |
} |
10 | 2970 |
} |