Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library backend_ast_emitter; | 5 library backend_ast_emitter; |
| 6 | 6 |
| 7 import 'tree_ir_nodes.dart' as tree; | 7 import 'tree_ir_nodes.dart' as tree; |
| 8 import 'backend_ast_nodes.dart'; | 8 import 'backend_ast_nodes.dart'; |
| 9 import '../dart2jslib.dart' as dart2js; | 9 import '../dart2jslib.dart' as dart2js; |
| 10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
| (...skipping 652 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 663 } | 663 } |
| 664 | 664 |
| 665 Expression emitConstant(ConstExp exp) => new ConstantEmitter(this).visit(exp); | 665 Expression emitConstant(ConstExp exp) => new ConstantEmitter(this).visit(exp); |
| 666 | 666 |
| 667 } | 667 } |
| 668 | 668 |
| 669 class ConstantEmitter extends ConstExpVisitor<Expression> { | 669 class ConstantEmitter extends ConstExpVisitor<Expression> { |
| 670 ASTEmitter parent; | 670 ASTEmitter parent; |
| 671 ConstantEmitter(this.parent); | 671 ConstantEmitter(this.parent); |
| 672 | 672 |
| 673 Expression visitPrimitive(PrimitiveConstExp exp) { | 673 Expression handlePrimitiveConstant(dart2js.PrimitiveConstant value) { |
| 674 // Num constants may be negative, while literals must be non-negative: | 674 // Num constants may be negative, while literals must be non-negative: |
| 675 // Literals are non-negative in the specification, and a negated literal | 675 // Literals are non-negative in the specification, and a negated literal |
| 676 // parses as a call to unary `-`. The AST unparser assumes literals are | 676 // parses as a call to unary `-`. The AST unparser assumes literals are |
| 677 // non-negative and relies on this to avoid incorrectly generating `--`, | 677 // non-negative and relies on this to avoid incorrectly generating `--`, |
| 678 // the predecrement operator. | 678 // the predecrement operator. |
| 679 // Translate such constants into their positive value wrapped by | 679 // Translate such constants into their positive value wrapped by |
| 680 // the unary minus operator. | 680 // the unary minus operator. |
| 681 if (exp.constant is dart2js.NumConstant) { | 681 if (value is dart2js.NumConstant) { |
| 682 dart2js.NumConstant numConstant = exp.constant; | 682 dart2js.NumConstant numConstant = value; |
| 683 if (numConstant.value.isNegative) { | 683 if (numConstant.value.isNegative) { |
| 684 return negatedLiteral(numConstant); | 684 return negatedLiteral(numConstant); |
| 685 } | 685 } |
| 686 } | 686 } |
| 687 return new Literal(exp.constant); | 687 return new Literal(value); |
| 688 } | |
| 689 | |
| 690 Expression visitPrimitive(PrimitiveConstExp exp) { | |
| 691 return handlePrimitiveConstant(exp.value); | |
| 688 } | 692 } |
| 689 | 693 |
| 690 /// Given a negative num constant, returns the corresponding positive | 694 /// Given a negative num constant, returns the corresponding positive |
| 691 /// literal wrapped by a unary minus operator. | 695 /// literal wrapped by a unary minus operator. |
| 692 Expression negatedLiteral(dart2js.NumConstant constant) { | 696 Expression negatedLiteral(dart2js.NumConstant constant) { |
| 693 assert(constant.value.isNegative); | 697 assert(constant.value.isNegative); |
| 694 dart2js.NumConstant positiveConstant; | 698 dart2js.NumConstant positiveConstant; |
| 695 if (constant.isInt) { | 699 if (constant.isInt) { |
| 696 positiveConstant = new dart2js.IntConstant(-constant.value); | 700 positiveConstant = new dart2js.IntConstant(-constant.value); |
| 697 } else if (constant.isDouble) { | 701 } else if (constant.isDouble) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 762 String name = parent.getConstantName(element); | 766 String name = parent.getConstantName(element); |
| 763 return new Identifier(name) | 767 return new Identifier(name) |
| 764 ..element = element; | 768 ..element = element; |
| 765 } | 769 } |
| 766 | 770 |
| 767 Expression visitFunction(FunctionConstExp exp) { | 771 Expression visitFunction(FunctionConstExp exp) { |
| 768 return new Identifier(exp.element.name) | 772 return new Identifier(exp.element.name) |
| 769 ..element = exp.element; | 773 ..element = exp.element; |
| 770 } | 774 } |
| 771 | 775 |
| 776 @override | |
|
sigurdm
2014/09/17 10:29:41
Maybe add override to all the overriding methods
Johnni Winther
2014/09/17 12:20:40
Done.
| |
| 777 Expression visitBinary(BinaryConstExp exp) { | |
| 778 return handlePrimitiveConstant(exp.value); | |
| 779 } | |
| 780 | |
| 781 @override | |
| 782 Expression visitConditional(ConditionalConstExp exp) { | |
| 783 if (exp.condition.value.isTrue) { | |
| 784 return exp.trueExp.accept(this); | |
| 785 } else { | |
| 786 return exp.falseExp.accept(this); | |
| 787 } | |
| 788 } | |
| 789 | |
| 790 @override | |
| 791 Expression visitUnary(UnaryConstExp exp) { | |
| 792 return handlePrimitiveConstant(exp.value); | |
| 793 } | |
| 772 } | 794 } |
| 773 | 795 |
| 774 /// Moves function parameters into a separate variable if one of its uses is | 796 /// Moves function parameters into a separate variable if one of its uses is |
| 775 /// shadowed by an inner function parameter. | 797 /// shadowed by an inner function parameter. |
| 776 /// This artifact is necessary because function parameters cannot be renamed. | 798 /// This artifact is necessary because function parameters cannot be renamed. |
| 777 class UnshadowParameters extends tree.RecursiveVisitor { | 799 class UnshadowParameters extends tree.RecursiveVisitor { |
| 778 | 800 |
| 779 /// Maps parameter names to their bindings. | 801 /// Maps parameter names to their bindings. |
| 780 Map<String, tree.Variable> environment = <String, tree.Variable>{}; | 802 Map<String, tree.Variable> environment = <String, tree.Variable>{}; |
| 781 | 803 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 818 } | 840 } |
| 819 } | 841 } |
| 820 | 842 |
| 821 visitVariable(tree.Variable variable) { | 843 visitVariable(tree.Variable variable) { |
| 822 if (shadowedParameters.contains(variable)) { | 844 if (shadowedParameters.contains(variable)) { |
| 823 hasShadowedUse.add(variable); | 845 hasShadowedUse.add(variable); |
| 824 } | 846 } |
| 825 } | 847 } |
| 826 | 848 |
| 827 } | 849 } |
| OLD | NEW |