4f2bdff90b
Change-Id: I1a3cc03fba9783807fa637a9d42fdbad68ee7686 Reviewed-on: https://dart-review.googlesource.com/31040 Commit-Queue: Peter von der Ahé <ahe@google.com> Reviewed-by: Kevin Millikin <kmillikin@google.com>
65 lines
1.9 KiB
Dart
65 lines
1.9 KiB
Dart
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
library kernel.transformations.argument_extraction;
|
|
|
|
import '../ast.dart'
|
|
show
|
|
Constructor,
|
|
FieldInitializer,
|
|
Initializer,
|
|
Library,
|
|
LocalInitializer,
|
|
Component,
|
|
VariableDeclaration,
|
|
VariableGet;
|
|
import '../core_types.dart' show CoreTypes;
|
|
import '../visitor.dart' show Transformer;
|
|
|
|
Component transformComponent(CoreTypes coreTypes, Component component) {
|
|
new ArgumentExtractionForTesting().visitComponent(component);
|
|
return component;
|
|
}
|
|
|
|
void transformLibraries(CoreTypes coreTypes, List<Library> libraries) {
|
|
var transformer = new ArgumentExtractionForTesting();
|
|
for (var library in libraries) {
|
|
transformer.visitLibrary(library);
|
|
}
|
|
}
|
|
|
|
class ArgumentExtractionForTesting extends Transformer {
|
|
visitConstructor(Constructor node) {
|
|
var newInits = <Initializer>[];
|
|
|
|
int nameCounter = 0;
|
|
for (var fieldInit in node.initializers) {
|
|
if (fieldInit is FieldInitializer &&
|
|
!fieldInit.field.name.name.endsWith("_li")) {
|
|
// Move the body of the initializer to a new local initializer, and
|
|
// eta-expand the reference to the local initializer in the body of the
|
|
// field initializer.
|
|
var value = fieldInit.value;
|
|
|
|
var decl = new VariableDeclaration('#li_$nameCounter');
|
|
decl.initializer = value;
|
|
var localInit = new LocalInitializer(decl);
|
|
localInit.parent = node;
|
|
newInits.add(localInit);
|
|
|
|
fieldInit.value = new VariableGet(decl);
|
|
fieldInit.value.parent = fieldInit;
|
|
|
|
++nameCounter;
|
|
newInits.add(fieldInit);
|
|
} else {
|
|
newInits.add(fieldInit);
|
|
}
|
|
}
|
|
|
|
node.initializers = newInits;
|
|
return super.visitConstructor(node);
|
|
}
|
|
}
|