Skip to content

Commit 81abcb8

Browse files
committed
Add a utility function for getting the interface value type of storage; NFC.
Because subscripts can be generic, this isn't quite as universally useful as you might think; it's pretty much only useful when mapping types into the context of the declaration, e.g. into an accessor.
1 parent 584146f commit 81abcb8

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,6 +4134,9 @@ class AbstractStorageDecl : public ValueDecl {
41344134
/// is a member. Currently only variables can be static.
41354135
inline bool isStatic() const; // defined in this header
41364136

4137+
/// \brief Return the interface type of the stored value.
4138+
Type getValueInterfaceType() const;
4139+
41374140
/// \brief Determine how this storage is implemented.
41384141
StorageImplInfo getImplInfo() const {
41394142
if (auto ptr = Accessors.getPointer())

lib/AST/Decl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4071,6 +4071,12 @@ SourceLoc AbstractStorageDecl::getOverrideLoc() const {
40714071
return SourceLoc();
40724072
}
40734073

4074+
Type AbstractStorageDecl::getValueInterfaceType() const {
4075+
if (auto var = dyn_cast<VarDecl>(this))
4076+
return var->getInterfaceType();
4077+
return cast<SubscriptDecl>(this)->getElementInterfaceType();
4078+
}
4079+
40744080
Type VarDecl::getType() const {
40754081
if (!typeInContext) {
40764082
const_cast<VarDecl *>(this)->typeInContext =

lib/ClangImporter/ImporterImpl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
625625
} else if (auto *CD = dyn_cast<ConstructorDecl>(decl)) {
626626
assert(CD->getInterfaceType());
627627
ty = CD->getResultInterfaceType();
628-
} else if (auto *SD = dyn_cast<SubscriptDecl>(decl)) {
629-
ty = SD->getElementInterfaceType();
630628
} else {
631-
auto *VD = cast<VarDecl>(decl);
632-
ty = VD->getInterfaceType()->getReferenceStorageReferent();
629+
ty = cast<AbstractStorageDecl>(decl)->getValueInterfaceType()
630+
->getReferenceStorageReferent();
633631
}
634632
#endif
635633

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ Type swift::getMemberTypeForComparison(ASTContext &ctx, ValueDecl *member,
7070

7171
auto abstractStorage = dyn_cast<AbstractStorageDecl>(member);
7272
assert((method || abstractStorage) && "Not a method or abstractStorage?");
73-
SubscriptDecl *subscript = nullptr;
74-
if (abstractStorage)
75-
subscript = dyn_cast<SubscriptDecl>(abstractStorage);
73+
SubscriptDecl *subscript = dyn_cast_or_null<SubscriptDecl>(abstractStorage);
7674

7775
auto memberType = member->getInterfaceType();
7876
if (derivedDecl) {

0 commit comments

Comments
 (0)