Skip to content

Commit b0e62aa

Browse files
committed
WIP
1 parent dbf4fff commit b0e62aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1410
-113
lines changed

src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public enum Feature {
8181
@JEP(number=470, title="PEM Encodings of Cryptographic Objects", status="Preview")
8282
PEM_API,
8383
LANGUAGE_MODEL,
84+
@JEP(number=999, title="Match Statements", status="First Preview")
85+
MATCH_STATEMENTS(),
8486
/**
8587
* A key for testing.
8688
*/

src/jdk.compiler/share/classes/com/sun/source/tree/EnhancedForLoopTree.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package com.sun.source.tree;
2727

28+
import jdk.internal.javac.PreviewFeature;
29+
2830
/**
2931
* A tree node for an "enhanced" {@code for} loop statement.
3032
*
@@ -41,12 +43,37 @@
4143
* @since 1.6
4244
*/
4345
public interface EnhancedForLoopTree extends StatementTree {
46+
/**
47+
* "Enhanced" {@code for} declarations come in two forms:
48+
* <ul>
49+
* <li> local variable declarations and
50+
* <li> record patterns
51+
* </ul>
52+
*
53+
* @since 26
54+
*/
55+
@PreviewFeature(feature=PreviewFeature.Feature.MATCH_STATEMENTS, reflective=true)
56+
public enum DeclarationKind {
57+
/** enum constant for local variable declarations */
58+
VARIABLE,
59+
/** enum constant for record pattern declarations */
60+
PATTERN
61+
}
62+
4463
/**
4564
* Returns the control variable for the loop.
46-
* @return the control variable
65+
* @return the control variable, or {@code null} if this "enhanced" {@code for} uses a pattern
4766
*/
4867
VariableTree getVariable();
4968

69+
/**
70+
* Returns the control variable or pattern for the loop.
71+
* @return the control variable or pattern
72+
* @since 26
73+
*/
74+
@PreviewFeature(feature=PreviewFeature.Feature.MATCH_STATEMENTS, reflective=true)
75+
Tree getVariableOrRecordPattern();
76+
5077
/**
5178
* Returns the expression yielding the values for the control variable.
5279
* @return the expression
@@ -58,4 +85,12 @@ public interface EnhancedForLoopTree extends StatementTree {
5885
* @return the body of the loop
5986
*/
6087
StatementTree getStatement();
61-
}
88+
89+
/**
90+
* Returns the kind of the declaration of the "enhanced" {@code for}.
91+
* @return the kind of the declaration
92+
* @since 20
93+
*/
94+
@PreviewFeature(feature=PreviewFeature.Feature.MATCH_STATEMENTS, reflective=true)
95+
DeclarationKind getDeclarationKind();
96+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
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
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
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+
*
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.
24+
*/
25+
26+
package com.sun.source.tree;
27+
28+
import jdk.internal.javac.PreviewFeature;
29+
30+
/**
31+
* A tree node for a "match" {@code match} statement.
32+
*
33+
* For example:
34+
* <pre>
35+
* match <em>record pattern</em> = <em>expression</em>;
36+
* </pre>
37+
*
38+
* @jls 14.23.1 match statements
39+
*
40+
* @author Angelos Bimpoudis
41+
* @since 21
42+
*/
43+
public interface MatchStatementTree extends StatementTree {
44+
/**
45+
* Returns the pattern for the match statement.
46+
* @return pattern
47+
*/
48+
@PreviewFeature(feature=PreviewFeature.Feature.MATCH_STATEMENTS, reflective=true)
49+
Tree getPattern();
50+
51+
/**
52+
* Returns the expression to be matched.
53+
* @return the expression
54+
*/
55+
@PreviewFeature(feature=PreviewFeature.Feature.MATCH_STATEMENTS, reflective=true)
56+
ExpressionTree getExpression();
57+
}

src/jdk.compiler/share/classes/com/sun/source/tree/Tree.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public enum Kind {
7878
*/
7979
ASSERT(AssertTree.class),
8080

81+
/**
82+
* Used for instances of {@link MatchStatementTree}.
83+
*/
84+
MATCH_STATEMENT(MatchStatementTree.class),
85+
8186
/**
8287
* Used for instances of {@link AssignmentTree}.
8388
*/

src/jdk.compiler/share/classes/com/sun/source/tree/TreeVisitor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ public interface TreeVisitor<R,P> {
8989
*/
9090
R visitAssert(AssertTree node, P p);
9191

92+
/**
93+
* Visits an {@code MatchStatementTree} node.
94+
* @param node the node being visited
95+
* @param p a parameter value
96+
* @return a result value
97+
*/
98+
R visitMatchStatement(MatchStatementTree node, P p);
99+
92100
/**
93101
* Visits an {@code AssignmentTree} node.
94102
* @param node the node being visited

src/jdk.compiler/share/classes/com/sun/source/util/SimpleTreeVisitor.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,20 @@ public R visitAssert(AssertTree node, P p) {
475475
return defaultAction(node, p);
476476
}
477477

478+
/**
479+
* {@inheritDoc}
480+
*
481+
* @implSpec This implementation calls {@code defaultAction}.
482+
*
483+
* @param node {@inheritDoc}
484+
* @param p {@inheritDoc}
485+
* @return the result of {@code defaultAction}
486+
*/
487+
@Override
488+
public R visitMatchStatement(MatchStatementTree node, P p) {
489+
return defaultAction(node, p);
490+
}
491+
478492
/**
479493
* {@inheritDoc}
480494
*

src/jdk.compiler/share/classes/com/sun/source/util/TreeScanner.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,28 @@ public R visitForLoop(ForLoopTree node, P p) {
332332
*/
333333
@Override
334334
public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
335-
R r = scan(node.getVariable(), p);
335+
R r = scan(node.getVariableOrRecordPattern(), p);
336336
r = scanAndReduce(node.getExpression(), p, r);
337337
r = scanAndReduce(node.getStatement(), p, r);
338338
return r;
339339
}
340340

341+
/**
342+
* {@inheritDoc}
343+
*
344+
* @implSpec This implementation scans the children in left to right order.
345+
*
346+
* @param node {@inheritDoc}
347+
* @param p {@inheritDoc}
348+
* @return the result of scanning
349+
*/
350+
@Override
351+
public R visitMatchStatement(MatchStatementTree node, P p) {
352+
R r = scan(node.getPattern(), p);
353+
r = scanAndReduce(node.getExpression(), p, r);
354+
return r;
355+
}
356+
341357
/**
342358
* {@inheritDoc}
343359
*

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public boolean isEnabled() {
212212
public boolean isPreview(Feature feature) {
213213
return switch (feature) {
214214
case PRIMITIVE_PATTERNS -> true;
215+
case MATCH_STATEMENTS -> true;
215216
//Note: this is a backdoor which allows to optionally treat all features as 'preview' (for testing).
216217
//When real preview features will be added, this method can be implemented to return 'true'
217218
//for those selected features, and 'false' for all the others.

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ public enum Feature {
277277
JAVA_BASE_TRANSITIVE(JDK25, Fragments.FeatureJavaBaseTransitive, DiagKind.PLURAL),
278278
PRIVATE_MEMBERS_IN_PERMITS_CLAUSE(JDK19),
279279
ERASE_POLY_SIG_RETURN_TYPE(JDK24),
280+
MATCH_STATEMENTS(JDK26, Fragments.FeatureMatchStatements, DiagKind.PLURAL),
280281
;
281282

282283
enum DiagKind {

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public static Symtab instance(Context context) {
191191
public final Type incompatibleClassChangeErrorType;
192192
public final Type cloneNotSupportedExceptionType;
193193
public final Type matchExceptionType;
194+
public final Type nullPointerExceptionType;
194195
public final Type annotationType;
195196
public final TypeSymbol enumSym;
196197
public final Type listType;
@@ -570,6 +571,7 @@ public <R, P> R accept(ElementVisitor<R, P> v, P p) {
570571
incompatibleClassChangeErrorType = enterClass("java.lang.IncompatibleClassChangeError");
571572
cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException");
572573
matchExceptionType = enterClass("java.lang.MatchException");
574+
nullPointerExceptionType = enterClass("java.lang.NullPointerException");
573575
annotationType = enterClass("java.lang.annotation.Annotation");
574576
classLoaderType = enterClass("java.lang.ClassLoader");
575577
enumSym = enterClass(java_base, names.java_lang_Enum);

0 commit comments

Comments
 (0)