Skip to content

Commit 58a84ea

Browse files
authored
[NFC] Cleanup some code to use 'swift::getParameterList(ValueDecl)' to fetch parameter lists (swiftlang#32979)
1 parent c28d9cd commit 58a84ea

File tree

10 files changed

+46
-88
lines changed

10 files changed

+46
-88
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -735,23 +735,12 @@ static bool getUnnamedParamIndex(const ParameterList *ParamList,
735735
}
736736

737737
static unsigned getUnnamedParamIndex(const ParamDecl *D) {
738-
if (auto SD = dyn_cast<SubscriptDecl>(D->getDeclContext())) {
739-
unsigned UnnamedIndex = 0;
740-
auto *ParamList = SD->getIndices();
741-
if (getUnnamedParamIndex(ParamList, D, UnnamedIndex))
742-
return UnnamedIndex;
743-
llvm_unreachable("param not found");
744-
}
745-
746738
ParameterList *ParamList;
747-
748-
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D->getDeclContext())) {
749-
ParamList = AFD->getParameters();
750-
} else if (auto EED = dyn_cast<EnumElementDecl>(D->getDeclContext())) {
751-
ParamList = EED->getParameterList();
739+
auto *DC = D->getDeclContext();
740+
if (isa<AbstractClosureExpr>(DC)) {
741+
ParamList = cast<AbstractClosureExpr>(DC)->getParameters();
752742
} else {
753-
auto ACE = cast<AbstractClosureExpr>(D->getDeclContext());
754-
ParamList = ACE->getParameters();
743+
ParamList = getParameterList(cast<ValueDecl>(DC->getAsDecl()));
755744
}
756745

757746
unsigned UnnamedIndex = 0;

lib/AST/Decl.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,16 +723,13 @@ bool ParameterList::hasInternalParameter(StringRef Prefix) const {
723723

724724
bool Decl::hasUnderscoredNaming() const {
725725
const Decl *D = this;
726-
if (const auto AFD = dyn_cast<AbstractFunctionDecl>(D)) {
727-
// If it's a function with a parameter with leading underscore, it's a
728-
// private function.
729-
if (AFD->getParameters()->hasInternalParameter("_")) {
730-
return true;
731-
}
732-
}
733726

734-
if (const auto SubscriptD = dyn_cast<SubscriptDecl>(D)) {
735-
if (SubscriptD->getIndices()->hasInternalParameter("_")) {
727+
// If it's a function or subscript with a parameter with leading
728+
// underscore, it's a private function or subscript.
729+
if (isa<AbstractFunctionDecl>(D) || isa<SubscriptDecl>(D)) {
730+
const auto VD = cast<ValueDecl>(D);
731+
if (getParameterList(const_cast<ValueDecl *>(VD))
732+
->hasInternalParameter("_")) {
736733
return true;
737734
}
738735
}

lib/AST/Type.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -850,14 +850,8 @@ ParameterListInfo::ParameterListInfo(
850850
return;
851851

852852
// Find the corresponding parameter list.
853-
const ParameterList *paramList = nullptr;
854-
if (auto *func = dyn_cast<AbstractFunctionDecl>(paramOwner)) {
855-
paramList = func->getParameters();
856-
} else if (auto *subscript = dyn_cast<SubscriptDecl>(paramOwner)) {
857-
paramList = subscript->getIndices();
858-
} else if (auto *enumElement = dyn_cast<EnumElementDecl>(paramOwner)) {
859-
paramList = enumElement->getParameterList();
860-
}
853+
const ParameterList *paramList =
854+
getParameterList(const_cast<ValueDecl *>(paramOwner));
861855

862856
// No parameter list means no default arguments - hand back the zeroed
863857
// bitvector.

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,7 @@ class ExprContextAnalyzer {
766766
auto Params = typeAndDecl.Type->getParams();
767767
ParameterList *paramList = nullptr;
768768
if (auto VD = typeAndDecl.Decl) {
769-
if (auto FD = dyn_cast<AbstractFunctionDecl>(VD))
770-
paramList = FD->getParameters();
771-
else if (auto SD = dyn_cast<SubscriptDecl>(VD))
772-
paramList = SD->getIndices();
769+
paramList = getParameterList(VD);
773770
if (paramList && paramList->size() != Params.size())
774771
paramList = nullptr;
775772
}

lib/IDE/Formatting.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -501,18 +501,14 @@ class RangeWalker: protected ASTWalker {
501501
} else if (auto *VD = dyn_cast<VarDecl>(D)) {
502502
if (!handleBraces(VD->getBracesRange(), VD->getNameLoc()))
503503
return Stop;
504-
} else if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
505-
if (auto *PL = AFD->getParameters()) {
506-
if (!handleParens(PL->getLParenLoc(), PL->getRParenLoc(), ContextLoc))
504+
} else if (isa<AbstractFunctionDecl>(D) || isa<SubscriptDecl>(D)) {
505+
if (isa<SubscriptDecl>(D)) {
506+
if (!handleBraces(cast<SubscriptDecl>(D)->getBracesRange(), ContextLoc))
507507
return Stop;
508508
}
509-
} else if (auto *SD = dyn_cast<SubscriptDecl>(D)) {
510-
if (!handleBraces(SD->getBracesRange(), ContextLoc))
509+
auto *PL = getParameterList(cast<ValueDecl>(D));
510+
if (!handleParens(PL->getLParenLoc(), PL->getRParenLoc(), ContextLoc))
511511
return Stop;
512-
if (auto *PL = SD->getIndices()) {
513-
if (!handleParens(PL->getLParenLoc(), PL->getRParenLoc(), ContextLoc))
514-
return Stop;
515-
}
516512
} else if (auto *PGD = dyn_cast<PrecedenceGroupDecl>(D)) {
517513
SourceRange Braces(PGD->getLBraceLoc(), PGD->getRBraceLoc());
518514
if (!handleBraces(Braces, ContextLoc))

lib/IDE/SourceEntityWalker.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,9 @@ bool SemaAnnotator::walkToDeclPre(Decl *D) {
140140
return false;
141141
};
142142

143-
if (auto AF = dyn_cast<AbstractFunctionDecl>(VD)) {
144-
if (ReportParamList(AF->getParameters()))
145-
return false;
146-
}
147-
if (auto SD = dyn_cast<SubscriptDecl>(VD)) {
148-
if (ReportParamList(SD->getIndices()))
143+
if (isa<AbstractFunctionDecl>(VD) || isa<SubscriptDecl>(VD)) {
144+
auto ParamList = getParameterList(VD);
145+
if (ReportParamList(ParamList))
149146
return false;
150147
}
151148
} else if (auto *ED = dyn_cast<ExtensionDecl>(D)) {

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -536,20 +536,17 @@ static void diagnoseGeneralOverrideFailure(ValueDecl *decl,
536536
static bool parameterTypesMatch(const ValueDecl *derivedDecl,
537537
const ValueDecl *baseDecl,
538538
TypeMatchOptions matchMode) {
539-
const ParameterList *derivedParams;
540-
const ParameterList *baseParams;
541-
if (auto *derived = dyn_cast<AbstractFunctionDecl>(derivedDecl)) {
542-
auto *base = dyn_cast<AbstractFunctionDecl>(baseDecl);
543-
if (!base)
544-
return false;
545-
baseParams = base->getParameters();
546-
derivedParams = derived->getParameters();
547-
} else {
548-
auto *base = dyn_cast<SubscriptDecl>(baseDecl);
549-
if (!base)
550-
return false;
551-
baseParams = base->getIndices();
552-
derivedParams = cast<SubscriptDecl>(derivedDecl)->getIndices();
539+
const ParameterList *derivedParams = nullptr;
540+
const ParameterList *baseParams = nullptr;
541+
if ((isa<AbstractFunctionDecl>(derivedDecl) &&
542+
isa<AbstractFunctionDecl>(baseDecl)) ||
543+
isa<SubscriptDecl>(baseDecl)) {
544+
derivedParams = getParameterList(const_cast<ValueDecl *>(derivedDecl));
545+
baseParams = getParameterList(const_cast<ValueDecl *>(baseDecl));
546+
}
547+
548+
if (!derivedParams && !baseParams) {
549+
return false;
553550
}
554551

555552
if (baseParams->size() != derivedParams->size())

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,10 +2076,8 @@ SourceLoc OptionalAdjustment::getOptionalityLoc(ValueDecl *witness) const {
20762076

20772077
// For parameter adjustments, dig out the pattern.
20782078
ParameterList *params = nullptr;
2079-
if (auto func = dyn_cast<AbstractFunctionDecl>(witness)) {
2080-
params = func->getParameters();
2081-
} else if (auto subscript = dyn_cast<SubscriptDecl>(witness)) {
2082-
params = subscript->getIndices();
2079+
if (isa<AbstractFunctionDecl>(witness) || isa<SubscriptDecl>(witness)) {
2080+
params = getParameterList(witness);
20832081
} else {
20842082
return SourceLoc();
20852083
}
@@ -4793,12 +4791,11 @@ static bool isGeneric(ValueDecl *decl) {
47934791
/// Determine whether this is an unlabeled initializer or subscript.
47944792
static bool isUnlabeledInitializerOrSubscript(ValueDecl *value) {
47954793
ParameterList *paramList = nullptr;
4796-
if (auto constructor = dyn_cast<ConstructorDecl>(value))
4797-
paramList = constructor->getParameters();
4798-
else if (auto subscript = dyn_cast<SubscriptDecl>(value))
4799-
paramList = subscript->getIndices();
4800-
else
4794+
if (isa<ConstructorDecl>(value) || isa<SubscriptDecl>(value)) {
4795+
paramList = getParameterList(value);
4796+
} else {
48014797
return false;
4798+
}
48024799

48034800
for (auto param : *paramList) {
48044801
if (!param->getArgumentName().empty()) return false;

lib/Serialization/SerializeDoc.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,13 @@ static bool hasDoubleUnderscore(Decl *D) {
313313
// base names.
314314
static StringRef Prefix = "__";
315315

316-
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D)) {
317-
// If it's a function with a parameter with leading double underscore,
318-
// it's a private function.
319-
if (AFD->getParameters()->hasInternalParameter(Prefix))
316+
// If it's a function or subscript with a parameter with leading
317+
// double underscore, it's a private function or subscript.
318+
if (isa<AbstractFunctionDecl>(D) || isa<SubscriptDecl>(D)) {
319+
if (getParameterList(cast<ValueDecl>(D))->hasInternalParameter(Prefix))
320320
return true;
321321
}
322322

323-
if (auto SubscriptD = dyn_cast<SubscriptDecl>(D)) {
324-
if (SubscriptD->getIndices()->hasInternalParameter(Prefix))
325-
return true;
326-
}
327323
if (auto *VD = dyn_cast<ValueDecl>(D)) {
328324
auto Name = VD->getBaseName();
329325
if (!Name.isSpecial() &&

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -915,12 +915,10 @@ void SwiftLangSupport::printMemberDeclDescription(const swift::ValueDecl *VD,
915915
}
916916
OS << ')';
917917
};
918-
if (auto EED = dyn_cast<EnumElementDecl>(VD)) {
919-
if (auto params = EED->getParameterList())
920-
printParams(params);
921-
} else if (auto *FD = dyn_cast<FuncDecl>(VD)) {
922-
if (auto params = FD->getParameters())
923-
printParams(params);
918+
if (isa<EnumElementDecl>(VD) || isa<FuncDecl>(VD)) {
919+
if (const auto ParamList = getParameterList(const_cast<ValueDecl *>(VD))) {
920+
printParams(ParamList);
921+
}
924922
} else if (isa<VarDecl>(VD)) {
925923
// Var decl doesn't have parameters.
926924
} else {

0 commit comments

Comments
 (0)