16
16
#include " swift/AST/Types.h"
17
17
#include " swift/AST/ASTContext.h"
18
18
#include " swift/AST/Decl.h"
19
+ #include " swift/AST/GenericParamList.h"
19
20
#include " swift/AST/ParameterList.h"
20
21
21
22
namespace swift {
22
23
23
24
struct SynthesisContext {
24
25
ASTContext &Context;
25
26
DeclContext *DC;
27
+ GenericParamList *GenericParams = nullptr ;
26
28
27
29
SynthesisContext (ASTContext &ctx, DeclContext *DC)
28
30
: Context(ctx), DC(DC) {}
29
31
};
30
32
33
+ // / Allow literal types to be passed at an arbitrary position
34
+ // / in the type-synthesis DSL.
35
+ inline Type synthesizeType (SynthesisContext &SC, Type type) {
36
+ return type;
37
+ }
38
+
31
39
// / A synthesizer which generates a specific type.
32
40
enum SingletonTypeSynthesizer {
33
- _void,
34
- _nativeObject,
41
+ _any,
42
+ _bridgeObject,
43
+ _error,
35
44
_job,
45
+ _nativeObject,
46
+ _never,
47
+ _rawPointer,
48
+ _void,
49
+ _word,
36
50
};
37
51
inline Type synthesizeType (SynthesisContext &SC,
38
52
SingletonTypeSynthesizer kind) {
39
53
switch (kind) {
40
- case _void: return SC.Context .TheEmptyTupleType ;
41
- case _nativeObject: return SC.Context .TheNativeObjectType ;
54
+ case _any: return SC.Context .TheAnyType ;
55
+ case _bridgeObject: return SC.Context .TheBridgeObjectType ;
56
+ case _error: return SC.Context .getExceptionType ();
42
57
case _job: return SC.Context .TheJobType ;
58
+ case _nativeObject: return SC.Context .TheNativeObjectType ;
59
+ case _never: return SC.Context .getNeverType ();
60
+ case _rawPointer: return SC.Context .TheRawPointerType ;
61
+ case _void: return SC.Context .TheEmptyTupleType ;
62
+ case _word: return BuiltinIntegerType::get (BuiltinIntegerWidth::pointer (),
63
+ SC.Context );
43
64
}
44
65
}
45
66
67
+ // / A synthesizer which generates an integer type.
68
+ struct IntegerTypeSynthesizer {
69
+ unsigned BitWidth;
70
+ };
71
+ constexpr inline IntegerTypeSynthesizer _int (unsigned bitWidth) {
72
+ return {bitWidth};
73
+ }
74
+ inline Type synthesizeType (SynthesisContext &SC, IntegerTypeSynthesizer S) {
75
+ return BuiltinIntegerType::get (S.BitWidth , SC.Context );
76
+ }
77
+
78
+ // / A synthesizer which generates a vector type.
79
+ template <class S >
80
+ struct VectorTypeSynthesizer {
81
+ unsigned Count;
82
+ S Sub;
83
+ };
84
+ template <class S >
85
+ constexpr VectorTypeSynthesizer<S> _vector (unsigned count, S sub) {
86
+ return {count, sub};
87
+ }
88
+ template <class S >
89
+ Type synthesizeType (SynthesisContext &SC,
90
+ const VectorTypeSynthesizer<S> &V) {
91
+ return BuiltinVectorType::get (SC.Context , synthesizeType (SC, V.Sub ),
92
+ V.Count );
93
+ }
94
+
95
+ // / A synthesizer which generates a metatype type.
96
+ template <class S >
97
+ struct MetatypeTypeSynthesizer {
98
+ S Sub;
99
+ };
100
+ template <class S >
101
+ constexpr MetatypeTypeSynthesizer<S> _metatype (S sub) {
102
+ return {sub};
103
+ }
104
+ template <class S >
105
+ Type synthesizeType (SynthesisContext &SC,
106
+ const MetatypeTypeSynthesizer<S> &M) {
107
+ return MetatypeType::get (synthesizeType (SC, M.Sub ));
108
+ }
109
+
110
+ // / A synthesizer which generates an existential metatype type.
111
+ template <class S >
112
+ struct ExistentialMetatypeTypeSynthesizer {
113
+ S Sub;
114
+ };
115
+ template <class S >
116
+ constexpr ExistentialMetatypeTypeSynthesizer<S> _existentialMetatype (S sub) {
117
+ return {sub};
118
+ }
119
+ template <class S >
120
+ Type synthesizeType (SynthesisContext &SC,
121
+ const ExistentialMetatypeTypeSynthesizer<S> &M) {
122
+ return ExistentialMetatypeType::get (synthesizeType (SC, M.Sub ));
123
+ }
124
+
46
125
// / Helper types for variadic synthesis.
47
126
template <class ... Ss>
48
127
struct VariadicSynthesizerStorage ;
@@ -51,9 +130,8 @@ template <>
51
130
struct VariadicSynthesizerStorage <> {
52
131
constexpr VariadicSynthesizerStorage () {}
53
132
54
- template <class T , class Fn >
55
- void collect (SynthesisContext &SC, SmallVectorImpl<T> &results,
56
- Fn fn) const {}
133
+ template <class Fn >
134
+ void visit (const Fn &fn) const {}
57
135
};
58
136
template <class Head , class ... Tail>
59
137
struct VariadicSynthesizerStorage <Head, Tail...> {
@@ -62,24 +140,38 @@ struct VariadicSynthesizerStorage<Head, Tail...> {
62
140
constexpr VariadicSynthesizerStorage (Head head, Tail... tail)
63
141
: head(head), tail(tail...) {}
64
142
65
- template <class T , class Fn >
66
- void collect (SynthesisContext &SC,
67
- SmallVectorImpl<T> &results,
68
- Fn fn) const {
69
- results.push_back (fn (SC, head));
70
- tail.collect (SC, results, fn);
143
+ template <class Fn >
144
+ void visit (const Fn &fn) const {
145
+ fn (head);
146
+ tail.visit (fn);
71
147
}
72
148
};
73
149
150
+ // / A synthesizer which generates a generic type parameter.
151
+ struct TypeParamTypeSynthesizer {
152
+ unsigned Index;
153
+ };
154
+ constexpr inline TypeParamTypeSynthesizer _typeparam (unsigned index) {
155
+ return {index};
156
+ }
157
+ inline Type synthesizeType (SynthesisContext &SC,
158
+ const TypeParamTypeSynthesizer &S) {
159
+ assert (SC.GenericParams );
160
+ return SC.GenericParams ->getParams ()[S.Index ]->getDeclaredInterfaceType ();
161
+ }
162
+
74
163
// / Synthesize tuple type elements.
75
164
template <class S >
76
165
TupleTypeElt synthesizeTupleTypeElt (SynthesisContext &SC, S s) {
77
166
return synthesizeType (SC, s);
78
167
}
79
- struct SynthesizeTupleTypeElt {
168
+ struct CollectTupleTypeElements {
169
+ SynthesisContext &SC;
170
+ SmallVectorImpl<TupleTypeElt> &Elts;
171
+
80
172
template <class S >
81
- TupleTypeElt operator ()(SynthesisContext &SC, const S &s) {
82
- synthesizeTupleTypeElt (SC, s);
173
+ void operator ()(const S &s) const {
174
+ Elts. push_back ( synthesizeTupleTypeElt (SC, s) );
83
175
}
84
176
};
85
177
@@ -96,7 +188,7 @@ template <class... Elts>
96
188
Type synthesizeType (SynthesisContext &SC,
97
189
const TupleSynthesizer<Elts...> &tuple) {
98
190
SmallVector<TupleTypeElt, sizeof ...(Elts)> elts;
99
- tuple.Elements .collect ( SC, elts, SynthesizeTupleTypeElt () );
191
+ tuple.Elements .visit (CollectTupleTypeElements{ SC, elts} );
100
192
return TupleType::get (elts, SC.Context );
101
193
}
102
194
@@ -107,6 +199,7 @@ ParamDecl *synthesizeParamDecl(SynthesisContext &SC, const S &s) {
107
199
auto PD = new (SC.Context ) ParamDecl (SourceLoc (), SourceLoc (),
108
200
Identifier (), SourceLoc (),
109
201
Identifier (), SC.DC );
202
+ PD->setSpecifier (ParamSpecifier::Default);
110
203
PD->setInterfaceType (type);
111
204
PD->setImplicit ();
112
205
return PD;
@@ -157,33 +250,39 @@ constexpr ParameterListSynthesizer<Params...> _parameters(Params... ps) {
157
250
return {{ps...}};
158
251
}
159
252
160
- struct SynthesizeParamDecl {
253
+ struct CollectParamDecls {
254
+ SynthesisContext &SC;
255
+ SmallVectorImpl<ParamDecl*> &Results;
256
+
161
257
template <class S >
162
- ParamDecl * operator ()(SynthesisContext &SC, const S &s) {
258
+ void operator ()(const S &s) const {
163
259
// Found by argument-dependent lookup.
164
- return synthesizeParamDecl (SC, s);
260
+ Results. push_back ( synthesizeParamDecl (SC, s) );
165
261
}
166
262
};
167
263
template <class ... Params>
168
264
ParameterList *synthesizeParameterList (SynthesisContext &SC,
169
265
const ParameterListSynthesizer<Params...> &list) {
170
266
SmallVector<ParamDecl*, 4 > decls;
171
- list.params .collect ( SC, decls, SynthesizeParamDecl () );
267
+ list.params .visit (CollectParamDecls{ SC, decls} );
172
268
return ParameterList::create (SC.Context , decls);
173
269
}
174
270
175
- struct SynthesizeParamType {
271
+ struct CollectParamTypes {
272
+ SynthesisContext &SC;
273
+ SmallVectorImpl<FunctionType::Param> &Results;
274
+
176
275
template <class S >
177
- FunctionType::Param operator ()(SynthesisContext &SC, const S &s) {
276
+ void operator ()(const S &s) const {
178
277
// Found by argument-dependent lookup.
179
- return synthesizeParamType (SC, s);
278
+ Results. push_back ( synthesizeParamType (SC, s) );
180
279
}
181
280
};
182
281
template <class ... Params>
183
282
void synthesizeParameterTypes (SynthesisContext &SC,
184
283
const ParameterListSynthesizer<Params...> &list,
185
284
SmallVectorImpl<FunctionType::Param> &types) {
186
- list.params .collect ( SC, types, SynthesizeParamType () );
285
+ list.params .visit (CollectParamTypes{ SC, types} );
187
286
}
188
287
189
288
// / Synthesize function ExtInfo.
0 commit comments