From 7359e2dd7d96e0f019c216aab195f388f79084f2 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Wed, 4 May 2022 21:11:54 +0000 Subject: [PATCH] Merge MacroClassInstance into _MacroApplication. Change-Id: I6ee1faad1f28d0bea797fcf39b3f253a7d565c2b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/243525 Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov --- pkg/analyzer/lib/src/summary2/macro.dart | 48 ++---------- .../lib/src/summary2/macro_application.dart | 73 ++++++++++++++----- 2 files changed, 59 insertions(+), 62 deletions(-) diff --git a/pkg/analyzer/lib/src/summary2/macro.dart b/pkg/analyzer/lib/src/summary2/macro.dart index a87f8321760..46f36e9050d 100644 --- a/pkg/analyzer/lib/src/summary2/macro.dart +++ b/pkg/analyzer/lib/src/summary2/macro.dart @@ -5,7 +5,6 @@ import 'dart:isolate'; import 'dart:typed_data'; -import 'package:_fe_analyzer_shared/src/macros/api.dart' as macro; import 'package:_fe_analyzer_shared/src/macros/bootstrap.dart' as macro; import 'package:_fe_analyzer_shared/src/macros/executor.dart' as macro; import 'package:_fe_analyzer_shared/src/macros/executor/isolated_executor.dart' @@ -53,19 +52,17 @@ class BundleMacroExecutor { } } - Future instantiate({ + /// Any macro must be instantiated using this method to guarantee that + /// the corresponding kernel was registered first. Although as it is now, + /// it is still possible to request an unrelated [libraryUri]. + Future instantiate({ required Uri libraryUri, required String className, required String constructorName, required macro.Arguments arguments, - required macro.IdentifierResolver identifierResolver, - required macro.DeclarationKind declarationKind, - required macro.Declaration declaration, }) async { - var instanceIdentifier = await macroExecutor.instantiateMacro( + return await macroExecutor.instantiateMacro( libraryUri, className, constructorName, arguments); - return MacroClassInstance._(this, identifierResolver, instanceIdentifier, - declarationKind, declaration); } } @@ -79,41 +76,6 @@ class MacroClass { }); } -class MacroClassInstance { - final BundleMacroExecutor _bundleExecutor; - final macro.IdentifierResolver _identifierResolver; - final macro.MacroInstanceIdentifier _instanceIdentifier; - final macro.DeclarationKind _declarationKind; - final macro.Declaration _declaration; - - MacroClassInstance._( - this._bundleExecutor, - this._identifierResolver, - this._instanceIdentifier, - this._declarationKind, - this._declaration, - ); - - Future executeDeclarationsPhase({ - required macro.TypeResolver typeResolver, - required macro.ClassIntrospector classIntrospector, - }) async { - macro.MacroExecutor executor = _bundleExecutor.macroExecutor; - return await executor.executeDeclarationsPhase(_instanceIdentifier, - _declaration, _identifierResolver, typeResolver, classIntrospector); - } - - Future executeTypesPhase() async { - macro.MacroExecutor executor = _bundleExecutor.macroExecutor; - return await executor.executeTypesPhase( - _instanceIdentifier, _declaration, _identifierResolver); - } - - bool shouldExecute(macro.Phase phase) { - return _instanceIdentifier.shouldExecute(_declarationKind, phase); - } -} - abstract class MacroFileEntry { String get content; diff --git a/pkg/analyzer/lib/src/summary2/macro_application.dart b/pkg/analyzer/lib/src/summary2/macro_application.dart index 6d759730e86..e3cb33dd0a2 100644 --- a/pkg/analyzer/lib/src/summary2/macro_application.dart +++ b/pkg/analyzer/lib/src/summary2/macro_application.dart @@ -17,7 +17,6 @@ import 'package:analyzer/dart/element/visitor.dart'; import 'package:analyzer/src/dart/element/element.dart'; import 'package:analyzer/src/summary2/library_builder.dart'; import 'package:analyzer/src/summary2/link.dart'; -import 'package:analyzer/src/summary2/macro.dart'; import 'package:analyzer/src/summary2/macro_application_error.dart'; class LibraryMacroApplier { @@ -26,7 +25,10 @@ class LibraryMacroApplier { final List<_MacroTarget> _targets = []; - final Map _classDeclarations = {}; + final Map _classDeclarations = + {}; + + final macro.IdentifierResolver _identifierResolver = _IdentifierResolver(); final macro.TypeResolver _typeResolver = _TypeResolver(); @@ -48,6 +50,7 @@ class LibraryMacroApplier { await _buildApplications( targetElement, targetNode.metadata, + macro.DeclarationKind.clazz, () => getClassDeclaration(targetNode), ); } @@ -61,11 +64,7 @@ class LibraryMacroApplier { if (application.shouldExecute(macro.Phase.declarations)) { await _runWithCatchingExceptions( () async { - final result = - await application.instance.executeDeclarationsPhase( - typeResolver: _typeResolver, - classIntrospector: _classIntrospector, - ); + final result = await application.executeDeclarationsPhase(); if (result.isNotEmpty) { results.add(result); } @@ -88,7 +87,7 @@ class LibraryMacroApplier { if (application.shouldExecute(macro.Phase.types)) { await _runWithCatchingExceptions( () async { - final result = await application.instance.executeTypesPhase(); + final result = await application.executeTypesPhase(); if (result.isNotEmpty) { results.add(result); } @@ -106,7 +105,7 @@ class LibraryMacroApplier { /// TODO(scheglov) Do we need this caching? /// Or do we need it only during macro applications creation? - macro.ClassDeclaration getClassDeclaration(ClassDeclaration node) { + macro.ClassDeclarationImpl getClassDeclaration(ClassDeclaration node) { return _classDeclarations[node] ??= _buildClassDeclaration(node); } @@ -115,12 +114,13 @@ class LibraryMacroApplier { Future _buildApplications( MacroTargetElement targetElement, List annotations, - macro.Declaration Function() getDeclaration, + macro.DeclarationKind declarationKind, + macro.DeclarationImpl Function() getDeclaration, ) async { final applications = <_MacroApplication>[]; for (var i = 0; i < annotations.length; i++) { - Future instantiateSingle({ + Future instantiateSingle({ required ClassElementImpl macroClass, required String constructorName, required ArgumentList argumentsNode, @@ -139,9 +139,6 @@ class LibraryMacroApplier { className: macroClass.name, constructorName: constructorName, arguments: arguments, - identifierResolver: _IdentifierResolver(), - declarationKind: macro.DeclarationKind.clazz, - declaration: getDeclaration(), ); }, annotationIndex: i, @@ -185,7 +182,7 @@ class LibraryMacroApplier { applications.add( _MacroApplication( annotationIndex: i, - instance: macroInstance, + instanceIdentifier: macroInstance, ), ); } @@ -194,7 +191,10 @@ class LibraryMacroApplier { if (applications.isNotEmpty) { _targets.add( _MacroTarget( + applier: this, element: targetElement, + declarationKind: declarationKind, + declaration: getDeclaration(), applications: applications, ), ); @@ -614,25 +614,60 @@ class _IdentifierResolver extends macro.IdentifierResolver { } class _MacroApplication { + late final _MacroTarget target; final int annotationIndex; - final MacroClassInstance instance; + final macro.MacroInstanceIdentifier instanceIdentifier; _MacroApplication({ required this.annotationIndex, - required this.instance, + required this.instanceIdentifier, }); - bool shouldExecute(macro.Phase phase) => instance.shouldExecute(phase); + Future executeDeclarationsPhase() async { + final applier = target.applier; + final executor = applier.macroExecutor; + return await executor.executeDeclarationsPhase( + instanceIdentifier, + target.declaration, + applier._identifierResolver, + applier._typeResolver, + applier._classIntrospector, + ); + } + + Future executeTypesPhase() async { + final applier = target.applier; + final executor = applier.macroExecutor; + return await executor.executeTypesPhase( + instanceIdentifier, + target.declaration, + applier._identifierResolver, + ); + } + + bool shouldExecute(macro.Phase phase) { + return instanceIdentifier.shouldExecute(target.declarationKind, phase); + } } class _MacroTarget { + final LibraryMacroApplier applier; final MacroTargetElement element; + final macro.DeclarationKind declarationKind; + final macro.DeclarationImpl declaration; final List<_MacroApplication> applications; _MacroTarget({ + required this.applier, required this.element, + required this.declarationKind, + required this.declaration, required this.applications, - }); + }) { + for (final application in applications) { + application.target = this; + } + } } class _MacroTargetElementCollector extends GeneralizingElementVisitor {