| OLD | NEW |
| (Empty) | |
| 1 import 'package:kernel/ast.dart' as ir; |
| 2 |
| 3 /// Helper class that traverses a kernel AST subtree to see if it has any |
| 4 /// continue statements in the body of any switch cases (having continue |
| 5 /// statements results in a more complex generated code). |
| 6 class SwitchContinueAnalysis extends ir.Visitor<bool> { |
| 7 |
| 8 SwitchContinueAnalysis._(); |
| 9 |
| 10 static bool containsContinue(ir.Statement switchCaseBody) { |
| 11 return switchCaseBody.accept(new SwitchContinueAnalysis._()); |
| 12 } |
| 13 |
| 14 bool visitContinueSwitchStatement(ir.ContinueSwitchStatement continueStmt) { |
| 15 // TODO(efortuna): Check what the target of this continue statement actually |
| 16 // IS, because depending on where the label points if we have a nested |
| 17 // switch statement we might be able to output simpler code (not the complex |
| 18 // switch statement). |
| 19 return true; |
| 20 } |
| 21 |
| 22 bool visitBlock(ir.Block block) { |
| 23 for (ir.Statement statement in block.statements) { |
| 24 if (statement.accept(this)) { |
| 25 return true; |
| 26 } |
| 27 } |
| 28 return false; |
| 29 } |
| 30 |
| 31 bool visitLabeledStatement(ir.LabeledStatement statement) { |
| 32 return statement.body.accept(this); |
| 33 } |
| 34 |
| 35 bool visitDoStatement(ir.DoStatement doStatement) { |
| 36 return doStatement.body.accept(this); |
| 37 } |
| 38 |
| 39 bool visitForStatement(ir.ForStatement forStatement) { |
| 40 return forStatement.body.accept(this); |
| 41 } |
| 42 |
| 43 bool visitForInStatement(ir.ForInStatement forInStatement) { |
| 44 return forInStatement.body.accept(this); |
| 45 } |
| 46 |
| 47 bool visitSwitchStatement(ir.SwitchStatement switchStatement) { |
| 48 for (var switchCase in switchStatement.cases) { |
| 49 if (switchCase.accept(this)) { |
| 50 return true; |
| 51 } |
| 52 } |
| 53 return false; |
| 54 } |
| 55 |
| 56 bool visitSwitchCase(ir.SwitchCase switchCase) { |
| 57 return switchCase.body.accept(this); |
| 58 } |
| 59 |
| 60 bool visitIfStatement(ir.IfStatement ifStatement) { |
| 61 if (ifStatement.then.accept(this)) { |
| 62 if (ifStatement.otherwise != null) { |
| 63 return ifStatement.otherwise.accept(this); |
| 64 } |
| 65 } |
| 66 return false; |
| 67 } |
| 68 |
| 69 bool visitTryCatch(ir.TryCatch tryCatch) { |
| 70 if (tryCatch.body.accept(this)) { |
| 71 for (var catchStatement in tryCatch.catches) { |
| 72 if (catchStatement.accept(this)) { |
| 73 return true; |
| 74 } |
| 75 } |
| 76 } |
| 77 return false; |
| 78 } |
| 79 |
| 80 bool visitWhileStatement(ir.WhileStatement statement) { |
| 81 return statement.body.accept(this); |
| 82 } |
| 83 |
| 84 bool visitCatch(ir.Catch catchStatement) { |
| 85 return catchStatement.body.accept(this); |
| 86 } |
| 87 |
| 88 bool visitTryFinally(ir.TryFinally tryFinally) { |
| 89 return tryFinally.body.accept(this) && |
| 90 tryFinally.finalizer.accept(this); |
| 91 } |
| 92 |
| 93 bool visitFunctionDeclaration(ir.FunctionDeclaration declaration) { |
| 94 return declaration.function.accept(this); |
| 95 } |
| 96 |
| 97 bool visitFunctionNode(ir.FunctionNode node) { |
| 98 return node.body.accept(this); |
| 99 } |
| 100 |
| 101 bool defaultStatement(ir.Statement node) { |
| 102 if (node is ir.ExpressionStatement || node is ir.EmptyStatement || |
| 103 node is ir.InvalidStatement || node is ir.BreakStatement || |
| 104 node is ir.ReturnStatement || node is ir.AssertStatement || |
| 105 node is ir.YieldStatement || node is ir.VariableDeclaration) { |
| 106 return false; |
| 107 } |
| 108 throw 'Statement type ${node.runtimeType} not handled in ' |
| 109 'SwitchContinueAnalysis'; |
| 110 } |
| 111 |
| 112 bool defaultNode(ir.Node node) => false; |
| 113 } |
| OLD | NEW |