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 ;
@@ -4591,23 +4590,22 @@ class DotSyntaxBaseIgnoredExpr : public Expr {
4591
4590
class ExplicitCastExpr : public Expr {
4592
4591
Expr *SubExpr;
4593
4592
SourceLoc AsLoc;
4594
- TypeLoc CastTy;
4593
+ TypeExpr * const CastTy;
4595
4594
4596
4595
protected:
4597
- ExplicitCastExpr (ExprKind kind, Expr *sub, SourceLoc AsLoc, TypeLoc castTy)
4598
- : Expr(kind, /* Implicit=*/ false ), SubExpr(sub), AsLoc(AsLoc), CastTy(castTy)
4599
- {}
4596
+ ExplicitCastExpr (ExprKind kind, Expr *sub, SourceLoc AsLoc, TypeExpr * castTy)
4597
+ : Expr(kind, /* Implicit=*/ false ), SubExpr(sub), AsLoc(AsLoc),
4598
+ CastTy (castTy) {}
4600
4599
4601
4600
public:
4602
4601
Expr *getSubExpr () const { return SubExpr; }
4603
-
4604
- // / Get the type syntactically spelled in the cast. For some forms of checked
4605
- // / cast this is different from the result type of the expression.
4606
- TypeLoc &getCastTypeLoc () { return CastTy; }
4607
4602
4608
4603
// / Get the type syntactically spelled in the cast. For some forms of checked
4609
4604
// / cast this is different from the result type of the expression.
4610
- TypeLoc getCastTypeLoc () const { return CastTy; }
4605
+ Type getCastType () const { return CastTy->getInstanceType (); }
4606
+ void setCastType (Type type);
4607
+
4608
+ TypeRepr *getCastTypeRepr () const { return CastTy->getTypeRepr (); }
4611
4609
4612
4610
void setSubExpr (Expr *E) { SubExpr = E; }
4613
4611
@@ -4623,7 +4621,7 @@ class ExplicitCastExpr : public Expr {
4623
4621
}
4624
4622
4625
4623
SourceRange getSourceRange () const {
4626
- SourceRange castTyRange = CastTy. getSourceRange ();
4624
+ const SourceRange castTyRange = CastTy-> getSourceRange ();
4627
4625
if (castTyRange.isInvalid ())
4628
4626
return SubExpr->getSourceRange ();
4629
4627
@@ -4648,14 +4646,13 @@ StringRef getCheckedCastKindName(CheckedCastKind kind);
4648
4646
// / Abstract base class for checked casts 'as' and 'is'. These represent
4649
4647
// / casts that can dynamically fail.
4650
4648
class CheckedCastExpr : public ExplicitCastExpr {
4651
- public:
4652
- CheckedCastExpr (ExprKind kind,
4653
- Expr *sub, SourceLoc asLoc, TypeLoc castTy)
4654
- : ExplicitCastExpr(kind, sub, asLoc, castTy)
4655
- {
4649
+ protected:
4650
+ CheckedCastExpr (ExprKind kind, Expr *sub, SourceLoc asLoc, TypeExpr *castTy)
4651
+ : ExplicitCastExpr(kind, sub, asLoc, castTy) {
4656
4652
Bits.CheckedCastExpr .CastKind = unsigned (CheckedCastKind::Unresolved);
4657
4653
}
4658
-
4654
+
4655
+ public:
4659
4656
// / Return the semantic kind of cast performed.
4660
4657
CheckedCastKind getCastKind () const {
4661
4658
return CheckedCastKind (Bits.CheckedCastExpr .CastKind );
@@ -4679,22 +4676,20 @@ class CheckedCastExpr : public ExplicitCastExpr {
4679
4676
// / from a value of some type to some specified subtype and fails dynamically
4680
4677
// / if the value does not have that type.
4681
4678
// / Spelled 'a as! T' and produces a value of type 'T'.
4682
- class ForcedCheckedCastExpr : public CheckedCastExpr {
4679
+ class ForcedCheckedCastExpr final : public CheckedCastExpr {
4683
4680
SourceLoc ExclaimLoc;
4684
4681
4685
- public:
4686
4682
ForcedCheckedCastExpr (Expr *sub, SourceLoc asLoc, SourceLoc exclaimLoc,
4687
- TypeLoc type)
4688
- : CheckedCastExpr(ExprKind::ForcedCheckedCast,
4689
- sub, asLoc, type),
4690
- ExclaimLoc (exclaimLoc)
4691
- {
4692
- }
4683
+ TypeExpr *type)
4684
+ : CheckedCastExpr(ExprKind::ForcedCheckedCast, sub, asLoc, type),
4685
+ ExclaimLoc (exclaimLoc) {}
4693
4686
4694
- ForcedCheckedCastExpr (SourceLoc asLoc, SourceLoc exclaimLoc, TypeLoc type)
4695
- : ForcedCheckedCastExpr(nullptr , asLoc, exclaimLoc, type)
4696
- {
4697
- }
4687
+ public:
4688
+ static ForcedCheckedCastExpr *create (ASTContext &ctx, SourceLoc asLoc,
4689
+ SourceLoc exclaimLoc, TypeRepr *tyRepr);
4690
+
4691
+ static ForcedCheckedCastExpr *createImplicit (ASTContext &ctx, Expr *sub,
4692
+ Type castTy);
4698
4693
4699
4694
// / Retrieve the location of the '!' that follows 'as'.
4700
4695
SourceLoc getExclaimLoc () const { return ExclaimLoc; }
@@ -4708,21 +4703,24 @@ class ForcedCheckedCastExpr : public CheckedCastExpr {
4708
4703
// / from a type to some subtype and produces an Optional value, which will be
4709
4704
// / .Some(x) if the cast succeeds, or .None if the cast fails.
4710
4705
// / Spelled 'a as? T' and produces a value of type 'T?'.
4711
- class ConditionalCheckedCastExpr : public CheckedCastExpr {
4706
+ class ConditionalCheckedCastExpr final : public CheckedCastExpr {
4712
4707
SourceLoc QuestionLoc;
4713
4708
4714
- public:
4715
4709
ConditionalCheckedCastExpr (Expr *sub, SourceLoc asLoc, SourceLoc questionLoc,
4716
- TypeLoc type)
4717
- : CheckedCastExpr(ExprKind::ConditionalCheckedCast,
4718
- sub, asLoc, type),
4719
- QuestionLoc (questionLoc)
4720
- { }
4721
-
4722
- ConditionalCheckedCastExpr (SourceLoc asLoc, SourceLoc questionLoc,
4723
- TypeLoc type)
4724
- : ConditionalCheckedCastExpr(nullptr , asLoc, questionLoc, type)
4725
- {}
4710
+ TypeExpr *type)
4711
+ : CheckedCastExpr(ExprKind::ConditionalCheckedCast, sub, asLoc, type),
4712
+ QuestionLoc (questionLoc) {}
4713
+
4714
+ public:
4715
+ static ConditionalCheckedCastExpr *create (ASTContext &ctx, SourceLoc asLoc,
4716
+ SourceLoc questionLoc,
4717
+ TypeRepr *tyRepr);
4718
+
4719
+ static ConditionalCheckedCastExpr *createImplicit (ASTContext &ctx, Expr *sub,
4720
+ Type castTy);
4721
+
4722
+ static ConditionalCheckedCastExpr *
4723
+ createImplicit (ASTContext &ctx, Expr *sub, TypeRepr *tyRepr, Type castTy);
4726
4724
4727
4725
// / Retrieve the location of the '?' that follows 'as'.
4728
4726
SourceLoc getQuestionLoc () const { return QuestionLoc; }
@@ -4737,16 +4735,13 @@ class ConditionalCheckedCastExpr : public CheckedCastExpr {
4737
4735
// / of the type and 'a as T' would succeed, false otherwise.
4738
4736
// /
4739
4737
// / FIXME: We should support type queries with a runtime metatype value too.
4740
- class IsExpr : public CheckedCastExpr {
4738
+ class IsExpr final : public CheckedCastExpr {
4739
+ IsExpr (Expr *sub, SourceLoc isLoc, TypeExpr *type)
4740
+ : CheckedCastExpr(ExprKind::Is, sub, isLoc, type) {}
4741
+
4741
4742
public:
4742
- IsExpr (Expr *sub, SourceLoc isLoc, TypeLoc type)
4743
- : CheckedCastExpr(ExprKind::Is, sub, isLoc, type)
4744
- {}
4745
-
4746
- IsExpr (SourceLoc isLoc, TypeLoc type)
4747
- : IsExpr(nullptr , isLoc, type)
4748
- {}
4749
-
4743
+ static IsExpr *create (ASTContext &ctx, SourceLoc isLoc, TypeRepr *tyRepr);
4744
+
4750
4745
static bool classof (const Expr *E) {
4751
4746
return E->getKind () == ExprKind::Is;
4752
4747
}
@@ -4755,35 +4750,29 @@ class IsExpr : public CheckedCastExpr {
4755
4750
// / Represents an explicit coercion from a value to a specific type.
4756
4751
// /
4757
4752
// / Spelled 'a as T' and produces a value of type 'T'.
4758
- class CoerceExpr : public ExplicitCastExpr {
4753
+ class CoerceExpr final : public ExplicitCastExpr {
4759
4754
// / Since there is already `asLoc` location,
4760
4755
// / we use it to store `start` of the initializer
4761
4756
// / call source range to save some storage.
4762
4757
SourceLoc InitRangeEnd;
4763
4758
4764
- public:
4765
- CoerceExpr (Expr *sub, SourceLoc asLoc, TypeLoc type)
4766
- : ExplicitCastExpr(ExprKind::Coerce, sub, asLoc, type)
4767
- { }
4759
+ CoerceExpr (Expr *sub, SourceLoc asLoc, TypeExpr *type)
4760
+ : ExplicitCastExpr(ExprKind::Coerce, sub, asLoc, type) {}
4768
4761
4769
- CoerceExpr (SourceLoc asLoc, TypeLoc type)
4770
- : CoerceExpr(nullptr , asLoc, type)
4771
- { }
4772
-
4773
- private:
4774
- CoerceExpr (SourceRange initRange, Expr *literal, TypeLoc type)
4775
- : ExplicitCastExpr(ExprKind::Coerce, literal, initRange.Start,
4776
- type), InitRangeEnd(initRange.End)
4777
- { setImplicit (); }
4762
+ CoerceExpr (SourceRange initRange, Expr *literal, TypeExpr *type)
4763
+ : ExplicitCastExpr(ExprKind::Coerce, literal, initRange.Start, type),
4764
+ InitRangeEnd (initRange.End) {}
4778
4765
4779
4766
public:
4767
+ static CoerceExpr *create (ASTContext &ctx, SourceLoc asLoc, TypeRepr *tyRepr);
4768
+
4769
+ static CoerceExpr *createImplicit (ASTContext &ctx, Expr *sub, Type castTy);
4770
+
4780
4771
// / Create an implicit coercion expression for literal initialization
4781
4772
// / preserving original source information, this way original call
4782
4773
// / could be recreated if needed.
4783
4774
static CoerceExpr *forLiteralInit (ASTContext &ctx, Expr *literal,
4784
- SourceRange range, TypeLoc literalType) {
4785
- return new (ctx) CoerceExpr (range, literal, literalType);
4786
- }
4775
+ SourceRange range, TypeRepr *literalTyRepr);
4787
4776
4788
4777
bool isLiteralInit () const { return InitRangeEnd.isValid (); }
4789
4778
0 commit comments