author | jjg |
Thu, 27 Feb 2014 13:57:57 -0800 | |
changeset 23131 | 90aee246c017 |
parent 22701 | 67811024e8b4 |
child 23133 | a58476887b3d |
permissions | -rw-r--r-- |
10 | 1 |
/* |
22701
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
2 |
* Copyright (c) 1999, 2014, 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 |
||
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
30 |
import com.sun.source.tree.MemberReferenceTree.ReferenceMode; |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
31 |
|
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
32 |
import com.sun.tools.javac.code.*; |
10815 | 33 |
import com.sun.tools.javac.parser.Tokens.*; |
10948 | 34 |
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle; |
10 | 35 |
import com.sun.tools.javac.tree.*; |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
36 |
import com.sun.tools.javac.tree.JCTree.*; |
10 | 37 |
import com.sun.tools.javac.util.*; |
7076
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
38 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag; |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
39 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
10 | 40 |
import com.sun.tools.javac.util.List; |
41 |
||
10950 | 42 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.*; |
43 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.ASSERT; |
|
44 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.CASE; |
|
45 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.CATCH; |
|
46 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.EQ; |
|
47 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.GT; |
|
48 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.IMPORT; |
|
49 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.LT; |
|
14541
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
14450
diff
changeset
|
50 |
import static com.sun.tools.javac.tree.JCTree.Tag.*; |
10 | 51 |
|
52 |
/** The parser maps a token sequence into an abstract syntax |
|
53 |
* tree. It operates by recursive descent, with code derived |
|
54 |
* systematically from an LL(1) grammar. For efficiency reasons, an |
|
55 |
* operator precedence scheme is used for parsing binary operation |
|
56 |
* expressions. |
|
57 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
58 |
* <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
|
59 |
* If you write code that depends on this, you do so at your own risk. |
10 | 60 |
* This code and its internal interfaces are subject to change or |
61 |
* deletion without notice.</b> |
|
62 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
63 |
public class JavacParser implements Parser { |
10 | 64 |
|
65 |
/** The number of precedence levels of infix operators. |
|
66 |
*/ |
|
67 |
private static final int infixPrecedenceLevels = 10; |
|
68 |
||
69 |
/** The scanner used for lexical analysis. |
|
70 |
*/ |
|
10200
56cc93e7b6ef
7068902: (javac) allow enabling or disabling of String folding
ksrini
parents:
10199
diff
changeset
|
71 |
protected Lexer S; |
10 | 72 |
|
73 |
/** The factory to be used for abstract syntax tree construction. |
|
74 |
*/ |
|
75 |
protected TreeMaker F; |
|
76 |
||
77 |
/** The log to be used for error diagnostics. |
|
78 |
*/ |
|
79 |
private Log log; |
|
80 |
||
81 |
/** The Source language setting. */ |
|
82 |
private Source source; |
|
83 |
||
84 |
/** The name table. */ |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1258
diff
changeset
|
85 |
private Names names; |
10 | 86 |
|
11055 | 87 |
/** End position mappings container */ |
88 |
private final AbstractEndPosTable endPosTable; |
|
89 |
||
15385 | 90 |
// Because of javac's limited lookahead, some contexts are ambiguous in |
91 |
// the presence of type annotations even though they are not ambiguous |
|
92 |
// in the absence of type annotations. Consider this code: |
|
93 |
// void m(String [] m) { } |
|
94 |
// void m(String ... m) { } |
|
95 |
// After parsing "String", javac calls bracketsOpt which immediately |
|
96 |
// returns if the next character is not '['. Similarly, javac can see |
|
97 |
// if the next token is ... and in that case parse an ellipsis. But in |
|
98 |
// the presence of type annotations: |
|
99 |
// void m(String @A [] m) { } |
|
100 |
// void m(String @A ... m) { } |
|
101 |
// no finite lookahead is enough to determine whether to read array |
|
102 |
// levels or an ellipsis. Furthermore, if you call bracketsOpt, then |
|
103 |
// bracketsOpt first reads all the leading annotations and only then |
|
104 |
// discovers that it needs to fail. bracketsOpt needs a way to push |
|
105 |
// back the extra annotations that it read. (But, bracketsOpt should |
|
106 |
// not *always* be allowed to push back extra annotations that it finds |
|
107 |
// -- in most contexts, any such extra annotation is an error. |
|
108 |
// |
|
109 |
// The following two variables permit type annotations that have |
|
110 |
// already been read to be stored for later use. Alternate |
|
111 |
// implementations are possible but would cause much larger changes to |
|
112 |
// the parser. |
|
113 |
||
114 |
/** Type annotations that have already been read but have not yet been used. **/ |
|
115 |
private List<JCAnnotation> typeAnnotationsPushedBack = List.nil(); |
|
116 |
||
117 |
/** |
|
118 |
* If the parser notices extra annotations, then it either immediately |
|
119 |
* issues an error (if this variable is false) or places the extra |
|
120 |
* annotations in variable typeAnnotationsPushedBack (if this variable |
|
121 |
* is true). |
|
122 |
*/ |
|
123 |
private boolean permitTypeAnnotationsPushBack = false; |
|
124 |
||
14450
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
125 |
interface ErrorRecoveryAction { |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
126 |
JCTree doRecover(JavacParser parser); |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
127 |
} |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
128 |
|
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
129 |
enum BasicErrorRecoveryAction implements ErrorRecoveryAction { |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
130 |
BLOCK_STMT {public JCTree doRecover(JavacParser parser) { return parser.parseStatementAsBlock(); }}, |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
131 |
CATCH_CLAUSE {public JCTree doRecover(JavacParser parser) { return parser.catchClause(); }} |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
132 |
} |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
133 |
|
10 | 134 |
/** Construct a parser from a given scanner, tree factory and log. |
135 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
136 |
protected JavacParser(ParserFactory fac, |
10 | 137 |
Lexer S, |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
138 |
boolean keepDocComments, |
11055 | 139 |
boolean keepLineMap, |
140 |
boolean keepEndPositions) { |
|
10 | 141 |
this.S = S; |
10815 | 142 |
nextToken(); // prime the pump |
10 | 143 |
this.F = fac.F; |
144 |
this.log = fac.log; |
|
145 |
this.names = fac.names; |
|
146 |
this.source = fac.source; |
|
147 |
this.allowGenerics = source.allowGenerics(); |
|
148 |
this.allowVarargs = source.allowVarargs(); |
|
149 |
this.allowAsserts = source.allowAsserts(); |
|
150 |
this.allowEnums = source.allowEnums(); |
|
151 |
this.allowForeach = source.allowForeach(); |
|
152 |
this.allowStaticImport = source.allowStaticImport(); |
|
153 |
this.allowAnnotations = source.allowAnnotations(); |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
154 |
this.allowTWR = source.allowTryWithResources(); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
155 |
this.allowDiamond = source.allowDiamond(); |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
156 |
this.allowMulticatch = source.allowMulticatch(); |
10200
56cc93e7b6ef
7068902: (javac) allow enabling or disabling of String folding
ksrini
parents:
10199
diff
changeset
|
157 |
this.allowStringFolding = fac.options.getBoolean("allowStringFolding", true); |
14547 | 158 |
this.allowLambda = source.allowLambda(); |
159 |
this.allowMethodReferences = source.allowMethodReferences(); |
|
160 |
this.allowDefaultMethods = source.allowDefaultMethods(); |
|
15377
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
161 |
this.allowStaticInterfaceMethods = source.allowStaticInterfaceMethods(); |
15375
d2529dc91d77
8006566: Remove transient lambda-related guards from JavacParser
mcimadamore
parents:
15367
diff
changeset
|
162 |
this.allowIntersectionTypesInCast = source.allowIntersectionTypesInCast(); |
15385 | 163 |
this.allowTypeAnnotations = source.allowTypeAnnotations(); |
10 | 164 |
this.keepDocComments = keepDocComments; |
14541
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
14450
diff
changeset
|
165 |
docComments = newDocCommentTable(keepDocComments, fac); |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
166 |
this.keepLineMap = keepLineMap; |
10 | 167 |
this.errorTree = F.Erroneous(); |
11055 | 168 |
endPosTable = newEndPosTable(keepEndPositions); |
10 | 169 |
} |
170 |
||
11055 | 171 |
protected AbstractEndPosTable newEndPosTable(boolean keepEndPositions) { |
172 |
return keepEndPositions |
|
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
173 |
? new SimpleEndPosTable(this) |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
174 |
: new EmptyEndPosTable(this); |
11055 | 175 |
} |
13077 | 176 |
|
14541
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
14450
diff
changeset
|
177 |
protected DocCommentTable newDocCommentTable(boolean keepDocComments, ParserFactory fac) { |
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
14450
diff
changeset
|
178 |
return keepDocComments ? new LazyDocCommentTable(fac) : null; |
13077 | 179 |
} |
180 |
||
10 | 181 |
/** Switch: Should generics be recognized? |
182 |
*/ |
|
183 |
boolean allowGenerics; |
|
184 |
||
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
185 |
/** Switch: Should diamond operator be recognized? |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
186 |
*/ |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
187 |
boolean allowDiamond; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
188 |
|
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
189 |
/** Switch: Should multicatch clause be accepted? |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
190 |
*/ |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
191 |
boolean allowMulticatch; |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
192 |
|
10 | 193 |
/** Switch: Should varargs be recognized? |
194 |
*/ |
|
195 |
boolean allowVarargs; |
|
196 |
||
197 |
/** Switch: should we recognize assert statements, or just give a warning? |
|
198 |
*/ |
|
199 |
boolean allowAsserts; |
|
200 |
||
201 |
/** Switch: should we recognize enums, or just give a warning? |
|
202 |
*/ |
|
203 |
boolean allowEnums; |
|
204 |
||
205 |
/** Switch: should we recognize foreach? |
|
206 |
*/ |
|
207 |
boolean allowForeach; |
|
208 |
||
209 |
/** Switch: should we recognize foreach? |
|
210 |
*/ |
|
211 |
boolean allowStaticImport; |
|
212 |
||
213 |
/** Switch: should we recognize annotations? |
|
214 |
*/ |
|
215 |
boolean allowAnnotations; |
|
216 |
||
8224
8f18e1622660
7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents:
8047
diff
changeset
|
217 |
/** Switch: should we recognize try-with-resources? |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
218 |
*/ |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
219 |
boolean allowTWR; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
220 |
|
10200
56cc93e7b6ef
7068902: (javac) allow enabling or disabling of String folding
ksrini
parents:
10199
diff
changeset
|
221 |
/** Switch: should we fold strings? |
56cc93e7b6ef
7068902: (javac) allow enabling or disabling of String folding
ksrini
parents:
10199
diff
changeset
|
222 |
*/ |
56cc93e7b6ef
7068902: (javac) allow enabling or disabling of String folding
ksrini
parents:
10199
diff
changeset
|
223 |
boolean allowStringFolding; |
56cc93e7b6ef
7068902: (javac) allow enabling or disabling of String folding
ksrini
parents:
10199
diff
changeset
|
224 |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
225 |
/** Switch: should we recognize lambda expressions? |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
226 |
*/ |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
227 |
boolean allowLambda; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
228 |
|
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
229 |
/** Switch: should we allow method/constructor references? |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
230 |
*/ |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
231 |
boolean allowMethodReferences; |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
232 |
|
14267
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
233 |
/** Switch: should we allow default methods in interfaces? |
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
234 |
*/ |
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
235 |
boolean allowDefaultMethods; |
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
236 |
|
15377
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
237 |
/** Switch: should we allow static methods in interfaces? |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
238 |
*/ |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
239 |
boolean allowStaticInterfaceMethods; |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
240 |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
241 |
/** Switch: should we allow intersection types in cast? |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
242 |
*/ |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
243 |
boolean allowIntersectionTypesInCast; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
244 |
|
10 | 245 |
/** Switch: should we keep docComments? |
246 |
*/ |
|
247 |
boolean keepDocComments; |
|
248 |
||
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
249 |
/** Switch: should we keep line table? |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
250 |
*/ |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
251 |
boolean keepLineMap; |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
252 |
|
15385 | 253 |
/** Switch: should we recognize type annotations? |
254 |
*/ |
|
255 |
boolean allowTypeAnnotations; |
|
256 |
||
257 |
/** Switch: is "this" allowed as an identifier? |
|
258 |
* This is needed to parse receiver types. |
|
259 |
*/ |
|
260 |
boolean allowThisIdent; |
|
261 |
||
262 |
/** The type of the method receiver, as specified by a first "this" parameter. |
|
263 |
*/ |
|
264 |
JCVariableDecl receiverParam; |
|
265 |
||
266 |
||
10 | 267 |
/** When terms are parsed, the mode determines which is expected: |
268 |
* mode = EXPR : an expression |
|
269 |
* mode = TYPE : a type |
|
270 |
* mode = NOPARAMS : no parameters allowed for type |
|
271 |
* mode = TYPEARG : type argument |
|
272 |
*/ |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
273 |
static final int EXPR = 0x1; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
274 |
static final int TYPE = 0x2; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
275 |
static final int NOPARAMS = 0x4; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
276 |
static final int TYPEARG = 0x8; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
277 |
static final int DIAMOND = 0x10; |
10 | 278 |
|
279 |
/** The current mode. |
|
280 |
*/ |
|
281 |
private int mode = 0; |
|
282 |
||
283 |
/** The mode of the term that was parsed last. |
|
284 |
*/ |
|
285 |
private int lastmode = 0; |
|
286 |
||
10815 | 287 |
/* ---------- token management -------------- */ |
288 |
||
289 |
protected Token token; |
|
290 |
||
14541
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
14450
diff
changeset
|
291 |
public Token token() { |
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
14450
diff
changeset
|
292 |
return token; |
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
14450
diff
changeset
|
293 |
} |
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
14450
diff
changeset
|
294 |
|
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
14450
diff
changeset
|
295 |
public void nextToken() { |
10815 | 296 |
S.nextToken(); |
297 |
token = S.token(); |
|
298 |
} |
|
299 |
||
15367 | 300 |
protected boolean peekToken(Filter<TokenKind> tk) { |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
301 |
return peekToken(0, tk); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
302 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
303 |
|
15367 | 304 |
protected boolean peekToken(int lookahead, Filter<TokenKind> tk) { |
305 |
return tk.accepts(S.token(lookahead + 1).kind); |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
306 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
307 |
|
15367 | 308 |
protected boolean peekToken(Filter<TokenKind> tk1, Filter<TokenKind> tk2) { |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
309 |
return peekToken(0, tk1, tk2); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
310 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
311 |
|
15367 | 312 |
protected boolean peekToken(int lookahead, Filter<TokenKind> tk1, Filter<TokenKind> tk2) { |
313 |
return tk1.accepts(S.token(lookahead + 1).kind) && |
|
314 |
tk2.accepts(S.token(lookahead + 2).kind); |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
315 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
316 |
|
15367 | 317 |
protected boolean peekToken(Filter<TokenKind> tk1, Filter<TokenKind> tk2, Filter<TokenKind> tk3) { |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
318 |
return peekToken(0, tk1, tk2, tk3); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
319 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
320 |
|
15367 | 321 |
protected boolean peekToken(int lookahead, Filter<TokenKind> tk1, Filter<TokenKind> tk2, Filter<TokenKind> tk3) { |
322 |
return tk1.accepts(S.token(lookahead + 1).kind) && |
|
323 |
tk2.accepts(S.token(lookahead + 2).kind) && |
|
324 |
tk3.accepts(S.token(lookahead + 3).kind); |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
325 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
326 |
|
15367 | 327 |
@SuppressWarnings("unchecked") |
328 |
protected boolean peekToken(Filter<TokenKind>... kinds) { |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
329 |
return peekToken(0, kinds); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
330 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
331 |
|
15367 | 332 |
@SuppressWarnings("unchecked") |
333 |
protected boolean peekToken(int lookahead, Filter<TokenKind>... kinds) { |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
334 |
for (; lookahead < kinds.length ; lookahead++) { |
15367 | 335 |
if (!kinds[lookahead].accepts(S.token(lookahead + 1).kind)) { |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
336 |
return false; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
337 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
338 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
339 |
return true; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
340 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
341 |
|
10815 | 342 |
/* ---------- error recovery -------------- */ |
10 | 343 |
|
344 |
private JCErroneous errorTree; |
|
345 |
||
346 |
/** Skip forward until a suitable stop token is found. |
|
347 |
*/ |
|
348 |
private void skip(boolean stopAtImport, boolean stopAtMemberDecl, boolean stopAtIdentifier, boolean stopAtStatement) { |
|
349 |
while (true) { |
|
10815 | 350 |
switch (token.kind) { |
10 | 351 |
case SEMI: |
10815 | 352 |
nextToken(); |
10 | 353 |
return; |
354 |
case PUBLIC: |
|
355 |
case FINAL: |
|
356 |
case ABSTRACT: |
|
357 |
case MONKEYS_AT: |
|
358 |
case EOF: |
|
359 |
case CLASS: |
|
360 |
case INTERFACE: |
|
361 |
case ENUM: |
|
362 |
return; |
|
363 |
case IMPORT: |
|
364 |
if (stopAtImport) |
|
365 |
return; |
|
366 |
break; |
|
367 |
case LBRACE: |
|
368 |
case RBRACE: |
|
369 |
case PRIVATE: |
|
370 |
case PROTECTED: |
|
371 |
case STATIC: |
|
372 |
case TRANSIENT: |
|
373 |
case NATIVE: |
|
374 |
case VOLATILE: |
|
375 |
case SYNCHRONIZED: |
|
376 |
case STRICTFP: |
|
377 |
case LT: |
|
378 |
case BYTE: |
|
379 |
case SHORT: |
|
380 |
case CHAR: |
|
381 |
case INT: |
|
382 |
case LONG: |
|
383 |
case FLOAT: |
|
384 |
case DOUBLE: |
|
385 |
case BOOLEAN: |
|
386 |
case VOID: |
|
387 |
if (stopAtMemberDecl) |
|
388 |
return; |
|
389 |
break; |
|
15367 | 390 |
case UNDERSCORE: |
10 | 391 |
case IDENTIFIER: |
392 |
if (stopAtIdentifier) |
|
393 |
return; |
|
394 |
break; |
|
395 |
case CASE: |
|
396 |
case DEFAULT: |
|
397 |
case IF: |
|
398 |
case FOR: |
|
399 |
case WHILE: |
|
400 |
case DO: |
|
401 |
case TRY: |
|
402 |
case SWITCH: |
|
403 |
case RETURN: |
|
404 |
case THROW: |
|
405 |
case BREAK: |
|
406 |
case CONTINUE: |
|
407 |
case ELSE: |
|
408 |
case FINALLY: |
|
409 |
case CATCH: |
|
410 |
if (stopAtStatement) |
|
411 |
return; |
|
412 |
break; |
|
413 |
} |
|
10815 | 414 |
nextToken(); |
10 | 415 |
} |
416 |
} |
|
417 |
||
10815 | 418 |
private JCErroneous syntaxError(int pos, String key, TokenKind... args) { |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
419 |
return syntaxError(pos, List.<JCTree>nil(), key, args); |
10 | 420 |
} |
421 |
||
10815 | 422 |
private JCErroneous syntaxError(int pos, List<JCTree> errs, String key, TokenKind... args) { |
10 | 423 |
setErrorEndPos(pos); |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
424 |
JCErroneous err = F.at(pos).Erroneous(errs); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
425 |
reportSyntaxError(err, key, (Object[])args); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
426 |
if (errs != null) { |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
427 |
JCTree last = errs.last(); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
428 |
if (last != null) |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
429 |
storeEnd(last, pos); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
430 |
} |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
431 |
return toP(err); |
10 | 432 |
} |
433 |
||
434 |
private int errorPos = Position.NOPOS; |
|
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
435 |
|
10 | 436 |
/** |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
437 |
* Report a syntax using the given the position parameter and arguments, |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
438 |
* unless one was already reported at the same position. |
10 | 439 |
*/ |
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
440 |
private void reportSyntaxError(int pos, String key, Object... args) { |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
441 |
JCDiagnostic.DiagnosticPosition diag = new JCDiagnostic.SimpleDiagnosticPosition(pos); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
442 |
reportSyntaxError(diag, key, args); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
443 |
} |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
444 |
|
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
445 |
/** |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
446 |
* Report a syntax error using the given DiagnosticPosition object and |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
447 |
* arguments, unless one was already reported at the same position. |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
448 |
*/ |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
449 |
private void reportSyntaxError(JCDiagnostic.DiagnosticPosition diagPos, String key, Object... args) { |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
450 |
int pos = diagPos.getPreferredPosition(); |
10 | 451 |
if (pos > S.errPos() || pos == Position.NOPOS) { |
10815 | 452 |
if (token.kind == EOF) { |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
453 |
error(diagPos, "premature.eof"); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
454 |
} else { |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
455 |
error(diagPos, key, args); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
456 |
} |
10 | 457 |
} |
458 |
S.errPos(pos); |
|
10815 | 459 |
if (token.pos == errorPos) |
460 |
nextToken(); // guarantee progress |
|
461 |
errorPos = token.pos; |
|
10 | 462 |
} |
463 |
||
464 |
||
465 |
/** Generate a syntax error at current position unless one was already |
|
466 |
* reported at the same position. |
|
467 |
*/ |
|
468 |
private JCErroneous syntaxError(String key) { |
|
10815 | 469 |
return syntaxError(token.pos, key); |
10 | 470 |
} |
471 |
||
472 |
/** Generate a syntax error at current position unless one was |
|
473 |
* already reported at the same position. |
|
474 |
*/ |
|
10815 | 475 |
private JCErroneous syntaxError(String key, TokenKind arg) { |
476 |
return syntaxError(token.pos, key, arg); |
|
10 | 477 |
} |
478 |
||
479 |
/** If next input token matches given token, skip it, otherwise report |
|
480 |
* an error. |
|
481 |
*/ |
|
10815 | 482 |
public void accept(TokenKind tk) { |
483 |
if (token.kind == tk) { |
|
484 |
nextToken(); |
|
10 | 485 |
} else { |
10815 | 486 |
setErrorEndPos(token.pos); |
487 |
reportSyntaxError(S.prevToken().endPos, "expected", tk); |
|
10 | 488 |
} |
489 |
} |
|
490 |
||
491 |
/** Report an illegal start of expression/type error at given position. |
|
492 |
*/ |
|
493 |
JCExpression illegal(int pos) { |
|
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
494 |
setErrorEndPos(pos); |
10 | 495 |
if ((mode & EXPR) != 0) |
496 |
return syntaxError(pos, "illegal.start.of.expr"); |
|
497 |
else |
|
498 |
return syntaxError(pos, "illegal.start.of.type"); |
|
499 |
||
500 |
} |
|
501 |
||
502 |
/** Report an illegal start of expression/type error at current position. |
|
503 |
*/ |
|
504 |
JCExpression illegal() { |
|
10815 | 505 |
return illegal(token.pos); |
10 | 506 |
} |
507 |
||
508 |
/** Diagnose a modifier flag from the set, if any. */ |
|
509 |
void checkNoMods(long mods) { |
|
510 |
if (mods != 0) { |
|
511 |
long lowestMod = mods & -mods; |
|
10815 | 512 |
error(token.pos, "mod.not.allowed.here", |
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
513 |
Flags.asFlagSet(lowestMod)); |
10 | 514 |
} |
515 |
} |
|
516 |
||
517 |
/* ---------- doc comments --------- */ |
|
518 |
||
13077 | 519 |
/** A table to store all documentation comments |
10 | 520 |
* indexed by the tree nodes they refer to. |
521 |
* defined only if option flag keepDocComment is set. |
|
522 |
*/ |
|
13077 | 523 |
private final DocCommentTable docComments; |
10 | 524 |
|
525 |
/** Make an entry into docComments hashtable, |
|
526 |
* provided flag keepDocComments is set and given doc comment is non-null. |
|
527 |
* @param tree The tree to be used as index in the hashtable |
|
528 |
* @param dc The doc comment to associate with the tree, or null. |
|
529 |
*/ |
|
13077 | 530 |
void attach(JCTree tree, Comment dc) { |
10 | 531 |
if (keepDocComments && dc != null) { |
532 |
// System.out.println("doc comment = ");System.out.println(dc);//DEBUG |
|
13077 | 533 |
docComments.putComment(tree, dc); |
10 | 534 |
} |
535 |
} |
|
536 |
||
537 |
/* -------- source positions ------- */ |
|
538 |
||
539 |
private void setErrorEndPos(int errPos) { |
|
11055 | 540 |
endPosTable.setErrorEndPos(errPos); |
10 | 541 |
} |
542 |
||
11055 | 543 |
private void storeEnd(JCTree tree, int endpos) { |
544 |
endPosTable.storeEnd(tree, endpos); |
|
10 | 545 |
} |
546 |
||
11055 | 547 |
private <T extends JCTree> T to(T t) { |
548 |
return endPosTable.to(t); |
|
549 |
} |
|
10 | 550 |
|
11055 | 551 |
private <T extends JCTree> T toP(T t) { |
552 |
return endPosTable.toP(t); |
|
553 |
} |
|
10 | 554 |
|
555 |
/** Get the start position for a tree node. The start position is |
|
556 |
* defined to be the position of the first character of the first |
|
557 |
* token of the node's source text. |
|
558 |
* @param tree The tree node |
|
559 |
*/ |
|
560 |
public int getStartPos(JCTree tree) { |
|
561 |
return TreeInfo.getStartPos(tree); |
|
562 |
} |
|
563 |
||
564 |
/** |
|
565 |
* Get the end position for a tree node. The end position is |
|
566 |
* defined to be the position of the last character of the last |
|
567 |
* token of the node's source text. Returns Position.NOPOS if end |
|
568 |
* positions are not generated or the position is otherwise not |
|
569 |
* found. |
|
570 |
* @param tree The tree node |
|
571 |
*/ |
|
572 |
public int getEndPos(JCTree tree) { |
|
11055 | 573 |
return endPosTable.getEndPos(tree); |
10 | 574 |
} |
575 |
||
576 |
||
577 |
||
578 |
/* ---------- parsing -------------- */ |
|
579 |
||
580 |
/** |
|
581 |
* Ident = IDENTIFIER |
|
582 |
*/ |
|
583 |
Name ident() { |
|
10815 | 584 |
if (token.kind == IDENTIFIER) { |
585 |
Name name = token.name(); |
|
586 |
nextToken(); |
|
10 | 587 |
return name; |
10815 | 588 |
} else if (token.kind == ASSERT) { |
10 | 589 |
if (allowAsserts) { |
10815 | 590 |
error(token.pos, "assert.as.identifier"); |
591 |
nextToken(); |
|
10 | 592 |
return names.error; |
593 |
} else { |
|
10815 | 594 |
warning(token.pos, "assert.as.identifier"); |
595 |
Name name = token.name(); |
|
596 |
nextToken(); |
|
10 | 597 |
return name; |
598 |
} |
|
10815 | 599 |
} else if (token.kind == ENUM) { |
10 | 600 |
if (allowEnums) { |
10815 | 601 |
error(token.pos, "enum.as.identifier"); |
602 |
nextToken(); |
|
10 | 603 |
return names.error; |
604 |
} else { |
|
10815 | 605 |
warning(token.pos, "enum.as.identifier"); |
606 |
Name name = token.name(); |
|
607 |
nextToken(); |
|
10 | 608 |
return name; |
609 |
} |
|
15385 | 610 |
} else if (token.kind == THIS) { |
611 |
if (allowThisIdent) { |
|
612 |
// Make sure we're using a supported source version. |
|
613 |
checkTypeAnnotations(); |
|
614 |
Name name = token.name(); |
|
615 |
nextToken(); |
|
616 |
return name; |
|
617 |
} else { |
|
618 |
error(token.pos, "this.as.identifier"); |
|
619 |
nextToken(); |
|
620 |
return names.error; |
|
621 |
} |
|
15367 | 622 |
} else if (token.kind == UNDERSCORE) { |
623 |
warning(token.pos, "underscore.as.identifier"); |
|
624 |
Name name = token.name(); |
|
625 |
nextToken(); |
|
626 |
return name; |
|
10 | 627 |
} else { |
628 |
accept(IDENTIFIER); |
|
629 |
return names.error; |
|
630 |
} |
|
15367 | 631 |
} |
10 | 632 |
|
633 |
/** |
|
15385 | 634 |
* Qualident = Ident { DOT [Annotations] Ident } |
10 | 635 |
*/ |
15385 | 636 |
public JCExpression qualident(boolean allowAnnos) { |
10815 | 637 |
JCExpression t = toP(F.at(token.pos).Ident(ident())); |
638 |
while (token.kind == DOT) { |
|
639 |
int pos = token.pos; |
|
640 |
nextToken(); |
|
15385 | 641 |
List<JCAnnotation> tyannos = null; |
642 |
if (allowAnnos) { |
|
643 |
tyannos = typeAnnotationsOpt(); |
|
644 |
} |
|
10 | 645 |
t = toP(F.at(pos).Select(t, ident())); |
15385 | 646 |
if (tyannos != null && tyannos.nonEmpty()) { |
647 |
t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t)); |
|
648 |
} |
|
10 | 649 |
} |
650 |
return t; |
|
651 |
} |
|
652 |
||
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
653 |
JCExpression literal(Name prefix) { |
10815 | 654 |
return literal(prefix, token.pos); |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
655 |
} |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
656 |
|
10 | 657 |
/** |
658 |
* Literal = |
|
659 |
* INTLITERAL |
|
660 |
* | LONGLITERAL |
|
661 |
* | FLOATLITERAL |
|
662 |
* | DOUBLELITERAL |
|
663 |
* | CHARLITERAL |
|
664 |
* | STRINGLITERAL |
|
665 |
* | TRUE |
|
666 |
* | FALSE |
|
667 |
* | NULL |
|
668 |
*/ |
|
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
669 |
JCExpression literal(Name prefix, int pos) { |
10 | 670 |
JCExpression t = errorTree; |
10815 | 671 |
switch (token.kind) { |
10 | 672 |
case INTLITERAL: |
673 |
try { |
|
674 |
t = F.at(pos).Literal( |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
675 |
TypeTag.INT, |
10815 | 676 |
Convert.string2int(strval(prefix), token.radix())); |
10 | 677 |
} catch (NumberFormatException ex) { |
10815 | 678 |
error(token.pos, "int.number.too.large", strval(prefix)); |
10 | 679 |
} |
680 |
break; |
|
681 |
case LONGLITERAL: |
|
682 |
try { |
|
683 |
t = F.at(pos).Literal( |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
684 |
TypeTag.LONG, |
10815 | 685 |
new Long(Convert.string2long(strval(prefix), token.radix()))); |
10 | 686 |
} catch (NumberFormatException ex) { |
10815 | 687 |
error(token.pos, "int.number.too.large", strval(prefix)); |
10 | 688 |
} |
689 |
break; |
|
690 |
case FLOATLITERAL: { |
|
10815 | 691 |
String proper = token.radix() == 16 ? |
692 |
("0x"+ token.stringVal()) : |
|
693 |
token.stringVal(); |
|
10 | 694 |
Float n; |
695 |
try { |
|
696 |
n = Float.valueOf(proper); |
|
697 |
} catch (NumberFormatException ex) { |
|
7636
030f141aa32b
6504896: TreeMaker.Literal(Object) does not support Booleans
jjg
parents:
7211
diff
changeset
|
698 |
// error already reported in scanner |
10 | 699 |
n = Float.NaN; |
700 |
} |
|
701 |
if (n.floatValue() == 0.0f && !isZero(proper)) |
|
10815 | 702 |
error(token.pos, "fp.number.too.small"); |
10 | 703 |
else if (n.floatValue() == Float.POSITIVE_INFINITY) |
10815 | 704 |
error(token.pos, "fp.number.too.large"); |
10 | 705 |
else |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
706 |
t = F.at(pos).Literal(TypeTag.FLOAT, n); |
10 | 707 |
break; |
708 |
} |
|
709 |
case DOUBLELITERAL: { |
|
10815 | 710 |
String proper = token.radix() == 16 ? |
711 |
("0x"+ token.stringVal()) : |
|
712 |
token.stringVal(); |
|
10 | 713 |
Double n; |
714 |
try { |
|
715 |
n = Double.valueOf(proper); |
|
716 |
} catch (NumberFormatException ex) { |
|
717 |
// error already reported in scanner |
|
718 |
n = Double.NaN; |
|
719 |
} |
|
720 |
if (n.doubleValue() == 0.0d && !isZero(proper)) |
|
10815 | 721 |
error(token.pos, "fp.number.too.small"); |
10 | 722 |
else if (n.doubleValue() == Double.POSITIVE_INFINITY) |
10815 | 723 |
error(token.pos, "fp.number.too.large"); |
10 | 724 |
else |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
725 |
t = F.at(pos).Literal(TypeTag.DOUBLE, n); |
10 | 726 |
break; |
727 |
} |
|
728 |
case CHARLITERAL: |
|
729 |
t = F.at(pos).Literal( |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
730 |
TypeTag.CHAR, |
10815 | 731 |
token.stringVal().charAt(0) + 0); |
10 | 732 |
break; |
733 |
case STRINGLITERAL: |
|
734 |
t = F.at(pos).Literal( |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
735 |
TypeTag.CLASS, |
10815 | 736 |
token.stringVal()); |
10 | 737 |
break; |
738 |
case TRUE: case FALSE: |
|
739 |
t = F.at(pos).Literal( |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
740 |
TypeTag.BOOLEAN, |
10815 | 741 |
(token.kind == TRUE ? 1 : 0)); |
10 | 742 |
break; |
743 |
case NULL: |
|
744 |
t = F.at(pos).Literal( |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
745 |
TypeTag.BOT, |
10 | 746 |
null); |
747 |
break; |
|
748 |
default: |
|
8032 | 749 |
Assert.error(); |
10 | 750 |
} |
751 |
if (t == errorTree) |
|
752 |
t = F.at(pos).Erroneous(); |
|
10815 | 753 |
storeEnd(t, token.endPos); |
754 |
nextToken(); |
|
10 | 755 |
return t; |
756 |
} |
|
15385 | 757 |
//where |
10 | 758 |
boolean isZero(String s) { |
759 |
char[] cs = s.toCharArray(); |
|
3894 | 760 |
int base = ((cs.length > 1 && Character.toLowerCase(cs[1]) == 'x') ? 16 : 10); |
10 | 761 |
int i = ((base==16) ? 2 : 0); |
762 |
while (i < cs.length && (cs[i] == '0' || cs[i] == '.')) i++; |
|
763 |
return !(i < cs.length && (Character.digit(cs[i], base) > 0)); |
|
764 |
} |
|
765 |
||
766 |
String strval(Name prefix) { |
|
10815 | 767 |
String s = token.stringVal(); |
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1258
diff
changeset
|
768 |
return prefix.isEmpty() ? s : prefix + s; |
10 | 769 |
} |
770 |
||
771 |
/** terms can be either expressions or types. |
|
772 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
773 |
public JCExpression parseExpression() { |
10 | 774 |
return term(EXPR); |
775 |
} |
|
776 |
||
15385 | 777 |
/** |
778 |
* parses (optional) type annotations followed by a type. If the |
|
779 |
* annotations are present before the type and are not consumed during array |
|
780 |
* parsing, this method returns a {@link JCAnnotatedType} consisting of |
|
781 |
* these annotations and the underlying type. Otherwise, it returns the |
|
782 |
* underlying type. |
|
783 |
* |
|
784 |
* <p> |
|
785 |
* |
|
786 |
* Note that this method sets {@code mode} to {@code TYPE} first, before |
|
787 |
* parsing annotations. |
|
788 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
789 |
public JCExpression parseType() { |
15385 | 790 |
List<JCAnnotation> annotations = typeAnnotationsOpt(); |
791 |
return parseType(annotations); |
|
792 |
} |
|
793 |
||
794 |
public JCExpression parseType(List<JCAnnotation> annotations) { |
|
795 |
JCExpression result = unannotatedType(); |
|
796 |
||
797 |
if (annotations.nonEmpty()) { |
|
798 |
result = insertAnnotationsToMostInner(result, annotations, false); |
|
799 |
} |
|
800 |
||
801 |
return result; |
|
802 |
} |
|
803 |
||
804 |
public JCExpression unannotatedType() { |
|
10 | 805 |
return term(TYPE); |
806 |
} |
|
807 |
||
808 |
JCExpression term(int newmode) { |
|
809 |
int prevmode = mode; |
|
810 |
mode = newmode; |
|
811 |
JCExpression t = term(); |
|
812 |
lastmode = mode; |
|
813 |
mode = prevmode; |
|
814 |
return t; |
|
815 |
} |
|
816 |
||
817 |
/** |
|
13844 | 818 |
* {@literal |
10 | 819 |
* Expression = Expression1 [ExpressionRest] |
820 |
* ExpressionRest = [AssignmentOperator Expression1] |
|
821 |
* AssignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" | |
|
822 |
* "&=" | "|=" | "^=" | |
|
823 |
* "%=" | "<<=" | ">>=" | ">>>=" |
|
824 |
* Type = Type1 |
|
825 |
* TypeNoParams = TypeNoParams1 |
|
826 |
* StatementExpression = Expression |
|
827 |
* ConstantExpression = Expression |
|
13844 | 828 |
* } |
10 | 829 |
*/ |
830 |
JCExpression term() { |
|
831 |
JCExpression t = term1(); |
|
832 |
if ((mode & EXPR) != 0 && |
|
10815 | 833 |
token.kind == EQ || PLUSEQ.compareTo(token.kind) <= 0 && token.kind.compareTo(GTGTGTEQ) <= 0) |
10 | 834 |
return termRest(t); |
835 |
else |
|
836 |
return t; |
|
837 |
} |
|
838 |
||
839 |
JCExpression termRest(JCExpression t) { |
|
10815 | 840 |
switch (token.kind) { |
10 | 841 |
case EQ: { |
10815 | 842 |
int pos = token.pos; |
843 |
nextToken(); |
|
10 | 844 |
mode = EXPR; |
845 |
JCExpression t1 = term(); |
|
846 |
return toP(F.at(pos).Assign(t, t1)); |
|
847 |
} |
|
848 |
case PLUSEQ: |
|
849 |
case SUBEQ: |
|
850 |
case STAREQ: |
|
851 |
case SLASHEQ: |
|
852 |
case PERCENTEQ: |
|
853 |
case AMPEQ: |
|
854 |
case BAREQ: |
|
855 |
case CARETEQ: |
|
856 |
case LTLTEQ: |
|
857 |
case GTGTEQ: |
|
858 |
case GTGTGTEQ: |
|
10815 | 859 |
int pos = token.pos; |
860 |
TokenKind tk = token.kind; |
|
861 |
nextToken(); |
|
10 | 862 |
mode = EXPR; |
863 |
JCExpression t1 = term(); |
|
10815 | 864 |
return F.at(pos).Assignop(optag(tk), t, t1); |
10 | 865 |
default: |
866 |
return t; |
|
867 |
} |
|
868 |
} |
|
869 |
||
870 |
/** Expression1 = Expression2 [Expression1Rest] |
|
871 |
* Type1 = Type2 |
|
872 |
* TypeNoParams1 = TypeNoParams2 |
|
873 |
*/ |
|
874 |
JCExpression term1() { |
|
875 |
JCExpression t = term2(); |
|
10815 | 876 |
if ((mode & EXPR) != 0 && token.kind == QUES) { |
10 | 877 |
mode = EXPR; |
878 |
return term1Rest(t); |
|
879 |
} else { |
|
880 |
return t; |
|
881 |
} |
|
882 |
} |
|
883 |
||
884 |
/** Expression1Rest = ["?" Expression ":" Expression1] |
|
885 |
*/ |
|
886 |
JCExpression term1Rest(JCExpression t) { |
|
10815 | 887 |
if (token.kind == QUES) { |
888 |
int pos = token.pos; |
|
889 |
nextToken(); |
|
10 | 890 |
JCExpression t1 = term(); |
891 |
accept(COLON); |
|
892 |
JCExpression t2 = term1(); |
|
893 |
return F.at(pos).Conditional(t, t1, t2); |
|
894 |
} else { |
|
895 |
return t; |
|
896 |
} |
|
897 |
} |
|
898 |
||
899 |
/** Expression2 = Expression3 [Expression2Rest] |
|
900 |
* Type2 = Type3 |
|
901 |
* TypeNoParams2 = TypeNoParams3 |
|
902 |
*/ |
|
903 |
JCExpression term2() { |
|
904 |
JCExpression t = term3(); |
|
10815 | 905 |
if ((mode & EXPR) != 0 && prec(token.kind) >= TreeInfo.orPrec) { |
10 | 906 |
mode = EXPR; |
907 |
return term2Rest(t, TreeInfo.orPrec); |
|
908 |
} else { |
|
909 |
return t; |
|
910 |
} |
|
911 |
} |
|
912 |
||
913 |
/* Expression2Rest = {infixop Expression3} |
|
914 |
* | Expression3 instanceof Type |
|
915 |
* infixop = "||" |
|
916 |
* | "&&" |
|
917 |
* | "|" |
|
918 |
* | "^" |
|
919 |
* | "&" |
|
920 |
* | "==" | "!=" |
|
921 |
* | "<" | ">" | "<=" | ">=" |
|
922 |
* | "<<" | ">>" | ">>>" |
|
923 |
* | "+" | "-" |
|
924 |
* | "*" | "/" | "%" |
|
925 |
*/ |
|
926 |
JCExpression term2Rest(JCExpression t, int minprec) { |
|
927 |
JCExpression[] odStack = newOdStack(); |
|
928 |
Token[] opStack = newOpStack(); |
|
10815 | 929 |
|
10 | 930 |
// optimization, was odStack = new Tree[...]; opStack = new Tree[...]; |
931 |
int top = 0; |
|
932 |
odStack[0] = t; |
|
10815 | 933 |
int startPos = token.pos; |
934 |
Token topOp = Tokens.DUMMY; |
|
935 |
while (prec(token.kind) >= minprec) { |
|
10 | 936 |
opStack[top] = topOp; |
937 |
top++; |
|
10815 | 938 |
topOp = token; |
939 |
nextToken(); |
|
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
940 |
odStack[top] = (topOp.kind == INSTANCEOF) ? parseType() : term3(); |
10815 | 941 |
while (top > 0 && prec(topOp.kind) >= prec(token.kind)) { |
942 |
odStack[top-1] = makeOp(topOp.pos, topOp.kind, odStack[top-1], |
|
10 | 943 |
odStack[top]); |
944 |
top--; |
|
945 |
topOp = opStack[top]; |
|
946 |
} |
|
947 |
} |
|
8032 | 948 |
Assert.check(top == 0); |
10 | 949 |
t = odStack[0]; |
950 |
||
10950 | 951 |
if (t.hasTag(JCTree.Tag.PLUS)) { |
14263 | 952 |
StringBuilder buf = foldStrings(t); |
10 | 953 |
if (buf != null) { |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
954 |
t = toP(F.at(startPos).Literal(TypeTag.CLASS, buf.toString())); |
10 | 955 |
} |
956 |
} |
|
957 |
||
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
958 |
odStackSupply.add(odStack); |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
959 |
opStackSupply.add(opStack); |
10 | 960 |
return t; |
961 |
} |
|
15385 | 962 |
//where |
10 | 963 |
/** Construct a binary or type test node. |
964 |
*/ |
|
965 |
private JCExpression makeOp(int pos, |
|
10815 | 966 |
TokenKind topOp, |
10 | 967 |
JCExpression od1, |
968 |
JCExpression od2) |
|
969 |
{ |
|
970 |
if (topOp == INSTANCEOF) { |
|
971 |
return F.at(pos).TypeTest(od1, od2); |
|
972 |
} else { |
|
973 |
return F.at(pos).Binary(optag(topOp), od1, od2); |
|
974 |
} |
|
975 |
} |
|
976 |
/** If tree is a concatenation of string literals, replace it |
|
977 |
* by a single literal representing the concatenated string. |
|
978 |
*/ |
|
14263 | 979 |
protected StringBuilder foldStrings(JCTree tree) { |
10200
56cc93e7b6ef
7068902: (javac) allow enabling or disabling of String folding
ksrini
parents:
10199
diff
changeset
|
980 |
if (!allowStringFolding) |
56cc93e7b6ef
7068902: (javac) allow enabling or disabling of String folding
ksrini
parents:
10199
diff
changeset
|
981 |
return null; |
10 | 982 |
List<String> buf = List.nil(); |
983 |
while (true) { |
|
10950 | 984 |
if (tree.hasTag(LITERAL)) { |
10 | 985 |
JCLiteral lit = (JCLiteral) tree; |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
986 |
if (lit.typetag == TypeTag.CLASS) { |
14263 | 987 |
StringBuilder sbuf = |
988 |
new StringBuilder((String)lit.value); |
|
10 | 989 |
while (buf.nonEmpty()) { |
990 |
sbuf.append(buf.head); |
|
991 |
buf = buf.tail; |
|
992 |
} |
|
993 |
return sbuf; |
|
994 |
} |
|
10950 | 995 |
} else if (tree.hasTag(JCTree.Tag.PLUS)) { |
10 | 996 |
JCBinary op = (JCBinary)tree; |
10950 | 997 |
if (op.rhs.hasTag(LITERAL)) { |
10 | 998 |
JCLiteral lit = (JCLiteral) op.rhs; |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
999 |
if (lit.typetag == TypeTag.CLASS) { |
10 | 1000 |
buf = buf.prepend((String) lit.value); |
1001 |
tree = op.lhs; |
|
1002 |
continue; |
|
1003 |
} |
|
1004 |
} |
|
1005 |
} |
|
1006 |
return null; |
|
1007 |
} |
|
1008 |
} |
|
1009 |
||
1010 |
/** optimization: To save allocating a new operand/operator stack |
|
1011 |
* for every binary operation, we use supplys. |
|
1012 |
*/ |
|
22163 | 1013 |
ArrayList<JCExpression[]> odStackSupply = new ArrayList<>(); |
1014 |
ArrayList<Token[]> opStackSupply = new ArrayList<>(); |
|
10 | 1015 |
|
1016 |
private JCExpression[] newOdStack() { |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
1017 |
if (odStackSupply.isEmpty()) |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
1018 |
return new JCExpression[infixPrecedenceLevels + 1]; |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
1019 |
return odStackSupply.remove(odStackSupply.size() - 1); |
10 | 1020 |
} |
1021 |
||
1022 |
private Token[] newOpStack() { |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
1023 |
if (opStackSupply.isEmpty()) |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
1024 |
return new Token[infixPrecedenceLevels + 1]; |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
1025 |
return opStackSupply.remove(opStackSupply.size() - 1); |
10 | 1026 |
} |
1027 |
||
13844 | 1028 |
/** |
1029 |
* Expression3 = PrefixOp Expression3 |
|
10 | 1030 |
* | "(" Expr | TypeNoParams ")" Expression3 |
1031 |
* | Primary {Selector} {PostfixOp} |
|
13844 | 1032 |
* |
1033 |
* {@literal |
|
10 | 1034 |
* Primary = "(" Expression ")" |
1035 |
* | Literal |
|
1036 |
* | [TypeArguments] THIS [Arguments] |
|
1037 |
* | [TypeArguments] SUPER SuperSuffix |
|
1038 |
* | NEW [TypeArguments] Creator |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1039 |
* | "(" Arguments ")" "->" ( Expression | Block ) |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1040 |
* | Ident "->" ( Expression | Block ) |
15385 | 1041 |
* | [Annotations] Ident { "." [Annotations] Ident } |
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1042 |
* | Expression3 MemberReferenceSuffix |
15385 | 1043 |
* [ [Annotations] "[" ( "]" BracketsOpt "." CLASS | Expression "]" ) |
10 | 1044 |
* | Arguments |
1045 |
* | "." ( CLASS | THIS | [TypeArguments] SUPER Arguments | NEW [TypeArguments] InnerCreator ) |
|
1046 |
* ] |
|
1047 |
* | BasicType BracketsOpt "." CLASS |
|
13844 | 1048 |
* } |
1049 |
* |
|
10 | 1050 |
* PrefixOp = "++" | "--" | "!" | "~" | "+" | "-" |
1051 |
* PostfixOp = "++" | "--" |
|
1052 |
* Type3 = Ident { "." Ident } [TypeArguments] {TypeSelector} BracketsOpt |
|
1053 |
* | BasicType |
|
1054 |
* TypeNoParams3 = Ident { "." Ident } BracketsOpt |
|
1055 |
* Selector = "." [TypeArguments] Ident [Arguments] |
|
1056 |
* | "." THIS |
|
1057 |
* | "." [TypeArguments] SUPER SuperSuffix |
|
1058 |
* | "." NEW [TypeArguments] InnerCreator |
|
1059 |
* | "[" Expression "]" |
|
1060 |
* TypeSelector = "." Ident [TypeArguments] |
|
1061 |
* SuperSuffix = Arguments | "." Ident [Arguments] |
|
1062 |
*/ |
|
1063 |
protected JCExpression term3() { |
|
10815 | 1064 |
int pos = token.pos; |
10 | 1065 |
JCExpression t; |
1066 |
List<JCExpression> typeArgs = typeArgumentsOpt(EXPR); |
|
10815 | 1067 |
switch (token.kind) { |
10 | 1068 |
case QUES: |
1069 |
if ((mode & TYPE) != 0 && (mode & (TYPEARG|NOPARAMS)) == TYPEARG) { |
|
1070 |
mode = TYPE; |
|
1071 |
return typeArgument(); |
|
1072 |
} else |
|
1073 |
return illegal(); |
|
1074 |
case PLUSPLUS: case SUBSUB: case BANG: case TILDE: case PLUS: case SUB: |
|
1075 |
if (typeArgs == null && (mode & EXPR) != 0) { |
|
10815 | 1076 |
TokenKind tk = token.kind; |
1077 |
nextToken(); |
|
10 | 1078 |
mode = EXPR; |
10815 | 1079 |
if (tk == SUB && |
1080 |
(token.kind == INTLITERAL || token.kind == LONGLITERAL) && |
|
1081 |
token.radix() == 10) { |
|
10 | 1082 |
mode = EXPR; |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
1083 |
t = literal(names.hyphen, pos); |
10 | 1084 |
} else { |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1085 |
t = term3(); |
10815 | 1086 |
return F.at(pos).Unary(unoptag(tk), t); |
10 | 1087 |
} |
1088 |
} else return illegal(); |
|
1089 |
break; |
|
1090 |
case LPAREN: |
|
1091 |
if (typeArgs == null && (mode & EXPR) != 0) { |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1092 |
ParensResult pres = analyzeParens(); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1093 |
switch (pres) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1094 |
case CAST: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1095 |
accept(LPAREN); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1096 |
mode = TYPE; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1097 |
int pos1 = pos; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1098 |
List<JCExpression> targets = List.of(t = term3()); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1099 |
while (token.kind == AMP) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1100 |
checkIntersectionTypesInCast(); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1101 |
accept(AMP); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1102 |
targets = targets.prepend(term3()); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1103 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1104 |
if (targets.length() > 1) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1105 |
t = toP(F.at(pos1).TypeIntersection(targets.reverse())); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1106 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1107 |
accept(RPAREN); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1108 |
mode = EXPR; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1109 |
JCExpression t1 = term3(); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1110 |
return F.at(pos).TypeCast(t, t1); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1111 |
case IMPLICIT_LAMBDA: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1112 |
case EXPLICIT_LAMBDA: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1113 |
t = lambdaExpressionOrStatement(true, pres == ParensResult.EXPLICIT_LAMBDA, pos); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1114 |
break; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1115 |
default: //PARENS |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1116 |
accept(LPAREN); |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1117 |
mode = EXPR; |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1118 |
t = termRest(term1Rest(term2Rest(term3(), TreeInfo.orPrec))); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1119 |
accept(RPAREN); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1120 |
t = toP(F.at(pos).Parens(t)); |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1121 |
break; |
10 | 1122 |
} |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1123 |
} else { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1124 |
return illegal(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1125 |
} |
10 | 1126 |
break; |
1127 |
case THIS: |
|
1128 |
if ((mode & EXPR) != 0) { |
|
1129 |
mode = EXPR; |
|
1130 |
t = to(F.at(pos).Ident(names._this)); |
|
10815 | 1131 |
nextToken(); |
10 | 1132 |
if (typeArgs == null) |
1133 |
t = argumentsOpt(null, t); |
|
1134 |
else |
|
1135 |
t = arguments(typeArgs, t); |
|
1136 |
typeArgs = null; |
|
1137 |
} else return illegal(); |
|
1138 |
break; |
|
1139 |
case SUPER: |
|
1140 |
if ((mode & EXPR) != 0) { |
|
1141 |
mode = EXPR; |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1142 |
t = to(F.at(pos).Ident(names._super)); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1143 |
t = superSuffix(typeArgs, t); |
10 | 1144 |
typeArgs = null; |
1145 |
} else return illegal(); |
|
1146 |
break; |
|
1147 |
case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: case DOUBLELITERAL: |
|
1148 |
case CHARLITERAL: case STRINGLITERAL: |
|
1149 |
case TRUE: case FALSE: case NULL: |
|
1150 |
if (typeArgs == null && (mode & EXPR) != 0) { |
|
1151 |
mode = EXPR; |
|
1152 |
t = literal(names.empty); |
|
1153 |
} else return illegal(); |
|
1154 |
break; |
|
1155 |
case NEW: |
|
1156 |
if (typeArgs != null) return illegal(); |
|
1157 |
if ((mode & EXPR) != 0) { |
|
1158 |
mode = EXPR; |
|
10815 | 1159 |
nextToken(); |
1160 |
if (token.kind == LT) typeArgs = typeArguments(false); |
|
10 | 1161 |
t = creator(pos, typeArgs); |
1162 |
typeArgs = null; |
|
1163 |
} else return illegal(); |
|
1164 |
break; |
|
15385 | 1165 |
case MONKEYS_AT: |
15718 | 1166 |
// Only annotated cast types and method references are valid |
15385 | 1167 |
List<JCAnnotation> typeAnnos = typeAnnotationsOpt(); |
1168 |
if (typeAnnos.isEmpty()) { |
|
1169 |
// else there would be no '@' |
|
1170 |
throw new AssertionError("Expected type annotations, but found none!"); |
|
1171 |
} |
|
1172 |
||
1173 |
JCExpression expr = term3(); |
|
1174 |
||
1175 |
if ((mode & TYPE) == 0) { |
|
1176 |
// Type annotations on class literals no longer legal |
|
15718 | 1177 |
switch (expr.getTag()) { |
1178 |
case REFERENCE: { |
|
1179 |
JCMemberReference mref = (JCMemberReference) expr; |
|
1180 |
mref.expr = toP(F.at(pos).AnnotatedType(typeAnnos, mref.expr)); |
|
1181 |
t = mref; |
|
1182 |
break; |
|
1183 |
} |
|
1184 |
case SELECT: { |
|
1185 |
JCFieldAccess sel = (JCFieldAccess) expr; |
|
1186 |
||
1187 |
if (sel.name != names._class) { |
|
1188 |
return illegal(); |
|
1189 |
} else { |
|
1190 |
log.error(token.pos, "no.annotations.on.dot.class"); |
|
1191 |
return expr; |
|
1192 |
} |
|
1193 |
} |
|
1194 |
default: |
|
15385 | 1195 |
return illegal(typeAnnos.head.pos); |
1196 |
} |
|
15718 | 1197 |
|
15385 | 1198 |
} else { |
1199 |
// Type annotations targeting a cast |
|
1200 |
t = insertAnnotationsToMostInner(expr, typeAnnos, false); |
|
1201 |
} |
|
1202 |
break; |
|
15367 | 1203 |
case UNDERSCORE: case IDENTIFIER: case ASSERT: case ENUM: |
10 | 1204 |
if (typeArgs != null) return illegal(); |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1205 |
if ((mode & EXPR) != 0 && peekToken(ARROW)) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1206 |
t = lambdaExpressionOrStatement(false, false, pos); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1207 |
} else { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1208 |
t = toP(F.at(token.pos).Ident(ident())); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1209 |
loop: while (true) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1210 |
pos = token.pos; |
15385 | 1211 |
final List<JCAnnotation> annos = typeAnnotationsOpt(); |
1212 |
||
1213 |
// need to report an error later if LBRACKET is for array |
|
1214 |
// index access rather than array creation level |
|
1215 |
if (!annos.isEmpty() && token.kind != LBRACKET && token.kind != ELLIPSIS) |
|
1216 |
return illegal(annos.head.pos); |
|
1217 |
||
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1218 |
switch (token.kind) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1219 |
case LBRACKET: |
10815 | 1220 |
nextToken(); |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1221 |
if (token.kind == RBRACKET) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1222 |
nextToken(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1223 |
t = bracketsOpt(t); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1224 |
t = toP(F.at(pos).TypeArray(t)); |
15385 | 1225 |
if (annos.nonEmpty()) { |
1226 |
t = toP(F.at(pos).AnnotatedType(annos, t)); |
|
1227 |
} |
|
1228 |
// .class is only allowed if there were no annotations |
|
1229 |
JCExpression nt = bracketsSuffix(t); |
|
1230 |
if (nt != t && (annos.nonEmpty() || TreeInfo.containsTypeAnnotation(t))) { |
|
1231 |
// t and nt are different if bracketsSuffix parsed a .class. |
|
1232 |
// The check for nonEmpty covers the case when the whole array is annotated. |
|
1233 |
// Helper method isAnnotated looks for annos deeply within t. |
|
1234 |
syntaxError("no.annotations.on.dot.class"); |
|
1235 |
} |
|
1236 |
t = nt; |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1237 |
} else { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1238 |
if ((mode & EXPR) != 0) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1239 |
mode = EXPR; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1240 |
JCExpression t1 = term(); |
15385 | 1241 |
if (!annos.isEmpty()) t = illegal(annos.head.pos); |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1242 |
t = to(F.at(pos).Indexed(t, t1)); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1243 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1244 |
accept(RBRACKET); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1245 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1246 |
break loop; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1247 |
case LPAREN: |
10 | 1248 |
if ((mode & EXPR) != 0) { |
1249 |
mode = EXPR; |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1250 |
t = arguments(typeArgs, t); |
15385 | 1251 |
if (!annos.isEmpty()) t = illegal(annos.head.pos); |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1252 |
typeArgs = null; |
10 | 1253 |
} |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1254 |
break loop; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1255 |
case DOT: |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1256 |
nextToken(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1257 |
int oldmode = mode; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1258 |
mode &= ~NOPARAMS; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1259 |
typeArgs = typeArgumentsOpt(EXPR); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1260 |
mode = oldmode; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1261 |
if ((mode & EXPR) != 0) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1262 |
switch (token.kind) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1263 |
case CLASS: |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1264 |
if (typeArgs != null) return illegal(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1265 |
mode = EXPR; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1266 |
t = to(F.at(pos).Select(t, names._class)); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1267 |
nextToken(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1268 |
break loop; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1269 |
case THIS: |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1270 |
if (typeArgs != null) return illegal(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1271 |
mode = EXPR; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1272 |
t = to(F.at(pos).Select(t, names._this)); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1273 |
nextToken(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1274 |
break loop; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1275 |
case SUPER: |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1276 |
mode = EXPR; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1277 |
t = to(F.at(pos).Select(t, names._super)); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1278 |
t = superSuffix(typeArgs, t); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1279 |
typeArgs = null; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1280 |
break loop; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1281 |
case NEW: |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1282 |
if (typeArgs != null) return illegal(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1283 |
mode = EXPR; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1284 |
int pos1 = token.pos; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1285 |
nextToken(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1286 |
if (token.kind == LT) typeArgs = typeArguments(false); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1287 |
t = innerCreator(pos1, typeArgs, t); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1288 |
typeArgs = null; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1289 |
break loop; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1290 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1291 |
} |
15385 | 1292 |
|
1293 |
List<JCAnnotation> tyannos = null; |
|
1294 |
if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) { |
|
1295 |
tyannos = typeAnnotationsOpt(); |
|
1296 |
} |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1297 |
// typeArgs saved for next loop iteration. |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1298 |
t = toP(F.at(pos).Select(t, ident())); |
15385 | 1299 |
if (tyannos != null && tyannos.nonEmpty()) { |
1300 |
t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t)); |
|
1301 |
} |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1302 |
break; |
15385 | 1303 |
case ELLIPSIS: |
1304 |
if (this.permitTypeAnnotationsPushBack) { |
|
1305 |
this.typeAnnotationsPushedBack = annos; |
|
1306 |
} else if (annos.nonEmpty()) { |
|
1307 |
// Don't return here -- error recovery attempt |
|
1308 |
illegal(annos.head.pos); |
|
1309 |
} |
|
1310 |
break loop; |
|
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1311 |
case LT: |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1312 |
if ((mode & TYPE) == 0 && isUnboundMemberRef()) { |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1313 |
//this is an unbound method reference whose qualifier |
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1314 |
//is a generic type i.e. A<S>::m |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1315 |
int pos1 = token.pos; |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1316 |
accept(LT); |
22163 | 1317 |
ListBuffer<JCExpression> args = new ListBuffer<>(); |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1318 |
args.append(typeArgument()); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1319 |
while (token.kind == COMMA) { |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1320 |
nextToken(); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1321 |
args.append(typeArgument()); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1322 |
} |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1323 |
accept(GT); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1324 |
t = toP(F.at(pos1).TypeApply(t, args.toList())); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1325 |
checkGenerics(); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1326 |
while (token.kind == DOT) { |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1327 |
nextToken(); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1328 |
mode = TYPE; |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1329 |
t = toP(F.at(token.pos).Select(t, ident())); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1330 |
t = typeArgumentsOpt(t); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1331 |
} |
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1332 |
t = bracketsOpt(t); |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1333 |
if (token.kind != COLCOL) { |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1334 |
//method reference expected here |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1335 |
t = illegal(); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1336 |
} |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1337 |
mode = EXPR; |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1338 |
return term3Rest(t, typeArgs); |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1339 |
} |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1340 |
break loop; |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1341 |
default: |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1342 |
break loop; |
10 | 1343 |
} |
1344 |
} |
|
1345 |
} |
|
1346 |
if (typeArgs != null) illegal(); |
|
1347 |
t = typeArgumentsOpt(t); |
|
1348 |
break; |
|
1349 |
case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT: |
|
1350 |
case DOUBLE: case BOOLEAN: |
|
1351 |
if (typeArgs != null) illegal(); |
|
1352 |
t = bracketsSuffix(bracketsOpt(basicType())); |
|
1353 |
break; |
|
1354 |
case VOID: |
|
1355 |
if (typeArgs != null) illegal(); |
|
1356 |
if ((mode & EXPR) != 0) { |
|
10815 | 1357 |
nextToken(); |
1358 |
if (token.kind == DOT) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
1359 |
JCPrimitiveTypeTree ti = toP(F.at(pos).TypeIdent(TypeTag.VOID)); |
10 | 1360 |
t = bracketsSuffix(ti); |
1361 |
} else { |
|
1362 |
return illegal(pos); |
|
1363 |
} |
|
1364 |
} else { |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1365 |
// 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
|
1366 |
// 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
|
1367 |
// The error will be reported in Attr.attribTypes or Attr.visitApply. |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
1368 |
JCPrimitiveTypeTree ti = to(F.at(pos).TypeIdent(TypeTag.VOID)); |
10815 | 1369 |
nextToken(); |
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1370 |
return ti; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
1371 |
//return illegal(); |
10 | 1372 |
} |
1373 |
break; |
|
1374 |
default: |
|
1375 |
return illegal(); |
|
1376 |
} |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1377 |
return term3Rest(t, typeArgs); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1378 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1379 |
|
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1380 |
JCExpression term3Rest(JCExpression t, List<JCExpression> typeArgs) { |
10 | 1381 |
if (typeArgs != null) illegal(); |
1382 |
while (true) { |
|
10815 | 1383 |
int pos1 = token.pos; |
15385 | 1384 |
final List<JCAnnotation> annos = typeAnnotationsOpt(); |
1385 |
||
10815 | 1386 |
if (token.kind == LBRACKET) { |
1387 |
nextToken(); |
|
10 | 1388 |
if ((mode & TYPE) != 0) { |
1389 |
int oldmode = mode; |
|
1390 |
mode = TYPE; |
|
10815 | 1391 |
if (token.kind == RBRACKET) { |
1392 |
nextToken(); |
|
7072 | 1393 |
t = bracketsOpt(t); |
10 | 1394 |
t = toP(F.at(pos1).TypeArray(t)); |
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1395 |
if (token.kind == COLCOL) { |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1396 |
mode = EXPR; |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1397 |
continue; |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1398 |
} |
15385 | 1399 |
if (annos.nonEmpty()) { |
1400 |
t = toP(F.at(pos1).AnnotatedType(annos, t)); |
|
1401 |
} |
|
10 | 1402 |
return t; |
1403 |
} |
|
1404 |
mode = oldmode; |
|
1405 |
} |
|
1406 |
if ((mode & EXPR) != 0) { |
|
1407 |
mode = EXPR; |
|
1408 |
JCExpression t1 = term(); |
|
1409 |
t = to(F.at(pos1).Indexed(t, t1)); |
|
1410 |
} |
|
1411 |
accept(RBRACKET); |
|
10815 | 1412 |
} else if (token.kind == DOT) { |
1413 |
nextToken(); |
|
10 | 1414 |
typeArgs = typeArgumentsOpt(EXPR); |
10815 | 1415 |
if (token.kind == SUPER && (mode & EXPR) != 0) { |
10 | 1416 |
mode = EXPR; |
1417 |
t = to(F.at(pos1).Select(t, names._super)); |
|
10815 | 1418 |
nextToken(); |
10 | 1419 |
t = arguments(typeArgs, t); |
1420 |
typeArgs = null; |
|
10815 | 1421 |
} else if (token.kind == NEW && (mode & EXPR) != 0) { |
10 | 1422 |
if (typeArgs != null) return illegal(); |
1423 |
mode = EXPR; |
|
10815 | 1424 |
int pos2 = token.pos; |
1425 |
nextToken(); |
|
1426 |
if (token.kind == LT) typeArgs = typeArguments(false); |
|
10 | 1427 |
t = innerCreator(pos2, typeArgs, t); |
1428 |
typeArgs = null; |
|
1429 |
} else { |
|
15385 | 1430 |
List<JCAnnotation> tyannos = null; |
1431 |
if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) { |
|
1432 |
// is the mode check needed? |
|
1433 |
tyannos = typeAnnotationsOpt(); |
|
1434 |
} |
|
10 | 1435 |
t = toP(F.at(pos1).Select(t, ident())); |
15385 | 1436 |
if (tyannos != null && tyannos.nonEmpty()) { |
1437 |
t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t)); |
|
1438 |
} |
|
10 | 1439 |
t = argumentsOpt(typeArgs, typeArgumentsOpt(t)); |
1440 |
typeArgs = null; |
|
1441 |
} |
|
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1442 |
} else if ((mode & EXPR) != 0 && token.kind == COLCOL) { |
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1443 |
mode = EXPR; |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1444 |
if (typeArgs != null) return illegal(); |
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1445 |
accept(COLCOL); |
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1446 |
t = memberReferenceSuffix(pos1, t); |
10 | 1447 |
} else { |
15385 | 1448 |
if (!annos.isEmpty()) { |
1449 |
if (permitTypeAnnotationsPushBack) |
|
1450 |
typeAnnotationsPushedBack = annos; |
|
1451 |
else |
|
1452 |
return illegal(annos.head.pos); |
|
1453 |
} |
|
10 | 1454 |
break; |
1455 |
} |
|
1456 |
} |
|
10815 | 1457 |
while ((token.kind == PLUSPLUS || token.kind == SUBSUB) && (mode & EXPR) != 0) { |
10 | 1458 |
mode = EXPR; |
10815 | 1459 |
t = to(F.at(token.pos).Unary( |
10950 | 1460 |
token.kind == PLUSPLUS ? POSTINC : POSTDEC, t)); |
10815 | 1461 |
nextToken(); |
10 | 1462 |
} |
1463 |
return toP(t); |
|
1464 |
} |
|
1465 |
||
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1466 |
/** |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1467 |
* If we see an identifier followed by a '<' it could be an unbound |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1468 |
* method reference or a binary expression. To disambiguate, look for a |
15718 | 1469 |
* matching '>' and see if the subsequent terminal is either '.' or '::'. |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1470 |
*/ |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1471 |
@SuppressWarnings("fallthrough") |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1472 |
boolean isUnboundMemberRef() { |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1473 |
int pos = 0, depth = 0; |
15718 | 1474 |
outer: for (Token t = S.token(pos) ; ; t = S.token(++pos)) { |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1475 |
switch (t.kind) { |
15367 | 1476 |
case IDENTIFIER: case UNDERSCORE: case QUES: case EXTENDS: case SUPER: |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1477 |
case DOT: case RBRACKET: case LBRACKET: case COMMA: |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1478 |
case BYTE: case SHORT: case INT: case LONG: case FLOAT: |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1479 |
case DOUBLE: case BOOLEAN: case CHAR: |
15718 | 1480 |
case MONKEYS_AT: |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1481 |
break; |
15718 | 1482 |
|
1483 |
case LPAREN: |
|
1484 |
// skip annotation values |
|
1485 |
int nesting = 0; |
|
1486 |
for (; ; pos++) { |
|
1487 |
TokenKind tk2 = S.token(pos).kind; |
|
1488 |
switch (tk2) { |
|
1489 |
case EOF: |
|
1490 |
return false; |
|
1491 |
case LPAREN: |
|
1492 |
nesting++; |
|
1493 |
break; |
|
1494 |
case RPAREN: |
|
1495 |
nesting--; |
|
1496 |
if (nesting == 0) { |
|
1497 |
continue outer; |
|
1498 |
} |
|
1499 |
break; |
|
1500 |
} |
|
1501 |
} |
|
1502 |
||
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1503 |
case LT: |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1504 |
depth++; break; |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1505 |
case GTGTGT: |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1506 |
depth--; |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1507 |
case GTGT: |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1508 |
depth--; |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1509 |
case GT: |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1510 |
depth--; |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1511 |
if (depth == 0) { |
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1512 |
TokenKind nextKind = S.token(pos + 1).kind; |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1513 |
return |
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1514 |
nextKind == TokenKind.DOT || |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1515 |
nextKind == TokenKind.LBRACKET || |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1516 |
nextKind == TokenKind.COLCOL; |
11322
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1517 |
} |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1518 |
break; |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1519 |
default: |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1520 |
return false; |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1521 |
} |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1522 |
} |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1523 |
} |
6ee85f80967a
7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents:
11149
diff
changeset
|
1524 |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1525 |
/** |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1526 |
* If we see an identifier followed by a '<' it could be an unbound |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1527 |
* method reference or a binary expression. To disambiguate, look for a |
15718 | 1528 |
* matching '>' and see if the subsequent terminal is either '.' or '::'. |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1529 |
*/ |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1530 |
@SuppressWarnings("fallthrough") |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1531 |
ParensResult analyzeParens() { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1532 |
int depth = 0; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1533 |
boolean type = false; |
15385 | 1534 |
outer: for (int lookahead = 0 ; ; lookahead++) { |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1535 |
TokenKind tk = S.token(lookahead).kind; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1536 |
switch (tk) { |
17803
d73ddda4575f
8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents:
17578
diff
changeset
|
1537 |
case COMMA: |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1538 |
type = true; |
17803
d73ddda4575f
8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents:
17578
diff
changeset
|
1539 |
case EXTENDS: case SUPER: case DOT: case AMP: |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1540 |
//skip |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1541 |
break; |
17803
d73ddda4575f
8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents:
17578
diff
changeset
|
1542 |
case QUES: |
d73ddda4575f
8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents:
17578
diff
changeset
|
1543 |
if (peekToken(lookahead, EXTENDS) || |
d73ddda4575f
8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents:
17578
diff
changeset
|
1544 |
peekToken(lookahead, SUPER)) { |
d73ddda4575f
8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents:
17578
diff
changeset
|
1545 |
//wildcards |
d73ddda4575f
8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents:
17578
diff
changeset
|
1546 |
type = true; |
d73ddda4575f
8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents:
17578
diff
changeset
|
1547 |
} |
d73ddda4575f
8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents:
17578
diff
changeset
|
1548 |
break; |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1549 |
case BYTE: case SHORT: case INT: case LONG: case FLOAT: |
22155
2f0a83a98593
8028235: Better error recovery for parsing 'void' as a type of the lambda parameter
jlahoda
parents:
22004
diff
changeset
|
1550 |
case DOUBLE: case BOOLEAN: case CHAR: case VOID: |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1551 |
if (peekToken(lookahead, RPAREN)) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1552 |
//Type, ')' -> cast |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1553 |
return ParensResult.CAST; |
15367 | 1554 |
} else if (peekToken(lookahead, LAX_IDENTIFIER)) { |
1555 |
//Type, Identifier/'_'/'assert'/'enum' -> explicit lambda |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1556 |
return ParensResult.EXPLICIT_LAMBDA; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1557 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1558 |
break; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1559 |
case LPAREN: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1560 |
if (lookahead != 0) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1561 |
// '(' in a non-starting position -> parens |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1562 |
return ParensResult.PARENS; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1563 |
} else if (peekToken(lookahead, RPAREN)) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1564 |
// '(', ')' -> explicit lambda |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1565 |
return ParensResult.EXPLICIT_LAMBDA; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1566 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1567 |
break; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1568 |
case RPAREN: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1569 |
// if we have seen something that looks like a type, |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1570 |
// then it's a cast expression |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1571 |
if (type) return ParensResult.CAST; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1572 |
// otherwise, disambiguate cast vs. parenthesized expression |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1573 |
// based on subsequent token. |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1574 |
switch (S.token(lookahead + 1).kind) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1575 |
/*case PLUSPLUS: case SUBSUB: */ |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1576 |
case BANG: case TILDE: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1577 |
case LPAREN: case THIS: case SUPER: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1578 |
case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1579 |
case DOUBLELITERAL: case CHARLITERAL: case STRINGLITERAL: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1580 |
case TRUE: case FALSE: case NULL: |
15367 | 1581 |
case NEW: case IDENTIFIER: case ASSERT: case ENUM: case UNDERSCORE: |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1582 |
case BYTE: case SHORT: case CHAR: case INT: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1583 |
case LONG: case FLOAT: case DOUBLE: case BOOLEAN: case VOID: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1584 |
return ParensResult.CAST; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1585 |
default: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1586 |
return ParensResult.PARENS; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1587 |
} |
15367 | 1588 |
case UNDERSCORE: |
1589 |
case ASSERT: |
|
1590 |
case ENUM: |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1591 |
case IDENTIFIER: |
15367 | 1592 |
if (peekToken(lookahead, LAX_IDENTIFIER)) { |
1593 |
// Identifier, Identifier/'_'/'assert'/'enum' -> explicit lambda |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1594 |
return ParensResult.EXPLICIT_LAMBDA; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1595 |
} else if (peekToken(lookahead, RPAREN, ARROW)) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1596 |
// Identifier, ')' '->' -> implicit lambda |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1597 |
return ParensResult.IMPLICIT_LAMBDA; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1598 |
} |
22004
203318f0799d
8029179: javac produces a compile error for valid boolean expressions
vromero
parents:
20609
diff
changeset
|
1599 |
type = false; |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1600 |
break; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1601 |
case FINAL: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1602 |
case ELLIPSIS: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1603 |
//those can only appear in explicit lambdas |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1604 |
return ParensResult.EXPLICIT_LAMBDA; |
15385 | 1605 |
case MONKEYS_AT: |
1606 |
type = true; |
|
1607 |
lookahead += 1; //skip '@' |
|
1608 |
while (peekToken(lookahead, DOT)) { |
|
1609 |
lookahead += 2; |
|
1610 |
} |
|
1611 |
if (peekToken(lookahead, LPAREN)) { |
|
1612 |
lookahead++; |
|
1613 |
//skip annotation values |
|
1614 |
int nesting = 0; |
|
1615 |
for (; ; lookahead++) { |
|
1616 |
TokenKind tk2 = S.token(lookahead).kind; |
|
1617 |
switch (tk2) { |
|
1618 |
case EOF: |
|
1619 |
return ParensResult.PARENS; |
|
1620 |
case LPAREN: |
|
1621 |
nesting++; |
|
1622 |
break; |
|
1623 |
case RPAREN: |
|
1624 |
nesting--; |
|
1625 |
if (nesting == 0) { |
|
1626 |
continue outer; |
|
1627 |
} |
|
1628 |
break; |
|
1629 |
} |
|
1630 |
} |
|
1631 |
} |
|
1632 |
break; |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1633 |
case LBRACKET: |
15367 | 1634 |
if (peekToken(lookahead, RBRACKET, LAX_IDENTIFIER)) { |
1635 |
// '[', ']', Identifier/'_'/'assert'/'enum' -> explicit lambda |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1636 |
return ParensResult.EXPLICIT_LAMBDA; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1637 |
} else if (peekToken(lookahead, RBRACKET, RPAREN) || |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1638 |
peekToken(lookahead, RBRACKET, AMP)) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1639 |
// '[', ']', ')' -> cast |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1640 |
// '[', ']', '&' -> cast (intersection type) |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1641 |
return ParensResult.CAST; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1642 |
} else if (peekToken(lookahead, RBRACKET)) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1643 |
//consume the ']' and skip |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1644 |
type = true; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1645 |
lookahead++; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1646 |
break; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1647 |
} else { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1648 |
return ParensResult.PARENS; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1649 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1650 |
case LT: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1651 |
depth++; break; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1652 |
case GTGTGT: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1653 |
depth--; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1654 |
case GTGT: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1655 |
depth--; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1656 |
case GT: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1657 |
depth--; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1658 |
if (depth == 0) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1659 |
if (peekToken(lookahead, RPAREN) || |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1660 |
peekToken(lookahead, AMP)) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1661 |
// '>', ')' -> cast |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1662 |
// '>', '&' -> cast |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1663 |
return ParensResult.CAST; |
15367 | 1664 |
} else if (peekToken(lookahead, LAX_IDENTIFIER, COMMA) || |
1665 |
peekToken(lookahead, LAX_IDENTIFIER, RPAREN, ARROW) || |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1666 |
peekToken(lookahead, ELLIPSIS)) { |
15367 | 1667 |
// '>', Identifier/'_'/'assert'/'enum', ',' -> explicit lambda |
1668 |
// '>', Identifier/'_'/'assert'/'enum', ')', '->' -> explicit lambda |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1669 |
// '>', '...' -> explicit lambda |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1670 |
return ParensResult.EXPLICIT_LAMBDA; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1671 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1672 |
//it looks a type, but could still be (i) a cast to generic type, |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1673 |
//(ii) an unbound method reference or (iii) an explicit lambda |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1674 |
type = true; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1675 |
break; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1676 |
} else if (depth < 0) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1677 |
//unbalanced '<', '>' - not a generic type |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1678 |
return ParensResult.PARENS; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1679 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1680 |
break; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1681 |
default: |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1682 |
//this includes EOF |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1683 |
return ParensResult.PARENS; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1684 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1685 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1686 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1687 |
|
15367 | 1688 |
/** Accepts all identifier-like tokens */ |
1689 |
Filter<TokenKind> LAX_IDENTIFIER = new Filter<TokenKind>() { |
|
1690 |
public boolean accepts(TokenKind t) { |
|
1691 |
return t == IDENTIFIER || t == UNDERSCORE || t == ASSERT || t == ENUM; |
|
1692 |
} |
|
1693 |
}; |
|
1694 |
||
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1695 |
enum ParensResult { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1696 |
CAST, |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1697 |
EXPLICIT_LAMBDA, |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1698 |
IMPLICIT_LAMBDA, |
22163 | 1699 |
PARENS |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1700 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
1701 |
|
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1702 |
JCExpression lambdaExpressionOrStatement(boolean hasParens, boolean explicitParams, int pos) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1703 |
List<JCVariableDecl> params = explicitParams ? |
15367 | 1704 |
formalParameters(true) : |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1705 |
implicitParameters(hasParens); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1706 |
|
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1707 |
return lambdaExpressionOrStatementRest(params, pos); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1708 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1709 |
|
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1710 |
JCExpression lambdaExpressionOrStatementRest(List<JCVariableDecl> args, int pos) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1711 |
checkLambda(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1712 |
accept(ARROW); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1713 |
|
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1714 |
return token.kind == LBRACE ? |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1715 |
lambdaStatement(args, pos, pos) : |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1716 |
lambdaExpression(args, pos); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1717 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1718 |
|
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1719 |
JCExpression lambdaStatement(List<JCVariableDecl> args, int pos, int pos2) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1720 |
JCBlock block = block(pos2, 0); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1721 |
return toP(F.at(pos).Lambda(args, block)); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1722 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1723 |
|
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1724 |
JCExpression lambdaExpression(List<JCVariableDecl> args, int pos) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1725 |
JCTree expr = parseExpression(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1726 |
return toP(F.at(pos).Lambda(args, expr)); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1727 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
1728 |
|
10 | 1729 |
/** SuperSuffix = Arguments | "." [TypeArguments] Ident [Arguments] |
1730 |
*/ |
|
1731 |
JCExpression superSuffix(List<JCExpression> typeArgs, JCExpression t) { |
|
10815 | 1732 |
nextToken(); |
1733 |
if (token.kind == LPAREN || typeArgs != null) { |
|
10 | 1734 |
t = arguments(typeArgs, t); |
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1735 |
} else if (token.kind == COLCOL) { |
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1736 |
if (typeArgs != null) return illegal(); |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1737 |
t = memberReferenceSuffix(t); |
10 | 1738 |
} else { |
10815 | 1739 |
int pos = token.pos; |
10 | 1740 |
accept(DOT); |
10815 | 1741 |
typeArgs = (token.kind == LT) ? typeArguments(false) : null; |
10 | 1742 |
t = toP(F.at(pos).Select(t, ident())); |
1743 |
t = argumentsOpt(typeArgs, t); |
|
1744 |
} |
|
1745 |
return t; |
|
1746 |
} |
|
1747 |
||
1748 |
/** BasicType = BYTE | SHORT | CHAR | INT | LONG | FLOAT | DOUBLE | BOOLEAN |
|
1749 |
*/ |
|
1750 |
JCPrimitiveTypeTree basicType() { |
|
10815 | 1751 |
JCPrimitiveTypeTree t = to(F.at(token.pos).TypeIdent(typetag(token.kind))); |
1752 |
nextToken(); |
|
10 | 1753 |
return t; |
1754 |
} |
|
1755 |
||
1756 |
/** ArgumentsOpt = [ Arguments ] |
|
1757 |
*/ |
|
1758 |
JCExpression argumentsOpt(List<JCExpression> typeArgs, JCExpression t) { |
|
10815 | 1759 |
if ((mode & EXPR) != 0 && token.kind == LPAREN || typeArgs != null) { |
10 | 1760 |
mode = EXPR; |
1761 |
return arguments(typeArgs, t); |
|
1762 |
} else { |
|
1763 |
return t; |
|
1764 |
} |
|
1765 |
} |
|
1766 |
||
1767 |
/** Arguments = "(" [Expression { COMMA Expression }] ")" |
|
1768 |
*/ |
|
1769 |
List<JCExpression> arguments() { |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19122
diff
changeset
|
1770 |
ListBuffer<JCExpression> args = new ListBuffer<>(); |
10815 | 1771 |
if (token.kind == LPAREN) { |
1772 |
nextToken(); |
|
1773 |
if (token.kind != RPAREN) { |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1774 |
args.append(parseExpression()); |
10815 | 1775 |
while (token.kind == COMMA) { |
1776 |
nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1777 |
args.append(parseExpression()); |
10 | 1778 |
} |
1779 |
} |
|
1780 |
accept(RPAREN); |
|
1781 |
} else { |
|
10815 | 1782 |
syntaxError(token.pos, "expected", LPAREN); |
10 | 1783 |
} |
1784 |
return args.toList(); |
|
1785 |
} |
|
1786 |
||
1787 |
JCMethodInvocation arguments(List<JCExpression> typeArgs, JCExpression t) { |
|
10815 | 1788 |
int pos = token.pos; |
10 | 1789 |
List<JCExpression> args = arguments(); |
1790 |
return toP(F.at(pos).Apply(typeArgs, t, args)); |
|
1791 |
} |
|
1792 |
||
1793 |
/** TypeArgumentsOpt = [ TypeArguments ] |
|
1794 |
*/ |
|
1795 |
JCExpression typeArgumentsOpt(JCExpression t) { |
|
10815 | 1796 |
if (token.kind == LT && |
10 | 1797 |
(mode & TYPE) != 0 && |
1798 |
(mode & NOPARAMS) == 0) { |
|
1799 |
mode = TYPE; |
|
1800 |
checkGenerics(); |
|
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1801 |
return typeArguments(t, false); |
10 | 1802 |
} else { |
1803 |
return t; |
|
1804 |
} |
|
1805 |
} |
|
1806 |
List<JCExpression> typeArgumentsOpt() { |
|
1807 |
return typeArgumentsOpt(TYPE); |
|
1808 |
} |
|
1809 |
||
1810 |
List<JCExpression> typeArgumentsOpt(int useMode) { |
|
10815 | 1811 |
if (token.kind == LT) { |
10 | 1812 |
checkGenerics(); |
1813 |
if ((mode & useMode) == 0 || |
|
1814 |
(mode & NOPARAMS) != 0) { |
|
1815 |
illegal(); |
|
1816 |
} |
|
1817 |
mode = useMode; |
|
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1818 |
return typeArguments(false); |
10 | 1819 |
} |
1820 |
return null; |
|
1821 |
} |
|
1822 |
||
13844 | 1823 |
/** |
1824 |
* {@literal |
|
1825 |
* TypeArguments = "<" TypeArgument {"," TypeArgument} ">" |
|
1826 |
* } |
|
10 | 1827 |
*/ |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1828 |
List<JCExpression> typeArguments(boolean diamondAllowed) { |
10815 | 1829 |
if (token.kind == LT) { |
1830 |
nextToken(); |
|
1831 |
if (token.kind == GT && diamondAllowed) { |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1832 |
checkDiamond(); |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1833 |
mode |= DIAMOND; |
10815 | 1834 |
nextToken(); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
1835 |
return List.nil(); |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1836 |
} else { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19122
diff
changeset
|
1837 |
ListBuffer<JCExpression> args = new ListBuffer<>(); |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
1838 |
args.append(((mode & EXPR) == 0) ? typeArgument() : parseType()); |
10815 | 1839 |
while (token.kind == COMMA) { |
1840 |
nextToken(); |
|
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1841 |
args.append(((mode & EXPR) == 0) ? typeArgument() : parseType()); |
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1842 |
} |
10815 | 1843 |
switch (token.kind) { |
1844 |
||
1845 |
case GTGTGTEQ: case GTGTEQ: case GTEQ: |
|
1846 |
case GTGTGT: case GTGT: |
|
1847 |
token = S.split(); |
|
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1848 |
break; |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
1849 |
case GT: |
10815 | 1850 |
nextToken(); |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
1851 |
break; |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1852 |
default: |
10815 | 1853 |
args.append(syntaxError(token.pos, "expected", GT)); |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1854 |
break; |
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1855 |
} |
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1856 |
return args.toList(); |
10 | 1857 |
} |
1858 |
} else { |
|
10815 | 1859 |
return List.<JCExpression>of(syntaxError(token.pos, "expected", LT)); |
10 | 1860 |
} |
1861 |
} |
|
1862 |
||
13844 | 1863 |
/** |
1864 |
* {@literal |
|
1865 |
* TypeArgument = Type |
|
15385 | 1866 |
* | [Annotations] "?" |
1867 |
* | [Annotations] "?" EXTENDS Type {"&" Type} |
|
1868 |
* | [Annotations] "?" SUPER Type |
|
13844 | 1869 |
* } |
10 | 1870 |
*/ |
1871 |
JCExpression typeArgument() { |
|
15385 | 1872 |
List<JCAnnotation> annotations = typeAnnotationsOpt(); |
1873 |
if (token.kind != QUES) return parseType(annotations); |
|
10815 | 1874 |
int pos = token.pos; |
1875 |
nextToken(); |
|
15385 | 1876 |
JCExpression result; |
10815 | 1877 |
if (token.kind == EXTENDS) { |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1878 |
TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.EXTENDS)); |
10815 | 1879 |
nextToken(); |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1880 |
JCExpression bound = parseType(); |
15385 | 1881 |
result = F.at(pos).Wildcard(t, bound); |
10815 | 1882 |
} else if (token.kind == SUPER) { |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1883 |
TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.SUPER)); |
10815 | 1884 |
nextToken(); |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1885 |
JCExpression bound = parseType(); |
15385 | 1886 |
result = F.at(pos).Wildcard(t, bound); |
15367 | 1887 |
} else if (LAX_IDENTIFIER.accepts(token.kind)) { |
10 | 1888 |
//error recovery |
1889 |
TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND); |
|
1890 |
JCExpression wc = toP(F.at(pos).Wildcard(t, null)); |
|
10815 | 1891 |
JCIdent id = toP(F.at(token.pos).Ident(ident())); |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
1892 |
JCErroneous err = F.at(pos).Erroneous(List.<JCTree>of(wc, id)); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
1893 |
reportSyntaxError(err, "expected3", GT, EXTENDS, SUPER); |
15385 | 1894 |
result = err; |
10 | 1895 |
} else { |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
1896 |
TypeBoundKind t = toP(F.at(pos).TypeBoundKind(BoundKind.UNBOUND)); |
15385 | 1897 |
result = toP(F.at(pos).Wildcard(t, null)); |
10 | 1898 |
} |
15385 | 1899 |
if (!annotations.isEmpty()) { |
1900 |
result = toP(F.at(annotations.head.pos).AnnotatedType(annotations,result)); |
|
1901 |
} |
|
1902 |
return result; |
|
10 | 1903 |
} |
1904 |
||
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1905 |
JCTypeApply typeArguments(JCExpression t, boolean diamondAllowed) { |
10815 | 1906 |
int pos = token.pos; |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
1907 |
List<JCExpression> args = typeArguments(diamondAllowed); |
10 | 1908 |
return toP(F.at(pos).TypeApply(t, args)); |
1909 |
} |
|
1910 |
||
15385 | 1911 |
/** |
1912 |
* BracketsOpt = { [Annotations] "[" "]" }* |
|
1913 |
* |
|
1914 |
* <p> |
|
1915 |
* |
|
1916 |
* <code>annotations</code> is the list of annotations targeting |
|
1917 |
* the expression <code>t</code>. |
|
10 | 1918 |
*/ |
15385 | 1919 |
private JCExpression bracketsOpt(JCExpression t, |
1920 |
List<JCAnnotation> annotations) { |
|
1921 |
List<JCAnnotation> nextLevelAnnotations = typeAnnotationsOpt(); |
|
1922 |
||
10815 | 1923 |
if (token.kind == LBRACKET) { |
1924 |
int pos = token.pos; |
|
1925 |
nextToken(); |
|
15385 | 1926 |
t = bracketsOptCont(t, pos, nextLevelAnnotations); |
1927 |
} else if (!nextLevelAnnotations.isEmpty()) { |
|
1928 |
if (permitTypeAnnotationsPushBack) { |
|
1929 |
this.typeAnnotationsPushedBack = nextLevelAnnotations; |
|
1930 |
} else { |
|
1931 |
return illegal(nextLevelAnnotations.head.pos); |
|
1932 |
} |
|
1933 |
} |
|
1934 |
||
1935 |
if (!annotations.isEmpty()) { |
|
1936 |
t = toP(F.at(token.pos).AnnotatedType(annotations, t)); |
|
10 | 1937 |
} |
1938 |
return t; |
|
1939 |
} |
|
1940 |
||
15385 | 1941 |
/** BracketsOpt = [ "[" "]" { [Annotations] "[" "]"} ] |
1942 |
*/ |
|
1943 |
private JCExpression bracketsOpt(JCExpression t) { |
|
1944 |
return bracketsOpt(t, List.<JCAnnotation>nil()); |
|
1945 |
} |
|
1946 |
||
1947 |
private JCExpression bracketsOptCont(JCExpression t, int pos, |
|
1948 |
List<JCAnnotation> annotations) { |
|
10 | 1949 |
accept(RBRACKET); |
7072 | 1950 |
t = bracketsOpt(t); |
15385 | 1951 |
t = toP(F.at(pos).TypeArray(t)); |
1952 |
if (annotations.nonEmpty()) { |
|
1953 |
t = toP(F.at(pos).AnnotatedType(annotations, t)); |
|
1954 |
} |
|
1955 |
return t; |
|
10 | 1956 |
} |
1957 |
||
1958 |
/** BracketsSuffixExpr = "." CLASS |
|
1959 |
* BracketsSuffixType = |
|
1960 |
*/ |
|
1961 |
JCExpression bracketsSuffix(JCExpression t) { |
|
10815 | 1962 |
if ((mode & EXPR) != 0 && token.kind == DOT) { |
10 | 1963 |
mode = EXPR; |
10815 | 1964 |
int pos = token.pos; |
1965 |
nextToken(); |
|
10 | 1966 |
accept(CLASS); |
11055 | 1967 |
if (token.pos == endPosTable.errorEndPos) { |
10 | 1968 |
// error recovery |
15385 | 1969 |
Name name; |
15367 | 1970 |
if (LAX_IDENTIFIER.accepts(token.kind)) { |
10815 | 1971 |
name = token.name(); |
1972 |
nextToken(); |
|
10 | 1973 |
} else { |
1974 |
name = names.error; |
|
1975 |
} |
|
1976 |
t = F.at(pos).Erroneous(List.<JCTree>of(toP(F.at(pos).Select(t, name)))); |
|
1977 |
} else { |
|
1978 |
t = toP(F.at(pos).Select(t, names._class)); |
|
1979 |
} |
|
1980 |
} else if ((mode & TYPE) != 0) { |
|
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1981 |
if (token.kind != COLCOL) { |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1982 |
mode = TYPE; |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1983 |
} |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1984 |
} else if (token.kind != COLCOL) { |
10815 | 1985 |
syntaxError(token.pos, "dot.class.expected"); |
10 | 1986 |
} |
1987 |
return t; |
|
1988 |
} |
|
1989 |
||
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1990 |
/** |
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1991 |
* MemberReferenceSuffix = "::" [TypeArguments] Ident |
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1992 |
* | "::" [TypeArguments] "new" |
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1993 |
*/ |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1994 |
JCExpression memberReferenceSuffix(JCExpression t) { |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1995 |
int pos1 = token.pos; |
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
1996 |
accept(COLCOL); |
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1997 |
return memberReferenceSuffix(pos1, t); |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1998 |
} |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
1999 |
|
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2000 |
JCExpression memberReferenceSuffix(int pos1, JCExpression t) { |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2001 |
checkMethodReferences(); |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2002 |
mode = EXPR; |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2003 |
List<JCExpression> typeArgs = null; |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2004 |
if (token.kind == LT) { |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2005 |
typeArgs = typeArguments(false); |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2006 |
} |
15385 | 2007 |
Name refName; |
2008 |
ReferenceMode refMode; |
|
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2009 |
if (token.kind == NEW) { |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2010 |
refMode = ReferenceMode.NEW; |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2011 |
refName = names.init; |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2012 |
nextToken(); |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2013 |
} else { |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2014 |
refMode = ReferenceMode.INVOKE; |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2015 |
refName = ident(); |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2016 |
} |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2017 |
return toP(F.at(t.getStartPosition()).Reference(refMode, refName, t, typeArgs)); |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2018 |
} |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
2019 |
|
15385 | 2020 |
/** Creator = [Annotations] Qualident [TypeArguments] ( ArrayCreatorRest | ClassCreatorRest ) |
10 | 2021 |
*/ |
2022 |
JCExpression creator(int newpos, List<JCExpression> typeArgs) { |
|
17578 | 2023 |
List<JCAnnotation> newAnnotations = annotationsOpt(Tag.ANNOTATION); |
15385 | 2024 |
|
10815 | 2025 |
switch (token.kind) { |
10 | 2026 |
case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT: |
2027 |
case DOUBLE: case BOOLEAN: |
|
15385 | 2028 |
if (typeArgs == null) { |
2029 |
if (newAnnotations.isEmpty()) { |
|
2030 |
return arrayCreatorRest(newpos, basicType()); |
|
2031 |
} else { |
|
2032 |
return arrayCreatorRest(newpos, toP(F.at(newAnnotations.head.pos).AnnotatedType(newAnnotations, basicType()))); |
|
2033 |
} |
|
2034 |
} |
|
10 | 2035 |
break; |
2036 |
default: |
|
2037 |
} |
|
15385 | 2038 |
JCExpression t = qualident(true); |
2039 |
||
10 | 2040 |
int oldmode = mode; |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2041 |
mode = TYPE; |
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2042 |
boolean diamondFound = false; |
10199
48bd09ecc88a
7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents:
9300
diff
changeset
|
2043 |
int lastTypeargsPos = -1; |
10815 | 2044 |
if (token.kind == LT) { |
10 | 2045 |
checkGenerics(); |
10815 | 2046 |
lastTypeargsPos = token.pos; |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2047 |
t = typeArguments(t, true); |
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2048 |
diamondFound = (mode & DIAMOND) != 0; |
10 | 2049 |
} |
10815 | 2050 |
while (token.kind == DOT) { |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2051 |
if (diamondFound) { |
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2052 |
//cannot select after a diamond |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
2053 |
illegal(); |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2054 |
} |
10815 | 2055 |
int pos = token.pos; |
2056 |
nextToken(); |
|
15385 | 2057 |
List<JCAnnotation> tyannos = typeAnnotationsOpt(); |
10 | 2058 |
t = toP(F.at(pos).Select(t, ident())); |
15385 | 2059 |
|
2060 |
if (tyannos != null && tyannos.nonEmpty()) { |
|
2061 |
t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t)); |
|
2062 |
} |
|
2063 |
||
10815 | 2064 |
if (token.kind == LT) { |
2065 |
lastTypeargsPos = token.pos; |
|
10 | 2066 |
checkGenerics(); |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2067 |
t = typeArguments(t, true); |
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2068 |
diamondFound = (mode & DIAMOND) != 0; |
10 | 2069 |
} |
2070 |
} |
|
2071 |
mode = oldmode; |
|
15385 | 2072 |
if (token.kind == LBRACKET || token.kind == MONKEYS_AT) { |
17578 | 2073 |
// handle type annotations for non primitive arrays |
2074 |
if (newAnnotations.nonEmpty()) { |
|
2075 |
t = insertAnnotationsToMostInner(t, newAnnotations, false); |
|
2076 |
} |
|
2077 |
||
10 | 2078 |
JCExpression e = arrayCreatorRest(newpos, t); |
10199
48bd09ecc88a
7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents:
9300
diff
changeset
|
2079 |
if (diamondFound) { |
48bd09ecc88a
7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents:
9300
diff
changeset
|
2080 |
reportSyntaxError(lastTypeargsPos, "cannot.create.array.with.diamond"); |
48bd09ecc88a
7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents:
9300
diff
changeset
|
2081 |
return toP(F.at(newpos).Erroneous(List.of(e))); |
48bd09ecc88a
7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents:
9300
diff
changeset
|
2082 |
} |
48bd09ecc88a
7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents:
9300
diff
changeset
|
2083 |
else if (typeArgs != null) { |
10 | 2084 |
int pos = newpos; |
2085 |
if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) { |
|
2086 |
// note: this should always happen but we should |
|
2087 |
// not rely on this as the parser is continuously |
|
2088 |
// modified to improve error recovery. |
|
2089 |
pos = typeArgs.head.pos; |
|
2090 |
} |
|
10815 | 2091 |
setErrorEndPos(S.prevToken().endPos); |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
2092 |
JCErroneous err = F.at(pos).Erroneous(typeArgs.prepend(e)); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
2093 |
reportSyntaxError(err, "cannot.create.array.with.type.arguments"); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
2094 |
return toP(err); |
10 | 2095 |
} |
2096 |
return e; |
|
10815 | 2097 |
} else if (token.kind == LPAREN) { |
15385 | 2098 |
JCNewClass newClass = classCreatorRest(newpos, null, typeArgs, t); |
2099 |
if (newClass.def != null) { |
|
2100 |
assert newClass.def.mods.annotations.isEmpty(); |
|
2101 |
if (newAnnotations.nonEmpty()) { |
|
17578 | 2102 |
// Add type and declaration annotations to the new class; |
2103 |
// com.sun.tools.javac.code.TypeAnnotations.TypeAnnotationPositions.visitNewClass(JCNewClass) |
|
2104 |
// will later remove all type annotations and only leave the |
|
2105 |
// declaration annotations. |
|
15385 | 2106 |
newClass.def.mods.pos = earlier(newClass.def.mods.pos, newAnnotations.head.pos); |
17578 | 2107 |
newClass.def.mods.annotations = newAnnotations; |
2108 |
} |
|
2109 |
} else { |
|
2110 |
// handle type annotations for instantiations |
|
2111 |
if (newAnnotations.nonEmpty()) { |
|
2112 |
t = insertAnnotationsToMostInner(t, newAnnotations, false); |
|
2113 |
newClass.clazz = t; |
|
15385 | 2114 |
} |
2115 |
} |
|
2116 |
return newClass; |
|
10 | 2117 |
} else { |
10815 | 2118 |
setErrorEndPos(token.pos); |
2119 |
reportSyntaxError(token.pos, "expected2", LPAREN, LBRACKET); |
|
10 | 2120 |
t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.<JCExpression>nil(), null)); |
2121 |
return toP(F.at(newpos).Erroneous(List.<JCTree>of(t))); |
|
2122 |
} |
|
2123 |
} |
|
2124 |
||
15385 | 2125 |
/** InnerCreator = [Annotations] Ident [TypeArguments] ClassCreatorRest |
10 | 2126 |
*/ |
2127 |
JCExpression innerCreator(int newpos, List<JCExpression> typeArgs, JCExpression encl) { |
|
15385 | 2128 |
List<JCAnnotation> newAnnotations = typeAnnotationsOpt(); |
2129 |
||
10815 | 2130 |
JCExpression t = toP(F.at(token.pos).Ident(ident())); |
15385 | 2131 |
|
2132 |
if (newAnnotations.nonEmpty()) { |
|
2133 |
t = toP(F.at(newAnnotations.head.pos).AnnotatedType(newAnnotations, t)); |
|
2134 |
} |
|
2135 |
||
10815 | 2136 |
if (token.kind == LT) { |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
2137 |
int oldmode = mode; |
10 | 2138 |
checkGenerics(); |
9073
a29adf86aa0c
7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents:
8625
diff
changeset
|
2139 |
t = typeArguments(t, true); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
2140 |
mode = oldmode; |
10 | 2141 |
} |
2142 |
return classCreatorRest(newpos, encl, typeArgs, t); |
|
2143 |
} |
|
2144 |
||
15385 | 2145 |
/** ArrayCreatorRest = [Annotations] "[" ( "]" BracketsOpt ArrayInitializer |
2146 |
* | Expression "]" {[Annotations] "[" Expression "]"} BracketsOpt ) |
|
10 | 2147 |
*/ |
2148 |
JCExpression arrayCreatorRest(int newpos, JCExpression elemtype) { |
|
15385 | 2149 |
List<JCAnnotation> annos = typeAnnotationsOpt(); |
2150 |
||
10 | 2151 |
accept(LBRACKET); |
10815 | 2152 |
if (token.kind == RBRACKET) { |
10 | 2153 |
accept(RBRACKET); |
15385 | 2154 |
elemtype = bracketsOpt(elemtype, annos); |
10815 | 2155 |
if (token.kind == LBRACE) { |
15385 | 2156 |
JCNewArray na = (JCNewArray)arrayInitializer(newpos, elemtype); |
2157 |
if (annos.nonEmpty()) { |
|
2158 |
// when an array initializer is present then |
|
2159 |
// the parsed annotations should target the |
|
2160 |
// new array tree |
|
2161 |
// bracketsOpt inserts the annotation in |
|
2162 |
// elemtype, and it needs to be corrected |
|
2163 |
// |
|
2164 |
JCAnnotatedType annotated = (JCAnnotatedType)elemtype; |
|
2165 |
assert annotated.annotations == annos; |
|
2166 |
na.annotations = annotated.annotations; |
|
2167 |
na.elemtype = annotated.underlyingType; |
|
2168 |
} |
|
2169 |
return na; |
|
10 | 2170 |
} else { |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
2171 |
JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.<JCExpression>nil(), null)); |
10815 | 2172 |
return syntaxError(token.pos, List.<JCTree>of(t), "array.dimension.missing"); |
10 | 2173 |
} |
2174 |
} else { |
|
22163 | 2175 |
ListBuffer<JCExpression> dims = new ListBuffer<>(); |
15385 | 2176 |
|
2177 |
// maintain array dimension type annotations |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19122
diff
changeset
|
2178 |
ListBuffer<List<JCAnnotation>> dimAnnotations = new ListBuffer<>(); |
15385 | 2179 |
dimAnnotations.append(annos); |
2180 |
||
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2181 |
dims.append(parseExpression()); |
10 | 2182 |
accept(RBRACKET); |
15385 | 2183 |
while (token.kind == LBRACKET |
2184 |
|| token.kind == MONKEYS_AT) { |
|
2185 |
List<JCAnnotation> maybeDimAnnos = typeAnnotationsOpt(); |
|
10815 | 2186 |
int pos = token.pos; |
2187 |
nextToken(); |
|
2188 |
if (token.kind == RBRACKET) { |
|
15385 | 2189 |
elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos); |
10 | 2190 |
} else { |
15385 | 2191 |
if (token.kind == RBRACKET) { // no dimension |
2192 |
elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos); |
|
2193 |
} else { |
|
2194 |
dimAnnotations.append(maybeDimAnnos); |
|
2195 |
dims.append(parseExpression()); |
|
2196 |
accept(RBRACKET); |
|
2197 |
} |
|
10 | 2198 |
} |
2199 |
} |
|
15385 | 2200 |
|
2201 |
JCNewArray na = toP(F.at(newpos).NewArray(elemtype, dims.toList(), null)); |
|
2202 |
na.dimAnnotations = dimAnnotations.toList(); |
|
2203 |
return na; |
|
10 | 2204 |
} |
2205 |
} |
|
2206 |
||
2207 |
/** ClassCreatorRest = Arguments [ClassBody] |
|
2208 |
*/ |
|
3149 | 2209 |
JCNewClass classCreatorRest(int newpos, |
10 | 2210 |
JCExpression encl, |
2211 |
List<JCExpression> typeArgs, |
|
2212 |
JCExpression t) |
|
2213 |
{ |
|
2214 |
List<JCExpression> args = arguments(); |
|
2215 |
JCClassDecl body = null; |
|
10815 | 2216 |
if (token.kind == LBRACE) { |
2217 |
int pos = token.pos; |
|
10 | 2218 |
List<JCTree> defs = classOrInterfaceBody(names.empty, false); |
2219 |
JCModifiers mods = F.at(Position.NOPOS).Modifiers(0); |
|
2220 |
body = toP(F.at(pos).AnonymousClassDef(mods, defs)); |
|
2221 |
} |
|
2222 |
return toP(F.at(newpos).NewClass(encl, typeArgs, t, args, body)); |
|
2223 |
} |
|
2224 |
||
2225 |
/** ArrayInitializer = "{" [VariableInitializer {"," VariableInitializer}] [","] "}" |
|
2226 |
*/ |
|
2227 |
JCExpression arrayInitializer(int newpos, JCExpression t) { |
|
2228 |
accept(LBRACE); |
|
22163 | 2229 |
ListBuffer<JCExpression> elems = new ListBuffer<>(); |
10815 | 2230 |
if (token.kind == COMMA) { |
2231 |
nextToken(); |
|
2232 |
} else if (token.kind != RBRACE) { |
|
10 | 2233 |
elems.append(variableInitializer()); |
10815 | 2234 |
while (token.kind == COMMA) { |
2235 |
nextToken(); |
|
2236 |
if (token.kind == RBRACE) break; |
|
10 | 2237 |
elems.append(variableInitializer()); |
2238 |
} |
|
2239 |
} |
|
2240 |
accept(RBRACE); |
|
2241 |
return toP(F.at(newpos).NewArray(t, List.<JCExpression>nil(), elems.toList())); |
|
2242 |
} |
|
2243 |
||
2244 |
/** VariableInitializer = ArrayInitializer | Expression |
|
2245 |
*/ |
|
2246 |
public JCExpression variableInitializer() { |
|
10815 | 2247 |
return token.kind == LBRACE ? arrayInitializer(token.pos, null) : parseExpression(); |
10 | 2248 |
} |
2249 |
||
2250 |
/** ParExpression = "(" Expression ")" |
|
2251 |
*/ |
|
2252 |
JCExpression parExpression() { |
|
11055 | 2253 |
int pos = token.pos; |
10 | 2254 |
accept(LPAREN); |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2255 |
JCExpression t = parseExpression(); |
10 | 2256 |
accept(RPAREN); |
11055 | 2257 |
return toP(F.at(pos).Parens(t)); |
10 | 2258 |
} |
2259 |
||
2260 |
/** Block = "{" BlockStatements "}" |
|
2261 |
*/ |
|
2262 |
JCBlock block(int pos, long flags) { |
|
2263 |
accept(LBRACE); |
|
2264 |
List<JCStatement> stats = blockStatements(); |
|
2265 |
JCBlock t = F.at(pos).Block(flags, stats); |
|
10815 | 2266 |
while (token.kind == CASE || token.kind == DEFAULT) { |
2267 |
syntaxError("orphaned", token.kind); |
|
10 | 2268 |
switchBlockStatementGroups(); |
2269 |
} |
|
2270 |
// the Block node has a field "endpos" for first char of last token, which is |
|
2271 |
// usually but not necessarily the last char of the last token. |
|
10815 | 2272 |
t.endpos = token.pos; |
10 | 2273 |
accept(RBRACE); |
2274 |
return toP(t); |
|
2275 |
} |
|
2276 |
||
2277 |
public JCBlock block() { |
|
10815 | 2278 |
return block(token.pos, 0); |
10 | 2279 |
} |
2280 |
||
2281 |
/** BlockStatements = { BlockStatement } |
|
2282 |
* BlockStatement = LocalVariableDeclarationStatement |
|
2283 |
* | ClassOrInterfaceOrEnumDeclaration |
|
2284 |
* | [Ident ":"] Statement |
|
2285 |
* LocalVariableDeclarationStatement |
|
2286 |
* = { FINAL | '@' Annotation } Type VariableDeclarators ";" |
|
2287 |
*/ |
|
2288 |
@SuppressWarnings("fallthrough") |
|
2289 |
List<JCStatement> blockStatements() { |
|
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2290 |
//todo: skip to anchor on error(?) |
22163 | 2291 |
ListBuffer<JCStatement> stats = new ListBuffer<>(); |
10 | 2292 |
while (true) { |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2293 |
List<JCStatement> stat = blockStatement(); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2294 |
if (stat.isEmpty()) { |
10 | 2295 |
return stats.toList(); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2296 |
} else { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2297 |
if (token.pos <= endPosTable.errorEndPos) { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2298 |
skip(false, true, true, true); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2299 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2300 |
stats.addAll(stat); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2301 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2302 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2303 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2304 |
|
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2305 |
/* |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2306 |
* This method parses a statement treating it as a block, relaxing the |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2307 |
* JLS restrictions, allows us to parse more faulty code, doing so |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2308 |
* enables us to provide better and accurate diagnostics to the user. |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2309 |
*/ |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2310 |
JCStatement parseStatementAsBlock() { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2311 |
int pos = token.pos; |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2312 |
List<JCStatement> stats = blockStatement(); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2313 |
if (stats.isEmpty()) { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2314 |
JCErroneous e = F.at(pos).Erroneous(); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2315 |
error(e, "illegal.start.of.stmt"); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2316 |
return F.at(pos).Exec(e); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2317 |
} else { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2318 |
JCStatement first = stats.head; |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2319 |
String error = null; |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2320 |
switch (first.getTag()) { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2321 |
case CLASSDEF: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2322 |
error = "class.not.allowed"; |
10 | 2323 |
break; |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2324 |
case VARDEF: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2325 |
error = "variable.not.allowed"; |
10 | 2326 |
break; |
2327 |
} |
|
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2328 |
if (error != null) { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2329 |
error(first, error); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2330 |
List<JCBlock> blist = List.of(F.at(first.pos).Block(0, stats)); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2331 |
return toP(F.at(pos).Exec(F.at(first.pos).Erroneous(blist))); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2332 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2333 |
return first; |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2334 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2335 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2336 |
|
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2337 |
@SuppressWarnings("fallthrough") |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2338 |
List<JCStatement> blockStatement() { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2339 |
//todo: skip to anchor on error(?) |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2340 |
int pos = token.pos; |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2341 |
switch (token.kind) { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2342 |
case RBRACE: case CASE: case DEFAULT: case EOF: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2343 |
return List.nil(); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2344 |
case LBRACE: case IF: case FOR: case WHILE: case DO: case TRY: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2345 |
case SWITCH: case SYNCHRONIZED: case RETURN: case THROW: case BREAK: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2346 |
case CONTINUE: case SEMI: case ELSE: case FINALLY: case CATCH: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2347 |
return List.of(parseStatement()); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2348 |
case MONKEYS_AT: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2349 |
case FINAL: { |
13077 | 2350 |
Comment dc = token.comment(CommentStyle.JAVADOC); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2351 |
JCModifiers mods = modifiersOpt(); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2352 |
if (token.kind == INTERFACE || |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2353 |
token.kind == CLASS || |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2354 |
allowEnums && token.kind == ENUM) { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2355 |
return List.of(classOrInterfaceOrEnumDeclaration(mods, dc)); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2356 |
} else { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2357 |
JCExpression t = parseType(); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2358 |
ListBuffer<JCStatement> stats = |
22165
ec53c8946fc2
8030807: langtools should still build using jdk 7
vromero
parents:
22163
diff
changeset
|
2359 |
variableDeclarators(mods, t, new ListBuffer<JCStatement>()); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2360 |
// A "LocalVariableDeclarationStatement" subsumes the terminating semicolon |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
2361 |
storeEnd(stats.last(), token.endPos); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2362 |
accept(SEMI); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2363 |
return stats.toList(); |
10 | 2364 |
} |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2365 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2366 |
case ABSTRACT: case STRICTFP: { |
13077 | 2367 |
Comment dc = token.comment(CommentStyle.JAVADOC); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2368 |
JCModifiers mods = modifiersOpt(); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2369 |
return List.of(classOrInterfaceOrEnumDeclaration(mods, dc)); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2370 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2371 |
case INTERFACE: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2372 |
case CLASS: |
13077 | 2373 |
Comment dc = token.comment(CommentStyle.JAVADOC); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2374 |
return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc)); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2375 |
case ENUM: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2376 |
case ASSERT: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2377 |
if (allowEnums && token.kind == ENUM) { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2378 |
error(token.pos, "local.enum"); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2379 |
dc = token.comment(CommentStyle.JAVADOC); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2380 |
return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc)); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2381 |
} else if (allowAsserts && token.kind == ASSERT) { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2382 |
return List.of(parseStatement()); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2383 |
} |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2384 |
/* fall through to default */ |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2385 |
default: |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2386 |
Token prevToken = token; |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2387 |
JCExpression t = term(EXPR | TYPE); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2388 |
if (token.kind == COLON && t.hasTag(IDENT)) { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2389 |
nextToken(); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2390 |
JCStatement stat = parseStatement(); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2391 |
return List.<JCStatement>of(F.at(pos).Labelled(prevToken.name(), stat)); |
15367 | 2392 |
} else if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) { |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2393 |
pos = token.pos; |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2394 |
JCModifiers mods = F.at(Position.NOPOS).Modifiers(0); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2395 |
F.at(pos); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2396 |
ListBuffer<JCStatement> stats = |
22165
ec53c8946fc2
8030807: langtools should still build using jdk 7
vromero
parents:
22163
diff
changeset
|
2397 |
variableDeclarators(mods, t, new ListBuffer<JCStatement>()); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2398 |
// A "LocalVariableDeclarationStatement" subsumes the terminating semicolon |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
2399 |
storeEnd(stats.last(), token.endPos); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2400 |
accept(SEMI); |
10 | 2401 |
return stats.toList(); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2402 |
} else { |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2403 |
// This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2404 |
JCExpressionStatement expr = to(F.at(pos).Exec(checkExprStat(t))); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2405 |
accept(SEMI); |
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2406 |
return List.<JCStatement>of(expr); |
10 | 2407 |
} |
2408 |
} |
|
2409 |
} |
|
2410 |
||
2411 |
/** Statement = |
|
2412 |
* Block |
|
2413 |
* | IF ParExpression Statement [ELSE Statement] |
|
2414 |
* | FOR "(" ForInitOpt ";" [Expression] ";" ForUpdateOpt ")" Statement |
|
2415 |
* | FOR "(" FormalParameter : Expression ")" Statement |
|
2416 |
* | WHILE ParExpression Statement |
|
2417 |
* | DO Statement WHILE ParExpression ";" |
|
2418 |
* | TRY Block ( Catches | [Catches] FinallyPart ) |
|
8234
5050975e2df0
7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources
darcy
parents:
8224
diff
changeset
|
2419 |
* | TRY "(" ResourceSpecification ";"opt ")" Block [Catches] [FinallyPart] |
10 | 2420 |
* | SWITCH ParExpression "{" SwitchBlockStatementGroups "}" |
2421 |
* | SYNCHRONIZED ParExpression Block |
|
2422 |
* | RETURN [Expression] ";" |
|
2423 |
* | THROW Expression ";" |
|
2424 |
* | BREAK [Ident] ";" |
|
2425 |
* | CONTINUE [Ident] ";" |
|
2426 |
* | ASSERT Expression [ ":" Expression ] ";" |
|
2427 |
* | ";" |
|
2428 |
* | ExpressionStatement |
|
2429 |
* | Ident ":" Statement |
|
2430 |
*/ |
|
2431 |
@SuppressWarnings("fallthrough") |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2432 |
public JCStatement parseStatement() { |
10815 | 2433 |
int pos = token.pos; |
2434 |
switch (token.kind) { |
|
10 | 2435 |
case LBRACE: |
2436 |
return block(); |
|
2437 |
case IF: { |
|
10815 | 2438 |
nextToken(); |
10 | 2439 |
JCExpression cond = parExpression(); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2440 |
JCStatement thenpart = parseStatementAsBlock(); |
10 | 2441 |
JCStatement elsepart = null; |
10815 | 2442 |
if (token.kind == ELSE) { |
2443 |
nextToken(); |
|
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2444 |
elsepart = parseStatementAsBlock(); |
10 | 2445 |
} |
2446 |
return F.at(pos).If(cond, thenpart, elsepart); |
|
2447 |
} |
|
2448 |
case FOR: { |
|
10815 | 2449 |
nextToken(); |
10 | 2450 |
accept(LPAREN); |
10815 | 2451 |
List<JCStatement> inits = token.kind == SEMI ? List.<JCStatement>nil() : forInit(); |
10 | 2452 |
if (inits.length() == 1 && |
10950 | 2453 |
inits.head.hasTag(VARDEF) && |
10 | 2454 |
((JCVariableDecl) inits.head).init == null && |
10815 | 2455 |
token.kind == COLON) { |
10 | 2456 |
checkForeach(); |
2457 |
JCVariableDecl var = (JCVariableDecl)inits.head; |
|
2458 |
accept(COLON); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2459 |
JCExpression expr = parseExpression(); |
10 | 2460 |
accept(RPAREN); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2461 |
JCStatement body = parseStatementAsBlock(); |
10 | 2462 |
return F.at(pos).ForeachLoop(var, expr, body); |
2463 |
} else { |
|
2464 |
accept(SEMI); |
|
10815 | 2465 |
JCExpression cond = token.kind == SEMI ? null : parseExpression(); |
10 | 2466 |
accept(SEMI); |
10815 | 2467 |
List<JCExpressionStatement> steps = token.kind == RPAREN ? List.<JCExpressionStatement>nil() : forUpdate(); |
10 | 2468 |
accept(RPAREN); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2469 |
JCStatement body = parseStatementAsBlock(); |
10 | 2470 |
return F.at(pos).ForLoop(inits, cond, steps, body); |
2471 |
} |
|
2472 |
} |
|
2473 |
case WHILE: { |
|
10815 | 2474 |
nextToken(); |
10 | 2475 |
JCExpression cond = parExpression(); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2476 |
JCStatement body = parseStatementAsBlock(); |
10 | 2477 |
return F.at(pos).WhileLoop(cond, body); |
2478 |
} |
|
2479 |
case DO: { |
|
10815 | 2480 |
nextToken(); |
12466
08863ee323df
7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents:
12337
diff
changeset
|
2481 |
JCStatement body = parseStatementAsBlock(); |
10 | 2482 |
accept(WHILE); |
2483 |
JCExpression cond = parExpression(); |
|
2484 |
JCDoWhileLoop t = to(F.at(pos).DoLoop(body, cond)); |
|
2485 |
accept(SEMI); |
|
2486 |
return t; |
|
2487 |
} |
|
2488 |
case TRY: { |
|
10815 | 2489 |
nextToken(); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2490 |
List<JCTree> resources = List.<JCTree>nil(); |
10815 | 2491 |
if (token.kind == LPAREN) { |
7211
163fe60f63de
6970016: Clean up ARM/try-with-resources implementation
mcimadamore
parents:
7076
diff
changeset
|
2492 |
checkTryWithResources(); |
10815 | 2493 |
nextToken(); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2494 |
resources = resources(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2495 |
accept(RPAREN); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2496 |
} |
10 | 2497 |
JCBlock body = block(); |
22163 | 2498 |
ListBuffer<JCCatch> catchers = new ListBuffer<>(); |
10 | 2499 |
JCBlock finalizer = null; |
10815 | 2500 |
if (token.kind == CATCH || token.kind == FINALLY) { |
2501 |
while (token.kind == CATCH) catchers.append(catchClause()); |
|
2502 |
if (token.kind == FINALLY) { |
|
2503 |
nextToken(); |
|
10 | 2504 |
finalizer = block(); |
2505 |
} |
|
2506 |
} else { |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2507 |
if (allowTWR) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2508 |
if (resources.isEmpty()) |
7076
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
2509 |
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
|
2510 |
} else |
7076
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
2511 |
error(pos, "try.without.catch.or.finally"); |
10 | 2512 |
} |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
2513 |
return F.at(pos).Try(resources, body, catchers.toList(), finalizer); |
10 | 2514 |
} |
2515 |
case SWITCH: { |
|
10815 | 2516 |
nextToken(); |
10 | 2517 |
JCExpression selector = parExpression(); |
2518 |
accept(LBRACE); |
|
2519 |
List<JCCase> cases = switchBlockStatementGroups(); |
|
2520 |
JCSwitch t = to(F.at(pos).Switch(selector, cases)); |
|
2521 |
accept(RBRACE); |
|
2522 |
return t; |
|
2523 |
} |
|
2524 |
case SYNCHRONIZED: { |
|
10815 | 2525 |
nextToken(); |
10 | 2526 |
JCExpression lock = parExpression(); |
2527 |
JCBlock body = block(); |
|
2528 |
return F.at(pos).Synchronized(lock, body); |
|
2529 |
} |
|
2530 |
case RETURN: { |
|
10815 | 2531 |
nextToken(); |
2532 |
JCExpression result = token.kind == SEMI ? null : parseExpression(); |
|
10 | 2533 |
JCReturn t = to(F.at(pos).Return(result)); |
2534 |
accept(SEMI); |
|
2535 |
return t; |
|
2536 |
} |
|
2537 |
case THROW: { |
|
10815 | 2538 |
nextToken(); |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2539 |
JCExpression exc = parseExpression(); |
10 | 2540 |
JCThrow t = to(F.at(pos).Throw(exc)); |
2541 |
accept(SEMI); |
|
2542 |
return t; |
|
2543 |
} |
|
2544 |
case BREAK: { |
|
10815 | 2545 |
nextToken(); |
15367 | 2546 |
Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null; |
10 | 2547 |
JCBreak t = to(F.at(pos).Break(label)); |
2548 |
accept(SEMI); |
|
2549 |
return t; |
|
2550 |
} |
|
2551 |
case CONTINUE: { |
|
10815 | 2552 |
nextToken(); |
15367 | 2553 |
Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null; |
10 | 2554 |
JCContinue t = to(F.at(pos).Continue(label)); |
2555 |
accept(SEMI); |
|
2556 |
return t; |
|
2557 |
} |
|
2558 |
case SEMI: |
|
10815 | 2559 |
nextToken(); |
10 | 2560 |
return toP(F.at(pos).Skip()); |
2561 |
case ELSE: |
|
14450
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2562 |
int elsePos = token.pos; |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2563 |
nextToken(); |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2564 |
return doRecover(elsePos, BasicErrorRecoveryAction.BLOCK_STMT, "else.without.if"); |
10 | 2565 |
case FINALLY: |
14450
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2566 |
int finallyPos = token.pos; |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2567 |
nextToken(); |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2568 |
return doRecover(finallyPos, BasicErrorRecoveryAction.BLOCK_STMT, "finally.without.try"); |
10 | 2569 |
case CATCH: |
14450
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2570 |
return doRecover(token.pos, BasicErrorRecoveryAction.CATCH_CLAUSE, "catch.without.try"); |
10 | 2571 |
case ASSERT: { |
10815 | 2572 |
if (allowAsserts && token.kind == ASSERT) { |
2573 |
nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2574 |
JCExpression assertion = parseExpression(); |
10 | 2575 |
JCExpression message = null; |
10815 | 2576 |
if (token.kind == COLON) { |
2577 |
nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2578 |
message = parseExpression(); |
10 | 2579 |
} |
2580 |
JCAssert t = to(F.at(pos).Assert(assertion, message)); |
|
2581 |
accept(SEMI); |
|
2582 |
return t; |
|
2583 |
} |
|
2584 |
/* else fall through to default case */ |
|
2585 |
} |
|
2586 |
case ENUM: |
|
2587 |
default: |
|
10815 | 2588 |
Token prevToken = token; |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2589 |
JCExpression expr = parseExpression(); |
10950 | 2590 |
if (token.kind == COLON && expr.hasTag(IDENT)) { |
10815 | 2591 |
nextToken(); |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2592 |
JCStatement stat = parseStatement(); |
10815 | 2593 |
return F.at(pos).Labelled(prevToken.name(), stat); |
10 | 2594 |
} else { |
2595 |
// This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon |
|
2596 |
JCExpressionStatement stat = to(F.at(pos).Exec(checkExprStat(expr))); |
|
2597 |
accept(SEMI); |
|
2598 |
return stat; |
|
2599 |
} |
|
2600 |
} |
|
2601 |
} |
|
2602 |
||
14450
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2603 |
private JCStatement doRecover(int startPos, ErrorRecoveryAction action, String key) { |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2604 |
int errPos = S.errPos(); |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2605 |
JCTree stm = action.doRecover(this); |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2606 |
S.errPos(errPos); |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2607 |
return toP(F.Exec(syntaxError(startPos, List.<JCTree>of(stm), key))); |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2608 |
} |
7a62c5b13d6e
8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents:
14359
diff
changeset
|
2609 |
|
10 | 2610 |
/** CatchClause = CATCH "(" FormalParameter ")" Block |
15385 | 2611 |
* TODO: the "FormalParameter" is not correct, it uses the special "catchTypes" rule below. |
10 | 2612 |
*/ |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
2613 |
protected JCCatch catchClause() { |
10815 | 2614 |
int pos = token.pos; |
10 | 2615 |
accept(CATCH); |
2616 |
accept(LPAREN); |
|
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2617 |
JCModifiers mods = optFinal(Flags.PARAMETER); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2618 |
List<JCExpression> catchTypes = catchTypes(); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2619 |
JCExpression paramType = catchTypes.size() > 1 ? |
9300
c2de4dd9853b
7033809: Rename "disjunctive" to "union" in javax.lang.model
darcy
parents:
9073
diff
changeset
|
2620 |
toP(F.at(catchTypes.head.getStartPosition()).TypeUnion(catchTypes)) : |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2621 |
catchTypes.head; |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2622 |
JCVariableDecl formal = variableDeclaratorId(mods, paramType); |
10 | 2623 |
accept(RPAREN); |
2624 |
JCBlock body = block(); |
|
2625 |
return F.at(pos).Catch(formal, body); |
|
2626 |
} |
|
2627 |
||
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2628 |
List<JCExpression> catchTypes() { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19122
diff
changeset
|
2629 |
ListBuffer<JCExpression> catchTypes = new ListBuffer<>(); |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2630 |
catchTypes.add(parseType()); |
10815 | 2631 |
while (token.kind == BAR) { |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2632 |
checkMulticatch(); |
10815 | 2633 |
nextToken(); |
15385 | 2634 |
// Instead of qualident this is now parseType. |
2635 |
// But would that allow too much, e.g. arrays or generics? |
|
2636 |
catchTypes.add(parseType()); |
|
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2637 |
} |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2638 |
return catchTypes.toList(); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2639 |
} |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
2640 |
|
10 | 2641 |
/** SwitchBlockStatementGroups = { SwitchBlockStatementGroup } |
2642 |
* SwitchBlockStatementGroup = SwitchLabel BlockStatements |
|
2643 |
* SwitchLabel = CASE ConstantExpression ":" | DEFAULT ":" |
|
2644 |
*/ |
|
2645 |
List<JCCase> switchBlockStatementGroups() { |
|
22163 | 2646 |
ListBuffer<JCCase> cases = new ListBuffer<>(); |
10 | 2647 |
while (true) { |
10815 | 2648 |
int pos = token.pos; |
2649 |
switch (token.kind) { |
|
14056 | 2650 |
case CASE: |
2651 |
case DEFAULT: |
|
2652 |
cases.append(switchBlockStatementGroup()); |
|
10 | 2653 |
break; |
2654 |
case RBRACE: case EOF: |
|
2655 |
return cases.toList(); |
|
2656 |
default: |
|
10815 | 2657 |
nextToken(); // to ensure progress |
10 | 2658 |
syntaxError(pos, "expected3", |
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
2659 |
CASE, DEFAULT, RBRACE); |
10 | 2660 |
} |
2661 |
} |
|
2662 |
} |
|
2663 |
||
14056 | 2664 |
protected JCCase switchBlockStatementGroup() { |
2665 |
int pos = token.pos; |
|
2666 |
List<JCStatement> stats; |
|
2667 |
JCCase c; |
|
2668 |
switch (token.kind) { |
|
2669 |
case CASE: |
|
2670 |
nextToken(); |
|
2671 |
JCExpression pat = parseExpression(); |
|
2672 |
accept(COLON); |
|
2673 |
stats = blockStatements(); |
|
2674 |
c = F.at(pos).Case(pat, stats); |
|
2675 |
if (stats.isEmpty()) |
|
2676 |
storeEnd(c, S.prevToken().endPos); |
|
2677 |
return c; |
|
2678 |
case DEFAULT: |
|
2679 |
nextToken(); |
|
2680 |
accept(COLON); |
|
2681 |
stats = blockStatements(); |
|
2682 |
c = F.at(pos).Case(null, stats); |
|
2683 |
if (stats.isEmpty()) |
|
2684 |
storeEnd(c, S.prevToken().endPos); |
|
2685 |
return c; |
|
2686 |
} |
|
2687 |
throw new AssertionError("should not reach here"); |
|
2688 |
} |
|
2689 |
||
10 | 2690 |
/** MoreStatementExpressions = { COMMA StatementExpression } |
2691 |
*/ |
|
2692 |
<T extends ListBuffer<? super JCExpressionStatement>> T moreStatementExpressions(int pos, |
|
2693 |
JCExpression first, |
|
2694 |
T stats) { |
|
2695 |
// This Exec is a "StatementExpression"; it subsumes no terminating token |
|
2696 |
stats.append(toP(F.at(pos).Exec(checkExprStat(first)))); |
|
10815 | 2697 |
while (token.kind == COMMA) { |
2698 |
nextToken(); |
|
2699 |
pos = token.pos; |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2700 |
JCExpression t = parseExpression(); |
10 | 2701 |
// This Exec is a "StatementExpression"; it subsumes no terminating token |
2702 |
stats.append(toP(F.at(pos).Exec(checkExprStat(t)))); |
|
2703 |
} |
|
2704 |
return stats; |
|
2705 |
} |
|
2706 |
||
2707 |
/** ForInit = StatementExpression MoreStatementExpressions |
|
2708 |
* | { FINAL | '@' Annotation } Type VariableDeclarators |
|
2709 |
*/ |
|
2710 |
List<JCStatement> forInit() { |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19122
diff
changeset
|
2711 |
ListBuffer<JCStatement> stats = new ListBuffer<>(); |
10815 | 2712 |
int pos = token.pos; |
2713 |
if (token.kind == FINAL || token.kind == MONKEYS_AT) { |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2714 |
return variableDeclarators(optFinal(0), parseType(), stats).toList(); |
10 | 2715 |
} else { |
2716 |
JCExpression t = term(EXPR | TYPE); |
|
15367 | 2717 |
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) { |
10 | 2718 |
return variableDeclarators(modifiersOpt(), t, stats).toList(); |
12715
139b8354de6a
7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents:
12466
diff
changeset
|
2719 |
} else if ((lastmode & TYPE) != 0 && token.kind == COLON) { |
139b8354de6a
7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents:
12466
diff
changeset
|
2720 |
error(pos, "bad.initializer", "for-loop"); |
139b8354de6a
7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents:
12466
diff
changeset
|
2721 |
return List.of((JCStatement)F.at(pos).VarDef(null, null, t, null)); |
139b8354de6a
7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents:
12466
diff
changeset
|
2722 |
} else { |
10 | 2723 |
return moreStatementExpressions(pos, t, stats).toList(); |
12715
139b8354de6a
7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents:
12466
diff
changeset
|
2724 |
} |
10 | 2725 |
} |
2726 |
} |
|
2727 |
||
2728 |
/** ForUpdate = StatementExpression MoreStatementExpressions |
|
2729 |
*/ |
|
2730 |
List<JCExpressionStatement> forUpdate() { |
|
10815 | 2731 |
return moreStatementExpressions(token.pos, |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
2732 |
parseExpression(), |
22165
ec53c8946fc2
8030807: langtools should still build using jdk 7
vromero
parents:
22163
diff
changeset
|
2733 |
new ListBuffer<JCExpressionStatement>()).toList(); |
10 | 2734 |
} |
2735 |
||
2736 |
/** AnnotationsOpt = { '@' Annotation } |
|
15385 | 2737 |
* |
2738 |
* @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION |
|
10 | 2739 |
*/ |
15385 | 2740 |
List<JCAnnotation> annotationsOpt(Tag kind) { |
10815 | 2741 |
if (token.kind != MONKEYS_AT) return List.nil(); // optimization |
22163 | 2742 |
ListBuffer<JCAnnotation> buf = new ListBuffer<>(); |
15385 | 2743 |
int prevmode = mode; |
10815 | 2744 |
while (token.kind == MONKEYS_AT) { |
2745 |
int pos = token.pos; |
|
2746 |
nextToken(); |
|
15385 | 2747 |
buf.append(annotation(pos, kind)); |
10 | 2748 |
} |
15385 | 2749 |
lastmode = mode; |
2750 |
mode = prevmode; |
|
2751 |
List<JCAnnotation> annotations = buf.toList(); |
|
2752 |
||
2753 |
return annotations; |
|
2754 |
} |
|
2755 |
||
2756 |
List<JCAnnotation> typeAnnotationsOpt() { |
|
2757 |
List<JCAnnotation> annotations = annotationsOpt(Tag.TYPE_ANNOTATION); |
|
2758 |
return annotations; |
|
10 | 2759 |
} |
2760 |
||
2761 |
/** ModifiersOpt = { Modifier } |
|
2762 |
* Modifier = PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL |
|
2763 |
* | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE | "@" |
|
2764 |
* | "@" Annotation |
|
2765 |
*/ |
|
2766 |
JCModifiers modifiersOpt() { |
|
2767 |
return modifiersOpt(null); |
|
2768 |
} |
|
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
2769 |
protected JCModifiers modifiersOpt(JCModifiers partial) { |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2770 |
long flags; |
22163 | 2771 |
ListBuffer<JCAnnotation> annotations = new ListBuffer<>(); |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2772 |
int pos; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2773 |
if (partial == null) { |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2774 |
flags = 0; |
10815 | 2775 |
pos = token.pos; |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2776 |
} else { |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2777 |
flags = partial.flags; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2778 |
annotations.appendList(partial.annotations); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2779 |
pos = partial.pos; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2780 |
} |
10948 | 2781 |
if (token.deprecatedFlag()) { |
10 | 2782 |
flags |= Flags.DEPRECATED; |
2783 |
} |
|
15385 | 2784 |
int lastPos; |
10 | 2785 |
loop: |
2786 |
while (true) { |
|
2787 |
long flag; |
|
10815 | 2788 |
switch (token.kind) { |
10 | 2789 |
case PRIVATE : flag = Flags.PRIVATE; break; |
2790 |
case PROTECTED : flag = Flags.PROTECTED; break; |
|
2791 |
case PUBLIC : flag = Flags.PUBLIC; break; |
|
2792 |
case STATIC : flag = Flags.STATIC; break; |
|
2793 |
case TRANSIENT : flag = Flags.TRANSIENT; break; |
|
2794 |
case FINAL : flag = Flags.FINAL; break; |
|
2795 |
case ABSTRACT : flag = Flags.ABSTRACT; break; |
|
2796 |
case NATIVE : flag = Flags.NATIVE; break; |
|
2797 |
case VOLATILE : flag = Flags.VOLATILE; break; |
|
2798 |
case SYNCHRONIZED: flag = Flags.SYNCHRONIZED; break; |
|
2799 |
case STRICTFP : flag = Flags.STRICTFP; break; |
|
2800 |
case MONKEYS_AT : flag = Flags.ANNOTATION; break; |
|
14267
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
2801 |
case DEFAULT : checkDefaultMethods(); flag = Flags.DEFAULT; break; |
10815 | 2802 |
case ERROR : flag = 0; nextToken(); break; |
10 | 2803 |
default: break loop; |
2804 |
} |
|
10815 | 2805 |
if ((flags & flag) != 0) error(token.pos, "repeated.modifier"); |
2806 |
lastPos = token.pos; |
|
2807 |
nextToken(); |
|
10 | 2808 |
if (flag == Flags.ANNOTATION) { |
2809 |
checkAnnotations(); |
|
10815 | 2810 |
if (token.kind != INTERFACE) { |
15385 | 2811 |
JCAnnotation ann = annotation(lastPos, Tag.ANNOTATION); |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2812 |
// 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
|
2813 |
if (flags == 0 && annotations.isEmpty()) |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2814 |
pos = ann.pos; |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2815 |
annotations.append(ann); |
10 | 2816 |
flag = 0; |
2817 |
} |
|
2818 |
} |
|
2819 |
flags |= flag; |
|
2820 |
} |
|
10815 | 2821 |
switch (token.kind) { |
10 | 2822 |
case ENUM: flags |= Flags.ENUM; break; |
2823 |
case INTERFACE: flags |= Flags.INTERFACE; break; |
|
2824 |
default: break; |
|
2825 |
} |
|
2826 |
||
2827 |
/* A modifiers tree with no modifier tokens or annotations |
|
2828 |
* has no text position. */ |
|
6152
111b884a19a7
6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
6148
diff
changeset
|
2829 |
if ((flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0 && annotations.isEmpty()) |
10 | 2830 |
pos = Position.NOPOS; |
2831 |
||
2832 |
JCModifiers mods = F.at(pos).Modifiers(flags, annotations.toList()); |
|
2833 |
if (pos != Position.NOPOS) |
|
10815 | 2834 |
storeEnd(mods, S.prevToken().endPos); |
10 | 2835 |
return mods; |
2836 |
} |
|
2837 |
||
2838 |
/** Annotation = "@" Qualident [ "(" AnnotationFieldValues ")" ] |
|
15385 | 2839 |
* |
10 | 2840 |
* @param pos position of "@" token |
15385 | 2841 |
* @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION |
10 | 2842 |
*/ |
15385 | 2843 |
JCAnnotation annotation(int pos, Tag kind) { |
10 | 2844 |
// accept(AT); // AT consumed by caller |
2845 |
checkAnnotations(); |
|
15385 | 2846 |
if (kind == Tag.TYPE_ANNOTATION) { |
2847 |
checkTypeAnnotations(); |
|
2848 |
} |
|
2849 |
JCTree ident = qualident(false); |
|
10 | 2850 |
List<JCExpression> fieldValues = annotationFieldValuesOpt(); |
15385 | 2851 |
JCAnnotation ann; |
2852 |
if (kind == Tag.ANNOTATION) { |
|
2853 |
ann = F.at(pos).Annotation(ident, fieldValues); |
|
2854 |
} else if (kind == Tag.TYPE_ANNOTATION) { |
|
2855 |
ann = F.at(pos).TypeAnnotation(ident, fieldValues); |
|
2856 |
} else { |
|
2857 |
throw new AssertionError("Unhandled annotation kind: " + kind); |
|
2858 |
} |
|
2859 |
||
10815 | 2860 |
storeEnd(ann, S.prevToken().endPos); |
10 | 2861 |
return ann; |
2862 |
} |
|
2863 |
||
2864 |
List<JCExpression> annotationFieldValuesOpt() { |
|
10815 | 2865 |
return (token.kind == LPAREN) ? annotationFieldValues() : List.<JCExpression>nil(); |
10 | 2866 |
} |
2867 |
||
2868 |
/** AnnotationFieldValues = "(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */ |
|
2869 |
List<JCExpression> annotationFieldValues() { |
|
2870 |
accept(LPAREN); |
|
22163 | 2871 |
ListBuffer<JCExpression> buf = new ListBuffer<>(); |
10815 | 2872 |
if (token.kind != RPAREN) { |
10 | 2873 |
buf.append(annotationFieldValue()); |
10815 | 2874 |
while (token.kind == COMMA) { |
2875 |
nextToken(); |
|
10 | 2876 |
buf.append(annotationFieldValue()); |
2877 |
} |
|
2878 |
} |
|
2879 |
accept(RPAREN); |
|
2880 |
return buf.toList(); |
|
2881 |
} |
|
2882 |
||
2883 |
/** AnnotationFieldValue = AnnotationValue |
|
2884 |
* | Identifier "=" AnnotationValue |
|
2885 |
*/ |
|
2886 |
JCExpression annotationFieldValue() { |
|
16296
22c10640a521
8007427: Annotation element as '_' gives compiler error instead of a warning
mcimadamore
parents:
15718
diff
changeset
|
2887 |
if (LAX_IDENTIFIER.accepts(token.kind)) { |
10 | 2888 |
mode = EXPR; |
2889 |
JCExpression t1 = term1(); |
|
10950 | 2890 |
if (t1.hasTag(IDENT) && token.kind == EQ) { |
10815 | 2891 |
int pos = token.pos; |
10 | 2892 |
accept(EQ); |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2893 |
JCExpression v = annotationValue(); |
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
2894 |
return toP(F.at(pos).Assign(t1, v)); |
10 | 2895 |
} else { |
2896 |
return t1; |
|
2897 |
} |
|
2898 |
} |
|
2899 |
return annotationValue(); |
|
2900 |
} |
|
2901 |
||
2902 |
/* AnnotationValue = ConditionalExpression |
|
2903 |
* | Annotation |
|
3997
35250de1d4ea
6337964: should ignore last comma in annotation array
darcy
parents:
3894
diff
changeset
|
2904 |
* | "{" [ AnnotationValue { "," AnnotationValue } ] [","] "}" |
10 | 2905 |
*/ |
2906 |
JCExpression annotationValue() { |
|
2907 |
int pos; |
|
10815 | 2908 |
switch (token.kind) { |
10 | 2909 |
case MONKEYS_AT: |
10815 | 2910 |
pos = token.pos; |
2911 |
nextToken(); |
|
15385 | 2912 |
return annotation(pos, Tag.ANNOTATION); |
10 | 2913 |
case LBRACE: |
10815 | 2914 |
pos = token.pos; |
10 | 2915 |
accept(LBRACE); |
22163 | 2916 |
ListBuffer<JCExpression> buf = new ListBuffer<>(); |
18645
4623b7161e9f
8012722: Single comma in array initializer should parse
emc
parents:
17803
diff
changeset
|
2917 |
if (token.kind == COMMA) { |
4623b7161e9f
8012722: Single comma in array initializer should parse
emc
parents:
17803
diff
changeset
|
2918 |
nextToken(); |
4623b7161e9f
8012722: Single comma in array initializer should parse
emc
parents:
17803
diff
changeset
|
2919 |
} else if (token.kind != RBRACE) { |
10 | 2920 |
buf.append(annotationValue()); |
10815 | 2921 |
while (token.kind == COMMA) { |
2922 |
nextToken(); |
|
2923 |
if (token.kind == RBRACE) break; |
|
10 | 2924 |
buf.append(annotationValue()); |
2925 |
} |
|
2926 |
} |
|
2927 |
accept(RBRACE); |
|
2928 |
return toP(F.at(pos).NewArray(null, List.<JCExpression>nil(), buf.toList())); |
|
2929 |
default: |
|
2930 |
mode = EXPR; |
|
2931 |
return term1(); |
|
2932 |
} |
|
2933 |
} |
|
2934 |
||
2935 |
/** VariableDeclarators = VariableDeclarator { "," VariableDeclarator } |
|
2936 |
*/ |
|
2937 |
public <T extends ListBuffer<? super JCVariableDecl>> T variableDeclarators(JCModifiers mods, |
|
2938 |
JCExpression type, |
|
2939 |
T vdefs) |
|
2940 |
{ |
|
10815 | 2941 |
return variableDeclaratorsRest(token.pos, mods, type, ident(), false, null, vdefs); |
10 | 2942 |
} |
2943 |
||
2944 |
/** VariableDeclaratorsRest = VariableDeclaratorRest { "," VariableDeclarator } |
|
2945 |
* ConstantDeclaratorsRest = ConstantDeclaratorRest { "," ConstantDeclarator } |
|
2946 |
* |
|
2947 |
* @param reqInit Is an initializer always required? |
|
2948 |
* @param dc The documentation comment for the variable declarations, or null. |
|
2949 |
*/ |
|
2950 |
<T extends ListBuffer<? super JCVariableDecl>> T variableDeclaratorsRest(int pos, |
|
2951 |
JCModifiers mods, |
|
2952 |
JCExpression type, |
|
2953 |
Name name, |
|
2954 |
boolean reqInit, |
|
13077 | 2955 |
Comment dc, |
10 | 2956 |
T vdefs) |
2957 |
{ |
|
2958 |
vdefs.append(variableDeclaratorRest(pos, mods, type, name, reqInit, dc)); |
|
10815 | 2959 |
while (token.kind == COMMA) { |
10 | 2960 |
// All but last of multiple declarators subsume a comma |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
2961 |
storeEnd((JCTree)vdefs.last(), token.endPos); |
10815 | 2962 |
nextToken(); |
10 | 2963 |
vdefs.append(variableDeclarator(mods, type, reqInit, dc)); |
2964 |
} |
|
2965 |
return vdefs; |
|
2966 |
} |
|
2967 |
||
2968 |
/** VariableDeclarator = Ident VariableDeclaratorRest |
|
2969 |
* ConstantDeclarator = Ident ConstantDeclaratorRest |
|
2970 |
*/ |
|
13077 | 2971 |
JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, Comment dc) { |
10815 | 2972 |
return variableDeclaratorRest(token.pos, mods, type, ident(), reqInit, dc); |
10 | 2973 |
} |
2974 |
||
2975 |
/** VariableDeclaratorRest = BracketsOpt ["=" VariableInitializer] |
|
2976 |
* ConstantDeclaratorRest = BracketsOpt "=" VariableInitializer |
|
2977 |
* |
|
2978 |
* @param reqInit Is an initializer always required? |
|
2979 |
* @param dc The documentation comment for the variable declarations, or null. |
|
2980 |
*/ |
|
2981 |
JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name, |
|
13077 | 2982 |
boolean reqInit, Comment dc) { |
10 | 2983 |
type = bracketsOpt(type); |
2984 |
JCExpression init = null; |
|
10815 | 2985 |
if (token.kind == EQ) { |
2986 |
nextToken(); |
|
10 | 2987 |
init = variableInitializer(); |
2988 |
} |
|
10815 | 2989 |
else if (reqInit) syntaxError(token.pos, "expected", EQ); |
10 | 2990 |
JCVariableDecl result = |
2991 |
toP(F.at(pos).VarDef(mods, name, type, init)); |
|
2992 |
attach(result, dc); |
|
2993 |
return result; |
|
2994 |
} |
|
2995 |
||
2996 |
/** VariableDeclaratorId = Ident BracketsOpt |
|
2997 |
*/ |
|
2998 |
JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type) { |
|
15367 | 2999 |
return variableDeclaratorId(mods, type, false); |
3000 |
} |
|
3001 |
//where |
|
3002 |
JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type, boolean lambdaParameter) { |
|
10815 | 3003 |
int pos = token.pos; |
15367 | 3004 |
Name name; |
3005 |
if (lambdaParameter && token.kind == UNDERSCORE) { |
|
20609
dce669916b62
8023679: Improve error message for '_' used as a lambda parameter name
vromero
parents:
20249
diff
changeset
|
3006 |
log.error(pos, "underscore.as.identifier.in.lambda"); |
15367 | 3007 |
name = token.name(); |
20609
dce669916b62
8023679: Improve error message for '_' used as a lambda parameter name
vromero
parents:
20249
diff
changeset
|
3008 |
nextToken(); |
15367 | 3009 |
} else { |
17578 | 3010 |
if (allowThisIdent) { |
3011 |
JCExpression pn = qualident(false); |
|
3012 |
if (pn.hasTag(Tag.IDENT) && ((JCIdent)pn).name != names._this) { |
|
3013 |
name = ((JCIdent)pn).name; |
|
3014 |
} else { |
|
3015 |
if ((mods.flags & Flags.VARARGS) != 0) { |
|
3016 |
log.error(token.pos, "varargs.and.receiver"); |
|
3017 |
} |
|
3018 |
if (token.kind == LBRACKET) { |
|
3019 |
log.error(token.pos, "array.and.receiver"); |
|
3020 |
} |
|
3021 |
return toP(F.at(pos).ReceiverVarDef(mods, pn, type)); |
|
3022 |
} |
|
3023 |
} else { |
|
3024 |
name = ident(); |
|
3025 |
} |
|
15367 | 3026 |
} |
8047
c7f08cdb5c3c
6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents:
8033
diff
changeset
|
3027 |
if ((mods.flags & Flags.VARARGS) != 0 && |
10815 | 3028 |
token.kind == LBRACKET) { |
3029 |
log.error(token.pos, "varargs.and.old.array.syntax"); |
|
8047
c7f08cdb5c3c
6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents:
8033
diff
changeset
|
3030 |
} |
c7f08cdb5c3c
6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents:
8033
diff
changeset
|
3031 |
type = bracketsOpt(type); |
10 | 3032 |
return toP(F.at(pos).VarDef(mods, name, type, null)); |
3033 |
} |
|
3034 |
||
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3035 |
/** Resources = Resource { ";" Resources } |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3036 |
*/ |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3037 |
List<JCTree> resources() { |
22163 | 3038 |
ListBuffer<JCTree> defs = new ListBuffer<>(); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3039 |
defs.append(resource()); |
10815 | 3040 |
while (token.kind == SEMI) { |
8234
5050975e2df0
7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources
darcy
parents:
8224
diff
changeset
|
3041 |
// All but last of multiple declarators must subsume a semicolon |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
3042 |
storeEnd(defs.last(), token.endPos); |
10815 | 3043 |
int semiColonPos = token.pos; |
3044 |
nextToken(); |
|
3045 |
if (token.kind == RPAREN) { // Optional trailing semicolon |
|
8224
8f18e1622660
7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents:
8047
diff
changeset
|
3046 |
// after last resource |
8f18e1622660
7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents:
8047
diff
changeset
|
3047 |
break; |
8f18e1622660
7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents:
8047
diff
changeset
|
3048 |
} |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3049 |
defs.append(resource()); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3050 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3051 |
return defs.toList(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3052 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3053 |
|
8224
8f18e1622660
7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents:
8047
diff
changeset
|
3054 |
/** Resource = VariableModifiersOpt Type VariableDeclaratorId = Expression |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3055 |
*/ |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3056 |
protected JCTree resource() { |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3057 |
JCModifiers optFinal = optFinal(Flags.FINAL); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3058 |
JCExpression type = parseType(); |
10815 | 3059 |
int pos = token.pos; |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3060 |
Name ident = ident(); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3061 |
return variableDeclaratorRest(pos, optFinal, type, ident, true, null); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3062 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
3063 |
|
10 | 3064 |
/** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration} |
3065 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3066 |
public JCTree.JCCompilationUnit parseCompilationUnit() { |
10815 | 3067 |
Token firstToken = token; |
10 | 3068 |
JCExpression pid = null; |
3069 |
JCModifiers mods = null; |
|
10815 | 3070 |
boolean consumedToplevelDoc = false; |
3071 |
boolean seenImport = false; |
|
3072 |
boolean seenPackage = false; |
|
10 | 3073 |
List<JCAnnotation> packageAnnotations = List.nil(); |
10815 | 3074 |
if (token.kind == MONKEYS_AT) |
10 | 3075 |
mods = modifiersOpt(); |
3076 |
||
10815 | 3077 |
if (token.kind == PACKAGE) { |
3078 |
seenPackage = true; |
|
10 | 3079 |
if (mods != null) { |
3080 |
checkNoMods(mods.flags); |
|
3081 |
packageAnnotations = mods.annotations; |
|
3082 |
mods = null; |
|
3083 |
} |
|
10815 | 3084 |
nextToken(); |
15385 | 3085 |
pid = qualident(false); |
10 | 3086 |
accept(SEMI); |
3087 |
} |
|
22163 | 3088 |
ListBuffer<JCTree> defs = new ListBuffer<>(); |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3089 |
boolean checkForImports = true; |
10815 | 3090 |
boolean firstTypeDecl = true; |
3091 |
while (token.kind != EOF) { |
|
15711
a975ad2bfe35
8005931: javac doesn't set ACC_STRICT for classes with package access
vromero
parents:
15385
diff
changeset
|
3092 |
if (token.pos > 0 && token.pos <= endPosTable.errorEndPos) { |
10 | 3093 |
// error recovery |
3094 |
skip(checkForImports, false, false, false); |
|
10815 | 3095 |
if (token.kind == EOF) |
10 | 3096 |
break; |
3097 |
} |
|
10815 | 3098 |
if (checkForImports && mods == null && token.kind == IMPORT) { |
3099 |
seenImport = true; |
|
10 | 3100 |
defs.append(importDeclaration()); |
3101 |
} else { |
|
13077 | 3102 |
Comment docComment = token.comment(CommentStyle.JAVADOC); |
10815 | 3103 |
if (firstTypeDecl && !seenImport && !seenPackage) { |
10948 | 3104 |
docComment = firstToken.comment(CommentStyle.JAVADOC); |
10815 | 3105 |
consumedToplevelDoc = true; |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
6585
diff
changeset
|
3106 |
} |
10815 | 3107 |
JCTree def = typeDeclaration(mods, docComment); |
10 | 3108 |
if (def instanceof JCExpressionStatement) |
3109 |
def = ((JCExpressionStatement)def).expr; |
|
3110 |
defs.append(def); |
|
3111 |
if (def instanceof JCClassDecl) |
|
3112 |
checkForImports = false; |
|
3113 |
mods = null; |
|
10815 | 3114 |
firstTypeDecl = false; |
10 | 3115 |
} |
3116 |
} |
|
10815 | 3117 |
JCTree.JCCompilationUnit toplevel = F.at(firstToken.pos).TopLevel(packageAnnotations, pid, defs.toList()); |
3118 |
if (!consumedToplevelDoc) |
|
10948 | 3119 |
attach(toplevel, firstToken.comment(CommentStyle.JAVADOC)); |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
14725
diff
changeset
|
3120 |
if (defs.isEmpty()) |
10815 | 3121 |
storeEnd(toplevel, S.prevToken().endPos); |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3122 |
if (keepDocComments) |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3123 |
toplevel.docComments = docComments; |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3124 |
if (keepLineMap) |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3125 |
toplevel.lineMap = S.getLineMap(); |
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
3126 |
this.endPosTable.setParser(null); // remove reference to parser |
11055 | 3127 |
toplevel.endPositions = this.endPosTable; |
10 | 3128 |
return toplevel; |
3129 |
} |
|
3130 |
||
3131 |
/** ImportDeclaration = IMPORT [ STATIC ] Ident { "." Ident } [ "." "*" ] ";" |
|
3132 |
*/ |
|
3133 |
JCTree importDeclaration() { |
|
10815 | 3134 |
int pos = token.pos; |
3135 |
nextToken(); |
|
10 | 3136 |
boolean importStatic = false; |
10815 | 3137 |
if (token.kind == STATIC) { |
10 | 3138 |
checkStaticImports(); |
3139 |
importStatic = true; |
|
10815 | 3140 |
nextToken(); |
10 | 3141 |
} |
10815 | 3142 |
JCExpression pid = toP(F.at(token.pos).Ident(ident())); |
10 | 3143 |
do { |
10815 | 3144 |
int pos1 = token.pos; |
10 | 3145 |
accept(DOT); |
10815 | 3146 |
if (token.kind == STAR) { |
10 | 3147 |
pid = to(F.at(pos1).Select(pid, names.asterisk)); |
10815 | 3148 |
nextToken(); |
10 | 3149 |
break; |
3150 |
} else { |
|
3151 |
pid = toP(F.at(pos1).Select(pid, ident())); |
|
3152 |
} |
|
10815 | 3153 |
} while (token.kind == DOT); |
10 | 3154 |
accept(SEMI); |
3155 |
return toP(F.at(pos).Import(pid, importStatic)); |
|
3156 |
} |
|
3157 |
||
3158 |
/** TypeDeclaration = ClassOrInterfaceOrEnumDeclaration |
|
3159 |
* | ";" |
|
3160 |
*/ |
|
13077 | 3161 |
JCTree typeDeclaration(JCModifiers mods, Comment docComment) { |
10815 | 3162 |
int pos = token.pos; |
3163 |
if (mods == null && token.kind == SEMI) { |
|
3164 |
nextToken(); |
|
10 | 3165 |
return toP(F.at(pos).Skip()); |
3166 |
} else { |
|
10815 | 3167 |
return classOrInterfaceOrEnumDeclaration(modifiersOpt(mods), docComment); |
10 | 3168 |
} |
3169 |
} |
|
3170 |
||
3171 |
/** ClassOrInterfaceOrEnumDeclaration = ModifiersOpt |
|
3172 |
* (ClassDeclaration | InterfaceDeclaration | EnumDeclaration) |
|
3173 |
* @param mods Any modifiers starting the class or interface declaration |
|
3174 |
* @param dc The documentation comment for the class, or null. |
|
3175 |
*/ |
|
13077 | 3176 |
JCStatement classOrInterfaceOrEnumDeclaration(JCModifiers mods, Comment dc) { |
10815 | 3177 |
if (token.kind == CLASS) { |
10 | 3178 |
return classDeclaration(mods, dc); |
10815 | 3179 |
} else if (token.kind == INTERFACE) { |
10 | 3180 |
return interfaceDeclaration(mods, dc); |
3181 |
} else if (allowEnums) { |
|
10815 | 3182 |
if (token.kind == ENUM) { |
10 | 3183 |
return enumDeclaration(mods, dc); |
3184 |
} else { |
|
10815 | 3185 |
int pos = token.pos; |
10 | 3186 |
List<JCTree> errs; |
15367 | 3187 |
if (LAX_IDENTIFIER.accepts(token.kind)) { |
10 | 3188 |
errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident()))); |
10815 | 3189 |
setErrorEndPos(token.pos); |
10 | 3190 |
} else { |
3191 |
errs = List.<JCTree>of(mods); |
|
3192 |
} |
|
3193 |
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
|
3194 |
CLASS, INTERFACE, ENUM))); |
10 | 3195 |
} |
3196 |
} else { |
|
10815 | 3197 |
if (token.kind == ENUM) { |
3198 |
error(token.pos, "enums.not.supported.in.source", source.name); |
|
10 | 3199 |
allowEnums = true; |
3200 |
return enumDeclaration(mods, dc); |
|
3201 |
} |
|
10815 | 3202 |
int pos = token.pos; |
10 | 3203 |
List<JCTree> errs; |
15367 | 3204 |
if (LAX_IDENTIFIER.accepts(token.kind)) { |
10 | 3205 |
errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident()))); |
10815 | 3206 |
setErrorEndPos(token.pos); |
10 | 3207 |
} else { |
3208 |
errs = List.<JCTree>of(mods); |
|
3209 |
} |
|
3210 |
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
|
3211 |
CLASS, INTERFACE))); |
10 | 3212 |
} |
3213 |
} |
|
3214 |
||
3215 |
/** ClassDeclaration = CLASS Ident TypeParametersOpt [EXTENDS Type] |
|
3216 |
* [IMPLEMENTS TypeList] ClassBody |
|
3217 |
* @param mods The modifiers starting the class declaration |
|
3218 |
* @param dc The documentation comment for the class, or null. |
|
3219 |
*/ |
|
13077 | 3220 |
protected JCClassDecl classDeclaration(JCModifiers mods, Comment dc) { |
10815 | 3221 |
int pos = token.pos; |
10 | 3222 |
accept(CLASS); |
3223 |
Name name = ident(); |
|
3224 |
||
3225 |
List<JCTypeParameter> typarams = typeParametersOpt(); |
|
3226 |
||
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8234
diff
changeset
|
3227 |
JCExpression extending = null; |
10815 | 3228 |
if (token.kind == EXTENDS) { |
3229 |
nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3230 |
extending = parseType(); |
10 | 3231 |
} |
3232 |
List<JCExpression> implementing = List.nil(); |
|
10815 | 3233 |
if (token.kind == IMPLEMENTS) { |
3234 |
nextToken(); |
|
10 | 3235 |
implementing = typeList(); |
3236 |
} |
|
3237 |
List<JCTree> defs = classOrInterfaceBody(name, false); |
|
3238 |
JCClassDecl result = toP(F.at(pos).ClassDef( |
|
3239 |
mods, name, typarams, extending, implementing, defs)); |
|
3240 |
attach(result, dc); |
|
3241 |
return result; |
|
3242 |
} |
|
3243 |
||
3244 |
/** InterfaceDeclaration = INTERFACE Ident TypeParametersOpt |
|
3245 |
* [EXTENDS TypeList] InterfaceBody |
|
3246 |
* @param mods The modifiers starting the interface declaration |
|
3247 |
* @param dc The documentation comment for the interface, or null. |
|
3248 |
*/ |
|
13077 | 3249 |
protected JCClassDecl interfaceDeclaration(JCModifiers mods, Comment dc) { |
10815 | 3250 |
int pos = token.pos; |
10 | 3251 |
accept(INTERFACE); |
3252 |
Name name = ident(); |
|
3253 |
||
3254 |
List<JCTypeParameter> typarams = typeParametersOpt(); |
|
3255 |
||
3256 |
List<JCExpression> extending = List.nil(); |
|
10815 | 3257 |
if (token.kind == EXTENDS) { |
3258 |
nextToken(); |
|
10 | 3259 |
extending = typeList(); |
3260 |
} |
|
3261 |
List<JCTree> defs = classOrInterfaceBody(name, true); |
|
3262 |
JCClassDecl result = toP(F.at(pos).ClassDef( |
|
3263 |
mods, name, typarams, null, extending, defs)); |
|
3264 |
attach(result, dc); |
|
3265 |
return result; |
|
3266 |
} |
|
3267 |
||
3268 |
/** EnumDeclaration = ENUM Ident [IMPLEMENTS TypeList] EnumBody |
|
3269 |
* @param mods The modifiers starting the enum declaration |
|
3270 |
* @param dc The documentation comment for the enum, or null. |
|
3271 |
*/ |
|
13077 | 3272 |
protected JCClassDecl enumDeclaration(JCModifiers mods, Comment dc) { |
10815 | 3273 |
int pos = token.pos; |
10 | 3274 |
accept(ENUM); |
3275 |
Name name = ident(); |
|
3276 |
||
3277 |
List<JCExpression> implementing = List.nil(); |
|
10815 | 3278 |
if (token.kind == IMPLEMENTS) { |
3279 |
nextToken(); |
|
10 | 3280 |
implementing = typeList(); |
3281 |
} |
|
3282 |
||
3283 |
List<JCTree> defs = enumBody(name); |
|
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
3284 |
mods.flags |= Flags.ENUM; |
10 | 3285 |
JCClassDecl result = toP(F.at(pos). |
4870
a132763160d7
6919889: assorted position errors in compiler syntax trees
jjg
parents:
4704
diff
changeset
|
3286 |
ClassDef(mods, name, List.<JCTypeParameter>nil(), |
10 | 3287 |
null, implementing, defs)); |
3288 |
attach(result, dc); |
|
3289 |
return result; |
|
3290 |
} |
|
3291 |
||
3292 |
/** EnumBody = "{" { EnumeratorDeclarationList } [","] |
|
3293 |
* [ ";" {ClassBodyDeclaration} ] "}" |
|
3294 |
*/ |
|
3295 |
List<JCTree> enumBody(Name enumName) { |
|
3296 |
accept(LBRACE); |
|
22163 | 3297 |
ListBuffer<JCTree> defs = new ListBuffer<>(); |
10815 | 3298 |
if (token.kind == COMMA) { |
3299 |
nextToken(); |
|
3300 |
} else if (token.kind != RBRACE && token.kind != SEMI) { |
|
10 | 3301 |
defs.append(enumeratorDeclaration(enumName)); |
10815 | 3302 |
while (token.kind == COMMA) { |
3303 |
nextToken(); |
|
3304 |
if (token.kind == RBRACE || token.kind == SEMI) break; |
|
10 | 3305 |
defs.append(enumeratorDeclaration(enumName)); |
3306 |
} |
|
10815 | 3307 |
if (token.kind != SEMI && token.kind != RBRACE) { |
3308 |
defs.append(syntaxError(token.pos, "expected3", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
3309 |
COMMA, RBRACE, SEMI)); |
10815 | 3310 |
nextToken(); |
10 | 3311 |
} |
3312 |
} |
|
10815 | 3313 |
if (token.kind == SEMI) { |
3314 |
nextToken(); |
|
3315 |
while (token.kind != RBRACE && token.kind != EOF) { |
|
10 | 3316 |
defs.appendList(classOrInterfaceBodyDeclaration(enumName, |
3317 |
false)); |
|
11055 | 3318 |
if (token.pos <= endPosTable.errorEndPos) { |
10 | 3319 |
// error recovery |
3320 |
skip(false, true, true, false); |
|
3321 |
} |
|
3322 |
} |
|
3323 |
} |
|
3324 |
accept(RBRACE); |
|
3325 |
return defs.toList(); |
|
3326 |
} |
|
3327 |
||
3328 |
/** EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ] |
|
3329 |
*/ |
|
3330 |
JCTree enumeratorDeclaration(Name enumName) { |
|
13077 | 3331 |
Comment dc = token.comment(CommentStyle.JAVADOC); |
10 | 3332 |
int flags = Flags.PUBLIC|Flags.STATIC|Flags.FINAL|Flags.ENUM; |
10948 | 3333 |
if (token.deprecatedFlag()) { |
10 | 3334 |
flags |= Flags.DEPRECATED; |
3335 |
} |
|
10815 | 3336 |
int pos = token.pos; |
15385 | 3337 |
List<JCAnnotation> annotations = annotationsOpt(Tag.ANNOTATION); |
10 | 3338 |
JCModifiers mods = F.at(annotations.isEmpty() ? Position.NOPOS : pos).Modifiers(flags, annotations); |
3339 |
List<JCExpression> typeArgs = typeArgumentsOpt(); |
|
10815 | 3340 |
int identPos = token.pos; |
10 | 3341 |
Name name = ident(); |
10815 | 3342 |
int createPos = token.pos; |
3343 |
List<JCExpression> args = (token.kind == LPAREN) |
|
10 | 3344 |
? arguments() : List.<JCExpression>nil(); |
3345 |
JCClassDecl body = null; |
|
10815 | 3346 |
if (token.kind == LBRACE) { |
10 | 3347 |
JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM | Flags.STATIC); |
3348 |
List<JCTree> defs = classOrInterfaceBody(names.empty, false); |
|
3349 |
body = toP(F.at(identPos).AnonymousClassDef(mods1, defs)); |
|
3350 |
} |
|
3351 |
if (args.isEmpty() && body == null) |
|
4704
5206047418c2
6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents:
3997
diff
changeset
|
3352 |
createPos = identPos; |
5206047418c2
6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents:
3997
diff
changeset
|
3353 |
JCIdent ident = F.at(identPos).Ident(enumName); |
10 | 3354 |
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
|
3355 |
if (createPos != identPos) |
10815 | 3356 |
storeEnd(create, S.prevToken().endPos); |
4704
5206047418c2
6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents:
3997
diff
changeset
|
3357 |
ident = F.at(identPos).Ident(enumName); |
10 | 3358 |
JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create)); |
3359 |
attach(result, dc); |
|
3360 |
return result; |
|
3361 |
} |
|
3362 |
||
3363 |
/** TypeList = Type {"," Type} |
|
3364 |
*/ |
|
3365 |
List<JCExpression> typeList() { |
|
22163 | 3366 |
ListBuffer<JCExpression> ts = new ListBuffer<>(); |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3367 |
ts.append(parseType()); |
10815 | 3368 |
while (token.kind == COMMA) { |
3369 |
nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3370 |
ts.append(parseType()); |
10 | 3371 |
} |
3372 |
return ts.toList(); |
|
3373 |
} |
|
3374 |
||
3375 |
/** ClassBody = "{" {ClassBodyDeclaration} "}" |
|
3376 |
* InterfaceBody = "{" {InterfaceBodyDeclaration} "}" |
|
3377 |
*/ |
|
3378 |
List<JCTree> classOrInterfaceBody(Name className, boolean isInterface) { |
|
3379 |
accept(LBRACE); |
|
11055 | 3380 |
if (token.pos <= endPosTable.errorEndPos) { |
10 | 3381 |
// error recovery |
3382 |
skip(false, true, false, false); |
|
10815 | 3383 |
if (token.kind == LBRACE) |
3384 |
nextToken(); |
|
10 | 3385 |
} |
22163 | 3386 |
ListBuffer<JCTree> defs = new ListBuffer<>(); |
10815 | 3387 |
while (token.kind != RBRACE && token.kind != EOF) { |
10 | 3388 |
defs.appendList(classOrInterfaceBodyDeclaration(className, isInterface)); |
11055 | 3389 |
if (token.pos <= endPosTable.errorEndPos) { |
10 | 3390 |
// error recovery |
3391 |
skip(false, true, true, false); |
|
3392 |
} |
|
3393 |
} |
|
3394 |
accept(RBRACE); |
|
3395 |
return defs.toList(); |
|
3396 |
} |
|
3397 |
||
3398 |
/** ClassBodyDeclaration = |
|
3399 |
* ";" |
|
3400 |
* | [STATIC] Block |
|
3401 |
* | ModifiersOpt |
|
3402 |
* ( Type Ident |
|
3403 |
* ( VariableDeclaratorsRest ";" | MethodDeclaratorRest ) |
|
3404 |
* | VOID Ident MethodDeclaratorRest |
|
3405 |
* | TypeParameters (Type | VOID) Ident MethodDeclaratorRest |
|
3406 |
* | Ident ConstructorDeclaratorRest |
|
3407 |
* | TypeParameters Ident ConstructorDeclaratorRest |
|
3408 |
* | ClassOrInterfaceOrEnumDeclaration |
|
3409 |
* ) |
|
3410 |
* InterfaceBodyDeclaration = |
|
3411 |
* ";" |
|
3412 |
* | ModifiersOpt Type Ident |
|
3413 |
* ( ConstantDeclaratorsRest | InterfaceMethodDeclaratorRest ";" ) |
|
3414 |
*/ |
|
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3415 |
protected List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) { |
10815 | 3416 |
if (token.kind == SEMI) { |
3417 |
nextToken(); |
|
6585
189f53daed52
6921495: spurious semicolons in class def cause empty NOPOS blocks
jjg
parents:
6152
diff
changeset
|
3418 |
return List.<JCTree>nil(); |
10 | 3419 |
} else { |
13077 | 3420 |
Comment dc = token.comment(CommentStyle.JAVADOC); |
10815 | 3421 |
int pos = token.pos; |
10 | 3422 |
JCModifiers mods = modifiersOpt(); |
10815 | 3423 |
if (token.kind == CLASS || |
3424 |
token.kind == INTERFACE || |
|
3425 |
allowEnums && token.kind == ENUM) { |
|
10 | 3426 |
return List.<JCTree>of(classOrInterfaceOrEnumDeclaration(mods, dc)); |
10815 | 3427 |
} else if (token.kind == LBRACE && !isInterface && |
10 | 3428 |
(mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 && |
3429 |
mods.annotations.isEmpty()) { |
|
3430 |
return List.<JCTree>of(block(pos, mods.flags)); |
|
3431 |
} else { |
|
10815 | 3432 |
pos = token.pos; |
10 | 3433 |
List<JCTypeParameter> typarams = typeParametersOpt(); |
8033
13d9c18b8fa1
6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
8032
diff
changeset
|
3434 |
// if there are type parameters but no modifiers, save the start |
13d9c18b8fa1
6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
8032
diff
changeset
|
3435 |
// position of the method in the modifiers. |
13d9c18b8fa1
6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
8032
diff
changeset
|
3436 |
if (typarams.nonEmpty() && mods.pos == Position.NOPOS) { |
13d9c18b8fa1
6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
8032
diff
changeset
|
3437 |
mods.pos = pos; |
13d9c18b8fa1
6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
8032
diff
changeset
|
3438 |
storeEnd(mods, pos); |
13d9c18b8fa1
6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
8032
diff
changeset
|
3439 |
} |
15385 | 3440 |
List<JCAnnotation> annosAfterParams = annotationsOpt(Tag.ANNOTATION); |
3441 |
||
10815 | 3442 |
Token tk = token; |
3443 |
pos = token.pos; |
|
10 | 3444 |
JCExpression type; |
10815 | 3445 |
boolean isVoid = token.kind == VOID; |
10 | 3446 |
if (isVoid) { |
15385 | 3447 |
if (annosAfterParams.nonEmpty()) |
3448 |
illegal(annosAfterParams.head.pos); |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3449 |
type = to(F.at(pos).TypeIdent(TypeTag.VOID)); |
10815 | 3450 |
nextToken(); |
10 | 3451 |
} else { |
15385 | 3452 |
if (annosAfterParams.nonEmpty()) { |
3453 |
mods.annotations = mods.annotations.appendList(annosAfterParams); |
|
3454 |
if (mods.pos == Position.NOPOS) |
|
3455 |
mods.pos = mods.annotations.head.pos; |
|
3456 |
} |
|
3457 |
// method returns types are un-annotated types |
|
3458 |
type = unannotatedType(); |
|
10 | 3459 |
} |
10950 | 3460 |
if (token.kind == LPAREN && !isInterface && type.hasTag(IDENT)) { |
10815 | 3461 |
if (isInterface || tk.name() != className) |
7076
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
3462 |
error(pos, "invalid.meth.decl.ret.type.req"); |
10 | 3463 |
return List.of(methodDeclaratorRest( |
3464 |
pos, mods, null, names.init, typarams, |
|
3465 |
isInterface, true, dc)); |
|
3466 |
} else { |
|
10815 | 3467 |
pos = token.pos; |
3468 |
Name name = ident(); |
|
3469 |
if (token.kind == LPAREN) { |
|
10 | 3470 |
return List.of(methodDeclaratorRest( |
3471 |
pos, mods, type, name, typarams, |
|
3472 |
isInterface, isVoid, dc)); |
|
3473 |
} else if (!isVoid && typarams.isEmpty()) { |
|
3474 |
List<JCTree> defs = |
|
3475 |
variableDeclaratorsRest(pos, mods, type, name, isInterface, dc, |
|
3476 |
new ListBuffer<JCTree>()).toList(); |
|
10815 | 3477 |
storeEnd(defs.last(), token.endPos); |
10 | 3478 |
accept(SEMI); |
3479 |
return defs; |
|
3480 |
} else { |
|
10815 | 3481 |
pos = token.pos; |
10 | 3482 |
List<JCTree> err = isVoid |
3483 |
? List.<JCTree>of(toP(F.at(pos).MethodDef(mods, name, type, typarams, |
|
3484 |
List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null))) |
|
3485 |
: null; |
|
10815 | 3486 |
return List.<JCTree>of(syntaxError(token.pos, err, "expected", LPAREN)); |
10 | 3487 |
} |
3488 |
} |
|
3489 |
} |
|
3490 |
} |
|
3491 |
} |
|
3492 |
||
3493 |
/** MethodDeclaratorRest = |
|
7072 | 3494 |
* FormalParameters BracketsOpt [Throws TypeList] ( MethodBody | [DEFAULT AnnotationValue] ";") |
10 | 3495 |
* VoidMethodDeclaratorRest = |
7072 | 3496 |
* FormalParameters [Throws TypeList] ( MethodBody | ";") |
10 | 3497 |
* InterfaceMethodDeclaratorRest = |
7072 | 3498 |
* FormalParameters BracketsOpt [THROWS TypeList] ";" |
10 | 3499 |
* VoidInterfaceMethodDeclaratorRest = |
7072 | 3500 |
* FormalParameters [THROWS TypeList] ";" |
10 | 3501 |
* ConstructorDeclaratorRest = |
7072 | 3502 |
* "(" FormalParameterListOpt ")" [THROWS TypeList] MethodBody |
10 | 3503 |
*/ |
11147
20770e90d993
7119032: (javac) increase visibility of JavacParser methods to improve subtyping
ksrini
parents:
11144
diff
changeset
|
3504 |
protected JCTree methodDeclaratorRest(int pos, |
10 | 3505 |
JCModifiers mods, |
3506 |
JCExpression type, |
|
3507 |
Name name, |
|
3508 |
List<JCTypeParameter> typarams, |
|
3509 |
boolean isInterface, boolean isVoid, |
|
13077 | 3510 |
Comment dc) { |
15377
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
3511 |
if (isInterface && (mods.flags & Flags.STATIC) != 0) { |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
3512 |
checkStaticInterfaceMethods(); |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
3513 |
} |
15385 | 3514 |
JCVariableDecl prevReceiverParam = this.receiverParam; |
3515 |
try { |
|
3516 |
this.receiverParam = null; |
|
3517 |
// Parsing formalParameters sets the receiverParam, if present |
|
3518 |
List<JCVariableDecl> params = formalParameters(); |
|
3519 |
if (!isVoid) type = bracketsOpt(type); |
|
3520 |
List<JCExpression> thrown = List.nil(); |
|
3521 |
if (token.kind == THROWS) { |
|
3522 |
nextToken(); |
|
3523 |
thrown = qualidentList(); |
|
3524 |
} |
|
3525 |
JCBlock body = null; |
|
3526 |
JCExpression defaultValue; |
|
3527 |
if (token.kind == LBRACE) { |
|
3528 |
body = block(); |
|
3529 |
defaultValue = null; |
|
10 | 3530 |
} else { |
15385 | 3531 |
if (token.kind == DEFAULT) { |
3532 |
accept(DEFAULT); |
|
3533 |
defaultValue = annotationValue(); |
|
3534 |
} else { |
|
3535 |
defaultValue = null; |
|
3536 |
} |
|
3537 |
accept(SEMI); |
|
3538 |
if (token.pos <= endPosTable.errorEndPos) { |
|
3539 |
// error recovery |
|
3540 |
skip(false, true, false, false); |
|
3541 |
if (token.kind == LBRACE) { |
|
3542 |
body = block(); |
|
3543 |
} |
|
10 | 3544 |
} |
3545 |
} |
|
15385 | 3546 |
|
3547 |
JCMethodDecl result = |
|
3548 |
toP(F.at(pos).MethodDef(mods, name, type, typarams, |
|
3549 |
receiverParam, params, thrown, |
|
3550 |
body, defaultValue)); |
|
3551 |
attach(result, dc); |
|
3552 |
return result; |
|
3553 |
} finally { |
|
3554 |
this.receiverParam = prevReceiverParam; |
|
10 | 3555 |
} |
3556 |
} |
|
3557 |
||
15385 | 3558 |
/** QualidentList = [Annotations] Qualident {"," [Annotations] Qualident} |
10 | 3559 |
*/ |
3560 |
List<JCExpression> qualidentList() { |
|
22163 | 3561 |
ListBuffer<JCExpression> ts = new ListBuffer<>(); |
15385 | 3562 |
|
3563 |
List<JCAnnotation> typeAnnos = typeAnnotationsOpt(); |
|
17578 | 3564 |
JCExpression qi = qualident(true); |
3565 |
if (!typeAnnos.isEmpty()) { |
|
3566 |
JCExpression at = insertAnnotationsToMostInner(qi, typeAnnos, false); |
|
3567 |
ts.append(at); |
|
3568 |
} else { |
|
3569 |
ts.append(qi); |
|
3570 |
} |
|
10815 | 3571 |
while (token.kind == COMMA) { |
3572 |
nextToken(); |
|
15385 | 3573 |
|
3574 |
typeAnnos = typeAnnotationsOpt(); |
|
17578 | 3575 |
qi = qualident(true); |
3576 |
if (!typeAnnos.isEmpty()) { |
|
3577 |
JCExpression at = insertAnnotationsToMostInner(qi, typeAnnos, false); |
|
3578 |
ts.append(at); |
|
3579 |
} else { |
|
3580 |
ts.append(qi); |
|
3581 |
} |
|
10 | 3582 |
} |
3583 |
return ts.toList(); |
|
3584 |
} |
|
3585 |
||
13844 | 3586 |
/** |
3587 |
* {@literal |
|
3588 |
* TypeParametersOpt = ["<" TypeParameter {"," TypeParameter} ">"] |
|
3589 |
* } |
|
10 | 3590 |
*/ |
3591 |
List<JCTypeParameter> typeParametersOpt() { |
|
10815 | 3592 |
if (token.kind == LT) { |
10 | 3593 |
checkGenerics(); |
22163 | 3594 |
ListBuffer<JCTypeParameter> typarams = new ListBuffer<>(); |
10815 | 3595 |
nextToken(); |
10 | 3596 |
typarams.append(typeParameter()); |
10815 | 3597 |
while (token.kind == COMMA) { |
3598 |
nextToken(); |
|
10 | 3599 |
typarams.append(typeParameter()); |
3600 |
} |
|
3601 |
accept(GT); |
|
3602 |
return typarams.toList(); |
|
3603 |
} else { |
|
3604 |
return List.nil(); |
|
3605 |
} |
|
3606 |
} |
|
3607 |
||
13844 | 3608 |
/** |
3609 |
* {@literal |
|
15385 | 3610 |
* TypeParameter = [Annotations] TypeVariable [TypeParameterBound] |
10 | 3611 |
* TypeParameterBound = EXTENDS Type {"&" Type} |
3612 |
* TypeVariable = Ident |
|
13844 | 3613 |
* } |
10 | 3614 |
*/ |
3615 |
JCTypeParameter typeParameter() { |
|
10815 | 3616 |
int pos = token.pos; |
15385 | 3617 |
List<JCAnnotation> annos = typeAnnotationsOpt(); |
10 | 3618 |
Name name = ident(); |
22163 | 3619 |
ListBuffer<JCExpression> bounds = new ListBuffer<>(); |
10815 | 3620 |
if (token.kind == EXTENDS) { |
3621 |
nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3622 |
bounds.append(parseType()); |
10815 | 3623 |
while (token.kind == AMP) { |
3624 |
nextToken(); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3625 |
bounds.append(parseType()); |
10 | 3626 |
} |
3627 |
} |
|
15385 | 3628 |
return toP(F.at(pos).TypeParameter(name, bounds.toList(), annos)); |
10 | 3629 |
} |
3630 |
||
3631 |
/** FormalParameters = "(" [ FormalParameterList ] ")" |
|
3632 |
* FormalParameterList = [ FormalParameterListNovarargs , ] LastFormalParameter |
|
3633 |
* FormalParameterListNovarargs = [ FormalParameterListNovarargs , ] FormalParameter |
|
3634 |
*/ |
|
3635 |
List<JCVariableDecl> formalParameters() { |
|
15367 | 3636 |
return formalParameters(false); |
3637 |
} |
|
3638 |
List<JCVariableDecl> formalParameters(boolean lambdaParameters) { |
|
22163 | 3639 |
ListBuffer<JCVariableDecl> params = new ListBuffer<>(); |
15385 | 3640 |
JCVariableDecl lastParam; |
10 | 3641 |
accept(LPAREN); |
10815 | 3642 |
if (token.kind != RPAREN) { |
15385 | 3643 |
this.allowThisIdent = true; |
3644 |
lastParam = formalParameter(lambdaParameters); |
|
17578 | 3645 |
if (lastParam.nameexpr != null) { |
15385 | 3646 |
this.receiverParam = lastParam; |
3647 |
} else { |
|
3648 |
params.append(lastParam); |
|
3649 |
} |
|
3650 |
this.allowThisIdent = false; |
|
22701
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3651 |
while (token.kind == COMMA) { |
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3652 |
if ((lastParam.mods.flags & Flags.VARARGS) != 0) { |
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3653 |
error(lastParam, "varargs.must.be.last"); |
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3654 |
} |
10815 | 3655 |
nextToken(); |
15367 | 3656 |
params.append(lastParam = formalParameter(lambdaParameters)); |
10 | 3657 |
} |
3658 |
} |
|
22701
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3659 |
if (token.kind == RPAREN) { |
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3660 |
nextToken(); |
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3661 |
} else { |
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3662 |
setErrorEndPos(token.pos); |
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3663 |
reportSyntaxError(S.prevToken().endPos, "expected3", COMMA, RPAREN, LBRACKET); |
67811024e8b4
8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents:
22165
diff
changeset
|
3664 |
} |
10 | 3665 |
return params.toList(); |
3666 |
} |
|
3667 |
||
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3668 |
List<JCVariableDecl> implicitParameters(boolean hasParens) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3669 |
if (hasParens) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3670 |
accept(LPAREN); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3671 |
} |
22163 | 3672 |
ListBuffer<JCVariableDecl> params = new ListBuffer<>(); |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3673 |
if (token.kind != RPAREN && token.kind != ARROW) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3674 |
params.append(implicitParameter()); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3675 |
while (token.kind == COMMA) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3676 |
nextToken(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3677 |
params.append(implicitParameter()); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3678 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3679 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3680 |
if (hasParens) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3681 |
accept(RPAREN); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3682 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3683 |
return params.toList(); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3684 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3685 |
|
10 | 3686 |
JCModifiers optFinal(long flags) { |
3687 |
JCModifiers mods = modifiersOpt(); |
|
3688 |
checkNoMods(mods.flags & ~(Flags.FINAL | Flags.DEPRECATED)); |
|
3689 |
mods.flags |= flags; |
|
3690 |
return mods; |
|
3691 |
} |
|
3692 |
||
15385 | 3693 |
/** |
3694 |
* Inserts the annotations (and possibly a new array level) |
|
3695 |
* to the left-most type in an array or nested type. |
|
3696 |
* |
|
3697 |
* When parsing a type like {@code @B Outer.Inner @A []}, the |
|
3698 |
* {@code @A} annotation should target the array itself, while |
|
3699 |
* {@code @B} targets the nested type {@code Outer}. |
|
3700 |
* |
|
3701 |
* Currently the parser parses the annotation first, then |
|
3702 |
* the array, and then inserts the annotation to the left-most |
|
3703 |
* nested type. |
|
3704 |
* |
|
3705 |
* When {@code createNewLevel} is true, then a new array |
|
3706 |
* level is inserted as the most inner type, and have the |
|
3707 |
* annotations target it. This is useful in the case of |
|
3708 |
* varargs, e.g. {@code String @A [] @B ...}, as the parser |
|
3709 |
* first parses the type {@code String @A []} then inserts |
|
3710 |
* a new array level with {@code @B} annotation. |
|
3711 |
*/ |
|
3712 |
private JCExpression insertAnnotationsToMostInner( |
|
3713 |
JCExpression type, List<JCAnnotation> annos, |
|
3714 |
boolean createNewLevel) { |
|
3715 |
int origEndPos = getEndPos(type); |
|
3716 |
JCExpression mostInnerType = type; |
|
3717 |
JCArrayTypeTree mostInnerArrayType = null; |
|
3718 |
while (TreeInfo.typeIn(mostInnerType).hasTag(TYPEARRAY)) { |
|
3719 |
mostInnerArrayType = (JCArrayTypeTree) TreeInfo.typeIn(mostInnerType); |
|
3720 |
mostInnerType = mostInnerArrayType.elemtype; |
|
3721 |
} |
|
3722 |
||
3723 |
if (createNewLevel) { |
|
3724 |
mostInnerType = to(F.at(token.pos).TypeArray(mostInnerType)); |
|
3725 |
} |
|
3726 |
||
3727 |
JCExpression mostInnerTypeToReturn = mostInnerType; |
|
3728 |
if (annos.nonEmpty()) { |
|
3729 |
JCExpression lastToModify = mostInnerType; |
|
3730 |
||
3731 |
while (TreeInfo.typeIn(mostInnerType).hasTag(SELECT) || |
|
3732 |
TreeInfo.typeIn(mostInnerType).hasTag(TYPEAPPLY)) { |
|
3733 |
while (TreeInfo.typeIn(mostInnerType).hasTag(SELECT)) { |
|
3734 |
lastToModify = mostInnerType; |
|
3735 |
mostInnerType = ((JCFieldAccess) TreeInfo.typeIn(mostInnerType)).getExpression(); |
|
3736 |
} |
|
3737 |
while (TreeInfo.typeIn(mostInnerType).hasTag(TYPEAPPLY)) { |
|
3738 |
lastToModify = mostInnerType; |
|
3739 |
mostInnerType = ((JCTypeApply) TreeInfo.typeIn(mostInnerType)).clazz; |
|
3740 |
} |
|
3741 |
} |
|
3742 |
||
3743 |
mostInnerType = F.at(annos.head.pos).AnnotatedType(annos, mostInnerType); |
|
3744 |
||
3745 |
if (TreeInfo.typeIn(lastToModify).hasTag(TYPEAPPLY)) { |
|
3746 |
((JCTypeApply) TreeInfo.typeIn(lastToModify)).clazz = mostInnerType; |
|
3747 |
} else if (TreeInfo.typeIn(lastToModify).hasTag(SELECT)) { |
|
3748 |
((JCFieldAccess) TreeInfo.typeIn(lastToModify)).selected = mostInnerType; |
|
3749 |
} else { |
|
3750 |
// We never saw a SELECT or TYPEAPPLY, return the annotated type. |
|
3751 |
mostInnerTypeToReturn = mostInnerType; |
|
3752 |
} |
|
3753 |
} |
|
3754 |
||
3755 |
if (mostInnerArrayType == null) { |
|
3756 |
return mostInnerTypeToReturn; |
|
3757 |
} else { |
|
3758 |
mostInnerArrayType.elemtype = mostInnerTypeToReturn; |
|
3759 |
storeEnd(type, origEndPos); |
|
3760 |
return type; |
|
3761 |
} |
|
3762 |
} |
|
3763 |
||
10 | 3764 |
/** FormalParameter = { FINAL | '@' Annotation } Type VariableDeclaratorId |
3765 |
* LastFormalParameter = { FINAL | '@' Annotation } Type '...' Ident | FormalParameter |
|
3766 |
*/ |
|
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3767 |
protected JCVariableDecl formalParameter() { |
15367 | 3768 |
return formalParameter(false); |
3769 |
} |
|
3770 |
protected JCVariableDecl formalParameter(boolean lambdaParameter) { |
|
10 | 3771 |
JCModifiers mods = optFinal(Flags.PARAMETER); |
15385 | 3772 |
// need to distinguish between vararg annos and array annos |
3773 |
// look at typeAnnotationsPushedBack comment |
|
3774 |
this.permitTypeAnnotationsPushBack = true; |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
939
diff
changeset
|
3775 |
JCExpression type = parseType(); |
15385 | 3776 |
this.permitTypeAnnotationsPushBack = false; |
3777 |
||
10815 | 3778 |
if (token.kind == ELLIPSIS) { |
15385 | 3779 |
List<JCAnnotation> varargsAnnos = typeAnnotationsPushedBack; |
3780 |
typeAnnotationsPushedBack = List.nil(); |
|
10 | 3781 |
checkVarargs(); |
3782 |
mods.flags |= Flags.VARARGS; |
|
15385 | 3783 |
// insert var arg type annotations |
3784 |
type = insertAnnotationsToMostInner(type, varargsAnnos, true); |
|
10815 | 3785 |
nextToken(); |
15385 | 3786 |
} else { |
3787 |
// if not a var arg, then typeAnnotationsPushedBack should be null |
|
3788 |
if (typeAnnotationsPushedBack.nonEmpty()) { |
|
3789 |
reportSyntaxError(typeAnnotationsPushedBack.head.pos, |
|
3790 |
"illegal.start.of.type"); |
|
3791 |
} |
|
3792 |
typeAnnotationsPushedBack = List.nil(); |
|
10 | 3793 |
} |
15367 | 3794 |
return variableDeclaratorId(mods, type, lambdaParameter); |
10 | 3795 |
} |
3796 |
||
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3797 |
protected JCVariableDecl implicitParameter() { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3798 |
JCModifiers mods = F.at(token.pos).Modifiers(Flags.PARAMETER); |
15367 | 3799 |
return variableDeclaratorId(mods, null, true); |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3800 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
3801 |
|
10 | 3802 |
/* ---------- auxiliary methods -------------- */ |
3803 |
||
7076
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
3804 |
void error(int pos, String key, Object ... args) { |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
3805 |
log.error(DiagnosticFlag.SYNTAX, pos, key, args); |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
3806 |
} |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
3807 |
|
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3808 |
void error(DiagnosticPosition pos, String key, Object ... args) { |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3809 |
log.error(DiagnosticFlag.SYNTAX, pos, key, args); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3810 |
} |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3811 |
|
7076
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
3812 |
void warning(int pos, String key, Object ... args) { |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
3813 |
log.warning(pos, key, args); |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
3814 |
} |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
7074
diff
changeset
|
3815 |
|
10 | 3816 |
/** Check that given tree is a legal expression statement. |
3817 |
*/ |
|
3818 |
protected JCExpression checkExprStat(JCExpression t) { |
|
14722
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14547
diff
changeset
|
3819 |
if (!TreeInfo.isExpressionStatement(t)) { |
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3820 |
JCExpression ret = F.at(t.pos).Erroneous(List.<JCTree>of(t)); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3821 |
error(ret, "not.stmt"); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
10200
diff
changeset
|
3822 |
return ret; |
14722
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14547
diff
changeset
|
3823 |
} else { |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14547
diff
changeset
|
3824 |
return t; |
10 | 3825 |
} |
3826 |
} |
|
3827 |
||
3828 |
/** Return precedence of operator represented by token, |
|
3829 |
* -1 if token is not a binary operator. @see TreeInfo.opPrec |
|
3830 |
*/ |
|
10815 | 3831 |
static int prec(TokenKind token) { |
10950 | 3832 |
JCTree.Tag oc = optag(token); |
3833 |
return (oc != NO_TAG) ? TreeInfo.opPrec(oc) : -1; |
|
10 | 3834 |
} |
3835 |
||
5013
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3836 |
/** |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3837 |
* Return the lesser of two positions, making allowance for either one |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3838 |
* being unset. |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3839 |
*/ |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3840 |
static int earlier(int pos1, int pos2) { |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3841 |
if (pos1 == Position.NOPOS) |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3842 |
return pos2; |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3843 |
if (pos2 == Position.NOPOS) |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3844 |
return pos1; |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3845 |
return (pos1 < pos2 ? pos1 : pos2); |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3846 |
} |
e942b2e52479
6931927: position issues with synthesized anonymous class
jjg
parents:
4870
diff
changeset
|
3847 |
|
10 | 3848 |
/** Return operation tag of binary operator represented by token, |
10950 | 3849 |
* No_TAG if token is not a binary operator. |
10 | 3850 |
*/ |
10950 | 3851 |
static JCTree.Tag optag(TokenKind token) { |
10 | 3852 |
switch (token) { |
3853 |
case BARBAR: |
|
10950 | 3854 |
return OR; |
10 | 3855 |
case AMPAMP: |
10950 | 3856 |
return AND; |
10 | 3857 |
case BAR: |
10950 | 3858 |
return BITOR; |
10 | 3859 |
case BAREQ: |
10950 | 3860 |
return BITOR_ASG; |
10 | 3861 |
case CARET: |
10950 | 3862 |
return BITXOR; |
10 | 3863 |
case CARETEQ: |
10950 | 3864 |
return BITXOR_ASG; |
10 | 3865 |
case AMP: |
10950 | 3866 |
return BITAND; |
10 | 3867 |
case AMPEQ: |
10950 | 3868 |
return BITAND_ASG; |
10 | 3869 |
case EQEQ: |
10950 | 3870 |
return JCTree.Tag.EQ; |
10 | 3871 |
case BANGEQ: |
10950 | 3872 |
return NE; |
10 | 3873 |
case LT: |
10950 | 3874 |
return JCTree.Tag.LT; |
10 | 3875 |
case GT: |
10950 | 3876 |
return JCTree.Tag.GT; |
10 | 3877 |
case LTEQ: |
10950 | 3878 |
return LE; |
10 | 3879 |
case GTEQ: |
10950 | 3880 |
return GE; |
10 | 3881 |
case LTLT: |
10950 | 3882 |
return SL; |
10 | 3883 |
case LTLTEQ: |
10950 | 3884 |
return SL_ASG; |
10 | 3885 |
case GTGT: |
10950 | 3886 |
return SR; |
10 | 3887 |
case GTGTEQ: |
10950 | 3888 |
return SR_ASG; |
10 | 3889 |
case GTGTGT: |
10950 | 3890 |
return USR; |
10 | 3891 |
case GTGTGTEQ: |
10950 | 3892 |
return USR_ASG; |
10 | 3893 |
case PLUS: |
10950 | 3894 |
return JCTree.Tag.PLUS; |
10 | 3895 |
case PLUSEQ: |
10950 | 3896 |
return PLUS_ASG; |
10 | 3897 |
case SUB: |
10950 | 3898 |
return MINUS; |
10 | 3899 |
case SUBEQ: |
10950 | 3900 |
return MINUS_ASG; |
10 | 3901 |
case STAR: |
10950 | 3902 |
return MUL; |
10 | 3903 |
case STAREQ: |
10950 | 3904 |
return MUL_ASG; |
10 | 3905 |
case SLASH: |
10950 | 3906 |
return DIV; |
10 | 3907 |
case SLASHEQ: |
10950 | 3908 |
return DIV_ASG; |
10 | 3909 |
case PERCENT: |
10950 | 3910 |
return MOD; |
10 | 3911 |
case PERCENTEQ: |
10950 | 3912 |
return MOD_ASG; |
10 | 3913 |
case INSTANCEOF: |
10950 | 3914 |
return TYPETEST; |
10 | 3915 |
default: |
10950 | 3916 |
return NO_TAG; |
10 | 3917 |
} |
3918 |
} |
|
3919 |
||
3920 |
/** Return operation tag of unary operator represented by token, |
|
10950 | 3921 |
* No_TAG if token is not a binary operator. |
10 | 3922 |
*/ |
10950 | 3923 |
static JCTree.Tag unoptag(TokenKind token) { |
10 | 3924 |
switch (token) { |
3925 |
case PLUS: |
|
10950 | 3926 |
return POS; |
10 | 3927 |
case SUB: |
10950 | 3928 |
return NEG; |
10 | 3929 |
case BANG: |
10950 | 3930 |
return NOT; |
10 | 3931 |
case TILDE: |
10950 | 3932 |
return COMPL; |
10 | 3933 |
case PLUSPLUS: |
10950 | 3934 |
return PREINC; |
10 | 3935 |
case SUBSUB: |
10950 | 3936 |
return PREDEC; |
10 | 3937 |
default: |
10950 | 3938 |
return NO_TAG; |
10 | 3939 |
} |
3940 |
} |
|
3941 |
||
3942 |
/** Return type tag of basic type represented by token, |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3943 |
* NONE if token is not a basic type identifier. |
10 | 3944 |
*/ |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3945 |
static TypeTag typetag(TokenKind token) { |
10 | 3946 |
switch (token) { |
3947 |
case BYTE: |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3948 |
return TypeTag.BYTE; |
10 | 3949 |
case CHAR: |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3950 |
return TypeTag.CHAR; |
10 | 3951 |
case SHORT: |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3952 |
return TypeTag.SHORT; |
10 | 3953 |
case INT: |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3954 |
return TypeTag.INT; |
10 | 3955 |
case LONG: |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3956 |
return TypeTag.LONG; |
10 | 3957 |
case FLOAT: |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3958 |
return TypeTag.FLOAT; |
10 | 3959 |
case DOUBLE: |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3960 |
return TypeTag.DOUBLE; |
10 | 3961 |
case BOOLEAN: |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3962 |
return TypeTag.BOOLEAN; |
10 | 3963 |
default: |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
3964 |
return TypeTag.NONE; |
10 | 3965 |
} |
3966 |
} |
|
3967 |
||
3968 |
void checkGenerics() { |
|
3969 |
if (!allowGenerics) { |
|
10815 | 3970 |
error(token.pos, "generics.not.supported.in.source", source.name); |
10 | 3971 |
allowGenerics = true; |
3972 |
} |
|
3973 |
} |
|
3974 |
void checkVarargs() { |
|
3975 |
if (!allowVarargs) { |
|
10815 | 3976 |
error(token.pos, "varargs.not.supported.in.source", source.name); |
10 | 3977 |
allowVarargs = true; |
3978 |
} |
|
3979 |
} |
|
3980 |
void checkForeach() { |
|
3981 |
if (!allowForeach) { |
|
10815 | 3982 |
error(token.pos, "foreach.not.supported.in.source", source.name); |
10 | 3983 |
allowForeach = true; |
3984 |
} |
|
3985 |
} |
|
3986 |
void checkStaticImports() { |
|
3987 |
if (!allowStaticImport) { |
|
10815 | 3988 |
error(token.pos, "static.import.not.supported.in.source", source.name); |
10 | 3989 |
allowStaticImport = true; |
3990 |
} |
|
3991 |
} |
|
3992 |
void checkAnnotations() { |
|
3993 |
if (!allowAnnotations) { |
|
10815 | 3994 |
error(token.pos, "annotations.not.supported.in.source", source.name); |
10 | 3995 |
allowAnnotations = true; |
3996 |
} |
|
3997 |
} |
|
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
3998 |
void checkDiamond() { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
3999 |
if (!allowDiamond) { |
10815 | 4000 |
error(token.pos, "diamond.not.supported.in.source", source.name); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
4001 |
allowDiamond = true; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
4002 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
4003 |
} |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
4004 |
void checkMulticatch() { |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
4005 |
if (!allowMulticatch) { |
10815 | 4006 |
error(token.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
|
4007 |
allowMulticatch = true; |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
4008 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
4009 |
} |
7211
163fe60f63de
6970016: Clean up ARM/try-with-resources implementation
mcimadamore
parents:
7076
diff
changeset
|
4010 |
void checkTryWithResources() { |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
4011 |
if (!allowTWR) { |
10815 | 4012 |
error(token.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
|
4013 |
allowTWR = true; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6146
diff
changeset
|
4014 |
} |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
4015 |
} |
11143
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
4016 |
void checkLambda() { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
4017 |
if (!allowLambda) { |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
4018 |
log.error(token.pos, "lambda.not.supported.in.source", source.name); |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
4019 |
allowLambda = true; |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
4020 |
} |
9dbe313bfb74
7115050: Add parser support for lambda expressions
mcimadamore
parents:
11055
diff
changeset
|
4021 |
} |
11144
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
4022 |
void checkMethodReferences() { |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
4023 |
if (!allowMethodReferences) { |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
4024 |
log.error(token.pos, "method.references.not.supported.in.source", source.name); |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
4025 |
allowMethodReferences = true; |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
4026 |
} |
8a4ae514eedf
7115052: Add parser support for method references
mcimadamore
parents:
11143
diff
changeset
|
4027 |
} |
14267
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
4028 |
void checkDefaultMethods() { |
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
4029 |
if (!allowDefaultMethods) { |
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
4030 |
log.error(token.pos, "default.methods.not.supported.in.source", source.name); |
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
4031 |
allowDefaultMethods = true; |
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
4032 |
} |
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14263
diff
changeset
|
4033 |
} |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
4034 |
void checkIntersectionTypesInCast() { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
4035 |
if (!allowIntersectionTypesInCast) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
4036 |
log.error(token.pos, "intersection.types.in.cast.not.supported.in.source", source.name); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
4037 |
allowIntersectionTypesInCast = true; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
4038 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14722
diff
changeset
|
4039 |
} |
15377
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
4040 |
void checkStaticInterfaceMethods() { |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
4041 |
if (!allowStaticInterfaceMethods) { |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
4042 |
log.error(token.pos, "static.intf.methods.not.supported.in.source", source.name); |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
4043 |
allowStaticInterfaceMethods = true; |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
4044 |
} |
515846bb6637
8005166: Add support for static interface methods
mcimadamore
parents:
15375
diff
changeset
|
4045 |
} |
15385 | 4046 |
void checkTypeAnnotations() { |
4047 |
if (!allowTypeAnnotations) { |
|
4048 |
log.error(token.pos, "type.annotations.not.supported.in.source", source.name); |
|
4049 |
allowTypeAnnotations = true; |
|
4050 |
} |
|
4051 |
} |
|
11055 | 4052 |
|
4053 |
/* |
|
4054 |
* a functional source tree and end position mappings |
|
4055 |
*/ |
|
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4056 |
protected static class SimpleEndPosTable extends AbstractEndPosTable { |
11055 | 4057 |
|
23131 | 4058 |
private final IntHashTable endPosMap; |
11055 | 4059 |
|
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4060 |
SimpleEndPosTable(JavacParser parser) { |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4061 |
super(parser); |
23131 | 4062 |
endPosMap = new IntHashTable(); |
11055 | 4063 |
} |
4064 |
||
19122
1841f2fa76de
8017216: javac doesn't fill in end position for some errors of type not found
ksrini
parents:
18645
diff
changeset
|
4065 |
public void storeEnd(JCTree tree, int endpos) { |
23131 | 4066 |
endPosMap.putAtIndex(tree, errorEndPos > endpos ? errorEndPos : endpos, |
4067 |
endPosMap.lookup(tree)); |
|
11055 | 4068 |
} |
4069 |
||
4070 |
protected <T extends JCTree> T to(T t) { |
|
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4071 |
storeEnd(t, parser.token.endPos); |
11055 | 4072 |
return t; |
4073 |
} |
|
4074 |
||
4075 |
protected <T extends JCTree> T toP(T t) { |
|
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4076 |
storeEnd(t, parser.S.prevToken().endPos); |
11055 | 4077 |
return t; |
4078 |
} |
|
4079 |
||
4080 |
public int getEndPos(JCTree tree) { |
|
23131 | 4081 |
int value = endPosMap.getFromIndex(endPosMap.lookup(tree)); |
4082 |
// As long as Position.NOPOS==-1, this just returns value. |
|
4083 |
return (value == -1) ? Position.NOPOS : value; |
|
11055 | 4084 |
} |
4085 |
||
4086 |
public int replaceTree(JCTree oldTree, JCTree newTree) { |
|
23131 | 4087 |
int pos = endPosMap.remove(oldTree); |
4088 |
if (pos != -1) { |
|
4089 |
storeEnd(newTree, pos); |
|
11055 | 4090 |
return pos; |
4091 |
} |
|
4092 |
return Position.NOPOS; |
|
4093 |
} |
|
4094 |
} |
|
4095 |
||
4096 |
/* |
|
4097 |
* a default skeletal implementation without any mapping overhead. |
|
4098 |
*/ |
|
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4099 |
protected static class EmptyEndPosTable extends AbstractEndPosTable { |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4100 |
|
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4101 |
EmptyEndPosTable(JavacParser parser) { |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4102 |
super(parser); |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4103 |
} |
11055 | 4104 |
|
19122
1841f2fa76de
8017216: javac doesn't fill in end position for some errors of type not found
ksrini
parents:
18645
diff
changeset
|
4105 |
public void storeEnd(JCTree tree, int endpos) { /* empty */ } |
11055 | 4106 |
|
4107 |
protected <T extends JCTree> T to(T t) { |
|
4108 |
return t; |
|
4109 |
} |
|
4110 |
||
4111 |
protected <T extends JCTree> T toP(T t) { |
|
4112 |
return t; |
|
4113 |
} |
|
4114 |
||
4115 |
public int getEndPos(JCTree tree) { |
|
4116 |
return Position.NOPOS; |
|
4117 |
} |
|
4118 |
||
4119 |
public int replaceTree(JCTree oldTree, JCTree newTree) { |
|
4120 |
return Position.NOPOS; |
|
4121 |
} |
|
4122 |
||
4123 |
} |
|
4124 |
||
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4125 |
protected static abstract class AbstractEndPosTable implements EndPosTable { |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4126 |
/** |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4127 |
* The current parser. |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4128 |
*/ |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4129 |
protected JavacParser parser; |
11055 | 4130 |
|
4131 |
/** |
|
4132 |
* Store the last error position. |
|
4133 |
*/ |
|
4134 |
protected int errorEndPos; |
|
4135 |
||
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4136 |
public AbstractEndPosTable(JavacParser parser) { |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4137 |
this.parser = parser; |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4138 |
} |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4139 |
|
11055 | 4140 |
/** |
4141 |
* Store current token's ending position for a tree, the value of which |
|
4142 |
* will be the greater of last error position and the ending position of |
|
4143 |
* the current token. |
|
4144 |
* @param t The tree. |
|
4145 |
*/ |
|
4146 |
protected abstract <T extends JCTree> T to(T t); |
|
4147 |
||
4148 |
/** |
|
4149 |
* Store current token's ending position for a tree, the value of which |
|
4150 |
* will be the greater of last error position and the ending position of |
|
4151 |
* the previous token. |
|
4152 |
* @param t The tree. |
|
4153 |
*/ |
|
4154 |
protected abstract <T extends JCTree> T toP(T t); |
|
4155 |
||
4156 |
/** |
|
4157 |
* Set the error position during the parsing phases, the value of which |
|
4158 |
* will be set only if it is greater than the last stored error position. |
|
4159 |
* @param errPos The error position |
|
4160 |
*/ |
|
4161 |
protected void setErrorEndPos(int errPos) { |
|
4162 |
if (errPos > errorEndPos) { |
|
4163 |
errorEndPos = errPos; |
|
4164 |
} |
|
4165 |
} |
|
16811
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4166 |
|
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4167 |
protected void setParser(JavacParser parser) { |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4168 |
this.parser = parser; |
fca5473a5823
8011677: EndPosTables should avoid hidden references to Parser
jjg
parents:
16296
diff
changeset
|
4169 |
} |
11055 | 4170 |
} |
10 | 4171 |
} |