| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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.namer; | 5 library js_backend.namer; |
| 6 | 6 |
| 7 import 'dart:collection' show HashMap; | 7 import 'dart:collection' show HashMap; |
| 8 | 8 |
| 9 import 'package:js_runtime/shared/embedded_names.dart' show JsGetName; | 9 import 'package:js_runtime/shared/embedded_names.dart' show JsGetName; |
| 10 | 10 |
| 11 import '../closure.dart'; | 11 import '../closure.dart'; |
| 12 import '../common.dart'; | 12 import '../common.dart'; |
| 13 import '../common/names.dart' show Identifiers, Selectors; | 13 import '../common/names.dart' show Identifiers, Selectors; |
| 14 import '../compiler.dart' show Compiler; | 14 import '../compiler.dart' show Compiler; |
| 15 import '../constants/values.dart'; | 15 import '../constants/values.dart'; |
| 16 import '../core_types.dart' show CoreClasses; | 16 import '../core_types.dart' show CoreClasses; |
| 17 import '../dart_types.dart'; | 17 import '../dart_types.dart'; |
| 18 import '../diagnostics/invariant.dart' show DEBUG_MODE; | 18 import '../diagnostics/invariant.dart' show DEBUG_MODE; |
| 19 import '../elements/elements.dart'; | 19 import '../elements/elements.dart'; |
| 20 import '../elements/entities.dart'; |
| 20 import '../js/js.dart' as jsAst; | 21 import '../js/js.dart' as jsAst; |
| 21 import '../js/js.dart' show js; | 22 import '../js/js.dart' show js; |
| 22 import '../tree/tree.dart'; | 23 import '../tree/tree.dart'; |
| 23 import '../universe/call_structure.dart' show CallStructure; | 24 import '../universe/call_structure.dart' show CallStructure; |
| 24 import '../universe/selector.dart' show Selector, SelectorKind; | 25 import '../universe/selector.dart' show Selector, SelectorKind; |
| 25 import '../util/characters.dart'; | 26 import '../util/characters.dart'; |
| 26 import '../util/util.dart'; | 27 import '../util/util.dart'; |
| 27 import '../world.dart' show ClosedWorld; | 28 import '../world.dart' show ClosedWorld; |
| 28 import 'backend.dart'; | 29 import 'backend.dart'; |
| 29 import 'backend_helpers.dart'; | 30 import 'backend_helpers.dart'; |
| (...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 return '$name\$${suffix.join(r'$')}'; | 661 return '$name\$${suffix.join(r'$')}'; |
| 661 } | 662 } |
| 662 | 663 |
| 663 /// Name for a constructor body. | 664 /// Name for a constructor body. |
| 664 jsAst.Name constructorBodyName(FunctionElement ctor) { | 665 jsAst.Name constructorBodyName(FunctionElement ctor) { |
| 665 return _disambiguateInternalMember( | 666 return _disambiguateInternalMember( |
| 666 ctor, () => _proposeNameForConstructorBody(ctor)); | 667 ctor, () => _proposeNameForConstructorBody(ctor)); |
| 667 } | 668 } |
| 668 | 669 |
| 669 /// Annotated name for [method] encoding arity and named parameters. | 670 /// Annotated name for [method] encoding arity and named parameters. |
| 670 jsAst.Name instanceMethodName(FunctionElement method) { | 671 jsAst.Name instanceMethodName(MethodElement method) { |
| 671 if (method.isGenerativeConstructorBody) { | 672 if (method.isGenerativeConstructorBody) { |
| 672 return constructorBodyName(method); | 673 return constructorBodyName(method); |
| 673 } | 674 } |
| 674 return invocationName(new Selector.fromElement(method)); | 675 return invocationName(new Selector.fromElement(method)); |
| 675 } | 676 } |
| 676 | 677 |
| 677 String _jsNameHelper(Element e) { | 678 String _jsNameHelper(Element e) { |
| 678 String jsInteropName = backend.nativeData.getJsInteropName(e); | 679 String jsInteropName = backend.nativeData.getJsInteropName(e); |
| 679 if (jsInteropName != null && jsInteropName.isNotEmpty) return jsInteropName; | 680 if (jsInteropName != null && jsInteropName.isNotEmpty) return jsInteropName; |
| 680 return e.isLibrary | 681 return e.isLibrary |
| (...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1243 // However, as names from the same libary should have the same library | 1244 // However, as names from the same libary should have the same library |
| 1244 // name part, we disambiguate the library name here. | 1245 // name part, we disambiguate the library name here. |
| 1245 String disambiguated = name; | 1246 String disambiguated = name; |
| 1246 for (int c = 0; libraryLongNames.containsValue(disambiguated); c++) { | 1247 for (int c = 0; libraryLongNames.containsValue(disambiguated); c++) { |
| 1247 disambiguated = "$name$c"; | 1248 disambiguated = "$name$c"; |
| 1248 } | 1249 } |
| 1249 libraryLongNames[library] = disambiguated; | 1250 libraryLongNames[library] = disambiguated; |
| 1250 return disambiguated; | 1251 return disambiguated; |
| 1251 } | 1252 } |
| 1252 | 1253 |
| 1253 String suffixForGetInterceptor(Iterable<ClassElement> classes) { | 1254 String suffixForGetInterceptor(Iterable<ClassEntity> classes) { |
| 1254 String abbreviate(ClassElement cls) { | 1255 String abbreviate(ClassElement cls) { |
| 1255 if (cls == coreClasses.objectClass) return "o"; | 1256 if (cls == coreClasses.objectClass) return "o"; |
| 1256 if (cls == helpers.jsStringClass) return "s"; | 1257 if (cls == helpers.jsStringClass) return "s"; |
| 1257 if (cls == helpers.jsArrayClass) return "a"; | 1258 if (cls == helpers.jsArrayClass) return "a"; |
| 1258 if (cls == helpers.jsDoubleClass) return "d"; | 1259 if (cls == helpers.jsDoubleClass) return "d"; |
| 1259 if (cls == helpers.jsIntClass) return "i"; | 1260 if (cls == helpers.jsIntClass) return "i"; |
| 1260 if (cls == helpers.jsNumberClass) return "n"; | 1261 if (cls == helpers.jsNumberClass) return "n"; |
| 1261 if (cls == helpers.jsNullClass) return "u"; | 1262 if (cls == helpers.jsNullClass) return "u"; |
| 1262 if (cls == helpers.jsBoolClass) return "b"; | 1263 if (cls == helpers.jsBoolClass) return "b"; |
| 1263 if (cls == helpers.jsInterceptorClass) return "I"; | 1264 if (cls == helpers.jsInterceptorClass) return "I"; |
| 1264 return cls.name; | 1265 return cls.name; |
| 1265 } | 1266 } |
| 1266 | 1267 |
| 1267 List<String> names = classes | 1268 List<String> names = classes |
| 1268 .where((cls) => !backend.isNativeOrExtendsNative(cls)) | 1269 .where((cls) => !backend.isNativeOrExtendsNative(cls)) |
| 1269 .map(abbreviate) | 1270 .map(abbreviate) |
| 1270 .toList(); | 1271 .toList(); |
| 1271 // There is one dispatch mechanism for all native classes. | 1272 // There is one dispatch mechanism for all native classes. |
| 1272 if (classes.any((cls) => backend.isNativeOrExtendsNative(cls))) { | 1273 if (classes.any((cls) => backend.isNativeOrExtendsNative(cls))) { |
| 1273 names.add("x"); | 1274 names.add("x"); |
| 1274 } | 1275 } |
| 1275 // Sort the names of the classes after abbreviating them to ensure | 1276 // Sort the names of the classes after abbreviating them to ensure |
| 1276 // the suffix is stable and predictable for the suggested names. | 1277 // the suffix is stable and predictable for the suggested names. |
| 1277 names.sort(); | 1278 names.sort(); |
| 1278 return names.join(); | 1279 return names.join(); |
| 1279 } | 1280 } |
| 1280 | 1281 |
| 1281 /// Property name used for `getInterceptor` or one of its specializations. | 1282 /// Property name used for `getInterceptor` or one of its specializations. |
| 1282 jsAst.Name nameForGetInterceptor(Iterable<ClassElement> classes) { | 1283 jsAst.Name nameForGetInterceptor(Iterable<ClassEntity> classes) { |
| 1283 FunctionElement getInterceptor = helpers.getInterceptorMethod; | 1284 FunctionElement getInterceptor = helpers.getInterceptorMethod; |
| 1284 if (classes.contains(helpers.jsInterceptorClass)) { | 1285 if (classes.contains(helpers.jsInterceptorClass)) { |
| 1285 // If the base Interceptor class is in the set of intercepted classes, we | 1286 // If the base Interceptor class is in the set of intercepted classes, we |
| 1286 // need to go through the generic getInterceptorMethod, since any subclass | 1287 // need to go through the generic getInterceptorMethod, since any subclass |
| 1287 // of the base Interceptor could match. | 1288 // of the base Interceptor could match. |
| 1288 // The unspecialized getInterceptor method can also be accessed through | 1289 // The unspecialized getInterceptor method can also be accessed through |
| 1289 // its element, so we treat this as a user-space global instead of an | 1290 // its element, so we treat this as a user-space global instead of an |
| 1290 // internal global. | 1291 // internal global. |
| 1291 return _disambiguateGlobal(getInterceptor); | 1292 return _disambiguateGlobal(getInterceptor); |
| 1292 } | 1293 } |
| (...skipping 807 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2100 void addSuggestion(String original, String suggestion) { | 2101 void addSuggestion(String original, String suggestion) { |
| 2101 assert(!_suggestedNames.containsKey(original)); | 2102 assert(!_suggestedNames.containsKey(original)); |
| 2102 _suggestedNames[original] = suggestion; | 2103 _suggestedNames[original] = suggestion; |
| 2103 } | 2104 } |
| 2104 | 2105 |
| 2105 bool hasSuggestion(String original) => _suggestedNames.containsKey(original); | 2106 bool hasSuggestion(String original) => _suggestedNames.containsKey(original); |
| 2106 bool isSuggestion(String candidate) { | 2107 bool isSuggestion(String candidate) { |
| 2107 return _suggestedNames.containsValue(candidate); | 2108 return _suggestedNames.containsValue(candidate); |
| 2108 } | 2109 } |
| 2109 } | 2110 } |
| OLD | NEW |