Skip to content

Commit 1ca81c4

Browse files
committed
AST: Factor out GenericParamList::configureGenericParamDepth()
1 parent 8a8b569 commit 1ca81c4

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,13 +1422,10 @@ class GenericParamList final :
14221422
Requirements.back().getSourceRange().End);
14231423
}
14241424

1425-
/// Retrieve the depth of this generic parameter list.
1426-
unsigned getDepth() const {
1427-
unsigned depth = 0;
1428-
for (auto gp = getOuterParameters(); gp; gp = gp->getOuterParameters())
1429-
++depth;
1430-
return depth;
1431-
}
1425+
unsigned getDepth() const;
1426+
1427+
/// Configure the depth of the generic parameters in this list.
1428+
void configureGenericParamDepth();
14321429

14331430
/// Create a copy of the generic parameter list and all of its generic
14341431
/// parameter declarations. The copied generic parameters are re-parented

lib/AST/Decl.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,21 @@ void GenericParamList::addTrailingWhereClause(
687687
Requirements = newRequirements;
688688
}
689689

690+
unsigned GenericParamList::getDepth() const {
691+
unsigned depth = 0;
692+
for (auto gpList = getOuterParameters();
693+
gpList != nullptr;
694+
gpList = gpList->getOuterParameters())
695+
++depth;
696+
return depth;
697+
}
698+
699+
void GenericParamList::configureGenericParamDepth() {
700+
unsigned depth = getDepth();
701+
for (auto param : *this)
702+
param->setDepth(depth);
703+
}
704+
690705
TrailingWhereClause::TrailingWhereClause(
691706
SourceLoc whereLoc,
692707
ArrayRef<RequirementRepr> requirements)

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,9 @@ TypeChecker::gatherGenericParamBindingsText(
147147
void
148148
TypeChecker::prepareGenericParamList(GenericParamList *gp,
149149
DeclContext *dc) {
150-
unsigned depth = gp->getDepth();
151-
for (auto paramDecl : *gp) {
150+
gp->configureGenericParamDepth();
151+
for (auto paramDecl : *gp)
152152
checkDeclAttributesEarly(paramDecl);
153-
paramDecl->setDepth(depth);
154-
}
155153
}
156154

157155
/// Add the generic parameter types from the given list to the vector.

lib/Sema/TypeChecker.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,6 @@ static GenericParamList *cloneGenericParams(ASTContext &ctx,
285285
return toParams;
286286
}
287287

288-
/// FIXME: Similar to TypeChecker::prepareGenericParamList(), which needs
289-
/// to be separated from the type checker.
290-
static void prepareGenericParamList(GenericParamList *genericParams) {
291-
unsigned depth = genericParams->getDepth();
292-
for (auto gp : *genericParams) {
293-
if (gp->getDepth() == depth)
294-
return;
295-
296-
gp->setDepth(depth);
297-
}
298-
299-
if (auto outerGenericParams = genericParams->getOuterParameters())
300-
prepareGenericParamList(outerGenericParams);
301-
}
302-
303288
/// Ensure that the outer generic parameters of the given generic
304289
/// context have been configured.
305290
static void configureOuterGenericParams(const GenericContext *dc) {
@@ -353,9 +338,12 @@ static void bindExtensionToNominal(ExtensionDecl *ext,
353338
cloneGenericParams(ext->getASTContext(), ext, genericParams));
354339
}
355340

341+
// Set the depth of every generic parameter.
356342
auto *genericParams = ext->getGenericParams();
357-
if (genericParams)
358-
prepareGenericParamList(genericParams);
343+
for (auto *outerParams = genericParams;
344+
outerParams != nullptr;
345+
outerParams = outerParams->getOuterParameters())
346+
outerParams->configureGenericParamDepth();
359347

360348
// If we have a trailing where clause, deal with it now.
361349
// For now, trailing where clauses are only permitted on protocol extensions.

0 commit comments

Comments
 (0)