[SemaTemplate] Stop passing insertion position around during VarTemplate instantiation

They can get stale at use time because of updates from other recursive
specializations. Instead, rely on the existence of previous declarations to add
the specialization.

Differential Revision: https://reviews.llvm.org/D87853
This commit is contained in:
Bruno Cardoso Lopes 2020-10-12 15:58:52 -07:00
parent 08924b54de
commit cffb0dd54d
5 changed files with 20 additions and 16 deletions

View File

@ -9171,7 +9171,7 @@ public:
const TemplateArgumentList &TemplateArgList, const TemplateArgumentList &TemplateArgList,
const TemplateArgumentListInfo &TemplateArgsInfo, const TemplateArgumentListInfo &TemplateArgsInfo,
SmallVectorImpl<TemplateArgument> &Converted, SmallVectorImpl<TemplateArgument> &Converted,
SourceLocation PointOfInstantiation, void *InsertPos, SourceLocation PointOfInstantiation,
LateInstantiatedAttrVec *LateAttrs = nullptr, LateInstantiatedAttrVec *LateAttrs = nullptr,
LocalInstantiationScope *StartingScope = nullptr); LocalInstantiationScope *StartingScope = nullptr);
VarTemplateSpecializationDecl *CompleteVarTemplateSpecializationDecl( VarTemplateSpecializationDecl *CompleteVarTemplateSpecializationDecl(

View File

@ -600,7 +600,7 @@ enum class TemplateSubstitutionKind : char {
TagDecl *NewDecl); TagDecl *NewDecl);
Decl *VisitVarTemplateSpecializationDecl( Decl *VisitVarTemplateSpecializationDecl(
VarTemplateDecl *VarTemplate, VarDecl *FromVar, void *InsertPos, VarTemplateDecl *VarTemplate, VarDecl *FromVar,
const TemplateArgumentListInfo &TemplateArgsInfo, const TemplateArgumentListInfo &TemplateArgsInfo,
ArrayRef<TemplateArgument> Converted, ArrayRef<TemplateArgument> Converted,
VarTemplateSpecializationDecl *PrevDecl = nullptr); VarTemplateSpecializationDecl *PrevDecl = nullptr);

View File

@ -4584,7 +4584,7 @@ Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc,
// FIXME: LateAttrs et al.? // FIXME: LateAttrs et al.?
VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation(
Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, Template, InstantiationPattern, *InstantiationArgs, TemplateArgs,
Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); Converted, TemplateNameLoc /*, LateAttrs, StartingScope*/);
if (!Decl) if (!Decl)
return true; return true;

View File

@ -3628,11 +3628,11 @@ Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
return nullptr; return nullptr;
return VisitVarTemplateSpecializationDecl( return VisitVarTemplateSpecializationDecl(
InstVarTemplate, D, InsertPos, VarTemplateArgsInfo, Converted, PrevDecl); InstVarTemplate, D, VarTemplateArgsInfo, Converted, PrevDecl);
} }
Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos, VarTemplateDecl *VarTemplate, VarDecl *D,
const TemplateArgumentListInfo &TemplateArgsInfo, const TemplateArgumentListInfo &TemplateArgsInfo,
ArrayRef<TemplateArgument> Converted, ArrayRef<TemplateArgument> Converted,
VarTemplateSpecializationDecl *PrevDecl) { VarTemplateSpecializationDecl *PrevDecl) {
@ -3655,8 +3655,11 @@ Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted); VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted);
Var->setTemplateArgsInfo(TemplateArgsInfo); Var->setTemplateArgsInfo(TemplateArgsInfo);
if (InsertPos) if (!PrevDecl) {
void *InsertPos = nullptr;
VarTemplate->findSpecialization(Converted, InsertPos);
VarTemplate->AddSpecialization(Var, InsertPos); VarTemplate->AddSpecialization(Var, InsertPos);
}
if (SemaRef.getLangOpts().OpenCL) if (SemaRef.getLangOpts().OpenCL)
SemaRef.deduceOpenCLAddressSpace(Var); SemaRef.deduceOpenCLAddressSpace(Var);
@ -4865,7 +4868,7 @@ VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation(
const TemplateArgumentList &TemplateArgList, const TemplateArgumentList &TemplateArgList,
const TemplateArgumentListInfo &TemplateArgsInfo, const TemplateArgumentListInfo &TemplateArgsInfo,
SmallVectorImpl<TemplateArgument> &Converted, SmallVectorImpl<TemplateArgument> &Converted,
SourceLocation PointOfInstantiation, void *InsertPos, SourceLocation PointOfInstantiation,
LateInstantiatedAttrVec *LateAttrs, LateInstantiatedAttrVec *LateAttrs,
LocalInstantiationScope *StartingScope) { LocalInstantiationScope *StartingScope) {
if (FromVar->isInvalidDecl()) if (FromVar->isInvalidDecl())
@ -4904,7 +4907,7 @@ VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation(
return cast_or_null<VarTemplateSpecializationDecl>( return cast_or_null<VarTemplateSpecializationDecl>(
Instantiator.VisitVarTemplateSpecializationDecl( Instantiator.VisitVarTemplateSpecializationDecl(
VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted)); VarTemplate, FromVar, TemplateArgsInfo, Converted));
} }
/// Instantiates a variable template specialization by completing it /// Instantiates a variable template specialization by completing it
@ -5327,8 +5330,8 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(),
TemplateArgs); TemplateArgs);
Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl(
VarSpec->getSpecializedTemplate(), Def, nullptr, VarSpec->getSpecializedTemplate(), Def, VarSpec->getTemplateArgsInfo(),
VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray())); VarSpec->getTemplateArgs().asArray(), VarSpec));
if (Var) { if (Var) {
llvm::PointerUnion<VarTemplateDecl *, llvm::PointerUnion<VarTemplateDecl *,
VarTemplatePartialSpecializationDecl *> PatternPtr = VarTemplatePartialSpecializationDecl *> PatternPtr =
@ -5338,12 +5341,6 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf(
Partial, &VarSpec->getTemplateInstantiationArgs()); Partial, &VarSpec->getTemplateInstantiationArgs());
// Merge the definition with the declaration.
LookupResult R(*this, Var->getDeclName(), Var->getLocation(),
LookupOrdinaryName, forRedeclarationInCurContext());
R.addDecl(OldVar);
MergeVarDecl(Var, R);
// Attach the initializer. // Attach the initializer.
InstantiateVariableInitializer(Var, Def, TemplateArgs); InstantiateVariableInitializer(Var, Def, TemplateArgs);
} }

View File

@ -40,3 +40,10 @@ namespace PR24483 {
template<typename... T> A<T...> models; template<typename... T> A<T...> models;
template<> struct B models<>; // expected-error {{incomplete type 'struct B'}} expected-note {{forward declaration}} template<> struct B models<>; // expected-error {{incomplete type 'struct B'}} expected-note {{forward declaration}}
} }
namespace InvalidInsertPos {
template<typename T, int N> T v;
template<int N> decltype(v<int, N-1>) v<int, N>;
template<> int v<int, 0>;
int k = v<int, 500>;
}