================ @@ -2220,23 +2220,103 @@ namespace { class ExtractTypeForDeductionGuide : public TreeTransform<ExtractTypeForDeductionGuide> { llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs; + ClassTemplateDecl *NestedPattern; + const MultiLevelTemplateArgumentList *OuterInstantiationArgs; public: typedef TreeTransform<ExtractTypeForDeductionGuide> Base; ExtractTypeForDeductionGuide( Sema &SemaRef, - llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs) - : Base(SemaRef), MaterializedTypedefs(MaterializedTypedefs) {} + llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs, + ClassTemplateDecl *NestedPattern, + const MultiLevelTemplateArgumentList *OuterInstantiationArgs) + : Base(SemaRef), MaterializedTypedefs(MaterializedTypedefs), + NestedPattern(NestedPattern), + OuterInstantiationArgs(OuterInstantiationArgs) {} TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); } + /// Returns true if it's safe to substitute \p Typedef with + /// \p OuterInstantiationArgs. + bool mightReferToOuterTemplateParameters(TypedefNameDecl *Typedef) { + if (!NestedPattern) + return false; + + static auto WalkUp = [](DeclContext *DC, DeclContext *TargetDC) { + if (DC->Equals(TargetDC)) + return true; + while (DC->isRecord()) { + if (DC->Equals(TargetDC)) + return true; + DC = DC->getParent(); + } + return false; + }; + + if (WalkUp(Typedef->getDeclContext(), NestedPattern->getTemplatedDecl())) + return true; + if (WalkUp(NestedPattern->getTemplatedDecl(), Typedef->getDeclContext())) + return true; + return false; + } + + QualType + RebuildTemplateSpecializationType(TemplateName Template, + SourceLocation TemplateNameLoc, + TemplateArgumentListInfo &TemplateArgs) { + if (!OuterInstantiationArgs || + !isa_and_present<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) + return Base::RebuildTemplateSpecializationType(Template, TemplateNameLoc, + TemplateArgs); + + auto *TATD = cast<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); + auto *Pattern = TATD; + while (Pattern->getInstantiatedFromMemberTemplate()) + Pattern = Pattern->getInstantiatedFromMemberTemplate(); + if (!mightReferToOuterTemplateParameters(Pattern->getTemplatedDecl())) + return Base::RebuildTemplateSpecializationType(Template, TemplateNameLoc, + TemplateArgs); + + Decl *NewD = SemaRef.SubstDecl( + TATD, SemaRef.getASTContext().getTranslationUnitDecl(), ---------------- hokein wrote:
I see, thanks for the link. There is a subtle difference: - In `D80743`, we create a `Decl` by calling `TypeAliasDecl::Create(.., TU, ...)`, which only sets the member `TypeAliasDecl::DeclCtx` to `TU`. - While here, we pass the `TU` to the `SubstDecl`. This has the side effect of adding the `NewD` into the `TU` (in the substitution implementation, `TU->addDecl(NewD)` is called). Consequently, in the AST dump, you will see `NewD` under the `TranslationUnit`, which doesn't look right. I guess we can fix this by calling `TU->removeDecl(NewD)`. Thinking more about this, we should have this information in the AST. Given the example: ```cpp template <class T> struct Outer { using Alias = S<T>; template <class U> struct Inner { Inner(Alias); }; }; Outer<int>::Inner inner(S<int>()); ``` We're performing a substitution for the `Alias` with `T = int` here. At this point, the `Outer<int>` class specialization should be available. Inside this specialization, we should have the `TypeAliasDecl` `S<int>`. So an alternative would be to reuse this `Decl` rather than creating a new one. I'm not sure if this is feasible. It looks like we don't have a good way to get it from the primary `TypeAliasTemplateDecl` (no specializations in the AST node). A hacky way is to perform a lookup from `Outer<int>`. https://github.com/llvm/llvm-project/pull/94740 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits