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