25
25
#include " swift/AST/ProtocolConformanceRef.h"
26
26
#include " swift/AST/TrailingCallArguments.h"
27
27
#include " swift/AST/TypeAlignments.h"
28
- #include " swift/AST/TypeLoc.h"
29
28
#include " swift/AST/Availability.h"
30
29
#include " swift/Basic/Debug.h"
31
30
#include " swift/Basic/InlineBitfield.h"
@@ -555,7 +554,7 @@ class alignas(8) Expr {
555
554
SWIFT_DEBUG_DUMP;
556
555
void dump (raw_ostream &OS, unsigned Indent = 0 ) const ;
557
556
void dump (raw_ostream &OS, llvm::function_ref<Type (Expr *)> getType,
558
- llvm::function_ref<Type (TypeLoc & )> getTypeOfTypeLoc ,
557
+ llvm::function_ref<Type (TypeRepr * )> getTypeOfTypeRepr ,
559
558
llvm::function_ref<Type (KeyPathExpr *E, unsigned index)>
560
559
getTypeOfKeyPathComponent,
561
560
unsigned Indent = 0 ) const ;
@@ -4587,23 +4586,22 @@ class DotSyntaxBaseIgnoredExpr : public Expr {
4587
4586
class ExplicitCastExpr : public Expr {
4588
4587
Expr *SubExpr;
4589
4588
SourceLoc AsLoc;
4590
- TypeLoc CastTy;
4589
+ TypeExpr * const CastTy;
4591
4590
4592
4591
protected:
4593
- ExplicitCastExpr (ExprKind kind, Expr *sub, SourceLoc AsLoc, TypeLoc castTy)
4594
- : Expr(kind, /* Implicit=*/ false ), SubExpr(sub), AsLoc(AsLoc), CastTy(castTy)
4595
- {}
4592
+ ExplicitCastExpr (ExprKind kind, Expr *sub, SourceLoc AsLoc, TypeExpr * castTy)
4593
+ : Expr(kind, /* Implicit=*/ false ), SubExpr(sub), AsLoc(AsLoc),
4594
+ CastTy (castTy) {}
4596
4595
4597
4596
public:
4598
4597
Expr *getSubExpr () const { return SubExpr; }
4599
-
4600
- // / Get the type syntactically spelled in the cast. For some forms of checked
4601
- // / cast this is different from the result type of the expression.
4602
- TypeLoc &getCastTypeLoc () { return CastTy; }
4603
4598
4604
4599
// / Get the type syntactically spelled in the cast. For some forms of checked
4605
4600
// / cast this is different from the result type of the expression.
4606
- TypeLoc getCastTypeLoc () const { return CastTy; }
4601
+ Type getCastType () const { return CastTy->getInstanceType (); }
4602
+ void setCastType (Type type);
4603
+
4604
+ TypeRepr *getCastTypeRepr () const { return CastTy->getTypeRepr (); }
4607
4605
4608
4606
void setSubExpr (Expr *E) { SubExpr = E; }
4609
4607
@@ -4619,7 +4617,7 @@ class ExplicitCastExpr : public Expr {
4619
4617
}
4620
4618
4621
4619
SourceRange getSourceRange () const {
4622
- SourceRange castTyRange = CastTy. getSourceRange ();
4620
+ const SourceRange castTyRange = CastTy-> getSourceRange ();
4623
4621
if (castTyRange.isInvalid ())
4624
4622
return SubExpr->getSourceRange ();
4625
4623
@@ -4644,14 +4642,13 @@ StringRef getCheckedCastKindName(CheckedCastKind kind);
4644
4642
// / Abstract base class for checked casts 'as' and 'is'. These represent
4645
4643
// / casts that can dynamically fail.
4646
4644
class CheckedCastExpr : public ExplicitCastExpr {
4647
- public:
4648
- CheckedCastExpr (ExprKind kind,
4649
- Expr *sub, SourceLoc asLoc, TypeLoc castTy)
4650
- : ExplicitCastExpr(kind, sub, asLoc, castTy)
4651
- {
4645
+ protected:
4646
+ CheckedCastExpr (ExprKind kind, Expr *sub, SourceLoc asLoc, TypeExpr *castTy)
4647
+ : ExplicitCastExpr(kind, sub, asLoc, castTy) {
4652
4648
Bits.CheckedCastExpr .CastKind = unsigned (CheckedCastKind::Unresolved);
4653
4649
}
4654
-
4650
+
4651
+ public:
4655
4652
// / Return the semantic kind of cast performed.
4656
4653
CheckedCastKind getCastKind () const {
4657
4654
return CheckedCastKind (Bits.CheckedCastExpr .CastKind );
@@ -4675,22 +4672,20 @@ class CheckedCastExpr : public ExplicitCastExpr {
4675
4672
// / from a value of some type to some specified subtype and fails dynamically
4676
4673
// / if the value does not have that type.
4677
4674
// / Spelled 'a as! T' and produces a value of type 'T'.
4678
- class ForcedCheckedCastExpr : public CheckedCastExpr {
4675
+ class ForcedCheckedCastExpr final : public CheckedCastExpr {
4679
4676
SourceLoc ExclaimLoc;
4680
4677
4681
- public:
4682
4678
ForcedCheckedCastExpr (Expr *sub, SourceLoc asLoc, SourceLoc exclaimLoc,
4683
- TypeLoc type)
4684
- : CheckedCastExpr(ExprKind::ForcedCheckedCast,
4685
- sub, asLoc, type),
4686
- ExclaimLoc (exclaimLoc)
4687
- {
4688
- }
4679
+ TypeExpr *type)
4680
+ : CheckedCastExpr(ExprKind::ForcedCheckedCast, sub, asLoc, type),
4681
+ ExclaimLoc (exclaimLoc) {}
4689
4682
4690
- ForcedCheckedCastExpr (SourceLoc asLoc, SourceLoc exclaimLoc, TypeLoc type)
4691
- : ForcedCheckedCastExpr(nullptr , asLoc, exclaimLoc, type)
4692
- {
4693
- }
4683
+ public:
4684
+ static ForcedCheckedCastExpr *create (ASTContext &ctx, SourceLoc asLoc,
4685
+ SourceLoc exclaimLoc, TypeRepr *tyRepr);
4686
+
4687
+ static ForcedCheckedCastExpr *createImplicit (ASTContext &ctx, Expr *sub,
4688
+ Type castTy);
4694
4689
4695
4690
// / Retrieve the location of the '!' that follows 'as'.
4696
4691
SourceLoc getExclaimLoc () const { return ExclaimLoc; }
@@ -4704,21 +4699,24 @@ class ForcedCheckedCastExpr : public CheckedCastExpr {
4704
4699
// / from a type to some subtype and produces an Optional value, which will be
4705
4700
// / .Some(x) if the cast succeeds, or .None if the cast fails.
4706
4701
// / Spelled 'a as? T' and produces a value of type 'T?'.
4707
- class ConditionalCheckedCastExpr : public CheckedCastExpr {
4702
+ class ConditionalCheckedCastExpr final : public CheckedCastExpr {
4708
4703
SourceLoc QuestionLoc;
4709
4704
4710
- public:
4711
4705
ConditionalCheckedCastExpr (Expr *sub, SourceLoc asLoc, SourceLoc questionLoc,
4712
- TypeLoc type)
4713
- : CheckedCastExpr(ExprKind::ConditionalCheckedCast,
4714
- sub, asLoc, type),
4715
- QuestionLoc (questionLoc)
4716
- { }
4717
-
4718
- ConditionalCheckedCastExpr (SourceLoc asLoc, SourceLoc questionLoc,
4719
- TypeLoc type)
4720
- : ConditionalCheckedCastExpr(nullptr , asLoc, questionLoc, type)
4721
- {}
4706
+ TypeExpr *type)
4707
+ : CheckedCastExpr(ExprKind::ConditionalCheckedCast, sub, asLoc, type),
4708
+ QuestionLoc (questionLoc) {}
4709
+
4710
+ public:
4711
+ static ConditionalCheckedCastExpr *create (ASTContext &ctx, SourceLoc asLoc,
4712
+ SourceLoc questionLoc,
4713
+ TypeRepr *tyRepr);
4714
+
4715
+ static ConditionalCheckedCastExpr *createImplicit (ASTContext &ctx, Expr *sub,
4716
+ Type castTy);
4717
+
4718
+ static ConditionalCheckedCastExpr *
4719
+ createImplicit (ASTContext &ctx, Expr *sub, TypeRepr *tyRepr, Type castTy);
4722
4720
4723
4721
// / Retrieve the location of the '?' that follows 'as'.
4724
4722
SourceLoc getQuestionLoc () const { return QuestionLoc; }
@@ -4733,16 +4731,13 @@ class ConditionalCheckedCastExpr : public CheckedCastExpr {
4733
4731
// / of the type and 'a as T' would succeed, false otherwise.
4734
4732
// /
4735
4733
// / FIXME: We should support type queries with a runtime metatype value too.
4736
- class IsExpr : public CheckedCastExpr {
4734
+ class IsExpr final : public CheckedCastExpr {
4735
+ IsExpr (Expr *sub, SourceLoc isLoc, TypeExpr *type)
4736
+ : CheckedCastExpr(ExprKind::Is, sub, isLoc, type) {}
4737
+
4737
4738
public:
4738
- IsExpr (Expr *sub, SourceLoc isLoc, TypeLoc type)
4739
- : CheckedCastExpr(ExprKind::Is, sub, isLoc, type)
4740
- {}
4741
-
4742
- IsExpr (SourceLoc isLoc, TypeLoc type)
4743
- : IsExpr(nullptr , isLoc, type)
4744
- {}
4745
-
4739
+ static IsExpr *create (ASTContext &ctx, SourceLoc isLoc, TypeRepr *tyRepr);
4740
+
4746
4741
static bool classof (const Expr *E) {
4747
4742
return E->getKind () == ExprKind::Is;
4748
4743
}
@@ -4751,35 +4746,29 @@ class IsExpr : public CheckedCastExpr {
4751
4746
// / Represents an explicit coercion from a value to a specific type.
4752
4747
// /
4753
4748
// / Spelled 'a as T' and produces a value of type 'T'.
4754
- class CoerceExpr : public ExplicitCastExpr {
4749
+ class CoerceExpr final : public ExplicitCastExpr {
4755
4750
// / Since there is already `asLoc` location,
4756
4751
// / we use it to store `start` of the initializer
4757
4752
// / call source range to save some storage.
4758
4753
SourceLoc InitRangeEnd;
4759
4754
4760
- public:
4761
- CoerceExpr (Expr *sub, SourceLoc asLoc, TypeLoc type)
4762
- : ExplicitCastExpr(ExprKind::Coerce, sub, asLoc, type)
4763
- { }
4755
+ CoerceExpr (Expr *sub, SourceLoc asLoc, TypeExpr *type)
4756
+ : ExplicitCastExpr(ExprKind::Coerce, sub, asLoc, type) {}
4764
4757
4765
- CoerceExpr (SourceLoc asLoc, TypeLoc type)
4766
- : CoerceExpr(nullptr , asLoc, type)
4767
- { }
4768
-
4769
- private:
4770
- CoerceExpr (SourceRange initRange, Expr *literal, TypeLoc type)
4771
- : ExplicitCastExpr(ExprKind::Coerce, literal, initRange.Start,
4772
- type), InitRangeEnd(initRange.End)
4773
- { setImplicit (); }
4758
+ CoerceExpr (SourceRange initRange, Expr *literal, TypeExpr *type)
4759
+ : ExplicitCastExpr(ExprKind::Coerce, literal, initRange.Start, type),
4760
+ InitRangeEnd (initRange.End) {}
4774
4761
4775
4762
public:
4763
+ static CoerceExpr *create (ASTContext &ctx, SourceLoc asLoc, TypeRepr *tyRepr);
4764
+
4765
+ static CoerceExpr *createImplicit (ASTContext &ctx, Expr *sub, Type castTy);
4766
+
4776
4767
// / Create an implicit coercion expression for literal initialization
4777
4768
// / preserving original source information, this way original call
4778
4769
// / could be recreated if needed.
4779
4770
static CoerceExpr *forLiteralInit (ASTContext &ctx, Expr *literal,
4780
- SourceRange range, TypeLoc literalType) {
4781
- return new (ctx) CoerceExpr (range, literal, literalType);
4782
- }
4771
+ SourceRange range, TypeRepr *literalTyRepr);
4783
4772
4784
4773
bool isLiteralInit () const { return InitRangeEnd.isValid (); }
4785
4774
0 commit comments