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

Side by Side Diff: pkg/compiler/lib/src/native/behavior.dart

Issue 2998543002: Handle js interop members in impact computation. (Closed)
Patch Set: Updated cf. comments. 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 import '../common.dart'; 5 import '../common.dart';
6 import '../common/backend_api.dart' show ForeignResolver; 6 import '../common/backend_api.dart' show ForeignResolver;
7 import '../common/resolution.dart' show ParsingContext, Resolution; 7 import '../common/resolution.dart' show ParsingContext, Resolution;
8 import '../compiler.dart' show Compiler; 8 import '../compiler.dart' show Compiler;
9 import '../constants/values.dart'; 9 import '../constants/values.dart';
10 import '../common_elements.dart' show CommonElements; 10 import '../common_elements.dart' show CommonElements, ElementEnvironment;
11 import '../elements/elements.dart'; 11 import '../elements/elements.dart';
12 import '../elements/entities.dart'; 12 import '../elements/entities.dart';
13 import '../elements/resolution_types.dart'; 13 import '../elements/resolution_types.dart';
14 import '../elements/types.dart'; 14 import '../elements/types.dart';
15 import '../js/js.dart' as js; 15 import '../js/js.dart' as js;
16 import '../js_backend/native_data.dart' show NativeBasicData; 16 import '../js_backend/native_data.dart' show NativeBasicData;
17 import '../resolution/resolution_strategy.dart';
17 import '../tree/tree.dart'; 18 import '../tree/tree.dart';
18 import '../universe/side_effects.dart' show SideEffects; 19 import '../universe/side_effects.dart' show SideEffects;
19 import '../util/util.dart'; 20 import '../util/util.dart';
20 import 'js.dart'; 21 import 'js.dart';
21 22
22 typedef dynamic /*DartType|SpecialType*/ TypeLookup(String typeString, 23 typedef dynamic /*DartType|SpecialType*/ TypeLookup(String typeString,
23 {bool required}); 24 {bool required});
24 25
25 /// This class is a temporary work-around until we get a more powerful DartType. 26 /// This class is a temporary work-around until we get a more powerful DartType.
26 class SpecialType { 27 class SpecialType {
(...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 } 845 }
845 return const ResolutionDynamicType(); 846 return const ResolutionDynamicType();
846 } 847 }
847 } 848 }
848 849
849 abstract class BehaviorBuilder { 850 abstract class BehaviorBuilder {
850 CommonElements get commonElements; 851 CommonElements get commonElements;
851 DiagnosticReporter get reporter; 852 DiagnosticReporter get reporter;
852 NativeBasicData get nativeBasicData; 853 NativeBasicData get nativeBasicData;
853 bool get trustJSInteropTypeAnnotations; 854 bool get trustJSInteropTypeAnnotations;
854 855 ElementEnvironment get elementEnvironment;
855 Resolution get resolution => null;
856 856
857 NativeBehavior _behavior; 857 NativeBehavior _behavior;
858 858
859 void _overrideWithAnnotations( 859 void _overrideWithAnnotations(
860 Iterable<ConstantValue> metadata, TypeLookup lookupType) { 860 Iterable<ConstantValue> metadata, TypeLookup lookupType) {
861 if (metadata.isEmpty) return; 861 if (metadata.isEmpty) return;
862 862
863 List creates = 863 List creates =
864 _collect(metadata, commonElements.annotationCreatesClass, lookupType); 864 _collect(metadata, commonElements.annotationCreatesClass, lookupType);
865 List returns = 865 List returns =
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
903 if (types == null) types = []; 903 if (types == null) types = [];
904 types.add(type); 904 types.add(type);
905 } 905 }
906 } 906 }
907 return types; 907 return types;
908 } 908 }
909 909
910 /// Models the behavior of having instances of [type] escape from Dart code 910 /// Models the behavior of having instances of [type] escape from Dart code
911 /// into native code. 911 /// into native code.
912 void _escape(DartType type) { 912 void _escape(DartType type) {
913 if (type is ResolutionDartType) { 913 type = elementEnvironment.getUnaliasedType(type);
914 type.computeUnaliased(resolution);
915 }
916 type = type.unaliased;
917 if (type is FunctionType) { 914 if (type is FunctionType) {
918 FunctionType functionType = type; 915 FunctionType functionType = type;
919 // A function might be called from native code, passing us novel 916 // A function might be called from native code, passing us novel
920 // parameters. 917 // parameters.
921 _escape(functionType.returnType); 918 _escape(functionType.returnType);
922 for (DartType parameter in functionType.parameterTypes) { 919 for (DartType parameter in functionType.parameterTypes) {
923 _capture(parameter); 920 _capture(parameter);
924 } 921 }
925 } 922 }
926 } 923 }
927 924
928 /// Models the behavior of Dart code receiving instances and methods of [type] 925 /// Models the behavior of Dart code receiving instances and methods of [type]
929 /// from native code. We usually start the analysis by capturing a native 926 /// from native code. We usually start the analysis by capturing a native
930 /// method that has been used. 927 /// method that has been used.
931 /// 928 ///
932 /// We assume that JS-interop APIs cannot instantiate Dart types or 929 /// We assume that JS-interop APIs cannot instantiate Dart types or
933 /// non-JSInterop native types. 930 /// non-JSInterop native types.
934 void _capture(DartType type, {bool isInterop: false}) { 931 void _capture(DartType type, {bool isInterop: false}) {
935 if (type is ResolutionDartType) { 932 type = elementEnvironment.getUnaliasedType(type);
936 type.computeUnaliased(resolution);
937 }
938 type = type.unaliased;
939 if (type is FunctionType) { 933 if (type is FunctionType) {
940 FunctionType functionType = type; 934 FunctionType functionType = type;
941 _capture(functionType.returnType, isInterop: isInterop); 935 _capture(functionType.returnType, isInterop: isInterop);
942 for (DartType parameter in functionType.parameterTypes) { 936 for (DartType parameter in functionType.parameterTypes) {
943 _escape(parameter); 937 _escape(parameter);
944 } 938 }
945 } else { 939 } else {
946 if (!isInterop) { 940 if (!isInterop) {
947 _behavior.typesInstantiated.add(type); 941 _behavior.typesInstantiated.add(type);
948 } else { 942 } else {
949 if (type is InterfaceType && 943 if (type is InterfaceType &&
950 nativeBasicData.isNativeClass(type.element)) { 944 nativeBasicData.isNativeClass(type.element)) {
951 // Any declared native or interop type (isNative implies isJsInterop) 945 // Any declared native or interop type (isNative implies isJsInterop)
952 // is assumed to be allocated. 946 // is assumed to be allocated.
953 _behavior.typesInstantiated.add(type); 947 _behavior.typesInstantiated.add(type);
954 } 948 }
955 949
956 if (!trustJSInteropTypeAnnotations || 950 if (!trustJSInteropTypeAnnotations ||
957 type.isDynamic || 951 type.isDynamic ||
958 type == commonElements.objectType) { 952 type == commonElements.objectType) {
959 // By saying that only JS-interop types can be created, we prevent 953 // By saying that only JS-interop types can be created, we prevent
960 // pulling in every other native type (e.g. all of dart:html) when a 954 // pulling in every other native type (e.g. all of dart:html) when a
961 // JS interop API returns dynamic or when we don't trust the type 955 // JS interop API returns dynamic or when we don't trust the type
962 // annotations. This means that to some degree we still use the return 956 // annotations. This means that to some degree we still use the return
963 // type to decide whether to include native types, even if we don't 957 // type to decide whether to include native types, even if we don't
964 // trust the type annotation. 958 // trust the type annotation.
965 ClassElement cls = commonElements.jsJavaScriptObjectClass; 959 ClassEntity cls = commonElements.jsJavaScriptObjectClass;
966 cls.ensureResolved(resolution); 960 _behavior.typesInstantiated.add(elementEnvironment.getThisType(cls));
967 _behavior.typesInstantiated.add(cls.thisType);
968 } else { 961 } else {
969 // Otherwise, when the declared type is a Dart type, we do not 962 // Otherwise, when the declared type is a Dart type, we do not
970 // register an allocation because we assume it cannot be instantiated 963 // register an allocation because we assume it cannot be instantiated
971 // from within the JS-interop code. It must have escaped from another 964 // from within the JS-interop code. It must have escaped from another
972 // API. 965 // API.
973 } 966 }
974 } 967 }
975 } 968 }
976 } 969 }
977 970
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1033 } 1026 }
1034 } 1027 }
1035 1028
1036 class ResolverBehaviorBuilder extends BehaviorBuilder { 1029 class ResolverBehaviorBuilder extends BehaviorBuilder {
1037 final Compiler compiler; 1030 final Compiler compiler;
1038 final NativeBasicData nativeBasicData; 1031 final NativeBasicData nativeBasicData;
1039 1032
1040 ResolverBehaviorBuilder(this.compiler, this.nativeBasicData); 1033 ResolverBehaviorBuilder(this.compiler, this.nativeBasicData);
1041 1034
1042 @override 1035 @override
1043 CommonElements get commonElements => resolution.commonElements; 1036 CommonElements get commonElements => compiler.resolution.commonElements;
1044 1037
1045 @override 1038 @override
1046 bool get trustJSInteropTypeAnnotations => 1039 bool get trustJSInteropTypeAnnotations =>
1047 compiler.options.trustJSInteropTypeAnnotations; 1040 compiler.options.trustJSInteropTypeAnnotations;
1048 1041
1049 @override 1042 @override
1050 DiagnosticReporter get reporter => compiler.reporter; 1043 DiagnosticReporter get reporter => compiler.reporter;
1051 1044
1052 @override 1045 @override
1053 Resolution get resolution => compiler.resolution; 1046 ElementEnvironment get elementEnvironment {
1047 ResolutionFrontEndStrategy frontendStrategy = compiler.frontendStrategy;
1048 return frontendStrategy.elementEnvironment;
1049 }
1054 } 1050 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/kernel/native_basic_data.dart ('k') | pkg/compiler/lib/src/native/resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698