23
23
#include " swift/AST/Stmt.h"
24
24
#include " swift/AST/TypeLoc.h"
25
25
#include " swift/Sema/ConstraintLocator.h"
26
+ #include " swift/Sema/ContextualTypeInfo.h"
26
27
27
28
namespace swift {
28
29
@@ -71,18 +72,8 @@ class SyntacticElementTarget {
71
72
// / type-checked.
72
73
DeclContext *dc;
73
74
74
- // TODO: Fold the 3 below fields into ContextualTypeInfo
75
-
76
- // / The purpose of the contextual type.
77
- ContextualTypePurpose contextualPurpose;
78
-
79
- // / The type to which the expression should be converted.
80
- TypeLoc convertType;
81
-
82
- // / The locator for the contextual type conversion constraint, or
83
- // / \c nullptr to use the default locator which is anchored directly on
84
- // / the expression.
85
- ConstraintLocator *convertTypeLocator;
75
+ // / The contextual type info for the expression.
76
+ ContextualTypeInfo contextualInfo;
86
77
87
78
// / When initializing a pattern from the expression, this is the
88
79
// / pattern.
@@ -169,26 +160,15 @@ class SyntacticElementTarget {
169
160
void maybeApplyPropertyWrapper ();
170
161
171
162
public:
172
- SyntacticElementTarget (Expr *expr, DeclContext *dc,
173
- ContextualTypePurpose contextualPurpose,
174
- Type convertType,
175
- ConstraintLocator *convertTypeLocator,
176
- bool isDiscarded)
177
- : SyntacticElementTarget(expr, dc, contextualPurpose,
178
- TypeLoc::withoutLoc (convertType),
179
- convertTypeLocator, isDiscarded) {}
180
-
181
163
SyntacticElementTarget (Expr *expr, DeclContext *dc,
182
164
ContextualTypePurpose contextualPurpose,
183
165
Type convertType, bool isDiscarded)
184
- : SyntacticElementTarget(expr, dc, contextualPurpose, convertType,
185
- /* convertTypeLocator*/ nullptr , isDiscarded) {}
166
+ : SyntacticElementTarget(
167
+ expr, dc, ContextualTypeInfo(convertType, contextualPurpose),
168
+ isDiscarded) {}
186
169
187
170
SyntacticElementTarget (Expr *expr, DeclContext *dc,
188
- ContextualTypePurpose contextualPurpose,
189
- TypeLoc convertType,
190
- ConstraintLocator *convertTypeLocator,
191
- bool isDiscarded);
171
+ ContextualTypeInfo contextualInfo, bool isDiscarded);
192
172
193
173
SyntacticElementTarget (ClosureExpr *closure, Type convertType) {
194
174
kind = Kind::closure;
@@ -375,26 +355,31 @@ class SyntacticElementTarget {
375
355
llvm_unreachable (" invalid decl context type" );
376
356
}
377
357
378
- ContextualTypePurpose getExprContextualTypePurpose () const {
358
+ // / Get the contextual type info for an expression target.
359
+ ContextualTypeInfo getExprContextualTypeInfo () const {
379
360
assert (kind == Kind::expression);
380
- return expression.contextualPurpose ;
361
+ return expression.contextualInfo ;
362
+ }
363
+
364
+ // / Get the contextual type purpose for an expression target.
365
+ ContextualTypePurpose getExprContextualTypePurpose () const {
366
+ return getExprContextualTypeInfo ().purpose ;
381
367
}
382
368
369
+ // / Get the contextual type for an expression target.
383
370
Type getExprContextualType () const {
384
371
return getExprContextualTypeLoc ().getType ();
385
372
}
386
373
374
+ // / Get the contextual type for an expression target.
387
375
TypeLoc getExprContextualTypeLoc () const {
388
- assert (kind == Kind::expression);
389
-
390
376
// For an @autoclosure parameter, the conversion type is
391
377
// the result of the function type.
392
- if (FunctionType *autoclosureParamType = getAsAutoclosureParamType ()) {
393
- return TypeLoc (expression.convertType .getTypeRepr (),
394
- autoclosureParamType->getResult ());
395
- }
378
+ auto typeLoc = getExprContextualTypeInfo ().typeLoc ;
379
+ if (FunctionType *autoclosureParamType = getAsAutoclosureParamType ())
380
+ return TypeLoc (typeLoc.getTypeRepr (), autoclosureParamType->getResult ());
396
381
397
- return expression. convertType ;
382
+ return typeLoc ;
398
383
}
399
384
400
385
// / Retrieve the type to which an expression should be converted, or
@@ -408,33 +393,32 @@ class SyntacticElementTarget {
408
393
// / Retrieve the conversion type locator for the expression, or \c nullptr
409
394
// / if it has not been set.
410
395
ConstraintLocator *getExprConvertTypeLocator () const {
411
- assert (kind == Kind::expression);
412
- return expression.convertTypeLocator ;
396
+ return getExprContextualTypeInfo ().locator ;
413
397
}
414
398
415
399
// / Returns the autoclosure parameter type, or \c nullptr if the
416
400
// / expression has a different kind of context.
417
401
FunctionType *getAsAutoclosureParamType () const {
418
- assert (kind == Kind::expression);
419
- if (expression. contextualPurpose == CTP_AutoclosureDefaultParameter)
420
- return expression. convertType . getType ()-> castTo <FunctionType>();
402
+ if ( getExprContextualTypePurpose () == CTP_AutoclosureDefaultParameter)
403
+ return getExprContextualTypeInfo (). getType ()-> castTo <FunctionType>();
404
+
421
405
return nullptr ;
422
406
}
423
407
424
408
void setExprConversionType (Type type) {
425
409
assert (kind == Kind::expression);
426
- expression.convertType = TypeLoc::withoutLoc (type);
410
+ expression.contextualInfo . typeLoc = TypeLoc::withoutLoc (type);
427
411
}
428
412
429
413
void setExprConversionTypeLoc (TypeLoc type) {
430
414
assert (kind == Kind::expression);
431
- expression.convertType = type;
415
+ expression.contextualInfo . typeLoc = type;
432
416
}
433
417
434
418
// / Whether this target is for an initialization expression and pattern.
435
419
bool isForInitialization () const {
436
420
return kind == Kind::expression &&
437
- expression. contextualPurpose == CTP_Initialization;
421
+ getExprContextualTypePurpose () == CTP_Initialization;
438
422
}
439
423
440
424
// / For a pattern initialization target, retrieve the pattern.
@@ -448,7 +432,7 @@ class SyntacticElementTarget {
448
432
449
433
ExprPattern *getExprPattern () const {
450
434
assert (kind == Kind::expression);
451
- assert (expression. contextualPurpose == CTP_ExprPattern);
435
+ assert (getExprContextualTypePurpose () == CTP_ExprPattern);
452
436
return cast<ExprPattern>(expression.pattern );
453
437
}
454
438
@@ -575,11 +559,16 @@ class SyntacticElementTarget {
575
559
return ;
576
560
}
577
561
578
- assert (kind == Kind::expression);
579
- assert (expression.contextualPurpose == CTP_Initialization ||
580
- expression.contextualPurpose == CTP_ForEachStmt ||
581
- expression.contextualPurpose == CTP_ForEachSequence ||
582
- expression.contextualPurpose == CTP_ExprPattern);
562
+ switch (getExprContextualTypePurpose ()) {
563
+ case CTP_Initialization:
564
+ case CTP_ForEachStmt:
565
+ case CTP_ForEachSequence:
566
+ case CTP_ExprPattern:
567
+ break ;
568
+ default :
569
+ assert (false && " Unexpected contextual type purpose" );
570
+ break ;
571
+ }
583
572
expression.pattern = pattern;
584
573
}
585
574
0 commit comments