Improved representation and support for friend class templates. Angst about same.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82088 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp
index 8d16139..5f639d8 100644
--- a/lib/AST/DeclBase.cpp
+++ b/lib/AST/DeclBase.cpp
@@ -230,6 +230,7 @@
// Never have names.
case Friend:
+ case FriendTemplate:
case LinkageSpec:
case FileScopeAsm:
case StaticAssert:
diff --git a/lib/AST/DeclTemplate.cpp b/lib/AST/DeclTemplate.cpp
index e01cbed..bd1aa7c 100644
--- a/lib/AST/DeclTemplate.cpp
+++ b/lib/AST/DeclTemplate.cpp
@@ -463,3 +463,19 @@
Context.getTypeDeclType(Result, PrevDecl);
return Result;
}
+
+//===----------------------------------------------------------------------===//
+// FriendTemplateDecl Implementation
+//===----------------------------------------------------------------------===//
+
+FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
+ DeclContext *DC,
+ SourceLocation L,
+ unsigned NParams,
+ TemplateParameterList **Params,
+ FriendUnion Friend,
+ SourceLocation FLoc) {
+ FriendTemplateDecl *Result
+ = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
+ return Result;
+}
diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp
index e1228cf..671a542 100644
--- a/lib/Parse/ParseDeclCXX.cpp
+++ b/lib/Parse/ParseDeclCXX.cpp
@@ -1011,12 +1011,15 @@
DeclSpec DS;
ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
+ Action::MultiTemplateParamsArg TemplateParams(Actions,
+ TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
+ TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
+
if (Tok.is(tok::semi)) {
ConsumeToken();
if (DS.isFriendSpecified()) {
- bool IsTemplate = TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate;
- Actions.ActOnFriendTypeDecl(CurScope, DS, IsTemplate);
+ Actions.ActOnFriendTypeDecl(CurScope, DS, move(TemplateParams));
} else
Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
@@ -1119,10 +1122,6 @@
// this call will *not* return the created decl; It will return null.
// See Sema::ActOnCXXMemberDeclarator for details.
- Action::MultiTemplateParamsArg TemplateParams(Actions,
- TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
- TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
-
DeclPtrTy ThisDecl;
if (DS.isFriendSpecified()) {
// TODO: handle initializers, bitfields, 'delete'
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index c078d43..802dab2 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -2248,7 +2248,8 @@
ExprArg AssertExpr,
ExprArg AssertMessageExpr);
- DeclPtrTy ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, bool IsTemplate);
+ DeclPtrTy ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS,
+ MultiTemplateParamsArg TemplateParams);
DeclPtrTy ActOnFriendFunctionDecl(Scope *S, Declarator &D, bool IsDefinition,
MultiTemplateParamsArg TemplateParams);
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 696c10a..9f86f12 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -3957,7 +3957,11 @@
= MatchTemplateParametersToScopeSpecifier(KWLoc, SS,
(TemplateParameterList**)TemplateParameterLists.get(),
TemplateParameterLists.size())) {
- if (TemplateParams->size() > 0) {
+ if (TUK == TUK_Friend) {
+ // When declaring a friend template, we do want to match the
+ // template parameters to the scope specifier, but don't go so far
+ // as to try to declare a new template.
+ } else if (TemplateParams->size() > 0) {
// This is a declaration or definition of a class template (which may
// be a member of another template).
OwnedDecl = false;
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index a971b8a..6135368 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -4023,41 +4023,58 @@
return DeclPtrTy::make(Decl);
}
+/// Handle a friend type declaration. This works in tandem with
+/// ActOnTag.
+///
+/// Notes on friend class templates:
+///
+/// We generally treat friend class declarations as if they were
+/// declaring a class. So, for example, the elaborated type specifier
+/// in a friend declaration is required to obey the restrictions of a
+/// class-head (i.e. no typedefs in the scope chain), template
+/// parameters are required to match up with simple template-ids, &c.
+/// However, unlike when declaring a template specialization, it's
+/// okay to refer to a template specialization without an empty
+/// template parameter declaration, e.g.
+/// friend class A<T>::B<unsigned>;
+/// We permit this as a special case; if there are any template
+/// parameters present at all, require proper matching, i.e.
+/// template <> template <class T> friend class A<int>::B;
Sema::DeclPtrTy Sema::ActOnFriendTypeDecl(Scope *S,
const DeclSpec &DS,
- bool IsTemplate) {
+ MultiTemplateParamsArg TempParams) {
SourceLocation Loc = DS.getSourceRange().getBegin();
assert(DS.isFriendSpecified());
assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified);
- // Handle friend templates specially.
- if (IsTemplate) {
- Decl *D;
- switch (DS.getTypeSpecType()) {
- default:
- // FIXME: implement this
- assert(false && "unelaborated type templates are currently unimplemented!");
- case DeclSpec::TST_class:
- case DeclSpec::TST_union:
- case DeclSpec::TST_struct:
- D = (Decl*) DS.getTypeRep();
- }
-
- ClassTemplateDecl *Temp = cast<ClassTemplateDecl>(D);
- FriendDecl *FD = FriendDecl::Create(Context, CurContext, Loc, Temp,
- DS.getFriendSpecLoc());
- FD->setAccess(AS_public);
- CurContext->addDecl(FD);
-
- return DeclPtrTy::make(FD);
- }
-
- // Try to convert the decl specifier to a type.
+ // Try to convert the decl specifier to a type. This works for
+ // friend templates because ActOnTag never produces a ClassTemplateDecl
+ // for a TUK_Friend.
bool invalid = false;
QualType T = ConvertDeclSpecToType(DS, Loc, invalid);
if (invalid) return DeclPtrTy();
+ // This is definitely an error in C++98. It's probably meant to
+ // be forbidden in C++0x, too, but the specification is just
+ // poorly written.
+ //
+ // The problem is with declarations like the following:
+ // template <T> friend A<T>::foo;
+ // where deciding whether a class C is a friend or not now hinges
+ // on whether there exists an instantiation of A that causes
+ // 'foo' to equal C. There are restrictions on class-heads
+ // (which we declare (by fiat) elaborated friend declarations to
+ // be) that makes this tractable.
+ //
+ // FIXME: handle "template <> friend class A<T>;", which
+ // is possibly well-formed? Who even knows?
+ if (TempParams.size() && !isa<ElaboratedType>(T)) {
+ Diag(Loc, diag::err_tagless_friend_type_template)
+ << DS.getSourceRange();
+ return DeclPtrTy();
+ }
+
// C++ [class.friend]p2:
// An elaborated-type-specifier shall be used in a friend declaration
// for a class.*
@@ -4085,7 +4102,6 @@
}
bool IsDefinition = false;
- FriendDecl::FriendUnion FU = T.getTypePtr();
// We want to do a few things differently if the type was declared with
// a tag: specifically, we want to use the associated RecordDecl as
@@ -4097,10 +4113,8 @@
case DeclSpec::TST_struct:
case DeclSpec::TST_union:
CXXRecordDecl *RD = cast_or_null<CXXRecordDecl>((Decl*) DS.getTypeRep());
- if (RD) {
+ if (RD)
IsDefinition |= RD->isDefinition();
- FU = RD;
- }
break;
}
@@ -4122,12 +4136,20 @@
if (RT->getDecl()->getDeclContext() == CurContext)
Diag(DS.getFriendSpecLoc(), diag::ext_friend_inner_class);
- FriendDecl *FD = FriendDecl::Create(Context, CurContext, Loc, FU,
- DS.getFriendSpecLoc());
- FD->setAccess(AS_public);
- CurContext->addDecl(FD);
+ Decl *D;
+ if (TempParams.size())
+ D = FriendTemplateDecl::Create(Context, CurContext, Loc,
+ TempParams.size(),
+ (TemplateParameterList**) TempParams.release(),
+ T.getTypePtr(),
+ DS.getFriendSpecLoc());
+ else
+ D = FriendDecl::Create(Context, CurContext, Loc, T.getTypePtr(),
+ DS.getFriendSpecLoc());
+ D->setAccess(AS_public);
+ CurContext->addDecl(D);
- return DeclPtrTy::make(FD);
+ return DeclPtrTy::make(D);
}
Sema::DeclPtrTy