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