Skip to content

Commit 14dd4ee

Browse files
committed
AST: Add a way to distinguish a parsed generic parameter list from a synthesized one
Add a new GenericContext::getParsedGenericParams(). This produces the same value as GenericContext::getGenericParams() if the generic parameter list was written in source. For extensions and protocols, this returns nullptr without synthesizing anything.
1 parent 32ceba8 commit 14dd4ee

File tree

8 files changed

+45
-26
lines changed

8 files changed

+45
-26
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,11 @@ class GenericContext : private _GenericContext, public DeclContext {
14151415
/// this context is not generic.
14161416
GenericParamList *getGenericParams() const;
14171417

1418+
/// Retrieve the generic parameters as written in source. Unlike
1419+
/// getGenericParams() this will not synthesize generic parameters for
1420+
/// extensions, protocols and certain type aliases.
1421+
GenericParamList *getParsedGenericParams() const;
1422+
14181423
/// Determine whether this context has generic parameters
14191424
/// of its own.
14201425
///

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ namespace {
561561
};
562562

563563
if (const auto GC = Owner.dyn_cast<const GenericContext *>()) {
564-
if (!GC->isGeneric() || isa<ProtocolDecl>(GC))
564+
if (GC->getParsedGenericParams() == nullptr)
565565
printWhere(GC->getTrailingWhereClause());
566566
} else {
567567
const auto ATD = Owner.get<const AssociatedTypeDecl *>();
@@ -730,9 +730,9 @@ namespace {
730730
OS << ' ';
731731
printDeclName(VD);
732732
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD))
733-
printGenericParameters(OS, AFD->getGenericParams());
733+
printGenericParameters(OS, AFD->getParsedGenericParams());
734734
if (auto *GTD = dyn_cast<GenericTypeDecl>(VD))
735-
printGenericParameters(OS, GTD->getGenericParams());
735+
printGenericParameters(OS, GTD->getParsedGenericParams());
736736

737737
if (auto *var = dyn_cast<VarDecl>(VD)) {
738738
PrintWithColorRAII(OS, TypeColor) << " type='";

lib/AST/ASTScopeCreation.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,9 +1599,13 @@ ASTScopeImpl *GenericTypeOrExtensionWholePortion::expandScope(
15991599
if (scope->shouldHaveABody() && !scope->doesDeclHaveABody())
16001600
return ip;
16011601

1602+
auto *context = scope->getGenericContext();
1603+
auto *genericParams = (isa<TypeAliasDecl>(context)
1604+
? context->getParsedGenericParams()
1605+
: context->getGenericParams());
16021606
auto *deepestScope = scopeCreator.addNestedGenericParamScopesToTree(
1603-
scope->getDecl(), scope->getGenericContext()->getGenericParams(), scope);
1604-
if (scope->getGenericContext()->getTrailingWhereClause())
1607+
scope->getDecl(), genericParams, scope);
1608+
if (context->getTrailingWhereClause())
16051609
scope->createTrailingWhereClauseScope(deepestScope, scopeCreator);
16061610
scope->createBodyScope(deepestScope, scopeCreator);
16071611
return ip;

lib/AST/ASTScopeLookup.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ NullablePtr<const GenericParamList> GenericTypeScope::genericParams() const {
292292
// Sigh... These must be here so that from body, we search generics before
293293
// members. But they also must be on the Decl scope for lookups starting from
294294
// generic parameters, where clauses, etc.
295-
return getGenericContext()->getGenericParams();
295+
auto *context = getGenericContext();
296+
if (isa<TypeAliasDecl>(context))
297+
return context->getParsedGenericParams();
298+
return context->getGenericParams();
296299
}
297300
NullablePtr<const GenericParamList> ExtensionScope::genericParams() const {
298301
return decl->getGenericParams();

lib/AST/ASTWalker.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
129129
bool visitGenericParamListIfNeeded(GenericContext *GC) {
130130
// Must check this first in case extensions have not been bound yet
131131
if (Walker.shouldWalkIntoGenericParams()) {
132-
if (auto *params = GC->getGenericParams()) {
132+
if (auto *params = GC->getParsedGenericParams()) {
133133
visitGenericParamList(params);
134134
}
135135
return true;
@@ -142,11 +142,10 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
142142
for (auto &Req: Where->getRequirements())
143143
if (doIt(Req))
144144
return true;
145-
} else if (!isa<ExtensionDecl>(GC)) {
146-
if (const auto GP = GC->getGenericParams())
147-
for (auto Req: GP->getTrailingRequirements())
148-
if (doIt(Req))
149-
return true;
145+
} else if (const auto GP = GC->getParsedGenericParams()) {
146+
for (auto Req: GP->getTrailingRequirements())
147+
if (doIt(Req))
148+
return true;
150149
}
151150
return false;
152151
}

lib/AST/Decl.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,8 @@ GenericContext::GenericContext(DeclContextKind Kind, DeclContext *Parent,
992992
GenericParamList *Params)
993993
: _GenericContext(), DeclContext(Kind, Parent) {
994994
if (Params) {
995-
Parent->getASTContext().evaluator.cacheOutput(
996-
GenericParamListRequest{const_cast<GenericContext *>(this)},
997-
std::move(Params));
995+
Params->setDeclContext(this);
996+
GenericParamsAndBit.setPointerAndInt(Params, false);
998997
}
999998
}
1000999

@@ -1020,6 +1019,12 @@ GenericParamList *GenericContext::getGenericParams() const {
10201019
const_cast<GenericContext *>(this)}, nullptr);
10211020
}
10221021

1022+
GenericParamList *GenericContext::getParsedGenericParams() const {
1023+
if (GenericParamsAndBit.getInt())
1024+
return nullptr;
1025+
return GenericParamsAndBit.getPointer();
1026+
}
1027+
10231028
bool GenericContext::hasComputedGenericSignature() const {
10241029
return GenericSigAndBit.getInt();
10251030
}
@@ -1049,8 +1054,8 @@ void GenericContext::setGenericSignature(GenericSignature genericSig) {
10491054
}
10501055

10511056
SourceRange GenericContext::getGenericTrailingWhereClauseSourceRange() const {
1052-
if (isGeneric())
1053-
return getGenericParams()->getTrailingWhereClauseSourceRange();
1057+
if (auto *params = getParsedGenericParams())
1058+
return params->getTrailingWhereClauseSourceRange();
10541059
else if (const auto *where = getTrailingWhereClause())
10551060
return where->getSourceRange();
10561061

lib/AST/NameLookupRequests.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,13 @@ evaluator::DependencySource GetDestructorRequest::readDependencySource(
225225

226226
Optional<GenericParamList *> GenericParamListRequest::getCachedResult() const {
227227
auto *decl = std::get<0>(getStorage());
228-
if (!decl->GenericParamsAndBit.getInt()) {
229-
return None;
230-
}
231-
return decl->GenericParamsAndBit.getPointer();
228+
if (auto *params = decl->GenericParamsAndBit.getPointer())
229+
return params;
230+
231+
if (decl->GenericParamsAndBit.getInt())
232+
return nullptr;
233+
234+
return None;
232235
}
233236

234237
void GenericParamListRequest::cacheResult(GenericParamList *params) const {

lib/IDE/Formatting.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ class RangeWalker: protected ASTWalker {
485485
bool SafeToAskForGenerics = !isa<ExtensionDecl>(D) &&
486486
!isa<ProtocolDecl>(D);
487487
if (SafeToAskForGenerics) {
488-
if (auto *GP = GC->getGenericParams()) {
488+
if (auto *GP = GC->getParsedGenericParams()) {
489489
if (!handleAngles(GP->getLAngleLoc(), GP->getRAngleLoc(), ContextLoc))
490490
return Stop;
491491
}
@@ -1583,7 +1583,7 @@ class FormatWalker : public ASTWalker {
15831583
return Ctx;
15841584
if (auto Ctx = getIndentContextFrom(AFD->getParameters(), ContextLoc))
15851585
return Ctx;
1586-
if (auto Ctx = getIndentContextFrom(AFD->getGenericParams(), ContextLoc, D))
1586+
if (auto Ctx = getIndentContextFrom(AFD->getParsedGenericParams(), ContextLoc, D))
15871587
return Ctx;
15881588

15891589
if (TrailingTarget)
@@ -1598,7 +1598,7 @@ class FormatWalker : public ASTWalker {
15981598
return Ctx;
15991599
if (auto Ctx = getIndentContextFromBraces(NTD->getBraces(), ContextLoc, NTD))
16001600
return Ctx;
1601-
if (auto Ctx = getIndentContextFrom(NTD->getGenericParams(), ContextLoc, D))
1601+
if (auto Ctx = getIndentContextFrom(NTD->getParsedGenericParams(), ContextLoc, D))
16021602
return Ctx;
16031603
if (auto Ctx = getIndentContextFrom(NTD->getTrailingWhereClause(), ContextLoc, D))
16041604
return Ctx;
@@ -1656,7 +1656,7 @@ class FormatWalker : public ASTWalker {
16561656
return Ctx;
16571657
if (auto Ctx = getIndentContextFrom(SD->getIndices(), ContextLoc))
16581658
return Ctx;
1659-
if (auto Ctx = getIndentContextFrom(SD->getGenericParams(), ContextLoc, D))
1659+
if (auto Ctx = getIndentContextFrom(SD->getParsedGenericParams(), ContextLoc, D))
16601660
return Ctx;
16611661

16621662
if (TrailingTarget)
@@ -1752,7 +1752,7 @@ class FormatWalker : public ASTWalker {
17521752
if (auto *TAD = dyn_cast<TypeAliasDecl>(D)) {
17531753
SourceLoc ContextLoc = TAD->getStartLoc();
17541754

1755-
if (auto Ctx = getIndentContextFrom(TAD->getGenericParams(), ContextLoc,
1755+
if (auto Ctx = getIndentContextFrom(TAD->getParsedGenericParams(), ContextLoc,
17561756
D)) {
17571757
return Ctx;
17581758
}

0 commit comments

Comments
 (0)