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 |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 final Map<Entity, jsAst.Name> userGlobals = new HashMap<Entity, jsAst.Name>(); | 520 final Map<Entity, jsAst.Name> userGlobals = new HashMap<Entity, jsAst.Name>(); |
521 final Map<String, jsAst.Name> internalGlobals = | 521 final Map<String, jsAst.Name> internalGlobals = |
522 new HashMap<String, jsAst.Name>(); | 522 new HashMap<String, jsAst.Name>(); |
523 | 523 |
524 /// Used disambiguated names in the instance namespace, issued by | 524 /// Used disambiguated names in the instance namespace, issued by |
525 /// [_disambiguateMember], [_disambiguateInternalMember], | 525 /// [_disambiguateMember], [_disambiguateInternalMember], |
526 /// [_disambiguateOperator], and [reservePublicMemberName]. | 526 /// [_disambiguateOperator], and [reservePublicMemberName]. |
527 final NamingScope instanceScope = new NamingScope(); | 527 final NamingScope instanceScope = new NamingScope(); |
528 final Map<String, jsAst.Name> userInstanceMembers = | 528 final Map<String, jsAst.Name> userInstanceMembers = |
529 new HashMap<String, jsAst.Name>(); | 529 new HashMap<String, jsAst.Name>(); |
530 final Map<Element, jsAst.Name> internalInstanceMembers = | 530 final Map<MemberEntity, jsAst.Name> internalInstanceMembers = |
531 new HashMap<Element, jsAst.Name>(); | 531 new HashMap<MemberEntity, jsAst.Name>(); |
532 final Map<String, jsAst.Name> userInstanceOperators = | 532 final Map<String, jsAst.Name> userInstanceOperators = |
533 new HashMap<String, jsAst.Name>(); | 533 new HashMap<String, jsAst.Name>(); |
534 | 534 |
535 /// Used to disambiguate names for constants in [constantName]. | 535 /// Used to disambiguate names for constants in [constantName]. |
536 final NamingScope constantScope = new NamingScope(); | 536 final NamingScope constantScope = new NamingScope(); |
537 | 537 |
538 /// Used to store scopes for instances of [PrivatelyNamedJsEntity] | 538 /// Used to store scopes for instances of [PrivatelyNamedJsEntity] |
539 final Map<Entity, NamingScope> _privateNamingScopes = | 539 final Map<Entity, NamingScope> _privateNamingScopes = |
540 new Map<Entity, NamingScope>(); | 540 new Map<Entity, NamingScope>(); |
541 | 541 |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 String _proposeNameForConstructorBody(ConstructorBodyElement method) { | 762 String _proposeNameForConstructorBody(ConstructorBodyElement method) { |
763 String name = Elements.reconstructConstructorNameSourceString(method); | 763 String name = Elements.reconstructConstructorNameSourceString(method); |
764 // We include the method suffix on constructor bodies. It has no purpose, | 764 // We include the method suffix on constructor bodies. It has no purpose, |
765 // but this way it produces the same names as previous versions of the | 765 // but this way it produces the same names as previous versions of the |
766 // Namer class did. | 766 // Namer class did. |
767 List<String> suffix = callSuffixForSignature(method.functionSignature); | 767 List<String> suffix = callSuffixForSignature(method.functionSignature); |
768 return '$name\$${suffix.join(r'$')}'; | 768 return '$name\$${suffix.join(r'$')}'; |
769 } | 769 } |
770 | 770 |
771 /// Name for a constructor body. | 771 /// Name for a constructor body. |
772 jsAst.Name constructorBodyName(FunctionElement ctor) { | 772 jsAst.Name constructorBodyName(ConstructorBodyElement ctor) { |
773 return _disambiguateInternalMember( | 773 return _disambiguateInternalMember( |
774 ctor, () => _proposeNameForConstructorBody(ctor)); | 774 ctor, () => _proposeNameForConstructorBody(ctor)); |
775 } | 775 } |
776 | 776 |
777 /// Annotated name for [method] encoding arity and named parameters. | 777 /// Annotated name for [method] encoding arity and named parameters. |
778 jsAst.Name instanceMethodName(FunctionEntity method) { | 778 jsAst.Name instanceMethodName(FunctionEntity method) { |
779 // TODO(johnniwinther): Avoid the use of [ConstructorBodyElement]. The | 779 // TODO(johnniwinther): Avoid the use of [ConstructorBodyElement]. The |
780 // codegen model should be explicit about its constructor body elements. | 780 // codegen model should be explicit about its constructor body elements. |
781 if (method is ConstructorBodyElement) { | 781 if (method is ConstructorBodyElement) { |
782 return constructorBodyName(method); | 782 return constructorBodyName(method); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 } | 977 } |
978 | 978 |
979 /// Annotated name for the setter of any member with [disambiguatedName]. | 979 /// Annotated name for the setter of any member with [disambiguatedName]. |
980 jsAst.Name deriveGetterName(jsAst.Name disambiguatedName) { | 980 jsAst.Name deriveGetterName(jsAst.Name disambiguatedName) { |
981 // We dynamically create getters from the field-name. The getter name must | 981 // We dynamically create getters from the field-name. The getter name must |
982 // therefore be derived from the instance field-name. | 982 // therefore be derived from the instance field-name. |
983 return new GetterName(_literalGetterPrefix, disambiguatedName); | 983 return new GetterName(_literalGetterPrefix, disambiguatedName); |
984 } | 984 } |
985 | 985 |
986 /// Annotated name for the getter of [element]. | 986 /// Annotated name for the getter of [element]. |
987 jsAst.Name getterForElement(MemberElement element) { | 987 jsAst.Name getterForElement(MemberEntity element) { |
988 // We dynamically create getters from the field-name. The getter name must | 988 // We dynamically create getters from the field-name. The getter name must |
989 // therefore be derived from the instance field-name. | 989 // therefore be derived from the instance field-name. |
990 jsAst.Name name = _disambiguateMember(element.memberName); | 990 jsAst.Name name = _disambiguateMember( |
| 991 new Name(element.name, element.library, isSetter: element.isSetter)); |
991 return deriveGetterName(name); | 992 return deriveGetterName(name); |
992 } | 993 } |
993 | 994 |
994 /// Property name for the getter of an instance member with [originalName]. | 995 /// Property name for the getter of an instance member with [originalName]. |
995 jsAst.Name getterForMember(Name originalName) { | 996 jsAst.Name getterForMember(Name originalName) { |
996 jsAst.Name disambiguatedName = _disambiguateMember(originalName); | 997 jsAst.Name disambiguatedName = _disambiguateMember(originalName); |
997 return deriveGetterName(disambiguatedName); | 998 return deriveGetterName(disambiguatedName); |
998 } | 999 } |
999 | 1000 |
1000 /// Disambiguated name for a compiler-owned global variable. | 1001 /// Disambiguated name for a compiler-owned global variable. |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1151 instanceScope.registerUse(disambiguatedName); | 1152 instanceScope.registerUse(disambiguatedName); |
1152 } | 1153 } |
1153 | 1154 |
1154 /// Disambiguated name unique to [element]. | 1155 /// Disambiguated name unique to [element]. |
1155 /// | 1156 /// |
1156 /// This is used as the property name for fields, type variables, | 1157 /// This is used as the property name for fields, type variables, |
1157 /// constructor bodies, and super-accessors. | 1158 /// constructor bodies, and super-accessors. |
1158 /// | 1159 /// |
1159 /// The resulting name is unique within the instance-member namespace. | 1160 /// The resulting name is unique within the instance-member namespace. |
1160 jsAst.Name _disambiguateInternalMember( | 1161 jsAst.Name _disambiguateInternalMember( |
1161 Element element, String proposeName()) { | 1162 MemberEntity element, String proposeName()) { |
1162 jsAst.Name newName = internalInstanceMembers[element]; | 1163 jsAst.Name newName = internalInstanceMembers[element]; |
1163 if (newName == null) { | 1164 if (newName == null) { |
1164 String name = proposeName(); | 1165 String name = proposeName(); |
1165 | 1166 |
1166 Entity asEntity = element; | 1167 if (element is PrivatelyNamedJSEntity) { |
1167 if (asEntity is PrivatelyNamedJSEntity) { | 1168 NamingScope scope = _getPrivateScopeFor(element); |
1168 NamingScope scope = _getPrivateScopeFor(asEntity); | |
1169 newName = getFreshName(scope, name, | 1169 newName = getFreshName(scope, name, |
1170 sanitizeForAnnotations: true, sanitizeForNatives: false); | 1170 sanitizeForAnnotations: true, sanitizeForNatives: false); |
1171 internalInstanceMembers[element] = newName; | 1171 internalInstanceMembers[element] = newName; |
1172 } else { | 1172 } else { |
1173 bool mayClashNative = | 1173 bool mayClashNative = |
1174 _isUserClassExtendingNative(element.enclosingClass); | 1174 _isUserClassExtendingNative(element.enclosingClass); |
1175 newName = getFreshName(instanceScope, name, | 1175 newName = getFreshName(instanceScope, name, |
1176 sanitizeForAnnotations: true, sanitizeForNatives: mayClashNative); | 1176 sanitizeForAnnotations: true, sanitizeForNatives: mayClashNative); |
1177 internalInstanceMembers[element] = newName; | 1177 internalInstanceMembers[element] = newName; |
1178 } | 1178 } |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1461 /// | 1461 /// |
1462 /// Example translation to JS: | 1462 /// Example translation to JS: |
1463 /// | 1463 /// |
1464 /// A.prototype.super$A$foo = function() {...} | 1464 /// A.prototype.super$A$foo = function() {...} |
1465 /// A.prototype.foo$0 = A.prototype.super$A$foo | 1465 /// A.prototype.foo$0 = A.prototype.super$A$foo |
1466 /// | 1466 /// |
1467 /// B.prototype.foo$0 = function() { | 1467 /// B.prototype.foo$0 = function() { |
1468 /// this.super$A$foo(); // super.foo() | 1468 /// this.super$A$foo(); // super.foo() |
1469 /// } | 1469 /// } |
1470 /// | 1470 /// |
1471 jsAst.Name aliasedSuperMemberPropertyName(MemberElement member) { | 1471 jsAst.Name aliasedSuperMemberPropertyName(MemberEntity member) { |
1472 assert(!member.isField); // Fields do not need super aliases. | 1472 assert(!member.isField); // Fields do not need super aliases. |
1473 return _disambiguateInternalMember(member, () { | 1473 return _disambiguateInternalMember(member, () { |
1474 String invocationName = operatorNameToIdentifier(member.name); | 1474 String invocationName = operatorNameToIdentifier(member.name); |
1475 return "super\$${member.enclosingClass.name}\$$invocationName"; | 1475 return "super\$${member.enclosingClass.name}\$$invocationName"; |
1476 }); | 1476 }); |
1477 } | 1477 } |
1478 | 1478 |
1479 /// Property name in which to store the given static or instance [method]. | 1479 /// Property name in which to store the given static or instance [method]. |
1480 /// For instance methods, this includes the suffix encoding arity and named | 1480 /// For instance methods, this includes the suffix encoding arity and named |
1481 /// parameters. | 1481 /// parameters. |
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2232 void addSuggestion(String original, String suggestion) { | 2232 void addSuggestion(String original, String suggestion) { |
2233 assert(!_suggestedNames.containsKey(original)); | 2233 assert(!_suggestedNames.containsKey(original)); |
2234 _suggestedNames[original] = suggestion; | 2234 _suggestedNames[original] = suggestion; |
2235 } | 2235 } |
2236 | 2236 |
2237 bool hasSuggestion(String original) => _suggestedNames.containsKey(original); | 2237 bool hasSuggestion(String original) => _suggestedNames.containsKey(original); |
2238 bool isSuggestion(String candidate) { | 2238 bool isSuggestion(String candidate) { |
2239 return _suggestedNames.containsValue(candidate); | 2239 return _suggestedNames.containsValue(candidate); |
2240 } | 2240 } |
2241 } | 2241 } |
OLD | NEW |