Skip to content

Commit 9027389

Browse files
tomballcopybara-github
authored andcommitted
Supports switch expressions by AST translators.
PiperOrigin-RevId: 729284458
1 parent e5f0713 commit 9027389

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

translator/src/main/java/com/google/devtools/j2objc/translate/Autoboxer.java

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.devtools.j2objc.ast.SimpleName;
4141
import com.google.devtools.j2objc.ast.SuperConstructorInvocation;
4242
import com.google.devtools.j2objc.ast.SuperMethodInvocation;
43+
import com.google.devtools.j2objc.ast.SwitchExpression;
4344
import com.google.devtools.j2objc.ast.SwitchStatement;
4445
import com.google.devtools.j2objc.ast.TreeNode;
4546
import com.google.devtools.j2objc.ast.TreeUtil;
@@ -463,6 +464,15 @@ public void endVisit(WhileStatement node) {
463464
}
464465
}
465466

467+
@Override
468+
@SuppressWarnings("UngroupedOverloads")
469+
public void endVisit(SwitchExpression node) {
470+
Expression expression = node.getExpression();
471+
if (!expression.getTypeMirror().getKind().isPrimitive()) {
472+
unbox(expression);
473+
}
474+
}
475+
466476
@Override
467477
public void endVisit(SwitchStatement node) {
468478
Expression expression = node.getExpression();

translator/src/main/java/com/google/devtools/j2objc/translate/NilCheckResolver.java

+15
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.google.devtools.j2objc.ast.SuperConstructorInvocation;
5151
import com.google.devtools.j2objc.ast.SuperMethodInvocation;
5252
import com.google.devtools.j2objc.ast.SwitchCase;
53+
import com.google.devtools.j2objc.ast.SwitchExpression;
5354
import com.google.devtools.j2objc.ast.SwitchStatement;
5455
import com.google.devtools.j2objc.ast.ThrowStatement;
5556
import com.google.devtools.j2objc.ast.TreeNode;
@@ -714,6 +715,20 @@ public boolean visit(ForStatement node) {
714715
return false;
715716
}
716717

718+
@Override
719+
@SuppressWarnings("UngroupedOverloads")
720+
public boolean visit(SwitchExpression node) {
721+
// No need to test for no matched value like SwitchStatement,
722+
// as switch expressions are exhaustive.
723+
node.getExpression().accept(this);
724+
pushLoopOrSwitchScope(null);
725+
for (Statement stmt : node.getStatements()) {
726+
stmt.accept(this);
727+
}
728+
popAndMerge();
729+
return false;
730+
}
731+
717732
@Override
718733
public boolean visit(SwitchStatement node) {
719734
node.getExpression().accept(this);

translator/src/main/java/com/google/devtools/j2objc/translate/UnsequencedExpressionRewriter.java

+13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.google.devtools.j2objc.ast.SimpleName;
4444
import com.google.devtools.j2objc.ast.Statement;
4545
import com.google.devtools.j2objc.ast.SuperConstructorInvocation;
46+
import com.google.devtools.j2objc.ast.SwitchExpression;
4647
import com.google.devtools.j2objc.ast.SwitchStatement;
4748
import com.google.devtools.j2objc.ast.SynchronizedStatement;
4849
import com.google.devtools.j2objc.ast.ThrowStatement;
@@ -531,6 +532,18 @@ public boolean visit(IfStatement node) {
531532
return false;
532533
}
533534

535+
@Override
536+
@SuppressWarnings("UngroupedOverloads")
537+
public boolean visit(SwitchExpression node) {
538+
Expression expr = node.getExpression();
539+
newExpression(expr);
540+
expr.accept(this);
541+
for (Statement stmt : node.getStatements()) {
542+
stmt.accept(this);
543+
}
544+
return false;
545+
}
546+
534547
@Override
535548
public boolean visit(SwitchStatement node) {
536549
visitAndExtract(node.getExpression(), node);

0 commit comments

Comments
 (0)