OLD | NEW |
| (Empty) |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library kernel.transformations.argument_extraction_for_redirecting; | |
6 | |
7 import '../ast.dart' | |
8 show | |
9 Program, | |
10 Constructor, | |
11 RedirectingInitializer, | |
12 Library, | |
13 LocalInitializer, | |
14 VariableDeclaration, | |
15 VariableGet, | |
16 Expression, | |
17 NamedExpression; | |
18 import '../core_types.dart' show CoreTypes; | |
19 import '../visitor.dart' show Transformer; | |
20 | |
21 Program transformProgram(CoreTypes coreTypes, Program program) { | |
22 new ArgumentExtractionForRedirecting().visitProgram(program); | |
23 return program; | |
24 } | |
25 | |
26 void transformLibraries(CoreTypes coreTypes, List<Library> libraries) { | |
27 var transformer = new ArgumentExtractionForRedirecting(); | |
28 for (var library in libraries) { | |
29 transformer.visitLibrary(library); | |
30 } | |
31 } | |
32 | |
33 class ArgumentExtractionForRedirecting extends Transformer { | |
34 visitConstructor(Constructor node) { | |
35 if (node.initializers.length == 1 && | |
36 node.initializers[0] is RedirectingInitializer) { | |
37 int index = 0; | |
38 RedirectingInitializer redirectingInitializer = node.initializers[0]; | |
39 List<Expression> positionalArguments = | |
40 redirectingInitializer.arguments.positional; | |
41 List<NamedExpression> namedArguments = | |
42 redirectingInitializer.arguments.named; | |
43 for (int i = 0; i < positionalArguments.length; i++) { | |
44 Expression argument = positionalArguments[i]; | |
45 VariableDeclaration extractedArgument = | |
46 new VariableDeclaration("extracted#$index", initializer: argument); | |
47 LocalInitializer initializer = new LocalInitializer(extractedArgument) | |
48 ..parent = node; | |
49 node.initializers.insert(index++, initializer); | |
50 positionalArguments[i] = new VariableGet(extractedArgument) | |
51 ..parent = redirectingInitializer.arguments; | |
52 } | |
53 for (int i = 0; i < namedArguments.length; i++) { | |
54 Expression argument = namedArguments[i].value; | |
55 VariableDeclaration extractedArgument = | |
56 new VariableDeclaration("extracted#$index", initializer: argument); | |
57 LocalInitializer initializer = new LocalInitializer(extractedArgument) | |
58 ..parent = node; | |
59 node.initializers.insert(index++, initializer); | |
60 namedArguments[i].value = new VariableGet(extractedArgument) | |
61 ..parent = redirectingInitializer.arguments; | |
62 } | |
63 } | |
64 return super.visitConstructor(node); | |
65 } | |
66 } | |
OLD | NEW |