diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/api/builders.dart b/pkg/_fe_analyzer_shared/lib/src/macros/api/builders.dart index 710567d0cfc..3a5efd0809e 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/api/builders.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/api/builders.dart @@ -8,11 +8,9 @@ part of '../api.dart'; /// as augment existing ones. abstract interface class Builder {} -/// Allows you to resolve arbitrary [Identifier]s. -/// -/// This class will likely disappear entirely once we have a different -/// mechanism. -abstract interface class IdentifierResolver { +/// The interface for all introspection that is allowed during the type phase +/// (and later). +abstract interface class TypePhaseIntrospector { /// Returns an [Identifier] for a top level [name] in [library]. /// /// You should only do this for libraries that are definitely in the @@ -24,7 +22,7 @@ abstract interface class IdentifierResolver { /// The API used by [Macro]s to contribute new type declarations to the /// current library, and get [TypeAnnotation]s from runtime [Type] objects. -abstract interface class TypeBuilder implements Builder, IdentifierResolver { +abstract interface class TypeBuilder implements Builder, TypePhaseIntrospector { /// Adds a new type declaration to the surrounding library. /// /// The [name] must match the name of the new [typeDeclaration] (this does @@ -32,12 +30,10 @@ abstract interface class TypeBuilder implements Builder, IdentifierResolver { void declareType(String name, DeclarationCode typeDeclaration); } -/// The interface used to create [StaticType] instances, which are used to -/// examine type relationships. -/// -/// This API is only available to the declaration and definition phases of -/// macro expansion. -abstract interface class TypeResolver { +/// The interface for all introspection that is allowed during the declaration +/// phase (and later). +abstract interface class DeclarationPhaseIntrospector + implements TypePhaseIntrospector { /// Instantiates a new [StaticType] for a given [type] annotation. /// /// Throws an error if the [type] object contains [Identifier]s which cannot @@ -46,15 +42,7 @@ abstract interface class TypeResolver { /// development cycle. It may be helpful for users if macros provide a best /// effort implementation in that case or handle the error in a useful way. Future resolve(TypeAnnotationCode type); -} -/// The API used to introspect on any [TypeDeclaration] which also has the -/// marker interface [IntrospectableType]. -/// -/// Can also be used to ask for all the types declared in a [Library]. -/// -/// Available in the declaration and definition phases. -abstract interface class TypeIntrospector { /// The values available for [enuum]. /// /// This may be incomplete if additional declaration macros are going to run @@ -89,13 +77,7 @@ abstract interface class TypeIntrospector { /// In the definitions phase, these will be [IntrospectableType]s where /// appropriate (but, for instance, type aliases will not be). Future> typesOf(covariant Library library); -} -/// The interface used by [Macro]s to resolve any [Identifier]s pointing to -/// types to their type declarations. -/// -/// Only available in the declaration and definition phases of macro expansion. -abstract interface class TypeDeclarationResolver { /// Resolves an [identifier] to its [TypeDeclaration]. /// /// If [identifier] does not resolve to a [TypeDeclaration], then an @@ -116,12 +98,7 @@ abstract interface class TypeDeclarationResolver { /// /// Can also be used to do subtype checks on types. abstract interface class DeclarationBuilder - implements - Builder, - IdentifierResolver, - TypeIntrospector, - TypeDeclarationResolver, - TypeResolver { + implements Builder, DeclarationPhaseIntrospector { /// Adds a new regular declaration to the surrounding library. /// /// Note that type declarations are not supported. @@ -142,23 +119,16 @@ abstract interface class EnumDeclarationBuilder void declareEnumValue(DeclarationCode declaration); } -/// The interface used by [Macro]s to get the inferred type for an -/// [OmittedTypeAnnotation]. -/// -/// Only available in the definition phase of macro expansion. -abstract interface class TypeInferrer { +/// The interface for all introspection that is allowed during the definition +/// phase (and later). +abstract interface class DefinitionPhaseIntrospector + implements DeclarationPhaseIntrospector { /// Infers a real type annotation for [omittedType]. /// /// If no type could be inferred, then a type annotation representing the /// dynamic type will be given. Future inferType(covariant OmittedTypeAnnotation omittedType); -} -/// The interface used by [Macro]s to get the list of all declarations in a -/// [Library]. -/// -/// Only available in the definition phase of macro expansion. -abstract interface class LibraryDeclarationsResolver { /// Returns a list of all the [Declaration]s in the given [library]. /// /// Where applicable, these will be introspectable declarations. @@ -169,14 +139,7 @@ abstract interface class LibraryDeclarationsResolver { /// any [TypeAnnotation] into its corresponding [TypeDeclaration], and also /// reflect more deeply on those. abstract interface class DefinitionBuilder - implements - Builder, - IdentifierResolver, - TypeIntrospector, - TypeDeclarationResolver, - TypeInferrer, - TypeResolver, - LibraryDeclarationsResolver {} + implements Builder, DefinitionPhaseIntrospector {} /// The APIs used by [Macro]s that run on library directives, to fill in the /// definitions of any declarations within that library. diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor.dart index 19c0ea55372..9b6247819b6 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/executor.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor.dart @@ -46,7 +46,7 @@ abstract class MacroExecutor { /// /// Throws an exception if there is an error executing the macro. Future executeTypesPhase(MacroInstanceIdentifier macro, - MacroTarget target, IdentifierResolver identifierResolver); + MacroTarget target, TypePhaseIntrospector introspector); /// Runs the declarations phase for [macro] on a given [declaration]. /// @@ -54,10 +54,7 @@ abstract class MacroExecutor { Future executeDeclarationsPhase( MacroInstanceIdentifier macro, MacroTarget target, - IdentifierResolver identifierResolver, - TypeDeclarationResolver typeDeclarationResolver, - TypeResolver typeResolver, - TypeIntrospector typeIntrospector); + DeclarationPhaseIntrospector introspector); /// Runs the definitions phase for [macro] on a given [declaration]. /// @@ -65,12 +62,7 @@ abstract class MacroExecutor { Future executeDefinitionsPhase( MacroInstanceIdentifier macro, MacroTarget target, - IdentifierResolver identifierResolver, - TypeDeclarationResolver typeDeclarationResolver, - TypeResolver typeResolver, - TypeIntrospector typeIntrospector, - TypeInferrer typeInferrer, - LibraryDeclarationsResolver libraryDeclarationsResolver); + DefinitionPhaseIntrospector introspector); /// Combines multiple [MacroExecutionResult]s into a single library /// augmentation file, and returns a [String] representing that file. diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/builder_impls.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/builder_impls.dart index 3cc96ac57ae..a0d61fefe4f 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/builder_impls.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/builder_impls.dart @@ -10,7 +10,7 @@ import '../executor.dart'; import '../api.dart'; import 'response_impls.dart'; -class TypeBuilderBase implements IdentifierResolver { +abstract class TypeBuilderBase implements TypePhaseIntrospector { /// The final result, will be built up over `augment` calls. final Map> _enumValueAugmentations; @@ -23,7 +23,7 @@ class TypeBuilderBase implements IdentifierResolver { /// The final result, will be built up over `augment` calls. final Map> _typeAugmentations; - final IdentifierResolver identifierResolver; + TypePhaseIntrospector get introspector; /// Creates and returns a [MacroExecutionResult] out of the [_augmentations] /// created by this builder. @@ -34,7 +34,7 @@ class TypeBuilderBase implements IdentifierResolver { typeAugmentations: _typeAugmentations, ); - TypeBuilderBase(this.identifierResolver, + TypeBuilderBase( {Map>? parentTypeAugmentations, Map>? parentEnumValueAugmentations, List? parentLibraryAugmentations}) @@ -45,11 +45,14 @@ class TypeBuilderBase implements IdentifierResolver { @override Future resolveIdentifier(Uri library, String identifier) => // ignore: deprecated_member_use_from_same_package - identifierResolver.resolveIdentifier(library, identifier); + introspector.resolveIdentifier(library, identifier); } class TypeBuilderImpl extends TypeBuilderBase implements TypeBuilder { - TypeBuilderImpl(super.identifierResolver); + @override + final TypePhaseIntrospector introspector; + + TypeBuilderImpl(this.introspector); @override void declareType(String name, DeclarationCode typeDeclaration) { @@ -59,57 +62,53 @@ class TypeBuilderImpl extends TypeBuilderBase implements TypeBuilder { } /// Base class for all [DeclarationBuilder]s. -class DeclarationBuilderBase extends TypeBuilderBase - implements TypeIntrospector, TypeDeclarationResolver, TypeResolver { - final TypeIntrospector typeIntrospector; - final TypeDeclarationResolver typeDeclarationResolver; - final TypeResolver typeResolver; +abstract class DeclarationBuilderBase extends TypeBuilderBase + implements DeclarationPhaseIntrospector { + @override + DeclarationPhaseIntrospector get introspector; - DeclarationBuilderBase(super.identifierResolver, this.typeIntrospector, - this.typeDeclarationResolver, this.typeResolver, + DeclarationBuilderBase( {super.parentTypeAugmentations, super.parentEnumValueAugmentations, super.parentLibraryAugmentations}); @override Future declarationOf(IdentifierImpl identifier) => - typeDeclarationResolver.declarationOf(identifier); + introspector.declarationOf(identifier); @override Future> constructorsOf( IntrospectableType type) => - typeIntrospector.constructorsOf(type); + introspector.constructorsOf(type); @override Future> valuesOf( covariant IntrospectableEnumDeclaration enuum) => - typeIntrospector.valuesOf(enuum); + introspector.valuesOf(enuum); @override Future> fieldsOf(IntrospectableType type) => - typeIntrospector.fieldsOf(type); + introspector.fieldsOf(type); @override Future> methodsOf(IntrospectableType type) => - typeIntrospector.methodsOf(type); + introspector.methodsOf(type); @override Future resolve(TypeAnnotationCode code) => - typeResolver.resolve(code); + introspector.resolve(code); @override Future> typesOf(Library library) => - typeIntrospector.typesOf(library); + introspector.typesOf(library); } class DeclarationBuilderImpl extends DeclarationBuilderBase implements DeclarationBuilder { - DeclarationBuilderImpl( - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, - ); + @override + final DeclarationPhaseIntrospector introspector; + + DeclarationBuilderImpl(this.introspector); @override void declareInLibrary(DeclarationCode declaration) { @@ -123,10 +122,7 @@ class MemberDeclarationBuilderImpl extends DeclarationBuilderImpl MemberDeclarationBuilderImpl( this.definingType, - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, + super.introspector, ); @override @@ -140,10 +136,7 @@ class EnumDeclarationBuilderImpl extends MemberDeclarationBuilderImpl implements EnumDeclarationBuilder { EnumDeclarationBuilderImpl( super.definingType, - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, + super.introspector, ); @override @@ -156,18 +149,12 @@ class EnumDeclarationBuilderImpl extends MemberDeclarationBuilderImpl /// Base class for all [DefinitionBuilder]s. class DefinitionBuilderBase extends DeclarationBuilderBase - implements TypeInferrer, LibraryDeclarationsResolver { - final TypeInferrer typeInferrer; - - final LibraryDeclarationsResolver libraryDeclarationsResolver; + implements DefinitionPhaseIntrospector { + @override + final DefinitionPhaseIntrospector introspector; DefinitionBuilderBase( - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, - this.typeInferrer, - this.libraryDeclarationsResolver, { + this.introspector, { super.parentTypeAugmentations, super.parentEnumValueAugmentations, super.parentLibraryAugmentations, @@ -175,11 +162,11 @@ class DefinitionBuilderBase extends DeclarationBuilderBase @override Future inferType(OmittedTypeAnnotationImpl omittedType) => - typeInferrer.inferType(omittedType); + introspector.inferType(omittedType); @override Future> topLevelDeclarationsOf(Library library) => - libraryDeclarationsResolver.topLevelDeclarationsOf(library); + introspector.topLevelDeclarationsOf(library); } class TypeDefinitionBuilderImpl extends DefinitionBuilderBase @@ -189,12 +176,7 @@ class TypeDefinitionBuilderImpl extends DefinitionBuilderBase TypeDefinitionBuilderImpl( this.declaration, - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, - super.typeInferrer, - super.libraryDeclarationsResolver, { + super.introspector, { super.parentTypeAugmentations, super.parentEnumValueAugmentations, super.parentLibraryAugmentations, @@ -203,52 +185,30 @@ class TypeDefinitionBuilderImpl extends DefinitionBuilderBase @override Future buildConstructor( Identifier identifier) async { - ConstructorDeclarationImpl constructor = (await typeIntrospector + ConstructorDeclarationImpl constructor = (await introspector .constructorsOf(declaration)) .firstWhere((constructor) => constructor.identifier == identifier) as ConstructorDeclarationImpl; - return new ConstructorDefinitionBuilderImpl( - constructor, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver, + return new ConstructorDefinitionBuilderImpl(constructor, introspector, parentTypeAugmentations: _typeAugmentations, parentLibraryAugmentations: _libraryAugmentations); } @override Future buildField(Identifier identifier) async { - FieldDeclaration field = (await typeIntrospector.fieldsOf(declaration)) + FieldDeclaration field = (await introspector.fieldsOf(declaration)) .firstWhere((field) => field.identifier == identifier); - return new VariableDefinitionBuilderImpl( - field, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver, + return new VariableDefinitionBuilderImpl(field, introspector, parentTypeAugmentations: _typeAugmentations, parentLibraryAugmentations: _libraryAugmentations); } @override Future buildMethod(Identifier identifier) async { - MethodDeclarationImpl method = - (await typeIntrospector.methodsOf(declaration)) - .firstWhere((method) => method.identifier == identifier) - as MethodDeclarationImpl; - return new FunctionDefinitionBuilderImpl( - method, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver, + MethodDeclarationImpl method = (await introspector.methodsOf(declaration)) + .firstWhere((method) => method.identifier == identifier) + as MethodDeclarationImpl; + return new FunctionDefinitionBuilderImpl(method, introspector, parentTypeAugmentations: _typeAugmentations, parentLibraryAugmentations: _libraryAugmentations); } @@ -262,12 +222,7 @@ class EnumDefinitionBuilderImpl extends TypeDefinitionBuilderImpl EnumDefinitionBuilderImpl( IntrospectableEnumDeclaration super.declaration, - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, - super.typeInferrer, - super.libraryDeclarationsResolver, { + super.introspector, { super.parentTypeAugmentations, super.parentEnumValueAugmentations, super.parentLibraryAugmentations, @@ -276,18 +231,10 @@ class EnumDefinitionBuilderImpl extends TypeDefinitionBuilderImpl @override Future buildEnumValue( Identifier identifier) async { - EnumValueDeclarationImpl entry = - (await typeIntrospector.valuesOf(declaration)) - .firstWhere((entry) => entry.identifier == identifier) - as EnumValueDeclarationImpl; - return new EnumValueDefinitionBuilderImpl( - entry, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver, + EnumValueDeclarationImpl entry = (await introspector.valuesOf(declaration)) + .firstWhere((entry) => entry.identifier == identifier) + as EnumValueDeclarationImpl; + return new EnumValueDefinitionBuilderImpl(entry, introspector, parentTypeAugmentations: _typeAugmentations, parentEnumValueAugmentations: _enumValueAugmentations, parentLibraryAugmentations: _libraryAugmentations); @@ -300,12 +247,7 @@ class EnumValueDefinitionBuilderImpl extends DefinitionBuilderBase EnumValueDefinitionBuilderImpl( this.declaration, - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, - super.typeInferrer, - super.libraryDeclarationsResolver, { + super.introspector, { super.parentTypeAugmentations, super.parentEnumValueAugmentations, super.parentLibraryAugmentations, @@ -326,12 +268,7 @@ class FunctionDefinitionBuilderImpl extends DefinitionBuilderBase FunctionDefinitionBuilderImpl( this.declaration, - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, - super.typeInferrer, - super.libraryDeclarationsResolver, { + super.introspector, { super.parentTypeAugmentations, super.parentEnumValueAugmentations, super.parentLibraryAugmentations, @@ -358,12 +295,7 @@ class ConstructorDefinitionBuilderImpl extends DefinitionBuilderBase ConstructorDefinitionBuilderImpl( this.declaration, - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, - super.typeInferrer, - super.libraryDeclarationsResolver, { + super.introspector, { super.parentTypeAugmentations, super.parentEnumValueAugmentations, super.parentLibraryAugmentations, @@ -391,12 +323,7 @@ class VariableDefinitionBuilderImpl extends DefinitionBuilderBase VariableDefinitionBuilderImpl( this.declaration, - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, - super.typeInferrer, - super.libraryDeclarationsResolver, { + super.introspector, { super.parentTypeAugmentations, super.parentEnumValueAugmentations, super.parentLibraryAugmentations, @@ -431,12 +358,7 @@ class LibraryDefinitionBuilderImpl extends DefinitionBuilderBase LibraryDefinitionBuilderImpl( this.library, - super.identifierResolver, - super.typeIntrospector, - super.typeDeclarationResolver, - super.typeResolver, - super.typeInferrer, - super.libraryDeclarationsResolver, { + super.introspector, { super.parentTypeAugmentations, super.parentEnumValueAugmentations, super.parentLibraryAugmentations, @@ -444,54 +366,33 @@ class LibraryDefinitionBuilderImpl extends DefinitionBuilderBase @override Future buildFunction(Identifier identifier) async { - FunctionDeclarationImpl function = (await libraryDeclarationsResolver + FunctionDeclarationImpl function = (await introspector .topLevelDeclarationsOf(library)) .firstWhere((declaration) => declaration.identifier == identifier) as FunctionDeclarationImpl; - return new FunctionDefinitionBuilderImpl( - function, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver, + return new FunctionDefinitionBuilderImpl(function, introspector, parentTypeAugmentations: _typeAugmentations, parentLibraryAugmentations: _libraryAugmentations); } @override Future buildType(Identifier identifier) async { - IntrospectableType type = (await libraryDeclarationsResolver + IntrospectableType type = (await introspector .topLevelDeclarationsOf(library)) .firstWhere((declaration) => declaration.identifier == identifier) as IntrospectableType; - return new TypeDefinitionBuilderImpl( - type, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver, + return new TypeDefinitionBuilderImpl(type, introspector, parentTypeAugmentations: _typeAugmentations, parentLibraryAugmentations: _libraryAugmentations); } @override Future buildVariable(Identifier identifier) async { - VariableDeclarationImpl variable = (await libraryDeclarationsResolver + VariableDeclarationImpl variable = (await introspector .topLevelDeclarationsOf(library)) .firstWhere((declaration) => declaration.identifier == identifier) as VariableDeclarationImpl; - return new VariableDefinitionBuilderImpl( - variable, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver, + return new VariableDefinitionBuilderImpl(variable, introspector, parentTypeAugmentations: _typeAugmentations, parentLibraryAugmentations: _libraryAugmentations); } diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/client.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/client.dart index 5729417369b..8468d7d6203 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/client.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/client.dart @@ -212,13 +212,13 @@ final class MacroExpansionClient { Macro instance = _macroInstances[request.macro] ?? (throw new StateError('Unrecognized macro instance ${request.macro}\n' 'Known instances: $_macroInstances)')); - ClientIdentifierResolver identifierResolver = - new ClientIdentifierResolver(sendRequest, - remoteInstance: request.identifierResolver, - serializationZoneId: request.serializationZoneId); + TypePhaseIntrospector introspector = new ClientTypePhaseIntrospector( + sendRequest, + remoteInstance: request.introspector, + serializationZoneId: request.serializationZoneId); MacroExecutionResult result = - await executeTypesMacro(instance, request.target, identifierResolver); + await executeTypesMacro(instance, request.target, introspector); return new SerializableResponse( responseType: MessageType.macroExecutionResult, response: result, @@ -242,29 +242,13 @@ final class MacroExpansionClient { (throw new StateError('Unrecognized macro instance ${request.macro}\n' 'Known instances: $_macroInstances)')); - ClientIdentifierResolver identifierResolver = - new ClientIdentifierResolver(sendRequest, - remoteInstance: request.identifierResolver, + DeclarationPhaseIntrospector introspector = + new ClientDeclarationPhaseIntrospector(sendRequest, + remoteInstance: request.introspector, serializationZoneId: request.serializationZoneId); - ClientTypeIntrospector typeIntrospector = new ClientTypeIntrospector( - sendRequest, - remoteInstance: request.typeIntrospector, - serializationZoneId: request.serializationZoneId); - ClientTypeDeclarationResolver typeDeclarationResolver = - new ClientTypeDeclarationResolver(sendRequest, - remoteInstance: request.typeDeclarationResolver, - serializationZoneId: request.serializationZoneId); - ClientTypeResolver typeResolver = new ClientTypeResolver(sendRequest, - remoteInstance: request.typeResolver, - serializationZoneId: request.serializationZoneId); MacroExecutionResult result = await executeDeclarationsMacro( - instance, - request.target, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver); + instance, request.target, introspector); return new SerializableResponse( responseType: MessageType.macroExecutionResult, response: result, @@ -287,38 +271,13 @@ final class MacroExpansionClient { Macro instance = _macroInstances[request.macro] ?? (throw new StateError('Unrecognized macro instance ${request.macro}\n' 'Known instances: $_macroInstances)')); - ClientIdentifierResolver identifierResolver = - new ClientIdentifierResolver(sendRequest, - remoteInstance: request.identifierResolver, - serializationZoneId: request.serializationZoneId); - ClientTypeResolver typeResolver = new ClientTypeResolver(sendRequest, - remoteInstance: request.typeResolver, - serializationZoneId: request.serializationZoneId); - ClientTypeDeclarationResolver typeDeclarationResolver = - new ClientTypeDeclarationResolver(sendRequest, - remoteInstance: request.typeDeclarationResolver, - serializationZoneId: request.serializationZoneId); - ClientTypeIntrospector typeIntrospector = new ClientTypeIntrospector( - sendRequest, - remoteInstance: request.typeIntrospector, - serializationZoneId: request.serializationZoneId); - ClientTypeInferrer typeInferrer = new ClientTypeInferrer(sendRequest, - remoteInstance: request.typeInferrer, - serializationZoneId: request.serializationZoneId); - ClientLibraryDeclarationsResolver libraryDeclarationsResolver = - new ClientLibraryDeclarationsResolver(sendRequest, - remoteInstance: request.libraryDeclarationsResolver, + DefinitionPhaseIntrospector introspector = + new ClientDefinitionPhaseIntrospector(sendRequest, + remoteInstance: request.introspector, serializationZoneId: request.serializationZoneId); - MacroExecutionResult result = await executeDefinitionMacro( - instance, - request.target, - identifierResolver, - typeIntrospector, - typeResolver, - typeDeclarationResolver, - typeInferrer, - libraryDeclarationsResolver); + MacroExecutionResult result = + await executeDefinitionMacro(instance, request.target, introspector); return new SerializableResponse( responseType: MessageType.macroExecutionResult, response: result, diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/execute_macro.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/execute_macro.dart index b93d6a361d4..f05619eb1eb 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/execute_macro.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/execute_macro.dart @@ -9,8 +9,8 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart'; /// Runs [macro] in the types phase and returns a [MacroExecutionResult]. Future executeTypesMacro( - Macro macro, Object target, IdentifierResolver identifierResolver) async { - TypeBuilderImpl builder = new TypeBuilderImpl(identifierResolver); + Macro macro, Object target, TypePhaseIntrospector introspector) async { + TypeBuilderImpl builder = new TypeBuilderImpl(introspector); switch ((target, macro)) { case (Library target, LibraryTypesMacro macro): await macro.buildTypesForLibrary(target, builder); @@ -40,13 +40,8 @@ Future executeTypesMacro( } /// Runs [macro] in the declaration phase and returns a [MacroExecutionResult]. -Future executeDeclarationsMacro( - Macro macro, - Object target, - IdentifierResolver identifierResolver, - TypeIntrospector typeIntrospector, - TypeDeclarationResolver typeDeclarationResolver, - TypeResolver typeResolver) async { +Future executeDeclarationsMacro(Macro macro, + Object target, DeclarationPhaseIntrospector introspector) async { // At most one of these will be used below. late MemberDeclarationBuilderImpl memberBuilder = new MemberDeclarationBuilderImpl( @@ -57,15 +52,9 @@ Future executeDeclarationsMacro( 'Can only create member declaration builders for types or ' 'member declarations, but got $target'), }, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver); - late DeclarationBuilderImpl topLevelBuilder = new DeclarationBuilderImpl( - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver); + introspector); + late DeclarationBuilderImpl topLevelBuilder = + new DeclarationBuilderImpl(introspector); late EnumDeclarationBuilderImpl enumBuilder = new EnumDeclarationBuilderImpl( switch (target) { EnumDeclarationImpl() => target.identifier, @@ -74,10 +63,7 @@ Future executeDeclarationsMacro( 'Can only create enum declaration builders for enum or enum ' 'value declarations, but got $target'), }, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver); + introspector); switch ((target, macro)) { case (Library target, LibraryDeclarationsMacro macro): @@ -133,53 +119,22 @@ Future executeDeclarationsMacro( } /// Runs [macro] in the definition phase and returns a [MacroExecutionResult]. -Future executeDefinitionMacro( - Macro macro, - Object target, - IdentifierResolver identifierResolver, - TypeIntrospector typeIntrospector, - TypeResolver typeResolver, - TypeDeclarationResolver typeDeclarationResolver, - TypeInferrer typeInferrer, - LibraryDeclarationsResolver libraryDeclarationsResolver) async { +Future executeDefinitionMacro(Macro macro, Object target, + DefinitionPhaseIntrospector introspector) async { // At most one of these will be used below. late FunctionDefinitionBuilderImpl functionBuilder = new FunctionDefinitionBuilderImpl( - target as FunctionDeclarationImpl, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver); + target as FunctionDeclarationImpl, introspector); late VariableDefinitionBuilderImpl variableBuilder = new VariableDefinitionBuilderImpl( - target as VariableDeclaration, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver); - late TypeDefinitionBuilderImpl typeBuilder = new TypeDefinitionBuilderImpl( - target as IntrospectableType, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver); + target as VariableDeclaration, introspector); + late TypeDefinitionBuilderImpl typeBuilder = + new TypeDefinitionBuilderImpl(target as IntrospectableType, introspector); switch ((target, macro)) { case (Library target, LibraryDefinitionMacro macro): - LibraryDefinitionBuilderImpl builder = new LibraryDefinitionBuilderImpl( - target, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver); + LibraryDefinitionBuilderImpl builder = + new LibraryDefinitionBuilderImpl(target, introspector); await macro.buildDefinitionForLibrary(target, builder); return builder.result; case (ClassDeclaration target, ClassDefinitionMacro macro): @@ -196,14 +151,8 @@ Future executeDefinitionMacro( 'Enum declarations annotated with a macro should be introspectable ' 'in the definitions phase.'); } - EnumDefinitionBuilderImpl builder = new EnumDefinitionBuilderImpl( - target, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver); + EnumDefinitionBuilderImpl builder = + new EnumDefinitionBuilderImpl(target, introspector); await macro.buildDefinitionForEnum(target, builder); return builder.result; case (MixinDeclaration target, MixinDefinitionMacro macro): @@ -217,25 +166,13 @@ Future executeDefinitionMacro( case (EnumValueDeclaration target, EnumValueDefinitionMacro macro): EnumValueDefinitionBuilderImpl builder = new EnumValueDefinitionBuilderImpl( - target as EnumValueDeclarationImpl, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver); + target as EnumValueDeclarationImpl, introspector); await macro.buildDefinitionForEnumValue(target, builder); return builder.result; case (ConstructorDeclaration target, ConstructorDefinitionMacro macro): ConstructorDefinitionBuilderImpl builder = new ConstructorDefinitionBuilderImpl( - target as ConstructorDeclarationImpl, - identifierResolver, - typeIntrospector, - typeDeclarationResolver, - typeResolver, - typeInferrer, - libraryDeclarationsResolver); + target as ConstructorDeclarationImpl, introspector); await macro.buildDefinitionForConstructor(target, builder); return builder.result; case (MethodDeclaration target, MethodDefinitionMacro macro): diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/executor_base.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/executor_base.dart index ffaf3ffc140..f3aa40ffd7d 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/executor_base.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/executor_base.dart @@ -62,8 +62,8 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { deserializer, zoneId); SerializableResponse response; try { - IdentifierImpl identifier = await (request - .identifierResolver.instance as IdentifierResolver) + IdentifierImpl identifier = await (request.introspector.instance + as TypePhaseIntrospector) // ignore: deprecated_member_use_from_same_package .resolveIdentifier(request.library, request.name) as IdentifierImpl; @@ -87,9 +87,9 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { case MessageType.resolveTypeRequest: ResolveTypeRequest request = new ResolveTypeRequest.deserialize(deserializer, zoneId); - StaticType instance = - await (request.typeResolver.instance as TypeResolver) - .resolve(request.typeAnnotationCode); + StaticType instance = await (request.introspector.instance + as DeclarationPhaseIntrospector) + .resolve(request.typeAnnotationCode); SerializableResponse response = new SerializableResponse( response: new RemoteInstanceImpl( id: RemoteInstance.uniqueId, @@ -109,9 +109,9 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { case MessageType.inferTypeRequest: InferTypeRequest request = new InferTypeRequest.deserialize(deserializer, zoneId); - TypeAnnotationImpl inferredType = - await (request.typeInferrer.instance as TypeInferrer) - .inferType(request.omittedType) as TypeAnnotationImpl; + TypeAnnotationImpl inferredType = await (request + .introspector.instance as DefinitionPhaseIntrospector) + .inferType(request.omittedType) as TypeAnnotationImpl; SerializableResponse response = new SerializableResponse( response: inferredType, requestId: request.id, @@ -156,16 +156,16 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { new DeclarationOfRequest.deserialize(deserializer, zoneId); SerializableResponse response; try { - TypeDeclarationResolver resolver = request - .typeDeclarationResolver - .instance as TypeDeclarationResolver; + DeclarationPhaseIntrospector introspector = request + .introspector.instance as DeclarationPhaseIntrospector; response = new SerializableResponse( requestId: request.id, responseType: MessageType.remoteInstance, - response: (await resolver.declarationOf(request.identifier) - // TODO: Consider refactoring to avoid the need for - // this cast. - as Serializable), + response: + (await introspector.declarationOf(request.identifier) + // TODO: Consider refactoring to avoid the need for + // this cast. + as Serializable), serializationZoneId: zoneId); } on ArgumentError catch (error) { response = new SerializableResponse( @@ -191,12 +191,12 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { TypeIntrospectorRequest request = new TypeIntrospectorRequest.deserialize( deserializer, messageType, zoneId); - TypeIntrospector typeIntrospector = - request.typeIntrospector.instance as TypeIntrospector; + DeclarationPhaseIntrospector introspector = + request.introspector.instance as DeclarationPhaseIntrospector; SerializableResponse response = new SerializableResponse( requestId: request.id, responseType: MessageType.declarationList, - response: new DeclarationList((await typeIntrospector + response: new DeclarationList((await introspector .constructorsOf( request.declaration as IntrospectableType)) // TODO: Consider refactoring to avoid the need for this. @@ -209,14 +209,13 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { case MessageType.topLevelDeclarationsOfRequest: DeclarationsOfRequest request = new DeclarationsOfRequest.deserialize(deserializer, zoneId); - LibraryDeclarationsResolver libraryDeclarationsResolver = request - .libraryDeclarationsResolver - .instance as LibraryDeclarationsResolver; + DefinitionPhaseIntrospector introspector = + request.introspector.instance as DefinitionPhaseIntrospector; SerializableResponse response = new SerializableResponse( requestId: request.id, responseType: MessageType.declarationList, response: new DeclarationList(// force newline - (await libraryDeclarationsResolver + (await introspector .topLevelDeclarationsOf(request.library)) // TODO: Consider refactoring to avoid the need for // this. @@ -230,12 +229,12 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { TypeIntrospectorRequest request = new TypeIntrospectorRequest.deserialize( deserializer, messageType, zoneId); - TypeIntrospector typeIntrospector = - request.typeIntrospector.instance as TypeIntrospector; + DeclarationPhaseIntrospector introspector = + request.introspector.instance as DeclarationPhaseIntrospector; SerializableResponse response = new SerializableResponse( requestId: request.id, responseType: MessageType.declarationList, - response: new DeclarationList((await typeIntrospector + response: new DeclarationList((await introspector .fieldsOf(request.declaration as IntrospectableType)) // TODO: Consider refactoring to avoid the need for this. .cast()), @@ -248,12 +247,12 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { TypeIntrospectorRequest request = new TypeIntrospectorRequest.deserialize( deserializer, messageType, zoneId); - TypeIntrospector typeIntrospector = - request.typeIntrospector.instance as TypeIntrospector; + DeclarationPhaseIntrospector introspector = + request.introspector.instance as DeclarationPhaseIntrospector; SerializableResponse response = new SerializableResponse( requestId: request.id, responseType: MessageType.declarationList, - response: new DeclarationList((await typeIntrospector + response: new DeclarationList((await introspector .methodsOf(request.declaration as IntrospectableType)) // TODO: Consider refactoring to avoid the need for this. .cast()), @@ -266,12 +265,12 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { TypeIntrospectorRequest request = new TypeIntrospectorRequest.deserialize( deserializer, messageType, zoneId); - TypeIntrospector typeIntrospector = - request.typeIntrospector.instance as TypeIntrospector; + DeclarationPhaseIntrospector introspector = + request.introspector.instance as DeclarationPhaseIntrospector; SerializableResponse response = new SerializableResponse( requestId: request.id, responseType: MessageType.declarationList, - response: new DeclarationList((await typeIntrospector + response: new DeclarationList((await introspector .typesOf(request.declaration as Library)) // TODO: Consider refactoring to avoid the need for this. .cast()), @@ -284,12 +283,12 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { TypeIntrospectorRequest request = new TypeIntrospectorRequest.deserialize( deserializer, messageType, zoneId); - TypeIntrospector typeIntrospector = - request.typeIntrospector.instance as TypeIntrospector; + DeclarationPhaseIntrospector introspector = + request.introspector.instance as DeclarationPhaseIntrospector; SerializableResponse response = new SerializableResponse( requestId: request.id, responseType: MessageType.declarationList, - response: new DeclarationList((await typeIntrospector + response: new DeclarationList((await introspector .valuesOf(request.declaration as IntrospectableEnum)) // TODO: Consider refactoring to avoid the need for this. .cast()), @@ -320,80 +319,40 @@ abstract class ExternalMacroExecutorBase extends MacroExecutor { Future executeDeclarationsPhase( MacroInstanceIdentifier macro, MacroTarget target, - IdentifierResolver identifierResolver, - TypeDeclarationResolver typeDeclarationResolver, - TypeResolver typeResolver, - TypeIntrospector typeIntrospector) => + DeclarationPhaseIntrospector introspector) => _sendRequest((zoneId) => new ExecuteDeclarationsPhaseRequest( macro, target as RemoteInstance, new RemoteInstanceImpl( - instance: identifierResolver, + instance: introspector, id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.identifierResolver), - new RemoteInstanceImpl( - instance: typeDeclarationResolver, - id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.typeDeclarationResolver), - new RemoteInstanceImpl( - instance: typeResolver, - id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.typeResolver), - new RemoteInstanceImpl( - instance: typeIntrospector, - id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.typeIntrospector), + kind: RemoteInstanceKind.declarationPhaseIntrospector), serializationZoneId: zoneId)); @override Future executeDefinitionsPhase( MacroInstanceIdentifier macro, MacroTarget target, - IdentifierResolver identifierResolver, - TypeDeclarationResolver typeDeclarationResolver, - TypeResolver typeResolver, - TypeIntrospector typeIntrospector, - TypeInferrer typeInferrer, - LibraryDeclarationsResolver libraryDeclarationsResolver) => + DefinitionPhaseIntrospector introspector) => _sendRequest((zoneId) => new ExecuteDefinitionsPhaseRequest( macro, target as RemoteInstance, new RemoteInstanceImpl( - instance: identifierResolver, + instance: introspector, id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.identifierResolver), - new RemoteInstanceImpl( - instance: typeResolver, - id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.typeResolver), - new RemoteInstanceImpl( - instance: typeIntrospector, - id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.typeIntrospector), - new RemoteInstanceImpl( - instance: typeDeclarationResolver, - id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.typeDeclarationResolver), - new RemoteInstanceImpl( - instance: typeInferrer, - id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.typeInferrer), - new RemoteInstanceImpl( - instance: libraryDeclarationsResolver, - id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.libraryDeclarationsResolver), + kind: RemoteInstanceKind.definitionPhaseIntrospector), serializationZoneId: zoneId)); @override Future executeTypesPhase(MacroInstanceIdentifier macro, - MacroTarget target, IdentifierResolver identifierResolver) => + MacroTarget target, TypePhaseIntrospector introspector) => _sendRequest((zoneId) => new ExecuteTypesPhaseRequest( macro, target as RemoteInstance, new RemoteInstanceImpl( - instance: identifierResolver, + instance: introspector, id: RemoteInstance.uniqueId, - kind: RemoteInstanceKind.identifierResolver), + kind: RemoteInstanceKind.typePhaseIntrospector), serializationZoneId: zoneId)); @override diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/multi_executor.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/multi_executor.dart index bfed060eee0..b2532e9f821 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/multi_executor.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/multi_executor.dart @@ -97,40 +97,23 @@ class MultiMacroExecutor extends MacroExecutor with AugmentationLibraryBuilder { Future executeDeclarationsPhase( MacroInstanceIdentifier macro, MacroTarget target, - IdentifierResolver identifierResolver, - TypeDeclarationResolver typeDeclarationResolver, - TypeResolver typeResolver, - TypeIntrospector typeIntrospector) => + DeclarationPhaseIntrospector introspector) => _instanceExecutors[macro]!._withInstance((executor) => - executor.executeDeclarationsPhase(macro, target, identifierResolver, - typeDeclarationResolver, typeResolver, typeIntrospector)); + executor.executeDeclarationsPhase(macro, target, introspector)); @override Future executeDefinitionsPhase( MacroInstanceIdentifier macro, MacroTarget target, - IdentifierResolver identifierResolver, - TypeDeclarationResolver typeDeclarationResolver, - TypeResolver typeResolver, - TypeIntrospector typeIntrospector, - TypeInferrer typeInferrer, - LibraryDeclarationsResolver libraryDeclarationsResolver) => + DefinitionPhaseIntrospector introspector) => _instanceExecutors[macro]!._withInstance((executor) => - executor.executeDefinitionsPhase( - macro, - target, - identifierResolver, - typeDeclarationResolver, - typeResolver, - typeIntrospector, - typeInferrer, - libraryDeclarationsResolver)); + executor.executeDefinitionsPhase(macro, target, introspector)); @override Future executeTypesPhase(MacroInstanceIdentifier macro, - MacroTarget target, IdentifierResolver identifierResolver) => + MacroTarget target, TypePhaseIntrospector introspector) => _instanceExecutors[macro]!._withInstance((executor) => - executor.executeTypesPhase(macro, target, identifierResolver)); + executor.executeTypesPhase(macro, target, introspector)); @override Future instantiateMacro( diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/protocol.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/protocol.dart index d806cbb2248..385a7c40b8b 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/protocol.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/protocol.dart @@ -275,134 +275,75 @@ class DisposeMacroRequest extends Request { } } -/// A request to execute a macro on a particular declaration in the types phase. -class ExecuteTypesPhaseRequest extends Request { +/// Base class for the requests to execute a macro in a certain phase. +abstract class ExecutePhaseRequest extends Request { final MacroInstanceIdentifier macro; final RemoteInstance target; + final RemoteInstanceImpl introspector; - final RemoteInstanceImpl identifierResolver; + MessageType get kind; - ExecuteTypesPhaseRequest(this.macro, this.target, this.identifierResolver, + ExecutePhaseRequest(this.macro, this.target, this.introspector, {required super.serializationZoneId}); /// When deserializing we have already consumed the message type, so we don't /// consume it again. + ExecutePhaseRequest.deserialize(super.deserializer, super.serializationZoneId) + : macro = new MacroInstanceIdentifierImpl.deserialize(deserializer), + target = RemoteInstance.deserialize(deserializer), + introspector = RemoteInstance.deserialize(deserializer), + super.deserialize(); + + @override + void serialize(Serializer serializer) { + serializer.addInt(kind.index); + macro.serialize(serializer); + target.serialize(serializer); + introspector.serialize(serializer); + + super.serialize(serializer); + } +} + +/// A request to execute a macro on a particular declaration in the types phase. +class ExecuteTypesPhaseRequest extends ExecutePhaseRequest { + @override + MessageType get kind => MessageType.executeTypesPhaseRequest; + + ExecuteTypesPhaseRequest(super.macro, super.target, super.identifierResolver, + {required super.serializationZoneId}); + ExecuteTypesPhaseRequest.deserialize( super.deserializer, super.serializationZoneId) - : macro = new MacroInstanceIdentifierImpl.deserialize(deserializer), - target = RemoteInstance.deserialize(deserializer), - identifierResolver = RemoteInstance.deserialize(deserializer), - super.deserialize(); - - @override - void serialize(Serializer serializer) { - serializer.addInt(MessageType.executeTypesPhaseRequest.index); - macro.serialize(serializer); - target.serialize(serializer); - identifierResolver.serialize(serializer); - - super.serialize(serializer); - } + : super.deserialize(); } -/// A request to execute a macro on a particular declaration in the definition -/// phase. -class ExecuteDeclarationsPhaseRequest extends Request { - final MacroInstanceIdentifier macro; - final RemoteInstance target; - - final RemoteInstanceImpl identifierResolver; - final RemoteInstanceImpl typeDeclarationResolver; - final RemoteInstanceImpl typeResolver; - final RemoteInstanceImpl typeIntrospector; +/// A request to execute a macro on a particular declaration in the types phase. +class ExecuteDeclarationsPhaseRequest extends ExecutePhaseRequest { + @override + MessageType get kind => MessageType.executeDeclarationsPhaseRequest; ExecuteDeclarationsPhaseRequest( - this.macro, - this.target, - this.identifierResolver, - this.typeDeclarationResolver, - this.typeResolver, - this.typeIntrospector, + super.macro, super.target, super.identifierResolver, {required super.serializationZoneId}); - /// When deserializing we have already consumed the message type, so we don't - /// consume it again. ExecuteDeclarationsPhaseRequest.deserialize( super.deserializer, super.serializationZoneId) - : macro = new MacroInstanceIdentifierImpl.deserialize(deserializer), - target = RemoteInstance.deserialize(deserializer), - identifierResolver = RemoteInstance.deserialize(deserializer), - typeDeclarationResolver = RemoteInstance.deserialize(deserializer), - typeResolver = RemoteInstance.deserialize(deserializer), - typeIntrospector = RemoteInstance.deserialize(deserializer), - super.deserialize(); - - @override - void serialize(Serializer serializer) { - serializer.addInt(MessageType.executeDeclarationsPhaseRequest.index); - macro.serialize(serializer); - target.serialize(serializer); - identifierResolver.serialize(serializer); - typeDeclarationResolver.serialize(serializer); - typeResolver.serialize(serializer); - typeIntrospector.serialize(serializer); - - super.serialize(serializer); - } + : super.deserialize(); } -/// A request to execute a macro on a particular declaration in the definition -/// phase. -class ExecuteDefinitionsPhaseRequest extends Request { - final MacroInstanceIdentifier macro; - final RemoteInstance target; - - final RemoteInstanceImpl identifierResolver; - final RemoteInstanceImpl typeResolver; - final RemoteInstanceImpl typeIntrospector; - final RemoteInstanceImpl typeDeclarationResolver; - final RemoteInstanceImpl typeInferrer; - final RemoteInstanceImpl libraryDeclarationsResolver; +/// A request to execute a macro on a particular declaration in the types phase. +class ExecuteDefinitionsPhaseRequest extends ExecutePhaseRequest { + @override + MessageType get kind => MessageType.executeDefinitionsPhaseRequest; ExecuteDefinitionsPhaseRequest( - this.macro, - this.target, - this.identifierResolver, - this.typeResolver, - this.typeIntrospector, - this.typeDeclarationResolver, - this.typeInferrer, - this.libraryDeclarationsResolver, + super.macro, super.target, super.identifierResolver, {required super.serializationZoneId}); - /// When deserializing we have already consumed the message type, so we don't - /// consume it again. ExecuteDefinitionsPhaseRequest.deserialize( super.deserializer, super.serializationZoneId) - : macro = new MacroInstanceIdentifierImpl.deserialize(deserializer), - target = RemoteInstance.deserialize(deserializer), - identifierResolver = RemoteInstance.deserialize(deserializer), - typeResolver = RemoteInstance.deserialize(deserializer), - typeIntrospector = RemoteInstance.deserialize(deserializer), - typeDeclarationResolver = RemoteInstance.deserialize(deserializer), - typeInferrer = RemoteInstance.deserialize(deserializer), - libraryDeclarationsResolver = RemoteInstance.deserialize(deserializer), - super.deserialize(); - - @override - void serialize(Serializer serializer) { - serializer.addInt(MessageType.executeDefinitionsPhaseRequest.index); - macro.serialize(serializer); - target.serialize(serializer); - identifierResolver.serialize(serializer); - typeResolver.serialize(serializer); - typeIntrospector.serialize(serializer); - typeDeclarationResolver.serialize(serializer); - typeInferrer.serialize(serializer); - libraryDeclarationsResolver.serialize(serializer); - - super.serialize(serializer); - } + : super.deserialize(); } /// A request to destroy a remote instance zone by id. @@ -420,23 +361,37 @@ class DestroyRemoteInstanceZoneRequest extends Request { } } +class IntrospectionRequest extends Request { + final RemoteInstanceImpl introspector; + + IntrospectionRequest(this.introspector, {required super.serializationZoneId}); + + IntrospectionRequest.deserialize( + super.deserializer, super.serializationZoneId) + : introspector = RemoteInstance.deserialize(deserializer), + super.deserialize(); + + @override + void serialize(Serializer serializer) { + introspector.serialize(serializer); + super.serialize(serializer); + } +} + /// A request to create a resolved identifier. -class ResolveIdentifierRequest extends Request { +class ResolveIdentifierRequest extends IntrospectionRequest { final Uri library; final String name; - final RemoteInstanceImpl identifierResolver; - /// When deserializing we have already consumed the message type, so we don't /// consume it again. - ResolveIdentifierRequest(this.library, this.name, this.identifierResolver, + ResolveIdentifierRequest(this.library, this.name, super.introspector, {required super.serializationZoneId}); ResolveIdentifierRequest.deserialize( super.deserializer, super.serializationZoneId) : library = Uri.parse((deserializer..moveNext()).expectString()), name = (deserializer..moveNext()).expectString(), - identifierResolver = RemoteInstance.deserialize(deserializer), super.deserialize(); @override @@ -445,32 +400,28 @@ class ResolveIdentifierRequest extends Request { ..addInt(MessageType.resolveIdentifierRequest.index) ..addString(library.toString()) ..addString(name); - identifierResolver.serialize(serializer); super.serialize(serializer); } } /// A request to resolve on a type annotation code object -class ResolveTypeRequest extends Request { +class ResolveTypeRequest extends IntrospectionRequest { final TypeAnnotationCode typeAnnotationCode; - final RemoteInstanceImpl typeResolver; - ResolveTypeRequest(this.typeAnnotationCode, this.typeResolver, + ResolveTypeRequest(this.typeAnnotationCode, super.introspector, {required super.serializationZoneId}); /// When deserializing we have already consumed the message type, so we don't /// consume it again. ResolveTypeRequest.deserialize(super.deserializer, super.serializationZoneId) : typeAnnotationCode = (deserializer..moveNext()).expectCode(), - typeResolver = RemoteInstance.deserialize(deserializer), super.deserialize(); @override void serialize(Serializer serializer) { serializer.addInt(MessageType.resolveTypeRequest.index); typeAnnotationCode.serialize(serializer); - typeResolver.serialize(serializer); super.serialize(serializer); } } @@ -525,14 +476,13 @@ class IsSubtypeOfRequest extends Request { } /// A general request class for all requests coming from methods on the -/// [TypeIntrospector] interface. -class TypeIntrospectorRequest extends Request { +/// [DeclarationPhaseIntrospector] interface that are related to a single type. +class TypeIntrospectorRequest extends IntrospectionRequest { final Object declaration; - final RemoteInstanceImpl typeIntrospector; final MessageType requestKind; TypeIntrospectorRequest( - this.declaration, this.typeIntrospector, this.requestKind, + this.declaration, super.introspector, this.requestKind, {required super.serializationZoneId}); /// When deserializing we have already consumed the message type, so we don't @@ -540,24 +490,21 @@ class TypeIntrospectorRequest extends Request { TypeIntrospectorRequest.deserialize( Deserializer deserializer, this.requestKind, int serializationZoneId) : declaration = RemoteInstance.deserialize(deserializer), - typeIntrospector = RemoteInstance.deserialize(deserializer), super.deserialize(deserializer, serializationZoneId); @override void serialize(Serializer serializer) { serializer.addInt(requestKind.index); (declaration as Serializable).serialize(serializer); - typeIntrospector.serialize(serializer); super.serialize(serializer); } } /// A request to get a [TypeDeclaration] for a [StaticType]. -class DeclarationOfRequest extends Request { +class DeclarationOfRequest extends IntrospectionRequest { final IdentifierImpl identifier; - final RemoteInstanceImpl typeDeclarationResolver; - DeclarationOfRequest(this.identifier, this.typeDeclarationResolver, + DeclarationOfRequest(this.identifier, super.introspector, {required super.serializationZoneId}); /// When deserializing we have already consumed the message type, so we don't @@ -565,49 +512,43 @@ class DeclarationOfRequest extends Request { DeclarationOfRequest.deserialize( super.deserializer, super.serializationZoneId) : identifier = RemoteInstance.deserialize(deserializer), - typeDeclarationResolver = RemoteInstance.deserialize(deserializer), super.deserialize(); @override void serialize(Serializer serializer) { serializer.addInt(MessageType.declarationOfRequest.index); identifier.serialize(serializer); - typeDeclarationResolver.serialize(serializer); super.serialize(serializer); } } /// A request to get an inferred [TypeAnnotation] for an /// [OmittedTypeAnnotation]. -class InferTypeRequest extends Request { +class InferTypeRequest extends IntrospectionRequest { final OmittedTypeAnnotationImpl omittedType; - final RemoteInstanceImpl typeInferrer; - InferTypeRequest(this.omittedType, this.typeInferrer, + InferTypeRequest(this.omittedType, super.introspector, {required super.serializationZoneId}); /// When deserializing we have already consumed the message type, so we don't /// consume it again. InferTypeRequest.deserialize(super.deserializer, super.serializationZoneId) : omittedType = RemoteInstance.deserialize(deserializer), - typeInferrer = RemoteInstance.deserialize(deserializer), super.deserialize(); @override void serialize(Serializer serializer) { serializer.addInt(MessageType.inferTypeRequest.index); omittedType.serialize(serializer); - typeInferrer.serialize(serializer); super.serialize(serializer); } } /// A request to get all the top level [Declaration]s in a [Library]. -class DeclarationsOfRequest extends Request { +class DeclarationsOfRequest extends IntrospectionRequest { final LibraryImpl library; - final RemoteInstanceImpl libraryDeclarationsResolver; - DeclarationsOfRequest(this.library, this.libraryDeclarationsResolver, + DeclarationsOfRequest(this.library, super.introspector, {required super.serializationZoneId}); /// When deserializing we have already consumed the message type, so we don't @@ -615,34 +556,41 @@ class DeclarationsOfRequest extends Request { DeclarationsOfRequest.deserialize( super.deserializer, super.serializationZoneId) : library = RemoteInstance.deserialize(deserializer), - libraryDeclarationsResolver = RemoteInstance.deserialize(deserializer), super.deserialize(); @override void serialize(Serializer serializer) { serializer.addInt(MessageType.topLevelDeclarationsOfRequest.index); library.serialize(serializer); - libraryDeclarationsResolver.serialize(serializer); super.serialize(serializer); } } -/// Client side implementation of an [IdentifierResolver], which creates a -/// [ResolveIdentifierRequest] and passes it to a given [_sendRequest] function -/// which can return the [Response]. -class ClientIdentifierResolver implements IdentifierResolver { - /// The actual remote instance of this type resolver. +/// The base class for the client side introspectors from any phase, as well as +/// client side [StaticType]s. +/// +/// These convert all method calls into RPCs, sent via [_sendRequest]. +base class ClientIntrospector { + /// The actual remote instance to call methods on. final RemoteInstanceImpl remoteInstance; - /// The ID of the zone in which to find the original type resolver. + /// The ID of the zone in which to find the original builder. final int serializationZoneId; /// A function that can send a request and return a response using an /// arbitrary communication channel. final Future Function(Request request) _sendRequest; - ClientIdentifierResolver(this._sendRequest, + ClientIntrospector(this._sendRequest, {required this.remoteInstance, required this.serializationZoneId}); +} + +/// Client side implementation of an [TypeBuilder], which creates converts all +/// method calls to remote procedure calls and sends them using [_sendRequest]. +final class ClientTypePhaseIntrospector extends ClientIntrospector + implements TypePhaseIntrospector { + ClientTypePhaseIntrospector(super._sendRequest, + {required super.remoteInstance, required super.serializationZoneId}); @override Future resolveIdentifier(Uri library, String name) async { @@ -653,22 +601,12 @@ class ClientIdentifierResolver implements IdentifierResolver { } } -/// Client side implementation of a [TypeResolver], which creates a -/// [ResolveTypeRequest] and passes it to a given [sendRequest] function which -/// can return the [Response]. -class ClientTypeResolver implements TypeResolver { - /// The actual remote instance of this type resolver. - final RemoteInstanceImpl remoteInstance; - - /// The ID of the zone in which to find the original type resolver. - final int serializationZoneId; - - /// A function that can send a request and return a response using an - /// arbitrary communication channel. - final Future Function(Request request) _sendRequest; - - ClientTypeResolver(this._sendRequest, - {required this.remoteInstance, required this.serializationZoneId}); +/// Client side implementation of a [DeclarationBuilder]. +final class ClientDeclarationPhaseIntrospector + extends ClientTypePhaseIntrospector + implements DeclarationPhaseIntrospector { + ClientDeclarationPhaseIntrospector(super._sendRequest, + {required super.remoteInstance, required super.serializationZoneId}); @override Future resolve(TypeAnnotationCode typeAnnotation) async { @@ -689,60 +627,6 @@ class ClientTypeResolver implements TypeResolver { '${remoteType.kind}'), }; } -} - -class ClientStaticTypeImpl implements StaticType { - /// The actual remote instance of this static type. - final RemoteInstanceImpl remoteInstance; - - final int serializationZoneId; - - /// A function that can send a request and return a response using an - /// arbitrary communication channel. - final Future Function(Request request) sendRequest; - - ClientStaticTypeImpl(this.sendRequest, - {required this.remoteInstance, required this.serializationZoneId}); - - @override - Future isExactly(ClientStaticTypeImpl other) async { - IsExactlyTypeRequest request = new IsExactlyTypeRequest( - this.remoteInstance, other.remoteInstance, - serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)).value; - } - - @override - Future isSubtypeOf(ClientStaticTypeImpl other) async { - IsSubtypeOfRequest request = new IsSubtypeOfRequest( - remoteInstance, other.remoteInstance, - serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)).value; - } -} - -/// Named variant of the [ClientStaticTypeImpl]. -class ClientNamedStaticTypeImpl extends ClientStaticTypeImpl - implements NamedStaticType { - ClientNamedStaticTypeImpl(super.sendRequest, - {required super.remoteInstance, required super.serializationZoneId}); -} - -/// Client side implementation of the [ClientTypeIntrospector], converts -/// all invocations into remote RPC calls. -class ClientTypeIntrospector implements TypeIntrospector { - /// The actual remote instance of this class introspector. - final RemoteInstanceImpl remoteInstance; - - /// The ID of the zone in which to find the original type resolver. - final int serializationZoneId; - - /// A function that can send a request and return a response using an - /// arbitrary communication channel. - final Future Function(Request request) sendRequest; - - ClientTypeIntrospector(this.sendRequest, - {required this.remoteInstance, required this.serializationZoneId}); @override Future> constructorsOf( @@ -750,7 +634,7 @@ class ClientTypeIntrospector implements TypeIntrospector { TypeIntrospectorRequest request = new TypeIntrospectorRequest( type, remoteInstance, MessageType.constructorsOfRequest, serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)) + return _handleResponse(await _sendRequest(request)) .declarations // TODO: Refactor so we can remove this cast .cast(); @@ -762,7 +646,7 @@ class ClientTypeIntrospector implements TypeIntrospector { TypeIntrospectorRequest request = new TypeIntrospectorRequest( enumType, remoteInstance, MessageType.valuesOfRequest, serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)) + return _handleResponse(await _sendRequest(request)) .declarations // TODO: Refactor so we can remove this cast .cast(); @@ -773,7 +657,7 @@ class ClientTypeIntrospector implements TypeIntrospector { TypeIntrospectorRequest request = new TypeIntrospectorRequest( type, remoteInstance, MessageType.fieldsOfRequest, serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)) + return _handleResponse(await _sendRequest(request)) .declarations // TODO: Refactor so we can remove this cast .cast(); @@ -784,7 +668,7 @@ class ClientTypeIntrospector implements TypeIntrospector { TypeIntrospectorRequest request = new TypeIntrospectorRequest( type, remoteInstance, MessageType.methodsOfRequest, serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)) + return _handleResponse(await _sendRequest(request)) .declarations // TODO: Refactor so we can remove this cast .cast(); @@ -795,85 +679,72 @@ class ClientTypeIntrospector implements TypeIntrospector { TypeIntrospectorRequest request = new TypeIntrospectorRequest( library, remoteInstance, MessageType.typesOfRequest, serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)) + return _handleResponse(await _sendRequest(request)) .declarations // TODO: Refactor so we can remove this cast. .cast(); } -} - -/// Client side implementation of a [TypeDeclarationResolver], converts all -/// invocations into remote procedure calls. -class ClientTypeDeclarationResolver implements TypeDeclarationResolver { - /// The actual remote instance of this type resolver. - final RemoteInstanceImpl remoteInstance; - - /// The ID of the zone in which to find the original type resolver. - final int serializationZoneId; - - /// A function that can send a request and return a response using an - /// arbitrary communication channel. - final Future Function(Request request) sendRequest; - - ClientTypeDeclarationResolver(this.sendRequest, - {required this.remoteInstance, required this.serializationZoneId}); @override Future declarationOf(IdentifierImpl identifier) async { DeclarationOfRequest request = new DeclarationOfRequest( identifier, remoteInstance, serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)); + return _handleResponse(await _sendRequest(request)); } } -/// Client side implementation of a [TypeInferrer], converts all -/// invocations into remote procedure calls. -class ClientTypeInferrer implements TypeInferrer { - /// The actual remote instance of this type resolver. - final RemoteInstanceImpl remoteInstance; +/// Client side implementation of a [StaticType]. +base class ClientStaticTypeImpl extends ClientIntrospector + implements StaticType { + ClientStaticTypeImpl(super._sendRequest, + {required super.remoteInstance, required super.serializationZoneId}); - /// The ID of the zone in which to find the original type resolver. - final int serializationZoneId; + @override + Future isExactly(ClientStaticTypeImpl other) async { + IsExactlyTypeRequest request = new IsExactlyTypeRequest( + this.remoteInstance, other.remoteInstance, + serializationZoneId: serializationZoneId); + return _handleResponse(await _sendRequest(request)).value; + } - /// A function that can send a request and return a response using an - /// arbitrary communication channel. - final Future Function(Request request) sendRequest; + @override + Future isSubtypeOf(ClientStaticTypeImpl other) async { + IsSubtypeOfRequest request = new IsSubtypeOfRequest( + remoteInstance, other.remoteInstance, + serializationZoneId: serializationZoneId); + return _handleResponse(await _sendRequest(request)).value; + } +} - ClientTypeInferrer(this.sendRequest, - {required this.remoteInstance, required this.serializationZoneId}); +/// Named variant of the [ClientStaticTypeImpl]. +final class ClientNamedStaticTypeImpl extends ClientStaticTypeImpl + implements NamedStaticType { + ClientNamedStaticTypeImpl(super.sendRequest, + {required super.remoteInstance, required super.serializationZoneId}); +} + +/// Client side implementation of a [DeclarationBuilder]. +final class ClientDefinitionPhaseIntrospector + extends ClientDeclarationPhaseIntrospector + implements DefinitionPhaseIntrospector { + ClientDefinitionPhaseIntrospector(super._sendRequest, + {required super.remoteInstance, required super.serializationZoneId}); @override Future inferType( OmittedTypeAnnotationImpl omittedType) async { InferTypeRequest request = new InferTypeRequest(omittedType, remoteInstance, serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)); + return _handleResponse(await _sendRequest(request)); } -} - -/// Client side implementation of a [LibraryDeclarationsResolver], converts all -/// invocations into remote procedure calls. -class ClientLibraryDeclarationsResolver implements LibraryDeclarationsResolver { - /// The actual remote instance of this library declarations resolver. - final RemoteInstanceImpl remoteInstance; - - /// The ID of the zone in which to find the original type resolver. - final int serializationZoneId; - - /// A function that can send a request and return a response using an - /// arbitrary communication channel. - final Future Function(Request request) sendRequest; - - ClientLibraryDeclarationsResolver(this.sendRequest, - {required this.remoteInstance, required this.serializationZoneId}); @override Future> topLevelDeclarationsOf(LibraryImpl library) async { DeclarationsOfRequest request = new DeclarationsOfRequest( library, remoteInstance, serializationZoneId: serializationZoneId); - return _handleResponse(await sendRequest(request)) + return _handleResponse(await _sendRequest(request)) .declarations; } } diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/remote_instance.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/remote_instance.dart index 871b2112efc..566ae3ed7e7 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/remote_instance.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/remote_instance.dart @@ -101,6 +101,8 @@ class RemoteInstanceImpl extends RemoteInstance { enum RemoteInstanceKind { classDeclaration, constructorDeclaration, + declarationPhaseIntrospector, + definitionPhaseIntrospector, constructorMetadataAnnotation, enumDeclaration, enumValueDeclaration, @@ -110,12 +112,10 @@ enum RemoteInstanceKind { functionTypeParameter, identifier, identifierMetadataAnnotation, - identifierResolver, introspectableClassDeclaration, introspectableEnumDeclaration, introspectableMixinDeclaration, library, - libraryDeclarationsResolver, methodDeclaration, mixinDeclaration, namedStaticType, @@ -126,11 +126,8 @@ enum RemoteInstanceKind { recordTypeAnnotation, staticType, typeAliasDeclaration, - typeDeclarationResolver, + typePhaseIntrospector, typeParameterDeclaration, - typeResolver, - typeInferrer, - typeIntrospector, variableDeclaration, } diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization_extensions.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization_extensions.dart index 6f500dc45e5..de5c364c701 100644 --- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization_extensions.dart +++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization_extensions.dart @@ -17,14 +17,11 @@ extension DeserializerExtensions on Deserializer { moveNext(); RemoteInstanceKind kind = RemoteInstanceKind.values[expectInt()]; final RemoteInstance instance = switch (kind) { - RemoteInstanceKind.typeIntrospector || - RemoteInstanceKind.identifierResolver || - RemoteInstanceKind.libraryDeclarationsResolver || + RemoteInstanceKind.declarationPhaseIntrospector || + RemoteInstanceKind.definitionPhaseIntrospector || + RemoteInstanceKind.typePhaseIntrospector || RemoteInstanceKind.namedStaticType || - RemoteInstanceKind.staticType || - RemoteInstanceKind.typeDeclarationResolver || - RemoteInstanceKind.typeResolver || - RemoteInstanceKind.typeInferrer => + RemoteInstanceKind.staticType => // These are simple wrappers, just pass in the kind new RemoteInstanceImpl(id: id, kind: kind), RemoteInstanceKind.classDeclaration => diff --git a/pkg/_fe_analyzer_shared/test/macros/api/api_test_expectations.dart b/pkg/_fe_analyzer_shared/test/macros/api/api_test_expectations.dart index 56d56385f54..4aae7baf5a9 100644 --- a/pkg/_fe_analyzer_shared/test/macros/api/api_test_expectations.dart +++ b/pkg/_fe_analyzer_shared/test/macros/api/api_test_expectations.dart @@ -117,31 +117,30 @@ void checkParameterDeclaration( } Future checkClassDeclaration(ClassDeclaration declaration, - {TypeDeclarationResolver? typeDeclarationResolver, - TypeIntrospector? typeIntrospector}) async { + {DeclarationPhaseIntrospector? introspector}) async { String name = declaration.identifier.name; ClassData? expected = expectedClassData[name]; if (expected != null) { expect(expected.isAbstract, declaration.hasAbstract, '$name.isAbstract'); expect(expected.isExternal, declaration.hasExternal, '$name.isExternal'); - if (typeDeclarationResolver != null) { + if (introspector != null) { TypeDeclaration? superclass = declaration.superclass == null ? null - : await typeDeclarationResolver + : await introspector .declarationOf(declaration.superclass!.identifier); expect( expected.superclass, superclass?.identifier.name, '$name.superclass'); if (superclass is ClassDeclaration) { TypeDeclaration? superSuperclass = superclass.superclass == null ? null - : await typeDeclarationResolver + : await introspector .declarationOf(superclass.superclass!.identifier); expect(expected.superSuperclass, superSuperclass?.identifier.name, '$name.superSuperclass'); } List mixins = [ for (NamedTypeAnnotation mixin in declaration.mixins) - await typeDeclarationResolver.declarationOf(mixin.identifier), + await introspector.declarationOf(mixin.identifier), ]; expect(expected.mixins.length, mixins.length, '$name.mixins.length'); for (int i = 0; i < mixins.length; i++) { @@ -151,7 +150,7 @@ Future checkClassDeclaration(ClassDeclaration declaration, List interfaces = [ for (NamedTypeAnnotation interface in declaration.interfaces) - await typeDeclarationResolver.declarationOf(interface.identifier), + await introspector.declarationOf(interface.identifier), ]; expect(expected.interfaces.length, interfaces.length, '$name.interfaces.length'); @@ -160,10 +159,9 @@ Future checkClassDeclaration(ClassDeclaration declaration, '$name.interfaces[$i]'); } } - if (typeIntrospector != null && - declaration is IntrospectableClassDeclaration) { + if (introspector != null && declaration is IntrospectableClassDeclaration) { List fieldsOf = - await typeIntrospector.fieldsOf(declaration); + await introspector.fieldsOf(declaration); expect( expected.fieldsOf.length, fieldsOf.length, '$name.fieldsOf.length'); for (int i = 0; i < fieldsOf.length; i++) { @@ -172,7 +170,7 @@ Future checkClassDeclaration(ClassDeclaration declaration, } List methodsOf = - await typeIntrospector.methodsOf(declaration); + await introspector.methodsOf(declaration); expect(expected.methodsOf.length, methodsOf.length, '$name.methodsOf.length'); for (int i = 0; i < methodsOf.length; i++) { @@ -181,7 +179,7 @@ Future checkClassDeclaration(ClassDeclaration declaration, } List constructorsOf = - await typeIntrospector.constructorsOf(declaration); + await introspector.constructorsOf(declaration); expect(expected.constructorsOf.length, constructorsOf.length, '$name.constructorsOf.length'); for (int i = 0; i < constructorsOf.length; i++) { @@ -228,18 +226,17 @@ void checkFunctionDeclaration(FunctionDeclaration actual) { } } -Future checkIdentifierResolver( - IdentifierResolver identifierResolver) async { +Future checkIdentifierResolver(TypePhaseIntrospector introspector) async { Uri dartCore = Uri.parse('dart:core'); Uri macroApiData = Uri.parse('package:macro_api_test/api_test_data.dart'); Future check(Uri uri, String name, {bool expectThrows = false}) async { if (expectThrows) { await throws(() async { - await identifierResolver.resolveIdentifier(uri, name); + await introspector.resolveIdentifier(uri, name); }, '$name from $uri'); } else { - Identifier result = await identifierResolver.resolveIdentifier(uri, name); + Identifier result = await introspector.resolveIdentifier(uri, name); expect(name, result.name, '$name from $uri'); } } @@ -260,20 +257,19 @@ Future checkIdentifierResolver( } Future checkTypeDeclarationResolver( - TypeDeclarationResolver typeDeclarationResolver, + DeclarationPhaseIntrospector introspector, Map test) async { Future check(Identifier identifier, String name, {bool expectThrows = false}) async { if (expectThrows) { await throws(() async { - await typeDeclarationResolver.declarationOf(identifier); + await introspector.declarationOf(identifier); }, '$name from $identifier', expectedError: (e) => e is! ArgumentError ? 'Expected ArgumentError, got ${e.runtimeType}: $e' : null); } else { - TypeDeclaration result = - await typeDeclarationResolver.declarationOf(identifier); + TypeDeclaration result = await introspector.declarationOf(identifier); expect(name, result.identifier.name, '$name from $identifier'); } } diff --git a/pkg/_fe_analyzer_shared/test/macros/api/api_test_macro.dart b/pkg/_fe_analyzer_shared/test/macros/api/api_test_macro.dart index 1de757247dd..452a232e602 100644 --- a/pkg/_fe_analyzer_shared/test/macros/api/api_test_macro.dart +++ b/pkg/_fe_analyzer_shared/test/macros/api/api_test_macro.dart @@ -20,12 +20,12 @@ class ClassMacro FutureOr buildDeclarationsForClass( ClassDeclaration clazz, MemberDeclarationBuilder builder) async { await checkClassDeclaration( - clazz, typeDeclarationResolver: builder, typeIntrospector: builder); + clazz, introspector: builder); } FutureOr buildDefinitionForClass( ClassDeclaration clazz, TypeDefinitionBuilder builder) async { - await checkClassDeclaration(clazz, typeIntrospector: builder); + await checkClassDeclaration(clazz, introspector: builder); await checkIdentifierResolver(builder); await checkTypeDeclarationResolver(builder, {clazz.identifier: clazz.identifier.name}); diff --git a/pkg/_fe_analyzer_shared/test/macros/api/package_config.json b/pkg/_fe_analyzer_shared/test/macros/api/package_config.json index 60374f3449e..d28731a093e 100644 --- a/pkg/_fe_analyzer_shared/test/macros/api/package_config.json +++ b/pkg/_fe_analyzer_shared/test/macros/api/package_config.json @@ -1,6 +1,10 @@ { "configVersion": 2, "packages": [ + { + "name": "dart_internal", + "rootUri": "../../../../dart_internal/" + }, { "name": "macro_api_test", "rootUri": "../../../../_fe_analyzer_shared/test/macros/api/" diff --git a/pkg/_fe_analyzer_shared/test/macros/executor/executor_test.dart b/pkg/_fe_analyzer_shared/test/macros/executor/executor_test.dart index 2dbb74958d2..8f755cbf3f8 100644 --- a/pkg/_fe_analyzer_shared/test/macros/executor/executor_test.dart +++ b/pkg/_fe_analyzer_shared/test/macros/executor/executor_test.dart @@ -132,8 +132,8 @@ void main() { tearDownAll(() async { executor.disposeMacro(instanceId); await expectLater( - () => executor.executeTypesPhase( - instanceId, Fixtures.myFunction, FakeIdentifierResolver()), + () => executor.executeTypesPhase(instanceId, + Fixtures.myFunction, TestTypePhaseIntrospector()), throwsA(isA().having((e) => e.error, 'error', contains('Unrecognized macro instance'))), reason: 'Should be able to dispose macro instances'); @@ -149,8 +149,8 @@ void main() { group('run macros', () { group('in the types phase', () { test('on functions', () async { - var result = await executor.executeTypesPhase( - instanceId, Fixtures.myFunction, FakeIdentifierResolver()); + var result = await executor.executeTypesPhase(instanceId, + Fixtures.myFunction, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -160,7 +160,7 @@ void main() { test('on methods', () async { var result = await executor.executeTypesPhase( - instanceId, Fixtures.myMethod, FakeIdentifierResolver()); + instanceId, Fixtures.myMethod, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -170,7 +170,7 @@ void main() { test('on getters', () async { var result = await executor.executeTypesPhase(instanceId, - Fixtures.myVariableGetter, FakeIdentifierResolver()); + Fixtures.myVariableGetter, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -181,7 +181,7 @@ void main() { test('on setters', () async { var result = await executor.executeTypesPhase(instanceId, - Fixtures.myVariableSetter, FakeIdentifierResolver()); + Fixtures.myVariableSetter, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -191,8 +191,8 @@ void main() { }); test('on variables', () async { - var result = await executor.executeTypesPhase( - instanceId, Fixtures.myVariable, FakeIdentifierResolver()); + var result = await executor.executeTypesPhase(instanceId, + Fixtures.myVariable, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -203,7 +203,7 @@ void main() { test('on constructors', () async { var result = await executor.executeTypesPhase(instanceId, - Fixtures.myConstructor, FakeIdentifierResolver()); + Fixtures.myConstructor, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -214,7 +214,7 @@ void main() { test('on fields', () async { var result = await executor.executeTypesPhase( - instanceId, Fixtures.myField, FakeIdentifierResolver()); + instanceId, Fixtures.myField, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -224,7 +224,7 @@ void main() { test('on classes', () async { var result = await executor.executeTypesPhase( - instanceId, Fixtures.myClass, FakeIdentifierResolver()); + instanceId, Fixtures.myClass, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -235,7 +235,7 @@ void main() { test('on enums', () async { var result = await executor.executeTypesPhase( - instanceId, Fixtures.myEnum, FakeIdentifierResolver()); + instanceId, Fixtures.myEnum, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -245,7 +245,7 @@ void main() { test('on enum values', () async { var result = await executor.executeTypesPhase(instanceId, - Fixtures.myEnumValues.first, FakeIdentifierResolver()); + Fixtures.myEnumValues.first, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -255,7 +255,7 @@ void main() { test('on mixins', () async { var result = await executor.executeTypesPhase( - instanceId, Fixtures.myMixin, FakeIdentifierResolver()); + instanceId, Fixtures.myMixin, TestTypePhaseIntrospector()); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -268,7 +268,7 @@ void main() { var result = await executor.executeTypesPhase( instanceId, Fixtures.library, - FakeIdentifierResolver(), + TestTypePhaseIntrospector(), ); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); @@ -291,10 +291,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myFunction, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -307,10 +304,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myMethod, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -324,10 +318,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myConstructor, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, hasLength(1)); expect( @@ -346,10 +337,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myVariableGetter, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -362,10 +350,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myVariableSetter, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -378,10 +363,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myVariable, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -394,10 +376,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myField, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, hasLength(1)); expect( @@ -415,10 +394,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myClass, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, hasLength(1)); expect( @@ -432,13 +408,8 @@ class LibraryInfo { }); test('on enums', () async { - var result = await executor.executeDeclarationsPhase( - instanceId, - Fixtures.myEnum, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + var result = await executor.executeDeclarationsPhase(instanceId, + Fixtures.myEnum, Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, hasLength(1)); expect( @@ -455,10 +426,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myEnumValues.first, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, hasLength(1)); expect( @@ -475,10 +443,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.myMixin, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, hasLength(1)); expect( @@ -496,10 +461,7 @@ class LibraryInfo { var result = await executor.executeDeclarationsPhase( instanceId, Fixtures.library, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector); + Fixtures.testDeclarationPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -514,12 +476,7 @@ class LibraryInfo { var result = await executor.executeDefinitionsPhase( instanceId, Fixtures.myFunction, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -539,12 +496,7 @@ class LibraryInfo { var definitionResult = await executor.executeDefinitionsPhase( instanceId, Fixtures.myMethod, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(definitionResult.enumValueAugmentations, isEmpty); expect(definitionResult.typeAugmentations, hasLength(1)); var augmentationStrings = definitionResult @@ -560,12 +512,7 @@ class LibraryInfo { var definitionResult = await executor.executeDefinitionsPhase( instanceId, Fixtures.myConstructor, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(definitionResult.enumValueAugmentations, isEmpty); expect(definitionResult.typeAugmentations, hasLength(1)); expect( @@ -582,12 +529,7 @@ class LibraryInfo { var result = await executor.executeDefinitionsPhase( instanceId, Fixtures.myVariableGetter, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -607,12 +549,7 @@ class LibraryInfo { var result = await executor.executeDefinitionsPhase( instanceId, Fixtures.myVariableSetter, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -633,12 +570,7 @@ class LibraryInfo { var result = await executor.executeDefinitionsPhase( instanceId, Fixtures.myVariable, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( @@ -667,12 +599,7 @@ class LibraryInfo { var definitionResult = await executor.executeDefinitionsPhase( instanceId, Fixtures.myField, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(definitionResult.enumValueAugmentations, isEmpty); expect(definitionResult.typeAugmentations, hasLength(1)); expect( @@ -687,12 +614,7 @@ class LibraryInfo { var definitionResult = await executor.executeDefinitionsPhase( instanceId, Fixtures.myClass, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(definitionResult.enumValueAugmentations, isEmpty); expect(definitionResult.typeAugmentations, hasLength(1)); var augmentationStrings = definitionResult @@ -712,12 +634,7 @@ class LibraryInfo { var definitionResult = await executor.executeDefinitionsPhase( instanceId, Fixtures.myEnum, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(definitionResult.enumValueAugmentations, hasLength(1)); var entryAugmentationStrings = definitionResult .enumValueAugmentations[Fixtures.myEnum.identifier]! @@ -752,12 +669,7 @@ class LibraryInfo { var definitionResult = await executor.executeDefinitionsPhase( instanceId, Fixtures.myEnumValues.first, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(definitionResult.enumValueAugmentations, hasLength(1)); var augmentationStrings = definitionResult .enumValueAugmentations[Fixtures.myEnum.identifier]! @@ -772,12 +684,7 @@ class LibraryInfo { var definitionResult = await executor.executeDefinitionsPhase( instanceId, Fixtures.myMixin, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + Fixtures.testDefinitionPhaseIntrospector); expect(definitionResult.enumValueAugmentations, isEmpty); expect(definitionResult.typeAugmentations, hasLength(1)); var augmentationStrings = definitionResult @@ -792,15 +699,8 @@ class LibraryInfo { }); test('on libraries', () async { - var result = await executor.executeDefinitionsPhase( - instanceId, - Fixtures.library, - FakeIdentifierResolver(), - Fixtures.testTypeDeclarationResolver, - Fixtures.testTypeResolver, - Fixtures.testTypeIntrospector, - Fixtures.testTypeInferrer, - Fixtures.testLibraryDeclarationsResolver); + var result = await executor.executeDefinitionsPhase(instanceId, + Fixtures.library, Fixtures.testDefinitionPhaseIntrospector); expect(result.enumValueAugmentations, isEmpty); expect(result.typeAugmentations, isEmpty); expect( diff --git a/pkg/_fe_analyzer_shared/test/macros/executor/simple_macro.dart b/pkg/_fe_analyzer_shared/test/macros/executor/simple_macro.dart index ecc16736fb2..8347a3ba427 100644 --- a/pkg/_fe_analyzer_shared/test/macros/executor/simple_macro.dart +++ b/pkg/_fe_analyzer_shared/test/macros/executor/simple_macro.dart @@ -578,10 +578,11 @@ class LibraryInfo { } Future _buildFunctionAugmentation( - FunctionDeclaration function, TypeInferrer inferrer) async { + FunctionDeclaration function, + DefinitionPhaseIntrospector introspector) async { Future> typeParts(TypeAnnotation annotation) async { if (annotation is OmittedTypeAnnotation) { - var inferred = await inferrer.inferType(annotation); + var inferred = await introspector.inferType(annotation); return [inferred.code, ' (inferred)']; } return [annotation.code]; diff --git a/pkg/_fe_analyzer_shared/test/macros/util.dart b/pkg/_fe_analyzer_shared/test/macros/util.dart index ea9b24923f7..d22bb6ef352 100644 --- a/pkg/_fe_analyzer_shared/test/macros/util.dart +++ b/pkg/_fe_analyzer_shared/test/macros/util.dart @@ -9,25 +9,49 @@ import 'package:_fe_analyzer_shared/src/macros/executor.dart'; import 'package:_fe_analyzer_shared/src/macros/executor/introspection_impls.dart'; import 'package:_fe_analyzer_shared/src/macros/executor/remote_instance.dart'; -import 'package:test/fake.dart'; import 'package:test/test.dart'; -class FakeTypeIntrospector extends Fake implements TypeIntrospector {} +class TestTypePhaseIntrospector implements TypePhaseIntrospector { + @override + Future resolveIdentifier(Uri library, String name) async { + if (library == Uri.parse('dart:core') && name == 'String') { + return Fixtures.stringType.identifier; + } + throw UnimplementedError('Cannot resolve the identifier $library:$name'); + } +} -class TestTypeIntrospector implements TypeIntrospector { +class TestDeclarationPhaseIntrospector extends TestTypePhaseIntrospector + implements DeclarationPhaseIntrospector { final Map> constructors; final Map> enumValues; final Map> fields; final Map> methods; final Map> libraryTypes; + final Map staticTypes; + final Map typeDeclarations; - TestTypeIntrospector({ - required this.constructors, - required this.enumValues, - required this.fields, - required this.methods, - required this.libraryTypes, - }); + TestDeclarationPhaseIntrospector( + {required this.constructors, + required this.enumValues, + required this.fields, + required this.methods, + required this.libraryTypes, + required this.staticTypes, + required this.typeDeclarations}); + + @override + Future declarationOf(covariant Identifier identifier) async { + var declaration = typeDeclarations[identifier]; + if (declaration != null) return declaration; + throw 'No declaration found for ${identifier.name}'; + } + + @override + Future resolve(covariant TypeAnnotationCode type) async { + assert(type.parts.length == 1); + return staticTypes[type.parts.first]!; + } @override Future> constructorsOf( @@ -54,44 +78,6 @@ class TestTypeIntrospector implements TypeIntrospector { libraryTypes[library]!; } -class FakeIdentifierResolver implements IdentifierResolver { - @override - Future resolveIdentifier(Uri library, String name) async { - if (library == Uri.parse('dart:core') && name == 'String') { - return Fixtures.stringType.identifier; - } - throw UnimplementedError('Cannot resolve the identifier $library:$name'); - } -} - -class FakeTypeDeclarationResolver extends Fake - implements TypeDeclarationResolver {} - -class TestTypeDeclarationResolver implements TypeDeclarationResolver { - final Map typeDeclarations; - - TestTypeDeclarationResolver(this.typeDeclarations); - - @override - Future declarationOf(covariant Identifier identifier) async { - var declaration = typeDeclarations[identifier]; - if (declaration != null) return declaration; - throw 'No declaration found for ${identifier.name}'; - } -} - -class TestTypeResolver implements TypeResolver { - final Map staticTypes; - - TestTypeResolver(this.staticTypes); - - @override - Future resolve(covariant TypeAnnotationCode type) async { - assert(type.parts.length == 1); - return staticTypes[type.parts.first]!; - } -} - /// Doesn't handle generics etc but thats ok for now class TestNamedStaticType implements NamedStaticType { final IdentifierImpl identifier; @@ -115,11 +101,29 @@ class TestNamedStaticType implements NamedStaticType { /// Assumes all omitted types are [TestOmittedTypeAnnotation]s and just returns /// the inferred type directly. -class TestTypeInferrer implements TypeInferrer { +class TestDefinitionsPhaseIntrospector extends TestDeclarationPhaseIntrospector + implements DefinitionPhaseIntrospector { + final Map> libraryDeclarations; + + TestDefinitionsPhaseIntrospector( + {required this.libraryDeclarations, + required super.constructors, + required super.enumValues, + required super.fields, + required super.methods, + required super.libraryTypes, + required super.staticTypes, + required super.typeDeclarations}); + @override Future inferType( TestOmittedTypeAnnotation omittedType) async => omittedType.inferredType!; + + @override + Future> topLevelDeclarationsOf( + covariant Library library) async => + libraryDeclarations[library]!; } /// Knows its inferred type ahead of time. @@ -144,17 +148,6 @@ class TestIdentifier extends IdentifierImpl { kind: kind, name: name, staticScope: staticScope, uri: uri); } -class TestLibraryDeclarationsResolver implements LibraryDeclarationsResolver { - final Map> libraryDeclarations; - - TestLibraryDeclarationsResolver(this.libraryDeclarations); - - @override - Future> topLevelDeclarationsOf( - covariant Library library) async => - libraryDeclarations[library]!; -} - extension DebugCodeString on Code { StringBuffer debugString([StringBuffer? buffer]) { buffer ??= StringBuffer(); @@ -630,12 +623,8 @@ class Fixtures { definingType: myMixinType.identifier, isStatic: false); - static final testTypeResolver = TestTypeResolver({ - stringType.identifier: - TestNamedStaticType(stringType.identifier, 'dart:core', []), - myClass.identifier: myClassStaticType, - }); - static final testTypeIntrospector = TestTypeIntrospector(constructors: { + static final testDeclarationPhaseIntrospector = + TestDeclarationPhaseIntrospector(constructors: { myClass: [myConstructor], myEnum: [myEnumConstructor], myMixin: [], @@ -655,8 +644,11 @@ class Fixtures { myEnum, myMixin, ], - }); - static final testTypeDeclarationResolver = TestTypeDeclarationResolver({ + }, staticTypes: { + stringType.identifier: + TestNamedStaticType(stringType.identifier, 'dart:core', []), + myClass.identifier: myClassStaticType, + }, typeDeclarations: { myClass.identifier: myClass, myEnum.identifier: myEnum, mySuperclass.identifier: mySuperclass, @@ -664,17 +656,23 @@ class Fixtures { myMixin.identifier: myMixin }); - static final testTypeInferrer = TestTypeInferrer(); - - static final testLibraryDeclarationsResolver = - TestLibraryDeclarationsResolver({ - Fixtures.library: [ - myClass, - myEnum, - myMixin, - myFunction, - myVariable, - libraryVariable, - ], - }); + static final testDefinitionPhaseIntrospector = + TestDefinitionsPhaseIntrospector( + constructors: testDeclarationPhaseIntrospector.constructors, + enumValues: testDeclarationPhaseIntrospector.enumValues, + fields: testDeclarationPhaseIntrospector.fields, + methods: testDeclarationPhaseIntrospector.methods, + libraryDeclarations: { + Fixtures.library: [ + myClass, + myEnum, + myMixin, + myFunction, + myVariable, + libraryVariable, + ], + }, + libraryTypes: testDeclarationPhaseIntrospector.libraryTypes, + staticTypes: testDeclarationPhaseIntrospector.staticTypes, + typeDeclarations: testDeclarationPhaseIntrospector.typeDeclarations); } diff --git a/pkg/analyzer/lib/src/summary2/macro_application.dart b/pkg/analyzer/lib/src/summary2/macro_application.dart index d428874ce65..3e3ec0a1cce 100644 --- a/pkg/analyzer/lib/src/summary2/macro_application.dart +++ b/pkg/analyzer/lib/src/summary2/macro_application.dart @@ -73,20 +73,17 @@ class LibraryMacroApplier { final LibraryBuilder libraryBuilder; final MultiMacroExecutor macroExecutor; + late final macro.DeclarationPhaseIntrospector _declarationPhaseIntrospector = + _DeclarationPhaseIntrospector( + _linker.elementFactory, + declarationBuilder, + libraryBuilder.element.typeSystem, + ); + final List<_MacroTarget> _targets = []; - late final macro.IdentifierResolver _identifierResolver = - _IdentifierResolver(_linker.elementFactory, declarationBuilder); - - late final macro.TypeDeclarationResolver _typeDeclarationResolver = - _TypeDeclarationResolver(declarationBuilder); - - late final macro.TypeIntrospector _typeIntrospector = - _TypeIntrospector(declarationBuilder); - - late final macro.TypeResolver _typeResolver = _TypeResolver( - typeSystem: libraryBuilder.element.typeSystem, - ); + late final macro.TypePhaseIntrospector _typePhaseIntrospector = + _TypePhaseIntrospector(_linker.elementFactory, declarationBuilder); LibraryMacroApplier({ required this.macroExecutor, @@ -487,20 +484,91 @@ class _ArgumentEvaluation { } } -class _IdentifierResolver implements macro.IdentifierResolver { - final LinkedElementFactory elementFactory; - final DeclarationBuilder declarationBuilder; +class _DeclarationPhaseIntrospector extends _TypePhaseIntrospector + implements macro.DeclarationPhaseIntrospector { + final TypeSystemImpl typeSystem; - _IdentifierResolver( - this.elementFactory, - this.declarationBuilder, - ); + _DeclarationPhaseIntrospector( + super.elementFactory, super.declarationBuilder, this.typeSystem); @override - Future resolveIdentifier(Uri library, String name) async { - final libraryElement = elementFactory.libraryOfUri2(library); - final element = libraryElement.scope.lookup(name).getter!; - return declarationBuilder.fromElement.identifier(element); + Future> constructorsOf( + covariant macro.IntrospectableType type) { + // TODO: implement constructorsOf + throw UnimplementedError(); + } + + @override + Future declarationOf( + covariant IdentifierImpl identifier, + ) async { + final element = identifier.element; + if (element is ClassElementImpl) { + return declarationBuilder.fromElement.classElement(element); + } else { + throw ArgumentError('element: $element'); + } + } + + @override + Future> fieldsOf( + covariant macro.IntrospectableType type, + ) async { + if (type is! IntrospectableClassDeclarationImpl) { + throw UnsupportedError('Only introspection on classes is supported'); + } + return type.element.fields + .where((e) => !e.isSynthetic) + .map(declarationBuilder.fromElement.fieldElement) + .toList(); + } + + @override + Future> methodsOf( + covariant macro.IntrospectableType clazz) { + // TODO: implement methodsOf + throw UnimplementedError(); + } + + @override + Future resolve(macro.TypeAnnotationCode type) async { + var dartType = _resolve(type); + return _StaticTypeImpl(typeSystem, dartType); + } + + @override + Future> typesOf(covariant macro.Library library) { + // TODO: implement typesOf + throw UnimplementedError(); + } + + @override + Future> valuesOf( + covariant macro.IntrospectableEnum type) { + // TODO: implement valuesOf + throw UnimplementedError(); + } + + DartType _resolve(macro.TypeAnnotationCode type) { + // TODO(scheglov) write tests + if (type is macro.NamedTypeAnnotationCode) { + final identifier = type.name as IdentifierImpl; + final element = identifier.element; + if (element is ClassElementImpl) { + return element.instantiate( + typeArguments: type.typeArguments.map(_resolve).toList(), + nullabilitySuffix: type.isNullable + ? NullabilitySuffix.question + : NullabilitySuffix.none, + ); + } else { + // TODO(scheglov) Implement other elements. + throw UnimplementedError('(${element.runtimeType}) $element'); + } + } else { + // TODO(scheglov) Implement other types. + throw UnimplementedError('(${type.runtimeType}) $type'); + } } } @@ -520,10 +588,7 @@ class _MacroApplication { return await executor.executeDeclarationsPhase( instanceIdentifier, target.declaration, - applier._identifierResolver, - applier._typeDeclarationResolver, - applier._typeResolver, - applier._typeIntrospector, + applier._declarationPhaseIntrospector, ); } @@ -533,7 +598,7 @@ class _MacroApplication { return await executor.executeTypesPhase( instanceIdentifier, target.declaration, - applier._identifierResolver, + applier._typePhaseIntrospector, ); } @@ -597,103 +662,20 @@ class _StaticTypeImpl implements macro.StaticType { } } -class _TypeDeclarationResolver implements macro.TypeDeclarationResolver { +class _TypePhaseIntrospector implements macro.TypePhaseIntrospector { + final LinkedElementFactory elementFactory; final DeclarationBuilder declarationBuilder; - _TypeDeclarationResolver(this.declarationBuilder); + _TypePhaseIntrospector( + this.elementFactory, + this.declarationBuilder, + ); @override - Future declarationOf( - covariant IdentifierImpl identifier, - ) async { - final element = identifier.element; - if (element is ClassElementImpl) { - return declarationBuilder.fromElement.classElement(element); - } else { - throw ArgumentError('element: $element'); - } - } -} - -class _TypeIntrospector implements macro.TypeIntrospector { - final DeclarationBuilder declarationBuilder; - - _TypeIntrospector(this.declarationBuilder); - - @override - Future> constructorsOf( - covariant macro.IntrospectableType type) { - // TODO: implement constructorsOf - throw UnimplementedError(); - } - - @override - Future> fieldsOf( - covariant macro.IntrospectableType type, - ) async { - if (type is! IntrospectableClassDeclarationImpl) { - throw UnsupportedError('Only introspection on classes is supported'); - } - return type.element.fields - .where((e) => !e.isSynthetic) - .map(declarationBuilder.fromElement.fieldElement) - .toList(); - } - - @override - Future> methodsOf( - covariant macro.IntrospectableType clazz) { - // TODO: implement methodsOf - throw UnimplementedError(); - } - - @override - Future> typesOf(covariant macro.Library library) { - // TODO: implement typesOf - throw UnimplementedError(); - } - - @override - Future> valuesOf( - covariant macro.IntrospectableEnum type) { - // TODO: implement valuesOf - throw UnimplementedError(); - } -} - -class _TypeResolver implements macro.TypeResolver { - final TypeSystemImpl typeSystem; - - _TypeResolver({ - required this.typeSystem, - }); - - @override - Future resolve(macro.TypeAnnotationCode type) async { - var dartType = _resolve(type); - return _StaticTypeImpl(typeSystem, dartType); - } - - DartType _resolve(macro.TypeAnnotationCode type) { - // TODO(scheglov) write tests - if (type is macro.NamedTypeAnnotationCode) { - final identifier = type.name as IdentifierImpl; - final element = identifier.element; - if (element is ClassElementImpl) { - return element.instantiate( - typeArguments: type.typeArguments.map(_resolve).toList(), - nullabilitySuffix: type.isNullable - ? NullabilitySuffix.question - : NullabilitySuffix.none, - ); - } else { - // TODO(scheglov) Implement other elements. - throw UnimplementedError('(${element.runtimeType}) $element'); - } - } else { - // TODO(scheglov) Implement other types. - throw UnimplementedError('(${type.runtimeType}) $type'); - } + Future resolveIdentifier(Uri library, String name) async { + final libraryElement = elementFactory.libraryOfUri2(library); + final element = libraryElement.scope.lookup(name).getter!; + return declarationBuilder.fromElement.identifier(element); } } diff --git a/pkg/analyzer/test/src/summary/macro/introspect_declarations_phase.dart b/pkg/analyzer/test/src/summary/macro/introspect_declarations_phase.dart index 15e93460f6c..85595ea2797 100644 --- a/pkg/analyzer/test/src/summary/macro/introspect_declarations_phase.dart +++ b/pkg/analyzer/test/src/summary/macro/introspect_declarations_phase.dart @@ -23,10 +23,7 @@ import 'introspect_shared.dart'; ) async { final printer = _DeclarationPrinter( withDetailsFor: withDetailsFor.cast(), - typeIntrospector: builder, - identifierResolver: builder, - typeDeclarationResolver: builder, - typeResolver: builder, + declarationPhaseIntrospector: builder, ); await printer.writeClassDeclaration(declaration); final text = printer._sink.toString(); @@ -42,10 +39,7 @@ import 'introspect_shared.dart'; class _DeclarationPrinter { final Set withDetailsFor; - final TypeIntrospector typeIntrospector; - final IdentifierResolver identifierResolver; - final TypeDeclarationResolver typeDeclarationResolver; - final TypeResolver typeResolver; + final DeclarationPhaseIntrospector declarationPhaseIntrospector; final StringBuffer _sink = StringBuffer(); String _indent = ''; @@ -54,10 +48,7 @@ class _DeclarationPrinter { _DeclarationPrinter({ required this.withDetailsFor, - required this.typeIntrospector, - required this.identifierResolver, - required this.typeDeclarationResolver, - required this.typeResolver, + required this.declarationPhaseIntrospector, }); Future writeClassDeclaration(IntrospectableClassDeclaration e) async { @@ -77,7 +68,7 @@ class _DeclarationPrinter { final superIdentifier = superAnnotation.identifier; _writelnWithIndent('superclass'); try { - final superDeclaration = await typeDeclarationResolver + final superDeclaration = await declarationPhaseIntrospector .declarationOf(superIdentifier) as IntrospectableClassDeclaration; await _withIndent(() => writeClassDeclaration(superDeclaration)); } on ArgumentError { @@ -94,7 +85,7 @@ class _DeclarationPrinter { _enclosingDeclarationIdentifier = e.identifier; await _writeElements( 'fields', - await typeIntrospector.fieldsOf(e), + await declarationPhaseIntrospector.fieldsOf(e), _writeField, ); }); diff --git a/pkg/front_end/lib/src/fasta/kernel/macro/macro.dart b/pkg/front_end/lib/src/fasta/kernel/macro/macro.dart index 91143f0d9e6..efa12b0dc7b 100644 --- a/pkg/front_end/lib/src/fasta/kernel/macro/macro.dart +++ b/pkg/front_end/lib/src/fasta/kernel/macro/macro.dart @@ -514,7 +514,7 @@ class MacroApplications { await _macroExecutor.executeTypesPhase( macroApplication.instanceIdentifier, declaration, - identifierResolver); + typePhaseIntrospector); if (result.isNotEmpty) { results.add(result); } @@ -527,13 +527,13 @@ class MacroApplications { return results; } - late macro.IdentifierResolver identifierResolver; + late macro.TypePhaseIntrospector typePhaseIntrospector; late SourceLoader sourceLoader; Future> applyTypeMacros( SourceLoader sourceLoader) async { this.sourceLoader = sourceLoader; - identifierResolver = new _IdentifierResolver(sourceLoader); + typePhaseIntrospector = new _TypePhaseIntrospector(sourceLoader); List augmentationLibraries = []; _ensureApplicationData(); for (MapEntry entry @@ -607,10 +607,7 @@ class MacroApplications { await _macroExecutor.executeDeclarationsPhase( macroApplication.instanceIdentifier, declaration, - identifierResolver, - typeDeclarationResolver, - typeResolver, - typeIntrospector); + declarationPhaseIntrospector); if (result.isNotEmpty) { Map omittedTypes = {}; String source = _macroExecutor.buildAugmentationLibrary([result], @@ -652,19 +649,18 @@ class MacroApplications { } } + late ClassHierarchyBuilder classHierarchy; late Types types; - late macro.TypeDeclarationResolver typeDeclarationResolver; - late macro.TypeResolver typeResolver; - late macro.TypeIntrospector typeIntrospector; + late macro.DeclarationPhaseIntrospector declarationPhaseIntrospector; Future applyDeclarationsMacros( ClassHierarchyBuilder classHierarchy, List sortedSourceClassBuilders, Future Function(SourceLibraryBuilder) onAugmentationLibrary) async { + this.classHierarchy = classHierarchy; types = new Types(classHierarchy); - typeDeclarationResolver = new _TypeDeclarationResolver(this); - typeResolver = new _TypeResolver(this); - typeIntrospector = new _TypeIntrospector(this, classHierarchy); + declarationPhaseIntrospector = + new _DeclarationPhaseIntrospector(this, classHierarchy, sourceLoader); // Apply macros to classes first, in class hierarchy order. for (SourceClassBuilder classBuilder in sortedSourceClassBuilders) { @@ -711,12 +707,7 @@ class MacroApplications { await _macroExecutor.executeDefinitionsPhase( macroApplication.instanceIdentifier, declaration, - identifierResolver, - typeDeclarationResolver, - typeResolver, - typeIntrospector, - typeInferrer, - libraryDeclarationsResolver); + definitionPhaseIntrospector); if (result.isNotEmpty) { results.add(result); } @@ -729,12 +720,11 @@ class MacroApplications { return results; } - late macro.TypeInferrer typeInferrer; - late macro.LibraryDeclarationsResolver libraryDeclarationsResolver; + late macro.DefinitionPhaseIntrospector definitionPhaseIntrospector; Future> applyDefinitionMacros() async { - typeInferrer = new _TypeInferrer(this); - libraryDeclarationsResolver = new _LibraryDeclarationsResolver(); + definitionPhaseIntrospector = + new _DefinitionPhaseIntrospector(this, classHierarchy, sourceLoader); List augmentationLibraries = []; for (MapEntry entry in libraryData.entries) { @@ -1233,10 +1223,10 @@ class _StaticTypeImpl implements macro.StaticType { } } -class _IdentifierResolver implements macro.IdentifierResolver { +class _TypePhaseIntrospector implements macro.TypePhaseIntrospector { final SourceLoader sourceLoader; - _IdentifierResolver(this.sourceLoader); + _TypePhaseIntrospector(this.sourceLoader); @override Future resolveIdentifier(Uri library, String name) { @@ -1278,23 +1268,22 @@ class _IdentifierResolver implements macro.IdentifierResolver { } } -class _TypeResolver implements macro.TypeResolver { +class _DeclarationPhaseIntrospector extends _TypePhaseIntrospector + implements macro.DeclarationPhaseIntrospector { + final ClassHierarchyBuilder classHierarchy; final MacroApplications macroApplications; - _TypeResolver(this.macroApplications); + _DeclarationPhaseIntrospector( + this.macroApplications, this.classHierarchy, super.sourceLoader); @override - Future resolve(macro.TypeAnnotationCode typeAnnotation) { - return new Future.value( - macroApplications.resolveTypeAnnotation(typeAnnotation)); + Future declarationOf(macro.Identifier identifier) { + if (identifier is IdentifierImpl) { + return identifier.resolveTypeDeclaration(macroApplications); + } + throw new UnsupportedError( + 'Unsupported identifier $identifier (${identifier.runtimeType})'); } -} - -class _TypeIntrospector implements macro.TypeIntrospector { - final MacroApplications macroApplications; - final ClassHierarchyBuilder classHierarchy; - - _TypeIntrospector(this.macroApplications, this.classHierarchy); @override Future> constructorsOf( @@ -1367,36 +1356,23 @@ class _TypeIntrospector implements macro.TypeIntrospector { // TODO: implement typesOf throw new UnimplementedError(); } -} - -class _TypeDeclarationResolver implements macro.TypeDeclarationResolver { - final MacroApplications macroApplications; - - _TypeDeclarationResolver(this.macroApplications); @override - Future declarationOf(macro.Identifier identifier) { - if (identifier is IdentifierImpl) { - return identifier.resolveTypeDeclaration(macroApplications); - } - throw new UnsupportedError( - 'Unsupported identifier $identifier (${identifier.runtimeType})'); + Future resolve(macro.TypeAnnotationCode typeAnnotation) { + return new Future.value( + macroApplications.resolveTypeAnnotation(typeAnnotation)); } } -class _TypeInferrer implements macro.TypeInferrer { - final MacroApplications _macroApplications; - - _TypeInferrer(this._macroApplications); +class _DefinitionPhaseIntrospector extends _DeclarationPhaseIntrospector + implements macro.DefinitionPhaseIntrospector { + _DefinitionPhaseIntrospector( + super.macroApplications, super.classHierarchy, super.sourceLoader); @override Future inferType( macro.OmittedTypeAnnotation omittedType) => - new Future.value(_macroApplications._inferOmittedType(omittedType)); -} - -class _LibraryDeclarationsResolver - implements macro.LibraryDeclarationsResolver { + new Future.value(macroApplications._inferOmittedType(omittedType)); @override Future> topLevelDeclarationsOf( macro.Library library) { diff --git a/pkg/front_end/test/macros/declaration/macro_declaration_test.dart b/pkg/front_end/test/macros/declaration/macro_declaration_test.dart index 307f47df1d1..fd1e725a361 100644 --- a/pkg/front_end/test/macros/declaration/macro_declaration_test.dart +++ b/pkg/front_end/test/macros/declaration/macro_declaration_test.dart @@ -303,10 +303,7 @@ class TestMacroExecutor extends MultiMacroExecutor { Future executeDeclarationsPhase( MacroInstanceIdentifier macro, MacroTarget target, - IdentifierResolver identifierResolver, - TypeDeclarationResolver typeDeclarationResolver, - TypeResolver typeResolver, - TypeIntrospector typeIntrospector) async { + DeclarationPhaseIntrospector introspector) async { return new _MacroExecutionResult(); } @@ -314,18 +311,13 @@ class TestMacroExecutor extends MultiMacroExecutor { Future executeDefinitionsPhase( MacroInstanceIdentifier macro, MacroTarget target, - IdentifierResolver identifierResolver, - TypeDeclarationResolver typeDeclarationResolver, - TypeResolver typeResolver, - TypeIntrospector typeIntrospector, - TypeInferrer typeInferrer, - LibraryDeclarationsResolver libraryDeclarationsResolver) async { + DefinitionPhaseIntrospector introspector) async { return new _MacroExecutionResult(); } @override Future executeTypesPhase(MacroInstanceIdentifier macro, - MacroTarget target, IdentifierResolver identifierResolver) async { + MacroTarget target, TypePhaseIntrospector introspector) async { return new _MacroExecutionResult(); } diff --git a/pkg/front_end/test/spell_checking_list_code.txt b/pkg/front_end/test/spell_checking_list_code.txt index da59adf8e43..16eb3cf057b 100644 --- a/pkg/front_end/test/spell_checking_list_code.txt +++ b/pkg/front_end/test/spell_checking_list_code.txt @@ -326,6 +326,7 @@ cross cruft cryptic crypto +cs csslib cstefantsova ctx @@ -785,6 +786,7 @@ introspect introspectable introspection introspector +introspectors ints invariants io @@ -1337,6 +1339,7 @@ roundtrip row row's rows +rp rparen rpc rs