5
5
import 'package:_fe_analyzer_shared/src/field_promotability.dart' ;
6
6
import 'package:_fe_analyzer_shared/src/macros/executor.dart' as macro;
7
7
import 'package:analyzer/dart/analysis/features.dart' ;
8
- import 'package:analyzer/dart/analysis/utilities.dart' ;
9
8
import 'package:analyzer/dart/element/element.dart' ;
10
- import 'package:analyzer/dart/element/visitor.dart' ;
11
9
import 'package:analyzer/src/dart/analysis/file_state.dart' as file_state;
12
10
import 'package:analyzer/src/dart/analysis/file_state.dart' hide DirectiveUri;
13
11
import 'package:analyzer/src/dart/analysis/unlinked_data.dart' ;
@@ -17,14 +15,14 @@ import 'package:analyzer/src/dart/element/element.dart';
17
15
import 'package:analyzer/src/dart/element/field_name_non_promotability_info.dart'
18
16
as element_model;
19
17
import 'package:analyzer/src/dart/resolver/scope.dart' ;
20
- import 'package:analyzer/src/generated/source.dart' ;
21
18
import 'package:analyzer/src/summary2/combinator.dart' ;
22
19
import 'package:analyzer/src/summary2/constructor_initializer_resolver.dart' ;
23
20
import 'package:analyzer/src/summary2/default_value_resolver.dart' ;
24
21
import 'package:analyzer/src/summary2/element_builder.dart' ;
25
22
import 'package:analyzer/src/summary2/export.dart' ;
26
23
import 'package:analyzer/src/summary2/link.dart' ;
27
24
import 'package:analyzer/src/summary2/macro_application.dart' ;
25
+ import 'package:analyzer/src/summary2/macro_merge.dart' ;
28
26
import 'package:analyzer/src/summary2/metadata_resolver.dart' ;
29
27
import 'package:analyzer/src/summary2/reference.dart' ;
30
28
import 'package:analyzer/src/summary2/reference_resolver.dart' ;
@@ -193,10 +191,6 @@ class LibraryBuilder {
193
191
required this .units,
194
192
});
195
193
196
- SourceFactory get _sourceFactory {
197
- return linker.elementFactory.analysisContext.sourceFactory;
198
- }
199
-
200
194
void addExporters () {
201
195
final containers = [element, ...element.augmentations];
202
196
for (var containerIndex = 0 ;
@@ -374,79 +368,44 @@ class LibraryBuilder {
374
368
},
375
369
);
376
370
377
- final augmentationLibrary = await macroApplier.executeDeclarationsPhase ();
378
- if (augmentationLibrary == null ) {
371
+ final macroResults = await performance.runAsync (
372
+ 'executeDeclarationsPhase' ,
373
+ (performance) async {
374
+ return await macroApplier.executeDeclarationsPhase ();
375
+ },
376
+ );
377
+ if (macroResults.isEmpty) {
379
378
return ;
380
379
}
380
+ _macroResults.add (macroResults);
381
381
382
- final parseResult = parseString (
383
- content: augmentationLibrary,
384
- featureSet: element.featureSet,
385
- throwIfDiagnostics: false ,
382
+ final augmentationCode = macroApplier.buildAugmentationLibraryCode (
383
+ macroResults,
386
384
);
387
- final unitNode = parseResult.unit as ast.CompilationUnitImpl ;
385
+ if (augmentationCode == null ) {
386
+ return ;
387
+ }
388
388
389
- // We don't actually keep this unit, but we need it for now as a container.
390
- // Eventually we will use actual augmentation libraries.
391
- final unitUri = uri.resolve ('_macro_declarations.dart' );
392
- final unitReference = reference.getChild ('@unit' ).getChild ('$unitUri ' );
393
- final unitElement = CompilationUnitElementImpl (
394
- source: _sourceFactory.forUri2 (unitUri)! ,
395
- librarySource: element.source,
396
- lineInfo: parseResult.lineInfo,
397
- )
398
- ..enclosingElement = element
399
- ..isSynthetic = true
400
- ..uri = unitUri.toString ();
401
-
402
- final elementBuilder = ElementBuilder (
403
- libraryBuilder: this ,
404
- container: element,
405
- unitReference: unitReference,
406
- unitElement: unitElement,
389
+ final importState = kind.addMacroAugmentation (
390
+ augmentationCode,
391
+ addLibraryAugmentDirective: true ,
392
+ partialIndex: _macroResults.length,
407
393
);
408
- elementBuilder.buildDeclarationElements (unitNode);
409
394
410
- // We move elements, so they don't have real offsets.
411
- unitElement.accept (_FlushElementOffsets ());
395
+ final augmentation = _addMacroAugmentation (importState);
396
+
397
+ final macroLinkingUnit = units.last;
398
+ ElementBuilder (
399
+ libraryBuilder: this ,
400
+ container: macroLinkingUnit.container,
401
+ unitReference: macroLinkingUnit.reference,
402
+ unitElement: macroLinkingUnit.element,
403
+ ).buildDeclarationElements (macroLinkingUnit.node);
412
404
413
405
final nodesToBuildType = NodesToBuildType ();
414
- final resolver = ReferenceResolver (linker, nodesToBuildType, element );
415
- unitNode .accept (resolver);
406
+ final resolver = ReferenceResolver (linker, nodesToBuildType, augmentation );
407
+ macroLinkingUnit.node .accept (resolver);
416
408
TypesBuilder (linker).build (nodesToBuildType);
417
-
418
- // Transplant built elements as if the augmentation was applied.
419
- final augmentedUnitElement = element.definingCompilationUnit;
420
- for (final augmentation in unitElement.classes) {
421
- // TODO(scheglov) if augmentation
422
- final augmented = element.getClass (augmentation.name);
423
- if (augmented is ClassElementImpl ) {
424
- augmented .accessors = [
425
- ...augmented .accessors,
426
- ...augmentation.accessors,
427
- ];
428
- augmented .constructors = [
429
- ...augmented .constructors,
430
- ...augmentation.constructors.where ((e) => ! e.isSynthetic),
431
- ];
432
- augmented .fields = [
433
- ...augmented .fields,
434
- ...augmentation.fields,
435
- ];
436
- augmented .methods = [
437
- ...augmented .methods,
438
- ...augmentation.methods,
439
- ];
440
- }
441
- }
442
- augmentedUnitElement.accessors = [
443
- ...augmentedUnitElement.accessors,
444
- ...unitElement.accessors,
445
- ];
446
- augmentedUnitElement.topLevelVariables = [
447
- ...augmentedUnitElement.topLevelVariables,
448
- ...unitElement.topLevelVariables,
449
- ];
450
409
}
451
410
452
411
Future <void > executeMacroTypesPhase ({
@@ -464,16 +423,6 @@ class LibraryBuilder {
464
423
performance: performance,
465
424
);
466
425
}
467
-
468
- // TODO(scheglov) do after all phases
469
- await performance.runAsync (
470
- 'mergeMacroAugmentations' ,
471
- (performance) async {
472
- await mergeMacroAugmentations (
473
- performance: performance,
474
- );
475
- },
476
- );
477
426
}
478
427
479
428
AugmentedInstanceDeclarationBuilder ? getAugmentedBuilder (String name) {
@@ -499,29 +448,75 @@ class LibraryBuilder {
499
448
500
449
kind.disposeMacroAugmentations ();
501
450
451
+ // Remove import for partial macro augmentations.
452
+ element.augmentationImports = element.augmentationImports
453
+ .take (element.augmentationImports.length - _macroResults.length)
454
+ .toFixedList ();
455
+
456
+ // Remove units with partial macro augmentations.
457
+ final partialUnits = units.sublist (units.length - _macroResults.length);
458
+ units.length -= _macroResults.length;
459
+
502
460
final importState = kind.addMacroAugmentation (
503
461
augmentationCode,
504
462
addLibraryAugmentDirective: true ,
505
463
partialIndex: null ,
506
464
);
507
- if (importState == null ) {
508
- return ;
509
- }
465
+ final importedAugmentation = importState.importedAugmentation! ;
466
+ final importedFile = importedAugmentation.file;
510
467
511
- element.augmentationImports = element.augmentationImports
512
- .take (element.augmentationImports.length - _macroResults.length)
513
- .toFixedList ();
468
+ final unitNode = importedFile.parse ();
469
+ final unitElement = CompilationUnitElementImpl (
470
+ source: importedFile.source,
471
+ librarySource: importedFile.source,
472
+ lineInfo: unitNode.lineInfo,
473
+ );
474
+ unitElement.setCodeRange (0 , unitNode.length);
514
475
515
- _addMacroAugmentation (importState);
476
+ final unitReference =
477
+ reference.getChild ('@augmentation' ).getChild (importedFile.uriStr);
478
+ _bindReference (unitReference, unitElement);
516
479
517
- // TODO(scheglov) Apply existing elements from partial augmentations.
518
- final macroLinkingUnit = units.last;
519
- ElementBuilder (
520
- libraryBuilder: this ,
521
- container: macroLinkingUnit.container,
522
- unitReference: macroLinkingUnit.reference,
523
- unitElement: macroLinkingUnit.element,
524
- ).buildDeclarationElements (macroLinkingUnit.node);
480
+ final augmentation = LibraryAugmentationElementImpl (
481
+ augmentationTarget: element,
482
+ nameOffset: importedAugmentation.unlinked.libraryKeywordOffset,
483
+ );
484
+ augmentation.definingCompilationUnit = unitElement;
485
+ augmentation.reference = unitReference;
486
+ augmentation.macroGenerated = MacroGeneratedAugmentationLibrary (
487
+ code: importedFile.content,
488
+ informativeBytes: importedFile.unlinked2.informativeBytes,
489
+ );
490
+
491
+ _buildDirectives (
492
+ kind: importedAugmentation,
493
+ container: augmentation,
494
+ );
495
+
496
+ MacroElementsMerger (
497
+ partialUnits: partialUnits,
498
+ unitReference: unitReference,
499
+ unitNode: unitNode,
500
+ unitElement: unitElement,
501
+ ).perform ();
502
+
503
+ final importUri = DirectiveUriWithAugmentationImpl (
504
+ relativeUriString: importState.uri.relativeUriStr,
505
+ relativeUri: importState.uri.relativeUri,
506
+ source: importedFile.source,
507
+ augmentation: augmentation,
508
+ );
509
+
510
+ final import = AugmentationImportElementImpl (
511
+ importKeywordOffset: importState.unlinked.importKeywordOffset,
512
+ uri: importUri,
513
+ );
514
+ import.isSynthetic = true ;
515
+
516
+ element.augmentationImports = [
517
+ ...element.augmentationImports,
518
+ import,
519
+ ].toFixedList ();
525
520
}
526
521
527
522
void putAugmentedBuilder (
@@ -608,7 +603,7 @@ class LibraryBuilder {
608
603
_augmentationTargets[name] = augmentation;
609
604
}
610
605
611
- LibraryAugmentationElementImpl ? _addMacroAugmentation (
606
+ LibraryAugmentationElementImpl _addMacroAugmentation (
612
607
AugmentationImportWithFile state,
613
608
) {
614
609
final import = _buildAugmentationImport (element, state);
@@ -618,8 +613,8 @@ class LibraryBuilder {
618
613
import,
619
614
].toFixedList ();
620
615
621
- final augmentation = import.importedAugmentation;
622
- augmentation! .macroGenerated = MacroGeneratedAugmentationLibrary (
616
+ final augmentation = import.importedAugmentation! ;
617
+ augmentation.macroGenerated = MacroGeneratedAugmentationLibrary (
623
618
code: state.importedFile.content,
624
619
informativeBytes: state.importedFile.unlinked2.informativeBytes,
625
620
);
@@ -997,9 +992,6 @@ class LibraryBuilder {
997
992
addLibraryAugmentDirective: true ,
998
993
partialIndex: _macroResults.length,
999
994
);
1000
- if (importState == null ) {
1001
- return null ;
1002
- }
1003
995
1004
996
final augmentation = _addMacroAugmentation (importState);
1005
997
@@ -1278,19 +1270,6 @@ class _FieldPromotability extends FieldPromotability<InterfaceElement,
1278
1270
}
1279
1271
}
1280
1272
1281
- class _FlushElementOffsets extends GeneralizingElementVisitor <void > {
1282
- @override
1283
- void visitElement (covariant ElementImpl element) {
1284
- element.isTempAugmentation = true ;
1285
- element.nameOffset = - 1 ;
1286
- if (element is ConstructorElementImpl ) {
1287
- element.periodOffset = null ;
1288
- element.nameEnd = null ;
1289
- }
1290
- super .visitElement (element);
1291
- }
1292
- }
1293
-
1294
1273
extension < T > on T ? {
1295
1274
R ? mapOrNull <R >(R Function (T ) mapper) {
1296
1275
final self = this ;
0 commit comments