OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 js_backend.runtime_types; | 5 library js_backend.runtime_types; |
6 | 6 |
7 import '../common_elements.dart' show CommonElements, ElementEnvironment; | 7 import '../common_elements.dart' show CommonElements, ElementEnvironment; |
8 import '../elements/elements.dart' show ClassElement; | 8 import '../elements/elements.dart' show ClassElement; |
9 import '../elements/entities.dart'; | 9 import '../elements/entities.dart'; |
10 import '../elements/resolution_types.dart' | 10 import '../elements/resolution_types.dart' |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 final Set<ClassEntity> classesUsingTypeVariableExpression; | 230 final Set<ClassEntity> classesUsingTypeVariableExpression; |
231 | 231 |
232 _RuntimeTypesNeed( | 232 _RuntimeTypesNeed( |
233 this._elementEnvironment, | 233 this._elementEnvironment, |
234 this._backendUsage, | 234 this._backendUsage, |
235 this.classesNeedingRti, | 235 this.classesNeedingRti, |
236 this.methodsNeedingRti, | 236 this.methodsNeedingRti, |
237 this.localFunctionsNeedingRti, | 237 this.localFunctionsNeedingRti, |
238 this.classesUsingTypeVariableExpression); | 238 this.classesUsingTypeVariableExpression); |
239 | 239 |
240 bool checkClass(ClassEntity cls) => true; | 240 bool checkClass(covariant ClassEntity cls) => true; |
241 | 241 |
242 bool classNeedsRti(ClassEntity cls) { | 242 bool classNeedsRti(ClassEntity cls) { |
243 assert(checkClass(cls)); | 243 assert(checkClass(cls)); |
244 if (_backendUsage.isRuntimeTypeUsed) return true; | 244 if (_backendUsage.isRuntimeTypeUsed) return true; |
245 return classesNeedingRti.contains(cls); | 245 return classesNeedingRti.contains(cls); |
246 } | 246 } |
247 | 247 |
248 bool classNeedsRtiField(ClassEntity cls) { | 248 bool classNeedsRtiField(ClassEntity cls) { |
249 assert(checkClass(cls)); | 249 assert(checkClass(cls)); |
250 if (!_elementEnvironment.isGenericClass(cls)) return false; | 250 if (!_elementEnvironment.isGenericClass(cls)) return false; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 | 293 |
294 final Map<ClassEntity, Set<ClassEntity>> rtiDependencies = | 294 final Map<ClassEntity, Set<ClassEntity>> rtiDependencies = |
295 <ClassEntity, Set<ClassEntity>>{}; | 295 <ClassEntity, Set<ClassEntity>>{}; |
296 | 296 |
297 final Set<ClassEntity> classesUsingTypeVariableExpression = | 297 final Set<ClassEntity> classesUsingTypeVariableExpression = |
298 new Set<ClassEntity>(); | 298 new Set<ClassEntity>(); |
299 | 299 |
300 RuntimeTypesNeedBuilderImpl(this._elementEnvironment, DartTypes types) | 300 RuntimeTypesNeedBuilderImpl(this._elementEnvironment, DartTypes types) |
301 : super(types); | 301 : super(types); |
302 | 302 |
303 bool checkClass(ClassEntity cls) => true; | 303 bool checkClass(covariant ClassEntity cls) => true; |
304 | 304 |
305 @override | 305 @override |
306 void registerClassUsingTypeVariableExpression(ClassEntity cls) { | 306 void registerClassUsingTypeVariableExpression(ClassEntity cls) { |
307 classesUsingTypeVariableExpression.add(cls); | 307 classesUsingTypeVariableExpression.add(cls); |
308 } | 308 } |
309 | 309 |
310 @override | 310 @override |
311 void registerRtiDependency(ClassEntity element, ClassEntity dependency) { | 311 void registerRtiDependency(ClassEntity element, ClassEntity dependency) { |
312 // We're not dealing with typedef for now. | 312 // We're not dealing with typedef for now. |
313 assert(element != null); | 313 assert(element != null); |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 jsAst.Expression encoding = | 838 jsAst.Expression encoding = |
839 getTypeRepresentation(emitter, type, onVariable); | 839 getTypeRepresentation(emitter, type, onVariable); |
840 if (contextClass == null && !alwaysGenerateFunction) { | 840 if (contextClass == null && !alwaysGenerateFunction) { |
841 return encoding; | 841 return encoding; |
842 } else { | 842 } else { |
843 List<String> parameters = const <String>[]; | 843 List<String> parameters = const <String>[]; |
844 if (contextClass != null) { | 844 if (contextClass != null) { |
845 parameters = _elementEnvironment | 845 parameters = _elementEnvironment |
846 .getThisType(contextClass) | 846 .getThisType(contextClass) |
847 .typeArguments | 847 .typeArguments |
848 .map((TypeVariableType type) { | 848 .map((DartType _type) { |
| 849 TypeVariableType type = _type; |
849 return type.element.name; | 850 return type.element.name; |
850 }).toList(); | 851 }).toList(); |
851 } | 852 } |
852 return js('function(#) { return # }', [parameters, encoding]); | 853 return js('function(#) { return # }', [parameters, encoding]); |
853 } | 854 } |
854 } | 855 } |
855 | 856 |
856 @override | 857 @override |
857 jsAst.Expression getSignatureEncoding( | 858 jsAst.Expression getSignatureEncoding( |
858 Emitter emitter, DartType type, jsAst.Expression this_) { | 859 Emitter emitter, DartType type, jsAst.Expression this_) { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
944 // TODO(sra): Since the result string is used only in constructing constant | 945 // TODO(sra): Since the result string is used only in constructing constant |
945 // names, it would result in more readable names if the final string was a | 946 // names, it would result in more readable names if the final string was a |
946 // legal JavaScript identifer. | 947 // legal JavaScript identifer. |
947 if (interface.typeArguments.isEmpty) return name; | 948 if (interface.typeArguments.isEmpty) return name; |
948 String arguments = | 949 String arguments = |
949 new List.filled(interface.typeArguments.length, 'dynamic').join(', '); | 950 new List.filled(interface.typeArguments.length, 'dynamic').join(', '); |
950 return '$name<$arguments>'; | 951 return '$name<$arguments>'; |
951 } | 952 } |
952 } | 953 } |
953 | 954 |
954 class TypeRepresentationGenerator implements ResolutionDartTypeVisitor { | 955 class TypeRepresentationGenerator |
| 956 implements ResolutionDartTypeVisitor<dynamic, Emitter> { |
955 final Namer namer; | 957 final Namer namer; |
956 OnVariableCallback onVariable; | 958 OnVariableCallback onVariable; |
957 ShouldEncodeTypedefCallback shouldEncodeTypedef; | 959 ShouldEncodeTypedefCallback shouldEncodeTypedef; |
958 Map<TypeVariableType, jsAst.Expression> typedefBindings; | 960 Map<TypeVariableType, jsAst.Expression> typedefBindings; |
959 | 961 |
960 TypeRepresentationGenerator(this.namer); | 962 TypeRepresentationGenerator(this.namer); |
961 | 963 |
962 /** | 964 /** |
963 * Creates a type representation for [type]. [onVariable] is called to provide | 965 * Creates a type representation for [type]. [onVariable] is called to provide |
964 * the type representation for type variables. | 966 * the type representation for type variables. |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1172 StringBuffer sb = new StringBuffer(); | 1174 StringBuffer sb = new StringBuffer(); |
1173 for (ClassEntity holder in classes) { | 1175 for (ClassEntity holder in classes) { |
1174 for (ClassEntity check in [holder]) { | 1176 for (ClassEntity check in [holder]) { |
1175 sb.write('${holder.name}.' '${check.name}, '); | 1177 sb.write('${holder.name}.' '${check.name}, '); |
1176 } | 1178 } |
1177 } | 1179 } |
1178 return '[$sb]'; | 1180 return '[$sb]'; |
1179 } | 1181 } |
1180 } | 1182 } |
1181 | 1183 |
1182 class ArgumentCollector extends ResolutionDartTypeVisitor { | 1184 class ArgumentCollector extends ResolutionDartTypeVisitor<dynamic, bool> { |
1183 final Set<ClassEntity> classes = new Set<ClassEntity>(); | 1185 final Set<ClassEntity> classes = new Set<ClassEntity>(); |
1184 | 1186 |
1185 collect(DartType type, {bool isTypeArgument: false}) { | 1187 collect(DartType type, {bool isTypeArgument: false}) { |
1186 visit(type, isTypeArgument); | 1188 visit(type, isTypeArgument); |
1187 } | 1189 } |
1188 | 1190 |
1189 /// Collect all types in the list as if they were arguments of an | 1191 /// Collect all types in the list as if they were arguments of an |
1190 /// InterfaceType. | 1192 /// InterfaceType. |
1191 collectAll(List<DartType> types, {bool isTypeArgument: false}) { | 1193 collectAll(List<DartType> types, {bool isTypeArgument: false}) { |
1192 for (DartType type in types) { | 1194 for (DartType type in types) { |
(...skipping 11 matching lines...) Expand all Loading... |
1204 } | 1206 } |
1205 | 1207 |
1206 visitFunctionType(FunctionType type, _) { | 1208 visitFunctionType(FunctionType type, _) { |
1207 collect(type.returnType, isTypeArgument: true); | 1209 collect(type.returnType, isTypeArgument: true); |
1208 collectAll(type.parameterTypes, isTypeArgument: true); | 1210 collectAll(type.parameterTypes, isTypeArgument: true); |
1209 collectAll(type.optionalParameterTypes, isTypeArgument: true); | 1211 collectAll(type.optionalParameterTypes, isTypeArgument: true); |
1210 collectAll(type.namedParameterTypes, isTypeArgument: true); | 1212 collectAll(type.namedParameterTypes, isTypeArgument: true); |
1211 } | 1213 } |
1212 } | 1214 } |
1213 | 1215 |
1214 class FunctionArgumentCollector extends ResolutionDartTypeVisitor { | 1216 class FunctionArgumentCollector |
| 1217 extends ResolutionDartTypeVisitor<dynamic, bool> { |
1215 final Set<ClassEntity> classes = new Set<ClassEntity>(); | 1218 final Set<ClassEntity> classes = new Set<ClassEntity>(); |
1216 | 1219 |
1217 FunctionArgumentCollector(); | 1220 FunctionArgumentCollector(); |
1218 | 1221 |
1219 collect(DartType type, {bool inFunctionType: false}) { | 1222 collect(DartType type, {bool inFunctionType: false}) { |
1220 visit(type, inFunctionType); | 1223 visit(type, inFunctionType); |
1221 } | 1224 } |
1222 | 1225 |
1223 collectAll(List<DartType> types, {bool inFunctionType: false}) { | 1226 collectAll(List<DartType> types, {bool inFunctionType: false}) { |
1224 for (DartType type in types) { | 1227 for (DartType type in types) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1272 * substition for this check. | 1275 * substition for this check. |
1273 */ | 1276 */ |
1274 class TypeCheck { | 1277 class TypeCheck { |
1275 final ClassEntity cls; | 1278 final ClassEntity cls; |
1276 final Substitution substitution; | 1279 final Substitution substitution; |
1277 final int hashCode = _nextHash = (_nextHash + 100003).toUnsigned(30); | 1280 final int hashCode = _nextHash = (_nextHash + 100003).toUnsigned(30); |
1278 static int _nextHash = 0; | 1281 static int _nextHash = 0; |
1279 | 1282 |
1280 TypeCheck(this.cls, this.substitution); | 1283 TypeCheck(this.cls, this.substitution); |
1281 } | 1284 } |
OLD | NEW |