blob: e655dedab43fda9bab8255e3c0cf1ca7fd73a67f [file] [log] [blame]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001//===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements parsing of C++ templates.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregoreb31f392008-12-01 23:54:00 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
Douglas Gregorb53edfb2009-11-10 19:49:08 +000018#include "clang/Parse/Template.h"
Douglas Gregora3dff8e2009-08-24 23:03:25 +000019#include "llvm/Support/Compiler.h"
Douglas Gregoreb31f392008-12-01 23:54:00 +000020using namespace clang;
21
Douglas Gregor1b57ff32009-05-12 23:25:50 +000022/// \brief Parse a template declaration, explicit instantiation, or
23/// explicit specialization.
24Parser::DeclPtrTy
25Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
26 SourceLocation &DeclEnd,
27 AccessSpecifier AS) {
28 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less))
Mike Stump11289f42009-09-09 15:08:12 +000029 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
Douglas Gregor43e75172009-09-04 06:33:52 +000030 DeclEnd);
Douglas Gregor1b57ff32009-05-12 23:25:50 +000031
32 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
33}
34
Douglas Gregora3dff8e2009-08-24 23:03:25 +000035/// \brief RAII class that manages the template parameter depth.
36namespace {
37 class VISIBILITY_HIDDEN TemplateParameterDepthCounter {
38 unsigned &Depth;
39 unsigned AddedLevels;
40
41 public:
Mike Stump11289f42009-09-09 15:08:12 +000042 explicit TemplateParameterDepthCounter(unsigned &Depth)
Douglas Gregora3dff8e2009-08-24 23:03:25 +000043 : Depth(Depth), AddedLevels(0) { }
Mike Stump11289f42009-09-09 15:08:12 +000044
Douglas Gregora3dff8e2009-08-24 23:03:25 +000045 ~TemplateParameterDepthCounter() {
46 Depth -= AddedLevels;
47 }
Mike Stump11289f42009-09-09 15:08:12 +000048
49 void operator++() {
Douglas Gregora3dff8e2009-08-24 23:03:25 +000050 ++Depth;
51 ++AddedLevels;
52 }
Mike Stump11289f42009-09-09 15:08:12 +000053
Douglas Gregora3dff8e2009-08-24 23:03:25 +000054 operator unsigned() const { return Depth; }
55 };
56}
57
Douglas Gregor67a65642009-02-17 23:15:12 +000058/// \brief Parse a template declaration or an explicit specialization.
59///
60/// Template declarations include one or more template parameter lists
61/// and either the function or class template declaration. Explicit
62/// specializations contain one or more 'template < >' prefixes
63/// followed by a (possibly templated) declaration. Since the
64/// syntactic form of both features is nearly identical, we parse all
65/// of the template headers together and let semantic analysis sort
66/// the declarations from the explicit specializations.
Douglas Gregoreb31f392008-12-01 23:54:00 +000067///
68/// template-declaration: [C++ temp]
69/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregor67a65642009-02-17 23:15:12 +000070///
71/// explicit-specialization: [ C++ temp.expl.spec]
72/// 'template' '<' '>' declaration
Chris Lattner83f095c2009-03-28 19:18:32 +000073Parser::DeclPtrTy
Anders Carlssondfbbdf62009-03-26 00:52:18 +000074Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +000075 SourceLocation &DeclEnd,
Anders Carlssondfbbdf62009-03-26 00:52:18 +000076 AccessSpecifier AS) {
Mike Stump11289f42009-09-09 15:08:12 +000077 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
78 "Token does not start a template declaration.");
79
Douglas Gregorf5586182008-12-02 00:41:28 +000080 // Enter template-parameter scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +000081 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregorf5586182008-12-02 00:41:28 +000082
Douglas Gregorb9bd8a92008-12-24 02:52:09 +000083 // Parse multiple levels of template headers within this template
84 // parameter scope, e.g.,
85 //
86 // template<typename T>
87 // template<typename U>
88 // class A<T>::B { ... };
89 //
90 // We parse multiple levels non-recursively so that we can build a
91 // single data structure containing all of the template parameter
Douglas Gregor67a65642009-02-17 23:15:12 +000092 // lists to easily differentiate between the case above and:
Douglas Gregorb9bd8a92008-12-24 02:52:09 +000093 //
94 // template<typename T>
95 // class A {
96 // template<typename U> class B;
97 // };
98 //
99 // In the first case, the action for declaring A<T>::B receives
100 // both template parameter lists. In the second case, the action for
101 // defining A<T>::B receives just the inner template parameter list
102 // (and retrieves the outer template parameter list from its
103 // context).
Douglas Gregor468535e2009-08-20 18:46:05 +0000104 bool isSpecialization = true;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000105 bool LastParamListWasEmpty = false;
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000106 TemplateParameterLists ParamLists;
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000107 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000108 do {
109 // Consume the 'export', if any.
110 SourceLocation ExportLoc;
111 if (Tok.is(tok::kw_export)) {
112 ExportLoc = ConsumeToken();
113 }
114
115 // Consume the 'template', which should be here.
116 SourceLocation TemplateLoc;
117 if (Tok.is(tok::kw_template)) {
118 TemplateLoc = ConsumeToken();
119 } else {
120 Diag(Tok.getLocation(), diag::err_expected_template);
Chris Lattner83f095c2009-03-28 19:18:32 +0000121 return DeclPtrTy();
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000124 // Parse the '<' template-parameter-list '>'
125 SourceLocation LAngleLoc, RAngleLoc;
126 TemplateParameterList TemplateParams;
Mike Stump11289f42009-09-09 15:08:12 +0000127 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000128 RAngleLoc)) {
129 // Skip until the semi-colon or a }.
130 SkipUntil(tok::r_brace, true, true);
131 if (Tok.is(tok::semi))
132 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000133 return DeclPtrTy();
Douglas Gregore93e46c2009-07-22 23:48:44 +0000134 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000135
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000136 ParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000137 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
138 TemplateLoc, LAngleLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000139 TemplateParams.data(),
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000140 TemplateParams.size(), RAngleLoc));
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000141
142 if (!TemplateParams.empty()) {
143 isSpecialization = false;
144 ++Depth;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000145 } else {
146 LastParamListWasEmpty = true;
Mike Stump11289f42009-09-09 15:08:12 +0000147 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000148 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
149
150 // Parse the actual template declaration.
Mike Stump11289f42009-09-09 15:08:12 +0000151 return ParseSingleDeclarationAfterTemplate(Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000152 ParsedTemplateInfo(&ParamLists,
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000153 isSpecialization,
154 LastParamListWasEmpty),
Douglas Gregor23996282009-05-12 21:31:51 +0000155 DeclEnd, AS);
156}
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000157
Douglas Gregor23996282009-05-12 21:31:51 +0000158/// \brief Parse a single declaration that declares a template,
159/// template specialization, or explicit instantiation of a template.
160///
161/// \param TemplateParams if non-NULL, the template parameter lists
162/// that preceded this declaration. In this case, the declaration is a
163/// template declaration, out-of-line definition of a template, or an
164/// explicit template specialization. When NULL, the declaration is an
165/// explicit template instantiation.
166///
167/// \param TemplateLoc when TemplateParams is NULL, the location of
168/// the 'template' keyword that indicates that we have an explicit
169/// template instantiation.
170///
171/// \param DeclEnd will receive the source location of the last token
172/// within this declaration.
173///
174/// \param AS the access specifier associated with this
175/// declaration. Will be AS_none for namespace-scope declarations.
176///
177/// \returns the new declaration.
Mike Stump11289f42009-09-09 15:08:12 +0000178Parser::DeclPtrTy
Douglas Gregor23996282009-05-12 21:31:51 +0000179Parser::ParseSingleDeclarationAfterTemplate(
180 unsigned Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000181 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor23996282009-05-12 21:31:51 +0000182 SourceLocation &DeclEnd,
183 AccessSpecifier AS) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000184 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
185 "Template information required");
186
Douglas Gregor3447e762009-08-20 22:52:58 +0000187 if (Context == Declarator::MemberContext) {
188 // We are parsing a member template.
189 ParseCXXClassMemberDeclaration(AS, TemplateInfo);
190 return DeclPtrTy::make((void*)0);
191 }
Mike Stump11289f42009-09-09 15:08:12 +0000192
Douglas Gregor23996282009-05-12 21:31:51 +0000193 // Parse the declaration specifiers.
John McCall28a6aea2009-11-04 02:18:39 +0000194 ParsingDeclSpec DS(*this);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000195 ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
Douglas Gregor23996282009-05-12 21:31:51 +0000196
197 if (Tok.is(tok::semi)) {
198 DeclEnd = ConsumeToken();
John McCall28a6aea2009-11-04 02:18:39 +0000199 DeclPtrTy Decl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
200 DS.complete(Decl);
201 return Decl;
Douglas Gregor23996282009-05-12 21:31:51 +0000202 }
203
204 // Parse the declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000205 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
Douglas Gregor23996282009-05-12 21:31:51 +0000206 ParseDeclarator(DeclaratorInfo);
207 // Error parsing the declarator?
208 if (!DeclaratorInfo.hasName()) {
209 // If so, skip until the semi-colon or a }.
210 SkipUntil(tok::r_brace, true, true);
211 if (Tok.is(tok::semi))
212 ConsumeToken();
213 return DeclPtrTy();
214 }
Mike Stump11289f42009-09-09 15:08:12 +0000215
Douglas Gregor23996282009-05-12 21:31:51 +0000216 // If we have a declaration or declarator list, handle it.
217 if (isDeclarationAfterDeclarator()) {
218 // Parse this declaration.
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000219 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
220 TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000221
222 if (Tok.is(tok::comma)) {
223 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000224 << (int)TemplateInfo.Kind;
Douglas Gregor23996282009-05-12 21:31:51 +0000225 SkipUntil(tok::semi, true, false);
226 return ThisDecl;
227 }
228
229 // Eat the semi colon after the declaration.
John McCallef50e992009-07-31 02:20:35 +0000230 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
John McCall28a6aea2009-11-04 02:18:39 +0000231 DS.complete(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +0000232 return ThisDecl;
233 }
234
235 if (DeclaratorInfo.isFunctionDeclarator() &&
236 isStartOfFunctionDefinition()) {
237 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
238 Diag(Tok, diag::err_function_declared_typedef);
239
240 if (Tok.is(tok::l_brace)) {
241 // This recovery skips the entire function body. It would be nice
242 // to simply call ParseFunctionDefinition() below, however Sema
243 // assumes the declarator represents a function, not a typedef.
244 ConsumeBrace();
245 SkipUntil(tok::r_brace, true);
246 } else {
247 SkipUntil(tok::semi);
248 }
249 return DeclPtrTy();
250 }
Douglas Gregor17a7c122009-06-24 00:54:41 +0000251 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000252 }
253
254 if (DeclaratorInfo.isFunctionDeclarator())
255 Diag(Tok, diag::err_expected_fn_body);
256 else
257 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
258 SkipUntil(tok::semi);
259 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000260}
261
262/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000263/// angle brackets. Depth is the depth of this template-parameter-list, which
264/// is the number of template headers directly enclosing this template header.
265/// TemplateParams is the current list of template parameters we're building.
266/// The template parameter we parse will be added to this list. LAngleLoc and
Mike Stump11289f42009-09-09 15:08:12 +0000267/// RAngleLoc will receive the positions of the '<' and '>', respectively,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000268/// that enclose this template parameter list.
Douglas Gregore93e46c2009-07-22 23:48:44 +0000269///
270/// \returns true if an error occurred, false otherwise.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000271bool Parser::ParseTemplateParameters(unsigned Depth,
272 TemplateParameterList &TemplateParams,
273 SourceLocation &LAngleLoc,
274 SourceLocation &RAngleLoc) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000275 // Get the template parameter list.
Mike Stump11289f42009-09-09 15:08:12 +0000276 if (!Tok.is(tok::less)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000277 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregore93e46c2009-07-22 23:48:44 +0000278 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000279 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000280 LAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000281
Douglas Gregoreb31f392008-12-01 23:54:00 +0000282 // Try to parse the template parameter list.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000283 if (Tok.is(tok::greater))
284 RAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000285 else if (ParseTemplateParameterList(Depth, TemplateParams)) {
286 if (!Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000287 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregore93e46c2009-07-22 23:48:44 +0000288 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000289 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000290 RAngleLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000291 }
Douglas Gregore93e46c2009-07-22 23:48:44 +0000292 return false;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000293}
294
295/// ParseTemplateParameterList - Parse a template parameter list. If
296/// the parsing fails badly (i.e., closing bracket was left out), this
297/// will try to put the token stream in a reasonable position (closing
Mike Stump11289f42009-09-09 15:08:12 +0000298/// a statement, etc.) and return false.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000299///
300/// template-parameter-list: [C++ temp]
301/// template-parameter
302/// template-parameter-list ',' template-parameter
Mike Stump11289f42009-09-09 15:08:12 +0000303bool
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000304Parser::ParseTemplateParameterList(unsigned Depth,
305 TemplateParameterList &TemplateParams) {
Mike Stump11289f42009-09-09 15:08:12 +0000306 while (1) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000307 if (DeclPtrTy TmpParam
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000308 = ParseTemplateParameter(Depth, TemplateParams.size())) {
309 TemplateParams.push_back(TmpParam);
310 } else {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000311 // If we failed to parse a template parameter, skip until we find
312 // a comma or closing brace.
313 SkipUntil(tok::comma, tok::greater, true, true);
314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
Douglas Gregoreb31f392008-12-01 23:54:00 +0000316 // Did we find a comma or the end of the template parmeter list?
Mike Stump11289f42009-09-09 15:08:12 +0000317 if (Tok.is(tok::comma)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000318 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000319 } else if (Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000320 // Don't consume this... that's done by template parser.
321 break;
322 } else {
323 // Somebody probably forgot to close the template. Skip ahead and
324 // try to get out of the expression. This error is currently
325 // subsumed by whatever goes on in ParseTemplateParameter.
326 // TODO: This could match >>, and it would be nice to avoid those
327 // silly errors with template <vec<T>>.
328 // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
329 SkipUntil(tok::greater, true, true);
330 return false;
331 }
332 }
333 return true;
334}
335
Douglas Gregor26aedb72009-11-21 02:07:55 +0000336/// \brief Determine whether the parser is at the start of a template
337/// type parameter.
338bool Parser::isStartOfTemplateTypeParameter() {
339 if (Tok.is(tok::kw_class))
340 return true;
341
342 if (Tok.isNot(tok::kw_typename))
343 return false;
344
345 // C++ [temp.param]p2:
346 // There is no semantic difference between class and typename in a
347 // template-parameter. typename followed by an unqualified-id
348 // names a template type parameter. typename followed by a
349 // qualified-id denotes the type in a non-type
350 // parameter-declaration.
351 Token Next = NextToken();
352
353 // If we have an identifier, skip over it.
354 if (Next.getKind() == tok::identifier)
355 Next = GetLookAheadToken(2);
356
357 switch (Next.getKind()) {
358 case tok::equal:
359 case tok::comma:
360 case tok::greater:
361 case tok::greatergreater:
362 case tok::ellipsis:
363 return true;
364
365 default:
366 return false;
367 }
368}
369
Douglas Gregoreb31f392008-12-01 23:54:00 +0000370/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
371///
372/// template-parameter: [C++ temp.param]
373/// type-parameter
374/// parameter-declaration
375///
376/// type-parameter: (see below)
Anders Carlssonf986ba72009-06-12 23:09:56 +0000377/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000378/// 'class' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000379/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000380/// 'typename' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000381/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000382/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Mike Stump11289f42009-09-09 15:08:12 +0000383Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000384Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor26aedb72009-11-21 02:07:55 +0000385 if (isStartOfTemplateTypeParameter())
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000386 return ParseTypeParameter(Depth, Position);
Mike Stump11289f42009-09-09 15:08:12 +0000387
388 if (Tok.is(tok::kw_template))
Chris Lattnera21db612009-01-04 23:51:17 +0000389 return ParseTemplateTemplateParameter(Depth, Position);
390
391 // If it's none of the above, then it must be a parameter declaration.
392 // NOTE: This will pick up errors in the closure of the template parameter
393 // list (e.g., template < ; Check here to implement >> style closures.
394 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000395}
396
397/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
398/// Other kinds of template parameters are parsed in
399/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
400///
401/// type-parameter: [C++ temp.param]
Anders Carlssonf986ba72009-06-12 23:09:56 +0000402/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000403/// 'class' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000404/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000405/// 'typename' identifier[opt] '=' type-id
Chris Lattner83f095c2009-03-28 19:18:32 +0000406Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregorf5586182008-12-02 00:41:28 +0000407 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
Mike Stump11289f42009-09-09 15:08:12 +0000408 "A type-parameter starts with 'class' or 'typename'");
Douglas Gregorf5586182008-12-02 00:41:28 +0000409
410 // Consume the 'class' or 'typename' keyword.
411 bool TypenameKeyword = Tok.is(tok::kw_typename);
412 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000413
Anders Carlsson01e9e932009-06-12 19:58:00 +0000414 // Grab the ellipsis (if given).
415 bool Ellipsis = false;
416 SourceLocation EllipsisLoc;
Anders Carlssonf986ba72009-06-12 23:09:56 +0000417 if (Tok.is(tok::ellipsis)) {
Anders Carlsson01e9e932009-06-12 19:58:00 +0000418 Ellipsis = true;
419 EllipsisLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000420
421 if (!getLang().CPlusPlus0x)
Anders Carlssonf986ba72009-06-12 23:09:56 +0000422 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson01e9e932009-06-12 19:58:00 +0000423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Douglas Gregoreb31f392008-12-01 23:54:00 +0000425 // Grab the template parameter name (if given)
Douglas Gregorf5586182008-12-02 00:41:28 +0000426 SourceLocation NameLoc;
427 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000428 if (Tok.is(tok::identifier)) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000429 ParamName = Tok.getIdentifierInfo();
430 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000431 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
432 Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000433 // Unnamed template parameter. Don't have to do anything here, just
434 // don't consume this token.
435 } else {
436 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +0000437 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000438 }
Mike Stump11289f42009-09-09 15:08:12 +0000439
Chris Lattner83f095c2009-03-28 19:18:32 +0000440 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson01e9e932009-06-12 19:58:00 +0000441 Ellipsis, EllipsisLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000442 KeyLoc, ParamName, NameLoc,
443 Depth, Position);
Douglas Gregorf5586182008-12-02 00:41:28 +0000444
Douglas Gregoreb31f392008-12-01 23:54:00 +0000445 // Grab a default type id (if given).
Mike Stump11289f42009-09-09 15:08:12 +0000446 if (Tok.is(tok::equal)) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000447 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregordba32632009-02-10 19:49:53 +0000448 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +0000449 TypeResult DefaultType = ParseTypeName();
450 if (!DefaultType.isInvalid())
Douglas Gregordba32632009-02-10 19:49:53 +0000451 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +0000452 DefaultType.get());
Douglas Gregoreb31f392008-12-01 23:54:00 +0000453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Douglas Gregorf5586182008-12-02 00:41:28 +0000455 return TypeParam;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000456}
457
458/// ParseTemplateTemplateParameter - Handle the parsing of template
Mike Stump11289f42009-09-09 15:08:12 +0000459/// template parameters.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000460///
461/// type-parameter: [C++ temp.param]
462/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
463/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattner83f095c2009-03-28 19:18:32 +0000464Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000465Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000466 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
467
468 // Handle the template <...> part.
469 SourceLocation TemplateLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000470 TemplateParameterList TemplateParams;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000471 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000472 {
473 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Mike Stump11289f42009-09-09 15:08:12 +0000474 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000475 RAngleLoc)) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000476 return DeclPtrTy();
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000477 }
Douglas Gregoreb31f392008-12-01 23:54:00 +0000478 }
479
480 // Generate a meaningful error if the user forgot to put class before the
481 // identifier, comma, or greater.
Mike Stump11289f42009-09-09 15:08:12 +0000482 if (!Tok.is(tok::kw_class)) {
483 Diag(Tok.getLocation(), diag::err_expected_class_before)
Douglas Gregoreb31f392008-12-01 23:54:00 +0000484 << PP.getSpelling(Tok);
Chris Lattner83f095c2009-03-28 19:18:32 +0000485 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000486 }
487 SourceLocation ClassLoc = ConsumeToken();
488
489 // Get the identifier, if given.
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000490 SourceLocation NameLoc;
491 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000492 if (Tok.is(tok::identifier)) {
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000493 ParamName = Tok.getIdentifierInfo();
494 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000495 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000496 // Unnamed template parameter. Don't have to do anything here, just
497 // don't consume this token.
498 } else {
499 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +0000500 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000501 }
502
Mike Stump11289f42009-09-09 15:08:12 +0000503 TemplateParamsTy *ParamList =
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000504 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
505 TemplateLoc, LAngleLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000506 &TemplateParams[0],
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000507 TemplateParams.size(),
508 RAngleLoc);
509
Chris Lattner83f095c2009-03-28 19:18:32 +0000510 Parser::DeclPtrTy Param
Douglas Gregordba32632009-02-10 19:49:53 +0000511 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
512 ParamList, ParamName,
513 NameLoc, Depth, Position);
514
515 // Get the a default value, if given.
516 if (Tok.is(tok::equal)) {
517 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000518 ParsedTemplateArgument Default = ParseTemplateTemplateArgument();
519 if (Default.isInvalid()) {
520 Diag(Tok.getLocation(),
521 diag::err_default_template_template_parameter_not_template);
522 static tok::TokenKind EndToks[] = {
523 tok::comma, tok::greater, tok::greatergreater
524 };
525 SkipUntil(EndToks, 3, true, true);
Douglas Gregordba32632009-02-10 19:49:53 +0000526 return Param;
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000527 } else if (Param)
528 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc, Default);
Douglas Gregordba32632009-02-10 19:49:53 +0000529 }
530
531 return Param;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000532}
533
534/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
Mike Stump11289f42009-09-09 15:08:12 +0000535/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000536///
Douglas Gregoreb31f392008-12-01 23:54:00 +0000537/// template-parameter:
538/// ...
539/// parameter-declaration
540///
541/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
542/// but that didn't work out to well. Instead, this tries to recrate the basic
543/// parsing of parameter declarations, but tries to constrain it for template
544/// parameters.
Douglas Gregorf5586182008-12-02 00:41:28 +0000545/// FIXME: We need to make a ParseParameterDeclaration that works for
546/// non-type template parameters and normal function parameters.
Mike Stump11289f42009-09-09 15:08:12 +0000547Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000548Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000549 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000550
551 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregorf5586182008-12-02 00:41:28 +0000552 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregoreb31f392008-12-01 23:54:00 +0000553 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregorf5586182008-12-02 00:41:28 +0000554 DeclSpec DS;
555 ParseDeclarationSpecifiers(DS);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000556
557 // Parse this as a typename.
Douglas Gregorf5586182008-12-02 00:41:28 +0000558 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
559 ParseDeclarator(ParamDecl);
Chris Lattnerb5134c02009-01-05 01:24:05 +0000560 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000561 // This probably shouldn't happen - and it's more of a Sema thing, but
562 // basically we didn't parse the type name because we couldn't associate
563 // it with an AST node. we should just skip to the comma or greater.
564 // TODO: This is currently a placeholder for some kind of Sema Error.
565 Diag(Tok.getLocation(), diag::err_parse_error);
566 SkipUntil(tok::comma, tok::greater, true, true);
Chris Lattner83f095c2009-03-28 19:18:32 +0000567 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000568 }
569
Mike Stump11289f42009-09-09 15:08:12 +0000570 // Create the parameter.
Chris Lattner83f095c2009-03-28 19:18:32 +0000571 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
572 Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000573
Douglas Gregordba32632009-02-10 19:49:53 +0000574 // If there is a default value, parse it.
Chris Lattnerb5134c02009-01-05 01:24:05 +0000575 if (Tok.is(tok::equal)) {
Douglas Gregordba32632009-02-10 19:49:53 +0000576 SourceLocation EqualLoc = ConsumeToken();
577
578 // C++ [temp.param]p15:
579 // When parsing a default template-argument for a non-type
580 // template-parameter, the first non-nested > is taken as the
581 // end of the template-parameter-list rather than a greater-than
582 // operator.
Mike Stump11289f42009-09-09 15:08:12 +0000583 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Douglas Gregordba32632009-02-10 19:49:53 +0000584
585 OwningExprResult DefaultArg = ParseAssignmentExpression();
586 if (DefaultArg.isInvalid())
587 SkipUntil(tok::comma, tok::greater, true, true);
588 else if (Param)
Mike Stump11289f42009-09-09 15:08:12 +0000589 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
Douglas Gregordba32632009-02-10 19:49:53 +0000590 move(DefaultArg));
Douglas Gregoreb31f392008-12-01 23:54:00 +0000591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Douglas Gregorf5586182008-12-02 00:41:28 +0000593 return Param;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000594}
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000595
Douglas Gregor67a65642009-02-17 23:15:12 +0000596/// \brief Parses a template-id that after the template name has
597/// already been parsed.
598///
599/// This routine takes care of parsing the enclosed template argument
600/// list ('<' template-parameter-list [opt] '>') and placing the
601/// results into a form that can be transferred to semantic analysis.
602///
603/// \param Template the template declaration produced by isTemplateName
604///
605/// \param TemplateNameLoc the source location of the template name
606///
607/// \param SS if non-NULL, the nested-name-specifier preceding the
608/// template name.
609///
610/// \param ConsumeLastToken if true, then we will consume the last
611/// token that forms the template-id. Otherwise, we will leave the
612/// last token in the stream (e.g., so that it can be replaced with an
613/// annotation token).
Mike Stump11289f42009-09-09 15:08:12 +0000614bool
Douglas Gregordc572a32009-03-30 22:58:21 +0000615Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Mike Stump11289f42009-09-09 15:08:12 +0000616 SourceLocation TemplateNameLoc,
Douglas Gregor67a65642009-02-17 23:15:12 +0000617 const CXXScopeSpec *SS,
618 bool ConsumeLastToken,
619 SourceLocation &LAngleLoc,
620 TemplateArgList &TemplateArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000621 SourceLocation &RAngleLoc) {
622 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
623
624 // Consume the '<'.
625 LAngleLoc = ConsumeToken();
626
627 // Parse the optional template-argument-list.
628 bool Invalid = false;
629 {
630 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
631 if (Tok.isNot(tok::greater))
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000632 Invalid = ParseTemplateArgumentList(TemplateArgs);
Douglas Gregor67a65642009-02-17 23:15:12 +0000633
634 if (Invalid) {
635 // Try to find the closing '>'.
636 SkipUntil(tok::greater, true, !ConsumeLastToken);
637
638 return true;
639 }
640 }
641
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000642 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
Douglas Gregor67a65642009-02-17 23:15:12 +0000643 return true;
644
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000645 // Determine the location of the '>' or '>>'. Only consume this
646 // token if the caller asked us to.
Douglas Gregor67a65642009-02-17 23:15:12 +0000647 RAngleLoc = Tok.getLocation();
648
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000649 if (Tok.is(tok::greatergreater)) {
Douglas Gregor87f95b02009-02-26 21:00:50 +0000650 if (!getLang().CPlusPlus0x) {
651 const char *ReplaceStr = "> >";
652 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
653 ReplaceStr = "> > ";
654
655 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregor96977da2009-02-27 17:53:17 +0000656 << CodeModificationHint::CreateReplacement(
657 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000658 }
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000659
660 Tok.setKind(tok::greater);
661 if (!ConsumeLastToken) {
662 // Since we're not supposed to consume the '>>' token, we need
663 // to insert a second '>' token after the first.
664 PP.EnterToken(Tok);
665 }
666 } else if (ConsumeLastToken)
Douglas Gregor67a65642009-02-17 23:15:12 +0000667 ConsumeToken();
668
669 return false;
670}
Mike Stump11289f42009-09-09 15:08:12 +0000671
Douglas Gregor7f741122009-02-25 19:37:18 +0000672/// \brief Replace the tokens that form a simple-template-id with an
673/// annotation token containing the complete template-id.
674///
675/// The first token in the stream must be the name of a template that
676/// is followed by a '<'. This routine will parse the complete
677/// simple-template-id and replace the tokens with a single annotation
678/// token with one of two different kinds: if the template-id names a
679/// type (and \p AllowTypeAnnotation is true), the annotation token is
680/// a type annotation that includes the optional nested-name-specifier
681/// (\p SS). Otherwise, the annotation token is a template-id
682/// annotation that does not include the optional
683/// nested-name-specifier.
684///
685/// \param Template the declaration of the template named by the first
686/// token (an identifier), as returned from \c Action::isTemplateName().
687///
688/// \param TemplateNameKind the kind of template that \p Template
689/// refers to, as returned from \c Action::isTemplateName().
690///
691/// \param SS if non-NULL, the nested-name-specifier that precedes
692/// this template name.
693///
694/// \param TemplateKWLoc if valid, specifies that this template-id
695/// annotation was preceded by the 'template' keyword and gives the
696/// location of that keyword. If invalid (the default), then this
697/// template-id was not preceded by a 'template' keyword.
698///
699/// \param AllowTypeAnnotation if true (the default), then a
700/// simple-template-id that refers to a class template, template
701/// template parameter, or other template that produces a type will be
702/// replaced with a type annotation token. Otherwise, the
703/// simple-template-id is always replaced with a template-id
704/// annotation token.
Chris Lattner5558e9f2009-06-26 04:27:47 +0000705///
706/// If an unrecoverable parse error occurs and no annotation token can be
707/// formed, this function returns true.
708///
709bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Mike Stump11289f42009-09-09 15:08:12 +0000710 const CXXScopeSpec *SS,
Douglas Gregor71395fa2009-11-04 00:56:37 +0000711 UnqualifiedId &TemplateName,
Douglas Gregor7f741122009-02-25 19:37:18 +0000712 SourceLocation TemplateKWLoc,
713 bool AllowTypeAnnotation) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000714 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
Douglas Gregor71395fa2009-11-04 00:56:37 +0000715 assert(Template && Tok.is(tok::less) &&
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000716 "Parser isn't at the beginning of a template-id");
717
718 // Consume the template-name.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000719 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000720
Douglas Gregor67a65642009-02-17 23:15:12 +0000721 // Parse the enclosed template argument list.
722 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor67b556a2009-02-09 19:34:22 +0000723 TemplateArgList TemplateArgs;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000724 bool Invalid = ParseTemplateIdAfterTemplateName(Template,
725 TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000726 SS, false, LAngleLoc,
727 TemplateArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000728 RAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000729
Chris Lattner5558e9f2009-06-26 04:27:47 +0000730 if (Invalid) {
731 // If we failed to parse the template ID but skipped ahead to a >, we're not
732 // going to be able to form a token annotation. Eat the '>' if present.
733 if (Tok.is(tok::greater))
734 ConsumeToken();
735 return true;
736 }
Douglas Gregord32e0282009-02-09 23:23:08 +0000737
Jay Foad7d0479f2009-05-21 09:52:38 +0000738 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor67a65642009-02-17 23:15:12 +0000739 TemplateArgs.size());
Douglas Gregor0db4ccd2009-02-09 21:04:56 +0000740
Douglas Gregor8bf42052009-02-09 18:46:07 +0000741 // Build the annotation token.
Douglas Gregorb67535d2009-03-31 00:43:58 +0000742 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Mike Stump11289f42009-09-09 15:08:12 +0000743 Action::TypeResult Type
Douglas Gregordc572a32009-03-30 22:58:21 +0000744 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
745 LAngleLoc, TemplateArgsPtr,
Douglas Gregordc572a32009-03-30 22:58:21 +0000746 RAngleLoc);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000747 if (Type.isInvalid()) {
748 // If we failed to parse the template ID but skipped ahead to a >, we're not
749 // going to be able to form a token annotation. Eat the '>' if present.
750 if (Tok.is(tok::greater))
751 ConsumeToken();
752 return true;
753 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000754
755 Tok.setKind(tok::annot_typename);
756 Tok.setAnnotationValue(Type.get());
Douglas Gregor7f741122009-02-25 19:37:18 +0000757 if (SS && SS->isNotEmpty())
758 Tok.setLocation(SS->getBeginLoc());
759 else if (TemplateKWLoc.isValid())
760 Tok.setLocation(TemplateKWLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000761 else
Douglas Gregor7f741122009-02-25 19:37:18 +0000762 Tok.setLocation(TemplateNameLoc);
Douglas Gregor67a65642009-02-17 23:15:12 +0000763 } else {
Douglas Gregorb67535d2009-03-31 00:43:58 +0000764 // Build a template-id annotation token that can be processed
765 // later.
Douglas Gregor7f741122009-02-25 19:37:18 +0000766 Tok.setKind(tok::annot_template_id);
Mike Stump11289f42009-09-09 15:08:12 +0000767 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000768 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor8bf42052009-02-09 18:46:07 +0000769 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000770 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
771 TemplateId->Name = TemplateName.Identifier;
772 TemplateId->Operator = OO_None;
773 } else {
774 TemplateId->Name = 0;
775 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
776 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000777 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor7f741122009-02-25 19:37:18 +0000778 TemplateId->Kind = TNK;
Douglas Gregor8bf42052009-02-09 18:46:07 +0000779 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000780 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000781 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
782 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
Douglas Gregor8bf42052009-02-09 18:46:07 +0000783 Args[Arg] = TemplateArgs[Arg];
784 Tok.setAnnotationValue(TemplateId);
Douglas Gregor7f741122009-02-25 19:37:18 +0000785 if (TemplateKWLoc.isValid())
786 Tok.setLocation(TemplateKWLoc);
787 else
788 Tok.setLocation(TemplateNameLoc);
789
790 TemplateArgsPtr.release();
Douglas Gregor8bf42052009-02-09 18:46:07 +0000791 }
792
793 // Common fields for the annotation token
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000794 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000795
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000796 // In case the tokens were cached, have Preprocessor replace them with the
797 // annotation token.
798 PP.AnnotateCachedTokens(Tok);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000799 return false;
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000800}
801
Douglas Gregor7f741122009-02-25 19:37:18 +0000802/// \brief Replaces a template-id annotation token with a type
803/// annotation token.
804///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000805/// If there was a failure when forming the type from the template-id,
806/// a type annotation token will still be created, but will have a
807/// NULL type pointer to signify an error.
808void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000809 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
810
Mike Stump11289f42009-09-09 15:08:12 +0000811 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000812 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000813 assert((TemplateId->Kind == TNK_Type_template ||
814 TemplateId->Kind == TNK_Dependent_template_name) &&
815 "Only works for type and dependent templates");
Mike Stump11289f42009-09-09 15:08:12 +0000816
817 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000818 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000819 TemplateId->NumArgs);
820
Mike Stump11289f42009-09-09 15:08:12 +0000821 Action::TypeResult Type
Douglas Gregordc572a32009-03-30 22:58:21 +0000822 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
823 TemplateId->TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000824 TemplateId->LAngleLoc,
Douglas Gregordc572a32009-03-30 22:58:21 +0000825 TemplateArgsPtr,
Douglas Gregordc572a32009-03-30 22:58:21 +0000826 TemplateId->RAngleLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +0000827 // Create the new "type" annotation token.
828 Tok.setKind(tok::annot_typename);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000829 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor7f741122009-02-25 19:37:18 +0000830 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
831 Tok.setLocation(SS->getBeginLoc());
Douglas Gregor35522592009-11-04 18:18:19 +0000832 Tok.setAnnotationEndLoc(TemplateId->TemplateNameLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +0000833
Douglas Gregor35522592009-11-04 18:18:19 +0000834 // Replace the template-id annotation token, and possible the scope-specifier
835 // that precedes it, with the typename annotation token.
836 PP.AnnotateCachedTokens(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +0000837 TemplateId->Destroy();
Douglas Gregor7f741122009-02-25 19:37:18 +0000838}
839
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000840/// \brief Determine whether the given token can end a template argument.
Benjamin Kramer2c9a91c2009-11-10 21:29:56 +0000841static bool isEndOfTemplateArgument(Token Tok) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000842 return Tok.is(tok::comma) || Tok.is(tok::greater) ||
843 Tok.is(tok::greatergreater);
844}
845
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000846/// \brief Parse a C++ template template argument.
847ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
848 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
849 !Tok.is(tok::annot_cxxscope))
850 return ParsedTemplateArgument();
851
852 // C++0x [temp.arg.template]p1:
853 // A template-argument for a template template-parameter shall be the name
854 // of a class template or a template alias, expressed as id-expression.
855 //
Douglas Gregorc9984092009-11-12 00:03:40 +0000856 // We parse an id-expression that refers to a class template or template
857 // alias. The grammar we parse is:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000858 //
859 // nested-name-specifier[opt] template[opt] identifier
860 //
861 // followed by a token that terminates a template argument, such as ',',
862 // '>', or (in some cases) '>>'.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000863 CXXScopeSpec SS; // nested-name-specifier, if present
864 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
865 /*EnteringContext=*/false);
866
867 if (SS.isSet() && Tok.is(tok::kw_template)) {
868 // Parse the optional 'template' keyword following the
869 // nested-name-specifier.
870 SourceLocation TemplateLoc = ConsumeToken();
871
872 if (Tok.is(tok::identifier)) {
873 // We appear to have a dependent template name.
874 UnqualifiedId Name;
875 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
876 ConsumeToken(); // the identifier
877
878 // If the next token signals the end of a template argument,
879 // then we have a dependent template name that could be a template
880 // template argument.
881 if (isEndOfTemplateArgument(Tok)) {
882 TemplateTy Template
883 = Actions.ActOnDependentTemplateName(TemplateLoc, SS, Name,
Douglas Gregorade9bcd2009-11-20 23:39:24 +0000884 /*ObjectType=*/0,
885 /*EnteringContext=*/false);
Douglas Gregorc9984092009-11-12 00:03:40 +0000886 if (Template.get())
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000887 return ParsedTemplateArgument(SS, Template, Name.StartLocation);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000888 }
889 }
890 } else if (Tok.is(tok::identifier)) {
891 // We may have a (non-dependent) template name.
892 TemplateTy Template;
893 UnqualifiedId Name;
894 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
895 ConsumeToken(); // the identifier
896
897 if (isEndOfTemplateArgument(Tok)) {
898 TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS, Name,
899 /*ObjectType=*/0,
900 /*EnteringContext=*/false,
901 Template);
902 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
903 // We have an id-expression that refers to a class template or
904 // (C++0x) template alias.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000905 return ParsedTemplateArgument(SS, Template, Name.StartLocation);
906 }
907 }
908 }
909
Douglas Gregorc9984092009-11-12 00:03:40 +0000910 // We don't have a template template argument.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000911 return ParsedTemplateArgument();
912}
913
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000914/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
915///
916/// template-argument: [C++ 14.2]
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000917/// constant-expression
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000918/// type-id
919/// id-expression
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000920ParsedTemplateArgument Parser::ParseTemplateArgument() {
Douglas Gregor8bf42052009-02-09 18:46:07 +0000921 // C++ [temp.arg]p2:
922 // In a template-argument, an ambiguity between a type-id and an
923 // expression is resolved to a type-id, regardless of the form of
924 // the corresponding template-parameter.
925 //
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000926 // Therefore, we initially try to parse a type-id.
Douglas Gregor97f34572009-02-10 00:53:15 +0000927 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000928 SourceLocation Loc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +0000929 TypeResult TypeArg = ParseTypeName();
930 if (TypeArg.isInvalid())
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000931 return ParsedTemplateArgument();
932
933 return ParsedTemplateArgument(ParsedTemplateArgument::Type, TypeArg.get(),
934 Loc);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000935 }
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000936
937 // Try to parse a template template argument.
Douglas Gregorc9984092009-11-12 00:03:40 +0000938 {
939 TentativeParsingAction TPA(*this);
940
941 ParsedTemplateArgument TemplateTemplateArgument
942 = ParseTemplateTemplateArgument();
943 if (!TemplateTemplateArgument.isInvalid()) {
944 TPA.Commit();
945 return TemplateTemplateArgument;
946 }
947
948 // Revert this tentative parse to parse a non-type template argument.
949 TPA.Revert();
950 }
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000951
952 // Parse a non-type template argument.
953 SourceLocation Loc = Tok.getLocation();
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000954 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregord32e0282009-02-09 23:23:08 +0000955 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000956 return ParsedTemplateArgument();
Douglas Gregor8bf42052009-02-09 18:46:07 +0000957
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000958 return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
959 ExprArg.release(), Loc);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000960}
961
962/// ParseTemplateArgumentList - Parse a C++ template-argument-list
963/// (C++ [temp.names]). Returns true if there was an error.
964///
965/// template-argument-list: [C++ 14.2]
966/// template-argument
967/// template-argument-list ',' template-argument
Mike Stump11289f42009-09-09 15:08:12 +0000968bool
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000969Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000970 while (true) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000971 ParsedTemplateArgument Arg = ParseTemplateArgument();
972 if (Arg.isInvalid()) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000973 SkipUntil(tok::comma, tok::greater, true, true);
974 return true;
975 }
Douglas Gregor67b556a2009-02-09 19:34:22 +0000976
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000977 // Save this template argument.
978 TemplateArgs.push_back(Arg);
979
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000980 // If the next token is a comma, consume it and keep reading
981 // arguments.
982 if (Tok.isNot(tok::comma)) break;
983
984 // Consume the comma.
985 ConsumeToken();
986 }
987
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000988 return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000989}
990
Mike Stump11289f42009-09-09 15:08:12 +0000991/// \brief Parse a C++ explicit template instantiation
Douglas Gregor23996282009-05-12 21:31:51 +0000992/// (C++ [temp.explicit]).
993///
994/// explicit-instantiation:
Douglas Gregor43e75172009-09-04 06:33:52 +0000995/// 'extern' [opt] 'template' declaration
996///
997/// Note that the 'extern' is a GNU extension and C++0x feature.
Mike Stump11289f42009-09-09 15:08:12 +0000998Parser::DeclPtrTy
Douglas Gregor43e75172009-09-04 06:33:52 +0000999Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
1000 SourceLocation TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001001 SourceLocation &DeclEnd) {
Mike Stump11289f42009-09-09 15:08:12 +00001002 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
Douglas Gregor43e75172009-09-04 06:33:52 +00001003 ParsedTemplateInfo(ExternLoc,
1004 TemplateLoc),
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001005 DeclEnd, AS_none);
Douglas Gregor23996282009-05-12 21:31:51 +00001006}