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

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

Issue 10908142: Add runtimeType() to Object which returns canonicalized instances of Type. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 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: lib/compiler/implementation/ssa/builder.dart
diff --git a/lib/compiler/implementation/ssa/builder.dart b/lib/compiler/implementation/ssa/builder.dart
index 75c7877de8d50ca3b1f5d008465d0361af8e9761..bb2c67497fbf8264524ecfebae591e69f4f225aa 100644
--- a/lib/compiler/implementation/ssa/builder.dart
+++ b/lib/compiler/implementation/ssa/builder.dart
@@ -1242,14 +1242,13 @@ class SsaBuilder extends ResolvedVisitor implements Visitor {
HForeignNew newObject = new HForeignNew(classElement, constructorArguments);
add(newObject);
- // If the class has type variables, create the runtime type
- // information with the type parameters provided.
- if (!classElement.typeVariables.isEmpty()) {
- List<HInstruction> rtiInputs = <HInstruction>[];
+ // Create the runtime type information, if needed.
+ if (needsRuntimeTypeInfo(classElement)) {
+ List<HInstruction> inputs = <HInstruction>[];
classElement.typeVariables.forEach((TypeVariableType typeVariable) {
- rtiInputs.add(localsHandler.directLocals[typeVariable.element]);
+ inputs.add(localsHandler.directLocals[typeVariable.element]);
});
- callSetRuntimeTypeInfo(classElement, rtiInputs, newObject);
+ callSetRuntimeTypeInfo(classElement, inputs, newObject);
}
// Generate calls to the constructor bodies.
@@ -2638,7 +2637,7 @@ class SsaBuilder extends ResolvedVisitor implements Visitor {
void handleListConstructor(InterfaceType type,
Node currentNode,
HInstruction newObject) {
- if (type.arguments.isEmpty()) return;
+ if (!needsRuntimeTypeInfo(type.element)) return;
List<HInstruction> inputs = <HInstruction>[];
type.arguments.forEach((DartType argument) {
inputs.add(analyzeTypeArgument(argument, currentNode));
@@ -2646,23 +2645,87 @@ class SsaBuilder extends ResolvedVisitor implements Visitor {
callSetRuntimeTypeInfo(type.element, inputs, newObject);
}
+ // Runtime type information is required if the type has type
+ // variables or if the program calls [runtimeType].
+ // TODO(karlklose): it is unnecessary for type variables that
+ // can never match an is-Check.
+ bool needsRuntimeTypeInfo(ClassElement element) {
+ bool classHasTypeVariables = !element.typeVariables.isEmpty();
+ bool runtimeTypeIsUsed = compiler.enabledRuntimeType;
+ return (classHasTypeVariables || runtimeTypeIsUsed);
+ }
+
void callSetRuntimeTypeInfo(ClassElement element,
- List<HInstruction> inputs,
+ List<HInstruction> rtiInputs,
HInstruction newObject) {
ngeoffray 2012/09/14 07:30:21 I believe you could move the creation of the strin
- List<String> typeVariables = <String>[];
- element.typeVariables.forEach((TypeVariableType typeVariable) {
- typeVariables.add("'$typeVariable': #");
+ bool classHasTypeVariables = !element.typeVariables.isEmpty();
+ bool runtimeTypeIsUsed = compiler.enabledRuntimeType;
+
+ // Prepare the format strings for the reified type variables and the
+ // runtime type, if necessary.
+ String runtimeTypeString = "'${element.name.slowToString()}";
kasperl 2012/09/14 07:26:48 Move the computation of the runtimeTypeString into
+ String typeVariablesString = '';
+ bool firstVariable = true;
+ int numberOfVariables = 0;
+ element.typeVariables.forEach((TypeVariableType variable) {
+ String name = variable.name.slowToString();
+ String value = (numberOfVariables < rtiInputs.length) ? '#' : "'Dynamic'";
+ if (runtimeTypeIsUsed) {
+ if (firstVariable) {
+ runtimeTypeString = "$runtimeTypeString<' + $value";
+ } else {
+ runtimeTypeString = "$runtimeTypeString + ', ' + $value";
+ }
+ }
+ if (!firstVariable) {
+ typeVariablesString = '$typeVariablesString, ';
+ }
+ typeVariablesString = "$typeVariablesString'$name': $value";
+ firstVariable = false;
+ numberOfVariables++;
});
-
- String jsCode = '{ ${Strings.join(typeVariables, ', ')} }';
- HInstruction typeInfo = new HForeign(new LiteralDartString(jsCode),
- new LiteralDartString('Object'),
- inputs);
- add(typeInfo);
+ if (classHasTypeVariables) {
+ runtimeTypeString = "$runtimeTypeString + '>";
+ }
+ runtimeTypeString = "$runtimeTypeString'";
+
+ // Construct the runtime type information.
+ HInstruction runtimeType;
+ HInstruction typeInfo;
+ String runtimeCode = '{';
kasperl 2012/09/14 07:26:48 Use StringBuffer.
+ List<HInstruction> runtimeCodeInputs = <HInstruction>[];
+ if (runtimeTypeIsUsed) {
+ runtimeType =
+ new HForeign(new LiteralDartString(runtimeTypeString),
+ new LiteralDartString('String'),
+ rtiInputs);
+ add(runtimeType);
+ runtimeCodeInputs.add(runtimeType);
+ runtimeCode = runtimeCode.concat('runtimeType: #');
+ }
+ if (classHasTypeVariables) {
+ if (runtimeTypeIsUsed) runtimeCode = runtimeCode.concat(', ');
+ HInstruction typeInfo =
+ new HForeign(new LiteralDartString(typeVariablesString),
+ new LiteralDartString('String'),
+ rtiInputs);
+ add(typeInfo);
+ runtimeCodeInputs.add(typeInfo);
+ runtimeCode = runtimeCode.concat('#');
+ }
+ runtimeCode = runtimeCode.concat('}');
+ HInstruction runtimeInfo =
+ new HForeign(new LiteralDartString(runtimeCode),
+ new LiteralDartString('Object'),
+ runtimeCodeInputs);
+ add(runtimeInfo);
+
+ // Set the runtime type information on the object.
Element typeInfoSetterElement = interceptors.getSetRuntimeTypeInfo();
HInstruction typeInfoSetter = new HStatic(typeInfoSetterElement);
add(typeInfoSetter);
- add(new HInvokeStatic(<HInstruction>[typeInfoSetter, newObject, typeInfo]));
+ add(new HInvokeStatic(
+ <HInstruction>[typeInfoSetter, newObject, runtimeInfo]));
}
visitNewSend(Send node) {

Powered by Google App Engine
This is Rietveld 408576698