@@ -26,10 +26,15 @@ using namespace swift;
2626using namespace symbolgraphgen ;
2727
2828Symbol::Symbol (SymbolGraph *Graph, const ValueDecl *VD,
29- const NominalTypeDecl *SynthesizedBaseTypeDecl)
29+ const NominalTypeDecl *SynthesizedBaseTypeDecl,
30+ Type BaseType)
3031: Graph(Graph),
3132 VD(VD),
32- SynthesizedBaseTypeDecl(SynthesizedBaseTypeDecl) {}
33+ BaseType(BaseType),
34+ SynthesizedBaseTypeDecl(SynthesizedBaseTypeDecl) {
35+ if (!BaseType && SynthesizedBaseTypeDecl)
36+ BaseType = SynthesizedBaseTypeDecl->getDeclaredInterfaceType ();
37+ }
3338
3439void Symbol::serializeKind (StringRef Identifier, StringRef DisplayName,
3540 llvm::json::OStream &OS) const {
@@ -40,6 +45,9 @@ void Symbol::serializeKind(StringRef Identifier, StringRef DisplayName,
4045}
4146
4247void Symbol::serializeKind (llvm::json::OStream &OS) const {
48+ // supportsKind and serializeKind must agree.
49+ assert (Symbol::supportsKind (VD->getKind ()) && " unsupported decl kind" );
50+
4351 AttributeRAII A (" kind" , OS);
4452 switch (VD->getKind ()) {
4553 case swift::DeclKind::Class:
@@ -244,7 +252,8 @@ void Symbol::serializeFunctionSignature(llvm::json::OStream &OS) const {
244252 }
245253 Graph->serializeDeclarationFragments (" declarationFragments" ,
246254 Symbol (Graph, Param,
247- nullptr ), OS);
255+ getSynthesizedBaseTypeDecl (),
256+ getBaseType ()), OS);
248257 }); // end parameter object
249258 }
250259 }); // end parameters:
@@ -253,35 +262,62 @@ void Symbol::serializeFunctionSignature(llvm::json::OStream &OS) const {
253262
254263 // Returns
255264 if (const auto ReturnType = FD->getResultInterfaceType ()) {
256- Graph->serializeDeclarationFragments (" returns" , ReturnType, OS);
265+ Graph->serializeDeclarationFragments (" returns" , ReturnType, BaseType,
266+ OS);
257267 }
258268 });
259269 }
260270}
261271
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+
262300void Symbol::serializeSwiftGenericMixin (llvm::json::OStream &OS) const {
301+
302+ SubstitutionMap SubMap;
303+ if (BaseType)
304+ SubMap = getSubMapForDecl (VD, BaseType);
305+
263306 if (const auto *GC = VD->getAsGenericContext ()) {
264307 if (const auto Generics = GC->getGenericSignature ()) {
265308
266309 SmallVector<const GenericTypeParamType *, 4 > FilteredParams;
267310 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);
276313
277314 const auto *Self = dyn_cast<NominalTypeDecl>(VD);
278315 if (!Self) {
279316 Self = VD->getDeclContext ()->getSelfNominalTypeDecl ();
280317 }
281318
282- filterGenericRequirements (Generics->getRequirements (),
283- Self,
284- FilteredRequirements);
319+ filterGenericRequirements (Generics->getRequirements (), Self,
320+ FilteredRequirements, SubMap, FilteredParams);
285321
286322 if (FilteredParams.empty () && FilteredRequirements.empty ()) {
287323 return ;
@@ -503,3 +539,23 @@ void Symbol::getUSR(SmallVectorImpl<char> &USR) const {
503539 ide::printDeclUSR (SynthesizedBaseTypeDecl, OS);
504540 }
505541}
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