| 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 part of js_backend; | 5 part of js_backend; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. | 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. |
| 9 * | 9 * |
| 10 * Names are generated through three stages: | 10 * Names are generated through three stages: |
| (...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 | 630 |
| 631 /// Annotated name for [method] encoding arity and named parameters. | 631 /// Annotated name for [method] encoding arity and named parameters. |
| 632 jsAst.Name instanceMethodName(FunctionElement method) { | 632 jsAst.Name instanceMethodName(FunctionElement method) { |
| 633 if (method.isGenerativeConstructorBody) { | 633 if (method.isGenerativeConstructorBody) { |
| 634 return constructorBodyName(method); | 634 return constructorBodyName(method); |
| 635 } | 635 } |
| 636 return invocationName(new Selector.fromElement(method)); | 636 return invocationName(new Selector.fromElement(method)); |
| 637 } | 637 } |
| 638 | 638 |
| 639 String _jsNameHelper(Element e) { | 639 String _jsNameHelper(Element e) { |
| 640 if (e.jsInteropName != null && e.jsInteropName.isNotEmpty) | 640 String jsInteropName = backend.getJsInteropName(e); |
| 641 return e.jsInteropName; | 641 if (jsInteropName != null && jsInteropName.isNotEmpty) |
| 642 return jsInteropName; |
| 642 return e.isLibrary ? 'self' : e.name; | 643 return e.isLibrary ? 'self' : e.name; |
| 643 } | 644 } |
| 644 | 645 |
| 645 /// Returns a JavaScript path specifying the context in which | 646 /// Returns a JavaScript path specifying the context in which |
| 646 /// [element.fixedBackendName] should be evaluated. Only applicable for | 647 /// [element.fixedBackendName] should be evaluated. Only applicable for |
| 647 /// elements using typed JavaScript interop. | 648 /// elements using typed JavaScript interop. |
| 648 /// For example: fixedBackendPath for the static method createMap in the | 649 /// For example: fixedBackendPath for the static method createMap in the |
| 649 /// Map class of the goog.map JavaScript library would have path | 650 /// Map class of the goog.map JavaScript library would have path |
| 650 /// "goog.maps.Map". | 651 /// "goog.maps.Map". |
| 651 String fixedBackendPath(Element element) { | 652 String fixedBackendPath(Element element) { |
| 652 if (!element.isJsInterop) return null; | 653 if (!backend.isJsInterop(element)) return null; |
| 653 if (element.isInstanceMember) return 'this'; | 654 if (element.isInstanceMember) return 'this'; |
| 654 if (element.isConstructor) return fixedBackendPath(element.enclosingClass); | 655 if (element.isConstructor) return fixedBackendPath(element.enclosingClass); |
| 655 if (element.isLibrary) return 'self'; | 656 if (element.isLibrary) return 'self'; |
| 656 var sb = new StringBuffer(); | 657 var sb = new StringBuffer(); |
| 657 sb..write(_jsNameHelper(element.library)); | 658 sb..write(_jsNameHelper(element.library)); |
| 658 | 659 |
| 659 if (element.enclosingClass != null && element.enclosingClass != element) { | 660 if (element.enclosingClass != null && element.enclosingClass != element) { |
| 660 sb..write('.')..write(_jsNameHelper(element.enclosingClass)); | 661 sb..write('.')..write(_jsNameHelper(element.enclosingClass)); |
| 661 } | 662 } |
| 662 return sb.toString(); | 663 return sb.toString(); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 jsAst.Name globalPropertyName(Element element) { | 785 jsAst.Name globalPropertyName(Element element) { |
| 785 return _disambiguateGlobal(element); | 786 return _disambiguateGlobal(element); |
| 786 } | 787 } |
| 787 | 788 |
| 788 /** | 789 /** |
| 789 * Returns the JavaScript property name used to store an instance field. | 790 * Returns the JavaScript property name used to store an instance field. |
| 790 */ | 791 */ |
| 791 jsAst.Name instanceFieldPropertyName(FieldElement element) { | 792 jsAst.Name instanceFieldPropertyName(FieldElement element) { |
| 792 ClassElement enclosingClass = element.enclosingClass; | 793 ClassElement enclosingClass = element.enclosingClass; |
| 793 | 794 |
| 794 if (element.hasFixedBackendName) { | 795 if (backend.hasFixedBackendName(element)) { |
| 795 return new StringBackedName(element.fixedBackendName); | 796 return new StringBackedName(backend.getFixedBackendName(element)); |
| 796 } | 797 } |
| 797 | 798 |
| 798 // Instances of BoxFieldElement are special. They are already created with | 799 // Instances of BoxFieldElement are special. They are already created with |
| 799 // a unique and safe name. However, as boxes are not really instances of | 800 // a unique and safe name. However, as boxes are not really instances of |
| 800 // classes, the usual naming scheme that tries to avoid name clashes with | 801 // classes, the usual naming scheme that tries to avoid name clashes with |
| 801 // super classes does not apply. We still do not mark the name as a | 802 // super classes does not apply. We still do not mark the name as a |
| 802 // fixedBackendName, as we want to allow other namers to do something more | 803 // fixedBackendName, as we want to allow other namers to do something more |
| 803 // clever with them. | 804 // clever with them. |
| 804 if (element is BoxFieldElement) { | 805 if (element is BoxFieldElement) { |
| 805 return new StringBackedName(element.name); | 806 return new StringBackedName(element.name); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 825 // the field name would have to be mangled. | 826 // the field name would have to be mangled. |
| 826 return _disambiguateMember(element.memberName); | 827 return _disambiguateMember(element.memberName); |
| 827 } | 828 } |
| 828 | 829 |
| 829 bool _isShadowingSuperField(Element element) { | 830 bool _isShadowingSuperField(Element element) { |
| 830 return element.enclosingClass.hasFieldShadowedBy(element); | 831 return element.enclosingClass.hasFieldShadowedBy(element); |
| 831 } | 832 } |
| 832 | 833 |
| 833 /// True if [class_] is a non-native class that inherits from a native class. | 834 /// True if [class_] is a non-native class that inherits from a native class. |
| 834 bool _isUserClassExtendingNative(ClassElement class_) { | 835 bool _isUserClassExtendingNative(ClassElement class_) { |
| 835 return !class_.isNative && | 836 return !backend.isNative(class_) && |
| 836 Elements.isNativeOrExtendsNative(class_.superclass); | 837 backend.isNativeOrExtendsNative(class_.superclass); |
| 837 } | 838 } |
| 838 | 839 |
| 839 /// Annotated name for the setter of [element]. | 840 /// Annotated name for the setter of [element]. |
| 840 jsAst.Name setterForElement(MemberElement element) { | 841 jsAst.Name setterForElement(MemberElement element) { |
| 841 // We dynamically create setters from the field-name. The setter name must | 842 // We dynamically create setters from the field-name. The setter name must |
| 842 // therefore be derived from the instance field-name. | 843 // therefore be derived from the instance field-name. |
| 843 jsAst.Name name = _disambiguateMember(element.memberName); | 844 jsAst.Name name = _disambiguateMember(element.memberName); |
| 844 return deriveSetterName(name); | 845 return deriveSetterName(name); |
| 845 } | 846 } |
| 846 | 847 |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 if (cls == backend.jsArrayClass) return "a"; | 1210 if (cls == backend.jsArrayClass) return "a"; |
| 1210 if (cls == backend.jsDoubleClass) return "d"; | 1211 if (cls == backend.jsDoubleClass) return "d"; |
| 1211 if (cls == backend.jsIntClass) return "i"; | 1212 if (cls == backend.jsIntClass) return "i"; |
| 1212 if (cls == backend.jsNumberClass) return "n"; | 1213 if (cls == backend.jsNumberClass) return "n"; |
| 1213 if (cls == backend.jsNullClass) return "u"; | 1214 if (cls == backend.jsNullClass) return "u"; |
| 1214 if (cls == backend.jsBoolClass) return "b"; | 1215 if (cls == backend.jsBoolClass) return "b"; |
| 1215 if (cls == backend.jsInterceptorClass) return "I"; | 1216 if (cls == backend.jsInterceptorClass) return "I"; |
| 1216 return cls.name; | 1217 return cls.name; |
| 1217 } | 1218 } |
| 1218 List<String> names = classes | 1219 List<String> names = classes |
| 1219 .where((cls) => !Elements.isNativeOrExtendsNative(cls)) | 1220 .where((cls) => !backend.isNativeOrExtendsNative(cls)) |
| 1220 .map(abbreviate) | 1221 .map(abbreviate) |
| 1221 .toList(); | 1222 .toList(); |
| 1222 // There is one dispatch mechanism for all native classes. | 1223 // There is one dispatch mechanism for all native classes. |
| 1223 if (classes.any((cls) => Elements.isNativeOrExtendsNative(cls))) { | 1224 if (classes.any((cls) => backend.isNativeOrExtendsNative(cls))) { |
| 1224 names.add("x"); | 1225 names.add("x"); |
| 1225 } | 1226 } |
| 1226 // Sort the names of the classes after abbreviating them to ensure | 1227 // Sort the names of the classes after abbreviating them to ensure |
| 1227 // the suffix is stable and predictable for the suggested names. | 1228 // the suffix is stable and predictable for the suggested names. |
| 1228 names.sort(); | 1229 names.sort(); |
| 1229 return names.join(); | 1230 return names.join(); |
| 1230 } | 1231 } |
| 1231 | 1232 |
| 1232 /// Property name used for `getInterceptor` or one of its specializations. | 1233 /// Property name used for `getInterceptor` or one of its specializations. |
| 1233 jsAst.Name nameForGetInterceptor(Iterable<ClassElement> classes) { | 1234 jsAst.Name nameForGetInterceptor(Iterable<ClassElement> classes) { |
| (...skipping 788 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2022 } | 2023 } |
| 2023 } | 2024 } |
| 2024 } | 2025 } |
| 2025 } | 2026 } |
| 2026 | 2027 |
| 2027 enum NamingScope { | 2028 enum NamingScope { |
| 2028 global, | 2029 global, |
| 2029 instance, | 2030 instance, |
| 2030 constant | 2031 constant |
| 2031 } | 2032 } |
| OLD | NEW |