| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'package:js_runtime/shared/embedded_names.dart'; | 5 import 'package:js_runtime/shared/embedded_names.dart'; |
| 6 import 'package:kernel/ast.dart' as ir; | 6 import 'package:kernel/ast.dart' as ir; |
| 7 | 7 |
| 8 import '../common.dart'; | 8 import '../common.dart'; |
| 9 import '../compiler.dart'; | 9 import '../compiler.dart'; |
| 10 import '../constants/expressions.dart'; | 10 import '../constants/expressions.dart'; |
| (...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 // Nested invalid types are treated as `dynamic`. | 708 // Nested invalid types are treated as `dynamic`. |
| 709 return const ResolutionDynamicType(); | 709 return const ResolutionDynamicType(); |
| 710 } | 710 } |
| 711 } | 711 } |
| 712 | 712 |
| 713 class KernelJumpTarget extends JumpTarget { | 713 class KernelJumpTarget extends JumpTarget { |
| 714 static int index = 0; | 714 static int index = 0; |
| 715 | 715 |
| 716 /// Pointer to the actual executable statements that a jump target refers to. | 716 /// Pointer to the actual executable statements that a jump target refers to. |
| 717 /// If this jump target was not initially constructed with a LabeledStatement, | 717 /// If this jump target was not initially constructed with a LabeledStatement, |
| 718 /// this value is identical to originalStatement. | 718 /// this value is identical to originalStatement. This Node is actually of |
| 719 // TODO(efortuna): In an ideal world the Node should be some common | 719 /// type either ir.Statement or ir.SwitchCase. |
| 720 // interface we create for both ir.Statements and ir.SwitchCase (the | |
| 721 // ContinueSwitchStatement's target is a SwitchCase) rather than general | |
| 722 // Node. Talking to Asger about this. | |
| 723 ir.Node targetStatement; | 720 ir.Node targetStatement; |
| 724 | 721 |
| 725 /// The original statement used to construct this jump target. | 722 /// The original statement used to construct this jump target. |
| 726 /// If this jump target was not initially constructed with a LabeledStatement, | 723 /// If this jump target was not initially constructed with a LabeledStatement, |
| 727 /// this value is identical to targetStatement. | 724 /// this value is identical to targetStatement. This Node is actually of |
| 725 /// type either ir.Statement or ir.SwitchCase. |
| 728 ir.Node originalStatement; | 726 ir.Node originalStatement; |
| 729 | 727 |
| 728 /// Used to provide unique numbers to labels that would otherwise be duplicate |
| 729 /// if one JumpTarget is inside another. |
| 730 int nestingLevel; |
| 731 |
| 730 @override | 732 @override |
| 731 bool isBreakTarget = false; | 733 bool isBreakTarget = false; |
| 732 | 734 |
| 733 @override | 735 @override |
| 734 bool isContinueTarget = false; | 736 bool isContinueTarget = false; |
| 735 | 737 |
| 736 KernelJumpTarget(this.targetStatement, KernelAstAdapter adapter, | 738 KernelJumpTarget(this.targetStatement, KernelAstAdapter adapter, |
| 737 {bool makeContinueLabel = false}) { | 739 {bool makeContinueLabel = false}) { |
| 738 originalStatement = targetStatement; | 740 originalStatement = targetStatement; |
| 739 this.labels = <LabelDefinition>[]; | 741 this.labels = <LabelDefinition>[]; |
| 740 if (targetStatement is ir.WhileStatement || | 742 if (targetStatement is ir.WhileStatement || |
| 741 targetStatement is ir.DoStatement || | 743 targetStatement is ir.DoStatement || |
| 742 targetStatement is ir.ForStatement || | 744 targetStatement is ir.ForStatement || |
| 743 targetStatement is ir.ForInStatement) { | 745 targetStatement is ir.ForInStatement) { |
| 744 // Currently these labels are set at resolution on the element itself. | 746 // Currently these labels are set at resolution on the element itself. |
| 745 // Once that gets updated, this logic can change downstream. | 747 // Once that gets updated, this logic can change downstream. |
| 746 JumpTarget target = adapter.elements | 748 JumpTarget target = adapter.elements |
| 747 .getTargetDefinition(adapter.getNode(targetStatement)); | 749 .getTargetDefinition(adapter.getNode(targetStatement)); |
| 748 if (target != null) { | 750 if (target != null) { |
| 749 labels.addAll(target.labels); | 751 labels.addAll(target.labels); |
| 750 isBreakTarget = target.isBreakTarget; | 752 isBreakTarget = target.isBreakTarget; |
| 751 isContinueTarget = target.isContinueTarget; | 753 isContinueTarget = target.isContinueTarget; |
| 752 } | 754 } |
| 753 } else if (targetStatement is ir.LabeledStatement) { | 755 } else if (targetStatement is ir.LabeledStatement) { |
| 754 targetStatement = (targetStatement as ir.LabeledStatement).body; | 756 targetStatement = (targetStatement as ir.LabeledStatement).body; |
| 755 labels.add( | 757 labels.add( |
| 756 new LabelDefinitionX(null, 'L${index++}', this)..setBreakTarget()); | 758 new LabelDefinitionX(null, 'L${index++}', this)..setBreakTarget()); |
| 757 isBreakTarget = true; | 759 isBreakTarget = true; |
| 758 } | 760 } |
| 761 var originalNode = adapter.getNode(originalStatement); |
| 762 var originalTarget = adapter.elements.getTargetDefinition(originalNode); |
| 763 if (originalTarget != null) { |
| 764 nestingLevel = originalTarget.nestingLevel; |
| 765 } else { |
| 766 nestingLevel = 0; |
| 767 } |
| 759 | 768 |
| 760 if (makeContinueLabel) { | 769 if (makeContinueLabel) { |
| 761 labels.add( | 770 labels.add( |
| 762 new LabelDefinitionX(null, 'L${index++}', this)..setContinueTarget()); | 771 new LabelDefinitionX(null, 'L${index++}', this)..setContinueTarget()); |
| 763 isContinueTarget = true; | 772 isContinueTarget = true; |
| 764 } | 773 } |
| 765 } | 774 } |
| 766 | 775 |
| 767 @override | 776 @override |
| 768 LabelDefinition addLabel(ast.Label label, String labelName) { | 777 LabelDefinition addLabel(ast.Label label, String labelName) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 782 | 791 |
| 783 @override | 792 @override |
| 784 bool get isTarget => isBreakTarget || isContinueTarget; | 793 bool get isTarget => isBreakTarget || isContinueTarget; |
| 785 | 794 |
| 786 @override | 795 @override |
| 787 List<LabelDefinition> labels; | 796 List<LabelDefinition> labels; |
| 788 | 797 |
| 789 @override | 798 @override |
| 790 String get name => 'target'; | 799 String get name => 'target'; |
| 791 | 800 |
| 792 // TODO(efortuna): In the original version, this nesting level is specified at | |
| 793 // jump target construction time, by the resolver. Because these are | |
| 794 // instantiated later, we don't have that information. When we move fully over | |
| 795 // to the kernel model, we can pass the nesting level in KernelJumpTarget's | |
| 796 // constructor. | |
| 797 @override | |
| 798 int get nestingLevel => 0; | |
| 799 | |
| 800 @override | 801 @override |
| 801 ast.Node get statement => null; | 802 ast.Node get statement => null; |
| 802 | 803 |
| 803 String toString() => 'Target:$targetStatement'; | 804 String toString() => 'Target:$targetStatement'; |
| 804 } | 805 } |
| 805 | 806 |
| 806 /// Special [JumpHandler] implementation used to handle continue statements | 807 /// Special [JumpHandler] implementation used to handle continue statements |
| 807 /// targeting switch cases. | 808 /// targeting switch cases. |
| 808 class KernelSwitchCaseJumpHandler extends SwitchCaseJumpHandler { | 809 class KernelSwitchCaseJumpHandler extends SwitchCaseJumpHandler { |
| 809 KernelSwitchCaseJumpHandler(GraphBuilder builder, JumpTarget target, | 810 KernelSwitchCaseJumpHandler(GraphBuilder builder, JumpTarget target, |
| 810 ir.SwitchStatement switchStatement, KernelAstAdapter astAdapter) | 811 ir.SwitchStatement switchStatement, KernelAstAdapter astAdapter) |
| 811 : super(builder, target) { | 812 : super(builder, target) { |
| 812 // The switch case indices must match those computed in | 813 // The switch case indices must match those computed in |
| 813 // [KernelSsaBuilder.buildSwitchCaseConstants]. | 814 // [KernelSsaBuilder.buildSwitchCaseConstants]. |
| 814 // Switch indices are 1-based so we can bypass the synthetic loop when no | 815 // Switch indices are 1-based so we can bypass the synthetic loop when no |
| 815 // cases match simply by branching on the index (which defaults to null). | 816 // cases match simply by branching on the index (which defaults to null). |
| 816 // TODO | 817 // TODO |
| 817 int switchIndex = 1; | 818 int switchIndex = 1; |
| 818 for (ir.SwitchCase switchCase in switchStatement.cases) { | 819 for (ir.SwitchCase switchCase in switchStatement.cases) { |
| 819 JumpTarget continueTarget = | 820 JumpTarget continueTarget = |
| 820 astAdapter.getJumpTarget(switchCase, isContinueTarget: true); | 821 astAdapter.getJumpTarget(switchCase, isContinueTarget: true); |
| 821 assert(continueTarget is KernelJumpTarget); | 822 assert(continueTarget is KernelJumpTarget); |
| 822 targetIndexMap[continueTarget] = switchIndex; | 823 targetIndexMap[continueTarget] = switchIndex; |
| 823 assert(builder.jumpTargets[continueTarget] == null); | 824 assert(builder.jumpTargets[continueTarget] == null); |
| 824 builder.jumpTargets[continueTarget] = this; | 825 builder.jumpTargets[continueTarget] = this; |
| 825 switchIndex++; | 826 switchIndex++; |
| 826 } | 827 } |
| 827 } | 828 } |
| 828 } | 829 } |
| OLD | NEW |