| 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 '../constants/expressions.dart'; | 9 import '../constants/expressions.dart'; |
| 10 import '../constants/values.dart'; | 10 import '../constants/values.dart'; |
| (...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 701 return new TypeAnnotation("dynamic") | 701 return new TypeAnnotation("dynamic") |
| 702 ..dartType = type; | 702 ..dartType = type; |
| 703 } else if (type is MalformedType) { | 703 } else if (type is MalformedType) { |
| 704 return new TypeAnnotation(type.name) | 704 return new TypeAnnotation(type.name) |
| 705 ..dartType = type; | 705 ..dartType = type; |
| 706 } else { | 706 } else { |
| 707 throw "Unsupported type annotation: $type"; | 707 throw "Unsupported type annotation: $type"; |
| 708 } | 708 } |
| 709 } | 709 } |
| 710 | 710 |
| 711 class ConstantEmitter extends ConstantExpressionVisitor<Expression> { | 711 class ConstantEmitter extends ConstantExpressionVisitor<Null, Expression> { |
| 712 ASTEmitter parent; | 712 ASTEmitter parent; |
| 713 ConstantEmitter(this.parent); | 713 ConstantEmitter(this.parent); |
| 714 | 714 |
| 715 Expression handlePrimitiveConstant(PrimitiveConstantValue value) { | 715 Expression handlePrimitiveConstant(PrimitiveConstantValue value) { |
| 716 // Num constants may be negative, while literals must be non-negative: | 716 // Num constants may be negative, while literals must be non-negative: |
| 717 // Literals are non-negative in the specification, and a negated literal | 717 // Literals are non-negative in the specification, and a negated literal |
| 718 // parses as a call to unary `-`. The AST unparser assumes literals are | 718 // parses as a call to unary `-`. The AST unparser assumes literals are |
| 719 // non-negative and relies on this to avoid incorrectly generating `--`, | 719 // non-negative and relies on this to avoid incorrectly generating `--`, |
| 720 // the predecrement operator. | 720 // the predecrement operator. |
| 721 // Translate such constants into their positive value wrapped by | 721 // Translate such constants into their positive value wrapped by |
| 722 // the unary minus operator. | 722 // the unary minus operator. |
| 723 if (value.isNum) { | 723 if (value.isNum) { |
| 724 NumConstantValue numConstant = value; | 724 NumConstantValue numConstant = value; |
| 725 if (numConstant.primitiveValue.isNegative) { | 725 if (numConstant.primitiveValue.isNegative) { |
| 726 return negatedLiteral(numConstant); | 726 return negatedLiteral(numConstant); |
| 727 } | 727 } |
| 728 } | 728 } |
| 729 return new Literal(value); | 729 return new Literal(value); |
| 730 } | 730 } |
| 731 | 731 |
| 732 @override | 732 @override |
| 733 Expression visitPrimitive(PrimitiveConstantExpression exp) { | 733 Expression visitPrimitive(PrimitiveConstantExpression exp, [_]) { |
| 734 return handlePrimitiveConstant(exp.value); | 734 return handlePrimitiveConstant(exp.value); |
| 735 } | 735 } |
| 736 | 736 |
| 737 /// Given a negative num constant, returns the corresponding positive | 737 /// Given a negative num constant, returns the corresponding positive |
| 738 /// literal wrapped by a unary minus operator. | 738 /// literal wrapped by a unary minus operator. |
| 739 Expression negatedLiteral(NumConstantValue constant) { | 739 Expression negatedLiteral(NumConstantValue constant, [_]) { |
| 740 assert(constant.primitiveValue.isNegative); | 740 assert(constant.primitiveValue.isNegative); |
| 741 NumConstantValue positiveConstant; | 741 NumConstantValue positiveConstant; |
| 742 if (constant.isInt) { | 742 if (constant.isInt) { |
| 743 positiveConstant = new IntConstantValue(-constant.primitiveValue); | 743 positiveConstant = new IntConstantValue(-constant.primitiveValue); |
| 744 } else if (constant.isDouble) { | 744 } else if (constant.isDouble) { |
| 745 positiveConstant = new DoubleConstantValue(-constant.primitiveValue); | 745 positiveConstant = new DoubleConstantValue(-constant.primitiveValue); |
| 746 } else { | 746 } else { |
| 747 throw "Unexpected type of NumConstant: $constant"; | 747 throw "Unexpected type of NumConstant: $constant"; |
| 748 } | 748 } |
| 749 return new UnaryOperator('-', new Literal(positiveConstant)); | 749 return new UnaryOperator('-', new Literal(positiveConstant)); |
| 750 } | 750 } |
| 751 | 751 |
| 752 @override | 752 @override |
| 753 Expression visitList(ListConstantExpression exp) { | 753 Expression visitList(ListConstantExpression exp, [_]) { |
| 754 return new LiteralList( | 754 return new LiteralList( |
| 755 exp.values.map(visit).toList(growable: false), | 755 exp.values.map(visit).toList(growable: false), |
| 756 isConst: true, | 756 isConst: true, |
| 757 typeArgument: parent.emitOptionalType(exp.type.typeArguments.single)); | 757 typeArgument: parent.emitOptionalType(exp.type.typeArguments.single)); |
| 758 } | 758 } |
| 759 | 759 |
| 760 @override | 760 @override |
| 761 Expression visitMap(MapConstantExpression exp) { | 761 Expression visitMap(MapConstantExpression exp, [_]) { |
| 762 List<LiteralMapEntry> entries = new List<LiteralMapEntry>.generate( | 762 List<LiteralMapEntry> entries = new List<LiteralMapEntry>.generate( |
| 763 exp.values.length, | 763 exp.values.length, |
| 764 (i) => new LiteralMapEntry(visit(exp.keys[i]), | 764 (i) => new LiteralMapEntry(visit(exp.keys[i]), |
| 765 visit(exp.values[i]))); | 765 visit(exp.values[i]))); |
| 766 List<TypeAnnotation> typeArguments = exp.type.treatAsRaw | 766 List<TypeAnnotation> typeArguments = exp.type.treatAsRaw |
| 767 ? null | 767 ? null |
| 768 : exp.type.typeArguments.map(createTypeAnnotation).toList(); | 768 : exp.type.typeArguments.map(createTypeAnnotation).toList(); |
| 769 return new LiteralMap(entries, isConst: true, typeArguments: typeArguments); | 769 return new LiteralMap(entries, isConst: true, typeArguments: typeArguments); |
| 770 } | 770 } |
| 771 | 771 |
| 772 @override | 772 @override |
| 773 Expression visitConstructor(ConstructedConstantExpresssion exp) { | 773 Expression visitConstructed(ConstructedConstantExpresssion exp, [_]) { |
| 774 int positionalArgumentCount = exp.selector.positionalArgumentCount; | 774 int positionalArgumentCount = exp.selector.positionalArgumentCount; |
| 775 List<Argument> args = new List<Argument>.generate( | 775 List<Argument> args = new List<Argument>.generate( |
| 776 positionalArgumentCount, | 776 positionalArgumentCount, |
| 777 (i) => visit(exp.arguments[i])); | 777 (i) => visit(exp.arguments[i])); |
| 778 for (int i = 0; i < exp.selector.namedArgumentCount; ++i) { | 778 for (int i = 0; i < exp.selector.namedArgumentCount; ++i) { |
| 779 args.add(new NamedArgument(exp.selector.namedArguments[i], | 779 args.add(new NamedArgument(exp.selector.namedArguments[i], |
| 780 visit(exp.arguments[positionalArgumentCount + i]))); | 780 visit(exp.arguments[positionalArgumentCount + i]))); |
| 781 } | 781 } |
| 782 | 782 |
| 783 FunctionElement constructor = exp.target; | 783 FunctionElement constructor = exp.target; |
| 784 String name = constructor.name.isEmpty ? null : constructor.name; | 784 String name = constructor.name.isEmpty ? null : constructor.name; |
| 785 return new CallNew(createTypeAnnotation(exp.type), | 785 return new CallNew(createTypeAnnotation(exp.type), |
| 786 args, | 786 args, |
| 787 constructorName: name, | 787 constructorName: name, |
| 788 isConst: true) | 788 isConst: true) |
| 789 ..constructor = constructor | 789 ..constructor = constructor |
| 790 ..dartType = exp.type; | 790 ..dartType = exp.type; |
| 791 } | 791 } |
| 792 | 792 |
| 793 @override | 793 @override |
| 794 Expression visitConcatenate(ConcatenateConstantExpression exp) { | 794 Expression visitConcatenate(ConcatenateConstantExpression exp, [_]) { |
| 795 return new StringConcat(exp.arguments.map(visit).toList(growable: false)); | 795 return new StringConcat(exp.arguments.map(visit).toList(growable: false)); |
| 796 } | 796 } |
| 797 | 797 |
| 798 @override | 798 @override |
| 799 Expression visitSymbol(SymbolConstantExpression exp) { | 799 Expression visitSymbol(SymbolConstantExpression exp, [_]) { |
| 800 return new LiteralSymbol(exp.name); | 800 return new LiteralSymbol(exp.name); |
| 801 } | 801 } |
| 802 | 802 |
| 803 @override | 803 @override |
| 804 Expression visitType(TypeConstantExpression exp) { | 804 Expression visitType(TypeConstantExpression exp, [_]) { |
| 805 DartType type = exp.type; | 805 DartType type = exp.type; |
| 806 return new LiteralType(type.name) | 806 return new LiteralType(type.name) |
| 807 ..type = type; | 807 ..type = type; |
| 808 } | 808 } |
| 809 | 809 |
| 810 @override | 810 @override |
| 811 Expression visitVariable(VariableConstantExpression exp) { | 811 Expression visitVariable(VariableConstantExpression exp, [_]) { |
| 812 Element element = exp.element; | 812 Element element = exp.element; |
| 813 if (element.kind != ElementKind.VARIABLE) { | 813 if (element.kind != ElementKind.VARIABLE) { |
| 814 return new Identifier(element.name)..element = element; | 814 return new Identifier(element.name)..element = element; |
| 815 } | 815 } |
| 816 String name = parent.getConstantName(element); | 816 String name = parent.getConstantName(element); |
| 817 return new Identifier(name) | 817 return new Identifier(name) |
| 818 ..element = element; | 818 ..element = element; |
| 819 } | 819 } |
| 820 | 820 |
| 821 @override | 821 @override |
| 822 Expression visitFunction(FunctionConstantExpression exp) { | 822 Expression visitFunction(FunctionConstantExpression exp, [_]) { |
| 823 return new Identifier(exp.element.name) | 823 return new Identifier(exp.element.name) |
| 824 ..element = exp.element; | 824 ..element = exp.element; |
| 825 } | 825 } |
| 826 | 826 |
| 827 @override | 827 @override |
| 828 Expression visitBinary(BinaryConstantExpression exp) { | 828 Expression visitBinary(BinaryConstantExpression exp, [_]) { |
| 829 return handlePrimitiveConstant(exp.value); | 829 return handlePrimitiveConstant(exp.value); |
| 830 } | 830 } |
| 831 | 831 |
| 832 @override | 832 @override |
| 833 Expression visitConditional(ConditionalConstantExpression exp) { | 833 Expression visitConditional(ConditionalConstantExpression exp, [_]) { |
| 834 if (exp.condition.value.isTrue) { | 834 if (exp.condition.value.isTrue) { |
| 835 return exp.trueExp.accept(this); | 835 return exp.trueExp.accept(this); |
| 836 } else { | 836 } else { |
| 837 return exp.falseExp.accept(this); | 837 return exp.falseExp.accept(this); |
| 838 } | 838 } |
| 839 } | 839 } |
| 840 | 840 |
| 841 @override | 841 @override |
| 842 Expression visitUnary(UnaryConstantExpression exp) { | 842 Expression visitUnary(UnaryConstantExpression exp, [_]) { |
| 843 return handlePrimitiveConstant(exp.value); | 843 return handlePrimitiveConstant(exp.value); |
| 844 } | 844 } |
| 845 } | 845 } |
| 846 | 846 |
| 847 /// Moves function parameters into a separate variable if one of its uses is | 847 /// Moves function parameters into a separate variable if one of its uses is |
| 848 /// shadowed by an inner function parameter. | 848 /// shadowed by an inner function parameter. |
| 849 /// This artifact is necessary because function parameters cannot be renamed. | 849 /// This artifact is necessary because function parameters cannot be renamed. |
| 850 class UnshadowParameters extends tree.RecursiveVisitor { | 850 class UnshadowParameters extends tree.RecursiveVisitor { |
| 851 | 851 |
| 852 /// Maps parameter names to their bindings. | 852 /// Maps parameter names to their bindings. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 : super(name, ElementKind.VARIABLE, enclosingElement, variables, null); | 910 : super(name, ElementKind.VARIABLE, enclosingElement, variables, null); |
| 911 | 911 |
| 912 ExecutableElement get executableContext => enclosingElement; | 912 ExecutableElement get executableContext => enclosingElement; |
| 913 | 913 |
| 914 ExecutableElement get memberContext => executableContext.memberContext; | 914 ExecutableElement get memberContext => executableContext.memberContext; |
| 915 | 915 |
| 916 bool get isLocal => true; | 916 bool get isLocal => true; |
| 917 | 917 |
| 918 LibraryElement get implementationLibrary => enclosingElement.library; | 918 LibraryElement get implementationLibrary => enclosingElement.library; |
| 919 } | 919 } |
| OLD | NEW |