Correctly handle elaborated template ids. Still not handled properly for friends.

llvm-svn: 80977
This commit is contained in:
John McCall 2009-09-04 01:14:41 +00:00
parent a3438c8bc2
commit 06f6fe8df7
8 changed files with 95 additions and 15 deletions

View File

@ -1340,6 +1340,11 @@ public:
} }
} }
/// getTagKindForTypeSpec - Converts a type specifier (DeclSpec::TST)
/// into a tag kind. It is an error to provide a type specifier
/// which *isn't* a tag kind here.
static TagKind getTagKindForTypeSpec(unsigned TypeSpec);
TagKind getTagKind() const { TagKind getTagKind() const {
return TagKind(TagDeclKind); return TagKind(TagDeclKind);
} }

View File

@ -1591,15 +1591,17 @@ public:
/// \param Template A template whose specialization results in a /// \param Template A template whose specialization results in a
/// type, e.g., a class template or template template parameter. /// type, e.g., a class template or template template parameter.
/// ///
/// \param IsSpecialization true when we are naming the class /// \param TagSpec The tag spec that was provided as part of the
/// template specialization as part of an explicit class /// elaborated-type-specifier, or TST_unspecified if this was not
/// specialization or class template partial specialization. /// an elaborated type.
virtual TypeResult ActOnTemplateIdType(TemplateTy Template, virtual TypeResult ActOnTemplateIdType(TemplateTy Template,
SourceLocation TemplateLoc, SourceLocation TemplateLoc,
SourceLocation LAngleLoc, SourceLocation LAngleLoc,
ASTTemplateArgsPtr TemplateArgs, ASTTemplateArgsPtr TemplateArgs,
SourceLocation *TemplateArgLocs, SourceLocation *TemplateArgLocs,
SourceLocation RAngleLoc) { SourceLocation RAngleLoc,
DeclSpec::TST TagSpec = DeclSpec::TST_unspecified,
SourceLocation TagLoc = SourceLocation()) {
return TypeResult(); return TypeResult();
}; };

View File

@ -22,6 +22,8 @@
#include "clang/AST/PrettyPrinter.h" #include "clang/AST/PrettyPrinter.h"
#include "clang/Basic/Builtins.h" #include "clang/Basic/Builtins.h"
#include "clang/Basic/IdentifierTable.h" #include "clang/Basic/IdentifierTable.h"
#include "clang/Parse/DeclSpec.h"
#include "llvm/Support/ErrorHandling.h"
#include <vector> #include <vector>
using namespace clang; using namespace clang;
@ -708,6 +710,16 @@ TagDecl* TagDecl::getDefinition(ASTContext& C) const {
return 0; return 0;
} }
TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
switch (TypeSpec) {
default: llvm::llvm_unreachable("unexpected type specifier");
case DeclSpec::TST_struct: return TK_struct;
case DeclSpec::TST_class: return TK_class;
case DeclSpec::TST_union: return TK_union;
case DeclSpec::TST_enum: return TK_enum;
}
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// RecordDecl Implementation // RecordDecl Implementation
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -606,7 +606,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
// to turn that template-id into a type. // to turn that template-id into a type.
bool Owned = false; bool Owned = false;
if (TemplateId && TUK != Action::TUK_Reference && TUK != Action::TUK_Friend) { if (TemplateId) {
// Explicit specialization, class template partial specialization, // Explicit specialization, class template partial specialization,
// or explicit instantiation. // or explicit instantiation.
ASTTemplateArgsPtr TemplateArgsPtr(Actions, ASTTemplateArgsPtr TemplateArgsPtr(Actions,
@ -629,6 +629,31 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
TemplateId->getTemplateArgLocations(), TemplateId->getTemplateArgLocations(),
TemplateId->RAngleLoc, TemplateId->RAngleLoc,
Attr); Attr);
} else if (TUK == Action::TUK_Reference || TUK == Action::TUK_Friend) {
Action::TypeResult TypeResult =
Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
TemplateId->TemplateNameLoc,
TemplateId->LAngleLoc,
TemplateArgsPtr,
TemplateId->getTemplateArgLocations(),
TemplateId->RAngleLoc,
TagType, StartLoc);
TemplateId->Destroy();
if (TypeResult.isInvalid()) {
DS.SetTypeSpecError();
return;
}
const char *PrevSpec = 0;
unsigned DiagID;
if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, PrevSpec,
DiagID, TypeResult.get()))
Diag(StartLoc, DiagID) << PrevSpec;
return;
} else { } else {
// This is an explicit specialization or a class template // This is an explicit specialization or a class template
// partial specialization. // partial specialization.

View File

@ -2396,7 +2396,9 @@ public:
SourceLocation LAngleLoc, SourceLocation LAngleLoc,
ASTTemplateArgsPtr TemplateArgs, ASTTemplateArgsPtr TemplateArgs,
SourceLocation *TemplateArgLocs, SourceLocation *TemplateArgLocs,
SourceLocation RAngleLoc); SourceLocation RAngleLoc,
DeclSpec::TST TagSpec,
SourceLocation TagLoc);
OwningExprResult BuildTemplateIdExpr(TemplateName Template, OwningExprResult BuildTemplateIdExpr(TemplateName Template,
SourceLocation TemplateNameLoc, SourceLocation TemplateNameLoc,

View File

@ -3966,14 +3966,7 @@ Sema::DeclPtrTy Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
"Nameless record must be a definition!"); "Nameless record must be a definition!");
OwnedDecl = false; OwnedDecl = false;
TagDecl::TagKind Kind; TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec);
switch (TagSpec) {
default: assert(0 && "Unknown tag type!");
case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break;
}
if (TUK != TUK_Reference) { if (TUK != TUK_Reference) {
if (TemplateParameterList *TemplateParams if (TemplateParameterList *TemplateParams

View File

@ -1121,7 +1121,9 @@ Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
SourceLocation LAngleLoc, SourceLocation LAngleLoc,
ASTTemplateArgsPtr TemplateArgsIn, ASTTemplateArgsPtr TemplateArgsIn,
SourceLocation *TemplateArgLocs, SourceLocation *TemplateArgLocs,
SourceLocation RAngleLoc) { SourceLocation RAngleLoc,
DeclSpec::TST TagSpec,
SourceLocation TagLoc) {
TemplateName Template = TemplateD.getAsVal<TemplateName>(); TemplateName Template = TemplateD.getAsVal<TemplateName>();
// Translate the parser's template argument list in our AST format. // Translate the parser's template argument list in our AST format.
@ -1137,6 +1139,25 @@ Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
if (Result.isNull()) if (Result.isNull())
return true; return true;
// If we were given a tag specifier, verify it.
if (TagSpec != DeclSpec::TST_unspecified) {
TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
if (const RecordType *T = Result->getAs<RecordType>()) {
RecordDecl *D = T->getDecl();
IdentifierInfo *Id = D->getIdentifier();
assert(Id && "templated class must have an identifier");
if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
Diag(TagLoc, diag::err_use_with_wrong_tag)
<< Id
<< CodeModificationHint::CreateReplacement(SourceRange(TagLoc),
D->getKindName());
}
}
}
return Result.getAsOpaquePtr(); return Result.getAsOpaquePtr();
} }
@ -2497,6 +2518,8 @@ Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
SourceLocation RAngleLoc, SourceLocation RAngleLoc,
AttributeList *Attr, AttributeList *Attr,
MultiTemplateParamsArg TemplateParameterLists) { MultiTemplateParamsArg TemplateParameterLists) {
assert(TUK == TUK_Declaration || TUK == TUK_Definition);
// Find the class template we're specializing // Find the class template we're specializing
TemplateName Name = TemplateD.getAsVal<TemplateName>(); TemplateName Name = TemplateD.getAsVal<TemplateName>();
ClassTemplateDecl *ClassTemplate ClassTemplateDecl *ClassTemplate

View File

@ -0,0 +1,18 @@
// RUN: clang-cc -fsyntax-only -verify %s
// elaborated-type-specifier:
// class-key '::'? nested-name-specifier? 'template'? simple-template-id
// Tests that this form is accepted by the compiler but does not follow
// the elaborated lookup rules of [basic.lookup.elab].
template <typename> class Ident {};
namespace A {
template <typename> void Ident();
class Ident<int> AIdent; // expected-error {{refers to a function template}}
class ::Ident<int> AnotherIdent;
}
class Ident<int> GlobalIdent;
union Ident<int> GlobalIdent; // expected-error {{ tag type that does not match }}