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