Merge MacroClassInstance into _MacroApplication.

Change-Id: I6ee1faad1f28d0bea797fcf39b3f253a7d565c2b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/243525
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2022-05-04 21:11:54 +00:00 committed by Commit Bot
parent a0da1a1dee
commit 7359e2dd7d
2 changed files with 59 additions and 62 deletions

View file

@ -5,7 +5,6 @@
import 'dart:isolate'; import 'dart:isolate';
import 'dart:typed_data'; 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/bootstrap.dart' as macro;
import 'package:_fe_analyzer_shared/src/macros/executor.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' import 'package:_fe_analyzer_shared/src/macros/executor/isolated_executor.dart'
@ -53,19 +52,17 @@ class BundleMacroExecutor {
} }
} }
Future<MacroClassInstance> 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<macro.MacroInstanceIdentifier> instantiate({
required Uri libraryUri, required Uri libraryUri,
required String className, required String className,
required String constructorName, required String constructorName,
required macro.Arguments arguments, required macro.Arguments arguments,
required macro.IdentifierResolver identifierResolver,
required macro.DeclarationKind declarationKind,
required macro.Declaration declaration,
}) async { }) async {
var instanceIdentifier = await macroExecutor.instantiateMacro( return await macroExecutor.instantiateMacro(
libraryUri, className, constructorName, arguments); 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<macro.MacroExecutionResult> 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<macro.MacroExecutionResult> 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 { abstract class MacroFileEntry {
String get content; String get content;

View file

@ -17,7 +17,6 @@ import 'package:analyzer/dart/element/visitor.dart';
import 'package:analyzer/src/dart/element/element.dart'; import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/summary2/library_builder.dart'; import 'package:analyzer/src/summary2/library_builder.dart';
import 'package:analyzer/src/summary2/link.dart'; import 'package:analyzer/src/summary2/link.dart';
import 'package:analyzer/src/summary2/macro.dart';
import 'package:analyzer/src/summary2/macro_application_error.dart'; import 'package:analyzer/src/summary2/macro_application_error.dart';
class LibraryMacroApplier { class LibraryMacroApplier {
@ -26,7 +25,10 @@ class LibraryMacroApplier {
final List<_MacroTarget> _targets = []; final List<_MacroTarget> _targets = [];
final Map<ClassDeclaration, macro.ClassDeclaration> _classDeclarations = {}; final Map<ClassDeclaration, macro.ClassDeclarationImpl> _classDeclarations =
{};
final macro.IdentifierResolver _identifierResolver = _IdentifierResolver();
final macro.TypeResolver _typeResolver = _TypeResolver(); final macro.TypeResolver _typeResolver = _TypeResolver();
@ -48,6 +50,7 @@ class LibraryMacroApplier {
await _buildApplications( await _buildApplications(
targetElement, targetElement,
targetNode.metadata, targetNode.metadata,
macro.DeclarationKind.clazz,
() => getClassDeclaration(targetNode), () => getClassDeclaration(targetNode),
); );
} }
@ -61,11 +64,7 @@ class LibraryMacroApplier {
if (application.shouldExecute(macro.Phase.declarations)) { if (application.shouldExecute(macro.Phase.declarations)) {
await _runWithCatchingExceptions( await _runWithCatchingExceptions(
() async { () async {
final result = final result = await application.executeDeclarationsPhase();
await application.instance.executeDeclarationsPhase(
typeResolver: _typeResolver,
classIntrospector: _classIntrospector,
);
if (result.isNotEmpty) { if (result.isNotEmpty) {
results.add(result); results.add(result);
} }
@ -88,7 +87,7 @@ class LibraryMacroApplier {
if (application.shouldExecute(macro.Phase.types)) { if (application.shouldExecute(macro.Phase.types)) {
await _runWithCatchingExceptions( await _runWithCatchingExceptions(
() async { () async {
final result = await application.instance.executeTypesPhase(); final result = await application.executeTypesPhase();
if (result.isNotEmpty) { if (result.isNotEmpty) {
results.add(result); results.add(result);
} }
@ -106,7 +105,7 @@ class LibraryMacroApplier {
/// TODO(scheglov) Do we need this caching? /// TODO(scheglov) Do we need this caching?
/// Or do we need it only during macro applications creation? /// 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); return _classDeclarations[node] ??= _buildClassDeclaration(node);
} }
@ -115,12 +114,13 @@ class LibraryMacroApplier {
Future<void> _buildApplications( Future<void> _buildApplications(
MacroTargetElement targetElement, MacroTargetElement targetElement,
List<Annotation> annotations, List<Annotation> annotations,
macro.Declaration Function() getDeclaration, macro.DeclarationKind declarationKind,
macro.DeclarationImpl Function() getDeclaration,
) async { ) async {
final applications = <_MacroApplication>[]; final applications = <_MacroApplication>[];
for (var i = 0; i < annotations.length; i++) { for (var i = 0; i < annotations.length; i++) {
Future<MacroClassInstance?> instantiateSingle({ Future<macro.MacroInstanceIdentifier?> instantiateSingle({
required ClassElementImpl macroClass, required ClassElementImpl macroClass,
required String constructorName, required String constructorName,
required ArgumentList argumentsNode, required ArgumentList argumentsNode,
@ -139,9 +139,6 @@ class LibraryMacroApplier {
className: macroClass.name, className: macroClass.name,
constructorName: constructorName, constructorName: constructorName,
arguments: arguments, arguments: arguments,
identifierResolver: _IdentifierResolver(),
declarationKind: macro.DeclarationKind.clazz,
declaration: getDeclaration(),
); );
}, },
annotationIndex: i, annotationIndex: i,
@ -185,7 +182,7 @@ class LibraryMacroApplier {
applications.add( applications.add(
_MacroApplication( _MacroApplication(
annotationIndex: i, annotationIndex: i,
instance: macroInstance, instanceIdentifier: macroInstance,
), ),
); );
} }
@ -194,7 +191,10 @@ class LibraryMacroApplier {
if (applications.isNotEmpty) { if (applications.isNotEmpty) {
_targets.add( _targets.add(
_MacroTarget( _MacroTarget(
applier: this,
element: targetElement, element: targetElement,
declarationKind: declarationKind,
declaration: getDeclaration(),
applications: applications, applications: applications,
), ),
); );
@ -614,25 +614,60 @@ class _IdentifierResolver extends macro.IdentifierResolver {
} }
class _MacroApplication { class _MacroApplication {
late final _MacroTarget target;
final int annotationIndex; final int annotationIndex;
final MacroClassInstance instance; final macro.MacroInstanceIdentifier instanceIdentifier;
_MacroApplication({ _MacroApplication({
required this.annotationIndex, required this.annotationIndex,
required this.instance, required this.instanceIdentifier,
}); });
bool shouldExecute(macro.Phase phase) => instance.shouldExecute(phase); Future<macro.MacroExecutionResult> executeDeclarationsPhase() async {
final applier = target.applier;
final executor = applier.macroExecutor;
return await executor.executeDeclarationsPhase(
instanceIdentifier,
target.declaration,
applier._identifierResolver,
applier._typeResolver,
applier._classIntrospector,
);
}
Future<macro.MacroExecutionResult> 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 { class _MacroTarget {
final LibraryMacroApplier applier;
final MacroTargetElement element; final MacroTargetElement element;
final macro.DeclarationKind declarationKind;
final macro.DeclarationImpl declaration;
final List<_MacroApplication> applications; final List<_MacroApplication> applications;
_MacroTarget({ _MacroTarget({
required this.applier,
required this.element, required this.element,
required this.declarationKind,
required this.declaration,
required this.applications, required this.applications,
}); }) {
for (final application in applications) {
application.target = this;
}
}
} }
class _MacroTargetElementCollector extends GeneralizingElementVisitor<void> { class _MacroTargetElementCollector extends GeneralizingElementVisitor<void> {