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