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; |
| 6 |
| 7 import '../ast.dart' |
| 8 show |
| 9 Constructor, |
| 10 FieldInitializer, |
| 11 Initializer, |
| 12 Library, |
| 13 LocalInitializer, |
| 14 Program, |
| 15 VariableDeclaration, |
| 16 VariableGet; |
| 17 import '../core_types.dart' show CoreTypes; |
| 18 import '../visitor.dart' show Transformer; |
| 19 |
| 20 Program transformProgram(CoreTypes coreTypes, Program program) { |
| 21 new ArgumentExtractionForTesting().visitProgram(program); |
| 22 return program; |
| 23 } |
| 24 |
| 25 void transformLibraries(CoreTypes coreTypes, List<Library> libraries) { |
| 26 var transformer = new ArgumentExtractionForTesting(); |
| 27 for (var library in libraries) { |
| 28 transformer.visitLibrary(library); |
| 29 } |
| 30 } |
| 31 |
| 32 class ArgumentExtractionForTesting extends Transformer { |
| 33 visitConstructor(Constructor node) { |
| 34 var newInits = <Initializer>[]; |
| 35 |
| 36 int nameCounter = 0; |
| 37 for (var fieldInit in node.initializers) { |
| 38 if (fieldInit is FieldInitializer && |
| 39 !fieldInit.field.name.name.endsWith("_li")) { |
| 40 // Move the body of the initializer to a new local initializer, and |
| 41 // eta-expand the reference to the local initializer in the body of the |
| 42 // field initializer. |
| 43 var value = fieldInit.value; |
| 44 |
| 45 var decl = new VariableDeclaration('#li_$nameCounter'); |
| 46 decl.initializer = value; |
| 47 var localInit = new LocalInitializer(decl); |
| 48 localInit.parent = node; |
| 49 newInits.add(localInit); |
| 50 |
| 51 fieldInit.value = new VariableGet(decl); |
| 52 fieldInit.value.parent = fieldInit; |
| 53 |
| 54 ++nameCounter; |
| 55 newInits.add(fieldInit); |
| 56 } else { |
| 57 newInits.add(fieldInit); |
| 58 } |
| 59 } |
| 60 |
| 61 node.initializers = newInits; |
| 62 return super.visitConstructor(node); |
| 63 } |
| 64 } |
OLD | NEW |