@@ -26,10 +26,15 @@ using namespace swift;
26
26
using namespace symbolgraphgen ;
27
27
28
28
Symbol::Symbol (SymbolGraph *Graph, const ValueDecl *VD,
29
- const NominalTypeDecl *SynthesizedBaseTypeDecl)
29
+ const NominalTypeDecl *SynthesizedBaseTypeDecl,
30
+ Type BaseType)
30
31
: Graph(Graph),
31
32
VD(VD),
32
- SynthesizedBaseTypeDecl(SynthesizedBaseTypeDecl) {}
33
+ BaseType(BaseType),
34
+ SynthesizedBaseTypeDecl(SynthesizedBaseTypeDecl) {
35
+ if (!BaseType && SynthesizedBaseTypeDecl)
36
+ BaseType = SynthesizedBaseTypeDecl->getDeclaredInterfaceType ();
37
+ }
33
38
34
39
void Symbol::serializeKind (StringRef Identifier, StringRef DisplayName,
35
40
llvm::json::OStream &OS) const {
@@ -40,6 +45,9 @@ void Symbol::serializeKind(StringRef Identifier, StringRef DisplayName,
40
45
}
41
46
42
47
void Symbol::serializeKind (llvm::json::OStream &OS) const {
48
+ // supportsKind and serializeKind must agree.
49
+ assert (Symbol::supportsKind (VD->getKind ()) && " unsupported decl kind" );
50
+
43
51
AttributeRAII A (" kind" , OS);
44
52
switch (VD->getKind ()) {
45
53
case swift::DeclKind::Class:
@@ -244,7 +252,8 @@ void Symbol::serializeFunctionSignature(llvm::json::OStream &OS) const {
244
252
}
245
253
Graph->serializeDeclarationFragments (" declarationFragments" ,
246
254
Symbol (Graph, Param,
247
- nullptr ), OS);
255
+ getSynthesizedBaseTypeDecl (),
256
+ getBaseType ()), OS);
248
257
}); // end parameter object
249
258
}
250
259
}); // end parameters:
@@ -253,35 +262,62 @@ void Symbol::serializeFunctionSignature(llvm::json::OStream &OS) const {
253
262
254
263
// Returns
255
264
if (const auto ReturnType = FD->getResultInterfaceType ()) {
256
- Graph->serializeDeclarationFragments (" returns" , ReturnType, OS);
265
+ Graph->serializeDeclarationFragments (" returns" , ReturnType, BaseType,
266
+ OS);
257
267
}
258
268
});
259
269
}
260
270
}
261
271
272
+ static SubstitutionMap getSubMapForDecl (const ValueDecl *D, Type BaseType) {
273
+ if (!BaseType || BaseType->isExistentialType ())
274
+ return {};
275
+
276
+ // Map from the base type into the this declaration's innermost type context,
277
+ // or if we're dealing with an extention rather than a member, into its
278
+ // extended nominal (the extension's own requirements shouldn't be considered
279
+ // in the substitution).
280
+ swift::DeclContext *DC;
281
+ if (isa<swift::ExtensionDecl>(D))
282
+ DC = cast<swift::ExtensionDecl>(D)->getExtendedNominal ();
283
+ else
284
+ DC = D->getInnermostDeclContext ()->getInnermostTypeContext ();
285
+
286
+ swift::ModuleDecl *M = DC->getParentModule ();
287
+ if (isa<swift::NominalTypeDecl>(D) || isa<swift::ExtensionDecl>(D)) {
288
+ return BaseType->getContextSubstitutionMap (M, DC);
289
+ }
290
+
291
+ const swift::ValueDecl *SubTarget = D;
292
+ if (isa<swift::ParamDecl>(D)) {
293
+ auto *DC = D->getDeclContext ();
294
+ if (auto *FD = dyn_cast<swift::AbstractFunctionDecl>(DC))
295
+ SubTarget = FD;
296
+ }
297
+ return BaseType->getMemberSubstitutionMap (M, SubTarget);
298
+ }
299
+
262
300
void Symbol::serializeSwiftGenericMixin (llvm::json::OStream &OS) const {
301
+
302
+ SubstitutionMap SubMap;
303
+ if (BaseType)
304
+ SubMap = getSubMapForDecl (VD, BaseType);
305
+
263
306
if (const auto *GC = VD->getAsGenericContext ()) {
264
307
if (const auto Generics = GC->getGenericSignature ()) {
265
308
266
309
SmallVector<const GenericTypeParamType *, 4 > FilteredParams;
267
310
SmallVector<Requirement, 4 > FilteredRequirements;
268
- for (const auto Param : Generics->getGenericParams ()) {
269
- if (const auto *D = Param->getDecl ()) {
270
- if (D->isImplicit ()) {
271
- continue ;
272
- }
273
- FilteredParams.push_back (Param);
274
- }
275
- }
311
+ filterGenericParams (Generics->getGenericParams (), FilteredParams,
312
+ SubMap);
276
313
277
314
const auto *Self = dyn_cast<NominalTypeDecl>(VD);
278
315
if (!Self) {
279
316
Self = VD->getDeclContext ()->getSelfNominalTypeDecl ();
280
317
}
281
318
282
- filterGenericRequirements (Generics->getRequirements (),
283
- Self,
284
- FilteredRequirements);
319
+ filterGenericRequirements (Generics->getRequirements (), Self,
320
+ FilteredRequirements, SubMap, FilteredParams);
285
321
286
322
if (FilteredParams.empty () && FilteredRequirements.empty ()) {
287
323
return ;
@@ -503,3 +539,23 @@ void Symbol::getUSR(SmallVectorImpl<char> &USR) const {
503
539
ide::printDeclUSR (SynthesizedBaseTypeDecl, OS);
504
540
}
505
541
}
542
+
543
+ bool Symbol::supportsKind (DeclKind Kind) {
544
+ switch (Kind) {
545
+ case DeclKind::Class: LLVM_FALLTHROUGH;
546
+ case DeclKind::Struct: LLVM_FALLTHROUGH;
547
+ case DeclKind::Enum: LLVM_FALLTHROUGH;
548
+ case DeclKind::EnumElement: LLVM_FALLTHROUGH;
549
+ case DeclKind::Protocol: LLVM_FALLTHROUGH;
550
+ case DeclKind::Constructor: LLVM_FALLTHROUGH;
551
+ case DeclKind::Destructor: LLVM_FALLTHROUGH;
552
+ case DeclKind::Func: LLVM_FALLTHROUGH;
553
+ case DeclKind::Var: LLVM_FALLTHROUGH;
554
+ case DeclKind::Subscript: LLVM_FALLTHROUGH;
555
+ case DeclKind::TypeAlias: LLVM_FALLTHROUGH;
556
+ case DeclKind::AssociatedType:
557
+ return true ;
558
+ default :
559
+ return false ;
560
+ }
561
+ }
0 commit comments