Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Unified Diff: lib/compiler/implementation/ssa/builder.dart

Issue 10873033: Support non-const instance field initializers. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/compiler/implementation/resolver.dart ('k') | tests/co19/co19-dart2js.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiler/implementation/ssa/builder.dart
===================================================================
--- lib/compiler/implementation/ssa/builder.dart (revision 11297)
+++ lib/compiler/implementation/ssa/builder.dart (working copy)
@@ -1004,6 +1004,8 @@
element: constructor);
}
+ buildFieldInitializers(constructor.enclosingElement, fieldValues);
+
int index = 0;
FunctionSignature params = constructor.computeSignature(compiler);
params.forEachParameter((Element parameter) {
@@ -1088,6 +1090,35 @@
}
/**
+ * Run through the fields of [cls] and add their potential
+ * initializers.
+ */
+ void buildFieldInitializers(ClassElement classElement,
+ Map<Element, HInstruction> fieldValues) {
+ classElement.forEachInstanceField(
+ includeBackendMembers: true,
+ includeSuperMembers: false,
+ f: (ClassElement enclosingClass, Element member) {
+ TreeElements definitions = compiler.analyzeElement(member);
+ Node node = member.parseNode(compiler);
+ SendSet assignment = node.asSendSet();
+ HInstruction value;
+ if (assignment === null) {
+ value = graph.addConstantNull();
+ } else {
+ Node right = assignment.arguments.head;
+ TreeElements savedElements = elements;
+ elements = definitions;
+ right.accept(this);
+ elements = savedElements;
+ value = pop();
+ }
+ fieldValues[member] = value;
+ });
+ }
+
+
+ /**
* Build the factory function corresponding to the constructor
* [functionElement]:
* - Initialize fields with the values of the field initializers of the
@@ -1107,10 +1138,16 @@
openFunction(functionElement, function);
Map<Element, HInstruction> fieldValues = new Map<Element, HInstruction>();
+
+ // Compile the possible initialization code for local fields and
+ // super fields.
+ buildFieldInitializers(classElement, fieldValues);
+
+ // Compile field-parameters such as [:this.x:].
FunctionSignature params = functionElement.computeSignature(compiler);
params.forEachParameter((Element element) {
if (element.kind == ElementKind.FIELD_PARAMETER) {
- // If the [element] is a field-parameter (such as [:this.x:] then
+ // If the [element] is a field-parameter then
// initialize the field element with its value.
FieldParameterElement fieldParameterElement = element;
HInstruction parameterValue = localsHandler.readLocal(element);
@@ -1118,10 +1155,9 @@
}
});
- List<FunctionElement> constructors = <FunctionElement>[functionElement];
-
// Analyze the constructor and all referenced constructors and collect
// initializers and constructor bodies.
+ List<FunctionElement> constructors = <FunctionElement>[functionElement];
buildInitializers(functionElement, constructors, fieldValues);
// Call the JavaScript constructor with the fields as argument.
@@ -1130,14 +1166,7 @@
includeBackendMembers: true,
includeSuperMembers: true,
f: (ClassElement enclosingClass, Element member) {
- HInstruction value = fieldValues[member];
- if (value === null) {
- // The field has no value in the initializer list. Initialize it
- // with the declaration-site constant (if any).
- Constant fieldValue = compiler.constantHandler.compileVariable(member);
- value = graph.addConstant(fieldValue);
- }
- constructorArguments.add(value);
+ constructorArguments.add(fieldValues[member]);
});
HForeignNew newObject = new HForeignNew(classElement, constructorArguments);
« no previous file with comments | « lib/compiler/implementation/resolver.dart ('k') | tests/co19/co19-dart2js.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698