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