2008-12-02 07:54:00 +08:00
|
|
|
//===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements parsing of C++ templates.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
|
|
|
#include "clang/Sema/ParsedTemplate.h"
|
|
|
|
#include "clang/Sema/Scope.h"
|
2009-12-10 08:45:15 +08:00
|
|
|
#include "RAIIObjectsForParser.h"
|
2011-04-23 06:18:13 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2008-12-02 07:54:00 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-05-13 07:25:50 +08:00
|
|
|
/// \brief Parse a template declaration, explicit instantiation, or
|
|
|
|
/// explicit specialization.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *
|
2009-05-13 07:25:50 +08:00
|
|
|
Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
|
|
|
|
SourceLocation &DeclEnd,
|
2011-10-13 17:41:32 +08:00
|
|
|
AccessSpecifier AS,
|
|
|
|
AttributeList *AccessAttrs) {
|
2011-08-23 01:59:19 +08:00
|
|
|
ObjCDeclContextSwitch ObjCDC(*this);
|
|
|
|
|
2011-08-22 23:54:49 +08:00
|
|
|
if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
|
2011-08-23 01:59:19 +08:00
|
|
|
return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
|
2011-08-22 23:54:49 +08:00
|
|
|
DeclEnd);
|
|
|
|
}
|
2011-10-13 17:41:32 +08:00
|
|
|
return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS,
|
|
|
|
AccessAttrs);
|
2009-05-13 07:25:50 +08:00
|
|
|
}
|
|
|
|
|
2009-08-25 07:03:25 +08:00
|
|
|
/// \brief RAII class that manages the template parameter depth.
|
|
|
|
namespace {
|
2009-11-29 03:45:26 +08:00
|
|
|
class TemplateParameterDepthCounter {
|
2009-08-25 07:03:25 +08:00
|
|
|
unsigned &Depth;
|
|
|
|
unsigned AddedLevels;
|
|
|
|
|
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
explicit TemplateParameterDepthCounter(unsigned &Depth)
|
2009-08-25 07:03:25 +08:00
|
|
|
: Depth(Depth), AddedLevels(0) { }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-25 07:03:25 +08:00
|
|
|
~TemplateParameterDepthCounter() {
|
|
|
|
Depth -= AddedLevels;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
void operator++() {
|
2009-08-25 07:03:25 +08:00
|
|
|
++Depth;
|
|
|
|
++AddedLevels;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-25 07:03:25 +08:00
|
|
|
operator unsigned() const { return Depth; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-02-18 07:15:12 +08:00
|
|
|
/// \brief Parse a template declaration or an explicit specialization.
|
|
|
|
///
|
|
|
|
/// Template declarations include one or more template parameter lists
|
|
|
|
/// and either the function or class template declaration. Explicit
|
|
|
|
/// specializations contain one or more 'template < >' prefixes
|
|
|
|
/// followed by a (possibly templated) declaration. Since the
|
|
|
|
/// syntactic form of both features is nearly identical, we parse all
|
|
|
|
/// of the template headers together and let semantic analysis sort
|
|
|
|
/// the declarations from the explicit specializations.
|
2008-12-02 07:54:00 +08:00
|
|
|
///
|
|
|
|
/// template-declaration: [C++ temp]
|
|
|
|
/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
|
2009-02-18 07:15:12 +08:00
|
|
|
///
|
|
|
|
/// explicit-specialization: [ C++ temp.expl.spec]
|
|
|
|
/// 'template' '<' '>' declaration
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *
|
2009-03-26 08:52:18 +08:00
|
|
|
Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
|
fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location. For something like this:
$ cat t2.c
#define bool int
void f(int x, int y) {
bool b = !x && y;
}
We used to produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14> <----
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
Now we produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14, line:4:17> <------
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
llvm-svn: 68288
2009-04-02 12:16:50 +08:00
|
|
|
SourceLocation &DeclEnd,
|
2011-10-13 17:41:32 +08:00
|
|
|
AccessSpecifier AS,
|
|
|
|
AttributeList *AccessAttrs) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
|
|
|
|
"Token does not start a template declaration.");
|
|
|
|
|
2008-12-24 10:52:09 +08:00
|
|
|
// Enter template-parameter scope.
|
|
|
|
ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
|
|
|
|
|
2010-07-16 16:13:16 +08:00
|
|
|
// Tell the action that names should be checked in the context of
|
|
|
|
// the declaration to come.
|
|
|
|
ParsingDeclRAIIObject ParsingTemplateParams(*this);
|
|
|
|
|
2008-12-24 10:52:09 +08:00
|
|
|
// Parse multiple levels of template headers within this template
|
|
|
|
// parameter scope, e.g.,
|
|
|
|
//
|
|
|
|
// template<typename T>
|
|
|
|
// template<typename U>
|
|
|
|
// class A<T>::B { ... };
|
|
|
|
//
|
|
|
|
// We parse multiple levels non-recursively so that we can build a
|
|
|
|
// single data structure containing all of the template parameter
|
2009-02-18 07:15:12 +08:00
|
|
|
// lists to easily differentiate between the case above and:
|
2008-12-24 10:52:09 +08:00
|
|
|
//
|
|
|
|
// template<typename T>
|
|
|
|
// class A {
|
|
|
|
// template<typename U> class B;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// In the first case, the action for declaring A<T>::B receives
|
|
|
|
// both template parameter lists. In the second case, the action for
|
|
|
|
// defining A<T>::B receives just the inner template parameter list
|
|
|
|
// (and retrieves the outer template parameter list from its
|
|
|
|
// context).
|
2009-08-21 02:46:05 +08:00
|
|
|
bool isSpecialization = true;
|
2009-10-31 06:09:44 +08:00
|
|
|
bool LastParamListWasEmpty = false;
|
2008-12-24 10:52:09 +08:00
|
|
|
TemplateParameterLists ParamLists;
|
2009-08-25 07:03:25 +08:00
|
|
|
TemplateParameterDepthCounter Depth(TemplateParameterDepth);
|
2008-12-24 10:52:09 +08:00
|
|
|
do {
|
|
|
|
// Consume the 'export', if any.
|
|
|
|
SourceLocation ExportLoc;
|
|
|
|
if (Tok.is(tok::kw_export)) {
|
|
|
|
ExportLoc = ConsumeToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the 'template', which should be here.
|
|
|
|
SourceLocation TemplateLoc;
|
|
|
|
if (Tok.is(tok::kw_template)) {
|
|
|
|
TemplateLoc = ConsumeToken();
|
|
|
|
} else {
|
2008-12-02 07:54:00 +08:00
|
|
|
Diag(Tok.getLocation(), diag::err_expected_template);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-24 10:52:09 +08:00
|
|
|
// Parse the '<' template-parameter-list '>'
|
|
|
|
SourceLocation LAngleLoc, RAngleLoc;
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Decl*, 4> TemplateParams;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
|
2009-07-23 07:48:44 +08:00
|
|
|
RAngleLoc)) {
|
|
|
|
// Skip until the semi-colon or a }.
|
|
|
|
SkipUntil(tok::r_brace, true, true);
|
|
|
|
if (Tok.is(tok::semi))
|
|
|
|
ConsumeToken();
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2009-07-23 07:48:44 +08:00
|
|
|
}
|
2009-05-13 07:25:50 +08:00
|
|
|
|
2008-12-24 10:52:09 +08:00
|
|
|
ParamLists.push_back(
|
2009-09-09 23:08:12 +08:00
|
|
|
Actions.ActOnTemplateParameterList(Depth, ExportLoc,
|
|
|
|
TemplateLoc, LAngleLoc,
|
2009-05-21 17:52:38 +08:00
|
|
|
TemplateParams.data(),
|
2008-12-24 10:52:09 +08:00
|
|
|
TemplateParams.size(), RAngleLoc));
|
2009-08-25 07:03:25 +08:00
|
|
|
|
|
|
|
if (!TemplateParams.empty()) {
|
|
|
|
isSpecialization = false;
|
|
|
|
++Depth;
|
2009-10-31 06:09:44 +08:00
|
|
|
} else {
|
|
|
|
LastParamListWasEmpty = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-12-24 10:52:09 +08:00
|
|
|
} while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
|
|
|
|
|
|
|
|
// Parse the actual template declaration.
|
2009-09-09 23:08:12 +08:00
|
|
|
return ParseSingleDeclarationAfterTemplate(Context,
|
2009-05-13 07:25:50 +08:00
|
|
|
ParsedTemplateInfo(&ParamLists,
|
2009-10-31 06:09:44 +08:00
|
|
|
isSpecialization,
|
|
|
|
LastParamListWasEmpty),
|
2010-07-16 16:13:16 +08:00
|
|
|
ParsingTemplateParams,
|
2011-10-13 17:41:32 +08:00
|
|
|
DeclEnd, AS, AccessAttrs);
|
2009-05-13 05:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Parse a single declaration that declares a template,
|
|
|
|
/// template specialization, or explicit instantiation of a template.
|
|
|
|
///
|
|
|
|
/// \param TemplateParams if non-NULL, the template parameter lists
|
|
|
|
/// that preceded this declaration. In this case, the declaration is a
|
|
|
|
/// template declaration, out-of-line definition of a template, or an
|
|
|
|
/// explicit template specialization. When NULL, the declaration is an
|
|
|
|
/// explicit template instantiation.
|
|
|
|
///
|
|
|
|
/// \param TemplateLoc when TemplateParams is NULL, the location of
|
|
|
|
/// the 'template' keyword that indicates that we have an explicit
|
|
|
|
/// template instantiation.
|
|
|
|
///
|
|
|
|
/// \param DeclEnd will receive the source location of the last token
|
|
|
|
/// within this declaration.
|
|
|
|
///
|
|
|
|
/// \param AS the access specifier associated with this
|
|
|
|
/// declaration. Will be AS_none for namespace-scope declarations.
|
|
|
|
///
|
|
|
|
/// \returns the new declaration.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *
|
2009-05-13 05:31:51 +08:00
|
|
|
Parser::ParseSingleDeclarationAfterTemplate(
|
|
|
|
unsigned Context,
|
2009-05-13 07:25:50 +08:00
|
|
|
const ParsedTemplateInfo &TemplateInfo,
|
2010-07-16 16:13:16 +08:00
|
|
|
ParsingDeclRAIIObject &DiagsFromTParams,
|
2009-05-13 05:31:51 +08:00
|
|
|
SourceLocation &DeclEnd,
|
2011-10-13 17:41:32 +08:00
|
|
|
AccessSpecifier AS,
|
|
|
|
AttributeList *AccessAttrs) {
|
2009-05-13 07:25:50 +08:00
|
|
|
assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
|
|
|
|
"Template information required");
|
|
|
|
|
2009-08-21 06:52:58 +08:00
|
|
|
if (Context == Declarator::MemberContext) {
|
|
|
|
// We are parsing a member template.
|
2011-10-13 17:41:32 +08:00
|
|
|
ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
|
|
|
|
&DiagsFromTParams);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2009-08-21 06:52:58 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-24 19:26:52 +08:00
|
|
|
ParsedAttributesWithRange prefixAttrs(AttrFactory);
|
2010-12-24 10:08:15 +08:00
|
|
|
MaybeParseCXX0XAttributes(prefixAttrs);
|
2010-11-10 10:40:36 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::kw_using))
|
|
|
|
return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
|
2010-12-24 10:08:15 +08:00
|
|
|
prefixAttrs);
|
2010-11-10 10:40:36 +08:00
|
|
|
|
2010-07-16 16:13:16 +08:00
|
|
|
// Parse the declaration specifiers, stealing the accumulated
|
|
|
|
// diagnostics from the template parameters.
|
2011-03-24 19:26:52 +08:00
|
|
|
ParsingDeclSpec DS(*this, &DiagsFromTParams);
|
2009-11-21 16:43:09 +08:00
|
|
|
|
2010-12-24 10:08:15 +08:00
|
|
|
DS.takeAttributesFrom(prefixAttrs);
|
2009-11-21 16:43:09 +08:00
|
|
|
|
2010-01-14 01:31:36 +08:00
|
|
|
ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
|
|
|
|
getDeclSpecContextFromDeclaratorContext(Context));
|
2009-05-13 05:31:51 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::semi)) {
|
|
|
|
DeclEnd = ConsumeToken();
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Decl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
|
2009-11-04 10:18:39 +08:00
|
|
|
DS.complete(Decl);
|
|
|
|
return Decl;
|
2009-05-13 05:31:51 +08:00
|
|
|
}
|
2009-03-30 00:50:03 +08:00
|
|
|
|
2009-05-13 05:31:51 +08:00
|
|
|
// Parse the declarator.
|
2009-11-04 10:18:39 +08:00
|
|
|
ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
|
2009-05-13 05:31:51 +08:00
|
|
|
ParseDeclarator(DeclaratorInfo);
|
|
|
|
// Error parsing the declarator?
|
|
|
|
if (!DeclaratorInfo.hasName()) {
|
|
|
|
// If so, skip until the semi-colon or a }.
|
|
|
|
SkipUntil(tok::r_brace, true, true);
|
|
|
|
if (Tok.is(tok::semi))
|
|
|
|
ConsumeToken();
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2009-05-13 05:31:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-13 05:31:51 +08:00
|
|
|
// If we have a declaration or declarator list, handle it.
|
|
|
|
if (isDeclarationAfterDeclarator()) {
|
|
|
|
// Parse this declaration.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
|
|
|
|
TemplateInfo);
|
2009-05-13 05:31:51 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
Diag(Tok, diag::err_multiple_template_declarators)
|
2009-05-13 07:25:50 +08:00
|
|
|
<< (int)TemplateInfo.Kind;
|
2009-05-13 05:31:51 +08:00
|
|
|
SkipUntil(tok::semi, true, false);
|
|
|
|
return ThisDecl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eat the semi colon after the declaration.
|
2009-07-31 10:20:35 +08:00
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
|
2011-02-14 15:13:47 +08:00
|
|
|
DeclaratorInfo.complete(ThisDecl);
|
2009-05-13 05:31:51 +08:00
|
|
|
return ThisDecl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DeclaratorInfo.isFunctionDeclarator() &&
|
2010-07-12 06:42:07 +08:00
|
|
|
isStartOfFunctionDefinition(DeclaratorInfo)) {
|
2009-05-13 05:31:51 +08:00
|
|
|
if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
|
|
|
|
Diag(Tok, diag::err_function_declared_typedef);
|
|
|
|
|
|
|
|
if (Tok.is(tok::l_brace)) {
|
|
|
|
// This recovery skips the entire function body. It would be nice
|
|
|
|
// to simply call ParseFunctionDefinition() below, however Sema
|
|
|
|
// assumes the declarator represents a function, not a typedef.
|
|
|
|
ConsumeBrace();
|
|
|
|
SkipUntil(tok::r_brace, true);
|
|
|
|
} else {
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
}
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2009-05-13 05:31:51 +08:00
|
|
|
}
|
2009-06-24 08:54:41 +08:00
|
|
|
return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
|
2009-05-13 05:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DeclaratorInfo.isFunctionDeclarator())
|
|
|
|
Diag(Tok, diag::err_expected_fn_body);
|
|
|
|
else
|
|
|
|
Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
|
|
|
|
SkipUntil(tok::semi);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
|
2009-02-05 03:02:06 +08:00
|
|
|
/// angle brackets. Depth is the depth of this template-parameter-list, which
|
|
|
|
/// is the number of template headers directly enclosing this template header.
|
|
|
|
/// TemplateParams is the current list of template parameters we're building.
|
|
|
|
/// The template parameter we parse will be added to this list. LAngleLoc and
|
2009-09-09 23:08:12 +08:00
|
|
|
/// RAngleLoc will receive the positions of the '<' and '>', respectively,
|
2009-02-05 03:02:06 +08:00
|
|
|
/// that enclose this template parameter list.
|
2009-07-23 07:48:44 +08:00
|
|
|
///
|
|
|
|
/// \returns true if an error occurred, false otherwise.
|
2008-12-24 10:52:09 +08:00
|
|
|
bool Parser::ParseTemplateParameters(unsigned Depth,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<Decl*> &TemplateParams,
|
2008-12-24 10:52:09 +08:00
|
|
|
SourceLocation &LAngleLoc,
|
|
|
|
SourceLocation &RAngleLoc) {
|
2008-12-02 07:54:00 +08:00
|
|
|
// Get the template parameter list.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!Tok.is(tok::less)) {
|
2008-12-02 07:54:00 +08:00
|
|
|
Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
|
2009-07-23 07:48:44 +08:00
|
|
|
return true;
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
2008-12-24 10:52:09 +08:00
|
|
|
LAngleLoc = ConsumeToken();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-02 07:54:00 +08:00
|
|
|
// Try to parse the template parameter list.
|
2008-12-24 10:52:09 +08:00
|
|
|
if (Tok.is(tok::greater))
|
|
|
|
RAngleLoc = ConsumeToken();
|
2009-09-09 23:08:12 +08:00
|
|
|
else if (ParseTemplateParameterList(Depth, TemplateParams)) {
|
|
|
|
if (!Tok.is(tok::greater)) {
|
2008-12-02 07:54:00 +08:00
|
|
|
Diag(Tok.getLocation(), diag::err_expected_greater);
|
2009-07-23 07:48:44 +08:00
|
|
|
return true;
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
2008-12-24 10:52:09 +08:00
|
|
|
RAngleLoc = ConsumeToken();
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
2009-07-23 07:48:44 +08:00
|
|
|
return false;
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseTemplateParameterList - Parse a template parameter list. If
|
|
|
|
/// the parsing fails badly (i.e., closing bracket was left out), this
|
|
|
|
/// will try to put the token stream in a reasonable position (closing
|
2009-09-09 23:08:12 +08:00
|
|
|
/// a statement, etc.) and return false.
|
2008-12-02 07:54:00 +08:00
|
|
|
///
|
|
|
|
/// template-parameter-list: [C++ temp]
|
|
|
|
/// template-parameter
|
|
|
|
/// template-parameter-list ',' template-parameter
|
2009-09-09 23:08:12 +08:00
|
|
|
bool
|
2008-12-24 10:52:09 +08:00
|
|
|
Parser::ParseTemplateParameterList(unsigned Depth,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<Decl*> &TemplateParams) {
|
2009-09-09 23:08:12 +08:00
|
|
|
while (1) {
|
2010-08-21 17:40:31 +08:00
|
|
|
if (Decl *TmpParam
|
2008-12-24 10:52:09 +08:00
|
|
|
= ParseTemplateParameter(Depth, TemplateParams.size())) {
|
|
|
|
TemplateParams.push_back(TmpParam);
|
|
|
|
} else {
|
2008-12-02 07:54:00 +08:00
|
|
|
// If we failed to parse a template parameter, skip until we find
|
|
|
|
// a comma or closing brace.
|
|
|
|
SkipUntil(tok::comma, tok::greater, true, true);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-02 07:54:00 +08:00
|
|
|
// Did we find a comma or the end of the template parmeter list?
|
2009-09-09 23:08:12 +08:00
|
|
|
if (Tok.is(tok::comma)) {
|
2008-12-02 07:54:00 +08:00
|
|
|
ConsumeToken();
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (Tok.is(tok::greater)) {
|
2008-12-02 07:54:00 +08:00
|
|
|
// Don't consume this... that's done by template parser.
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Somebody probably forgot to close the template. Skip ahead and
|
|
|
|
// try to get out of the expression. This error is currently
|
|
|
|
// subsumed by whatever goes on in ParseTemplateParameter.
|
|
|
|
// TODO: This could match >>, and it would be nice to avoid those
|
|
|
|
// silly errors with template <vec<T>>.
|
2010-10-15 09:15:58 +08:00
|
|
|
Diag(Tok.getLocation(), diag::err_expected_comma_greater);
|
2008-12-02 07:54:00 +08:00
|
|
|
SkipUntil(tok::greater, true, true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-21 10:07:55 +08:00
|
|
|
/// \brief Determine whether the parser is at the start of a template
|
|
|
|
/// type parameter.
|
|
|
|
bool Parser::isStartOfTemplateTypeParameter() {
|
2010-06-04 15:30:15 +08:00
|
|
|
if (Tok.is(tok::kw_class)) {
|
|
|
|
// "class" may be the start of an elaborated-type-specifier or a
|
|
|
|
// type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter.
|
|
|
|
switch (NextToken().getKind()) {
|
|
|
|
case tok::equal:
|
|
|
|
case tok::comma:
|
|
|
|
case tok::greater:
|
|
|
|
case tok::greatergreater:
|
|
|
|
case tok::ellipsis:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case tok::identifier:
|
|
|
|
// This may be either a type-parameter or an elaborated-type-specifier.
|
|
|
|
// We have to look further.
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (GetLookAheadToken(2).getKind()) {
|
|
|
|
case tok::equal:
|
|
|
|
case tok::comma:
|
|
|
|
case tok::greater:
|
|
|
|
case tok::greatergreater:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-11-21 10:07:55 +08:00
|
|
|
|
|
|
|
if (Tok.isNot(tok::kw_typename))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// C++ [temp.param]p2:
|
|
|
|
// There is no semantic difference between class and typename in a
|
|
|
|
// template-parameter. typename followed by an unqualified-id
|
|
|
|
// names a template type parameter. typename followed by a
|
|
|
|
// qualified-id denotes the type in a non-type
|
|
|
|
// parameter-declaration.
|
|
|
|
Token Next = NextToken();
|
|
|
|
|
|
|
|
// If we have an identifier, skip over it.
|
|
|
|
if (Next.getKind() == tok::identifier)
|
|
|
|
Next = GetLookAheadToken(2);
|
|
|
|
|
|
|
|
switch (Next.getKind()) {
|
|
|
|
case tok::equal:
|
|
|
|
case tok::comma:
|
|
|
|
case tok::greater:
|
|
|
|
case tok::greatergreater:
|
|
|
|
case tok::ellipsis:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-02 07:54:00 +08:00
|
|
|
/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
|
|
|
|
///
|
|
|
|
/// template-parameter: [C++ temp.param]
|
|
|
|
/// type-parameter
|
|
|
|
/// parameter-declaration
|
|
|
|
///
|
|
|
|
/// type-parameter: (see below)
|
2011-01-05 23:48:55 +08:00
|
|
|
/// 'class' ...[opt] identifier[opt]
|
2008-12-02 07:54:00 +08:00
|
|
|
/// 'class' identifier[opt] '=' type-id
|
2011-01-05 23:48:55 +08:00
|
|
|
/// 'typename' ...[opt] identifier[opt]
|
2008-12-02 07:54:00 +08:00
|
|
|
/// 'typename' identifier[opt] '=' type-id
|
2011-01-05 23:48:55 +08:00
|
|
|
/// 'template' '<' template-parameter-list '>'
|
|
|
|
/// 'class' ...[opt] identifier[opt]
|
|
|
|
/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
|
|
|
|
/// = id-expression
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
|
2009-11-21 10:07:55 +08:00
|
|
|
if (isStartOfTemplateTypeParameter())
|
2008-12-24 10:52:09 +08:00
|
|
|
return ParseTypeParameter(Depth, Position);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::kw_template))
|
2009-01-05 07:51:17 +08:00
|
|
|
return ParseTemplateTemplateParameter(Depth, Position);
|
|
|
|
|
|
|
|
// If it's none of the above, then it must be a parameter declaration.
|
|
|
|
// NOTE: This will pick up errors in the closure of the template parameter
|
|
|
|
// list (e.g., template < ; Check here to implement >> style closures.
|
|
|
|
return ParseNonTypeTemplateParameter(Depth, Position);
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
|
|
|
|
/// Other kinds of template parameters are parsed in
|
|
|
|
/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
|
|
|
|
///
|
|
|
|
/// type-parameter: [C++ temp.param]
|
2009-06-13 07:09:56 +08:00
|
|
|
/// 'class' ...[opt][C++0x] identifier[opt]
|
2008-12-02 07:54:00 +08:00
|
|
|
/// 'class' identifier[opt] '=' type-id
|
2009-06-13 07:09:56 +08:00
|
|
|
/// 'typename' ...[opt][C++0x] identifier[opt]
|
2008-12-02 07:54:00 +08:00
|
|
|
/// 'typename' identifier[opt] '=' type-id
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
|
2008-12-02 08:41:28 +08:00
|
|
|
assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
|
2009-09-09 23:08:12 +08:00
|
|
|
"A type-parameter starts with 'class' or 'typename'");
|
2008-12-02 08:41:28 +08:00
|
|
|
|
|
|
|
// Consume the 'class' or 'typename' keyword.
|
|
|
|
bool TypenameKeyword = Tok.is(tok::kw_typename);
|
|
|
|
SourceLocation KeyLoc = ConsumeToken();
|
2008-12-02 07:54:00 +08:00
|
|
|
|
2009-06-13 03:58:00 +08:00
|
|
|
// Grab the ellipsis (if given).
|
|
|
|
bool Ellipsis = false;
|
|
|
|
SourceLocation EllipsisLoc;
|
2009-06-13 07:09:56 +08:00
|
|
|
if (Tok.is(tok::ellipsis)) {
|
2009-06-13 03:58:00 +08:00
|
|
|
Ellipsis = true;
|
|
|
|
EllipsisLoc = ConsumeToken();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-15 04:31:37 +08:00
|
|
|
Diag(EllipsisLoc,
|
|
|
|
getLang().CPlusPlus0x
|
|
|
|
? diag::warn_cxx98_compat_variadic_templates
|
|
|
|
: diag::ext_variadic_templates);
|
2009-06-13 03:58:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-02 07:54:00 +08:00
|
|
|
// Grab the template parameter name (if given)
|
2008-12-02 08:41:28 +08:00
|
|
|
SourceLocation NameLoc;
|
|
|
|
IdentifierInfo* ParamName = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (Tok.is(tok::identifier)) {
|
2008-12-02 08:41:28 +08:00
|
|
|
ParamName = Tok.getIdentifierInfo();
|
|
|
|
NameLoc = ConsumeToken();
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
|
|
|
|
Tok.is(tok::greater)) {
|
2008-12-02 07:54:00 +08:00
|
|
|
// Unnamed template parameter. Don't have to do anything here, just
|
|
|
|
// don't consume this token.
|
|
|
|
} else {
|
|
|
|
Diag(Tok.getLocation(), diag::err_expected_ident);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-01 08:00:45 +08:00
|
|
|
// Grab a default argument (if available).
|
|
|
|
// Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
|
|
|
|
// we introduce the type parameter into the local scope.
|
|
|
|
SourceLocation EqualLoc;
|
2010-08-24 13:47:05 +08:00
|
|
|
ParsedType DefaultArg;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (Tok.is(tok::equal)) {
|
2010-07-01 08:00:45 +08:00
|
|
|
EqualLoc = ConsumeToken();
|
|
|
|
DefaultArg = ParseTypeName().get();
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
2010-07-01 08:00:45 +08:00
|
|
|
|
2010-07-03 01:43:08 +08:00
|
|
|
return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, Ellipsis,
|
2010-07-01 08:00:45 +08:00
|
|
|
EllipsisLoc, KeyLoc, ParamName, NameLoc,
|
|
|
|
Depth, Position, EqualLoc, DefaultArg);
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseTemplateTemplateParameter - Handle the parsing of template
|
2009-09-09 23:08:12 +08:00
|
|
|
/// template parameters.
|
2008-12-02 07:54:00 +08:00
|
|
|
///
|
|
|
|
/// type-parameter: [C++ temp.param]
|
2011-01-05 23:48:55 +08:00
|
|
|
/// 'template' '<' template-parameter-list '>' 'class'
|
|
|
|
/// ...[opt] identifier[opt]
|
|
|
|
/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
|
|
|
|
/// = id-expression
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *
|
2008-12-24 10:52:09 +08:00
|
|
|
Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
|
2008-12-02 07:54:00 +08:00
|
|
|
assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
|
|
|
|
|
|
|
|
// Handle the template <...> part.
|
|
|
|
SourceLocation TemplateLoc = ConsumeToken();
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Decl*,8> TemplateParams;
|
2009-02-07 06:42:48 +08:00
|
|
|
SourceLocation LAngleLoc, RAngleLoc;
|
2009-02-11 03:52:54 +08:00
|
|
|
{
|
|
|
|
ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
|
2009-07-23 07:48:44 +08:00
|
|
|
RAngleLoc)) {
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2009-02-11 03:52:54 +08:00
|
|
|
}
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a meaningful error if the user forgot to put class before the
|
|
|
|
// identifier, comma, or greater.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!Tok.is(tok::kw_class)) {
|
|
|
|
Diag(Tok.getLocation(), diag::err_expected_class_before)
|
2008-12-02 07:54:00 +08:00
|
|
|
<< PP.getSpelling(Tok);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
2011-01-18 10:00:16 +08:00
|
|
|
ConsumeToken();
|
2008-12-02 07:54:00 +08:00
|
|
|
|
2011-01-05 23:48:55 +08:00
|
|
|
// Parse the ellipsis, if given.
|
|
|
|
SourceLocation EllipsisLoc;
|
|
|
|
if (Tok.is(tok::ellipsis)) {
|
|
|
|
EllipsisLoc = ConsumeToken();
|
|
|
|
|
2011-10-15 04:31:37 +08:00
|
|
|
Diag(EllipsisLoc,
|
|
|
|
getLang().CPlusPlus0x
|
|
|
|
? diag::warn_cxx98_compat_variadic_templates
|
|
|
|
: diag::ext_variadic_templates);
|
2011-01-05 23:48:55 +08:00
|
|
|
}
|
|
|
|
|
2008-12-02 07:54:00 +08:00
|
|
|
// Get the identifier, if given.
|
2009-02-05 03:02:06 +08:00
|
|
|
SourceLocation NameLoc;
|
|
|
|
IdentifierInfo* ParamName = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (Tok.is(tok::identifier)) {
|
2009-02-05 03:02:06 +08:00
|
|
|
ParamName = Tok.getIdentifierInfo();
|
|
|
|
NameLoc = ConsumeToken();
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
|
2008-12-02 07:54:00 +08:00
|
|
|
// Unnamed template parameter. Don't have to do anything here, just
|
|
|
|
// don't consume this token.
|
|
|
|
} else {
|
|
|
|
Diag(Tok.getLocation(), diag::err_expected_ident);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
|
|
|
|
2011-09-09 11:18:59 +08:00
|
|
|
TemplateParameterList *ParamList =
|
2009-02-07 06:42:48 +08:00
|
|
|
Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
|
|
|
|
TemplateLoc, LAngleLoc,
|
2010-10-22 01:26:49 +08:00
|
|
|
TemplateParams.data(),
|
2009-02-07 06:42:48 +08:00
|
|
|
TemplateParams.size(),
|
|
|
|
RAngleLoc);
|
|
|
|
|
2010-07-01 08:00:45 +08:00
|
|
|
// Grab a default argument (if available).
|
|
|
|
// Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
|
|
|
|
// we introduce the template parameter into the local scope.
|
|
|
|
SourceLocation EqualLoc;
|
|
|
|
ParsedTemplateArgument DefaultArg;
|
2009-02-11 03:49:53 +08:00
|
|
|
if (Tok.is(tok::equal)) {
|
2010-07-01 08:00:45 +08:00
|
|
|
EqualLoc = ConsumeToken();
|
|
|
|
DefaultArg = ParseTemplateTemplateArgument();
|
|
|
|
if (DefaultArg.isInvalid()) {
|
2009-11-11 09:00:40 +08:00
|
|
|
Diag(Tok.getLocation(),
|
|
|
|
diag::err_default_template_template_parameter_not_template);
|
2009-12-10 08:07:02 +08:00
|
|
|
static const tok::TokenKind EndToks[] = {
|
2009-11-11 09:00:40 +08:00
|
|
|
tok::comma, tok::greater, tok::greatergreater
|
|
|
|
};
|
|
|
|
SkipUntil(EndToks, 3, true, true);
|
2010-07-01 08:00:45 +08:00
|
|
|
}
|
2009-02-11 03:49:53 +08:00
|
|
|
}
|
2010-07-01 08:00:45 +08:00
|
|
|
|
2010-07-03 01:43:08 +08:00
|
|
|
return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
|
2011-01-05 23:48:55 +08:00
|
|
|
ParamList, EllipsisLoc,
|
|
|
|
ParamName, NameLoc, Depth,
|
|
|
|
Position, EqualLoc, DefaultArg);
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
|
2009-09-09 23:08:12 +08:00
|
|
|
/// template parameters (e.g., in "template<int Size> class array;").
|
2008-12-19 03:37:40 +08:00
|
|
|
///
|
2008-12-02 07:54:00 +08:00
|
|
|
/// template-parameter:
|
|
|
|
/// ...
|
|
|
|
/// parameter-declaration
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *
|
2008-12-24 10:52:09 +08:00
|
|
|
Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
|
2008-12-02 07:54:00 +08:00
|
|
|
// Parse the declaration-specifiers (i.e., the type).
|
2008-12-02 08:41:28 +08:00
|
|
|
// FIXME: The type should probably be restricted in some way... Not all
|
2008-12-02 07:54:00 +08:00
|
|
|
// declarators (parts of declarators?) are accepted for parameters.
|
2011-03-24 19:26:52 +08:00
|
|
|
DeclSpec DS(AttrFactory);
|
2008-12-02 08:41:28 +08:00
|
|
|
ParseDeclarationSpecifiers(DS);
|
2008-12-02 07:54:00 +08:00
|
|
|
|
|
|
|
// Parse this as a typename.
|
2008-12-02 08:41:28 +08:00
|
|
|
Declarator ParamDecl(DS, Declarator::TemplateParamContext);
|
|
|
|
ParseDeclarator(ParamDecl);
|
2010-08-24 13:47:05 +08:00
|
|
|
if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
|
2008-12-02 07:54:00 +08:00
|
|
|
// This probably shouldn't happen - and it's more of a Sema thing, but
|
|
|
|
// basically we didn't parse the type name because we couldn't associate
|
|
|
|
// it with an AST node. we should just skip to the comma or greater.
|
|
|
|
// TODO: This is currently a placeholder for some kind of Sema Error.
|
|
|
|
Diag(Tok.getLocation(), diag::err_parse_error);
|
|
|
|
SkipUntil(tok::comma, tok::greater, true, true);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
// If there is a default value, parse it.
|
2010-07-01 08:00:45 +08:00
|
|
|
// Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
|
|
|
|
// we introduce the template parameter into the local scope.
|
|
|
|
SourceLocation EqualLoc;
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult DefaultArg;
|
2009-01-05 09:24:05 +08:00
|
|
|
if (Tok.is(tok::equal)) {
|
2010-07-01 08:00:45 +08:00
|
|
|
EqualLoc = ConsumeToken();
|
2009-02-11 03:49:53 +08:00
|
|
|
|
|
|
|
// C++ [temp.param]p15:
|
|
|
|
// When parsing a default template-argument for a non-type
|
|
|
|
// template-parameter, the first non-nested > is taken as the
|
|
|
|
// end of the template-parameter-list rather than a greater-than
|
|
|
|
// operator.
|
2009-09-09 23:08:12 +08:00
|
|
|
GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
|
2009-02-11 03:49:53 +08:00
|
|
|
|
2010-07-01 08:00:45 +08:00
|
|
|
DefaultArg = ParseAssignmentExpression();
|
2009-02-11 03:49:53 +08:00
|
|
|
if (DefaultArg.isInvalid())
|
|
|
|
SkipUntil(tok::comma, tok::greater, true, true);
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-01 08:00:45 +08:00
|
|
|
// Create the parameter.
|
2010-07-03 01:43:08 +08:00
|
|
|
return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
|
2010-07-01 08:00:45 +08:00
|
|
|
Depth, Position, EqualLoc,
|
2010-08-24 07:25:46 +08:00
|
|
|
DefaultArg.take());
|
2008-12-02 07:54:00 +08:00
|
|
|
}
|
2008-12-19 03:37:40 +08:00
|
|
|
|
2009-02-18 07:15:12 +08:00
|
|
|
/// \brief Parses a template-id that after the template name has
|
|
|
|
/// already been parsed.
|
|
|
|
///
|
|
|
|
/// This routine takes care of parsing the enclosed template argument
|
|
|
|
/// list ('<' template-parameter-list [opt] '>') and placing the
|
|
|
|
/// results into a form that can be transferred to semantic analysis.
|
|
|
|
///
|
|
|
|
/// \param Template the template declaration produced by isTemplateName
|
|
|
|
///
|
|
|
|
/// \param TemplateNameLoc the source location of the template name
|
|
|
|
///
|
|
|
|
/// \param SS if non-NULL, the nested-name-specifier preceding the
|
|
|
|
/// template name.
|
|
|
|
///
|
|
|
|
/// \param ConsumeLastToken if true, then we will consume the last
|
|
|
|
/// token that forms the template-id. Otherwise, we will leave the
|
|
|
|
/// last token in the stream (e.g., so that it can be replaced with an
|
|
|
|
/// annotation token).
|
2009-09-09 23:08:12 +08:00
|
|
|
bool
|
2009-03-31 06:58:21 +08:00
|
|
|
Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation TemplateNameLoc,
|
2011-03-02 08:47:37 +08:00
|
|
|
const CXXScopeSpec &SS,
|
2009-02-18 07:15:12 +08:00
|
|
|
bool ConsumeLastToken,
|
|
|
|
SourceLocation &LAngleLoc,
|
|
|
|
TemplateArgList &TemplateArgs,
|
|
|
|
SourceLocation &RAngleLoc) {
|
|
|
|
assert(Tok.is(tok::less) && "Must have already parsed the template-name");
|
|
|
|
|
|
|
|
// Consume the '<'.
|
|
|
|
LAngleLoc = ConsumeToken();
|
|
|
|
|
|
|
|
// Parse the optional template-argument-list.
|
|
|
|
bool Invalid = false;
|
|
|
|
{
|
|
|
|
GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
|
2011-01-11 08:45:18 +08:00
|
|
|
if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
|
2009-11-11 03:49:08 +08:00
|
|
|
Invalid = ParseTemplateArgumentList(TemplateArgs);
|
2009-02-18 07:15:12 +08:00
|
|
|
|
|
|
|
if (Invalid) {
|
|
|
|
// Try to find the closing '>'.
|
|
|
|
SkipUntil(tok::greater, true, !ConsumeLastToken);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-28 06:31:18 +08:00
|
|
|
if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) {
|
|
|
|
Diag(Tok.getLocation(), diag::err_expected_greater);
|
2009-02-18 07:15:12 +08:00
|
|
|
return true;
|
2009-12-28 06:31:18 +08:00
|
|
|
}
|
2009-02-18 07:15:12 +08:00
|
|
|
|
2009-02-26 07:02:36 +08:00
|
|
|
// Determine the location of the '>' or '>>'. Only consume this
|
|
|
|
// token if the caller asked us to.
|
2009-02-18 07:15:12 +08:00
|
|
|
RAngleLoc = Tok.getLocation();
|
|
|
|
|
2009-02-26 07:02:36 +08:00
|
|
|
if (Tok.is(tok::greatergreater)) {
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
if (!getLang().CPlusPlus0x) {
|
|
|
|
const char *ReplaceStr = "> >";
|
|
|
|
if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
|
|
|
|
ReplaceStr = "> > ";
|
|
|
|
|
|
|
|
Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
|
2010-04-01 01:46:05 +08:00
|
|
|
<< FixItHint::CreateReplacement(
|
2009-02-28 01:53:17 +08:00
|
|
|
SourceRange(Tok.getLocation()), ReplaceStr);
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
}
|
2009-02-26 07:02:36 +08:00
|
|
|
|
|
|
|
Tok.setKind(tok::greater);
|
|
|
|
if (!ConsumeLastToken) {
|
|
|
|
// Since we're not supposed to consume the '>>' token, we need
|
|
|
|
// to insert a second '>' token after the first.
|
|
|
|
PP.EnterToken(Tok);
|
|
|
|
}
|
|
|
|
} else if (ConsumeLastToken)
|
2009-02-18 07:15:12 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
/// \brief Replace the tokens that form a simple-template-id with an
|
|
|
|
/// annotation token containing the complete template-id.
|
|
|
|
///
|
|
|
|
/// The first token in the stream must be the name of a template that
|
|
|
|
/// is followed by a '<'. This routine will parse the complete
|
|
|
|
/// simple-template-id and replace the tokens with a single annotation
|
|
|
|
/// token with one of two different kinds: if the template-id names a
|
|
|
|
/// type (and \p AllowTypeAnnotation is true), the annotation token is
|
|
|
|
/// a type annotation that includes the optional nested-name-specifier
|
|
|
|
/// (\p SS). Otherwise, the annotation token is a template-id
|
|
|
|
/// annotation that does not include the optional
|
|
|
|
/// nested-name-specifier.
|
|
|
|
///
|
|
|
|
/// \param Template the declaration of the template named by the first
|
|
|
|
/// token (an identifier), as returned from \c Action::isTemplateName().
|
|
|
|
///
|
|
|
|
/// \param TemplateNameKind the kind of template that \p Template
|
|
|
|
/// refers to, as returned from \c Action::isTemplateName().
|
|
|
|
///
|
|
|
|
/// \param SS if non-NULL, the nested-name-specifier that precedes
|
|
|
|
/// this template name.
|
|
|
|
///
|
|
|
|
/// \param TemplateKWLoc if valid, specifies that this template-id
|
|
|
|
/// annotation was preceded by the 'template' keyword and gives the
|
|
|
|
/// location of that keyword. If invalid (the default), then this
|
|
|
|
/// template-id was not preceded by a 'template' keyword.
|
|
|
|
///
|
|
|
|
/// \param AllowTypeAnnotation if true (the default), then a
|
|
|
|
/// simple-template-id that refers to a class template, template
|
|
|
|
/// template parameter, or other template that produces a type will be
|
|
|
|
/// replaced with a type annotation token. Otherwise, the
|
|
|
|
/// simple-template-id is always replaced with a template-id
|
|
|
|
/// annotation token.
|
2009-06-26 12:27:47 +08:00
|
|
|
///
|
|
|
|
/// If an unrecoverable parse error occurs and no annotation token can be
|
|
|
|
/// formed, this function returns true.
|
|
|
|
///
|
|
|
|
bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
|
2011-03-02 08:47:37 +08:00
|
|
|
CXXScopeSpec &SS,
|
2009-11-04 08:56:37 +08:00
|
|
|
UnqualifiedId &TemplateName,
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
SourceLocation TemplateKWLoc,
|
|
|
|
bool AllowTypeAnnotation) {
|
2008-12-19 03:37:40 +08:00
|
|
|
assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
|
2009-11-04 08:56:37 +08:00
|
|
|
assert(Template && Tok.is(tok::less) &&
|
2008-12-19 03:37:40 +08:00
|
|
|
"Parser isn't at the beginning of a template-id");
|
|
|
|
|
|
|
|
// Consume the template-name.
|
2009-11-04 08:56:37 +08:00
|
|
|
SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
|
2008-12-19 03:37:40 +08:00
|
|
|
|
2009-02-18 07:15:12 +08:00
|
|
|
// Parse the enclosed template argument list.
|
|
|
|
SourceLocation LAngleLoc, RAngleLoc;
|
2009-02-10 03:34:22 +08:00
|
|
|
TemplateArgList TemplateArgs;
|
2009-11-04 08:56:37 +08:00
|
|
|
bool Invalid = ParseTemplateIdAfterTemplateName(Template,
|
|
|
|
TemplateNameLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
SS, false, LAngleLoc,
|
|
|
|
TemplateArgs,
|
2009-02-18 07:15:12 +08:00
|
|
|
RAngleLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-26 12:27:47 +08:00
|
|
|
if (Invalid) {
|
|
|
|
// If we failed to parse the template ID but skipped ahead to a >, we're not
|
|
|
|
// going to be able to form a token annotation. Eat the '>' if present.
|
|
|
|
if (Tok.is(tok::greater))
|
|
|
|
ConsumeToken();
|
|
|
|
return true;
|
|
|
|
}
|
2009-02-10 07:23:08 +08:00
|
|
|
|
2009-05-21 17:52:38 +08:00
|
|
|
ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
|
2009-02-18 07:15:12 +08:00
|
|
|
TemplateArgs.size());
|
2008-12-19 03:37:40 +08:00
|
|
|
|
2009-02-10 02:46:07 +08:00
|
|
|
// Build the annotation token.
|
2009-03-31 08:43:58 +08:00
|
|
|
if (TNK == TNK_Type_template && AllowTypeAnnotation) {
|
2010-08-27 07:41:50 +08:00
|
|
|
TypeResult Type
|
2011-03-02 08:47:37 +08:00
|
|
|
= Actions.ActOnTemplateIdType(SS,
|
|
|
|
Template, TemplateNameLoc,
|
2009-03-31 06:58:21 +08:00
|
|
|
LAngleLoc, TemplateArgsPtr,
|
|
|
|
RAngleLoc);
|
2009-06-26 12:27:47 +08:00
|
|
|
if (Type.isInvalid()) {
|
|
|
|
// If we failed to parse the template ID but skipped ahead to a >, we're not
|
|
|
|
// going to be able to form a token annotation. Eat the '>' if present.
|
|
|
|
if (Tok.is(tok::greater))
|
|
|
|
ConsumeToken();
|
|
|
|
return true;
|
|
|
|
}
|
2009-02-18 07:15:12 +08:00
|
|
|
|
|
|
|
Tok.setKind(tok::annot_typename);
|
2010-08-24 13:47:05 +08:00
|
|
|
setTypeAnnotation(Tok, Type.get());
|
2011-03-02 08:47:37 +08:00
|
|
|
if (SS.isNotEmpty())
|
|
|
|
Tok.setLocation(SS.getBeginLoc());
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
else if (TemplateKWLoc.isValid())
|
|
|
|
Tok.setLocation(TemplateKWLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
Tok.setLocation(TemplateNameLoc);
|
2009-02-18 07:15:12 +08:00
|
|
|
} else {
|
2009-03-31 08:43:58 +08:00
|
|
|
// Build a template-id annotation token that can be processed
|
|
|
|
// later.
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
Tok.setKind(tok::annot_template_id);
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateIdAnnotation *TemplateId
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
= TemplateIdAnnotation::Allocate(TemplateArgs.size());
|
2009-02-10 02:46:07 +08:00
|
|
|
TemplateId->TemplateNameLoc = TemplateNameLoc;
|
2009-11-04 08:56:37 +08:00
|
|
|
if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
|
|
|
|
TemplateId->Name = TemplateName.Identifier;
|
|
|
|
TemplateId->Operator = OO_None;
|
|
|
|
} else {
|
|
|
|
TemplateId->Name = 0;
|
|
|
|
TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
|
|
|
|
}
|
2011-03-02 08:47:37 +08:00
|
|
|
TemplateId->SS = SS;
|
2010-08-23 15:28:44 +08:00
|
|
|
TemplateId->Template = Template;
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
TemplateId->Kind = TNK;
|
2009-02-10 02:46:07 +08:00
|
|
|
TemplateId->LAngleLoc = LAngleLoc;
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
TemplateId->RAngleLoc = RAngleLoc;
|
2009-11-11 03:49:08 +08:00
|
|
|
ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
|
|
|
|
for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
|
2011-02-25 01:54:50 +08:00
|
|
|
Args[Arg] = ParsedTemplateArgument(TemplateArgs[Arg]);
|
2009-02-10 02:46:07 +08:00
|
|
|
Tok.setAnnotationValue(TemplateId);
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
if (TemplateKWLoc.isValid())
|
|
|
|
Tok.setLocation(TemplateKWLoc);
|
|
|
|
else
|
|
|
|
Tok.setLocation(TemplateNameLoc);
|
|
|
|
|
|
|
|
TemplateArgsPtr.release();
|
2009-02-10 02:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common fields for the annotation token
|
2008-12-19 03:37:40 +08:00
|
|
|
Tok.setAnnotationEndLoc(RAngleLoc);
|
|
|
|
|
|
|
|
// In case the tokens were cached, have Preprocessor replace them with the
|
|
|
|
// annotation token.
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
2009-06-26 12:27:47 +08:00
|
|
|
return false;
|
2008-12-19 03:37:40 +08:00
|
|
|
}
|
|
|
|
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
/// \brief Replaces a template-id annotation token with a type
|
|
|
|
/// annotation token.
|
|
|
|
///
|
2009-04-02 05:51:26 +08:00
|
|
|
/// If there was a failure when forming the type from the template-id,
|
|
|
|
/// a type annotation token will still be created, but will have a
|
|
|
|
/// NULL type pointer to signify an error.
|
2011-03-02 08:47:37 +08:00
|
|
|
void Parser::AnnotateTemplateIdTokenAsType() {
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
|
|
|
|
|
2011-06-22 14:09:49 +08:00
|
|
|
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
|
2009-03-31 08:43:58 +08:00
|
|
|
assert((TemplateId->Kind == TNK_Type_template ||
|
|
|
|
TemplateId->Kind == TNK_Dependent_template_name) &&
|
|
|
|
"Only works for type and dependent templates");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ASTTemplateArgsPtr TemplateArgsPtr(Actions,
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
TemplateId->getTemplateArgs(),
|
|
|
|
TemplateId->NumArgs);
|
|
|
|
|
2010-08-27 07:41:50 +08:00
|
|
|
TypeResult Type
|
2011-03-02 08:47:37 +08:00
|
|
|
= Actions.ActOnTemplateIdType(TemplateId->SS,
|
|
|
|
TemplateId->Template,
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateId->TemplateNameLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateId->LAngleLoc,
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateArgsPtr,
|
|
|
|
TemplateId->RAngleLoc);
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
// Create the new "type" annotation token.
|
|
|
|
Tok.setKind(tok::annot_typename);
|
2010-08-24 13:47:05 +08:00
|
|
|
setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get());
|
2011-03-02 08:47:37 +08:00
|
|
|
if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name.
|
|
|
|
Tok.setLocation(TemplateId->SS.getBeginLoc());
|
2010-02-09 03:35:18 +08:00
|
|
|
// End location stays the same
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
|
2009-11-05 02:18:19 +08:00
|
|
|
// Replace the template-id annotation token, and possible the scope-specifier
|
|
|
|
// that precedes it, with the typename annotation token.
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 03:49:08 +08:00
|
|
|
/// \brief Determine whether the given token can end a template argument.
|
2009-11-11 05:29:56 +08:00
|
|
|
static bool isEndOfTemplateArgument(Token Tok) {
|
2009-11-11 03:49:08 +08:00
|
|
|
return Tok.is(tok::comma) || Tok.is(tok::greater) ||
|
|
|
|
Tok.is(tok::greatergreater);
|
|
|
|
}
|
|
|
|
|
2009-11-11 09:00:40 +08:00
|
|
|
/// \brief Parse a C++ template template argument.
|
|
|
|
ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
|
|
|
|
if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
|
|
|
|
!Tok.is(tok::annot_cxxscope))
|
|
|
|
return ParsedTemplateArgument();
|
2009-02-10 02:46:07 +08:00
|
|
|
|
2009-11-11 03:49:08 +08:00
|
|
|
// C++0x [temp.arg.template]p1:
|
|
|
|
// A template-argument for a template template-parameter shall be the name
|
2011-05-06 05:57:07 +08:00
|
|
|
// of a class template or an alias template, expressed as id-expression.
|
2009-11-11 09:00:40 +08:00
|
|
|
//
|
2011-05-06 05:57:07 +08:00
|
|
|
// We parse an id-expression that refers to a class template or alias
|
|
|
|
// template. The grammar we parse is:
|
2009-11-11 03:49:08 +08:00
|
|
|
//
|
2011-01-06 01:33:50 +08:00
|
|
|
// nested-name-specifier[opt] template[opt] identifier ...[opt]
|
2009-11-11 03:49:08 +08:00
|
|
|
//
|
|
|
|
// followed by a token that terminates a template argument, such as ',',
|
|
|
|
// '>', or (in some cases) '>>'.
|
2009-11-11 09:00:40 +08:00
|
|
|
CXXScopeSpec SS; // nested-name-specifier, if present
|
2010-08-24 13:47:05 +08:00
|
|
|
ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
|
2009-11-11 09:00:40 +08:00
|
|
|
/*EnteringContext=*/false);
|
|
|
|
|
2011-01-06 01:33:50 +08:00
|
|
|
ParsedTemplateArgument Result;
|
|
|
|
SourceLocation EllipsisLoc;
|
2009-11-11 09:00:40 +08:00
|
|
|
if (SS.isSet() && Tok.is(tok::kw_template)) {
|
|
|
|
// Parse the optional 'template' keyword following the
|
|
|
|
// nested-name-specifier.
|
|
|
|
SourceLocation TemplateLoc = ConsumeToken();
|
|
|
|
|
|
|
|
if (Tok.is(tok::identifier)) {
|
|
|
|
// We appear to have a dependent template name.
|
2009-11-11 03:49:08 +08:00
|
|
|
UnqualifiedId Name;
|
|
|
|
Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
|
|
|
|
ConsumeToken(); // the identifier
|
2009-11-11 09:00:40 +08:00
|
|
|
|
2011-01-06 01:33:50 +08:00
|
|
|
// Parse the ellipsis.
|
|
|
|
if (Tok.is(tok::ellipsis))
|
|
|
|
EllipsisLoc = ConsumeToken();
|
|
|
|
|
2009-11-11 09:00:40 +08:00
|
|
|
// If the next token signals the end of a template argument,
|
|
|
|
// then we have a dependent template name that could be a template
|
|
|
|
// template argument.
|
2010-06-17 07:00:59 +08:00
|
|
|
TemplateTy Template;
|
|
|
|
if (isEndOfTemplateArgument(Tok) &&
|
2010-08-24 13:47:05 +08:00
|
|
|
Actions.ActOnDependentTemplateName(getCurScope(), TemplateLoc,
|
|
|
|
SS, Name,
|
|
|
|
/*ObjectType=*/ ParsedType(),
|
2010-06-17 07:00:59 +08:00
|
|
|
/*EnteringContext=*/false,
|
|
|
|
Template))
|
2011-01-06 01:33:50 +08:00
|
|
|
Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
|
2010-06-17 07:00:59 +08:00
|
|
|
}
|
2009-11-11 09:00:40 +08:00
|
|
|
} else if (Tok.is(tok::identifier)) {
|
|
|
|
// We may have a (non-dependent) template name.
|
|
|
|
TemplateTy Template;
|
|
|
|
UnqualifiedId Name;
|
|
|
|
Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
|
|
|
|
ConsumeToken(); // the identifier
|
|
|
|
|
2011-01-06 01:33:50 +08:00
|
|
|
// Parse the ellipsis.
|
|
|
|
if (Tok.is(tok::ellipsis))
|
|
|
|
EllipsisLoc = ConsumeToken();
|
|
|
|
|
2009-11-11 09:00:40 +08:00
|
|
|
if (isEndOfTemplateArgument(Tok)) {
|
2010-05-22 07:18:07 +08:00
|
|
|
bool MemberOfUnknownSpecialization;
|
2010-08-06 20:11:11 +08:00
|
|
|
TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
|
|
|
|
/*hasTemplateKeyword=*/false,
|
|
|
|
Name,
|
2010-08-24 13:47:05 +08:00
|
|
|
/*ObjectType=*/ ParsedType(),
|
2009-11-11 09:00:40 +08:00
|
|
|
/*EnteringContext=*/false,
|
2010-05-22 07:18:07 +08:00
|
|
|
Template,
|
|
|
|
MemberOfUnknownSpecialization);
|
2009-11-11 09:00:40 +08:00
|
|
|
if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
|
|
|
|
// We have an id-expression that refers to a class template or
|
2011-05-06 05:57:07 +08:00
|
|
|
// (C++0x) alias template.
|
2011-01-06 01:33:50 +08:00
|
|
|
Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
|
2009-11-11 09:00:40 +08:00
|
|
|
}
|
2009-11-11 03:49:08 +08:00
|
|
|
}
|
2009-11-11 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
2011-01-06 01:33:50 +08:00
|
|
|
// If this is a pack expansion, build it as such.
|
|
|
|
if (EllipsisLoc.isValid() && !Result.isInvalid())
|
|
|
|
Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
|
|
|
|
|
|
|
|
return Result;
|
2009-11-11 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
|
|
|
|
///
|
|
|
|
/// template-argument: [C++ 14.2]
|
|
|
|
/// constant-expression
|
|
|
|
/// type-id
|
|
|
|
/// id-expression
|
|
|
|
ParsedTemplateArgument Parser::ParseTemplateArgument() {
|
|
|
|
// C++ [temp.arg]p2:
|
|
|
|
// In a template-argument, an ambiguity between a type-id and an
|
|
|
|
// expression is resolved to a type-id, regardless of the form of
|
|
|
|
// the corresponding template-parameter.
|
|
|
|
//
|
|
|
|
// Therefore, we initially try to parse a type-id.
|
|
|
|
if (isCXXTypeId(TypeIdAsTemplateArgument)) {
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
2011-02-01 00:09:46 +08:00
|
|
|
TypeResult TypeArg = ParseTypeName(/*Range=*/0,
|
|
|
|
Declarator::TemplateTypeArgContext);
|
2009-11-11 09:00:40 +08:00
|
|
|
if (TypeArg.isInvalid())
|
|
|
|
return ParsedTemplateArgument();
|
2009-11-11 03:49:08 +08:00
|
|
|
|
2010-08-24 13:47:05 +08:00
|
|
|
return ParsedTemplateArgument(ParsedTemplateArgument::Type,
|
|
|
|
TypeArg.get().getAsOpaquePtr(),
|
2009-11-11 09:00:40 +08:00
|
|
|
Loc);
|
2009-11-11 03:49:08 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 09:00:40 +08:00
|
|
|
// Try to parse a template template argument.
|
2009-11-12 08:03:40 +08:00
|
|
|
{
|
|
|
|
TentativeParsingAction TPA(*this);
|
|
|
|
|
|
|
|
ParsedTemplateArgument TemplateTemplateArgument
|
|
|
|
= ParseTemplateTemplateArgument();
|
|
|
|
if (!TemplateTemplateArgument.isInvalid()) {
|
|
|
|
TPA.Commit();
|
|
|
|
return TemplateTemplateArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revert this tentative parse to parse a non-type template argument.
|
|
|
|
TPA.Revert();
|
|
|
|
}
|
2009-11-11 09:00:40 +08:00
|
|
|
|
2009-11-11 03:49:08 +08:00
|
|
|
// Parse a non-type template argument.
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult ExprArg = ParseConstantExpression();
|
2009-02-10 07:23:08 +08:00
|
|
|
if (ExprArg.isInvalid() || !ExprArg.get())
|
2009-11-11 03:49:08 +08:00
|
|
|
return ParsedTemplateArgument();
|
2009-02-10 02:46:07 +08:00
|
|
|
|
2009-11-11 03:49:08 +08:00
|
|
|
return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
|
|
|
|
ExprArg.release(), Loc);
|
2008-12-19 03:37:40 +08:00
|
|
|
}
|
|
|
|
|
2010-05-22 07:18:07 +08:00
|
|
|
/// \brief Determine whether the current tokens can only be parsed as a
|
|
|
|
/// template argument list (starting with the '<') and never as a '<'
|
|
|
|
/// expression.
|
2010-05-22 07:43:39 +08:00
|
|
|
bool Parser::IsTemplateArgumentList(unsigned Skip) {
|
2010-05-22 07:18:07 +08:00
|
|
|
struct AlwaysRevertAction : TentativeParsingAction {
|
|
|
|
AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { }
|
|
|
|
~AlwaysRevertAction() { Revert(); }
|
|
|
|
} Tentative(*this);
|
|
|
|
|
2010-05-22 07:43:39 +08:00
|
|
|
while (Skip) {
|
|
|
|
ConsumeToken();
|
|
|
|
--Skip;
|
|
|
|
}
|
|
|
|
|
2010-05-22 07:18:07 +08:00
|
|
|
// '<'
|
|
|
|
if (!Tok.is(tok::less))
|
|
|
|
return false;
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// An empty template argument list.
|
|
|
|
if (Tok.is(tok::greater))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// See whether we have declaration specifiers, which indicate a type.
|
|
|
|
while (isCXXDeclarationSpecifier() == TPResult::True())
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// If we have a '>' or a ',' then this is a template argument list.
|
|
|
|
return Tok.is(tok::greater) || Tok.is(tok::comma);
|
|
|
|
}
|
|
|
|
|
2008-12-19 03:37:40 +08:00
|
|
|
/// ParseTemplateArgumentList - Parse a C++ template-argument-list
|
|
|
|
/// (C++ [temp.names]). Returns true if there was an error.
|
|
|
|
///
|
|
|
|
/// template-argument-list: [C++ 14.2]
|
|
|
|
/// template-argument
|
|
|
|
/// template-argument-list ',' template-argument
|
2009-09-09 23:08:12 +08:00
|
|
|
bool
|
2009-11-11 03:49:08 +08:00
|
|
|
Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
|
2008-12-19 03:37:40 +08:00
|
|
|
while (true) {
|
2009-11-11 03:49:08 +08:00
|
|
|
ParsedTemplateArgument Arg = ParseTemplateArgument();
|
2010-12-20 10:24:11 +08:00
|
|
|
if (Tok.is(tok::ellipsis)) {
|
|
|
|
SourceLocation EllipsisLoc = ConsumeToken();
|
|
|
|
Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
|
|
|
|
}
|
|
|
|
|
2009-11-11 03:49:08 +08:00
|
|
|
if (Arg.isInvalid()) {
|
2008-12-19 03:37:40 +08:00
|
|
|
SkipUntil(tok::comma, tok::greater, true, true);
|
|
|
|
return true;
|
|
|
|
}
|
2009-02-10 03:34:22 +08:00
|
|
|
|
2009-11-11 03:49:08 +08:00
|
|
|
// Save this template argument.
|
|
|
|
TemplateArgs.push_back(Arg);
|
|
|
|
|
2008-12-19 03:37:40 +08:00
|
|
|
// If the next token is a comma, consume it and keep reading
|
|
|
|
// arguments.
|
|
|
|
if (Tok.isNot(tok::comma)) break;
|
|
|
|
|
|
|
|
// Consume the comma.
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
2009-12-28 06:31:18 +08:00
|
|
|
return false;
|
2008-12-19 03:37:40 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// \brief Parse a C++ explicit template instantiation
|
2009-05-13 05:31:51 +08:00
|
|
|
/// (C++ [temp.explicit]).
|
|
|
|
///
|
|
|
|
/// explicit-instantiation:
|
2009-09-04 14:33:52 +08:00
|
|
|
/// 'extern' [opt] 'template' declaration
|
|
|
|
///
|
|
|
|
/// Note that the 'extern' is a GNU extension and C++0x feature.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
|
|
|
|
SourceLocation TemplateLoc,
|
|
|
|
SourceLocation &DeclEnd) {
|
2010-07-16 16:13:16 +08:00
|
|
|
// This isn't really required here.
|
|
|
|
ParsingDeclRAIIObject ParsingTemplateParams(*this);
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
|
2009-09-04 14:33:52 +08:00
|
|
|
ParsedTemplateInfo(ExternLoc,
|
|
|
|
TemplateLoc),
|
2010-07-16 16:13:16 +08:00
|
|
|
ParsingTemplateParams,
|
2009-05-13 07:25:50 +08:00
|
|
|
DeclEnd, AS_none);
|
2009-05-13 05:31:51 +08:00
|
|
|
}
|
2010-11-10 10:40:36 +08:00
|
|
|
|
|
|
|
SourceRange Parser::ParsedTemplateInfo::getSourceRange() const {
|
|
|
|
if (TemplateParams)
|
|
|
|
return getTemplateParamsRange(TemplateParams->data(),
|
|
|
|
TemplateParams->size());
|
|
|
|
|
|
|
|
SourceRange R(TemplateLoc);
|
|
|
|
if (ExternLoc.isValid())
|
|
|
|
R.setBegin(ExternLoc);
|
|
|
|
return R;
|
|
|
|
}
|
2011-04-23 06:18:13 +08:00
|
|
|
|
2011-04-23 19:52:20 +08:00
|
|
|
void Parser::LateTemplateParserCallback(void *P, const FunctionDecl *FD) {
|
2011-04-23 06:18:13 +08:00
|
|
|
((Parser*)P)->LateTemplateParser(FD);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-23 19:52:20 +08:00
|
|
|
void Parser::LateTemplateParser(const FunctionDecl *FD) {
|
2011-04-23 06:18:13 +08:00
|
|
|
LateParsedTemplatedFunction *LPT = LateParsedTemplateMap[FD];
|
|
|
|
if (LPT) {
|
|
|
|
ParseLateTemplatedFuncDef(*LPT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Late templated function without associated lexed tokens");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Late parse a C++ function template in Microsoft mode.
|
|
|
|
void Parser::ParseLateTemplatedFuncDef(LateParsedTemplatedFunction &LMT) {
|
|
|
|
if(!LMT.D)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If this is a member template, introduce the template parameter scope.
|
|
|
|
ParseScope TemplateScope(this, Scope::TemplateParamScope);
|
|
|
|
|
|
|
|
// Get the FunctionDecl.
|
|
|
|
FunctionDecl *FD = 0;
|
|
|
|
if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(LMT.D))
|
|
|
|
FD = FunTmpl->getTemplatedDecl();
|
|
|
|
else
|
|
|
|
FD = cast<FunctionDecl>(LMT.D);
|
|
|
|
|
|
|
|
// Reinject the template parameters.
|
2011-09-23 06:14:56 +08:00
|
|
|
SmallVector<ParseScope*, 4> TemplateParamScopeStack;
|
2011-04-23 06:18:13 +08:00
|
|
|
DeclaratorDecl* Declarator = dyn_cast<DeclaratorDecl>(FD);
|
|
|
|
if (Declarator && Declarator->getNumTemplateParameterLists() != 0) {
|
|
|
|
Actions.ActOnReenterDeclaratorTemplateScope(getCurScope(), Declarator);
|
|
|
|
Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D);
|
|
|
|
} else {
|
|
|
|
Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D);
|
|
|
|
|
2011-09-23 06:14:56 +08:00
|
|
|
// Get the list of DeclContext to reenter.
|
|
|
|
SmallVector<DeclContext*, 4> DeclContextToReenter;
|
2011-04-23 06:18:13 +08:00
|
|
|
DeclContext *DD = FD->getLexicalParent();
|
|
|
|
while (DD && DD->isRecord()) {
|
2011-09-23 06:14:56 +08:00
|
|
|
DeclContextToReenter.push_back(DD);
|
2011-04-23 06:18:13 +08:00
|
|
|
DD = DD->getLexicalParent();
|
|
|
|
}
|
2011-09-23 06:14:56 +08:00
|
|
|
|
2011-09-24 00:02:49 +08:00
|
|
|
// Reenter template scopes from outmost to innermost.
|
2011-09-23 06:14:56 +08:00
|
|
|
SmallVector<DeclContext*, 4>::reverse_iterator II =
|
|
|
|
DeclContextToReenter.rbegin();
|
|
|
|
for (; II != DeclContextToReenter.rend(); ++II) {
|
|
|
|
if (ClassTemplatePartialSpecializationDecl* MD =
|
|
|
|
dyn_cast_or_null<ClassTemplatePartialSpecializationDecl>(*II)) {
|
2011-09-30 16:32:17 +08:00
|
|
|
TemplateParamScopeStack.push_back(new ParseScope(this,
|
|
|
|
Scope::TemplateParamScope));
|
2011-09-23 06:14:56 +08:00
|
|
|
Actions.ActOnReenterTemplateScope(getCurScope(), MD);
|
|
|
|
} else if (CXXRecordDecl* MD = dyn_cast_or_null<CXXRecordDecl>(*II)) {
|
2011-09-30 16:32:17 +08:00
|
|
|
TemplateParamScopeStack.push_back(new ParseScope(this,
|
|
|
|
Scope::TemplateParamScope,
|
|
|
|
MD->getDescribedClassTemplate() != 0 ));
|
2011-09-23 06:14:56 +08:00
|
|
|
Actions.ActOnReenterTemplateScope(getCurScope(),
|
|
|
|
MD->getDescribedClassTemplate());
|
|
|
|
}
|
|
|
|
}
|
2011-04-23 06:18:13 +08:00
|
|
|
}
|
|
|
|
assert(!LMT.Toks.empty() && "Empty body!");
|
|
|
|
|
|
|
|
// Append the current token at the end of the new token stream so that it
|
|
|
|
// doesn't get lost.
|
|
|
|
LMT.Toks.push_back(Tok);
|
|
|
|
PP.EnterTokenStream(LMT.Toks.data(), LMT.Toks.size(), true, false);
|
|
|
|
|
|
|
|
// Consume the previously pushed token.
|
|
|
|
ConsumeAnyToken();
|
|
|
|
assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
|
|
|
|
&& "Inline method not starting with '{', ':' or 'try'");
|
|
|
|
|
|
|
|
// Parse the method body. Function body parsing code is similar enough
|
|
|
|
// to be re-used for method bodies as well.
|
|
|
|
ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
|
|
|
|
|
|
|
|
// Recreate the DeclContext.
|
|
|
|
Sema::ContextRAII SavedContext(Actions, Actions.getContainingDC(FD));
|
|
|
|
|
|
|
|
if (FunctionTemplateDecl *FunctionTemplate
|
|
|
|
= dyn_cast_or_null<FunctionTemplateDecl>(LMT.D))
|
|
|
|
Actions.ActOnStartOfFunctionDef(getCurScope(),
|
|
|
|
FunctionTemplate->getTemplatedDecl());
|
|
|
|
if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(LMT.D))
|
|
|
|
Actions.ActOnStartOfFunctionDef(getCurScope(), Function);
|
|
|
|
|
|
|
|
|
|
|
|
if (Tok.is(tok::kw_try)) {
|
|
|
|
ParseFunctionTryBlock(LMT.D, FnScope);
|
2011-09-23 06:14:56 +08:00
|
|
|
} else {
|
|
|
|
if (Tok.is(tok::colon))
|
|
|
|
ParseConstructorInitializer(LMT.D);
|
|
|
|
else
|
|
|
|
Actions.ActOnDefaultCtorInitializers(LMT.D);
|
2011-04-23 06:18:13 +08:00
|
|
|
|
2011-09-23 06:14:56 +08:00
|
|
|
if (Tok.is(tok::l_brace)) {
|
|
|
|
ParseFunctionStatementBody(LMT.D, FnScope);
|
|
|
|
Actions.MarkAsLateParsedTemplate(FD, false);
|
|
|
|
} else
|
2011-04-23 06:18:13 +08:00
|
|
|
Actions.ActOnFinishFunctionBody(LMT.D, 0);
|
2011-09-23 06:14:56 +08:00
|
|
|
}
|
2011-04-23 06:18:13 +08:00
|
|
|
|
2011-09-23 06:14:56 +08:00
|
|
|
// Exit scopes.
|
|
|
|
FnScope.Exit();
|
|
|
|
SmallVector<ParseScope*, 4>::reverse_iterator I =
|
|
|
|
TemplateParamScopeStack.rbegin();
|
|
|
|
for (; I != TemplateParamScopeStack.rend(); ++I)
|
|
|
|
delete *I;
|
2011-04-23 06:18:13 +08:00
|
|
|
|
|
|
|
DeclGroupPtrTy grp = Actions.ConvertDeclToDeclGroup(LMT.D);
|
|
|
|
if (grp)
|
|
|
|
Actions.getASTConsumer().HandleTopLevelDecl(grp.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Lex a delayed template function for late parsing.
|
|
|
|
void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
|
|
|
|
tok::TokenKind kind = Tok.getKind();
|
2011-09-30 16:32:17 +08:00
|
|
|
if (!ConsumeAndStoreFunctionPrologue(Toks)) {
|
|
|
|
// Consume everything up to (and including) the matching right brace.
|
|
|
|
ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
|
2011-04-23 06:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're in a function-try-block, we need to store all the catch blocks.
|
|
|
|
if (kind == tok::kw_try) {
|
|
|
|
while (Tok.is(tok::kw_catch)) {
|
|
|
|
ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
|
|
|
|
ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|