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
@@ -171,7 +172,7 @@ Type synthesizeType(SynthesisContext &SC,
171
172
}
172
173
173
174
MetatypeRepresentation
174
- synthesizeMetatypeRepresentation (RepresentationSynthesizer rep) {
175
+ inline synthesizeMetatypeRepresentation (RepresentationSynthesizer rep) {
175
176
switch (rep) {
176
177
case _thin: return MetatypeRepresentation::Thin;
177
178
case _thick: return MetatypeRepresentation::Thick;
@@ -299,10 +300,12 @@ Type synthesizeType(SynthesisContext &SC,
299
300
300
301
// / Synthesize parameter declarations.
301
302
template <class S >
302
- 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 ());
303
306
auto type = synthesizeType (SC, s);
304
307
auto PD = new (SC.Context ) ParamDecl (SourceLoc (), SourceLoc (),
305
- Identifier () , SourceLoc (),
308
+ argLabelIdent , SourceLoc (),
306
309
Identifier (), SC.DC );
307
310
PD->setSpecifier (ParamSpecifier::Default);
308
311
PD->setInterfaceType (type);
@@ -330,8 +333,9 @@ constexpr SpecifiedParamSynthesizer<G> _inout(G sub) {
330
333
}
331
334
template <class S >
332
335
ParamDecl *synthesizeParamDecl (SynthesisContext &SC,
333
- const SpecifiedParamSynthesizer<S> &s) {
334
- auto param = synthesizeParamDecl (SC, s.sub );
336
+ const SpecifiedParamSynthesizer<S> &s,
337
+ const char *label = nullptr ) {
338
+ auto param = synthesizeParamDecl (SC, s.sub , label);
335
339
param->setSpecifier (s.specifier );
336
340
return param;
337
341
}
@@ -345,6 +349,61 @@ FunctionType::Param synthesizeParamType(SynthesisContext &SC,
345
349
return param.withFlags (flags);
346
350
}
347
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
+
348
407
// / Synthesize a parameter list.
349
408
template <class ... Params>
350
409
struct ParameterListSynthesizer {
@@ -468,6 +527,36 @@ Type synthesizeType(SynthesisContext &SC, const OptionalSynthesizer<S> &s) {
468
527
return OptionalType::get (synthesizeType (SC, s.sub ));
469
528
}
470
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
+
471
560
} // end namespace swift
472
561
473
562
#endif
0 commit comments