Implement parsing for explicit instantiations of class templates, e.g.,
template class X<int>;
This also cleans up the propagation of template information through
declaration parsing, which is used to improve some diagnostics.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71608 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp
index 9d7d9d4..cdf84bf 100644
--- a/lib/Parse/ParseDecl.cpp
+++ b/lib/Parse/ParseDecl.cpp
@@ -234,14 +234,8 @@
DeclPtrTy SingleDecl;
switch (Tok.getKind()) {
case tok::kw_template:
- if (NextToken().isNot(tok::less)) {
- SingleDecl = ParseExplicitInstantiation(DeclEnd);
- break;
- }
- // Fall through for template declarations and specializations
-
case tok::kw_export:
- SingleDecl = ParseTemplateDeclarationOrSpecialization(Context, DeclEnd);
+ SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
break;
case tok::kw_namespace:
SingleDecl = ParseNamespace(Context, DeclEnd);
@@ -521,7 +515,7 @@
/// other pieces of declspec after it, it returns true.
///
bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
- TemplateParameterLists *TemplateParams,
+ const ParsedTemplateInfo &TemplateInfo,
AccessSpecifier AS) {
assert(Tok.is(tok::identifier) && "should have identifier");
@@ -576,7 +570,7 @@
if (TagKind == tok::kw_enum)
ParseEnumSpecifier(Loc, DS, AS);
else
- ParseClassSpecifier(TagKind, Loc, DS, TemplateParams, AS);
+ ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
return true;
}
}
@@ -622,7 +616,7 @@
///
void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
- TemplateParameterLists *TemplateParams,
+ const ParsedTemplateInfo &TemplateInfo,
AccessSpecifier AS) {
DS.SetRangeStart(Tok.getLocation());
while (1) {
@@ -686,7 +680,7 @@
// typename.
if (TypeRep == 0) {
ConsumeToken(); // Eat the scope spec so the identifier is current.
- if (ParseImplicitInt(DS, &SS, TemplateParams, AS)) continue;
+ if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
goto DoneWithDeclSpec;
}
@@ -748,7 +742,7 @@
// If this is not a typedef name, don't parse it as part of the declspec,
// it must be an implicit int or an error.
if (TypeRep == 0) {
- if (ParseImplicitInt(DS, 0, TemplateParams, AS)) continue;
+ if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
goto DoneWithDeclSpec;
}
@@ -934,7 +928,7 @@
case tok::kw_union: {
tok::TokenKind Kind = Tok.getKind();
ConsumeToken();
- ParseClassSpecifier(Kind, Loc, DS, TemplateParams, AS);
+ ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
continue;
}
@@ -1047,7 +1041,7 @@
/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
const char *&PrevSpec,
- TemplateParameterLists *TemplateParams){
+ const ParsedTemplateInfo &TemplateInfo) {
SourceLocation Loc = Tok.getLocation();
switch (Tok.getKind()) {
@@ -1056,7 +1050,7 @@
// Annotate typenames and C++ scope specifiers. If we get one, just
// recurse to handle whatever we get.
if (TryAnnotateTypeOrScopeToken())
- return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
+ return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
// Otherwise, not a type specifier.
return false;
case tok::coloncolon: // ::foo::bar
@@ -1067,7 +1061,7 @@
// Annotate typenames and C++ scope specifiers. If we get one, just
// recurse to handle whatever we get.
if (TryAnnotateTypeOrScopeToken())
- return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
+ return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
// Otherwise, not a type specifier.
return false;
@@ -1156,7 +1150,7 @@
case tok::kw_union: {
tok::TokenKind Kind = Tok.getKind();
ConsumeToken();
- ParseClassSpecifier(Kind, Loc, DS, TemplateParams);
+ ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
return true;
}
diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp
index 0718d3b..5650396 100644
--- a/lib/Parse/ParseDeclCXX.cpp
+++ b/lib/Parse/ParseDeclCXX.cpp
@@ -392,7 +392,7 @@
/// 'union'
void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
SourceLocation StartLoc, DeclSpec &DS,
- TemplateParameterLists *TemplateParams,
+ const ParsedTemplateInfo &TemplateInfo,
AccessSpecifier AS) {
DeclSpec::TST TagType;
if (TagTokKind == tok::kw_struct)
@@ -475,15 +475,72 @@
// Create the tag portion of the class or class template.
Action::DeclResult TagOrTempResult;
+ TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
+
+ // FIXME: When TK == TK_Reference and we have a template-id, we need
+ // to turn that template-id into a type.
+
if (TemplateId && TK != Action::TK_Reference) {
- // Explicit specialization or class template partial
- // specialization. Let semantic analysis decide.
+ // Explicit specialization, class template partial specialization,
+ // or explicit instantiation.
ASTTemplateArgsPtr TemplateArgsPtr(Actions,
TemplateId->getTemplateArgs(),
TemplateId->getTemplateArgIsType(),
TemplateId->NumArgs);
- TagOrTempResult
- = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
+ if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
+ TK == Action::TK_Declaration) {
+ // This is an explicit instantiation of a class template.
+ TagOrTempResult
+ = Actions.ActOnExplicitInstantiation(CurScope,
+ TemplateInfo.TemplateLoc,
+ TagType,
+ StartLoc,
+ SS,
+ TemplateTy::make(TemplateId->Template),
+ TemplateId->TemplateNameLoc,
+ TemplateId->LAngleLoc,
+ TemplateArgsPtr,
+ TemplateId->getTemplateArgLocations(),
+ TemplateId->RAngleLoc,
+ Attr);
+ } else {
+ // This is an explicit specialization or a class template
+ // partial specialization.
+ TemplateParameterLists FakedParamLists;
+
+ if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
+ // This looks like an explicit instantiation, because we have
+ // something like
+ //
+ // template class Foo<X>
+ //
+ // but it is actually a declaration. Most likely, this was
+ // meant to be an explicit specialization, but the user forgot
+ // the '<>' after 'template'.
+ assert(TK == Action::TK_Definition && "Can only get a definition here");
+
+ SourceLocation LAngleLoc
+ = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
+ Diag(TemplateId->TemplateNameLoc,
+ diag::err_explicit_instantiation_with_definition)
+ << SourceRange(TemplateInfo.TemplateLoc)
+ << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
+
+ // Create a fake template parameter list that contains only
+ // "template<>", so that we treat this construct as a class
+ // template specialization.
+ FakedParamLists.push_back(
+ Actions.ActOnTemplateParameterList(0, SourceLocation(),
+ TemplateInfo.TemplateLoc,
+ LAngleLoc,
+ 0, 0,
+ LAngleLoc));
+ TemplateParams = &FakedParamLists;
+ }
+
+ // Build the class template specialization.
+ TagOrTempResult
+ = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
StartLoc, SS,
TemplateTy::make(TemplateId->Template),
TemplateId->TemplateNameLoc,
@@ -495,6 +552,7 @@
Action::MultiTemplateParamsArg(Actions,
TemplateParams? &(*TemplateParams)[0] : 0,
TemplateParams? TemplateParams->size() : 0));
+ }
TemplateId->Destroy();
} else if (TemplateParams && TK != Action::TK_Reference)
TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
@@ -691,8 +749,8 @@
if (Tok.is(tok::kw_template)) {
SourceLocation DeclEnd;
- ParseTemplateDeclarationOrSpecialization(Declarator::MemberContext, DeclEnd,
- AS);
+ ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
+ AS);
return;
}
@@ -708,7 +766,7 @@
// decl-specifier-seq:
// Parse the common declaration-specifiers piece.
DeclSpec DS;
- ParseDeclarationSpecifiers(DS, 0, AS);
+ ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
if (Tok.is(tok::semi)) {
ConsumeToken();
diff --git a/lib/Parse/ParseTemplate.cpp b/lib/Parse/ParseTemplate.cpp
index 22d1177..490f276 100644
--- a/lib/Parse/ParseTemplate.cpp
+++ b/lib/Parse/ParseTemplate.cpp
@@ -18,6 +18,18 @@
#include "AstGuard.h"
using namespace clang;
+/// \brief Parse a template declaration, explicit instantiation, or
+/// explicit specialization.
+Parser::DeclPtrTy
+Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
+ SourceLocation &DeclEnd,
+ AccessSpecifier AS) {
+ if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less))
+ return ParseExplicitInstantiation(ConsumeToken(), DeclEnd);
+
+ return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
+}
+
/// \brief Parse a template declaration or an explicit specialization.
///
/// Template declarations include one or more template parameter lists
@@ -64,6 +76,7 @@
// defining A<T>::B receives just the inner template parameter list
// (and retrieves the outer template parameter list from its
// context).
+ bool isSpecialiation = true;
TemplateParameterLists ParamLists;
do {
// Consume the 'export', if any.
@@ -87,6 +100,9 @@
ParseTemplateParameters(ParamLists.size(), TemplateParams, LAngleLoc,
RAngleLoc);
+ if (!TemplateParams.empty())
+ isSpecialiation = false;
+
ParamLists.push_back(
Actions.ActOnTemplateParameterList(ParamLists.size(), ExportLoc,
TemplateLoc, LAngleLoc,
@@ -95,8 +111,9 @@
} while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
// Parse the actual template declaration.
- return ParseSingleDeclarationAfterTemplate(Context, &ParamLists,
- SourceLocation(),
+ return ParseSingleDeclarationAfterTemplate(Context,
+ ParsedTemplateInfo(&ParamLists,
+ isSpecialiation),
DeclEnd, AS);
}
@@ -123,14 +140,16 @@
Parser::DeclPtrTy
Parser::ParseSingleDeclarationAfterTemplate(
unsigned Context,
- TemplateParameterLists *TemplateParams,
- SourceLocation TemplateLoc,
+ const ParsedTemplateInfo &TemplateInfo,
SourceLocation &DeclEnd,
AccessSpecifier AS) {
+ assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
+ "Template information required");
+
// Parse the declaration specifiers.
DeclSpec DS;
// FIXME: Pass TemplateLoc through for explicit template instantiations
- ParseDeclarationSpecifiers(DS, TemplateParams, AS);
+ ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
if (Tok.is(tok::semi)) {
DeclEnd = ConsumeToken();
@@ -156,7 +175,7 @@
if (Tok.is(tok::comma)) {
Diag(Tok, diag::err_multiple_template_declarators)
- << (TemplateParams == 0);
+ << (int)TemplateInfo.Kind;
SkipUntil(tok::semi, true, false);
return ThisDecl;
}
@@ -785,11 +804,10 @@
///
/// explicit-instantiation:
/// 'template' declaration
-Parser::DeclPtrTy Parser::ParseExplicitInstantiation(SourceLocation &DeclEnd) {
- assert(Tok.is(tok::kw_template) && NextToken().isNot(tok::less) &&
- "Token does not start an explicit instantiation.");
-
- SourceLocation TemplateLoc = ConsumeToken();
- return ParseSingleDeclarationAfterTemplate(Declarator::FileContext, 0,
- TemplateLoc, DeclEnd, AS_none);
+Parser::DeclPtrTy
+Parser::ParseExplicitInstantiation(SourceLocation TemplateLoc,
+ SourceLocation &DeclEnd) {
+ return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
+ ParsedTemplateInfo(TemplateLoc),
+ DeclEnd, AS_none);
}
diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp
index d07d52e..12e5843 100644
--- a/lib/Parse/Parser.cpp
+++ b/lib/Parse/Parser.cpp
@@ -480,7 +480,7 @@
Parser::ParseDeclarationOrFunctionDefinition(AccessSpecifier AS) {
// Parse the common declaration-specifiers piece.
DeclSpec DS;
- ParseDeclarationSpecifiers(DS, 0, AS);
+ ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
// C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
// declaration-specifiers init-declarator-list[opt] ';'