@@ -6,14 +6,7 @@ import 'macros.dart'; // For dart docs.
6
6
7
7
/// The base interface used to add declarations to the program as well
8
8
/// 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 {}
17
10
18
11
/// The api used by [Macro] s to contribute new type declarations to the
19
12
/// current library, and get [TypeAnnotation] s from runtime [Type] objects.
@@ -22,30 +15,30 @@ abstract class TypeBuilder implements Builder {
22
15
void declareType (DeclarationCode typeDeclaration);
23
16
}
24
17
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] .
27
19
///
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] .
31
27
///
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);
46
34
}
47
35
48
36
/// 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.
49
42
abstract class ClassIntrospector {
50
43
/// The fields available for [clazz] .
51
44
///
@@ -75,23 +68,47 @@ abstract class ClassIntrospector {
75
68
Future <List <ClassDeclaration >> interfacesOf (ClassDeclaration clazz);
76
69
}
77
70
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
+
78
89
/// The api used by [Macro] s to reflect on the currently available
79
90
/// members, superclass, and mixins for a given [ClassDeclaration]
80
91
abstract class ClassDeclarationBuilder
81
92
implements ClassMemberDeclarationBuilder , ClassIntrospector {}
82
93
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);
88
101
}
89
102
90
103
/// The base class for builders in the definition phase. These can convert
91
104
/// any [TypeAnnotation] into its corresponding [TypeDeclaration] , and also
92
105
/// reflect more deeply on those.
93
106
abstract class DefinitionBuilder
94
- implements Builder , ClassIntrospector , TypeIntrospector {}
107
+ implements
108
+ Builder ,
109
+ TypeResolver ,
110
+ ClassIntrospector ,
111
+ TypeDeclarationResolver {}
95
112
96
113
/// The apis used by [Macro] s that run on classes, to fill in the definitions
97
114
/// of any external declarations within that class.
0 commit comments