Skip to content

Commit 3d3b1ca

Browse files
committed
[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

Lines changed: 8 additions & 12 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 24 additions & 19 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)