Skip to content

Commit aeabe28

Browse files
authored
Merge pull request #30317 from bitjammer/acgarland/rdar-60091161-conditional-conformance
[SymbolGraph] Track conditional conformance
2 parents 5572956 + 7ce6753 commit aeabe28

File tree

12 files changed

+317
-115
lines changed

12 files changed

+317
-115
lines changed

lib/SymbolGraphGen/Edge.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,14 @@ void Edge::serialize(llvm::json::OStream &OS) const {
4343
Target.printPath(PathOS);
4444
OS.attribute("targetFallback", Scratch.str());
4545
}
46+
47+
if (ConformanceExtension &&
48+
!ConformanceExtension->getGenericRequirements().empty()) {
49+
OS.attributeArray("swiftConstraints", [&](){
50+
for (const auto &Req : ConformanceExtension->getGenericRequirements()) {
51+
::serialize(Req, OS);
52+
}
53+
});
54+
}
4655
});
4756
}

lib/SymbolGraphGen/Edge.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ struct Edge {
126126

127127
/// The precise identifier of the target symbol node.
128128
Symbol Target;
129+
130+
/// If this is a conformsTo relationship, the extension that defined
131+
/// the conformance.
132+
const ExtensionDecl *ConformanceExtension;
129133

130134
void serialize(llvm::json::OStream &OS) const;
131135
};
@@ -137,13 +141,15 @@ namespace llvm {
137141
using SymbolGraph = swift::symbolgraphgen::SymbolGraph;
138142
using Symbol = swift::symbolgraphgen::Symbol;
139143
using Edge = swift::symbolgraphgen::Edge;
144+
using ExtensionDecl = swift::ExtensionDecl;
140145
template <> struct DenseMapInfo<Edge> {
141146
static inline Edge getEmptyKey() {
142147
return {
143148
DenseMapInfo<SymbolGraph *>::getEmptyKey(),
144149
{ "Empty" },
145150
DenseMapInfo<Symbol>::getEmptyKey(),
146-
DenseMapInfo<Symbol>::getEmptyKey()
151+
DenseMapInfo<Symbol>::getEmptyKey(),
152+
DenseMapInfo<const ExtensionDecl *>::getEmptyKey(),
147153
};
148154
}
149155
static inline Edge getTombstoneKey() {
@@ -152,19 +158,24 @@ template <> struct DenseMapInfo<Edge> {
152158
{ "Tombstone" },
153159
DenseMapInfo<Symbol>::getTombstoneKey(),
154160
DenseMapInfo<Symbol>::getTombstoneKey(),
161+
DenseMapInfo<const ExtensionDecl *>::getTombstoneKey(),
155162
};
156163
}
157164
static unsigned getHashValue(const Edge E) {
158165
unsigned H = 0;
159166
H ^= DenseMapInfo<StringRef>::getHashValue(E.Kind.Name);
160167
H ^= DenseMapInfo<Symbol>::getHashValue(E.Source);
161168
H ^= DenseMapInfo<Symbol>::getHashValue(E.Target);
169+
H ^= DenseMapInfo<const ExtensionDecl *>::
170+
getHashValue(E.ConformanceExtension);
162171
return H;
163172
}
164173
static bool isEqual(const Edge LHS, const Edge RHS) {
165174
return LHS.Kind == RHS.Kind &&
166175
DenseMapInfo<Symbol>::isEqual(LHS.Source, RHS.Source) &&
167-
DenseMapInfo<Symbol>::isEqual(LHS.Target, RHS.Target);
176+
DenseMapInfo<Symbol>::isEqual(LHS.Target, RHS.Target) &&
177+
DenseMapInfo<const ExtensionDecl *>::isEqual(LHS.ConformanceExtension,
178+
RHS.ConformanceExtension);
168179
}
169180
};
170181
} // end namespace llvm

lib/SymbolGraphGen/JSON.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// Adds Symbol Graph JSON serialization to other types.
1313
//===----------------------------------------------------------------------===//
1414

15+
#include "swift/AST/Decl.h"
16+
#include "swift/AST/Module.h"
1517
#include "JSON.h"
1618

1719
void swift::symbolgraphgen::serialize(const llvm::VersionTuple &VT,
@@ -55,3 +57,47 @@ void swift::symbolgraphgen::serialize(const llvm::Triple &T,
5557
});
5658
});
5759
}
60+
61+
void swift::symbolgraphgen::serialize(const ExtensionDecl *Extension,
62+
llvm::json::OStream &OS) {
63+
OS.attributeObject("swiftExtension", [&](){
64+
if (const auto *ExtendedNominal = Extension->getExtendedNominal()) {
65+
if (const auto *ExtendedModule = ExtendedNominal->getModuleContext()) {
66+
OS.attribute("extendedModule", ExtendedModule->getNameStr());
67+
}
68+
}
69+
auto Generics = Extension->getGenericSignature();
70+
if (Generics && !Generics->getRequirements().empty()) {
71+
OS.attributeArray("constraints", [&](){
72+
for (const auto &Requirement : Generics->getRequirements()) {
73+
serialize(Requirement, OS);
74+
}
75+
}); // end constraints:
76+
}
77+
}); // end swiftExtension:
78+
}
79+
80+
void swift::symbolgraphgen::serialize(const Requirement &Req,
81+
llvm::json::OStream &OS) {
82+
StringRef Kind;
83+
switch (Req.getKind()) {
84+
case swift::RequirementKind::Conformance:
85+
Kind = "conformance";
86+
break;
87+
case swift::RequirementKind::Superclass:
88+
Kind = "superclass";
89+
break;
90+
case swift::RequirementKind::SameType:
91+
Kind = "sameType";
92+
break;
93+
case swift::RequirementKind::Layout:
94+
return;
95+
}
96+
97+
OS.object([&](){
98+
OS.attribute("kind", Kind);
99+
OS.attribute("lhs", Req.getFirstType()->getString());
100+
OS.attribute("rhs", Req.getSecondType()->getString());
101+
});
102+
103+
}

lib/SymbolGraphGen/JSON.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct AttributeRAII {
3838

3939
void serialize(const llvm::VersionTuple &VT, llvm::json::OStream &OS);
4040
void serialize(const llvm::Triple &T, llvm::json::OStream &OS);
41+
void serialize(const ExtensionDecl *Extension, llvm::json::OStream &OS);
42+
void serialize(const Requirement &Req, llvm::json::OStream &OS);
4143

4244
} // end namespace symbolgraphgen
4345
} // end namespace swift

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -241,30 +241,6 @@ void Symbol::serializeGenericParam(const swift::GenericTypeParamType &Param,
241241
});
242242
}
243243

244-
void Symbol::serializeGenericRequirement(const swift::Requirement &Req,
245-
llvm::json::OStream &OS) const {
246-
StringRef Kind;
247-
switch (Req.getKind()) {
248-
case swift::RequirementKind::Conformance:
249-
Kind = "conformance";
250-
break;
251-
case swift::RequirementKind::Superclass:
252-
Kind = "superclass";
253-
break;
254-
case swift::RequirementKind::SameType:
255-
Kind = "sameType";
256-
break;
257-
case swift::RequirementKind::Layout:
258-
return;
259-
}
260-
261-
OS.object([&](){
262-
OS.attribute("kind", Kind);
263-
OS.attribute("lhs", Req.getFirstType()->getString());
264-
OS.attribute("rhs", Req.getSecondType()->getString());
265-
});
266-
}
267-
268244
void Symbol::serializeSwiftGenericMixin(llvm::json::OStream &OS) const {
269245
if (const auto *GC = VD->getAsGenericContext()) {
270246
if (const auto Generics = GC->getGenericSignature()) {
@@ -286,7 +262,7 @@ void Symbol::serializeSwiftGenericMixin(llvm::json::OStream &OS) const {
286262
if (!Generics->getRequirements().empty()) {
287263
OS.attributeArray("constraints", [&](){
288264
for (const auto &Requirement : Generics->getRequirements()) {
289-
serializeGenericRequirement(Requirement, OS);
265+
::serialize(Requirement, OS);
290266
}
291267
}); // end constraints:
292268
}
@@ -299,21 +275,7 @@ void Symbol::serializeSwiftGenericMixin(llvm::json::OStream &OS) const {
299275
void Symbol::serializeSwiftExtensionMixin(llvm::json::OStream &OS) const {
300276
if (const auto *Extension
301277
= dyn_cast_or_null<ExtensionDecl>(VD->getInnermostDeclContext())) {
302-
OS.attributeObject("swiftExtension", [&](){
303-
if (const auto *ExtendedNominal = Extension->getExtendedNominal()) {
304-
if (const auto *ExtendedModule = ExtendedNominal->getModuleContext()) {
305-
OS.attribute("extendedModule", ExtendedModule->getNameStr());
306-
}
307-
}
308-
auto Generics = Extension->getGenericSignature();
309-
if (Generics && !Generics->getRequirements().empty()) {
310-
OS.attributeArray("constraints", [&](){
311-
for (const auto &Requirement : Generics->getRequirements()) {
312-
serializeGenericRequirement(Requirement, OS);
313-
}
314-
}); // end constraints:
315-
}
316-
}); // end swiftExtension:
278+
::serialize(Extension, OS);
317279
}
318280
}
319281

lib/SymbolGraphGen/Symbol.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ class Symbol {
5858
void serializeGenericParam(const swift::GenericTypeParamType &Param,
5959
llvm::json::OStream &OS) const;
6060

61-
void serializeGenericRequirement(const swift::Requirement &Req,
62-
llvm::json::OStream &OS) const;
63-
6461
void serializeSwiftGenericMixin(llvm::json::OStream &OS) const;
6562

6663
void serializeSwiftExtensionMixin(llvm::json::OStream &OS) const;

0 commit comments

Comments
 (0)