Skip to content

Commit 4b46341

Browse files
committed
Sema: Remove TypeLoc from ExplicitCast
1 parent ad0a8d3 commit 4b46341

26 files changed

+322
-266
lines changed

include/swift/AST/ASTNode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace swift {
3030
class Stmt;
3131
class Decl;
3232
class Pattern;
33-
class TypeLoc;
33+
class TypeRepr;
3434
class DeclContext;
3535
class SourceLoc;
3636
class SourceRange;
@@ -41,7 +41,7 @@ namespace swift {
4141
enum class StmtKind;
4242

4343
struct ASTNode : public llvm::PointerUnion<Expr *, Stmt *, Decl *, Pattern *,
44-
TypeLoc *> {
44+
TypeRepr *> {
4545
// Inherit the constructors from PointerUnion.
4646
using PointerUnion::PointerUnion;
4747

include/swift/AST/Expr.h

Lines changed: 57 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "swift/AST/ProtocolConformanceRef.h"
2626
#include "swift/AST/TrailingCallArguments.h"
2727
#include "swift/AST/TypeAlignments.h"
28-
#include "swift/AST/TypeLoc.h"
2928
#include "swift/AST/Availability.h"
3029
#include "swift/Basic/Debug.h"
3130
#include "swift/Basic/InlineBitfield.h"
@@ -555,7 +554,7 @@ class alignas(8) Expr {
555554
SWIFT_DEBUG_DUMP;
556555
void dump(raw_ostream &OS, unsigned Indent = 0) const;
557556
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,
559558
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
560559
getTypeOfKeyPathComponent,
561560
unsigned Indent = 0) const;
@@ -4591,23 +4590,22 @@ class DotSyntaxBaseIgnoredExpr : public Expr {
45914590
class ExplicitCastExpr : public Expr {
45924591
Expr *SubExpr;
45934592
SourceLoc AsLoc;
4594-
TypeLoc CastTy;
4593+
TypeExpr *const CastTy;
45954594

45964595
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) {}
46004599

46014600
public:
46024601
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; }
46074602

46084603
/// Get the type syntactically spelled in the cast. For some forms of checked
46094604
/// 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(); }
46114609

46124610
void setSubExpr(Expr *E) { SubExpr = E; }
46134611

@@ -4623,7 +4621,7 @@ class ExplicitCastExpr : public Expr {
46234621
}
46244622

46254623
SourceRange getSourceRange() const {
4626-
SourceRange castTyRange = CastTy.getSourceRange();
4624+
const SourceRange castTyRange = CastTy->getSourceRange();
46274625
if (castTyRange.isInvalid())
46284626
return SubExpr->getSourceRange();
46294627

@@ -4648,14 +4646,13 @@ StringRef getCheckedCastKindName(CheckedCastKind kind);
46484646
/// Abstract base class for checked casts 'as' and 'is'. These represent
46494647
/// casts that can dynamically fail.
46504648
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) {
46564652
Bits.CheckedCastExpr.CastKind = unsigned(CheckedCastKind::Unresolved);
46574653
}
4658-
4654+
4655+
public:
46594656
/// Return the semantic kind of cast performed.
46604657
CheckedCastKind getCastKind() const {
46614658
return CheckedCastKind(Bits.CheckedCastExpr.CastKind);
@@ -4679,22 +4676,20 @@ class CheckedCastExpr : public ExplicitCastExpr {
46794676
/// from a value of some type to some specified subtype and fails dynamically
46804677
/// if the value does not have that type.
46814678
/// Spelled 'a as! T' and produces a value of type 'T'.
4682-
class ForcedCheckedCastExpr : public CheckedCastExpr {
4679+
class ForcedCheckedCastExpr final : public CheckedCastExpr {
46834680
SourceLoc ExclaimLoc;
46844681

4685-
public:
46864682
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) {}
46934686

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);
46984693

46994694
/// Retrieve the location of the '!' that follows 'as'.
47004695
SourceLoc getExclaimLoc() const { return ExclaimLoc; }
@@ -4708,21 +4703,24 @@ class ForcedCheckedCastExpr : public CheckedCastExpr {
47084703
/// from a type to some subtype and produces an Optional value, which will be
47094704
/// .Some(x) if the cast succeeds, or .None if the cast fails.
47104705
/// Spelled 'a as? T' and produces a value of type 'T?'.
4711-
class ConditionalCheckedCastExpr : public CheckedCastExpr {
4706+
class ConditionalCheckedCastExpr final : public CheckedCastExpr {
47124707
SourceLoc QuestionLoc;
47134708

4714-
public:
47154709
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);
47264724

47274725
/// Retrieve the location of the '?' that follows 'as'.
47284726
SourceLoc getQuestionLoc() const { return QuestionLoc; }
@@ -4737,16 +4735,13 @@ class ConditionalCheckedCastExpr : public CheckedCastExpr {
47374735
/// of the type and 'a as T' would succeed, false otherwise.
47384736
///
47394737
/// 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+
47414742
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+
47504745
static bool classof(const Expr *E) {
47514746
return E->getKind() == ExprKind::Is;
47524747
}
@@ -4755,35 +4750,29 @@ class IsExpr : public CheckedCastExpr {
47554750
/// Represents an explicit coercion from a value to a specific type.
47564751
///
47574752
/// Spelled 'a as T' and produces a value of type 'T'.
4758-
class CoerceExpr : public ExplicitCastExpr {
4753+
class CoerceExpr final : public ExplicitCastExpr {
47594754
/// Since there is already `asLoc` location,
47604755
/// we use it to store `start` of the initializer
47614756
/// call source range to save some storage.
47624757
SourceLoc InitRangeEnd;
47634758

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) {}
47684761

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) {}
47784765

47794766
public:
4767+
static CoerceExpr *create(ASTContext &ctx, SourceLoc asLoc, TypeRepr *tyRepr);
4768+
4769+
static CoerceExpr *createImplicit(ASTContext &ctx, Expr *sub, Type castTy);
4770+
47804771
/// Create an implicit coercion expression for literal initialization
47814772
/// preserving original source information, this way original call
47824773
/// could be recreated if needed.
47834774
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);
47874776

47884777
bool isLiteralInit() const { return InitRangeEnd.isValid(); }
47894778

include/swift/AST/TypeAlignments.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace swift {
5151
class TypeVariableType;
5252
class TypeBase;
5353
class TypeDecl;
54-
class TypeLoc;
54+
class TypeRepr;
5555
class ValueDecl;
5656

5757
/// We frequently use three tag bits on all of these types.
@@ -64,7 +64,7 @@ namespace swift {
6464
constexpr size_t PatternAlignInBits = 3;
6565
constexpr size_t SILFunctionAlignInBits = 2;
6666
constexpr size_t TypeVariableAlignInBits = 4;
67-
constexpr size_t TypeLocAlignInBits = 3;
67+
constexpr size_t TypeReprAlignInBits = 3;
6868
}
6969

7070
namespace llvm {
@@ -126,7 +126,7 @@ LLVM_DECLARE_TYPE_ALIGNMENT(swift::SILFunction,
126126

127127
LLVM_DECLARE_TYPE_ALIGNMENT(swift::AttributeBase, swift::AttrAlignInBits)
128128

129-
LLVM_DECLARE_TYPE_ALIGNMENT(swift::TypeLoc, swift::TypeLocAlignInBits)
129+
LLVM_DECLARE_TYPE_ALIGNMENT(swift::TypeRepr, swift::TypeReprAlignInBits)
130130

131131
static_assert(alignof(void*) >= 2, "pointer alignment is too small");
132132

include/swift/AST/TypeLoc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TypeRepr;
2929

3030
/// TypeLoc - Provides source location information for a parsed type.
3131
/// A TypeLoc is stored in AST nodes which use an explicitly written type.
32-
class alignas(1 << TypeLocAlignInBits) TypeLoc {
32+
class alignas(1 << TypeReprAlignInBits) TypeLoc final {
3333
Type Ty;
3434
TypeRepr *TyR = nullptr;
3535

include/swift/AST/TypeRepr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ enum : unsigned { NumTypeReprKindBits =
4848
countBitsUsed(static_cast<unsigned>(TypeReprKind::Last_TypeRepr)) };
4949

5050
/// Representation of a type as written in source.
51-
class alignas(8) TypeRepr {
51+
class alignas(1 << TypeReprAlignInBits) TypeRepr {
5252
TypeRepr(const TypeRepr&) = delete;
5353
void operator=(const TypeRepr&) = delete;
5454

lib/AST/ASTDumper.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,18 +1775,18 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
17751775
public:
17761776
raw_ostream &OS;
17771777
llvm::function_ref<Type(Expr *)> GetTypeOfExpr;
1778-
llvm::function_ref<Type(TypeLoc &)> GetTypeOfTypeLoc;
1778+
llvm::function_ref<Type(TypeRepr *)> GetTypeOfTypeRepr;
17791779
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
17801780
GetTypeOfKeyPathComponent;
17811781
unsigned Indent;
17821782

17831783
PrintExpr(raw_ostream &os, llvm::function_ref<Type(Expr *)> getTypeOfExpr,
1784-
llvm::function_ref<Type(TypeLoc &)> getTypeOfTypeLoc,
1784+
llvm::function_ref<Type(TypeRepr *)> getTypeOfTypeRepr,
17851785
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
17861786
getTypeOfKeyPathComponent,
17871787
unsigned indent)
17881788
: OS(os), GetTypeOfExpr(getTypeOfExpr),
1789-
GetTypeOfTypeLoc(getTypeOfTypeLoc),
1789+
GetTypeOfTypeRepr(getTypeOfTypeRepr),
17901790
GetTypeOfKeyPathComponent(getTypeOfKeyPathComponent), Indent(indent) {}
17911791

17921792
void printRec(Expr *E) {
@@ -2605,7 +2605,10 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
26052605
if (auto checkedCast = dyn_cast<CheckedCastExpr>(E))
26062606
OS << getCheckedCastKindName(checkedCast->getCastKind()) << ' ';
26072607
OS << "writtenType='";
2608-
GetTypeOfTypeLoc(E->getCastTypeLoc()).print(OS);
2608+
if (GetTypeOfTypeRepr)
2609+
GetTypeOfTypeRepr(E->getCastTypeRepr()).print(OS);
2610+
else
2611+
E->getCastType().print(OS);
26092612
OS << "'\n";
26102613
printRec(E->getSubExpr());
26112614
PrintWithColorRAII(OS, ParenthesisColor) << ')';
@@ -2870,21 +2873,22 @@ void Expr::dump() const {
28702873
}
28712874

28722875
void Expr::dump(raw_ostream &OS, llvm::function_ref<Type(Expr *)> getTypeOfExpr,
2873-
llvm::function_ref<Type(TypeLoc &)> getTypeOfTypeLoc,
2876+
llvm::function_ref<Type(TypeRepr *)> getTypeOfTypeRepr,
28742877
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
28752878
getTypeOfKeyPathComponent,
28762879
unsigned Indent) const {
2877-
PrintExpr(OS, getTypeOfExpr, getTypeOfTypeLoc, getTypeOfKeyPathComponent, Indent)
2880+
PrintExpr(OS, getTypeOfExpr, getTypeOfTypeRepr, getTypeOfKeyPathComponent,
2881+
Indent)
28782882
.visit(const_cast<Expr *>(this));
28792883
}
28802884

28812885
void Expr::dump(raw_ostream &OS, unsigned Indent) const {
28822886
auto getTypeOfExpr = [](Expr *E) -> Type { return E->getType(); };
2883-
auto getTypeOfTypeLoc = [](TypeLoc &TL) -> Type { return TL.getType(); };
28842887
auto getTypeOfKeyPathComponent = [](KeyPathExpr *E, unsigned index) -> Type {
28852888
return E->getComponents()[index].getComponentType();
28862889
};
2887-
dump(OS, getTypeOfExpr, getTypeOfTypeLoc, getTypeOfKeyPathComponent, Indent);
2890+
dump(OS, getTypeOfExpr, /*getTypeOfTypeRepr*/ nullptr,
2891+
getTypeOfKeyPathComponent, Indent);
28882892
}
28892893

28902894
void Expr::print(ASTPrinter &Printer, const PrintOptions &Opts) const {

lib/AST/ASTNode.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "swift/AST/Expr.h"
2020
#include "swift/AST/Stmt.h"
2121
#include "swift/AST/Pattern.h"
22-
#include "swift/AST/TypeLoc.h"
22+
#include "swift/AST/TypeRepr.h"
2323
#include "swift/Basic/SourceLoc.h"
2424

2525
using namespace swift;
@@ -33,8 +33,8 @@ SourceRange ASTNode::getSourceRange() const {
3333
return D->getSourceRange();
3434
if (const auto *P = this->dyn_cast<Pattern*>())
3535
return P->getSourceRange();
36-
if (const auto *L = this->dyn_cast<TypeLoc *>())
37-
return L->getSourceRange();
36+
if (const auto *T = this->dyn_cast<TypeRepr *>())
37+
return T->getSourceRange();
3838
llvm_unreachable("unsupported AST node");
3939
}
4040

@@ -71,7 +71,7 @@ bool ASTNode::isImplicit() const {
7171
return D->isImplicit();
7272
if (const auto *P = this->dyn_cast<Pattern*>())
7373
return P->isImplicit();
74-
if (const auto *L = this->dyn_cast<TypeLoc*>())
74+
if (const auto *T = this->dyn_cast<TypeRepr*>())
7575
return false;
7676
llvm_unreachable("unsupported AST node");
7777
}
@@ -85,6 +85,8 @@ void ASTNode::walk(ASTWalker &Walker) {
8585
D->walk(Walker);
8686
else if (auto *P = this->dyn_cast<Pattern*>())
8787
P->walk(Walker);
88+
else if (auto *T = this->dyn_cast<TypeRepr*>())
89+
T->walk(Walker);
8890
else
8991
llvm_unreachable("unsupported AST node");
9092
}
@@ -98,8 +100,10 @@ void ASTNode::dump(raw_ostream &OS, unsigned Indent) const {
98100
D->dump(OS, Indent);
99101
else if (auto P = dyn_cast<Pattern*>())
100102
P->dump(OS, Indent);
103+
else if (auto T = dyn_cast<TypeRepr*>())
104+
T->print(OS);
101105
else
102-
OS << "<null>";
106+
llvm_unreachable("unsupported AST node");
103107
}
104108

105109
void ASTNode::dump() const {

0 commit comments

Comments
 (0)