-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathkernel_strategy.dart
476 lines (431 loc) · 15.8 KB
/
kernel_strategy.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library;
import 'package:kernel/ast.dart' as ir;
import '../common.dart';
import '../common/elements.dart';
import '../common/tasks.dart';
import '../common/work.dart';
import '../compiler.dart';
import '../deferred_load/deferred_load.dart' show DeferredLoadTask;
import '../elements/entities.dart';
import '../enqueue.dart';
import '../ir/annotations.dart';
import '../ir/closure.dart' show ClosureScopeModel;
import '../ir/impact.dart';
import '../ir/modular.dart';
import '../ir/scope.dart' show ScopeModel;
import '../js_backend/annotations.dart';
import '../js_backend/backend_impact.dart';
import '../js_backend/backend_usage.dart';
import '../js_backend/custom_elements_analysis.dart';
import '../js_backend/field_analysis.dart' show KFieldAnalysis;
import '../js_backend/interceptor_data.dart';
import '../js_backend/native_data.dart';
import '../js_backend/no_such_method_registry.dart';
import '../js_backend/resolution_listener.dart';
import '../js_backend/runtime_types_resolution.dart';
import '../js_model/elements.dart';
import '../kernel/dart2js_target.dart';
import '../kernel/no_such_method_resolver.dart';
import '../native/enqueue.dart' show NativeResolutionEnqueuer;
import '../native/resolver.dart';
import '../options.dart';
import '../resolution/enqueuer.dart';
import '../universe/class_hierarchy.dart';
import '../universe/resolution_world_builder.dart';
import '../universe/world_builder.dart';
import '../universe/world_impact.dart';
import '../util/enumset.dart';
import 'element_map.dart';
import 'element_map_impl.dart';
import 'native_basic_data.dart';
/// Front end strategy that loads '.dill' files and builds a resolved element
/// model from kernel IR nodes.
class KernelFrontendStrategy {
final CompilerOptions _options;
final CompilerTask _compilerTask;
late final KernelToElementMap _elementMap;
late final RuntimeTypesNeedBuilder _runtimeTypesNeedBuilder =
_options.disableRtiOptimization
? const TrivialRuntimeTypesNeedBuilder()
: RuntimeTypesNeedBuilderImpl(elementEnvironment);
RuntimeTypesNeedBuilder get runtimeTypesNeedBuilderForTesting =>
_runtimeTypesNeedBuilder;
late KernelAnnotationProcessor _annotationProcessor;
final Map<MemberEntity, ClosureScopeModel> closureModels = {};
late ModularStrategy _modularStrategy;
late IrAnnotationData _irAnnotationData;
late NativeDataBuilder _nativeDataBuilder;
NativeDataBuilder get nativeDataBuilder => _nativeDataBuilder;
late final BackendUsageBuilder _backendUsageBuilder = BackendUsageBuilder(
this,
);
late NativeResolutionEnqueuer _nativeResolutionEnqueuer;
/// Resolution support for generating table of interceptors and
/// constructors for custom elements.
late CustomElementsResolutionAnalysis _customElementsResolutionAnalysis;
late KFieldAnalysis _fieldAnalysis;
/// Support for classifying `noSuchMethod` implementations.
late NoSuchMethodRegistry noSuchMethodRegistry;
KernelFrontendStrategy(
this._compilerTask,
this._options,
DiagnosticReporter reporter,
) : _elementMap = KernelToElementMap(reporter, _options) {
_modularStrategy = KernelModularStrategy(_compilerTask, _elementMap);
noSuchMethodRegistry = NoSuchMethodRegistry(
commonElements,
NoSuchMethodResolver(_elementMap),
);
}
NativeResolutionEnqueuer get nativeResolutionEnqueuerForTesting =>
_nativeResolutionEnqueuer;
KFieldAnalysis get fieldAnalysisForTesting => _fieldAnalysis;
/// Called before processing of the resolution queue is started.
void onResolutionStart() {
// TODO(johnniwinther): Avoid the compiler.elementEnvironment.getThisType
// calls. Currently needed to ensure resolution of the classes for various
// queries in native behavior computation, inference and codegen.
elementEnvironment.getThisType(commonElements.jsArrayClass);
elementEnvironment.getThisType(commonElements.jsExtendableArrayClass);
_validateInterceptorImplementsAllObjectMethods(
commonElements.jsInterceptorClass,
);
// The null-interceptor must also implement *all* methods.
_validateInterceptorImplementsAllObjectMethods(commonElements.jsNullClass);
}
void _validateInterceptorImplementsAllObjectMethods(
ClassEntity interceptorClass,
) {
ClassEntity objectClass = commonElements.objectClass;
elementEnvironment.forEachClassMember(objectClass, (
_,
MemberEntity member,
) {
if (!member.isInstanceMember) return;
MemberEntity interceptorMember =
elementEnvironment.lookupLocalClassMember(
interceptorClass,
member.memberName,
)!;
// Interceptors must override all Object methods due to calling convention
// differences.
assert(
interceptorMember.enclosingClass == interceptorClass,
failedAt(
interceptorMember,
"Member ${member.name} not overridden in $interceptorClass. "
"Found $interceptorMember from "
"${interceptorMember.enclosingClass}.",
),
);
});
}
ResolutionEnqueuer createResolutionEnqueuer(
CompilerTask task,
Compiler compiler,
) {
RuntimeTypesNeedBuilder rtiNeedBuilder = _runtimeTypesNeedBuilder;
BackendImpacts impacts = BackendImpacts(commonElements, compiler.options);
final nativeBasicData = _elementMap.nativeBasicData;
_nativeResolutionEnqueuer = NativeResolutionEnqueuer(
compiler.options,
elementEnvironment,
commonElements,
_elementMap.types,
NativeClassFinder(elementEnvironment, nativeBasicData),
);
_nativeDataBuilder = NativeDataBuilder(nativeBasicData);
_customElementsResolutionAnalysis = CustomElementsResolutionAnalysis(
elementEnvironment,
commonElements,
nativeBasicData,
_backendUsageBuilder,
);
_fieldAnalysis = KFieldAnalysis(elementMap);
ClassHierarchyBuilder classHierarchyBuilder = ClassHierarchyBuilder(
commonElements,
elementMap,
);
AnnotationsDataBuilder annotationsDataBuilder = AnnotationsDataBuilder();
// TODO(johnniwinther): This is a hack. The annotation data is built while
// using it. With CFE constants the annotations data can be built fully
// before creating the resolution enqueuer.
AnnotationsData annotationsData = AnnotationsDataImpl(
compiler.options,
compiler.reporter,
annotationsDataBuilder.pragmaAnnotations,
);
InterceptorDataBuilder interceptorDataBuilder = InterceptorDataBuilderImpl(
nativeBasicData,
elementEnvironment,
commonElements,
);
return ResolutionEnqueuer(
task,
compiler.reporter,
ResolutionEnqueuerListener(
compiler.options,
elementEnvironment,
commonElements,
impacts,
nativeBasicData,
interceptorDataBuilder,
_backendUsageBuilder,
noSuchMethodRegistry,
_customElementsResolutionAnalysis,
_nativeResolutionEnqueuer,
_fieldAnalysis,
compiler.deferredLoadTask,
),
ResolutionWorldBuilder(
_options,
elementMap,
elementEnvironment,
_elementMap.types,
commonElements,
nativeBasicData,
nativeDataBuilder,
interceptorDataBuilder,
_backendUsageBuilder,
rtiNeedBuilder,
_fieldAnalysis,
_nativeResolutionEnqueuer,
noSuchMethodRegistry,
annotationsDataBuilder,
const StrongModeWorldStrategy(),
classHierarchyBuilder,
),
KernelWorkItemBuilder(
_compilerTask,
elementMap,
nativeBasicData,
nativeDataBuilder,
annotationsDataBuilder,
closureModels,
compiler.impactCache,
_fieldAnalysis,
_modularStrategy,
_irAnnotationData,
impacts,
_nativeResolutionEnqueuer,
_backendUsageBuilder,
_customElementsResolutionAnalysis,
rtiNeedBuilder,
annotationsData,
),
annotationsData,
);
}
/// Registers a component with this strategy.
void registerComponent(ir.Component component) {
_elementMap.addComponent(component);
}
/// Registers a set of loaded libraries with this strategy.
void registerLoadedLibraries(ir.Component component, List<Uri> libraries) {
registerComponent(component);
_irAnnotationData = processAnnotations(
ModularCore(component, _elementMap.typeEnvironment),
);
_annotationProcessor = KernelAnnotationProcessor(
elementMap,
elementMap.nativeBasicDataBuilder,
_irAnnotationData,
);
for (Uri uri in libraries) {
LibraryEntity library = elementEnvironment.lookupLibrary(uri)!;
if (maybeEnableNative(library.canonicalUri)) {
_annotationProcessor.extractNativeAnnotations(library);
}
_annotationProcessor.extractJsInteropAnnotations(library);
}
}
IrAnnotationData get irAnnotationDataForTesting => _irAnnotationData;
ModularStrategy get modularStrategyForTesting => _modularStrategy;
/// Returns the [ElementEnvironment] for the element model used in this
/// strategy.
KernelElementEnvironment get elementEnvironment =>
_elementMap.elementEnvironment;
/// Returns the [CommonElements] for the element model used in this
/// strategy.
KCommonElements get commonElements => _elementMap.commonElements;
KernelToElementMap get elementMap => _elementMap;
/// Creates a [DeferredLoadTask] for the element model used in this strategy.
DeferredLoadTask createDeferredLoadTask(Compiler compiler) =>
DeferredLoadTask(compiler, _elementMap);
/// Computes the main function from [mainLibrary] adding additional world
/// impact to [impactBuilder].
FunctionEntity? computeMain(WorldImpactBuilder impactBuilder) {
return elementEnvironment.mainFunction;
}
/// Creates a [SourceSpan] from [spannable] in context of [currentElement].
SourceSpan spanFromSpannable(Spannable spannable, Entity? currentElement) {
return _elementMap.getSourceSpan(spannable, currentElement);
}
}
class KernelWorkItemBuilder implements WorkItemBuilder {
final CompilerTask _compilerTask;
final KernelToElementMap _elementMap;
final KernelNativeMemberResolver _nativeMemberResolver;
final AnnotationsDataBuilder _annotationsDataBuilder;
final Map<MemberEntity, ClosureScopeModel> _closureModels;
final Map<Entity, WorldImpact> _impactCache;
final KFieldAnalysis _fieldAnalysis;
final ModularStrategy _modularStrategy;
final IrAnnotationData _irAnnotationData;
final BackendImpacts _impacts;
final NativeResolutionEnqueuer _nativeResolutionEnqueuer;
final BackendUsageBuilder _backendUsageBuilder;
final CustomElementsResolutionAnalysis _customElementsResolutionAnalysis;
final RuntimeTypesNeedBuilder _rtiNeedBuilder;
final AnnotationsData _annotationsData;
KernelWorkItemBuilder(
this._compilerTask,
this._elementMap,
NativeBasicData nativeBasicData,
NativeDataBuilder nativeDataBuilder,
this._annotationsDataBuilder,
this._closureModels,
this._impactCache,
this._fieldAnalysis,
this._modularStrategy,
this._irAnnotationData,
this._impacts,
this._nativeResolutionEnqueuer,
this._backendUsageBuilder,
this._customElementsResolutionAnalysis,
this._rtiNeedBuilder,
this._annotationsData,
) : _nativeMemberResolver = KernelNativeMemberResolver(
_elementMap,
nativeBasicData,
nativeDataBuilder,
);
@override
WorkItem createWorkItem(MemberEntity entity) {
return KernelWorkItem(
_compilerTask,
_elementMap,
_nativeMemberResolver,
_annotationsDataBuilder,
entity,
_closureModels,
_impactCache,
_fieldAnalysis,
_modularStrategy,
_irAnnotationData,
_impacts,
_nativeResolutionEnqueuer,
_backendUsageBuilder,
_customElementsResolutionAnalysis,
_rtiNeedBuilder,
_annotationsData,
);
}
}
class KernelWorkItem implements WorkItem {
final CompilerTask _compilerTask;
final KernelToElementMap _elementMap;
final KernelNativeMemberResolver _nativeMemberResolver;
final AnnotationsDataBuilder _annotationsDataBuilder;
@override
final MemberEntity element;
final Map<MemberEntity, ClosureScopeModel> _closureModels;
final Map<Entity, WorldImpact> _impactCache;
final KFieldAnalysis _fieldAnalysis;
final ModularStrategy _modularStrategy;
final IrAnnotationData _irAnnotationData;
final BackendImpacts _impacts;
final NativeResolutionEnqueuer _nativeResolutionEnqueuer;
final BackendUsageBuilder _backendUsageBuilder;
final CustomElementsResolutionAnalysis _customElementsResolutionAnalysis;
final RuntimeTypesNeedBuilder _rtiNeedBuilder;
final AnnotationsData _annotationsData;
KernelWorkItem(
this._compilerTask,
this._elementMap,
this._nativeMemberResolver,
this._annotationsDataBuilder,
this.element,
this._closureModels,
this._impactCache,
this._fieldAnalysis,
this._modularStrategy,
this._irAnnotationData,
this._impacts,
this._nativeResolutionEnqueuer,
this._backendUsageBuilder,
this._customElementsResolutionAnalysis,
this._rtiNeedBuilder,
this._annotationsData,
);
@override
WorldImpact run() {
return _compilerTask.measure(() {
ir.Member node = _elementMap.getMemberNode(element);
_nativeMemberResolver.resolveNativeMember(node, _irAnnotationData);
List<PragmaAnnotationData> pragmaAnnotationData = _modularStrategy
.getPragmaAnnotationData(node);
EnumSet<PragmaAnnotation> annotations = processMemberAnnotations(
_elementMap.options,
_elementMap.reporter,
node,
pragmaAnnotationData,
);
_annotationsDataBuilder.registerPragmaAnnotations(element, annotations);
// TODO(sra): Replace the above three statements with a single call to a
// new API on AnnotationsData that causes the annotations to be parsed and
// checked.
ModularMemberData modularMemberData = _modularStrategy
.getModularMemberData(node);
ScopeModel scopeModel = modularMemberData.scopeModel;
if (scopeModel.closureScopeModel != null) {
_closureModels[element] = scopeModel.closureScopeModel!;
}
if (element is FieldEntity && !element.isInstanceMember) {
_fieldAnalysis.registerStaticField(
element as JField,
scopeModel.initializerComplexity,
);
}
ImpactBuilderData impactBuilderData = modularMemberData.impactBuilderData;
return _compilerTask.measureSubtask('worldImpact', () {
WorldImpact worldImpact = _elementMap.computeWorldImpact(
element as JMember,
_impacts,
_nativeResolutionEnqueuer,
_backendUsageBuilder,
_customElementsResolutionAnalysis,
_rtiNeedBuilder,
_annotationsData,
impactBuilderData,
);
_impactCache[element] = worldImpact;
return worldImpact;
});
});
}
@override
String toString() => 'KernelWorkItem($element)';
}
class KernelModularStrategy extends ModularStrategy {
final CompilerTask _compilerTask;
final KernelToElementMap _elementMap;
KernelModularStrategy(this._compilerTask, this._elementMap);
@override
List<PragmaAnnotationData> getPragmaAnnotationData(ir.Member node) {
return computePragmaAnnotationDataFromIr(node);
}
@override
ModularMemberData getModularMemberData(ir.Member node) {
ScopeModel scopeModel = _compilerTask.measureSubtask(
'closures',
() => ScopeModel.from(node, _elementMap.typeEnvironment),
);
return _compilerTask.measureSubtask('worldImpact', () {
return computeModularMemberData(_elementMap, node, scopeModel);
});
}
}