erichkeane updated this revision to Diff 268524.
erichkeane added a comment.
@rsmith I think this implements what you've suggested? I'm struggling a little
with the template instantiations here, I'm not terribly sure I understand them
as well as I'd hope.
This seems to 'work' for a small subset of works, but it doesn't properly
register the typedef to the LocalInstantiationScope, so the normal template
instantiation (like here
https://github.com/llvm/llvm-project/blob/master/clang/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp#L156)
ends up hitting the 'findInstantiationOf' assert here:
https://github.com/llvm/llvm-project/blob/master/clang/lib/Sema/SemaTemplateInstantiate.cpp#L3564
I can see the parameter itself with the correct type being registered here:
https://github.com/llvm/llvm-project/blob/master/clang/lib/Sema/SemaTemplate.cpp#L2249
But I don't have a good idea on how I'm supposed to do this with the typedef.
Since it is a materialized typedef, it doesn't seem like we have the 'old' decl
to register, nor access to the LocalInstantiationScope.
Can you make a suggestion? Am I missing something simple, or did I just not do
what you suggested? If thats the case, can you rephrase your thought?
Thanks!
-Erich
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D80743/new/
https://reviews.llvm.org/D80743
Files:
clang/lib/Sema/SemaTemplate.cpp
clang/test/AST/deduction-guides.cpp
Index: clang/test/AST/deduction-guides.cpp
===================================================================
--- /dev/null
+++ clang/test/AST/deduction-guides.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -fsyntax-only %s -ast-dump -std=c++17 | FileCheck %s
+
+namespace PR46111 {
+template <typename>
+struct S;
+
+template <typename T>
+struct HasDeductionGuide {
+ typedef PR46111::S<T> STy;
+ HasDeductionGuide(typename STy::Child);
+ };
+
+// This causes deduction guides to be generated for all constructors.
+HasDeductionGuide() ->HasDeductionGuide<int>;
+
+// The parameter to this one shouldn't be an elaborated type.
+// CHECK: CXXDeductionGuideDecl {{.*}} implicit <deduction guide for HasDeductionGuide> 'auto (typename STy::Child) -> HasDeductionGuide<T>'
+// CHECK: CXXDeductionGuideDecl {{.*}} implicit <deduction guide for HasDeductionGuide> 'auto (HasDeductionGuide<T>) -> HasDeductionGuide<T>'
+// CHECK: CXXDeductionGuideDecl {{.*}} <deduction guide for HasDeductionGuide> 'auto () -> HasDeductionGuide<int>'
+} // namespace PR46111
Index: clang/lib/Sema/SemaTemplate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplate.cpp
+++ clang/lib/Sema/SemaTemplate.cpp
@@ -1947,16 +1947,35 @@
/// constructor to a deduction guide.
class ExtractTypeForDeductionGuide
: public TreeTransform<ExtractTypeForDeductionGuide> {
+ llvm::SmallVectorImpl<TypedefDecl *> &MaterializedTypedefs;
+
public:
typedef TreeTransform<ExtractTypeForDeductionGuide> Base;
- ExtractTypeForDeductionGuide(Sema &SemaRef) : Base(SemaRef) {}
+ ExtractTypeForDeductionGuide(
+ Sema &SemaRef, llvm::SmallVectorImpl<TypedefDecl *> &MaterializedTypedefs)
+ : Base(SemaRef), MaterializedTypedefs(MaterializedTypedefs) {}
TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); }
QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) {
- return TransformType(
- TLB,
- TL.getTypedefNameDecl()->getTypeSourceInfo()->getTypeLoc());
+ ASTContext &Context = SemaRef.getASTContext();
+ TypeLocBuilder InnerTLB;
+ QualType Transformed = TransformType(
+ InnerTLB, TL.getTypedefNameDecl()->getTypeSourceInfo()->getTypeLoc());
+ TypeSourceInfo *TSI = Context.getTrivialTypeSourceInfo(Transformed);
+
+ auto *Decl = TypedefDecl::Create(
+ SemaRef.getASTContext(),
+ SemaRef.getASTContext().getTranslationUnitDecl(), SourceLocation(),
+ SourceLocation(), TL.getTypedefNameDecl()->getIdentifier(), TSI);
+ MaterializedTypedefs.push_back(Decl);
+
+ QualType TDTy = Context.getTypedefType(Decl);
+ TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(TDTy);
+ TypedefTL.setNameLoc(TL.getNameLoc());
+
+ return TDTy;
+
}
};
@@ -2041,14 +2060,16 @@
// new ones.
TypeLocBuilder TLB;
SmallVector<ParmVarDecl*, 8> Params;
- QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args);
+ SmallVector<TypedefDecl*, 4> MaterializedTypedefs;
+ QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args,
+ MaterializedTypedefs);
if (NewType.isNull())
return nullptr;
TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType);
- return buildDeductionGuide(TemplateParams, CD->getExplicitSpecifier(),
- NewTInfo, CD->getBeginLoc(), CD->getLocation(),
- CD->getEndLoc());
+ return buildDeductionGuide(
+ TemplateParams, CD->getExplicitSpecifier(), NewTInfo, CD->getBeginLoc(),
+ CD->getLocation(), CD->getEndLoc(), MaterializedTypedefs);
}
/// Build a deduction guide with the specified parameter types.
@@ -2143,16 +2164,18 @@
return NewParam;
}
- QualType transformFunctionProtoType(TypeLocBuilder &TLB,
- FunctionProtoTypeLoc TL,
- SmallVectorImpl<ParmVarDecl*> &Params,
- MultiLevelTemplateArgumentList &Args) {
+ QualType transformFunctionProtoType(
+ TypeLocBuilder &TLB, FunctionProtoTypeLoc TL,
+ SmallVectorImpl<ParmVarDecl *> &Params,
+ MultiLevelTemplateArgumentList &Args,
+ SmallVectorImpl<TypedefDecl *> &MaterializedTypedefs) {
SmallVector<QualType, 4> ParamTypes;
const FunctionProtoType *T = TL.getTypePtr();
// -- The types of the function parameters are those of the constructor.
for (auto *OldParam : TL.getParams()) {
- ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args);
+ ParmVarDecl *NewParam =
+ transformFunctionTypeParam(OldParam, Args, MaterializedTypedefs);
if (!NewParam)
return QualType();
ParamTypes.push_back(NewParam->getType());
@@ -2194,9 +2217,9 @@
return Result;
}
- ParmVarDecl *
- transformFunctionTypeParam(ParmVarDecl *OldParam,
- MultiLevelTemplateArgumentList &Args) {
+ ParmVarDecl *transformFunctionTypeParam(
+ ParmVarDecl *OldParam, MultiLevelTemplateArgumentList &Args,
+ llvm::SmallVectorImpl<TypedefDecl *> &MaterializedTypedefs) {
TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo();
TypeSourceInfo *NewDI;
if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) {
@@ -2219,7 +2242,8 @@
// members of the current instantiations with the definitions of those
// typedefs, avoiding triggering instantiation of the deduced type during
// deduction.
- NewDI = ExtractTypeForDeductionGuide(SemaRef).transform(NewDI);
+ NewDI = ExtractTypeForDeductionGuide(SemaRef, MaterializedTypedefs)
+ .transform(NewDI);
// Resolving a wording defect, we also inherit default arguments from the
// constructor.
@@ -2250,10 +2274,12 @@
return NewParam;
}
- NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams,
- ExplicitSpecifier ES, TypeSourceInfo *TInfo,
- SourceLocation LocStart, SourceLocation Loc,
- SourceLocation LocEnd) {
+ FunctionTemplateDecl *
+ buildDeductionGuide(TemplateParameterList *TemplateParams,
+ ExplicitSpecifier ES, TypeSourceInfo *TInfo,
+ SourceLocation LocStart, SourceLocation Loc,
+ SourceLocation LocEnd,
+ llvm::ArrayRef<TypedefDecl *> MaterializedTypedefs = {}) {
DeclarationNameInfo Name(DeductionGuideName, Loc);
ArrayRef<ParmVarDecl *> Params =
TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams();
@@ -2267,6 +2293,8 @@
for (auto *Param : Params)
Param->setDeclContext(Guide);
+ for (auto *TD : MaterializedTypedefs)
+ TD->setDeclContext(Guide);
auto *GuideTemplate = FunctionTemplateDecl::Create(
SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide);
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits