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

Side by Side Diff: pkg/compiler/lib/src/js_emitter/program_builder/program_builder.dart

Issue 3001823002: Revert "Various redemptions" (Closed)
Patch Set: Created 3 years, 4 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) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 dart2js.js_emitter.program_builder; 5 library dart2js.js_emitter.program_builder;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 import 'dart:convert' show JSON; 8 import 'dart:convert' show JSON;
9 9
10 import '../../closure.dart' show ClosureConversionTask, ClosureFieldElement; 10 import '../../closure.dart' show ClosureConversionTask, ClosureFieldElement;
11 import '../../common.dart'; 11 import '../../common.dart';
12 import '../../common/names.dart' show Names, Selectors; 12 import '../../common/names.dart' show Names, Selectors;
13 import '../../constants/values.dart' 13 import '../../constants/values.dart'
14 show ConstantValue, InterceptorConstantValue; 14 show ConstantValue, InterceptorConstantValue;
15 import '../../common_elements.dart' show CommonElements, ElementEnvironment; 15 import '../../common_elements.dart' show CommonElements, ElementEnvironment;
16 import '../../deferred_load.dart' show DeferredLoadTask, OutputUnit; 16 import '../../deferred_load.dart' show DeferredLoadTask, OutputUnit;
17 import '../../elements/elements.dart' 17 import '../../elements/elements.dart'
18 show ClassElement, FieldElement, LibraryElement, MethodElement; 18 show
19 ClassElement,
20 FieldElement,
21 FunctionSignature,
22 LibraryElement,
23 MemberElement,
24 MethodElement,
25 ParameterElement;
19 import '../../elements/entities.dart'; 26 import '../../elements/entities.dart';
27 import '../../elements/resolution_types.dart' show ResolutionDartType;
20 import '../../elements/types.dart'; 28 import '../../elements/types.dart';
21 import '../../js/js.dart' as js; 29 import '../../js/js.dart' as js;
22 import '../../js_backend/backend.dart' show SuperMemberData; 30 import '../../js_backend/backend.dart' show SuperMemberData;
23 import '../../js_backend/backend_usage.dart'; 31 import '../../js_backend/backend_usage.dart';
24 import '../../js_backend/constant_handler_javascript.dart' 32 import '../../js_backend/constant_handler_javascript.dart'
25 show JavaScriptConstantCompiler; 33 show JavaScriptConstantCompiler;
26 import '../../js_backend/custom_elements_analysis.dart'; 34 import '../../js_backend/custom_elements_analysis.dart';
27 import '../../js_backend/namer.dart' show Namer, StringBackedName; 35 import '../../js_backend/namer.dart' show Namer, StringBackedName;
28 import '../../js_backend/native_data.dart'; 36 import '../../js_backend/native_data.dart';
29 import '../../js_backend/interceptor_data.dart'; 37 import '../../js_backend/interceptor_data.dart';
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 final Map<js.Name, String> _symbolsMap = <js.Name, String>{}; 185 final Map<js.Name, String> _symbolsMap = <js.Name, String>{};
178 186
179 Set<Class> _unneededNativeClasses; 187 Set<Class> _unneededNativeClasses;
180 188
181 /// Classes that have been allocated during a profile run. 189 /// Classes that have been allocated during a profile run.
182 /// 190 ///
183 /// These classes should not be soft-deferred. 191 /// These classes should not be soft-deferred.
184 /// 192 ///
185 /// Also contains classes that are not tracked by the profile run (like 193 /// Also contains classes that are not tracked by the profile run (like
186 /// interceptors, ...). 194 /// interceptors, ...).
187 Set<ClassEntity> _notSoftDeferred; 195 Set<ClassElement> _notSoftDeferred;
188 196
189 Program buildProgram({bool storeFunctionTypesInMetadata: false}) { 197 Program buildProgram({bool storeFunctionTypesInMetadata: false}) {
190 collector.collect(); 198 collector.collect();
191 _initializeSoftDeferredMap(); 199 _initializeSoftDeferredMap();
192 200
193 this._storeFunctionTypesInMetadata = storeFunctionTypesInMetadata; 201 this._storeFunctionTypesInMetadata = storeFunctionTypesInMetadata;
194 // Note: In rare cases (mostly tests) output units can be empty. This 202 // Note: In rare cases (mostly tests) output units can be empty. This
195 // happens when the deferred code is dead-code eliminated but we still need 203 // happens when the deferred code is dead-code eliminated but we still need
196 // to check that the library has been loaded. 204 // to check that the library has been loaded.
197 _deferredLoadTask.allOutputUnits.forEach(_registry.registerOutputUnit); 205 _deferredLoadTask.allOutputUnits.forEach(_registry.registerOutputUnit);
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 _reporter.log("Profile file does not exist: $allocatedClassesPath"); 319 _reporter.log("Profile file does not exist: $allocatedClassesPath");
312 return; 320 return;
313 } 321 }
314 if (file.lengthSync() == 0) { 322 if (file.lengthSync() == 0) {
315 _reporter.log("Profile information (allocated classes) is empty."); 323 _reporter.log("Profile information (allocated classes) is empty.");
316 return; 324 return;
317 } 325 }
318 326
319 String data = new File(allocatedClassesPath).readAsStringSync(); 327 String data = new File(allocatedClassesPath).readAsStringSync();
320 Set<String> allocatedClassesKeys = JSON.decode(data).keys.toSet(); 328 Set<String> allocatedClassesKeys = JSON.decode(data).keys.toSet();
321 Set<ClassEntity> allocatedClasses = new Set<ClassEntity>(); 329 Set<ClassElement> allocatedClasses = new Set<ClassElement>();
322 330
323 // Collects all super and mixin classes of a class. 331 // Collects all super and mixin classes of a class.
324 void collect(ClassEntity element) { 332 void collect(ClassElement element) {
325 allocatedClasses.add(element); 333 allocatedClasses.add(element);
326 if (_elementEnvironment.isMixinApplication(element)) { 334 if (element.isMixinApplication) {
327 collect(_elementEnvironment.getEffectiveMixinClass(element)); 335 collect(_elementEnvironment.getEffectiveMixinClass(element));
328 } 336 }
329 ClassEntity superclass = _elementEnvironment.getSuperClass(element); 337 if (element.superclass != null) {
330 if (superclass != null) { 338 collect(element.superclass);
331 collect(superclass);
332 } 339 }
333 } 340 }
334 341
335 // For every known class, see if it was allocated in the profile. If yes, 342 // For every known class, see if it was allocated in the profile. If yes,
336 // collect its dependencies (supers and mixins) and mark them as 343 // collect its dependencies (supers and mixins) and mark them as
337 // not-soft-deferrable. 344 // not-soft-deferrable.
338 collector.outputClassLists.forEach((_, List<ClassEntity> elements) { 345 collector.outputClassLists.forEach((_, List<ClassEntity> elements) {
339 for (ClassEntity element in elements) { 346 for (ClassElement element in elements) {
340 // TODO(29574): share the encoding of the element with the code 347 // TODO(29574): share the encoding of the element with the code
341 // that emits the profile-run. 348 // that emits the profile-run.
342 var key = "${element.library.canonicalUri}:${element.name}"; 349 var key = "${element.library.canonicalUri}:${element.name}";
343 if (allocatedClassesKeys.contains(key) || 350 if (allocatedClassesKeys.contains(key) ||
344 _nativeData.isJsInteropClass(element) || 351 _nativeData.isJsInteropClass(element) ||
345 blackList.contains(element.library.canonicalUri.toString())) { 352 blackList.contains(element.library.canonicalUri.toString())) {
346 collect(element); 353 collect(element);
347 } 354 }
348 } 355 }
349 }); 356 });
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 } 555 }
549 } 556 }
550 557
551 // Generating stubs for direct calls and stubs for call-through 558 // Generating stubs for direct calls and stubs for call-through
552 // of getters that happen to be functions. 559 // of getters that happen to be functions.
553 bool isFunctionLike = false; 560 bool isFunctionLike = false;
554 FunctionType functionType = null; 561 FunctionType functionType = null;
555 562
556 if (member.isFunction) { 563 if (member.isFunction) {
557 MethodElement fn = member; 564 MethodElement fn = member;
558 functionType = _elementEnvironment.getFunctionType(fn); 565 functionType = fn.type;
559 } else if (member.isGetter) { 566 } else if (member.isGetter) {
560 if (_options.trustTypeAnnotations) { 567 if (_options.trustTypeAnnotations) {
561 DartType returnType = 568 DartType returnType =
562 _elementEnvironment.getFunctionType(member).returnType; 569 _elementEnvironment.getFunctionType(member).returnType;
563 if (returnType.isFunctionType) { 570 if (returnType.isFunctionType) {
564 functionType = returnType; 571 functionType = returnType;
565 } else if (returnType.treatAsDynamic || 572 } else if (returnType.treatAsDynamic ||
566 _types.isSubtype( 573 _types.isSubtype(
567 returnType, 574 returnType,
568 // ignore: UNNECESSARY_CAST 575 // ignore: UNNECESSARY_CAST
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 if (_nativeData.isJsInteropClass(cls)) { 779 if (_nativeData.isJsInteropClass(cls)) {
773 typeTests.forEachProperty(_sorter, (js.Name name, js.Node code) { 780 typeTests.forEachProperty(_sorter, (js.Name name, js.Node code) {
774 _classes[_commonElements.jsInterceptorClass] 781 _classes[_commonElements.jsInterceptorClass]
775 .isChecks 782 .isChecks
776 .add(_buildStubMethod(name, code)); 783 .add(_buildStubMethod(name, code));
777 }); 784 });
778 } else { 785 } else {
779 for (Field field in instanceFields) { 786 for (Field field in instanceFields) {
780 if (field.needsCheckedSetter) { 787 if (field.needsCheckedSetter) {
781 assert(!field.needsUncheckedSetter); 788 assert(!field.needsUncheckedSetter);
782 FieldEntity element = field.element; 789 FieldElement element = field.element;
783 js.Expression code = _generatedCode[element]; 790 js.Expression code = _generatedCode[element];
784 assert(code != null); 791 assert(code != null);
785 js.Name name = _namer.deriveSetterName(field.accessorName); 792 js.Name name = _namer.deriveSetterName(field.accessorName);
786 checkedSetters.add(_buildStubMethod(name, code, element: element)); 793 checkedSetters.add(_buildStubMethod(name, code, element: element));
787 } 794 }
788 } 795 }
789 796
790 typeTests.forEachProperty(_sorter, (js.Name name, js.Node code) { 797 typeTests.forEachProperty(_sorter, (js.Name name, js.Node code) {
791 isChecks.add(_buildStubMethod(name, code)); 798 isChecks.add(_buildStubMethod(name, code));
792 }); 799 });
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 856
850 bool _methodCanBeReflected(FunctionEntity method) { 857 bool _methodCanBeReflected(FunctionEntity method) {
851 return _mirrorsData.isMemberAccessibleByReflection(method); 858 return _mirrorsData.isMemberAccessibleByReflection(method);
852 } 859 }
853 860
854 bool _methodCanBeApplied(FunctionEntity method) { 861 bool _methodCanBeApplied(FunctionEntity method) {
855 return _backendUsage.isFunctionApplyUsed && 862 return _backendUsage.isFunctionApplyUsed &&
856 _closedWorld.getMightBePassedToApply(method); 863 _closedWorld.getMightBePassedToApply(method);
857 } 864 }
858 865
859 /* Map | List */ _computeParameterDefaultValues(FunctionEntity method) { 866 /* Map | List */ _computeParameterDefaultValues(FunctionSignature signature) {
860 var /* Map | List */ optionalParameterDefaultValues; 867 var /* Map | List */ optionalParameterDefaultValues;
861 ParameterStructure parameterStructure = method.parameterStructure; 868 if (signature.optionalParametersAreNamed) {
862 if (parameterStructure.namedParameters.isNotEmpty) {
863 optionalParameterDefaultValues = new Map<String, ConstantValue>(); 869 optionalParameterDefaultValues = new Map<String, ConstantValue>();
864 _worldBuilder.forEachParameter(method, 870 signature.forEachOptionalParameter((_parameter) {
865 (DartType type, String name, ConstantValue defaultValue) { 871 ParameterElement parameter = _parameter;
866 if (parameterStructure.namedParameters.contains(name)) { 872 ConstantValue def =
867 assert(defaultValue != null); 873 _constantHandler.getConstantValue(parameter.constant);
868 optionalParameterDefaultValues[name] = defaultValue; 874 optionalParameterDefaultValues[parameter.name] = def;
869 }
870 }); 875 });
871 } else { 876 } else {
872 optionalParameterDefaultValues = <ConstantValue>[]; 877 optionalParameterDefaultValues = <ConstantValue>[];
873 int index = 0; 878 signature.forEachOptionalParameter((_parameter) {
874 _worldBuilder.forEachParameter(method, 879 ParameterElement parameter = _parameter;
875 (DartType type, String name, ConstantValue defaultValue) { 880 ConstantValue def =
876 if (index >= parameterStructure.requiredParameters) { 881 _constantHandler.getConstantValue(parameter.constant);
877 assert(defaultValue != null); 882 optionalParameterDefaultValues.add(def);
878 optionalParameterDefaultValues.add(defaultValue);
879 }
880 index++;
881 }); 883 });
882 } 884 }
883 return optionalParameterDefaultValues; 885 return optionalParameterDefaultValues;
884 } 886 }
885 887
886 DartMethod _buildMethod(FunctionEntity element) { 888 DartMethod _buildMethod(FunctionEntity element) {
887 assert(!(element is MethodElement && !element.isDeclaration)); 889 assert(!(element is MethodElement && !element.isDeclaration));
888 js.Name name = _namer.methodPropertyName(element); 890 js.Name name = _namer.methodPropertyName(element);
889 js.Expression code = _generatedCode[element]; 891 js.Expression code = _generatedCode[element];
890 892
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 js.Expression functionType; 938 js.Expression functionType;
937 if (canTearOff || canBeReflected) { 939 if (canTearOff || canBeReflected) {
938 OutputUnit outputUnit = _deferredLoadTask.outputUnitForMember(element); 940 OutputUnit outputUnit = _deferredLoadTask.outputUnitForMember(element);
939 functionType = _generateFunctionType(memberType, outputUnit); 941 functionType = _generateFunctionType(memberType, outputUnit);
940 } 942 }
941 943
942 int requiredParameterCount; 944 int requiredParameterCount;
943 var /* List | Map */ optionalParameterDefaultValues; 945 var /* List | Map */ optionalParameterDefaultValues;
944 if (canBeApplied || canBeReflected) { 946 if (canBeApplied || canBeReflected) {
945 // TODO(redemption): Handle function entities. 947 // TODO(redemption): Handle function entities.
946 FunctionEntity method = element; 948 MethodElement method = element;
947 ParameterStructure parameterStructure = method.parameterStructure; 949 FunctionSignature signature = method.functionSignature;
948 requiredParameterCount = parameterStructure.requiredParameters; 950 requiredParameterCount = signature.requiredParameterCount;
949 optionalParameterDefaultValues = _computeParameterDefaultValues(method); 951 optionalParameterDefaultValues =
952 _computeParameterDefaultValues(signature);
950 } 953 }
951 954
952 return new InstanceMethod(element, name, code, 955 return new InstanceMethod(element, name, code,
953 _generateParameterStubs(element, canTearOff), callName, 956 _generateParameterStubs(element, canTearOff), callName,
954 needsTearOff: canTearOff, 957 needsTearOff: canTearOff,
955 tearOffName: tearOffName, 958 tearOffName: tearOffName,
956 isClosureCallMethod: isClosureCallMethod, 959 isClosureCallMethod: isClosureCallMethod,
957 aliasName: aliasName, 960 aliasName: aliasName,
958 canBeApplied: canBeApplied, 961 canBeApplied: canBeApplied,
959 canBeReflected: canBeReflected, 962 canBeReflected: canBeReflected,
(...skipping 25 matching lines...) Expand all
985 _worldBuilder, 988 _worldBuilder,
986 _closedWorld); 989 _closedWorld);
987 return generator.generateParameterStubs(element, canTearOff: canTearOff); 990 return generator.generateParameterStubs(element, canTearOff: canTearOff);
988 } 991 }
989 992
990 /// Builds a stub method. 993 /// Builds a stub method.
991 /// 994 ///
992 /// Stub methods may have an element that can be used for code-size 995 /// Stub methods may have an element that can be used for code-size
993 /// attribution. 996 /// attribution.
994 Method _buildStubMethod(js.Name name, js.Expression code, 997 Method _buildStubMethod(js.Name name, js.Expression code,
995 {MemberEntity element}) { 998 {MemberElement element}) {
996 return new StubMethod(name, code, element: element); 999 return new StubMethod(name, code, element: element);
997 } 1000 }
998 1001
999 // The getInterceptor methods directly access the prototype of classes. 1002 // The getInterceptor methods directly access the prototype of classes.
1000 // We must evaluate these classes eagerly so that the prototype is 1003 // We must evaluate these classes eagerly so that the prototype is
1001 // accessible. 1004 // accessible.
1002 void _markEagerInterceptorClasses() { 1005 void _markEagerInterceptorClasses() {
1003 Iterable<js.Name> names = 1006 Iterable<js.Name> names =
1004 _oneShotInterceptorData.specializedGetInterceptorNames; 1007 _oneShotInterceptorData.specializedGetInterceptorNames;
1005 for (js.Name name in names) { 1008 for (js.Name name in names) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1075 setterFlags += 2; 1078 setterFlags += 2;
1076 if (!isHolderInterceptedClass) { 1079 if (!isHolderInterceptedClass) {
1077 setterFlags += 1; 1080 setterFlags += 1;
1078 } 1081 }
1079 } 1082 }
1080 } 1083 }
1081 fields.add(new Field(field, name, accessorName, getterFlags, setterFlags, 1084 fields.add(new Field(field, name, accessorName, getterFlags, setterFlags,
1082 needsCheckedSetter)); 1085 needsCheckedSetter));
1083 } 1086 }
1084 1087
1085 FieldVisitor visitor = new FieldVisitor( 1088 FieldVisitor visitor = new FieldVisitor(_options, _elementEnvironment,
1086 _options, 1089 _worldBuilder, _nativeData, _mirrorsData, _namer, _closedWorld);
1087 _elementEnvironment,
1088 _commonElements,
1089 _worldBuilder,
1090 _nativeData,
1091 _mirrorsData,
1092 _namer,
1093 _closedWorld);
1094 visitor.visitFields(visitField, 1090 visitor.visitFields(visitField,
1095 visitStatics: visitStatics, library: library, cls: cls); 1091 visitStatics: visitStatics, library: library, cls: cls);
1096 1092
1097 return fields; 1093 return fields;
1098 } 1094 }
1099 1095
1100 Iterable<StaticStubMethod> _generateOneShotInterceptors() { 1096 Iterable<StaticStubMethod> _generateOneShotInterceptors() {
1101 InterceptorStubGenerator stubGenerator = new InterceptorStubGenerator( 1097 InterceptorStubGenerator stubGenerator = new InterceptorStubGenerator(
1102 _options, 1098 _options,
1103 _commonElements, 1099 _commonElements,
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 DartType type = _elementEnvironment.getFunctionType(element); 1145 DartType type = _elementEnvironment.getFunctionType(element);
1150 if (needsTearOff || canBeReflected) { 1146 if (needsTearOff || canBeReflected) {
1151 OutputUnit outputUnit = _deferredLoadTask.outputUnitForMember(element); 1147 OutputUnit outputUnit = _deferredLoadTask.outputUnitForMember(element);
1152 functionType = _generateFunctionType(type, outputUnit); 1148 functionType = _generateFunctionType(type, outputUnit);
1153 } 1149 }
1154 1150
1155 int requiredParameterCount; 1151 int requiredParameterCount;
1156 var /* List | Map */ optionalParameterDefaultValues; 1152 var /* List | Map */ optionalParameterDefaultValues;
1157 if (canBeApplied || canBeReflected) { 1153 if (canBeApplied || canBeReflected) {
1158 // TODO(redemption): Support entities; 1154 // TODO(redemption): Support entities;
1159 FunctionEntity method = element; 1155 MethodElement method = element;
1160 ParameterStructure parameterStructure = method.parameterStructure; 1156 FunctionSignature signature = method.functionSignature;
1161 requiredParameterCount = parameterStructure.requiredParameters; 1157 requiredParameterCount = signature.requiredParameterCount;
1162 optionalParameterDefaultValues = _computeParameterDefaultValues(method); 1158 optionalParameterDefaultValues =
1159 _computeParameterDefaultValues(signature);
1163 } 1160 }
1164 1161
1165 // TODO(floitsch): we shouldn't update the registry in the middle of 1162 // TODO(floitsch): we shouldn't update the registry in the middle of
1166 // building a static method. 1163 // building a static method.
1167 return new StaticDartMethod(element, name, _registry.registerHolder(holder), 1164 return new StaticDartMethod(element, name, _registry.registerHolder(holder),
1168 code, _generateParameterStubs(element, needsTearOff), callName, 1165 code, _generateParameterStubs(element, needsTearOff), callName,
1169 needsTearOff: needsTearOff, 1166 needsTearOff: needsTearOff,
1170 tearOffName: tearOffName, 1167 tearOffName: tearOffName,
1171 canBeApplied: canBeApplied, 1168 canBeApplied: canBeApplied,
1172 canBeReflected: canBeReflected, 1169 canBeReflected: canBeReflected,
(...skipping 16 matching lines...) Expand all
1189 Constant constant = new Constant(name, holder, constantValue); 1186 Constant constant = new Constant(name, holder, constantValue);
1190 _constants[constantValue] = constant; 1187 _constants[constantValue] = constant;
1191 } 1188 }
1192 } 1189 }
1193 1190
1194 Holder _registerStaticStateHolder() { 1191 Holder _registerStaticStateHolder() {
1195 return _registry.registerHolder(_namer.staticStateHolder, 1192 return _registry.registerHolder(_namer.staticStateHolder,
1196 isStaticStateHolder: true); 1193 isStaticStateHolder: true);
1197 } 1194 }
1198 } 1195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698