Skip to content

Commit

Permalink
Final polish work for Enhanced Switches v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-sankaran committed Dec 16, 2024
1 parent 9096700 commit 101a00c
Showing 1 changed file with 9 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,6 @@ protected void initializeLabels(CodeStream codeStream) {
this.swich.defaultLabel = new CaseLabel(codeStream, true /* allow narrow branch to */);
if (this.swich.defaultCase != null)
this.swich.defaultCase.targetLabel = this.swich.defaultLabel; // Replace the vanilla branch label with a case label that doubles as a branch label.

}

@Override
Expand Down Expand Up @@ -1041,25 +1040,6 @@ protected void gatherCaseConstantsOrProxies() {
}
}

private char[] typeSwitchSignature(TypeBinding exprType) {
char[] arg1 = switch (exprType.id) {
case TypeIds.T_JavaLangLong, TypeIds.T_JavaLangFloat, TypeIds.T_JavaLangDouble, TypeIds.T_JavaLangBoolean,
TypeIds.T_JavaLangByte, TypeIds.T_JavaLangShort, TypeIds.T_JavaLangInteger, TypeIds.T_JavaLangCharacter->
this.swich.isPrimitiveSwitch
? exprType.signature()
: "Ljava/lang/Object;".toCharArray(); //$NON-NLS-1$
default -> {
if (exprType.id > TypeIds.T_LastWellKnownTypeId && exprType.erasure().isBoxedPrimitiveType())
yield exprType.erasure().signature(); // <T extends Integer> / <? extends Short> ...
else
yield exprType.isPrimitiveType()
? exprType.signature()
: "Ljava/lang/Object;".toCharArray(); //$NON-NLS-1$
}
};
return CharOperation.concat("(".toCharArray(), arg1, "I)I".toCharArray()); //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
protected void generateSelectorExpression(BlockScope currentScope, CodeStream codeStream) {
this.swich.expression.generateCode(currentScope, codeStream, true);
Expand All @@ -1079,27 +1059,13 @@ protected void generateSelectorExpression(BlockScope currentScope, CodeStream co
this.swich.switchPatternRestartTarget = new BranchLabel(codeStream);
this.swich.switchPatternRestartTarget.place();

if (this.swich.expression.resolvedType.isEnum()) {
String genericTypeSignature = new String(this.swich.expression.resolvedType.genericTypeSignature());
String callingParams = "(" + genericTypeSignature + "I)I"; //$NON-NLS-1$ //$NON-NLS-2$
codeStream.invokeDynamic(invokeDynamicNumber,
2, // Object, restartIndex
1, // int
"enumSwitch".toCharArray(), //$NON-NLS-1$
callingParams.toCharArray(),
TypeBinding.INT);
} else {
TypeBinding exprType = this.swich.expression.resolvedType;
char[] signature = typeSwitchSignature(exprType);
int argsSize = TypeIds.getCategory(exprType.id) + 1; // Object | PRIM, restartIndex (PRIM = Z|S|I..)
codeStream.invokeDynamic(invokeDynamicNumber,
argsSize,
1, // int
ConstantPool.TYPESWITCH,
signature,
TypeBinding.INT);
}
TypeBinding selectorType = this.swich.expression.resolvedType;
char[] signature = CharOperation.concat("(".toCharArray(), selectorType.erasure().signature(), "I)I".toCharArray()); //$NON-NLS-1$ //$NON-NLS-2$
int argsSize = TypeIds.getCategory(selectorType.id) + 1; // ReferenceType | PRIM, restartIndex (PRIM = Z|S|I..)
char [] bootstrap = selectorType.isEnum() ? ConstantPool.ENUMSWITCH : ConstantPool.TYPESWITCH;
codeStream.invokeDynamic(invokeDynamicNumber, argsSize, 1 /* int case constant/proxy */, bootstrap, signature, TypeBinding.INT);

// This is not relevant to selector generation, but is stuffed in here for now... smelly - yeah.
for (int i = 0; i < this.swich.labelExpressions.length; i++) {
LabelExpression c = this.swich.labelExpressions[i];
if (c.type.isPrimitiveType()) {
Expand Down Expand Up @@ -1177,11 +1143,9 @@ public StringBuilder printExpression(int indent, StringBuilder output) {
public void traverse(ASTVisitor visitor, BlockScope blockScope) {
if (visitor.visit(this, blockScope)) {
this.expression.traverse(visitor, blockScope);
if (this.statements != null) {
int statementsLength = this.statements.length;
for (int i = 0; i < statementsLength; i++)
this.statements[i].traverse(visitor, this.scope);
}
if (this.statements != null)
for (Statement statement : this.statements)
statement.traverse(visitor, this.scope);
}
visitor.endVisit(this, blockScope);
}
Expand Down

0 comments on commit 101a00c

Please sign in to comment.