@@ -6,14 +6,7 @@ import 'macros.dart'; // For dart docs.
66
77/// The base interface used to add declarations to the program as well
88/// as augment existing ones.
9- abstract class Builder {
10- /// Used to construct a [TypeAnnotation] to a runtime type available to the
11- /// the macro implementation code.
12- ///
13- /// This can be used to emit a reference to it in generated code, or do
14- /// subtype checks (depending on support in the current phase).
15- TypeAnnotation typeAnnotationOf <T >();
16- }
9+ abstract class Builder {}
1710
1811/// The api used by [Macro] s to contribute new type declarations to the
1912/// current library, and get [TypeAnnotation] s from runtime [Type] objects.
@@ -22,30 +15,30 @@ abstract class TypeBuilder implements Builder {
2215 void declareType (DeclarationCode typeDeclaration);
2316}
2417
25- /// The api used by [Macro] s to contribute new (non-type)
26- /// declarations to the current library.
18+ /// The interface to resolve a [TypeAnnotation] to a [StaticType] .
2719///
28- /// Can also be used to do subtype checks on types.
29- abstract class DeclarationBuilder implements Builder {
30- /// Adds a new regular declaration to the surrounding library.
20+ /// The [StaticType] s can be compared against other [StaticType] s to see how
21+ /// they relate to each other.
22+ ///
23+ /// This api is only available to the declaration and definition phases of
24+ /// macro expansion.
25+ abstract class TypeResolver {
26+ /// Resolves [typeAnnotation] to a [StaticType] .
3127 ///
32- /// Note that type declarations are not supported.
33- Declaration declareInLibrary (DeclarationCode declaration);
34-
35- /// Returns true if [leftType] is a subtype of [rightType] .
36- bool isSubtypeOf (TypeAnnotation leftType, TypeAnnotation rightType);
37-
38- /// Retruns true if [leftType] is an identical type to [rightType] .
39- bool isExactly (TypeAnnotation leftType, TypeAnnotation rightType);
40- }
41-
42- /// The api used by [Macro] s to contribute new members to a class.
43- abstract class ClassMemberDeclarationBuilder implements DeclarationBuilder {
44- /// Adds a new declaration to the surrounding class.
45- void declareInClass (DeclarationCode declaration);
28+ /// Throws an error if the type annotation cannot be resolved. This should
29+ /// only happen in the case of incomplete or invalid programs, but macros
30+ /// may be asked to run in this state during the development cycle. It is
31+ /// helpful for users if macros provide a best effort implementation in that
32+ /// case or handle the error in a useful way.
33+ Future <StaticType > resolve (TypeAnnotation typeAnnotation);
4634}
4735
4836/// The api used to introspect on a [ClassDeclaration] .
37+ ///
38+ /// Available in the declaration and definition phases, but limited in the
39+ /// declaration phase to immediately annotated [ClassDeclaration] s. This is
40+ /// done by limiting the access to the [TypeDeclarationResolver] to the
41+ /// definition phase.
4942abstract class ClassIntrospector {
5043 /// The fields available for [clazz] .
5144 ///
@@ -75,23 +68,47 @@ abstract class ClassIntrospector {
7568 Future <List <ClassDeclaration >> interfacesOf (ClassDeclaration clazz);
7669}
7770
71+ /// The api used by [Macro] s to contribute new (non-type)
72+ /// declarations to the current library.
73+ ///
74+ /// Can also be used to do subtype checks on types.
75+ abstract class DeclarationBuilder
76+ implements Builder , TypeResolver , ClassIntrospector {
77+ /// Adds a new regular declaration to the surrounding library.
78+ ///
79+ /// Note that type declarations are not supported.
80+ void declareInLibrary (DeclarationCode declaration);
81+ }
82+
83+ /// The api used by [Macro] s to contribute new members to a class.
84+ abstract class ClassMemberDeclarationBuilder implements DeclarationBuilder {
85+ /// Adds a new declaration to the surrounding class.
86+ void declareInClass (DeclarationCode declaration);
87+ }
88+
7889/// The api used by [Macro] s to reflect on the currently available
7990/// members, superclass, and mixins for a given [ClassDeclaration]
8091abstract class ClassDeclarationBuilder
8192 implements ClassMemberDeclarationBuilder , ClassIntrospector {}
8293
83- /// The api used by [Macro] to get a [TypeDeclaration] for any given
84- /// [TypeAnnotation] .
85- abstract class TypeIntrospector {
86- /// Resolves a [NamedTypeAnnotation] to its declaration.
87- Future <TypeDeclaration > resolve (NamedTypeAnnotation annotation);
94+ /// The interface used by [Macro] s to resolve any [NamedStaticType] to its
95+ /// declaration.
96+ ///
97+ /// Only available in the definition phase of macro expansion.
98+ abstract class TypeDeclarationResolver {
99+ /// Resolves a [NamedStaticType] to its [TypeDeclaration] .
100+ Future <TypeDeclaration > declarationOf (NamedStaticType annotation);
88101}
89102
90103/// The base class for builders in the definition phase. These can convert
91104/// any [TypeAnnotation] into its corresponding [TypeDeclaration] , and also
92105/// reflect more deeply on those.
93106abstract class DefinitionBuilder
94- implements Builder , ClassIntrospector , TypeIntrospector {}
107+ implements
108+ Builder ,
109+ TypeResolver ,
110+ ClassIntrospector ,
111+ TypeDeclarationResolver {}
95112
96113/// The apis used by [Macro] s that run on classes, to fill in the definitions
97114/// of any external declarations within that class.
0 commit comments