1616#include " swift/AST/Types.h"
1717#include " swift/AST/ASTContext.h"
1818#include " swift/AST/Decl.h"
19+ #include " swift/AST/GenericParamList.h"
1920#include " swift/AST/ParameterList.h"
2021
2122namespace swift {
2223
2324struct SynthesisContext {
2425 ASTContext &Context;
2526 DeclContext *DC;
27+ GenericParamList *GenericParams = nullptr ;
2628
2729 SynthesisContext (ASTContext &ctx, DeclContext *DC)
2830 : Context(ctx), DC(DC) {}
2931};
3032
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+
3139// / A synthesizer which generates a specific type.
3240enum SingletonTypeSynthesizer {
33- _void,
34- _nativeObject,
41+ _any,
42+ _bridgeObject,
43+ _error,
3544 _job,
45+ _nativeObject,
46+ _never,
47+ _rawPointer,
48+ _void,
49+ _word,
3650};
3751inline Type synthesizeType (SynthesisContext &SC,
3852 SingletonTypeSynthesizer kind) {
3953 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 ();
4257 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 );
4364 }
4465}
4566
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+
46125// / Helper types for variadic synthesis.
47126template <class ... Ss>
48127struct VariadicSynthesizerStorage ;
@@ -51,9 +130,8 @@ template <>
51130struct VariadicSynthesizerStorage <> {
52131 constexpr VariadicSynthesizerStorage () {}
53132
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 {}
57135};
58136template <class Head , class ... Tail>
59137struct VariadicSynthesizerStorage <Head, Tail...> {
@@ -62,24 +140,38 @@ struct VariadicSynthesizerStorage<Head, Tail...> {
62140 constexpr VariadicSynthesizerStorage (Head head, Tail... tail)
63141 : head(head), tail(tail...) {}
64142
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);
71147 }
72148};
73149
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+
74163// / Synthesize tuple type elements.
75164template <class S >
76165TupleTypeElt synthesizeTupleTypeElt (SynthesisContext &SC, S s) {
77166 return synthesizeType (SC, s);
78167}
79- struct SynthesizeTupleTypeElt {
168+ struct CollectTupleTypeElements {
169+ SynthesisContext &SC;
170+ SmallVectorImpl<TupleTypeElt> &Elts;
171+
80172 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) );
83175 }
84176};
85177
@@ -96,7 +188,7 @@ template <class... Elts>
96188Type synthesizeType (SynthesisContext &SC,
97189 const TupleSynthesizer<Elts...> &tuple) {
98190 SmallVector<TupleTypeElt, sizeof ...(Elts)> elts;
99- tuple.Elements .collect ( SC, elts, SynthesizeTupleTypeElt () );
191+ tuple.Elements .visit (CollectTupleTypeElements{ SC, elts} );
100192 return TupleType::get (elts, SC.Context );
101193}
102194
@@ -107,6 +199,7 @@ ParamDecl *synthesizeParamDecl(SynthesisContext &SC, const S &s) {
107199 auto PD = new (SC.Context ) ParamDecl (SourceLoc (), SourceLoc (),
108200 Identifier (), SourceLoc (),
109201 Identifier (), SC.DC );
202+ PD->setSpecifier (ParamSpecifier::Default);
110203 PD->setInterfaceType (type);
111204 PD->setImplicit ();
112205 return PD;
@@ -157,33 +250,39 @@ constexpr ParameterListSynthesizer<Params...> _parameters(Params... ps) {
157250 return {{ps...}};
158251}
159252
160- struct SynthesizeParamDecl {
253+ struct CollectParamDecls {
254+ SynthesisContext &SC;
255+ SmallVectorImpl<ParamDecl*> &Results;
256+
161257 template <class S >
162- ParamDecl * operator ()(SynthesisContext &SC, const S &s) {
258+ void operator ()(const S &s) const {
163259 // Found by argument-dependent lookup.
164- return synthesizeParamDecl (SC, s);
260+ Results. push_back ( synthesizeParamDecl (SC, s) );
165261 }
166262};
167263template <class ... Params>
168264ParameterList *synthesizeParameterList (SynthesisContext &SC,
169265 const ParameterListSynthesizer<Params...> &list) {
170266 SmallVector<ParamDecl*, 4 > decls;
171- list.params .collect ( SC, decls, SynthesizeParamDecl () );
267+ list.params .visit (CollectParamDecls{ SC, decls} );
172268 return ParameterList::create (SC.Context , decls);
173269}
174270
175- struct SynthesizeParamType {
271+ struct CollectParamTypes {
272+ SynthesisContext &SC;
273+ SmallVectorImpl<FunctionType::Param> &Results;
274+
176275 template <class S >
177- FunctionType::Param operator ()(SynthesisContext &SC, const S &s) {
276+ void operator ()(const S &s) const {
178277 // Found by argument-dependent lookup.
179- return synthesizeParamType (SC, s);
278+ Results. push_back ( synthesizeParamType (SC, s) );
180279 }
181280};
182281template <class ... Params>
183282void synthesizeParameterTypes (SynthesisContext &SC,
184283 const ParameterListSynthesizer<Params...> &list,
185284 SmallVectorImpl<FunctionType::Param> &types) {
186- list.params .collect ( SC, types, SynthesizeParamType () );
285+ list.params .visit (CollectParamTypes{ SC, types} );
187286}
188287
189288// / Synthesize function ExtInfo.
0 commit comments