Skip to content

Commit 3d3b1ca

Browse files
committedMar 19, 2025
[NFC] AST: Turn getParameterList into a method on ValueDecl
1 parent 012ac5d commit 3d3b1ca

18 files changed

+58
-64
lines changed
 

‎include/swift/AST/Decl.h

+8-12
Original file line numberDiff line numberDiff line change
@@ -3320,6 +3320,14 @@ class ValueDecl : public Decl {
33203320
/// parameter lists, for example an enum element without associated values.
33213321
bool hasParameterList() const;
33223322

3323+
/// Returns the parameter list directly associated with this declaration,
3324+
/// or `nullptr` if there is none.
3325+
///
3326+
/// Note that some declarations with function interface types do not have
3327+
/// parameter lists. For example, an enum element without associated values.
3328+
ParameterList *getParameterList();
3329+
const ParameterList *getParameterList() const;
3330+
33233331
/// Returns the number of curry levels in the declaration's interface type.
33243332
unsigned getNumCurryLevels() const;
33253333

@@ -9720,14 +9728,6 @@ inline bool ValueDecl::hasCurriedSelf() const {
97209728
return false;
97219729
}
97229730

9723-
inline bool ValueDecl::hasParameterList() const {
9724-
if (auto *eed = dyn_cast<EnumElementDecl>(this))
9725-
return eed->hasAssociatedValues();
9726-
if (auto *macro = dyn_cast<MacroDecl>(this))
9727-
return macro->parameterList != nullptr;
9728-
return isa<AbstractFunctionDecl>(this) || isa<SubscriptDecl>(this);
9729-
}
9730-
97319731
inline unsigned ValueDecl::getNumCurryLevels() const {
97329732
unsigned curryLevels = 0;
97339733
if (hasParameterList())
@@ -9815,10 +9815,6 @@ inline EnumElementDecl *EnumDecl::getUniqueElement(bool hasValue) const {
98159815
return result;
98169816
}
98179817

9818-
/// Retrieve the parameter list for a given declaration, or nullptr if there
9819-
/// is none.
9820-
ParameterList *getParameterList(ValueDecl *source);
9821-
98229818
/// Retrieve the parameter list for a given declaration context, or nullptr if
98239819
/// there is none.
98249820
ParameterList *getParameterList(DeclContext *source);

‎lib/AST/ASTMangler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ static unsigned getUnnamedParamIndex(const ParamDecl *D) {
10741074
if (isa<AbstractClosureExpr>(DC)) {
10751075
ParamList = cast<AbstractClosureExpr>(DC)->getParameters();
10761076
} else {
1077-
ParamList = getParameterList(cast<ValueDecl>(DC->getAsDecl()));
1077+
ParamList = cast<ValueDecl>(DC->getAsDecl())->getParameterList();
10781078
}
10791079

10801080
unsigned UnnamedIndex = 0;

‎lib/AST/Decl.cpp

+24-19
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,7 @@ bool Decl::hasUnderscoredNaming() const {
13161316
// underscore, it's a private function or subscript.
13171317
if (isa<AbstractFunctionDecl>(D) || isa<SubscriptDecl>(D)) {
13181318
const auto VD = cast<ValueDecl>(D);
1319-
if (getParameterList(const_cast<ValueDecl *>(VD))
1320-
->hasInternalParameter("_")) {
1319+
if (VD->getParameterList()->hasInternalParameter("_")) {
13211320
return true;
13221321
}
13231322
}
@@ -4096,6 +4095,26 @@ ValueDecl::getCachedOpaqueResultTypeDecl() const {
40964095
.getCachedResult();
40974096
}
40984097

4098+
ParameterList *ValueDecl::getParameterList() {
4099+
if (auto *function = dyn_cast<AbstractFunctionDecl>(this)) {
4100+
return function->getParameters();
4101+
} else if (auto *enumElement = dyn_cast<EnumElementDecl>(this)) {
4102+
return enumElement->getParameterList();
4103+
} else if (auto *subscript = dyn_cast<SubscriptDecl>(this)) {
4104+
return subscript->getIndices();
4105+
} else if (auto *macro = dyn_cast<MacroDecl>(this)) {
4106+
return macro->parameterList;
4107+
}
4108+
4109+
return nullptr;
4110+
}
4111+
4112+
const ParameterList *ValueDecl::getParameterList() const {
4113+
return const_cast<ValueDecl *>(this)->getParameterList();
4114+
}
4115+
4116+
bool ValueDecl::hasParameterList() const { return (bool)getParameterList(); }
4117+
40994118
bool ValueDecl::isObjC() const {
41004119
ASTContext &ctx = getASTContext();
41014120
return evaluateOrDefault(ctx.evaluator,
@@ -9578,24 +9597,10 @@ DeclName AbstractFunctionDecl::getEffectiveFullName() const {
95789597
return DeclName();
95799598
}
95809599

9581-
ParameterList *swift::getParameterList(ValueDecl *source) {
9582-
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(source)) {
9583-
return AFD->getParameters();
9584-
} else if (auto *EED = dyn_cast<EnumElementDecl>(source)) {
9585-
return EED->getParameterList();
9586-
} else if (auto *SD = dyn_cast<SubscriptDecl>(source)) {
9587-
return SD->getIndices();
9588-
} else if (auto *MD = dyn_cast<MacroDecl>(source)) {
9589-
return MD->parameterList;
9590-
}
9591-
9592-
return nullptr;
9593-
}
9594-
95959600
ParameterList *swift::getParameterList(DeclContext *source) {
95969601
if (auto *D = source->getAsDecl()) {
95979602
if (auto *VD = dyn_cast<ValueDecl>(D)) {
9598-
return getParameterList(VD);
9603+
return VD->getParameterList();
95999604
}
96009605
} else if (auto *CE = dyn_cast<AbstractClosureExpr>(source)) {
96019606
return CE->getParameters();
@@ -9607,7 +9612,7 @@ ParameterList *swift::getParameterList(DeclContext *source) {
96079612
const ParamDecl *swift::getParameterAt(ConcreteDeclRef declRef,
96089613
unsigned index) {
96099614
auto *source = declRef.getDecl();
9610-
if (auto *params = getParameterList(const_cast<ValueDecl *>(source))) {
9615+
if (auto *params = source->getParameterList()) {
96119616
unsigned origIndex = params->getOrigParamIndex(declRef.getSubstitutions(),
96129617
index);
96139618
return params->get(origIndex);
@@ -9617,7 +9622,7 @@ const ParamDecl *swift::getParameterAt(ConcreteDeclRef declRef,
96179622

96189623
const ParamDecl *swift::getParameterAt(const ValueDecl *source,
96199624
unsigned index) {
9620-
if (auto *params = getParameterList(const_cast<ValueDecl *>(source))) {
9625+
if (auto *params = source->getParameterList()) {
96219626
return index < params->size() ? params->get(index) : nullptr;
96229627
}
96239628
return nullptr;

‎lib/AST/DeclContext.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
501501
dc = dc->getParent();
502502

503503
auto *VD = cast<ValueDecl>(dc->getAsDecl());
504-
assert(VD->hasParameterList());
504+
ASSERT(VD->hasParameterList());
505505

506506
if (VD->getDeclContext()->isLocalContext()) {
507507
auto kind = VD->getDeclContext()->getFragileFunctionKind();

‎lib/AST/FeatureSet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ static bool usesFeatureExecutionAttribute(Decl *decl) {
497497
};
498498

499499
// Check if any parameters that have `@execution` attribute.
500-
if (auto *PL = getParameterList(VD)) {
500+
if (auto *PL = VD->getParameterList()) {
501501
for (auto *P : *PL) {
502502
if (hasExecutionAttr(P->getTypeRepr()))
503503
return true;

‎lib/AST/RawComment.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static bool hasDoubleUnderscore(const Decl *D) {
242242
// If it's a function or subscript with a parameter with leading
243243
// double underscore, it's a private function or subscript.
244244
if (isa<AbstractFunctionDecl>(D) || isa<SubscriptDecl>(D)) {
245-
auto *params = getParameterList(cast<ValueDecl>(const_cast<Decl *>(D)));
245+
auto *params = cast<ValueDecl>(D)->getParameterList();
246246
if (params->hasInternalParameter(Prefix))
247247
return true;
248248
}

‎lib/AST/Type.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -1336,13 +1336,11 @@ ParameterListInfo::ParameterListInfo(
13361336
return;
13371337

13381338
// Find the corresponding parameter list.
1339-
const ParameterList *paramList =
1340-
getParameterList(const_cast<ValueDecl *>(paramOwner));
1339+
auto *paramList = paramOwner->getParameterList();
13411340

13421341
// No parameter list means no default arguments - hand back the zeroed
13431342
// bitvector.
13441343
if (!paramList) {
1345-
assert(!paramOwner->hasParameterList());
13461344
return;
13471345
}
13481346

‎lib/IDE/Formatting.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ class RangeWalker: protected ASTWalker {
518518
if (!handleBraces(cast<SubscriptDecl>(D)->getBracesRange(), ContextLoc))
519519
return Action::Stop();
520520
}
521-
auto *PL = getParameterList(cast<ValueDecl>(D));
521+
auto *PL = cast<ValueDecl>(D)->getParameterList();
522522
if (!handleParens(PL->getLParenLoc(), PL->getRParenLoc(), ContextLoc))
523523
return Action::Stop();
524524
} else if (auto *PGD = dyn_cast<PrecedenceGroupDecl>(D)) {

‎lib/IDE/SourceEntityWalker.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ ASTWalker::PreWalkAction SemaAnnotator::walkToDeclPreProper(Decl *D) {
169169
};
170170

171171
if (isa<AbstractFunctionDecl>(VD) || isa<SubscriptDecl>(VD)) {
172-
auto ParamList = getParameterList(VD);
172+
auto ParamList = VD->getParameterList();
173173
if (!ReportParamList(ParamList))
174174
return Action::Stop();
175175
}

‎lib/Sema/CSApply.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -6230,8 +6230,8 @@ ArgumentList *ExprRewriter::coerceCallArguments(
62306230
// If bindings were substituted we need to find "original"
62316231
// (or contextless) parameter index for the default argument.
62326232
if (shouldSubstituteBindings) {
6233-
auto *paramList = getParameterList(callee.getDecl());
6234-
assert(paramList);
6233+
auto *paramList = callee.getDecl()->getParameterList();
6234+
ASSERT(paramList);
62356235
paramIdxForDefault =
62366236
paramList->getOrigParamIndex(callee.getSubstitutions(), paramIdx);
62376237
}

‎lib/Sema/CSDiagnostics.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9354,7 +9354,7 @@ bool DefaultExprTypeMismatch::diagnoseAsError() {
93549354

93559355
auto overload = getSolution().getCalleeOverloadChoice(locator);
93569356

9357-
auto *PD = getParameterList(overload.choice.getDecl())->get(paramIdx);
9357+
auto *PD = overload.choice.getDecl()->getParameterList()->get(paramIdx);
93589358

93599359
auto note = emitDiagnosticAt(PD->getLoc(), diag::default_value_declared_here);
93609360

‎lib/Sema/CSSimplify.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
17031703
if (parameterBindings[paramIdx].empty() && callee) {
17041704
// Type inference from default value expressions.
17051705
{
1706-
auto *paramList = getParameterList(callee);
1706+
auto *paramList = callee->getParameterList();
17071707
if (!paramList)
17081708
continue;
17091709

@@ -5758,7 +5758,7 @@ bool ConstraintSystem::repairFailures(
57585758

57595759
// Ignore decls that don't have meaningful parameter lists - this
57605760
// matches variables and parameters with function types.
5761-
auto *paramList = getParameterList(overload->choice.getDecl());
5761+
auto *paramList = overload->choice.getDecl()->getParameterList();
57625762
if (!paramList)
57635763
return true;
57645764

@@ -6052,7 +6052,7 @@ bool ConstraintSystem::repairFailures(
60526052

60536053
if (auto overload = findSelectedOverloadFor(calleeLocator)) {
60546054
if (auto *decl = overload->choice.getDeclOrNull()) {
6055-
if (auto paramList = getParameterList(decl)) {
6055+
if (auto paramList = decl->getParameterList()) {
60566056
if (paramList->get(paramIdx)->getTypeOfDefaultExpr()) {
60576057
conversionsOrFixes.push_back(
60586058
IgnoreDefaultExprTypeMismatch::create(*this, lhs, rhs, loc));

‎lib/Sema/TypeCheckConcurrency.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5644,7 +5644,7 @@ static OverrideIsolationResult validOverrideIsolation(
56445644
/// Retrieve the index of the first isolated parameter of the given
56455645
/// declaration, if there is one.
56465646
static std::optional<unsigned> getIsolatedParamIndex(ValueDecl *value) {
5647-
auto params = getParameterList(value);
5647+
auto *params = value->getParameterList();
56485648
if (!params)
56495649
return std::nullopt;
56505650

@@ -5864,7 +5864,7 @@ static InferredActorIsolation computeActorIsolation(Evaluator &evaluator,
58645864
if (auto paramIdx = getIsolatedParamIndex(value)) {
58655865
checkDeclWithIsolatedParameter(value);
58665866

5867-
ParamDecl *param = getParameterList(value)->get(*paramIdx);
5867+
ParamDecl *param = value->getParameterList()->get(*paramIdx);
58685868
Type paramType = param->getDeclContext()->mapTypeIntoContext(
58695869
param->getInterfaceType());
58705870
Type actorType;

‎lib/Sema/TypeCheckDeclOverride.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,8 @@ static bool parameterTypesMatch(const ValueDecl *derivedDecl,
648648
if ((isa<AbstractFunctionDecl>(derivedDecl) &&
649649
isa<AbstractFunctionDecl>(baseDecl)) ||
650650
isa<SubscriptDecl>(baseDecl)) {
651-
derivedParams = getParameterList(const_cast<ValueDecl *>(derivedDecl));
652-
baseParams = getParameterList(const_cast<ValueDecl *>(baseDecl));
651+
derivedParams = derivedDecl->getParameterList();
652+
baseParams = baseDecl->getParameterList();
653653
}
654654

655655
if (!derivedParams && !baseParams) {

‎lib/Sema/TypeCheckDeclPrimary.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ DefaultArgumentInitializer *
13651365
DefaultArgumentInitContextRequest::evaluate(Evaluator &eval,
13661366
ParamDecl *param) const {
13671367
auto *parentDC = param->getDeclContext();
1368-
auto *paramList = getParameterList(cast<ValueDecl>(parentDC->getAsDecl()));
1368+
auto *paramList = cast<ValueDecl>(parentDC->getAsDecl())->getParameterList();
13691369

13701370
// In order to compute the initializer context for this parameter, we need to
13711371
// know its index in the parameter list. Therefore iterate over the parameters

‎lib/Sema/TypeCheckEffects.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ class AbstractFunction {
398398
.getParameterType();
399399

400400
case Kind::Function: {
401-
auto params = getParameterList(static_cast<ValueDecl *>(getFunction()));
401+
auto *params = getFunction()->getParameters();
402402
auto origIndex = params->getOrigParamIndex(getSubstitutions(), substIndex);
403403
return params->get(origIndex)->getInterfaceType();
404404
}

‎lib/Sema/TypeCheckProtocol.cpp

+6-11
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,10 @@ RequirementMatch swift::matchWitness(
804804
return RequirementMatch(witness, MatchKind::TypeConflict,
805805
witnessType);
806806

807-
ParameterList *witnessParamList = getParameterList(witness);
807+
ParameterList *witnessParamList = witness->getParameterList();
808808
assert(witnessParamList->size() == witnessParams.size());
809809

810-
ParameterList *reqParamList = getParameterList(req);
810+
ParameterList *reqParamList = req->getParameterList();
811811
assert(reqParamList->size() == reqParams.size());
812812

813813
// Match each of the parameters.
@@ -2837,10 +2837,8 @@ SourceLoc OptionalAdjustment::getOptionalityLoc(ValueDecl *witness) const {
28372837
}
28382838

28392839
// For parameter adjustments, dig out the pattern.
2840-
ParameterList *params = nullptr;
2841-
if (isa<AbstractFunctionDecl>(witness) || isa<SubscriptDecl>(witness)) {
2842-
params = getParameterList(witness);
2843-
} else {
2840+
auto *params = witness->getParameterList();
2841+
if (!params) {
28442842
return SourceLoc();
28452843
}
28462844

@@ -6010,14 +6008,11 @@ static bool isGeneric(ValueDecl *decl) {
60106008

60116009
/// Determine whether this is an unlabeled initializer or subscript.
60126010
static bool isUnlabeledInitializerOrSubscript(ValueDecl *value) {
6013-
ParameterList *paramList = nullptr;
6014-
if (isa<ConstructorDecl>(value) || isa<SubscriptDecl>(value)) {
6015-
paramList = getParameterList(value);
6016-
} else {
6011+
if (!(isa<ConstructorDecl>(value) || isa<SubscriptDecl>(value))) {
60176012
return false;
60186013
}
60196014

6020-
for (auto param : *paramList) {
6015+
for (auto param : *value->getParameterList()) {
60216016
if (!param->getArgumentName().empty()) return false;
60226017
}
60236018

‎tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ void SwiftLangSupport::printMemberDeclDescription(const swift::ValueDecl *VD,
966966
OS << ')';
967967
};
968968
if (isa<EnumElementDecl>(VD) || isa<FuncDecl>(VD)) {
969-
if (const auto ParamList = getParameterList(const_cast<ValueDecl *>(VD))) {
969+
if (const auto ParamList = VD->getParameterList()) {
970970
printParams(ParamList);
971971
}
972972
} else if (isa<VarDecl>(VD)) {

0 commit comments

Comments
 (0)
Please sign in to comment.