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