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