Skip to content

Commit

Permalink
[Enhanced Switch] "Retrofit" some design into code generation for switch
Browse files Browse the repository at this point in the history
statement

* Fixes #3430
  • Loading branch information
srikanth-sankaran committed Dec 13, 2024
1 parent b90780a commit da3e604
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class CaseStatement extends Statement {

public BranchLabel targetLabel;
public Expression[] constantExpressions; // case with multiple expressions - if you want a under-the-hood view, use peeledLabelExpressions()
public BranchLabel[] targetLabels; // for multiple expressions
public boolean isSwitchRule = false;

public SwitchStatement swich; // owning switch
Expand Down Expand Up @@ -106,7 +105,7 @@ private boolean essentiallyQualifiedEnumerator(Expression e, TypeBinding selecto
private void checkDuplicateDefault(BlockScope scope, ASTNode node) {
if (this.swich.defaultCase != null)
scope.problemReporter().duplicateDefaultCase(node);
else if (this.swich.totalPattern != null)
else if (this.swich.unconditionalPatternCase != null)
scope.problemReporter().illegalTotalPatternWithDefault(this);
this.swich.defaultCase = this;
}
Expand Down Expand Up @@ -200,7 +199,7 @@ private Constant resolvePatternLabel(BlockScope scope, TypeBinding caseType, Typ
this.swich.switchBits |= SwitchStatement.Exhaustive;
pattern.isTotalTypeNode = true;
if (pattern.isUnconditional(selectorType, scope)) // unguarded is implied from 'coversType()' above
this.swich.totalPattern = pattern;
this.swich.unconditionalPatternCase = this;
}
return constant;
}
Expand Down Expand Up @@ -229,6 +228,7 @@ public void resolve(BlockScope scope) {
return;
}

this.swich.switchBits |= SwitchStatement.HasNondefaultCase;
int count = 0;
int nullCaseCount = 0;
for (Expression e : this.constantExpressions) {
Expand Down Expand Up @@ -324,13 +324,9 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl
public void generateCode(BlockScope currentScope, CodeStream codeStream) {
if ((this.bits & ASTNode.IsReachable) == 0)
return;

int pc = codeStream.position;
if (this.targetLabels != null) {
for (BranchLabel label : this.targetLabels)
label.place();
}
if (this.targetLabel != null)
this.targetLabel.place();
this.targetLabel.place();

if (containsPatternVariable(true)) {

Expand Down
Loading

0 comments on commit da3e604

Please sign in to comment.