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

Unified Diff: sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart

Issue 11360228: Simplify runtime type support. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month 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: sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart
diff --git a/sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart b/sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart
index e4b8122b9ccb05e3f61488b4786b28afcbd855d4..7feb4013b844d73ef177bc24fd779dee4a7eaea3 100644
--- a/sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart
+++ b/sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart
@@ -2,68 +2,50 @@
// 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 runtime_types;
-
-import '../dart2jslib.dart';
-import '../elements/elements.dart';
-import '../tree/tree.dart';
-import '../universe/universe.dart';
-import '../util/util.dart';
+part of js_backend;
class RuntimeTypeInformation {
- /**
- * Names used for elements in runtime type information. This map is kept to
- * detect elements with the same name and use a different name instead.
- */
- final Map<String, Element> usedNames = new Map<String, Element>();
-
final Compiler compiler;
- RuntimeTypeInformation(this.compiler) {
- // Reserve the name 'dynamic' for the dynamic type.
- usedNames['dynamic'] = compiler.dynamicClass;
- }
-
- /** Get a unique name for the element. */
- String getName(Element element) {
- if (element == compiler.dynamicClass) return 'dynamic';
- String guess = element.name.slowToString();
- String name = guess;
- int id = 0;
- while (usedNames.containsKey(name) && usedNames[name] != element) {
- name = '$guess@$id';
- id++;
- }
- usedNames[name] = element;
- return name;
- }
+ RuntimeTypeInformation(this.compiler);
// TODO(karlklose): remove when using type representations.
- String buildStringRepresentation(DartType type) {
+ String getStringRepresentation(DartType type, {bool expandRawType: false}) {
StringBuffer builder = new StringBuffer();
void build(DartType t) {
- builder.add(getName(t.element));
+ if (t is TypeVariableType) {
+ builder.add(t.name.slowToString());
+ return;
+ }
+ JavaScriptBackend backend = compiler.backend;
+ builder.add(backend.namer.getName(t.element));
if (t is InterfaceType) {
InterfaceType interface = t;
- if (interface.arguments.isEmpty) return;
- bool firstArgument = true;
+ ClassElement element = t.element;
+ if (element.typeVariables.isEmpty) return;
+ bool isRaw = interface.arguments.isEmpty;
+ if (isRaw && !expandRawType) return;
builder.add('<');
- for (DartType argument in interface.arguments) {
- if (firstArgument) {
- firstArgument = false;
+ Iterable items = isRaw ? element.typeVariables : interface.arguments;
+ var stringify = isRaw ? (_) => 'dynamic' : (type) => type.toString();
+ bool first = true;
+ for (var item in items) {
+ if (first) {
+ first = false;
} else {
builder.add(', ');
}
- build(argument);
+ builder.add(stringify(item));
}
builder.add('>');
}
}
+
build(type);
return builder.toString();
}
- bool hasTypeArguments(DartType type) {
+ static bool hasTypeArguments(DartType type) {
if (type is InterfaceType) {
InterfaceType interfaceType = type;
return !interfaceType.arguments.isEmpty;
@@ -71,57 +53,12 @@ class RuntimeTypeInformation {
return false;
}
- /**
- * Map type variables to strings calling [:stringify:] and joins the results
- * to a single string separated by commas.
- * The argument [:hasValue:] is used to treat variables that will not receive
- * a value at the use site of the code that is generated with this function.
- */
- static String stringifyTypeVariables(Link collection,
- int numberOfInputs,
- stringify(TypeVariableType variable,
- bool hasValue)) {
- int currentVariable = 0;
- bool isFirst = true;
- StringBuffer buffer = new StringBuffer();
- collection.forEach((TypeVariableType variable) {
- if (!isFirst) buffer.add(", ");
- bool hasValue = currentVariable < numberOfInputs;
- buffer.add(stringify(variable, hasValue));
- isFirst = false;
- currentVariable++;
- });
- return buffer.toString();
- }
-
- /**
- * Generate a string representation template for this element, using '#' to
- * denote the place for the type argument input. If there are more type
- * variables than [numberOfInputs], 'dynamic' is used as the value for these
- * arguments.
- */
- String generateRuntimeTypeString(ClassElement element, int numberOfInputs) {
- String elementName = getName(element);
- if (element.typeVariables.isEmpty) return "$elementName";
- String stringify(_, bool hasValue) => hasValue ? "' + # + '" : "dynamic";
- String arguments = stringifyTypeVariables(element.typeVariables,
- numberOfInputs,
- stringify);
- return "$elementName<$arguments>";
- }
-
- /**
- * Generate a string template for the runtime type fields that contain the
- * type descriptions of the reified type arguments, using '#' to denote the
- * place for the type argument value, or [:null:] if there are more than
- * [numberOfInputs] type variables.
- */
- static String generateTypeVariableString(ClassElement element,
- int numberOfInputs) {
- String stringify(TypeVariableType variable, bool hasValue) {
- return "'${variable.name.slowToString()}': #";
+ static int getTypeVariableIndex(TypeVariableType variable) {
+ ClassElement classElement = variable.element.getEnclosingClass();
+ Link<DartType> variables = classElement.typeVariables;
+ for (int index = 0; !variables.isEmpty;
+ index++, variables = variables.tail) {
+ if (variables.head == variable) return index;
}
- return stringifyTypeVariables(element.typeVariables, numberOfInputs,
- stringify);
}
}

Powered by Google App Engine
This is Rietveld 408576698