Skip to content

Commit b43b1ec

Browse files
committed
[NFC] Allow getParameter{List,At} to be called on any ValueDecl
These now return nullptr for parameter-list-free types; previously those would have failed a cast.
1 parent 6c99bda commit b43b1ec

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7405,10 +7405,12 @@ inline EnumElementDecl *EnumDecl::getUniqueElement(bool hasValue) const {
74057405
return result;
74067406
}
74077407

7408-
/// Retrieve the parameter list for a given declaration.
7408+
/// Retrieve the parameter list for a given declaration, or nullputr if there
7409+
/// is none.
74097410
ParameterList *getParameterList(ValueDecl *source);
74107411

7411-
/// Retrieve parameter declaration from the given source at given index.
7412+
/// Retrieve parameter declaration from the given source at given index, or
7413+
/// nullptr if the source does not have a parameter list.
74127414
const ParamDecl *getParameterAt(const ValueDecl *source, unsigned index);
74137415

74147416
/// Display Decl subclasses.

lib/AST/Decl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6623,14 +6623,19 @@ ParameterList *swift::getParameterList(ValueDecl *source) {
66236623
return AFD->getParameters();
66246624
} else if (auto *EED = dyn_cast<EnumElementDecl>(source)) {
66256625
return EED->getParameterList();
6626-
} else {
6627-
return cast<SubscriptDecl>(source)->getIndices();
6626+
} else if (auto *SD = dyn_cast<SubscriptDecl>(source)) {
6627+
return SD->getIndices();
66286628
}
6629+
6630+
return nullptr;
66296631
}
66306632

66316633
const ParamDecl *swift::getParameterAt(const ValueDecl *source,
66326634
unsigned index) {
6633-
return getParameterList(const_cast<ValueDecl *>(source))->get(index);
6635+
if (auto *params = getParameterList(const_cast<ValueDecl *>(source))) {
6636+
return params->get(index);
6637+
}
6638+
return nullptr;
66346639
}
66356640

66366641
Type AbstractFunctionDecl::getMethodInterfaceType() const {

0 commit comments

Comments
 (0)