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

Unified Diff: pkg/compiler/lib/src/cps_ir/cps_ir_builder_task.dart

Issue 1011403003: cps-ir: Set runtime type information for new objects that require it. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add parameter for each type variable to the JS-factory. Created 5 years, 9 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
Index: pkg/compiler/lib/src/cps_ir/cps_ir_builder_task.dart
diff --git a/pkg/compiler/lib/src/cps_ir/cps_ir_builder_task.dart b/pkg/compiler/lib/src/cps_ir/cps_ir_builder_task.dart
index ef33a135f4deaf41d3d735f9f4bd12fb948c0ff8..c111f4a6453c1751f4cab63ea7ff2ff7ce80d13c 100644
--- a/pkg/compiler/lib/src/cps_ir/cps_ir_builder_task.dart
+++ b/pkg/compiler/lib/src/cps_ir/cps_ir_builder_task.dart
@@ -856,6 +856,7 @@ abstract class IrBuilderVisitor extends ResolvedVisitor<ir.Primitive>
}
}
+ // TODO(karlklose): move to IrBuilder. Remove first argument.
ir.Primitive buildReifyTypeVariable(ir.Primitive target,
TypeVariableType variable);
@@ -1283,6 +1284,21 @@ class DartIrBuilderVisitor extends IrBuilderVisitor {
}
}
+/// The [IrBuilder]s view on the information about the program that has been
+/// computed in resolution and and type interence.
+class GlobalProgramInformation {
+ final Compiler _compiler;
+ JavaScriptBackend get _backend => _compiler.backend;
+
+ GlobalProgramInformation(this._compiler);
+
+ /// Returns [true], if the analysis could not determine that the type
+ /// arguments for the class [cls] are never used in the program.
+ bool requiresRuntimeTypesFor(ClassElement cls) {
+ return cls.typeVariables.isNotEmpty && _backend.classNeedsRti(cls);
+ }
+}
+
/// IR builder specific to the JavaScript backend, coupled to the [JsIrBuilder].
class JsIrBuilderVisitor extends IrBuilderVisitor {
/// Promote the type of [irBuilder] to [JsIrBuilder].
@@ -1428,6 +1444,13 @@ class JsIrBuilderVisitor extends IrBuilderVisitor {
return visitor.withBuilder(irBuilder, () => visitor.translateConstant(exp));
}
+ JsIrBuilder getBuilderFor(Element element) {
+ return new JsIrBuilder(
+ new GlobalProgramInformation(compiler),
+ compiler.backend.constantSystem,
+ element);
+ }
+
/// Builds the IR for a given constructor.
///
/// 1. Evaluates all own or inherited field initializers.
@@ -1438,21 +1461,54 @@ class JsIrBuilderVisitor extends IrBuilderVisitor {
constructor = constructor.implementation;
ClassElement classElement = constructor.enclosingClass.implementation;
- JsIrBuilder builder =
- new JsIrBuilder(compiler.backend.constantSystem, constructor);
+ JsIrBuilder builder = getBuilderFor(constructor);
+
+ final bool requiresTypeInformation =
+ builder.program.requiresRuntimeTypesFor(classElement);
return withBuilder(builder, () {
// Setup parameters and create a box if anything is captured.
- List<ParameterElement> parameters = [];
- constructor.functionSignature.orderedForEachParameter(parameters.add);
- builder.buildFunctionHeader(parameters,
+ List<Local> parameters = <Local>[];
+ constructor.functionSignature.orderedForEachParameter(
+ // Note: the closure is explicit to allow typechecking [FormalElement]
+ // against [Local].
+ (ParameterElement p) => parameters.add(p));
+
+ int firstTypeArgumentParameterIndex;
+
+ // If instances of the class may need runtime type information, we add a
+ // synthetic parameter for each type parameter.
+ if (requiresTypeInformation) {
+ firstTypeArgumentParameterIndex = parameters.length;
+ classElement.typeVariables.forEach((TypeVariableType variable) {
+ parameters.add(
+ new TypeInformationParameter(variable.element, constructor));
+ });
+ }
+
+ // Create IR parameters and setup the environment.
+ List<ir.Parameter> irParameters = builder.buildFunctionHeader(parameters,
closureScope: getClosureScopeForFunction(constructor));
+ // Create a list of the values of all type argument parameters, if any.
+ List<ir.Primitive> typeInformation;
+ if (requiresTypeInformation) {
+ List<ir.Primitive> typeArgumentParameters = <ir.Primitive>[];
+ for (int index = firstTypeArgumentParameterIndex;
+ index < irParameters.length;
+ ++index) {
+ typeArgumentParameters.add(irParameters[index]);
+ }
+ typeInformation = typeArgumentParameters;
+ }
+
// -- Step 1: evaluate field initializers ---
// Evaluate field initializers in constructor and super constructors.
+ irBuilder.enterInitializers();
List<ConstructorElement> constructorList = <ConstructorElement>[];
evaluateConstructorFieldInitializers(constructor, constructorList);
-
+ irBuilder.leaveInitializers();
+
// All parameters in all constructors are now bound in the environment.
// BoxLocals for captured parameters are also in the environment.
// The initial value of all fields are now bound in [fieldValues].
@@ -1469,8 +1525,10 @@ class JsIrBuilderVisitor extends IrBuilderVisitor {
// Native fields are initialized elsewhere.
}
}, includeSuperAndInjectedMembers: true);
- ir.Primitive instance =
- new ir.CreateInstance(classElement, instanceArguments);
+ ir.Primitive instance = new ir.CreateInstance(
+ classElement,
+ instanceArguments,
+ typeInformation);
irBuilder.add(new ir.LetPrim(instance));
// --- Step 3: call constructor bodies ---
@@ -1704,8 +1762,7 @@ class JsIrBuilderVisitor extends IrBuilderVisitor {
node,
elements);
- JsIrBuilder builder =
- new JsIrBuilder(compiler.backend.constantSystem, body);
+ JsIrBuilder builder = getBuilderFor(body);
return withBuilder(builder, () {
irBuilder.buildConstructorBodyHeader(getConstructorBodyParameters(body),
@@ -1727,8 +1784,7 @@ class JsIrBuilderVisitor extends IrBuilderVisitor {
element,
node,
elements);
- IrBuilder builder =
- new JsIrBuilder(compiler.backend.constantSystem, element);
+ IrBuilder builder = getBuilderFor(element);
return withBuilder(builder, () => _makeFunctionBody(element, node));
}
@@ -1810,8 +1866,9 @@ class JsIrBuilderVisitor extends IrBuilderVisitor {
@override
ir.Primitive buildReifyTypeVariable(ir.Primitive target,
TypeVariableType variable) {
- ir.Primitive typeArgument = new ir.ReadTypeVariable(variable, target);
- irBuilder.add(new ir.LetPrim(typeArgument));
+ ir.Primitive typeArgument =
+ irBuilder.buildTypeVariableAccess(target, variable);
+
ir.Primitive type = new ir.ReifyRuntimeType(typeArgument);
irBuilder.add(new ir.LetPrim(type));
return type;

Powered by Google App Engine
This is Rietveld 408576698