Skip to content

Commit 426d930

Browse files
authored
Merge pull request #32301 from CodaFi/location-location-location
Strip TypeLoc out of Patterns
2 parents f89e213 + e101576 commit 426d930

19 files changed

+288
-237
lines changed

include/swift/AST/Pattern.h

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "swift/Basic/LLVM.h"
2525
#include "swift/AST/Type.h"
2626
#include "swift/AST/Types.h"
27-
#include "swift/AST/TypeLoc.h"
2827
#include "swift/AST/TypeAlignments.h"
2928
#include "swift/Basic/InlineBitfield.h"
3029
#include "swift/Basic/OptionSet.h"
@@ -35,6 +34,7 @@ namespace swift {
3534
class Expr;
3635
enum class CheckedCastKind : unsigned;
3736
class TypeExpr;
37+
class TypeLoc;
3838

3939
/// PatternKind - The classification of different kinds of
4040
/// value-matching pattern.
@@ -470,19 +470,14 @@ class IsPattern : public Pattern {
470470
CheckedCastKind CastKind;
471471

472472
/// The type being checked for.
473-
TypeLoc CastType;
473+
TypeExpr *CastType;
474474

475475
public:
476-
IsPattern(SourceLoc IsLoc, TypeLoc CastTy,
477-
Pattern *SubPattern,
478-
CheckedCastKind Kind)
479-
: Pattern(PatternKind::Is),
480-
IsLoc(IsLoc),
481-
SubPattern(SubPattern),
482-
CastKind(Kind),
483-
CastType(CastTy) {
484-
assert(IsLoc.isValid() == CastTy.hasLocation());
485-
}
476+
IsPattern(SourceLoc IsLoc, TypeExpr *CastTy, Pattern *SubPattern,
477+
CheckedCastKind Kind);
478+
479+
static IsPattern *createImplicit(ASTContext &Ctx, Type castTy,
480+
Pattern *SubPattern, CheckedCastKind Kind);
486481

487482
CheckedCastKind getCastKind() const { return CastKind; }
488483
void setCastKind(CheckedCastKind kind) { CastKind = kind; }
@@ -493,16 +488,11 @@ class IsPattern : public Pattern {
493488
void setSubPattern(Pattern *p) { SubPattern = p; }
494489

495490
SourceLoc getLoc() const { return IsLoc; }
496-
SourceRange getSourceRange() const {
497-
SourceLoc beginLoc =
498-
SubPattern ? SubPattern->getSourceRange().Start : IsLoc;
499-
SourceLoc endLoc =
500-
(isImplicit() ? beginLoc : CastType.getSourceRange().End);
501-
return { beginLoc, endLoc };
502-
}
491+
SourceRange getSourceRange() const;
503492

504-
TypeLoc &getCastTypeLoc() { return CastType; }
505-
TypeLoc getCastTypeLoc() const { return CastType; }
493+
void setCastType(Type castTy);
494+
Type getCastType() const;
495+
TypeRepr *getCastTypeRepr() const;
506496

507497
static bool classof(const Pattern *P) {
508498
return P->getKind() == PatternKind::Is;
@@ -513,35 +503,31 @@ class IsPattern : public Pattern {
513503
/// case, then the value is extracted. If there is a subpattern, it is then
514504
/// matched against the associated value for the case.
515505
class EnumElementPattern : public Pattern {
516-
TypeLoc ParentType;
506+
TypeExpr *ParentType;
517507
SourceLoc DotLoc;
518508
DeclNameLoc NameLoc;
519509
DeclNameRef Name;
520510
PointerUnion<EnumElementDecl *, Expr*> ElementDeclOrUnresolvedOriginalExpr;
521511
Pattern /*nullable*/ *SubPattern;
522512

523513
public:
524-
EnumElementPattern(TypeLoc ParentType, SourceLoc DotLoc, DeclNameLoc NameLoc,
525-
DeclNameRef Name, EnumElementDecl *Element,
526-
Pattern *SubPattern)
527-
: Pattern(PatternKind::EnumElement),
528-
ParentType(ParentType), DotLoc(DotLoc), NameLoc(NameLoc), Name(Name),
529-
ElementDeclOrUnresolvedOriginalExpr(Element),
530-
SubPattern(SubPattern) { }
514+
EnumElementPattern(TypeExpr *ParentType, SourceLoc DotLoc,
515+
DeclNameLoc NameLoc, DeclNameRef Name,
516+
EnumElementDecl *Element, Pattern *SubPattern)
517+
: Pattern(PatternKind::EnumElement), ParentType(ParentType),
518+
DotLoc(DotLoc), NameLoc(NameLoc), Name(Name),
519+
ElementDeclOrUnresolvedOriginalExpr(Element), SubPattern(SubPattern) {
520+
assert(ParentType && "Missing parent type?");
521+
}
531522

532523
/// Create an unresolved EnumElementPattern for a `.foo` pattern relying on
533524
/// contextual type.
534-
EnumElementPattern(SourceLoc DotLoc,
535-
DeclNameLoc NameLoc,
536-
DeclNameRef Name,
537-
Pattern *SubPattern,
538-
Expr *UnresolvedOriginalExpr)
539-
: Pattern(PatternKind::EnumElement),
540-
ParentType(), DotLoc(DotLoc), NameLoc(NameLoc), Name(Name),
541-
ElementDeclOrUnresolvedOriginalExpr(UnresolvedOriginalExpr),
542-
SubPattern(SubPattern) {
543-
544-
}
525+
EnumElementPattern(SourceLoc DotLoc, DeclNameLoc NameLoc, DeclNameRef Name,
526+
Pattern *SubPattern, Expr *UnresolvedOriginalExpr)
527+
: Pattern(PatternKind::EnumElement), ParentType(nullptr), DotLoc(DotLoc),
528+
NameLoc(NameLoc), Name(Name),
529+
ElementDeclOrUnresolvedOriginalExpr(UnresolvedOriginalExpr),
530+
SubPattern(SubPattern) {}
545531

546532
bool hasSubPattern() const { return SubPattern; }
547533

@@ -553,10 +539,6 @@ class EnumElementPattern : public Pattern {
553539
return SubPattern;
554540
}
555541

556-
bool isParentTypeImplicit() {
557-
return !ParentType.hasLocation();
558-
}
559-
560542
void setSubPattern(Pattern *p) { SubPattern = p; }
561543

562544
DeclNameRef getName() const { return Name; }
@@ -577,21 +559,14 @@ class EnumElementPattern : public Pattern {
577559

578560
DeclNameLoc getNameLoc() const { return NameLoc; }
579561
SourceLoc getLoc() const { return NameLoc.getBaseNameLoc(); }
580-
SourceLoc getStartLoc() const {
581-
return ParentType.hasLocation() ? ParentType.getSourceRange().Start :
582-
DotLoc.isValid() ? DotLoc
583-
: NameLoc.getBaseNameLoc();
584-
}
585-
SourceLoc getEndLoc() const {
586-
if (SubPattern && SubPattern->getSourceRange().isValid()) {
587-
return SubPattern->getSourceRange().End;
588-
}
589-
return NameLoc.getEndLoc();
590-
}
562+
SourceLoc getStartLoc() const;
563+
SourceLoc getEndLoc() const;
591564
SourceRange getSourceRange() const { return {getStartLoc(), getEndLoc()}; }
592565

593-
TypeLoc &getParentType() { return ParentType; }
594-
TypeLoc getParentType() const { return ParentType; }
566+
TypeRepr *getParentTypeRepr() const;
567+
568+
void setParentType(Type ty);
569+
Type getParentType() const;
595570

596571
static bool classof(const Pattern *P) {
597572
return P->getKind() == PatternKind::EnumElement;

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,17 +479,17 @@ namespace {
479479
void visitTypedPattern(TypedPattern *P) {
480480
printCommon(P, "pattern_typed") << '\n';
481481
printRec(P->getSubPattern());
482-
if (P->getTypeLoc().getTypeRepr()) {
482+
if (auto *repr = P->getTypeRepr()) {
483483
OS << '\n';
484-
printRec(P->getTypeLoc().getTypeRepr());
484+
printRec(repr);
485485
}
486486
PrintWithColorRAII(OS, ParenthesisColor) << ')';
487487
}
488488

489489
void visitIsPattern(IsPattern *P) {
490490
printCommon(P, "pattern_is")
491491
<< ' ' << getCheckedCastKindName(P->getCastKind()) << ' ';
492-
P->getCastTypeLoc().getType().print(OS);
492+
P->getCastType().print(OS);
493493
if (auto sub = P->getSubPattern()) {
494494
OS << '\n';
495495
printRec(sub);
@@ -514,8 +514,7 @@ namespace {
514514
void visitEnumElementPattern(EnumElementPattern *P) {
515515
printCommon(P, "pattern_enum_element");
516516
OS << ' ';
517-
P->getParentType().getType().print(
518-
PrintWithColorRAII(OS, TypeColor).getOS());
517+
P->getParentType().print(PrintWithColorRAII(OS, TypeColor).getOS());
519518
PrintWithColorRAII(OS, IdentifierColor) << '.' << P->getName();
520519
if (P->hasSubPattern()) {
521520
OS << '\n';

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ void PrintAST::printPattern(const Pattern *pattern) {
11631163
case PatternKind::Is: {
11641164
auto isa = cast<IsPattern>(pattern);
11651165
Printer << tok::kw_is << " ";
1166-
isa->getCastTypeLoc().getType().print(Printer, Options);
1166+
isa->getCastType().print(Printer, Options);
11671167
break;
11681168
}
11691169

lib/AST/ASTWalker.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,14 +1699,15 @@ Pattern *Traversal::visitIsPattern(IsPattern *P) {
16991699
}
17001700
}
17011701
if (!P->isImplicit())
1702-
if (doIt(P->getCastTypeLoc()))
1703-
return nullptr;
1702+
if (auto *TR = P->getCastTypeRepr())
1703+
if (doIt(TR))
1704+
return nullptr;
17041705
return P;
17051706
}
17061707

17071708
Pattern *Traversal::visitEnumElementPattern(EnumElementPattern *P) {
1708-
if (!P->isParentTypeImplicit())
1709-
if (doIt(P->getParentType()))
1709+
if (auto *TR = P->getParentTypeRepr())
1710+
if (doIt(TR))
17101711
return nullptr;
17111712

17121713
if (!P->hasSubPattern())

lib/AST/Decl.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ bool PatternBindingDecl::isDefaultInitializable(unsigned i) const {
18601860
// If the pattern is typed as optional (or tuples thereof), it is
18611861
// default initializable.
18621862
if (const auto typedPattern = dyn_cast<TypedPattern>(entry.getPattern())) {
1863-
if (const auto typeRepr = typedPattern->getTypeLoc().getTypeRepr()) {
1863+
if (const auto typeRepr = typedPattern->getTypeRepr()) {
18641864
if (::isDefaultInitializable(typeRepr, ctx))
18651865
return true;
18661866
} else if (typedPattern->isImplicit()) {
@@ -1870,11 +1870,15 @@ bool PatternBindingDecl::isDefaultInitializable(unsigned i) const {
18701870
//
18711871
// All lazy storage is implicitly default initializable, though, because
18721872
// lazy backing storage is optional.
1873-
if (const auto *varDecl = typedPattern->getSingleVar())
1873+
if (const auto *varDecl = typedPattern->getSingleVar()) {
18741874
// Lazy storage is never user accessible.
1875-
if (!varDecl->isUserAccessible())
1876-
if (typedPattern->getTypeLoc().getType()->getOptionalObjectType())
1875+
if (!varDecl->isUserAccessible()) {
1876+
if (typedPattern->hasType() &&
1877+
typedPattern->getType()->getOptionalObjectType()) {
18771878
return true;
1879+
}
1880+
}
1881+
}
18781882
}
18791883
}
18801884

@@ -2841,7 +2845,7 @@ OpaqueReturnTypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
28412845
assert(NP->getDecl() == VD);
28422846
(void) NP;
28432847

2844-
returnRepr = TP->getTypeLoc().getTypeRepr();
2848+
returnRepr = TP->getTypeRepr();
28452849
}
28462850
}
28472851
} else {
@@ -5586,7 +5590,7 @@ SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
55865590
if (auto *VP = dyn_cast<VarPattern>(Pat))
55875591
Pat = VP->getSubPattern();
55885592
if (auto *TP = dyn_cast<TypedPattern>(Pat))
5589-
if (auto typeRepr = TP->getTypeLoc().getTypeRepr())
5593+
if (auto typeRepr = TP->getTypeRepr())
55905594
return typeRepr->getSourceRange();
55915595

55925596
return SourceRange();

lib/AST/Pattern.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,37 @@ SourceRange TypedPattern::getSourceRange() const {
441441
PatTypeRepr->getSourceRange().End };
442442
}
443443

444+
IsPattern::IsPattern(SourceLoc IsLoc, TypeExpr *CastTy, Pattern *SubPattern,
445+
CheckedCastKind Kind)
446+
: Pattern(PatternKind::Is), IsLoc(IsLoc), SubPattern(SubPattern),
447+
CastKind(Kind), CastType(CastTy) {
448+
assert(IsLoc.isValid() == CastTy->getLoc().isValid());
449+
}
450+
451+
IsPattern *IsPattern::createImplicit(ASTContext &Ctx, Type castTy,
452+
Pattern *SubPattern,
453+
CheckedCastKind Kind) {
454+
assert(castTy);
455+
auto *CastTE = TypeExpr::createImplicit(castTy, Ctx);
456+
auto *ip = new (Ctx) IsPattern(SourceLoc(), CastTE, SubPattern, Kind);
457+
ip->setImplicit();
458+
return ip;
459+
}
460+
461+
SourceRange IsPattern::getSourceRange() const {
462+
SourceLoc beginLoc = SubPattern ? SubPattern->getSourceRange().Start : IsLoc;
463+
SourceLoc endLoc = (isImplicit() ? beginLoc : CastType->getEndLoc());
464+
return {beginLoc, endLoc};
465+
}
466+
467+
Type IsPattern::getCastType() const { return CastType->getInstanceType(); }
468+
void IsPattern::setCastType(Type type) {
469+
assert(type);
470+
CastType->setType(MetatypeType::get(type));
471+
}
472+
473+
TypeRepr *IsPattern::getCastTypeRepr() const { return CastType->getTypeRepr(); }
474+
444475
/// Construct an ExprPattern.
445476
ExprPattern::ExprPattern(Expr *e, bool isResolved, Expr *matchExpr,
446477
VarDecl *matchVar)
@@ -449,6 +480,40 @@ ExprPattern::ExprPattern(Expr *e, bool isResolved, Expr *matchExpr,
449480
assert(!matchExpr || e->isImplicit() == matchExpr->isImplicit());
450481
}
451482

483+
SourceLoc EnumElementPattern::getStartLoc() const {
484+
return (ParentType && !ParentType->isImplicit())
485+
? ParentType->getSourceRange().Start
486+
: DotLoc.isValid() ? DotLoc : NameLoc.getBaseNameLoc();
487+
}
488+
489+
SourceLoc EnumElementPattern::getEndLoc() const {
490+
if (SubPattern && SubPattern->getSourceRange().isValid()) {
491+
return SubPattern->getSourceRange().End;
492+
}
493+
return NameLoc.getEndLoc();
494+
}
495+
496+
TypeRepr *EnumElementPattern::getParentTypeRepr() const {
497+
if (!ParentType)
498+
return nullptr;
499+
return ParentType->getTypeRepr();
500+
}
501+
502+
Type EnumElementPattern::getParentType() const {
503+
if (!ParentType)
504+
return Type();
505+
return ParentType->getInstanceType();
506+
}
507+
508+
void EnumElementPattern::setParentType(Type type) {
509+
assert(type);
510+
if (ParentType) {
511+
ParentType->setType(MetatypeType::get(type));
512+
} else {
513+
ParentType = TypeExpr::createImplicit(type, type->getASTContext());
514+
}
515+
}
516+
452517
SourceLoc ExprPattern::getLoc() const {
453518
return getSubExpr()->getLoc();
454519
}

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5694,12 +5694,7 @@ Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
56945694
if (!PrimaryVar)
56955695
return nullptr;
56965696

5697-
TypeLoc TyLoc;
5698-
if (auto *TP = dyn_cast<TypedPattern>(pattern)) {
5699-
TyLoc = TP->getTypeLoc();
5700-
}
5701-
5702-
if (!TyLoc.hasLocation()) {
5697+
if (!isa<TypedPattern>(pattern)) {
57035698
if (accessors.Get || accessors.Set || accessors.Address ||
57045699
accessors.MutableAddress) {
57055700
SourceLoc locAfterPattern = pattern->getLoc().getAdvancedLoc(

lib/Parse/ParsePattern.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,9 +1184,9 @@ ParserResult<Pattern> Parser::parseMatchingPattern(bool isExprBasic) {
11841184
ParserResult<TypeRepr> castType = parseType();
11851185
if (castType.isNull() || castType.hasCodeCompletion())
11861186
return nullptr;
1187-
return makeParserResult(new (Context) IsPattern(isLoc, castType.get(),
1188-
nullptr,
1189-
CheckedCastKind::Unresolved));
1187+
auto *CastTE = new (Context) TypeExpr(castType.get());
1188+
return makeParserResult(new (Context) IsPattern(
1189+
isLoc, CastTE, nullptr, CheckedCastKind::Unresolved));
11901190
}
11911191

11921192
// matching-pattern ::= expr

lib/SILGen/SILGenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ copyOrInitValueInto(SILGenFunction &SGF, SILLocation loc,
965965

966966
// Try to perform the cast to the destination type, producing an optional that
967967
// indicates whether we succeeded.
968-
auto destType = OptionalType::get(pattern->getCastTypeLoc().getType());
968+
auto destType = OptionalType::get(pattern->getCastType());
969969

970970
value =
971971
emitConditionalCheckedCast(SGF, loc, value, pattern->getType(), destType,

0 commit comments

Comments
 (0)