OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 'package:kernel/ast.dart' as ir; | 5 import 'package:kernel/ast.dart' as ir; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common.dart'; |
8 import '../common/names.dart'; | 8 import '../common/names.dart'; |
9 import '../constants/constructors.dart'; | 9 import '../constants/constructors.dart'; |
10 import '../constants/expressions.dart'; | 10 import '../constants/expressions.dart'; |
11 import '../common_elements.dart'; | 11 import '../common_elements.dart'; |
12 import '../elements/elements.dart'; | 12 import '../elements/elements.dart'; |
13 import '../elements/entities.dart'; | 13 import '../elements/entities.dart'; |
14 import '../elements/types.dart'; | 14 import '../elements/types.dart'; |
15 import '../js_backend/backend_helpers.dart'; | 15 import '../js_backend/backend.dart' show JavaScriptBackend; |
16 import '../native/native.dart' as native; | 16 import '../native/native.dart' as native; |
17 import '../universe/call_structure.dart'; | 17 import '../universe/call_structure.dart'; |
18 import '../universe/selector.dart'; | 18 import '../universe/selector.dart'; |
19 import 'kernel_debug.dart'; | 19 import 'kernel_debug.dart'; |
20 | 20 |
21 /// Interface that translates between Kernel IR nodes and entities. | 21 /// Interface that translates between Kernel IR nodes and entities. |
22 abstract class KernelElementAdapter { | 22 abstract class KernelElementAdapter { |
23 /// Access to the commonly used elements and types. | 23 /// Access to the commonly used elements and types. |
24 CommonElements get commonElements; | 24 CommonElements get commonElements; |
25 | 25 |
26 // Access to backend helpers. | |
27 BackendHelpers get helpers; | |
28 | |
29 /// [ElementEnvironment] for library, class and member lookup. | 26 /// [ElementEnvironment] for library, class and member lookup. |
30 ElementEnvironment get elementEnvironment; | 27 ElementEnvironment get elementEnvironment; |
31 | 28 |
32 /// Returns the [DartType] corresponding to [type]. | 29 /// Returns the [DartType] corresponding to [type]. |
33 DartType getDartType(ir.DartType type); | 30 DartType getDartType(ir.DartType type); |
34 | 31 |
35 /// Returns the list of [DartType]s corresponding to [types]. | 32 /// Returns the list of [DartType]s corresponding to [types]. |
36 List<DartType> getDartTypes(List<ir.DartType> types); | 33 List<DartType> getDartTypes(List<ir.DartType> types); |
37 | 34 |
38 /// Returns the [InterfaceType] corresponding to [type]. | 35 /// Returns the [InterfaceType] corresponding to [type]. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 JS_BUILTIN, | 115 JS_BUILTIN, |
119 JS_EMBEDDED_GLOBAL, | 116 JS_EMBEDDED_GLOBAL, |
120 JS_INTERCEPTOR_CONSTANT, | 117 JS_INTERCEPTOR_CONSTANT, |
121 NONE, | 118 NONE, |
122 } | 119 } |
123 | 120 |
124 abstract class KernelElementAdapterMixin implements KernelElementAdapter { | 121 abstract class KernelElementAdapterMixin implements KernelElementAdapter { |
125 DiagnosticReporter get reporter; | 122 DiagnosticReporter get reporter; |
126 FunctionType getFunctionType(ir.FunctionNode node); | 123 FunctionType getFunctionType(ir.FunctionNode node); |
127 native.BehaviorBuilder get nativeBehaviorBuilder; | 124 native.BehaviorBuilder get nativeBehaviorBuilder; |
128 BackendHelpers _helpers; | |
129 | |
130 @override | |
131 BackendHelpers get helpers => | |
132 _helpers ??= new BackendHelpers(elementEnvironment, commonElements); | |
133 | 125 |
134 @override | 126 @override |
135 Name getName(ir.Name name) { | 127 Name getName(ir.Name name) { |
136 return new Name( | 128 return new Name( |
137 name.name, name.isPrivate ? getLibrary(name.library) : null); | 129 name.name, name.isPrivate ? getLibrary(name.library) : null); |
138 } | 130 } |
139 | 131 |
140 @override | 132 @override |
141 CallStructure getCallStructure(ir.Arguments arguments) { | 133 CallStructure getCallStructure(ir.Arguments arguments) { |
142 int argumentCount = arguments.positional.length + arguments.named.length; | 134 int argumentCount = arguments.positional.length + arguments.named.length; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 }); | 204 }); |
213 return metadata; | 205 return metadata; |
214 } | 206 } |
215 | 207 |
216 /// Returns `true` is [node] has a `@Native(...)` annotation. | 208 /// Returns `true` is [node] has a `@Native(...)` annotation. |
217 // TODO(johnniwinther): Cache this for later use. | 209 // TODO(johnniwinther): Cache this for later use. |
218 bool isNativeClass(ir.Class node) { | 210 bool isNativeClass(ir.Class node) { |
219 for (ir.Expression annotation in node.annotations) { | 211 for (ir.Expression annotation in node.annotations) { |
220 if (annotation is ir.ConstructorInvocation) { | 212 if (annotation is ir.ConstructorInvocation) { |
221 FunctionEntity target = getConstructor(annotation.target); | 213 FunctionEntity target = getConstructor(annotation.target); |
222 if (target.enclosingClass == helpers.nativeAnnotationClass) { | 214 if (target.enclosingClass == commonElements.nativeAnnotationClass) { |
223 return true; | 215 return true; |
224 } | 216 } |
225 } | 217 } |
226 } | 218 } |
227 return false; | 219 return false; |
228 } | 220 } |
229 | 221 |
230 /// Compute the kind of foreign helper function called by [node], if any. | 222 /// Compute the kind of foreign helper function called by [node], if any. |
231 ForeignKind getForeignKind(ir.StaticInvocation node) { | 223 ForeignKind getForeignKind(ir.StaticInvocation node) { |
232 if (isForeignLibrary(node.target.enclosingLibrary)) { | 224 if (isForeignLibrary(node.target.enclosingLibrary)) { |
233 switch (node.target.name.name) { | 225 switch (node.target.name.name) { |
234 case BackendHelpers.JS: | 226 case JavaScriptBackend.JS: |
235 return ForeignKind.JS; | 227 return ForeignKind.JS; |
236 case BackendHelpers.JS_BUILTIN: | 228 case JavaScriptBackend.JS_BUILTIN: |
237 return ForeignKind.JS_BUILTIN; | 229 return ForeignKind.JS_BUILTIN; |
238 case BackendHelpers.JS_EMBEDDED_GLOBAL: | 230 case JavaScriptBackend.JS_EMBEDDED_GLOBAL: |
239 return ForeignKind.JS_EMBEDDED_GLOBAL; | 231 return ForeignKind.JS_EMBEDDED_GLOBAL; |
240 case BackendHelpers.JS_INTERCEPTOR_CONSTANT: | 232 case JavaScriptBackend.JS_INTERCEPTOR_CONSTANT: |
241 return ForeignKind.JS_INTERCEPTOR_CONSTANT; | 233 return ForeignKind.JS_INTERCEPTOR_CONSTANT; |
242 } | 234 } |
243 } | 235 } |
244 return ForeignKind.NONE; | 236 return ForeignKind.NONE; |
245 } | 237 } |
246 | 238 |
247 /// Return `true` if [node] is the `dart:_foreign_helper` library. | 239 /// Return `true` if [node] is the `dart:_foreign_helper` library. |
248 bool isForeignLibrary(ir.Library node) { | 240 bool isForeignLibrary(ir.Library node) { |
249 return node.importUri == BackendHelpers.DART_FOREIGN_HELPER; | 241 return node.importUri == Uris.dart__foreign_helper; |
250 } | 242 } |
251 | 243 |
252 /// Looks up [typeName] for use in the spec-string of a `JS` called. | 244 /// Looks up [typeName] for use in the spec-string of a `JS` called. |
253 // TODO(johnniwinther): Use this in [native.NativeBehavior] instead of calling | 245 // TODO(johnniwinther): Use this in [native.NativeBehavior] instead of calling |
254 // the `ForeignResolver`. | 246 // the `ForeignResolver`. |
255 // TODO(johnniwinther): Cache the result to avoid redundant lookups? | 247 // TODO(johnniwinther): Cache the result to avoid redundant lookups? |
256 native.TypeLookup typeLookup({bool resolveAsRaw: true}) { | 248 native.TypeLookup typeLookup({bool resolveAsRaw: true}) { |
257 DartType lookup(String typeName, {bool required}) { | 249 DartType lookup(String typeName, {bool required}) { |
258 DartType findIn(Uri uri) { | 250 DartType findIn(Uri uri) { |
259 LibraryEntity library = elementEnvironment.lookupLibrary(uri); | 251 LibraryEntity library = elementEnvironment.lookupLibrary(uri); |
260 if (library != null) { | 252 if (library != null) { |
261 ClassEntity cls = elementEnvironment.lookupClass(library, typeName); | 253 ClassEntity cls = elementEnvironment.lookupClass(library, typeName); |
262 if (cls != null) { | 254 if (cls != null) { |
263 // TODO(johnniwinther): Align semantics. | 255 // TODO(johnniwinther): Align semantics. |
264 return resolveAsRaw | 256 return resolveAsRaw |
265 ? elementEnvironment.getRawType(cls) | 257 ? elementEnvironment.getRawType(cls) |
266 : elementEnvironment.getThisType(cls); | 258 : elementEnvironment.getThisType(cls); |
267 } | 259 } |
268 } | 260 } |
269 return null; | 261 return null; |
270 } | 262 } |
271 | 263 |
272 // TODO(johnniwinther): Narrow the set of lookups base on the depending | 264 // TODO(johnniwinther): Narrow the set of lookups base on the depending |
273 // library. | 265 // library. |
274 DartType type = findIn(Uris.dart_core); | 266 DartType type = findIn(Uris.dart_core); |
275 type ??= findIn(BackendHelpers.DART_JS_HELPER); | 267 type ??= findIn(Uris.dart__js_helper); |
276 type ??= findIn(BackendHelpers.DART_INTERCEPTORS); | 268 type ??= findIn(Uris.dart__interceptors); |
277 type ??= findIn(BackendHelpers.DART_ISOLATE_HELPER); | 269 type ??= findIn(Uris.dart__isolate_helper); |
278 type ??= findIn(Uris.dart__native_typed_data); | 270 type ??= findIn(Uris.dart__native_typed_data); |
279 type ??= findIn(Uris.dart_collection); | 271 type ??= findIn(Uris.dart_collection); |
280 type ??= findIn(Uris.dart_math); | 272 type ??= findIn(Uris.dart_math); |
281 type ??= findIn(Uris.dart_html); | 273 type ??= findIn(Uris.dart_html); |
282 type ??= findIn(Uris.dart_html_common); | 274 type ??= findIn(Uris.dart_html_common); |
283 type ??= findIn(Uris.dart_svg); | 275 type ??= findIn(Uris.dart_svg); |
284 type ??= findIn(Uris.dart_web_audio); | 276 type ??= findIn(Uris.dart_web_audio); |
285 type ??= findIn(Uris.dart_web_gl); | 277 type ??= findIn(Uris.dart_web_gl); |
286 type ??= findIn(Uris.dart_web_sql); | 278 type ??= findIn(Uris.dart_web_sql); |
287 type ??= findIn(Uris.dart_indexed_db); | 279 type ??= findIn(Uris.dart_indexed_db); |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 } | 605 } |
614 if (isRedirecting) { | 606 if (isRedirecting) { |
615 return new RedirectingGenerativeConstantConstructor( | 607 return new RedirectingGenerativeConstantConstructor( |
616 defaultValues, superConstructorInvocation); | 608 defaultValues, superConstructorInvocation); |
617 } else { | 609 } else { |
618 return new GenerativeConstantConstructor( | 610 return new GenerativeConstantConstructor( |
619 type, defaultValues, fieldMap, superConstructorInvocation); | 611 type, defaultValues, fieldMap, superConstructorInvocation); |
620 } | 612 } |
621 } | 613 } |
622 } | 614 } |
OLD | NEW |