Skip to content

Commit a6fc9b3

Browse files
authored
Merge pull request #31253 from CodaFi/casting-call
Strip TypeExpr of its TypeLoc
2 parents eb5b57e + d5edee8 commit a6fc9b3

15 files changed

+201
-155
lines changed

include/swift/AST/Expr.h

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,7 @@ class alignas(8) Expr {
497497
bool isImplicit() const {
498498
return Bits.Expr.Implicit;
499499
}
500-
void setImplicit(bool Implicit = true) {
501-
Bits.Expr.Implicit = Implicit;
502-
}
500+
void setImplicit(bool Implicit = true);
503501

504502
/// Retrieves the declaration that is being referenced by this
505503
/// expression, if any.
@@ -1333,40 +1331,63 @@ class SuperRefExpr : public Expr {
13331331
}
13341332
};
13351333

1336-
/// A reference to a type in expression context, spelled out as a TypeLoc.
1334+
/// A reference to a type in expression context.
13371335
///
1338-
/// The type of this expression is always \c MetaTypeType.
1336+
/// The type of this expression is always \c MetatypeType.
13391337
class TypeExpr : public Expr {
1340-
TypeLoc Info;
1341-
TypeExpr(Type Ty);
1338+
TypeRepr *Repr;
13421339
public:
1343-
// Create a TypeExpr with location information.
1344-
TypeExpr(TypeLoc Ty);
1345-
1346-
// The type of a TypeExpr is always a metatype type. Return the instance
1347-
// type, ErrorType if an error, or null if not set yet.
1348-
Type getInstanceType(llvm::function_ref<bool(const Expr *)> hasType =
1349-
[](const Expr *E) -> bool { return !!E->getType(); },
1350-
llvm::function_ref<Type(const Expr *)> getType =
1351-
[](const Expr *E) -> Type {
1352-
return E->getType();
1353-
}) const;
1340+
/// Create a \c TypeExpr from a parsed \c TypeRepr.
1341+
TypeExpr(TypeRepr *Ty);
13541342

1355-
// Create an implicit TypeExpr, which has no location information.
1356-
static TypeExpr *createImplicit(Type Ty, ASTContext &C) {
1357-
return new (C) TypeExpr(Ty);
1358-
}
1343+
/// Retrieves the corresponding instance type of the type referenced by this
1344+
/// expression.
1345+
///
1346+
/// If this node has no type, the resulting instance type is also the
1347+
/// null \c Type(). If the type of this node is not a \c MetatypeType, the
1348+
/// resulting instance type is \c ErrorType.
1349+
Type getInstanceType() const;
13591350

1360-
// Create an implicit TypeExpr, with location information even though it
1361-
// shouldn't have one. This is presently used to work around other location
1362-
// processing bugs. If you have an implicit location, use createImplicit.
1351+
public:
1352+
/// Create an implicit \c TypeExpr.
1353+
///
1354+
/// The given type is required to be non-null and must be not be
1355+
/// a \c MetatypeType as this function will wrap the given type in one.
1356+
///
1357+
/// FIXME: This behavior is bizarre.
1358+
static TypeExpr *createImplicit(Type Ty, ASTContext &C);
1359+
1360+
/// Create an implicit \c TypeExpr that has artificial
1361+
/// location information attached.
1362+
///
1363+
/// The given type is required to be non-null and must be not be
1364+
/// a \c MetatypeType as this function will wrap the given type in one.
1365+
///
1366+
/// FIXME: This behavior is bizarre.
1367+
///
1368+
/// Due to limitations in the modeling of certain AST elements, implicit
1369+
/// \c TypeExpr nodes are often the only source of location information the
1370+
/// expression checker has when it comes time to diagnose an error.
13631371
static TypeExpr *createImplicitHack(SourceLoc Loc, Type Ty, ASTContext &C);
13641372

1365-
1366-
/// Create a TypeExpr for a TypeDecl at the specified location.
1367-
static TypeExpr *createForDecl(DeclNameLoc Loc, TypeDecl *D,
1368-
DeclContext *DC,
1369-
bool isImplicit);
1373+
/// Create an implicit \c TypeExpr for a given \c TypeDecl at the specified location.
1374+
///
1375+
/// The given type is required to be non-null and must be not be
1376+
/// a \c MetatypeType as this function will wrap the given type in one.
1377+
///
1378+
/// FIXME: This behavior is bizarre.
1379+
///
1380+
/// Unlike the non-implicit case, the given location is not required to be
1381+
/// valid.
1382+
static TypeExpr *createImplicitForDecl(DeclNameLoc Loc, TypeDecl *D,
1383+
DeclContext *DC, Type ty);
1384+
1385+
public:
1386+
/// Create a \c TypeExpr for a given \c TypeDecl at the specified location.
1387+
///
1388+
/// The given location must be valid. If it is not, you must use
1389+
/// \c TypeExpr::createImplicitForDecl instead.
1390+
static TypeExpr *createForDecl(DeclNameLoc Loc, TypeDecl *D, DeclContext *DC);
13701391

13711392
/// Create a TypeExpr for a member TypeDecl of the given parent TypeDecl.
13721393
static TypeExpr *createForMemberDecl(DeclNameLoc ParentNameLoc,
@@ -1389,13 +1410,11 @@ class TypeExpr : public Expr {
13891410
SourceRange AngleLocs,
13901411
ASTContext &C);
13911412

1392-
TypeLoc &getTypeLoc() { return Info; }
1393-
TypeLoc getTypeLoc() const { return Info; }
1394-
TypeRepr *getTypeRepr() const { return Info.getTypeRepr(); }
1413+
TypeRepr *getTypeRepr() const { return Repr; }
13951414
// NOTE: TypeExpr::getType() returns the type of the expr node, which is the
13961415
// metatype of what is stored as an operand type.
13971416

1398-
SourceRange getSourceRange() const { return Info.getSourceRange(); }
1417+
SourceRange getSourceRange() const;
13991418
// TODO: optimize getStartLoc() and getEndLoc() when TypeLoc allows it.
14001419

14011420
static bool classof(const Expr *E) {
@@ -4490,8 +4509,7 @@ class ExplicitCastExpr : public Expr {
44904509
TypeLoc CastTy;
44914510

44924511
protected:
4493-
ExplicitCastExpr(ExprKind kind, Expr *sub, SourceLoc AsLoc, TypeLoc castTy,
4494-
Type resultTy)
4512+
ExplicitCastExpr(ExprKind kind, Expr *sub, SourceLoc AsLoc, TypeLoc castTy)
44954513
: Expr(kind, /*Implicit=*/false), SubExpr(sub), AsLoc(AsLoc), CastTy(castTy)
44964514
{}
44974515

@@ -4547,8 +4565,8 @@ StringRef getCheckedCastKindName(CheckedCastKind kind);
45474565
class CheckedCastExpr : public ExplicitCastExpr {
45484566
public:
45494567
CheckedCastExpr(ExprKind kind,
4550-
Expr *sub, SourceLoc asLoc, TypeLoc castTy, Type resultTy)
4551-
: ExplicitCastExpr(kind, sub, asLoc, castTy, resultTy)
4568+
Expr *sub, SourceLoc asLoc, TypeLoc castTy)
4569+
: ExplicitCastExpr(kind, sub, asLoc, castTy)
45524570
{
45534571
Bits.CheckedCastExpr.CastKind = unsigned(CheckedCastKind::Unresolved);
45544572
}
@@ -4583,7 +4601,7 @@ class ForcedCheckedCastExpr : public CheckedCastExpr {
45834601
ForcedCheckedCastExpr(Expr *sub, SourceLoc asLoc, SourceLoc exclaimLoc,
45844602
TypeLoc type)
45854603
: CheckedCastExpr(ExprKind::ForcedCheckedCast,
4586-
sub, asLoc, type, type.getType()),
4604+
sub, asLoc, type),
45874605
ExclaimLoc(exclaimLoc)
45884606
{
45894607
}
@@ -4612,7 +4630,7 @@ class ConditionalCheckedCastExpr : public CheckedCastExpr {
46124630
ConditionalCheckedCastExpr(Expr *sub, SourceLoc asLoc, SourceLoc questionLoc,
46134631
TypeLoc type)
46144632
: CheckedCastExpr(ExprKind::ConditionalCheckedCast,
4615-
sub, asLoc, type, type.getType()),
4633+
sub, asLoc, type),
46164634
QuestionLoc(questionLoc)
46174635
{ }
46184636

@@ -4637,8 +4655,7 @@ class ConditionalCheckedCastExpr : public CheckedCastExpr {
46374655
class IsExpr : public CheckedCastExpr {
46384656
public:
46394657
IsExpr(Expr *sub, SourceLoc isLoc, TypeLoc type)
4640-
: CheckedCastExpr(ExprKind::Is,
4641-
sub, isLoc, type, Type())
4658+
: CheckedCastExpr(ExprKind::Is, sub, isLoc, type)
46424659
{}
46434660

46444661
IsExpr(SourceLoc isLoc, TypeLoc type)
@@ -4661,7 +4678,7 @@ class CoerceExpr : public ExplicitCastExpr {
46614678

46624679
public:
46634680
CoerceExpr(Expr *sub, SourceLoc asLoc, TypeLoc type)
4664-
: ExplicitCastExpr(ExprKind::Coerce, sub, asLoc, type, type.getType())
4681+
: ExplicitCastExpr(ExprKind::Coerce, sub, asLoc, type)
46654682
{ }
46664683

46674684
CoerceExpr(SourceLoc asLoc, TypeLoc type)
@@ -4671,7 +4688,7 @@ class CoerceExpr : public ExplicitCastExpr {
46714688
private:
46724689
CoerceExpr(SourceRange initRange, Expr *literal, TypeLoc type)
46734690
: ExplicitCastExpr(ExprKind::Coerce, literal, initRange.Start,
4674-
type, type.getType()), InitRangeEnd(initRange.End)
4691+
type), InitRangeEnd(initRange.End)
46754692
{ setImplicit(); }
46764693

46774694
public:

lib/AST/ASTWalker.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,9 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
481481
Expr *visitDiscardAssignmentExpr(DiscardAssignmentExpr *E) { return E; }
482482
Expr *visitTypeExpr(TypeExpr *E) {
483483
if (!E->isImplicit())
484-
if (doIt(E->getTypeLoc()))
485-
return nullptr;
484+
if (auto *typerepr = E->getTypeRepr())
485+
if (doIt(typerepr))
486+
return nullptr;
486487

487488
return E;
488489
}

lib/AST/Expr.cpp

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ void Expr::setType(Type T) {
130130
Ty = T;
131131
}
132132

133+
void Expr::setImplicit(bool Implicit) {
134+
assert(!isa<TypeExpr>(this) || getType() &&
135+
"Cannot make a TypeExpr implicit without a contextual type.");
136+
Bits.Expr.Implicit = Implicit;
137+
}
138+
133139
template <class T> static SourceRange getSourceRangeImpl(const T *E) {
134140
static_assert(isOverriddenFromExpr(&T::getSourceRange) ||
135141
(isOverriddenFromExpr(&T::getStartLoc) &&
@@ -936,8 +942,9 @@ static void
936942
computeSingleArgumentType(ASTContext &ctx, Expr *arg, bool implicit,
937943
llvm::function_ref<Type(const Expr *)> getType) {
938944
// Propagate 'implicit' to the argument.
939-
if (implicit)
945+
if (implicit) {
940946
arg->setImplicit(true);
947+
}
941948

942949
// Handle parenthesized expressions.
943950
if (auto paren = dyn_cast<ParenExpr>(arg)) {
@@ -1920,44 +1927,48 @@ Expr *AutoClosureExpr::getUnwrappedCurryThunkExpr() const {
19201927

19211928
FORWARD_SOURCE_LOCS_TO(UnresolvedPatternExpr, subPattern)
19221929

1923-
TypeExpr::TypeExpr(TypeLoc TyLoc)
1924-
: Expr(ExprKind::Type, /*implicit*/false), Info(TyLoc) {
1925-
Type Ty = TyLoc.getType();
1926-
if (Ty && Ty->hasCanonicalTypeComputed())
1927-
setType(MetatypeType::get(Ty, Ty->getASTContext()));
1928-
}
1930+
TypeExpr::TypeExpr(TypeRepr *Repr)
1931+
: Expr(ExprKind::Type, /*implicit*/false), Repr(Repr) {}
19291932

1930-
TypeExpr::TypeExpr(Type Ty)
1931-
: Expr(ExprKind::Type, /*implicit*/true), Info(TypeLoc::withoutLoc(Ty)) {
1932-
if (Ty->hasCanonicalTypeComputed())
1933-
setType(MetatypeType::get(Ty, Ty->getASTContext()));
1933+
TypeExpr *TypeExpr::createImplicit(Type Ty, ASTContext &C) {
1934+
assert(Ty);
1935+
auto *result = new (C) TypeExpr(nullptr);
1936+
result->setType(MetatypeType::get(Ty, Ty->getASTContext()));
1937+
result->setImplicit();
1938+
return result;
19341939
}
19351940

19361941
// The type of a TypeExpr is always a metatype type. Return the instance
19371942
// type or null if not set yet.
1938-
Type TypeExpr::getInstanceType(
1939-
llvm::function_ref<bool(const Expr *)> hasType,
1940-
llvm::function_ref<Type(const Expr *)> getType) const {
1941-
if (!hasType(this))
1943+
Type TypeExpr::getInstanceType() const {
1944+
auto ty = getType();
1945+
if (!ty)
19421946
return Type();
19431947

1944-
if (auto metaType = getType(this)->getAs<MetatypeType>())
1948+
if (auto metaType = ty->getAs<MetatypeType>())
19451949
return metaType->getInstanceType();
19461950

1947-
return ErrorType::get(getType(this)->getASTContext());
1951+
return ErrorType::get(ty->getASTContext());
19481952
}
19491953

1950-
19511954
TypeExpr *TypeExpr::createForDecl(DeclNameLoc Loc, TypeDecl *Decl,
1952-
DeclContext *DC,
1953-
bool isImplicit) {
1955+
DeclContext *DC) {
1956+
ASTContext &C = Decl->getASTContext();
1957+
assert(Loc.isValid());
1958+
auto *Repr = new (C) SimpleIdentTypeRepr(Loc, Decl->createNameRef());
1959+
Repr->setValue(Decl, DC);
1960+
return new (C) TypeExpr(Repr);
1961+
}
1962+
1963+
TypeExpr *TypeExpr::createImplicitForDecl(DeclNameLoc Loc, TypeDecl *Decl,
1964+
DeclContext *DC, Type ty) {
19541965
ASTContext &C = Decl->getASTContext();
1955-
assert(Loc.isValid() || isImplicit);
19561966
auto *Repr = new (C) SimpleIdentTypeRepr(Loc, Decl->createNameRef());
19571967
Repr->setValue(Decl, DC);
1958-
auto result = new (C) TypeExpr(TypeLoc(Repr, Type()));
1959-
if (isImplicit)
1960-
result->setImplicit();
1968+
auto result = new (C) TypeExpr(Repr);
1969+
assert(ty && !ty->hasTypeParameter());
1970+
result->setType(ty);
1971+
result->setImplicit();
19611972
return result;
19621973
}
19631974

@@ -1985,7 +1996,7 @@ TypeExpr *TypeExpr::createForMemberDecl(DeclNameLoc ParentNameLoc,
19851996
Components.push_back(NewComp);
19861997

19871998
auto *NewTypeRepr = IdentTypeRepr::create(C, Components);
1988-
return new (C) TypeExpr(TypeLoc(NewTypeRepr, Type()));
1999+
return new (C) TypeExpr(NewTypeRepr);
19892000
}
19902001

19912002
TypeExpr *TypeExpr::createForMemberDecl(IdentTypeRepr *ParentTR,
@@ -2006,7 +2017,7 @@ TypeExpr *TypeExpr::createForMemberDecl(IdentTypeRepr *ParentTR,
20062017
Components.push_back(NewComp);
20072018

20082019
auto *NewTypeRepr = IdentTypeRepr::create(C, Components);
2009-
return new (C) TypeExpr(TypeLoc(NewTypeRepr, Type()));
2020+
return new (C) TypeExpr(NewTypeRepr);
20102021
}
20112022

20122023
TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
@@ -2055,7 +2066,7 @@ TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
20552066
components.push_back(genericComp);
20562067

20572068
auto *genericRepr = IdentTypeRepr::create(C, components);
2058-
return new (C) TypeExpr(TypeLoc(genericRepr, Type()));
2069+
return new (C) TypeExpr(genericRepr);
20592070
}
20602071

20612072
return nullptr;
@@ -2066,14 +2077,20 @@ TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
20662077
// processing bugs. If you have an implicit location, use createImplicit.
20672078
TypeExpr *TypeExpr::createImplicitHack(SourceLoc Loc, Type Ty, ASTContext &C) {
20682079
// FIXME: This is horrible.
2080+
assert(Ty);
20692081
if (Loc.isInvalid()) return createImplicit(Ty, C);
20702082
auto *Repr = new (C) FixedTypeRepr(Ty, Loc);
2071-
auto *Res = new (C) TypeExpr(TypeLoc(Repr, Ty));
2072-
Res->setImplicit();
2083+
auto *Res = new (C) TypeExpr(Repr);
20732084
Res->setType(MetatypeType::get(Ty, C));
2085+
Res->setImplicit();
20742086
return Res;
20752087
}
20762088

2089+
SourceRange TypeExpr::getSourceRange() const {
2090+
if (!getTypeRepr()) return SourceRange();
2091+
return getTypeRepr()->getSourceRange();
2092+
}
2093+
20772094
bool Expr::isSelfExprOf(const AbstractFunctionDecl *AFD, bool sameBase) const {
20782095
auto *E = getSemanticsProvidingExpr();
20792096

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ static void collectPossibleCalleesByQualifiedLookup(
375375
// Re-typecheck TypeExpr so it's typechecked without the arguments which may
376376
// affects the inference of the generic arguments.
377377
if (TypeExpr *tyExpr = dyn_cast<TypeExpr>(baseExpr)) {
378-
tyExpr->setType(nullptr);
379-
tyExpr->getTypeLoc().setType(nullptr);
378+
if (!tyExpr->isImplicit())
379+
tyExpr->setType(nullptr);
380380
}
381381

382382
auto baseTyOpt = getTypeOfCompletionContextExpr(

lib/Parse/ParseExpr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
395395
ParserResult<TypeRepr> ty = parseType();
396396
if (ty.isNonNull())
397397
return makeParserResult(
398-
new (Context) TypeExpr(TypeLoc(ty.get(), Type())));
398+
new (Context) TypeExpr(ty.get()));
399399
checkForInputIncomplete();
400400
return nullptr;
401401
}
@@ -1534,7 +1534,7 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
15341534
case tok::kw_Any: { // Any
15351535
ExprContext.setCreateSyntax(SyntaxKind::TypeExpr);
15361536
auto TyR = parseAnyType();
1537-
return makeParserResult(new (Context) TypeExpr(TypeLoc(TyR.get())));
1537+
return makeParserResult(new (Context) TypeExpr(TyR.get()));
15381538
}
15391539

15401540
case tok::dollarident: // $1
@@ -2306,7 +2306,7 @@ Expr *Parser::parseExprIdentifier() {
23062306
// global or local declarations here.
23072307
assert(!TD->getDeclContext()->isTypeContext() ||
23082308
isa<GenericTypeParamDecl>(TD));
2309-
E = TypeExpr::createForDecl(loc, TD, /*DC*/nullptr, /*implicit*/false);
2309+
E = TypeExpr::createForDecl(loc, TD, /*DC*/ nullptr);
23102310
} else {
23112311
E = new (Context) DeclRefExpr(D, loc, /*Implicit=*/false);
23122312
}

0 commit comments

Comments
 (0)