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