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/Expr.h"
19
20
#include " swift/AST/GenericParamList.h"
20
21
#include " swift/AST/ParameterList.h"
21
22
@@ -51,6 +52,7 @@ enum SingletonTypeSynthesizer {
51
52
_rawUnsafeContinuation,
52
53
_void,
53
54
_word,
55
+ _swiftInt, // Swift.Int
54
56
_serialExecutor, // the '_Concurrency.SerialExecutor' protocol
55
57
_taskExecutor, // the '_Concurrency.TaskExecutor' protocol
56
58
_actor, // the '_Concurrency.Actor' protocol
@@ -70,6 +72,7 @@ inline Type synthesizeType(SynthesisContext &SC,
70
72
case _void: return SC.Context .TheEmptyTupleType ;
71
73
case _word: return BuiltinIntegerType::get (BuiltinIntegerWidth::pointer (),
72
74
SC.Context );
75
+ case _swiftInt: return SC.Context .getIntType ();
73
76
case _serialExecutor:
74
77
return SC.Context .getProtocol (KnownProtocolKind::SerialExecutor)
75
78
->getDeclaredInterfaceType ();
@@ -88,6 +91,11 @@ inline Type synthesizeType(SynthesisContext &SC,
88
91
}
89
92
}
90
93
94
+ enum RepresentationSynthesizer {
95
+ _thin,
96
+ _thick
97
+ };
98
+
91
99
// / A synthesizer which generates an integer type.
92
100
struct IntegerTypeSynthesizer {
93
101
unsigned BitWidth;
@@ -131,6 +139,23 @@ Type synthesizeType(SynthesisContext &SC,
131
139
return MetatypeType::get (synthesizeType (SC, M.Sub ));
132
140
}
133
141
142
+ template <class S >
143
+ struct RepMetatypeTypeSynthesizer {
144
+ S Sub;
145
+ RepresentationSynthesizer Rep;
146
+ };
147
+ template <class S >
148
+ constexpr RepMetatypeTypeSynthesizer<S>
149
+ _metatype (S sub, RepresentationSynthesizer rep ) {
150
+ return {sub, rep};
151
+ }
152
+ template <class S >
153
+ Type synthesizeType (SynthesisContext &SC,
154
+ const RepMetatypeTypeSynthesizer<S> &M) {
155
+ auto instanceType = synthesizeType (SC, M.Sub );
156
+ return MetatypeType::get (instanceType, synthesizeMetatypeRepresentation (M.Rep ));
157
+ }
158
+
134
159
// / A synthesizer which generates an existential type from a requirement type.
135
160
template <class S >
136
161
struct ExistentialTypeSynthesizer {
@@ -146,6 +171,16 @@ Type synthesizeType(SynthesisContext &SC,
146
171
return ExistentialType::get (synthesizeType (SC, M.Sub ));
147
172
}
148
173
174
+ MetatypeRepresentation
175
+ inline synthesizeMetatypeRepresentation (RepresentationSynthesizer rep) {
176
+ switch (rep) {
177
+ case _thin: return MetatypeRepresentation::Thin;
178
+ case _thick: return MetatypeRepresentation::Thick;
179
+ // TOOD: maybe add _objc?
180
+ }
181
+ llvm_unreachable (" bad kind" );
182
+ }
183
+
149
184
// / A synthesizer which generates an existential metatype type.
150
185
template <class S >
151
186
struct ExistentialMetatypeTypeSynthesizer {
@@ -160,6 +195,22 @@ Type synthesizeType(SynthesisContext &SC,
160
195
const ExistentialMetatypeTypeSynthesizer<S> &M) {
161
196
return ExistentialMetatypeType::get (synthesizeType (SC, M.Sub ));
162
197
}
198
+ template <class S >
199
+ struct RepExistentialMetatypeTypeSynthesizer {
200
+ S Sub;
201
+ RepresentationSynthesizer Rep;
202
+ };
203
+ template <class S >
204
+ constexpr RepExistentialMetatypeTypeSynthesizer<S>
205
+ _existentialMetatype (S sub, RepresentationSynthesizer rep) {
206
+ return {sub, rep};
207
+ }
208
+ template <class S >
209
+ Type synthesizeType (SynthesisContext &SC,
210
+ const RepExistentialMetatypeTypeSynthesizer<S> &M) {
211
+ return ExistentialMetatypeType::get (synthesizeType (SC, M.Sub ),
212
+ synthesizeMetatypeRepresentation (M.Rep ));
213
+ }
163
214
164
215
// / A synthesizer that generates a MoveOnly wrapper of a type.
165
216
template <class S >
@@ -249,10 +300,12 @@ Type synthesizeType(SynthesisContext &SC,
249
300
250
301
// / Synthesize parameter declarations.
251
302
template <class S >
252
- ParamDecl *synthesizeParamDecl (SynthesisContext &SC, const S &s) {
303
+ ParamDecl *synthesizeParamDecl (SynthesisContext &SC, const S &s,
304
+ const char *label = nullptr ) {
305
+ auto argLabelIdent = (label ? SC.Context .getIdentifier (label) : Identifier ());
253
306
auto type = synthesizeType (SC, s);
254
307
auto PD = new (SC.Context ) ParamDecl (SourceLoc (), SourceLoc (),
255
- Identifier () , SourceLoc (),
308
+ argLabelIdent , SourceLoc (),
256
309
Identifier (), SC.DC );
257
310
PD->setSpecifier (ParamSpecifier::Default);
258
311
PD->setInterfaceType (type);
@@ -280,8 +333,9 @@ constexpr SpecifiedParamSynthesizer<G> _inout(G sub) {
280
333
}
281
334
template <class S >
282
335
ParamDecl *synthesizeParamDecl (SynthesisContext &SC,
283
- const SpecifiedParamSynthesizer<S> &s) {
284
- auto param = synthesizeParamDecl (SC, s.sub );
336
+ const SpecifiedParamSynthesizer<S> &s,
337
+ const char *label = nullptr ) {
338
+ auto param = synthesizeParamDecl (SC, s.sub , label);
285
339
param->setSpecifier (s.specifier );
286
340
return param;
287
341
}
@@ -295,6 +349,61 @@ FunctionType::Param synthesizeParamType(SynthesisContext &SC,
295
349
return param.withFlags (flags);
296
350
}
297
351
352
+ template <class S >
353
+ void synthesizeDefaultArgument (SynthesisContext &SC, const S &s,
354
+ ParamDecl *param) {
355
+ synthesizeDefaultArgumentFromExpr (SC, s, param);
356
+ }
357
+ template <class S >
358
+ void synthesizeDefaultArgumentFromExpr (SynthesisContext &SC, const S &s,
359
+ ParamDecl *param) {
360
+ // FIXME: this works except that we tend to crash in diagnostics trying
361
+ // to render the default argument if you mess up the call.
362
+ auto expr = synthesizeExpr (SC, s);
363
+ param->setDefaultArgumentKind (DefaultArgumentKind::Normal);
364
+ param->setDefaultExpr (expr, /* type checked*/ false );
365
+ }
366
+
367
+ // / Default arguments.
368
+ template <class S , class A >
369
+ struct DefaultedSynthesizer { S sub; A arg; };
370
+ template <class S , class A >
371
+ constexpr DefaultedSynthesizer<S, A> _defaulted (S sub, A arg) {
372
+ return {sub, arg};
373
+ }
374
+ template <class S , class A >
375
+ ParamDecl *synthesizeParamDecl (SynthesisContext &SC,
376
+ const DefaultedSynthesizer<S, A> &s,
377
+ const char *label = nullptr ) {
378
+ auto param = synthesizeParamDecl (SC, s.sub , label);
379
+ synthesizeDefaultArgument (SC, s.arg , param);
380
+ return param;
381
+ }
382
+ template <class S , class A >
383
+ FunctionType::Param synthesizeParamType (SynthesisContext &SC,
384
+ const DefaultedSynthesizer<S, A> &s) {
385
+ return synthesizeParamType (s.sub );
386
+ }
387
+
388
+ // / Labels.
389
+ template <class S >
390
+ struct LabelSynthesizer { const char *label; S sub; };
391
+ template <class S >
392
+ constexpr LabelSynthesizer<S> _label (const char *label, S sub) {
393
+ return {label, sub};
394
+ }
395
+ template <class S >
396
+ ParamDecl *synthesizeParamDecl (SynthesisContext &SC,
397
+ const LabelSynthesizer<S> &s) {
398
+ return synthesizeParamDecl (SC, s.sub , s.label );
399
+ }
400
+ template <class S >
401
+ FunctionType::Param synthesizeParamType (SynthesisContext &SC,
402
+ const LabelSynthesizer<S> &s) {
403
+ auto label = SC.Context .getIdentifier (s.label );
404
+ return synthesizeParamType (SC, s.sub ).withLabel (label);
405
+ }
406
+
298
407
// / Synthesize a parameter list.
299
408
template <class ... Params>
300
409
struct ParameterListSynthesizer {
@@ -341,22 +450,21 @@ void synthesizeParameterTypes(SynthesisContext &SC,
341
450
}
342
451
343
452
// / Synthesize function ExtInfo.
344
- enum FunctionRepresentationSynthesizer {
345
- _thin,
346
- _thick
347
- };
348
453
template <class S > struct ThrowsSynthesizer { S sub; };
349
454
template <class S > struct AsyncSynthesizer { S sub; };
350
455
template <class S > struct NoescapeSynthesizer { S sub; };
456
+ template <class S > struct SendableModSynthesizer { S sub; };
351
457
template <class S >
352
458
constexpr ThrowsSynthesizer<S> _throws (S sub) { return {sub}; }
353
459
template <class S >
354
460
constexpr AsyncSynthesizer<S> _async (S sub) { return {sub}; }
355
461
template <class S >
356
462
constexpr NoescapeSynthesizer<S> _noescape (S sub) { return {sub}; }
463
+ template <class S >
464
+ constexpr SendableModSynthesizer<S> _sendable (S sub) { return {sub}; }
357
465
358
466
inline ASTExtInfo synthesizeExtInfo (SynthesisContext &SC,
359
- FunctionRepresentationSynthesizer kind) {
467
+ RepresentationSynthesizer kind) {
360
468
switch (kind) {
361
469
case _thin: return ASTExtInfo ().withRepresentation (
362
470
FunctionTypeRepresentation::Thin);
@@ -379,6 +487,11 @@ ASTExtInfo synthesizeExtInfo(SynthesisContext &SC,
379
487
const NoescapeSynthesizer<S> &s) {
380
488
return synthesizeExtInfo (SC, s.sub ).withNoEscape ();
381
489
}
490
+ template <class S >
491
+ ASTExtInfo synthesizeExtInfo (SynthesisContext &SC,
492
+ const SendableModSynthesizer<S> &s) {
493
+ return synthesizeExtInfo (SC, s.sub ).withConcurrent ();
494
+ }
382
495
383
496
// / Synthesize a function type.
384
497
template <class ExtInfoS , class ResultS , class ParamsS >
@@ -414,6 +527,36 @@ Type synthesizeType(SynthesisContext &SC, const OptionalSynthesizer<S> &s) {
414
527
return OptionalType::get (synthesizeType (SC, s.sub ));
415
528
}
416
529
530
+ // / Expressions.
531
+ enum SingletonExprSynthesizer {
532
+ _nil
533
+ };
534
+ inline Expr *synthesizeExpr (SynthesisContext &SC, SingletonExprSynthesizer s) {
535
+ switch (s) {
536
+ case _nil:
537
+ return new (SC.Context ) NilLiteralExpr (SourceLoc (), /* implicit*/ true );
538
+ }
539
+ llvm_unreachable (" bad singleton kind" );
540
+ }
541
+ inline void synthesizeDefaultArgument (SynthesisContext &SC,
542
+ SingletonExprSynthesizer s,
543
+ ParamDecl *param) {
544
+ switch (s) {
545
+ case _nil: {
546
+ auto expr = synthesizeExpr (SC, s);
547
+ param->setDefaultArgumentKind (DefaultArgumentKind::NilLiteral);
548
+ param->setDefaultExpr (expr, /* type checked*/ false );
549
+ return ;
550
+ }
551
+
552
+ /*
553
+ default:
554
+ synthesizeDefaultArgumentFromExpr(SC, s, param);
555
+ return;
556
+ */
557
+ }
558
+ }
559
+
417
560
} // end namespace swift
418
561
419
562
#endif
0 commit comments