Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: pkg/compiler/lib/src/js_backend/namer.dart

Issue 2929143002: Handle instance fields in compile_from_dill_test (Closed)
Patch Set: Cleanup Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 850 matching lines...) Expand 10 before | Expand all | Expand 10 after
861 /** 861 /**
862 * Returns the internal name used for an invocation mirror of this selector. 862 * Returns the internal name used for an invocation mirror of this selector.
863 */ 863 */
864 jsAst.Name invocationMirrorInternalName(Selector selector) => 864 jsAst.Name invocationMirrorInternalName(Selector selector) =>
865 invocationName(selector); 865 invocationName(selector);
866 866
867 /** 867 /**
868 * Returns the disambiguated name for the given field, used for constructing 868 * Returns the disambiguated name for the given field, used for constructing
869 * the getter and setter names. 869 * the getter and setter names.
870 */ 870 */
871 jsAst.Name fieldAccessorName(FieldElement element) { 871 jsAst.Name fieldAccessorName(FieldEntity element) {
872 return element.isInstanceMember 872 return element.isInstanceMember
873 ? _disambiguateMember(element.memberName) 873 ? _disambiguateMember(element.memberName)
874 : _disambiguateGlobalMember(element); 874 : _disambiguateGlobalMember(element);
875 } 875 }
876 876
877 /** 877 /**
878 * Returns name of the JavaScript property used to store a static or instance 878 * Returns name of the JavaScript property used to store a static or instance
879 * field. 879 * field.
880 */ 880 */
881 jsAst.Name fieldPropertyName(FieldElement element) { 881 jsAst.Name fieldPropertyName(FieldEntity element) {
882 return element.isInstanceMember 882 return element.isInstanceMember
883 ? instanceFieldPropertyName(element) 883 ? instanceFieldPropertyName(element)
884 : _disambiguateGlobalMember(element); 884 : _disambiguateGlobalMember(element);
885 } 885 }
886 886
887 /// Returns a JavaScript property name used to store the member [element] on 887 /// Returns a JavaScript property name used to store the member [element] on
888 /// one of the global objects. 888 /// one of the global objects.
889 /// 889 ///
890 /// Should be used together with [globalObjectForMember], which denotes the 890 /// Should be used together with [globalObjectForMember], which denotes the
891 /// object on which the returned property name should be used. 891 /// object on which the returned property name should be used.
(...skipping 12 matching lines...) Expand all
904 /// [element] on one of the global objects. 904 /// [element] on one of the global objects.
905 /// 905 ///
906 /// Should be used together with [globalObjectForType], which denotes the 906 /// Should be used together with [globalObjectForType], which denotes the
907 /// object on which the returned property name should be used. 907 /// object on which the returned property name should be used.
908 jsAst.Name globalPropertyNameForType(TypeDeclarationElement element) => 908 jsAst.Name globalPropertyNameForType(TypeDeclarationElement element) =>
909 _disambiguateGlobalType(element); 909 _disambiguateGlobalType(element);
910 910
911 /** 911 /**
912 * Returns the JavaScript property name used to store an instance field. 912 * Returns the JavaScript property name used to store an instance field.
913 */ 913 */
914 jsAst.Name instanceFieldPropertyName(FieldElement element) { 914 jsAst.Name instanceFieldPropertyName(FieldEntity element) {
915 ClassElement enclosingClass = element.enclosingClass; 915 ClassEntity enclosingClass = element.enclosingClass;
916 916
917 if (_nativeData.hasFixedBackendName(element)) { 917 if (_nativeData.hasFixedBackendName(element)) {
918 return new StringBackedName(_nativeData.getFixedBackendName(element)); 918 return new StringBackedName(_nativeData.getFixedBackendName(element));
919 } 919 }
920 920
921 // Some elements, like e.g. instances of BoxFieldElement are special. 921 // Some elements, like e.g. instances of BoxFieldElement are special.
922 // They are created with a unique and safe name for the element model. 922 // They are created with a unique and safe name for the element model.
923 // While their name is unique, it is not very readable. So we try to 923 // While their name is unique, it is not very readable. So we try to
924 // preserve the original, proposed name. 924 // preserve the original, proposed name.
925 // However, as boxes are not really instances of classes, the usual naming 925 // However, as boxes are not really instances of classes, the usual naming
926 // scheme that tries to avoid name clashes with super classes does not 926 // scheme that tries to avoid name clashes with super classes does not
927 // apply. So we can directly grab a name. 927 // apply. So we can directly grab a name.
928 Entity asEntity = element; 928 if (element is JSEntity) {
929 if (asEntity is JSEntity) { 929 var jsEntity = element;
930 return _disambiguateInternalMember( 930 return _disambiguateInternalMember(
931 element, () => asEntity.declaredEntity.name); 931 jsEntity,
932 // ignore: UNDEFINED_GETTER
933 () => jsEntity.declaredEntity.name);
932 } 934 }
933 935
934 // If the name of the field might clash with another field, 936 // If the name of the field might clash with another field,
935 // use a mangled field name to avoid potential clashes. 937 // use a mangled field name to avoid potential clashes.
936 // Note that if the class extends a native class, that native class might 938 // Note that if the class extends a native class, that native class might
937 // have fields with fixed backend names, so we assume the worst and always 939 // have fields with fixed backend names, so we assume the worst and always
938 // mangle the field names of classes extending native classes. 940 // mangle the field names of classes extending native classes.
939 // Methods on such classes are stored on the interceptor, not the instance, 941 // Methods on such classes are stored on the interceptor, not the instance,
940 // so only fields have the potential to clash with a native property name. 942 // so only fields have the potential to clash with a native property name.
941 if (_closedWorld.isUsedAsMixin(enclosingClass) || 943 if (_closedWorld.isUsedAsMixin(enclosingClass) ||
942 _isShadowingSuperField(element) || 944 _isShadowingSuperField(element) ||
943 _isUserClassExtendingNative(enclosingClass)) { 945 _isUserClassExtendingNative(enclosingClass)) {
944 String proposeName() => '${enclosingClass.name}_${element.name}'; 946 String proposeName() => '${enclosingClass.name}_${element.name}';
945 return _disambiguateInternalMember(element, proposeName); 947 return _disambiguateInternalMember(element, proposeName);
946 } 948 }
947 949
948 // No superclass uses the disambiguated name as a property name, so we can 950 // No superclass uses the disambiguated name as a property name, so we can
949 // use it for this field. This generates nicer field names since otherwise 951 // use it for this field. This generates nicer field names since otherwise
950 // the field name would have to be mangled. 952 // the field name would have to be mangled.
951 return _disambiguateMember(new Name(element.name, element.library)); 953 return _disambiguateMember(new Name(element.name, element.library));
952 } 954 }
953 955
954 bool _isShadowingSuperField(Element element) { 956 bool _isShadowingSuperField(FieldEntity element) {
955 return element.enclosingClass.hasFieldShadowedBy(element); 957 ClassEntity cls = element.enclosingClass;
958 if (cls is ClassElement) {
959 return cls.hasFieldShadowedBy(element);
960 }
961 // TODO(johnniwinther): Support class entities.
962 return false;
956 } 963 }
957 964
958 /// True if [class_] is a non-native class that inherits from a native class. 965 /// True if [class_] is a non-native class that inherits from a native class.
959 bool _isUserClassExtendingNative(ClassElement class_) { 966 bool _isUserClassExtendingNative(ClassEntity class_) {
960 return !_nativeData.isNativeClass(class_) && 967 return !_nativeData.isNativeClass(class_) &&
961 _nativeData.isNativeOrExtendsNative(class_.superclass); 968 _nativeData.isNativeOrExtendsNative(class_);
962 } 969 }
963 970
964 /// Annotated name for the setter of [element]. 971 /// Annotated name for the setter of [element].
965 jsAst.Name setterForElement(MemberElement element) { 972 jsAst.Name setterForMember(MemberEntity element) {
966 // We dynamically create setters from the field-name. The setter name must 973 // We dynamically create setters from the field-name. The setter name must
967 // therefore be derived from the instance field-name. 974 // therefore be derived from the instance field-name.
968 jsAst.Name name = _disambiguateMember(element.memberName); 975 jsAst.Name name = _disambiguateMember(element.memberName);
969 return deriveSetterName(name); 976 return deriveSetterName(name);
970 } 977 }
971 978
972 /// Annotated name for the setter of any member with [disambiguatedName]. 979 /// Annotated name for the setter of any member with [disambiguatedName].
973 jsAst.Name deriveSetterName(jsAst.Name disambiguatedName) { 980 jsAst.Name deriveSetterName(jsAst.Name disambiguatedName) {
974 // We dynamically create setters from the field-name. The setter name must 981 // We dynamically create setters from the field-name. The setter name must
975 // therefore be derived from the instance field-name. 982 // therefore be derived from the instance field-name.
(...skipping 1255 matching lines...) Expand 10 before | Expand all | Expand 10 after
2231 void addSuggestion(String original, String suggestion) { 2238 void addSuggestion(String original, String suggestion) {
2232 assert(!_suggestedNames.containsKey(original)); 2239 assert(!_suggestedNames.containsKey(original));
2233 _suggestedNames[original] = suggestion; 2240 _suggestedNames[original] = suggestion;
2234 } 2241 }
2235 2242
2236 bool hasSuggestion(String original) => _suggestedNames.containsKey(original); 2243 bool hasSuggestion(String original) => _suggestedNames.containsKey(original);
2237 bool isSuggestion(String candidate) { 2244 bool isSuggestion(String candidate) {
2238 return _suggestedNames.containsValue(candidate); 2245 return _suggestedNames.containsValue(candidate);
2239 } 2246 }
2240 } 2247 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/elements/elements.dart ('k') | pkg/compiler/lib/src/js_emitter/full_emitter/class_emitter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698