Skip to content

Commit 492156c

Browse files
committed
Remove SubscriptDecl::getBodyResultTypeLoc
1 parent 21faa48 commit 492156c

16 files changed

+114
-39
lines changed

include/swift/AST/Decl.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5598,13 +5598,16 @@ enum class ObjCSubscriptKind {
55985598
/// signatures (indices and element type) are distinct.
55995599
///
56005600
class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
5601+
friend class ResultTypeRequest;
5602+
56015603
SourceLoc StaticLoc;
56025604
SourceLoc ArrowLoc;
56035605
SourceLoc EndLoc;
56045606
ParameterList *Indices;
56055607
TypeLoc ElementTy;
56065608

5607-
public:
5609+
void setElementInterfaceType(Type type);
5610+
56085611
SubscriptDecl(DeclName Name,
56095612
SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
56105613
SourceLoc SubscriptLoc, ParameterList *Indices,
@@ -5620,6 +5623,27 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
56205623
Bits.SubscriptDecl.StaticSpelling = static_cast<unsigned>(StaticSpelling);
56215624
setIndices(Indices);
56225625
}
5626+
5627+
public:
5628+
/// Factory function only for use by deserialization.
5629+
static SubscriptDecl *createDeserialized(ASTContext &Context, DeclName Name,
5630+
StaticSpellingKind StaticSpelling,
5631+
Type ElementTy, DeclContext *Parent,
5632+
GenericParamList *GenericParams);
5633+
5634+
static SubscriptDecl *create(ASTContext &Context, DeclName Name,
5635+
SourceLoc StaticLoc,
5636+
StaticSpellingKind StaticSpelling,
5637+
SourceLoc SubscriptLoc, ParameterList *Indices,
5638+
SourceLoc ArrowLoc, TypeLoc ElementTy,
5639+
DeclContext *Parent,
5640+
GenericParamList *GenericParams);
5641+
5642+
static SubscriptDecl *createImported(ASTContext &Context, DeclName Name,
5643+
SourceLoc SubscriptLoc,
5644+
ParameterList *Indices,
5645+
SourceLoc ArrowLoc, Type ElementTy,
5646+
DeclContext *Parent, ClangNode ClangN);
56235647

56245648
/// \returns the way 'static'/'class' was spelled in the source.
56255649
StaticSpellingKind getStaticSpelling() const {
@@ -5646,7 +5670,7 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
56465670
/// Retrieve the type of the element referenced by a subscript
56475671
/// operation.
56485672
Type getElementInterfaceType() const;
5649-
TypeLoc &getElementTypeLoc() { return ElementTy; }
5673+
56505674
TypeRepr *getElementTypeRepr() const { return ElementTy.getTypeRepr(); }
56515675
SourceRange getElementTypeSourceRange() const {
56525676
return ElementTy.getSourceRange();

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3166,9 +3166,8 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
31663166
});
31673167
Printer << " -> ";
31683168

3169-
TypeLoc elementTy = decl->getElementTypeLoc();
3170-
if (!elementTy.getTypeRepr())
3171-
elementTy = TypeLoc::withoutLoc(decl->getElementInterfaceType());
3169+
TypeLoc elementTy(decl->getElementTypeRepr(),
3170+
decl->getElementInterfaceType());
31723171
Printer.printDeclResultTypePre(decl, elementTy);
31733172
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
31743173

lib/AST/ASTWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
358358
bool WalkGenerics = visitGenericParamListIfNeeded(SD);
359359

360360
visit(SD->getIndices());
361-
if (auto *const TyR = SD->getElementTypeLoc().getTypeRepr())
361+
if (auto *const TyR = SD->getElementTypeRepr())
362362
if (doIt(TyR))
363363
return true;
364364

lib/AST/Decl.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6584,6 +6584,56 @@ ObjCSubscriptKind SubscriptDecl::getObjCSubscriptKind() const {
65846584
return ObjCSubscriptKind::Keyed;
65856585
}
65866586

6587+
void SubscriptDecl::setElementInterfaceType(Type type) {
6588+
getASTContext().evaluator.cacheOutput(ResultTypeRequest{this},
6589+
std::move(type));
6590+
}
6591+
6592+
SubscriptDecl *
6593+
SubscriptDecl::createDeserialized(ASTContext &Context, DeclName Name,
6594+
StaticSpellingKind StaticSpelling,
6595+
Type ElementTy, DeclContext *Parent,
6596+
GenericParamList *GenericParams) {
6597+
assert(ElementTy && "Deserialized element type must not be null");
6598+
auto *const SD = new (Context)
6599+
SubscriptDecl(Name, SourceLoc(), StaticSpelling, SourceLoc(), nullptr,
6600+
SourceLoc(), TypeLoc(), Parent, GenericParams);
6601+
SD->setElementInterfaceType(ElementTy);
6602+
return SD;
6603+
}
6604+
6605+
SubscriptDecl *SubscriptDecl::create(ASTContext &Context, DeclName Name,
6606+
SourceLoc StaticLoc,
6607+
StaticSpellingKind StaticSpelling,
6608+
SourceLoc SubscriptLoc,
6609+
ParameterList *Indices, SourceLoc ArrowLoc,
6610+
TypeLoc ElementTy, DeclContext *Parent,
6611+
GenericParamList *GenericParams) {
6612+
auto *const SD = new (Context)
6613+
SubscriptDecl(Name, StaticLoc, StaticSpelling, SubscriptLoc, Indices,
6614+
ArrowLoc, ElementTy, Parent, GenericParams);
6615+
return SD;
6616+
}
6617+
6618+
SubscriptDecl *SubscriptDecl::createImported(ASTContext &Context, DeclName Name,
6619+
SourceLoc SubscriptLoc,
6620+
ParameterList *Indices,
6621+
SourceLoc ArrowLoc, Type ElementTy,
6622+
DeclContext *Parent,
6623+
ClangNode ClangN) {
6624+
assert(ClangN && ElementTy);
6625+
auto *DeclPtr = allocateMemoryForDecl<SubscriptDecl>(
6626+
Context, sizeof(SubscriptDecl), /*includeSpaceForClangNode=*/true);
6627+
6628+
auto *const SD = ::new (DeclPtr)
6629+
SubscriptDecl(Name, SourceLoc(), StaticSpellingKind::None, SubscriptLoc,
6630+
Indices, ArrowLoc, TypeLoc(), Parent,
6631+
/*GenericParams=*/nullptr);
6632+
SD->setElementInterfaceType(ElementTy);
6633+
SD->setClangNode(ClangN);
6634+
return SD;
6635+
}
6636+
65876637
SourceRange SubscriptDecl::getSourceRange() const {
65886638
return {getSubscriptLoc(), getEndLoc()};
65896639
}

lib/AST/TypeCheckRequests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ Optional<Type> ResultTypeRequest::getCachedResult() const {
941941
if (const auto *const funcDecl = dyn_cast<FuncDecl>(decl)) {
942942
type = funcDecl->FnRetType.getType();
943943
} else {
944-
type = cast<SubscriptDecl>(decl)->getElementTypeLoc().getType();
944+
type = cast<SubscriptDecl>(decl)->ElementTy.getType();
945945
}
946946

947947
if (type.isNull())
@@ -955,7 +955,7 @@ void ResultTypeRequest::cacheResult(Type type) const {
955955
if (auto *const funcDecl = dyn_cast<FuncDecl>(decl)) {
956956
funcDecl->FnRetType.setType(type);
957957
} else {
958-
cast<SubscriptDecl>(decl)->getElementTypeLoc().setType(type);
958+
cast<SubscriptDecl>(decl)->ElementTy.setType(type);
959959
}
960960
}
961961

lib/ClangImporter/ImportDecl.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6879,12 +6879,14 @@ SwiftDeclConverter::importSubscript(Decl *decl,
68796879
auto &C = Impl.SwiftContext;
68806880
auto bodyParams = ParameterList::create(C, getterIndex);
68816881
DeclName name(C, DeclBaseName::createSubscript(), {Identifier()});
6882-
auto subscript = Impl.createDeclWithClangNode<SubscriptDecl>(
6883-
getter->getClangNode(), getOverridableAccessLevel(dc), name,
6884-
/*StaticLoc=*/SourceLoc(), StaticSpellingKind::None,
6885-
decl->getLoc(), bodyParams, decl->getLoc(),
6886-
TypeLoc::withoutLoc(elementTy), dc,
6887-
/*GenericParams=*/nullptr);
6882+
auto *const subscript = SubscriptDecl::createImported(C,
6883+
name, decl->getLoc(),
6884+
bodyParams, decl->getLoc(),
6885+
elementTy, dc,
6886+
getter->getClangNode());
6887+
const auto access = getOverridableAccessLevel(dc);
6888+
subscript->setAccess(access);
6889+
subscript->setSetterAccess(access);
68886890

68896891
// Build the thunks.
68906892
AccessorDecl *getterThunk =

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
23872387
// i.e. cases where 'ExprType' != 'keyPathInfo.baseType'.
23882388

23892389
auto *SD = keyPathInfo.subscript;
2390-
auto elementTy = SD->getElementTypeLoc().getType();
2390+
const auto elementTy = SD->getElementInterfaceType();
23912391
if (!elementTy->hasTypeParameter())
23922392
return elementTy;
23932393

lib/IDE/SyntaxModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
10961096
SN.NameRange = charSourceRangeFromSourceRange(SM,
10971097
SubscriptD->getSignatureSourceRange());
10981098
SN.TypeRange = charSourceRangeFromSourceRange(SM,
1099-
SubscriptD->getElementTypeLoc().getSourceRange());
1099+
SubscriptD->getElementTypeSourceRange());
11001100
pushStructureNode(SN, SubscriptD);
11011101
} else if (auto *AssociatedTypeD = dyn_cast<AssociatedTypeDecl>(D)) {
11021102
SyntaxStructureNode SN;

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7263,12 +7263,9 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
72637263
// Build an AST for the subscript declaration.
72647264
DeclName name = DeclName(Context, DeclBaseName::createSubscript(),
72657265
argumentNames);
7266-
auto *Subscript = new (Context) SubscriptDecl(name,
7267-
StaticLoc, StaticSpelling,
7268-
SubscriptLoc, Indices.get(),
7269-
ArrowLoc, ElementTy.get(),
7270-
CurDeclContext,
7271-
GenericParams);
7266+
auto *const Subscript = SubscriptDecl::create(
7267+
Context, name, StaticLoc, StaticSpelling, SubscriptLoc, Indices.get(),
7268+
ArrowLoc, ElementTy.get(), CurDeclContext, GenericParams);
72727269
Subscript->getAttrs() = Attributes;
72737270

72747271
// Let the source file track the opaque return type mapping, if any.

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,7 @@ static bool fixItOverrideDeclarationTypesImpl(
22462246
baseSubscript->getElementInterfaceType());
22472247
fixedAny |= checkType(resultType, ParamDecl::Specifier::Default,
22482248
baseResultType, ParamDecl::Specifier::Default,
2249-
subscript->getElementTypeLoc().getSourceRange());
2249+
subscript->getElementTypeSourceRange());
22502250
return fixedAny;
22512251
}
22522252

0 commit comments

Comments
 (0)