diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 9b9d164dcbc1..531c2801bf92 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -6994,19 +6994,18 @@ NamedDecl *Sema::ActOnVariableDeclarator( TemplateParams->getRAngleLoc()); TemplateParams = nullptr; } else { + // Check that we can declare a template here. + if (CheckTemplateDeclScope(S, TemplateParams)) + return nullptr; + if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { // This is an explicit specialization or a partial specialization. - // FIXME: Check that we can declare a specialization here. IsVariableTemplateSpecialization = true; IsPartialSpecialization = TemplateParams->size() > 0; } else { // if (TemplateParams->size() > 0) // This is a template declaration. IsVariableTemplate = true; - // Check that we can declare a template here. - if (CheckTemplateDeclScope(S, TemplateParams)) - return nullptr; - // Only C++1y supports variable templates (N3651). Diag(D.getIdentifierLoc(), getLangOpts().CPlusPlus14 @@ -7015,6 +7014,10 @@ NamedDecl *Sema::ActOnVariableDeclarator( } } } else { + // Check that we can declare a member specialization here. + if (!TemplateParamLists.empty() && IsMemberSpecialization && + CheckTemplateDeclScope(S, TemplateParamLists.back())) + return nullptr; assert((Invalid || D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) && "should have a 'template<>' for this decl"); @@ -8941,13 +8944,13 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, TemplateParamLists, isFriend, isMemberSpecialization, Invalid); if (TemplateParams) { + // Check that we can declare a template here. + if (CheckTemplateDeclScope(S, TemplateParams)) + NewFD->setInvalidDecl(); + if (TemplateParams->size() > 0) { // This is a function template - // Check that we can declare a template here. - if (CheckTemplateDeclScope(S, TemplateParams)) - NewFD->setInvalidDecl(); - // A destructor cannot be a template. if (Name.getNameKind() == DeclarationName::CXXDestructorName) { Diag(NewFD->getLocation(), diag::err_destructor_template); @@ -9006,6 +9009,11 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, } } } else { + // Check that we can declare a template here. + if (!TemplateParamLists.empty() && isMemberSpecialization && + CheckTemplateDeclScope(S, TemplateParamLists.back())) + NewFD->setInvalidDecl(); + // All template param lists were matched against the scope specifier: // this is NOT (an explicit specialization of) a template. if (TemplateParamLists.size() > 0) @@ -15301,6 +15309,10 @@ Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, isMemberSpecialization = true; } } + + if (!TemplateParameterLists.empty() && isMemberSpecialization && + CheckTemplateDeclScope(S, TemplateParameterLists.back())) + return nullptr; } // Figure out the underlying type if this a enum declaration. We need to do @@ -17300,7 +17312,7 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, CXXRecordDecl *CXXRecord = cast(Record); CheckForZeroSize = CXXRecord->getLexicalDeclContext()->isExternCContext() && - !CXXRecord->isDependentType() && + !CXXRecord->isDependentType() && !inTemplateInstantiation() && CXXRecord->isCLike(); } if (CheckForZeroSize) { diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 76ba12303d84..3991f2b47977 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -7771,8 +7771,9 @@ Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { (S->getFlags() & Scope::TemplateParamScope) != 0) S = S->getParent(); - // C++ [temp]p4: - // A template [...] shall not have C linkage. + // C++ [temp.pre]p6: [P2096] + // A template, explicit specialization, or partial specialization shall not + // have C linkage. DeclContext *Ctx = S->getEntity(); if (Ctx && Ctx->isExternCContext()) { Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) @@ -7786,6 +7787,12 @@ Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { // C++ [temp]p2: // A template-declaration can appear only as a namespace scope or // class scope declaration. + // C++ [temp.expl.spec]p3: + // An explicit specialization may be declared in any scope in which the + // corresponding primary template may be defined. + // C++ [temp.class.spec]p6: [P2096] + // A partial specialization may be declared in any scope in which the + // corresponding primary template may be defined. if (Ctx) { if (Ctx->isFileContext()) return false; @@ -8105,6 +8112,10 @@ DeclResult Sema::ActOnClassTemplateSpecialization( if (Invalid) return true; + // Check that we can declare a template specialization here. + if (TemplateParams && CheckTemplateDeclScope(S, TemplateParams)) + return true; + if (TemplateParams && TemplateParams->size() > 0) { isPartialSpecialization = true; diff --git a/clang/test/CXX/temp/temp.pre/p6.cpp b/clang/test/CXX/temp/temp.pre/p6.cpp new file mode 100644 index 000000000000..cb8c70ca3abe --- /dev/null +++ b/clang/test/CXX/temp/temp.pre/p6.cpp @@ -0,0 +1,79 @@ +// RUN: %clang_cc1 -std=c++20 -verify %s + +// Templates and partial and explicit specializations can't have C linkage. +namespace extern_c_templates { + +template struct A { + static int a; + struct b; + void c(); + enum class d; + + template static int e; + template struct f; + template void g(); +}; + +template int B; +template void C(); + +extern "C" { // expected-note 1+{{begins here}} + // templates + template struct A; // expected-error {{templates must have C++ linkage}} + template int B; // expected-error {{templates must have C++ linkage}} + template void C(); // expected-error {{templates must have C++ linkage}} + + // non-template members of a template + // FIXME: Should these really be valid? + template int A::a; + template struct A::b {}; + template void A::c() {} + template enum class A::d {}; + + // templates + template template int A::e; // expected-error {{templates must have C++ linkage}} + template template struct A::f {}; // expected-error {{templates must have C++ linkage}} + template template void A::g() {} // expected-error {{templates must have C++ linkage}} + + // partial specializations + template struct A; // expected-error {{templates must have C++ linkage}} + template int B; // expected-error {{templates must have C++ linkage}} + template template int A::e; // expected-error {{templates must have C++ linkage}} + template template struct A::f {}; // expected-error {{templates must have C++ linkage}} + + // explicit specializations of templates + template<> struct A {}; // expected-error {{templates must have C++ linkage}} + template<> int B; // expected-error {{templates must have C++ linkage}} + template<> void C() {} // expected-error {{templates must have C++ linkage}} + + // explicit specializations of members of a template + template<> int A::a; // expected-error {{templates must have C++ linkage}} + template<> struct A::b {}; // expected-error {{templates must have C++ linkage}} + template<> void A::c() {} // expected-error {{templates must have C++ linkage}} + template<> enum class A::d {}; // expected-error {{templates must have C++ linkage}} + + // explicit specializations of member templates + template<> template int A::e; // expected-error {{templates must have C++ linkage}} + template<> template struct A::f {}; // expected-error {{templates must have C++ linkage}} + template<> template void A::g() {} // expected-error {{templates must have C++ linkage}} +} + +// Provide valid definitions for the explicit instantiations below. +// FIXME: Our recovery from the invalid definitions above isn't very good. +template template int A::e; +template template struct A::f {}; +template template void A::g() {} + +extern "C" { + // explicit instantiations + // FIXME: Should these really be valid? + template struct A; + template int A::a; + template struct A::b; + template void A::c(); + template int A::e; + template struct A::f; + template void A::g(); +} + +} diff --git a/clang/test/SemaTemplate/class-template-decl.cpp b/clang/test/SemaTemplate/class-template-decl.cpp index dd9dcd2de9f4..453218ac3b40 100644 --- a/clang/test/SemaTemplate/class-template-decl.cpp +++ b/clang/test/SemaTemplate/class-template-decl.cpp @@ -1,17 +1,26 @@ // RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s -template class A; +template class A {}; extern "C++" { - template class B; + template class B {}; + template class A; + template<> class A; + template class A; + template class B; + template<> class B; + template class B; } namespace N { template class C; } -extern "C" { // expected-note {{extern "C" language linkage specification begins here}} +extern "C" { // expected-note 3 {{extern "C" language linkage specification begins here}} template class D; // expected-error{{templates must have C++ linkage}} + template class A; // expected-error{{templates must have C++ linkage}} + template<> class A; // expected-error{{templates must have C++ linkage}} + template class A; // OK (surprisingly) FIXME: Should we warn on this? } extern "C" { // expected-note 2 {{extern "C" language linkage specification begins here}}