Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library('runtime_types'); | 5 #library('runtime_types'); |
| 6 | 6 |
| 7 #import('leg.dart'); | 7 #import('leg.dart'); |
| 8 #import('elements/elements.dart'); | 8 #import('elements/elements.dart'); |
| 9 #import('tree/tree.dart'); | 9 #import('tree/tree.dart'); |
| 10 #import('universe/universe.dart'); | 10 #import('universe/universe.dart'); |
| 11 #import('util/util.dart'); | 11 #import('util/util.dart'); |
| 12 | 12 |
| 13 class RuntimeTypeInformation { | 13 class RuntimeTypeInformation { |
| 14 /** | |
| 15 * Names used for elements in runtime type information. This map is kept to | |
| 16 * detect elements with the same name use a different name instead. | |
| 17 */ | |
| 18 final Map<String, Element> usedNames = new Map<String, Element>(); | |
| 19 | |
| 20 /** Get a unique name for the element. */ | |
| 21 String getName(Element element) { | |
|
ngeoffray
2012/10/15 12:57:55
This very much look like code dealt else where. Co
| |
| 22 String guess = element.name.slowToString(); | |
| 23 String name = guess; | |
| 24 int id = 0; | |
| 25 while (usedNames.containsKey(name) && usedNames[name] != element) { | |
| 26 name = '$guess@$id'; | |
| 27 id++; | |
| 28 } | |
| 29 usedNames[name] = element; | |
| 30 return name; | |
| 31 } | |
| 32 | |
| 14 bool hasTypeArguments(DartType type) { | 33 bool hasTypeArguments(DartType type) { |
| 15 if (type is InterfaceType) { | 34 if (type is InterfaceType) { |
| 16 InterfaceType interfaceType = type; | 35 InterfaceType interfaceType = type; |
| 17 return !interfaceType.arguments.isEmpty(); | 36 return !interfaceType.arguments.isEmpty(); |
| 18 } | 37 } |
| 19 return false; | 38 return false; |
| 20 } | 39 } |
| 21 | 40 |
| 22 /** | 41 /** |
| 23 * Map type variables to strings calling [:stringify:] and joins the results | 42 * Map type variables to strings calling [:stringify:] and joins the results |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 41 }); | 60 }); |
| 42 return buffer.toString(); | 61 return buffer.toString(); |
| 43 } | 62 } |
| 44 | 63 |
| 45 /** | 64 /** |
| 46 * Generate a string representation template for this element, using '#' to | 65 * Generate a string representation template for this element, using '#' to |
| 47 * denote the place for the type argument input. If there are more type | 66 * denote the place for the type argument input. If there are more type |
| 48 * variables than [numberOfInputs], 'Dynamic' is used as the value for these | 67 * variables than [numberOfInputs], 'Dynamic' is used as the value for these |
| 49 * arguments. | 68 * arguments. |
| 50 */ | 69 */ |
| 51 static String generateRuntimeTypeString(ClassElement element, | 70 String generateRuntimeTypeString(ClassElement element, int numberOfInputs) { |
| 52 int numberOfInputs) { | 71 String elementName = getName(element); |
|
ngeoffray
2012/10/15 12:57:55
Use the namer instead?
| |
| 53 String elementName = element.name.slowToString(); | |
| 54 if (element.typeVariables.isEmpty()) return "'$elementName'"; | 72 if (element.typeVariables.isEmpty()) return "'$elementName'"; |
| 55 String stringify(_, bool hasValue) => hasValue ? "' + # + '" : "Dynamic"; | 73 String stringify(_, bool hasValue) => hasValue ? "' + # + '" : "Dynamic"; |
| 56 String arguments = stringifyTypeVariables(element.typeVariables, | 74 String arguments = stringifyTypeVariables(element.typeVariables, |
| 57 numberOfInputs, | 75 numberOfInputs, |
| 58 stringify); | 76 stringify); |
| 59 return "'$elementName<$arguments>'"; | 77 return "'$elementName<$arguments>'"; |
| 60 } | 78 } |
| 61 | 79 |
| 62 /** | 80 /** |
| 63 * Generate a string template for the runtime type fields that contain the | 81 * Generate a string template for the runtime type fields that contain the |
| 64 * type descriptions of the reified type arguments, using '#' to denote the | 82 * type descriptions of the reified type arguments, using '#' to denote the |
| 65 * place for the type argument value, or [:null:] if there are more than | 83 * place for the type argument value, or [:null:] if there are more than |
| 66 * [numberOfInputs] type variables. | 84 * [numberOfInputs] type variables. |
| 67 */ | 85 */ |
| 68 static String generateTypeVariableString(ClassElement element, | 86 static String generateTypeVariableString(ClassElement element, |
| 69 int numberOfInputs) { | 87 int numberOfInputs) { |
| 70 String stringify(TypeVariableType variable, bool hasValue) { | 88 String stringify(TypeVariableType variable, bool hasValue) { |
| 71 return "'${variable.name.slowToString()}': #"; | 89 return "'${variable.name.slowToString()}': #"; |
| 72 } | 90 } |
| 73 return stringifyTypeVariables(element.typeVariables, numberOfInputs, | 91 return stringifyTypeVariables(element.typeVariables, numberOfInputs, |
| 74 stringify); | 92 stringify); |
| 75 } | 93 } |
| 76 } | 94 } |
| OLD | NEW |