Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List of patterns in case statements #1742

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8389ad0
* Redundant calls to
srikanth-sankaran Feb 12, 2024
7b0ef8e
* Withdraw the internal AST APIs Pattern.suspendVariables &
srikanth-sankaran Feb 14, 2024
e7d7ab2
* Various code cleanups in pattern matching
srikanth-sankaran Feb 14, 2024
646c8bc
* toString() output of AST nodes shows up with modifier sealed for
srikanth-sankaran Feb 14, 2024
226970e
* High degree of code duplication in instanceof pattern code generation
srikanth-sankaran Feb 14, 2024
9b3d813
* Exclude unnamed pattern bindings from scope, flow analysis and code
srikanth-sankaran Feb 16, 2024
1fd5440
* ECJ incorrectly allows boxing and primitive widening/narrowing at
srikanth-sankaran Feb 16, 2024
a734e9b
* Instanceof with record deconstruction patterns should never be flagged
srikanth-sankaran Feb 17, 2024
c36277c
Various cleanups
srikanth-sankaran Feb 17, 2024
e3da39a
* Unnecessary code generated for pattern switch
srikanth-sankaran Feb 17, 2024
4538cfb
* Rewire RecordPattern to extend Pattern rather than TypePattern
srikanth-sankaran Feb 18, 2024
a229ca3
* Code completion support for instance of type pattern
srikanth-sankaran Feb 18, 2024
bb94409
* Eliminate repeated resolution attempts on patterns
srikanth-sankaran Feb 18, 2024
b7036d2
* org.eclipse.jdt.core.dom.Expression#resolveTypeBinding returns null
srikanth-sankaran Feb 19, 2024
96105b2
* Grammar file references impossible and nop reduce action methods
srikanth-sankaran Feb 19, 2024
429aeb1
Fix for problems reported by Jay introduced by the fix for
srikanth-sankaran Feb 20, 2024
82cbff3
Fix for problem reported by Jay introduced by the fix for
srikanth-sankaran Feb 20, 2024
6477b93
Fix for problem reported by Jay introduced by the fix for
srikanth-sankaran Feb 21, 2024
a9de560
* Guarded patterns are incorrectly held to be unconditional
srikanth-sankaran Feb 21, 2024
588a3e3
Parse multiple Patterns in a case statement
datho7561 Nov 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions org.eclipse.jdt.core.compiler.batch/grammar/java.g
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ Goal ::= '->' SwitchLabelCaseLhs
Goal ::= RestrictedIdentifiersealed Modifiersopt
Goal ::= RestrictedIdentifierpermits PermittedSubclasses
-- jsr 427 --
Goal ::= BeginCaseElement Pattern
Goal ::= CaseLabelPatternElements
Goal ::= RestrictedIdentifierWhen Expression
/:$readableName Goal:/

Expand Down Expand Up @@ -1244,21 +1244,18 @@ InstanceofExpression ::= InstanceofExpression InstanceofRHS

InstanceofRHS -> InstanceofClassic
InstanceofRHS -> InstanceofPattern
/.$putCase consumeInstanceOfRHS(); $break ./
/:$readableName Expression:/
/:$readableName InstanceofRHS:/

InstanceofClassic ::= 'instanceof' Modifiersopt Type
/.$putCase consumeInstanceOfClassic(); $break ./
/:$readableName InstanceofClassic:/

InstanceofPattern ::= 'instanceof' Pattern
/.$putCase consumeInstanceofPattern(); $break ./
InstanceofPattern -> 'instanceof' Pattern
/:$readableName InstanceofPattern:/


Pattern -> TypePattern
Pattern -> RecordPattern
/.$putCase consumePattern(); $break ./
/:$readableName Pattern:/

TypePattern ::= Modifiersopt Type 'Identifier'
Expand Down Expand Up @@ -1591,34 +1588,41 @@ SwitchLabelCaseLhs ::= 'case' CaseLabelElements

-- END SwitchExpression (JEP 325) --

CaseLabelElements -> CaseLabelElement
CaseLabelElements ::= CaseLabelElements ',' CaseLabelElement
CaseLabelElements -> CaseLabelConstantElements
CaseLabelElements -> CaseLabelPatternElements

CaseLabelConstantElements ::= CaseLabelConstantElement
CaseLabelConstantElements ::= CaseLabelConstantElements ',' CaseLabelConstantElement
/.$putCase consumeCaseLabelElements(); $break ./
/:$readableName CaseLabelElements:/
/:$readableName CaseLabelConstantElements:/

-- Production name hardcoded in parser. Must be ::= and not -> (need to hook at cCLE)
CaseLabelElement ::= ConstantExpression
CaseLabelConstantElement ::= ConstantExpression
/.$putCase consumeCaseLabelElement(CaseLabelKind.CASE_EXPRESSION); $break ./
/:$readableName CaseLabelElement:/

-- following 'null' in CASE_EXPRESSION - passes through existing grammar
-- CaseLabelElement -> 'null'

CaseLabelElement ::= 'default'
CaseLabelConstantElement ::= 'default'
/.$putCase consumeCaseLabelElement(CaseLabelKind.CASE_DEFAULT); $break ./
/:$readableName CaseLabelElement:/

CaseLabelElement ::= CaseLabelElementPattern
/.$putCase consumeCaseLabelElement(CaseLabelKind.CASE_PATTERN); $break ./
CaseLabelPatternElements ::= CaseLabelPatternElementsInternal
/:$readableName CaseLabelElement:/

CaseLabelElement ::= CaseLabelElementPattern Guard
/.$putCase consumeCaseLabelElement(CaseLabelKind.CASE_PATTERN); $break ./
/:$readableName CaseLabelElement:/
CaseLabelPatternElementsInternal ::= BeginCaseElement CasePatternList
CaseLabelPatternElementsInternal ::= BeginCaseElement CasePatternList Guard

CasePatternList ::= CasePattern
/:$readableName CasePatternList :/
CasePatternList ::= CasePatternList ',' CasePattern
/.$putCase consumeCaseLabelElements(); $break ./
/:$readableName CasePatternList :/

CaseLabelElementPattern ::= BeginCaseElement Pattern
/.$putCase consumeCaseLabelElementPattern(); $break ./
/:$readableName CaseLabelElementPattern:/
CasePattern ::= Pattern
/.$putCase consumeCaseLabelElement(CaseLabelKind.CASE_PATTERN); $break ./
/:$readableName CasePattern:/

Guard ::= RestrictedIdentifierWhen Expression
/.$putCase consumeGuard(); $break ./
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void main(String[] args) throws IOException, InterruptedException
File javasymFile = new File(grammarDir, "javasym.java");
File terminalTokensFile = new File(parserDir, "TerminalTokens.java");
String javasymText = new String(Files.readAllBytes(javasymFile.toPath())).replace("\r\n", "\n");
String terminalTokensText = new String(Files.readAllBytes(terminalTokensFile.toPath()));
String terminalTokensText = new String(Files.readAllBytes(terminalTokensFile.toPath())).replace("\r\n", "\n");
{
String startTag = "// BEGIN_AUTOGENERATED_REGION\n";
int start = terminalTokensText.indexOf(startTag);
Expand All @@ -89,7 +89,7 @@ public static void main(String[] args) throws IOException, InterruptedException
File javadefFile = new File(grammarDir, "javadef.java");
File parserBasicInformationFile = new File(parserDir, "ParserBasicInformation.java");
String javadefText = new String(Files.readAllBytes(javadefFile.toPath())).replace("\r\n", "\n");
String parserBasicInformationText = new String(Files.readAllBytes(parserBasicInformationFile.toPath()));
String parserBasicInformationText = new String(Files.readAllBytes(parserBasicInformationFile.toPath())).replace("\r\n", "\n");
{
String startTag = "// BEGIN_AUTOGENERATED_REGION";
int start = parserBasicInformationText.indexOf(startTag);
Expand All @@ -112,7 +112,7 @@ public static void main(String[] args) throws IOException, InterruptedException

// Update method consumeRule in Parser.java
File parserFile = new File(parserDir, "Parser.java");
String parserText = new String(Files.readAllBytes(parserFile.toPath()));
String parserText = new String(Files.readAllBytes(parserFile.toPath())).replace("\r\n", "\n");
File javaActionFile = new File(grammarDir, "JavaAction.java");
String javaActionText = new String(Files.readAllBytes(javaActionFile.toPath()))
.replace("\r\n", "\n").replace(" \n", "\n").replace(" \n", "\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2576,6 +2576,10 @@ public interface IProblem {
*/
int IllegalRecordPattern = TypeRelated + 1941;

/**
* @since 3.37
*/
int CaseLabelWithPatternMustHaveExactlyOnePattern = Internal + 1942;

/**
* @since 3.35
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean
}
if (this.rightInitStateIndex != -1) {
codeStream.addDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
}
if (rightIsConst) {
this.right.generateCode(currentScope, codeStream, false);
Expand Down Expand Up @@ -230,8 +231,8 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
break generateOperands; // no need to generate right operand
}
if (this.rightInitStateIndex != -1) {
codeStream
.addDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
codeStream.addDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
}
this.right.generateOptimizedBoolean(currentScope, codeStream, trueLabel, null,
valueRequired && !rightIsConst);
Expand All @@ -255,8 +256,8 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
break generateOperands; // no need to generate right operand
}
if (this.rightInitStateIndex != -1) {
codeStream
.addDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
codeStream.addDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
}
this.right.generateOptimizedBoolean(currentScope, codeStream, null, falseLabel, valueRequired && !rightIsConst);
if (valueRequired && rightIsConst && !rightIsTrue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ public boolean isUnnamed(BlockScope scope) {
&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK21
&& scope.compilerOptions().enablePreviewFeatures;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1825,15 +1825,6 @@ public StringBuilder printExpressionNoParenthesis(int indent, StringBuilder outp
return this.right.printExpression(0, output);
}

@Override
public void addPatternVariables(BlockScope scope, CodeStream codeStream) {
this.left.addPatternVariables(scope, codeStream); // Srikanth
this.right.addPatternVariables(scope, codeStream);
}
@Override
public boolean containsPatternVariable() {
return this.left.containsPatternVariable() || this.right.containsPatternVariable();
}
@Override
public TypeBinding resolveType(BlockScope scope) {
// keep implementation in sync with CombinedBinaryExpression#resolveType
Expand Down
Loading
Loading