OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 dart2js.ir_builder_task; | 5 library dart2js.ir_builder_task; |
6 | 6 |
7 import '../closure.dart' as closurelib; | 7 import '../closure.dart' as closurelib; |
8 import '../closure.dart' hide ClosureScope; | 8 import '../closure.dart' hide ClosureScope; |
9 import '../constants/expressions.dart'; | 9 import '../constants/expressions.dart'; |
10 import '../dart_types.dart'; | 10 import '../dart_types.dart'; |
11 import '../dart2jslib.dart'; | 11 import '../dart2jslib.dart'; |
12 import '../elements/elements.dart'; | 12 import '../elements/elements.dart'; |
13 import '../elements/modelx.dart' show SynthesizedConstructorElementX, | 13 import '../elements/modelx.dart' show SynthesizedConstructorElementX, |
14 ConstructorBodyElementX, FunctionSignatureX; | 14 ConstructorBodyElementX, FunctionSignatureX; |
15 import '../io/source_file.dart'; | 15 import '../io/source_file.dart'; |
16 import '../io/source_information.dart'; | 16 import '../io/source_information.dart'; |
17 import '../js_backend/js_backend.dart' show JavaScriptBackend; | 17 import '../js_backend/js_backend.dart' show JavaScriptBackend; |
18 import '../resolution/semantic_visitor.dart'; | 18 import '../resolution/semantic_visitor.dart'; |
19 import '../resolution/operators.dart' as op; | 19 import '../resolution/operators.dart' as op; |
20 import '../scanner/scannerlib.dart' show Token, isUserDefinableOperator; | 20 import '../scanner/scannerlib.dart' show Token, isUserDefinableOperator; |
21 import '../tree/tree.dart' as ast; | 21 import '../tree/tree.dart' as ast; |
22 import '../universe/universe.dart' show SelectorKind; | 22 import '../universe/universe.dart' show SelectorKind, CallStructure; |
23 import 'cps_ir_nodes.dart' as ir; | 23 import 'cps_ir_nodes.dart' as ir; |
24 import 'cps_ir_builder.dart'; | 24 import 'cps_ir_builder.dart'; |
25 | 25 |
26 /** | 26 /** |
27 * This task iterates through all resolved elements and builds [ir.Node]s. The | 27 * This task iterates through all resolved elements and builds [ir.Node]s. The |
28 * nodes are stored in the [nodes] map and accessible through [hasIr] and | 28 * nodes are stored in the [nodes] map and accessible through [hasIr] and |
29 * [getIr]. | 29 * [getIr]. |
30 * | 30 * |
31 * The functionality of the IrNodes is added gradually, therefore elements might | 31 * The functionality of the IrNodes is added gradually, therefore elements might |
32 * have an IR or not, depending on the language features that are used. For | 32 * have an IR or not, depending on the language features that are used. For |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 ClosureEnvironment getClosureEnvironment(); | 180 ClosureEnvironment getClosureEnvironment(); |
181 | 181 |
182 /// Normalizes the argument list to a static invocation (i.e. where the target | 182 /// Normalizes the argument list to a static invocation (i.e. where the target |
183 /// element is known). | 183 /// element is known). |
184 /// | 184 /// |
185 /// For the JS backend, inserts default arguments and normalizes order of | 185 /// For the JS backend, inserts default arguments and normalizes order of |
186 /// named arguments. | 186 /// named arguments. |
187 /// | 187 /// |
188 /// For the Dart backend, returns [arguments]. | 188 /// For the Dart backend, returns [arguments]. |
189 List<ir.Primitive> normalizeStaticArguments( | 189 List<ir.Primitive> normalizeStaticArguments( |
190 Selector selector, | 190 CallStructure callStructure, |
191 FunctionElement target, | 191 FunctionElement target, |
192 List<ir.Primitive> arguments); | 192 List<ir.Primitive> arguments); |
193 | 193 |
194 /// Normalizes the argument list of a dynamic invocation (i.e. where the | 194 /// Normalizes the argument list of a dynamic invocation (i.e. where the |
195 /// target element is unknown). | 195 /// target element is unknown). |
196 /// | 196 /// |
197 /// For the JS backend, normalizes order of named arguments. | 197 /// For the JS backend, normalizes order of named arguments. |
198 /// | 198 /// |
199 /// For the Dart backend, returns [arguments]. | 199 /// For the Dart backend, returns [arguments]. |
200 List<ir.Primitive> normalizeDynamicArguments( | 200 List<ir.Primitive> normalizeDynamicArguments( |
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 ast.Node argument, | 917 ast.Node argument, |
918 _) { | 918 _) { |
919 return irBuilder.buildNegation( | 919 return irBuilder.buildNegation( |
920 translateSuperBinary(function, op.BinaryOperator.NOT_EQ, argument)); | 920 translateSuperBinary(function, op.BinaryOperator.NOT_EQ, argument)); |
921 } | 921 } |
922 | 922 |
923 @override | 923 @override |
924 ir.Primitive visitUnary(ast.Send node, | 924 ir.Primitive visitUnary(ast.Send node, |
925 op.UnaryOperator operator, ast.Node expression, _) { | 925 op.UnaryOperator operator, ast.Node expression, _) { |
926 // TODO(johnniwinther): Clean up the creation of selectors. | 926 // TODO(johnniwinther): Clean up the creation of selectors. |
927 Selector selector = | 927 Selector selector = new Selector( |
928 new Selector(SelectorKind.OPERATOR, operator.selectorName, null, 0); | 928 SelectorKind.OPERATOR, |
| 929 new PublicName(operator.selectorName), |
| 930 CallStructure.NO_ARGS); |
929 ir.Primitive receiver = translateReceiver(expression); | 931 ir.Primitive receiver = translateReceiver(expression); |
930 return irBuilder.buildDynamicInvocation(receiver, selector, const []); | 932 return irBuilder.buildDynamicInvocation(receiver, selector, const []); |
931 } | 933 } |
932 | 934 |
933 @override | 935 @override |
934 ir.Primitive visitSuperUnary( | 936 ir.Primitive visitSuperUnary( |
935 ast.Send node, | 937 ast.Send node, |
936 op.UnaryOperator operator, | 938 op.UnaryOperator operator, |
937 FunctionElement function, | 939 FunctionElement function, |
938 _) { | 940 _) { |
939 // TODO(johnniwinther): Clean up the creation of selectors. | 941 // TODO(johnniwinther): Clean up the creation of selectors. |
940 Selector selector = | 942 Selector selector = new Selector( |
941 new Selector(SelectorKind.OPERATOR, operator.selectorName, null, 0); | 943 SelectorKind.OPERATOR, |
| 944 new PublicName(operator.selectorName), |
| 945 CallStructure.NO_ARGS); |
942 return irBuilder.buildSuperInvocation(function, selector, const []); | 946 return irBuilder.buildSuperInvocation(function, selector, const []); |
943 } | 947 } |
944 | 948 |
945 // TODO(johnniwinther): Handle this in the [IrBuilder] to ensure the correct | 949 // TODO(johnniwinther): Handle this in the [IrBuilder] to ensure the correct |
946 // semantic correlation between arguments and invocation. | 950 // semantic correlation between arguments and invocation. |
947 List<ir.Primitive> translateDynamicArguments(ast.NodeList nodeList, | 951 List<ir.Primitive> translateDynamicArguments(ast.NodeList nodeList, |
948 Selector selector) { | 952 Selector selector) { |
949 List<ir.Primitive> arguments = nodeList.nodes.mapToList(visit); | 953 List<ir.Primitive> arguments = nodeList.nodes.mapToList(visit); |
950 return normalizeDynamicArguments(selector, arguments); | 954 return normalizeDynamicArguments(selector, arguments); |
951 } | 955 } |
952 | 956 |
953 // TODO(johnniwinther): Handle this in the [IrBuilder] to ensure the correct | 957 // TODO(johnniwinther): Handle this in the [IrBuilder] to ensure the correct |
954 // semantic correlation between arguments and invocation. | 958 // semantic correlation between arguments and invocation. |
955 List<ir.Primitive> translateStaticArguments(ast.NodeList nodeList, | 959 List<ir.Primitive> translateStaticArguments(ast.NodeList nodeList, |
956 Element element, | 960 Element element, |
957 Selector selector) { | 961 CallStructure callStructure) { |
958 List<ir.Primitive> arguments = nodeList.nodes.mapToList(visit); | 962 List<ir.Primitive> arguments = nodeList.nodes.mapToList(visit); |
959 return normalizeStaticArguments(selector, element, arguments); | 963 return normalizeStaticArguments(callStructure, element, arguments); |
960 } | 964 } |
961 | 965 |
962 ir.Primitive translateCallInvoke(ir.Primitive target, | 966 ir.Primitive translateCallInvoke(ir.Primitive target, |
963 ast.NodeList arguments, | 967 ast.NodeList arguments, |
964 Selector selector) { | 968 Selector selector) { |
965 | 969 |
966 return irBuilder.buildCallInvocation(target, selector, | 970 return irBuilder.buildCallInvocation(target, selector, |
967 translateDynamicArguments(arguments, selector)); | 971 translateDynamicArguments(arguments, selector)); |
968 } | 972 } |
969 | 973 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 ast.Send node, | 1029 ast.Send node, |
1026 MethodElement function, | 1030 MethodElement function, |
1027 ast.NodeList arguments, | 1031 ast.NodeList arguments, |
1028 Selector selector, | 1032 Selector selector, |
1029 _) { | 1033 _) { |
1030 // TODO(karlklose): support foreign functions. | 1034 // TODO(karlklose): support foreign functions. |
1031 if (function.isForeign(compiler.backend)) { | 1035 if (function.isForeign(compiler.backend)) { |
1032 return giveup(node, 'handleStaticFunctionInvoke: foreign: $function'); | 1036 return giveup(node, 'handleStaticFunctionInvoke: foreign: $function'); |
1033 } | 1037 } |
1034 return irBuilder.buildStaticInvocation(function, selector, | 1038 return irBuilder.buildStaticInvocation(function, selector, |
1035 translateStaticArguments(arguments, function, selector), | 1039 translateStaticArguments(arguments, function, selector.callStructure), |
1036 sourceInformation: sourceInformationBuilder.buildCall(node)); | 1040 sourceInformation: sourceInformationBuilder.buildCall(node)); |
1037 } | 1041 } |
1038 | 1042 |
1039 @override | 1043 @override |
1040 ir.Primitive handleStaticGetterInvoke( | 1044 ir.Primitive handleStaticGetterInvoke( |
1041 ast.Send node, | 1045 ast.Send node, |
1042 FunctionElement getter, | 1046 FunctionElement getter, |
1043 ast.NodeList arguments, | 1047 ast.NodeList arguments, |
1044 Selector selector, | 1048 Selector selector, |
1045 _) { | 1049 _) { |
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1707 | 1711 |
1708 @override | 1712 @override |
1709 ir.Primitive handleConstructorInvoke( | 1713 ir.Primitive handleConstructorInvoke( |
1710 ast.NewExpression node, | 1714 ast.NewExpression node, |
1711 ConstructorElement constructor, | 1715 ConstructorElement constructor, |
1712 DartType type, | 1716 DartType type, |
1713 ast.NodeList arguments, | 1717 ast.NodeList arguments, |
1714 Selector selector, _) { | 1718 Selector selector, _) { |
1715 List<ir.Primitive> arguments = | 1719 List<ir.Primitive> arguments = |
1716 node.send.arguments.mapToList(visit, growable:false); | 1720 node.send.arguments.mapToList(visit, growable:false); |
1717 arguments = normalizeStaticArguments(selector, constructor, arguments); | 1721 arguments = normalizeStaticArguments( |
| 1722 selector.callStructure, constructor, arguments); |
1718 return irBuilder.buildConstructorInvocation( | 1723 return irBuilder.buildConstructorInvocation( |
1719 constructor, selector, type, arguments); | 1724 constructor, selector, type, arguments); |
1720 } | 1725 } |
1721 | 1726 |
1722 ir.Primitive visitStringJuxtaposition(ast.StringJuxtaposition node) { | 1727 ir.Primitive visitStringJuxtaposition(ast.StringJuxtaposition node) { |
1723 assert(irBuilder.isOpen); | 1728 assert(irBuilder.isOpen); |
1724 ir.Primitive first = visit(node.first); | 1729 ir.Primitive first = visit(node.first); |
1725 ir.Primitive second = visit(node.second); | 1730 ir.Primitive second = visit(node.second); |
1726 return irBuilder.buildStringConcatenation([first, second]); | 1731 return irBuilder.buildStringConcatenation([first, second]); |
1727 } | 1732 } |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2007 giveup(null, 'cannot handle synthetic forwarding constructors'); | 2012 giveup(null, 'cannot handle synthetic forwarding constructors'); |
2008 } | 2013 } |
2009 } | 2014 } |
2010 | 2015 |
2011 IrBuilder builder = makeIRBuilder(node, element); | 2016 IrBuilder builder = makeIRBuilder(node, element); |
2012 | 2017 |
2013 return withBuilder(builder, () => _makeFunctionBody(element, node)); | 2018 return withBuilder(builder, () => _makeFunctionBody(element, node)); |
2014 } | 2019 } |
2015 | 2020 |
2016 List<ir.Primitive> normalizeStaticArguments( | 2021 List<ir.Primitive> normalizeStaticArguments( |
2017 Selector selector, | 2022 CallStructure callStructure, |
2018 FunctionElement target, | 2023 FunctionElement target, |
2019 List<ir.Primitive> arguments) { | 2024 List<ir.Primitive> arguments) { |
2020 return arguments; | 2025 return arguments; |
2021 } | 2026 } |
2022 | 2027 |
2023 List<ir.Primitive> normalizeDynamicArguments( | 2028 List<ir.Primitive> normalizeDynamicArguments( |
2024 Selector selector, | 2029 Selector selector, |
2025 List<ir.Primitive> arguments) { | 2030 List<ir.Primitive> arguments) { |
2026 return arguments; | 2031 return arguments; |
2027 } | 2032 } |
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2538 ir.Primitive translateDefaultValue(ParameterElement parameter) { | 2543 ir.Primitive translateDefaultValue(ParameterElement parameter) { |
2539 if (parameter.initializer == null) { | 2544 if (parameter.initializer == null) { |
2540 return irBuilder.buildNullLiteral(); | 2545 return irBuilder.buildNullLiteral(); |
2541 } else { | 2546 } else { |
2542 return inlineConstant(parameter.executableContext, parameter.initializer); | 2547 return inlineConstant(parameter.executableContext, parameter.initializer); |
2543 } | 2548 } |
2544 } | 2549 } |
2545 | 2550 |
2546 /// Inserts default arguments and normalizes order of named arguments. | 2551 /// Inserts default arguments and normalizes order of named arguments. |
2547 List<ir.Primitive> normalizeStaticArguments( | 2552 List<ir.Primitive> normalizeStaticArguments( |
2548 Selector selector, | 2553 CallStructure callStructure, |
2549 FunctionElement target, | 2554 FunctionElement target, |
2550 List<ir.Primitive> arguments) { | 2555 List<ir.Primitive> arguments) { |
2551 target = target.implementation; | 2556 target = target.implementation; |
2552 FunctionSignature signature = target.functionSignature; | 2557 FunctionSignature signature = target.functionSignature; |
2553 if (!signature.optionalParametersAreNamed && | 2558 if (!signature.optionalParametersAreNamed && |
2554 signature.parameterCount == arguments.length) { | 2559 signature.parameterCount == arguments.length) { |
2555 // Optimization: don't copy the argument list for trivial cases. | 2560 // Optimization: don't copy the argument list for trivial cases. |
2556 return arguments; | 2561 return arguments; |
2557 } | 2562 } |
2558 | 2563 |
(...skipping 12 matching lines...) Expand all Loading... |
2571 } else { | 2576 } else { |
2572 result.add(translateDefaultValue(element)); | 2577 result.add(translateDefaultValue(element)); |
2573 } | 2578 } |
2574 }); | 2579 }); |
2575 } else { | 2580 } else { |
2576 int offset = i; | 2581 int offset = i; |
2577 // Iterate over the optional parameters of the signature, and try to | 2582 // Iterate over the optional parameters of the signature, and try to |
2578 // find them in [compiledNamedArguments]. If found, we use the | 2583 // find them in [compiledNamedArguments]. If found, we use the |
2579 // value in the temporary list, otherwise the default value. | 2584 // value in the temporary list, otherwise the default value. |
2580 signature.orderedOptionalParameters.forEach((ParameterElement element) { | 2585 signature.orderedOptionalParameters.forEach((ParameterElement element) { |
2581 int nameIndex = selector.namedArguments.indexOf(element.name); | 2586 int nameIndex = callStructure.namedArguments.indexOf(element.name); |
2582 if (nameIndex != -1) { | 2587 if (nameIndex != -1) { |
2583 int translatedIndex = offset + nameIndex; | 2588 int translatedIndex = offset + nameIndex; |
2584 result.add(arguments[translatedIndex]); | 2589 result.add(arguments[translatedIndex]); |
2585 } else { | 2590 } else { |
2586 result.add(translateDefaultValue(element)); | 2591 result.add(translateDefaultValue(element)); |
2587 } | 2592 } |
2588 }); | 2593 }); |
2589 } | 2594 } |
2590 return result; | 2595 return result; |
2591 } | 2596 } |
2592 | 2597 |
2593 /// Normalizes order of named arguments. | 2598 /// Normalizes order of named arguments. |
2594 List<ir.Primitive> normalizeDynamicArguments( | 2599 List<ir.Primitive> normalizeDynamicArguments( |
2595 Selector selector, | 2600 Selector selector, |
2596 List<ir.Primitive> arguments) { | 2601 List<ir.Primitive> arguments) { |
2597 assert(arguments.length == selector.argumentCount); | 2602 CallStructure callStructure = selector.callStructure; |
| 2603 assert(arguments.length == callStructure.argumentCount); |
2598 // Optimization: don't copy the argument list for trivial cases. | 2604 // Optimization: don't copy the argument list for trivial cases. |
2599 if (selector.namedArguments.isEmpty) return arguments; | 2605 if (callStructure.namedArguments.isEmpty) return arguments; |
2600 List<ir.Primitive> result = <ir.Primitive>[]; | 2606 List<ir.Primitive> result = <ir.Primitive>[]; |
2601 for (int i=0; i < selector.positionalArgumentCount; i++) { | 2607 for (int i=0; i < callStructure.positionalArgumentCount; i++) { |
2602 result.add(arguments[i]); | 2608 result.add(arguments[i]); |
2603 } | 2609 } |
2604 for (String argName in selector.getOrderedNamedArguments()) { | 2610 for (String argName in callStructure.getOrderedNamedArguments()) { |
2605 int nameIndex = selector.namedArguments.indexOf(argName); | 2611 int nameIndex = callStructure.namedArguments.indexOf(argName); |
2606 int translatedIndex = selector.positionalArgumentCount + nameIndex; | 2612 int translatedIndex = callStructure.positionalArgumentCount + nameIndex; |
2607 result.add(arguments[translatedIndex]); | 2613 result.add(arguments[translatedIndex]); |
2608 } | 2614 } |
2609 return result; | 2615 return result; |
2610 } | 2616 } |
2611 | 2617 |
2612 @override | 2618 @override |
2613 ir.Primitive buildReifyTypeVariable(ir.Primitive target, | 2619 ir.Primitive buildReifyTypeVariable(ir.Primitive target, |
2614 TypeVariableType variable) { | 2620 TypeVariableType variable) { |
2615 ir.Primitive typeArgument = | 2621 ir.Primitive typeArgument = |
2616 irBuilder.buildTypeVariableAccess(target, variable); | 2622 irBuilder.buildTypeVariableAccess(target, variable); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2654 SourceInformation buildCall(ast.Node node) { | 2660 SourceInformation buildCall(ast.Node node) { |
2655 return new PositionSourceInformation( | 2661 return new PositionSourceInformation( |
2656 new TokenSourceLocation(sourceFile, node.getBeginToken(), name)); | 2662 new TokenSourceLocation(sourceFile, node.getBeginToken(), name)); |
2657 } | 2663 } |
2658 | 2664 |
2659 @override | 2665 @override |
2660 SourceInformationBuilder forContext(AstElement element) { | 2666 SourceInformationBuilder forContext(AstElement element) { |
2661 return new PositionSourceInformationBuilder(element); | 2667 return new PositionSourceInformationBuilder(element); |
2662 } | 2668 } |
2663 } | 2669 } |
OLD | NEW |