blob: dd745d603ac7df16c31582a61031ead7f58d9b9c [file] [log] [blame]
Douglas Gregoradcac882008-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 Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregoradcac882008-12-01 23:54:00 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
Douglas Gregoradcac882008-12-01 23:54:00 +000018using namespace clang;
19
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000020/// \brief Parse a template declaration, explicit instantiation, or
21/// explicit specialization.
22Parser::DeclPtrTy
23Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
24 SourceLocation &DeclEnd,
25 AccessSpecifier AS) {
26 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less))
27 return ParseExplicitInstantiation(ConsumeToken(), DeclEnd);
28
29 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
30}
31
Douglas Gregorcc636682009-02-17 23:15:12 +000032/// \brief Parse a template declaration or an explicit specialization.
33///
34/// Template declarations include one or more template parameter lists
35/// and either the function or class template declaration. Explicit
36/// specializations contain one or more 'template < >' prefixes
37/// followed by a (possibly templated) declaration. Since the
38/// syntactic form of both features is nearly identical, we parse all
39/// of the template headers together and let semantic analysis sort
40/// the declarations from the explicit specializations.
Douglas Gregoradcac882008-12-01 23:54:00 +000041///
42/// template-declaration: [C++ temp]
43/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregorcc636682009-02-17 23:15:12 +000044///
45/// explicit-specialization: [ C++ temp.expl.spec]
46/// 'template' '<' '>' declaration
Chris Lattnerb28317a2009-03-28 19:18:32 +000047Parser::DeclPtrTy
Anders Carlsson5aeccdb2009-03-26 00:52:18 +000048Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +000049 SourceLocation &DeclEnd,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +000050 AccessSpecifier AS) {
Douglas Gregoradcac882008-12-01 23:54:00 +000051 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
52 "Token does not start a template declaration.");
53
Douglas Gregor26236e82008-12-02 00:41:28 +000054 // Enter template-parameter scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +000055 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregor26236e82008-12-02 00:41:28 +000056
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +000057 // Parse multiple levels of template headers within this template
58 // parameter scope, e.g.,
59 //
60 // template<typename T>
61 // template<typename U>
62 // class A<T>::B { ... };
63 //
64 // We parse multiple levels non-recursively so that we can build a
65 // single data structure containing all of the template parameter
Douglas Gregorcc636682009-02-17 23:15:12 +000066 // lists to easily differentiate between the case above and:
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +000067 //
68 // template<typename T>
69 // class A {
70 // template<typename U> class B;
71 // };
72 //
73 // In the first case, the action for declaring A<T>::B receives
74 // both template parameter lists. In the second case, the action for
75 // defining A<T>::B receives just the inner template parameter list
76 // (and retrieves the outer template parameter list from its
77 // context).
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000078 bool isSpecialiation = true;
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +000079 TemplateParameterLists ParamLists;
80 do {
81 // Consume the 'export', if any.
82 SourceLocation ExportLoc;
83 if (Tok.is(tok::kw_export)) {
84 ExportLoc = ConsumeToken();
85 }
86
87 // Consume the 'template', which should be here.
88 SourceLocation TemplateLoc;
89 if (Tok.is(tok::kw_template)) {
90 TemplateLoc = ConsumeToken();
91 } else {
92 Diag(Tok.getLocation(), diag::err_expected_template);
Chris Lattnerb28317a2009-03-28 19:18:32 +000093 return DeclPtrTy();
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +000094 }
95
96 // Parse the '<' template-parameter-list '>'
97 SourceLocation LAngleLoc, RAngleLoc;
98 TemplateParameterList TemplateParams;
Douglas Gregor7cdbc582009-07-22 23:48:44 +000099 if (ParseTemplateParameters(ParamLists.size(), TemplateParams, LAngleLoc,
100 RAngleLoc)) {
101 // Skip until the semi-colon or a }.
102 SkipUntil(tok::r_brace, true, true);
103 if (Tok.is(tok::semi))
104 ConsumeToken();
105 return DeclPtrTy();
106 }
107
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000108 if (!TemplateParams.empty())
109 isSpecialiation = false;
110
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000111 ParamLists.push_back(
112 Actions.ActOnTemplateParameterList(ParamLists.size(), ExportLoc,
113 TemplateLoc, LAngleLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000114 TemplateParams.data(),
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000115 TemplateParams.size(), RAngleLoc));
116 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
117
118 // Parse the actual template declaration.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000119 return ParseSingleDeclarationAfterTemplate(Context,
120 ParsedTemplateInfo(&ParamLists,
121 isSpecialiation),
Douglas Gregor1426e532009-05-12 21:31:51 +0000122 DeclEnd, AS);
123}
Chris Lattner682bf922009-03-29 16:50:03 +0000124
Douglas Gregor1426e532009-05-12 21:31:51 +0000125/// \brief Parse a single declaration that declares a template,
126/// template specialization, or explicit instantiation of a template.
127///
128/// \param TemplateParams if non-NULL, the template parameter lists
129/// that preceded this declaration. In this case, the declaration is a
130/// template declaration, out-of-line definition of a template, or an
131/// explicit template specialization. When NULL, the declaration is an
132/// explicit template instantiation.
133///
134/// \param TemplateLoc when TemplateParams is NULL, the location of
135/// the 'template' keyword that indicates that we have an explicit
136/// template instantiation.
137///
138/// \param DeclEnd will receive the source location of the last token
139/// within this declaration.
140///
141/// \param AS the access specifier associated with this
142/// declaration. Will be AS_none for namespace-scope declarations.
143///
144/// \returns the new declaration.
145Parser::DeclPtrTy
146Parser::ParseSingleDeclarationAfterTemplate(
147 unsigned Context,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000148 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor1426e532009-05-12 21:31:51 +0000149 SourceLocation &DeclEnd,
150 AccessSpecifier AS) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000151 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
152 "Template information required");
153
Douglas Gregor1426e532009-05-12 21:31:51 +0000154 // Parse the declaration specifiers.
155 DeclSpec DS;
156 // FIXME: Pass TemplateLoc through for explicit template instantiations
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000157 ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
Douglas Gregor1426e532009-05-12 21:31:51 +0000158
159 if (Tok.is(tok::semi)) {
160 DeclEnd = ConsumeToken();
161 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
162 }
163
164 // Parse the declarator.
165 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
166 ParseDeclarator(DeclaratorInfo);
167 // Error parsing the declarator?
168 if (!DeclaratorInfo.hasName()) {
169 // If so, skip until the semi-colon or a }.
170 SkipUntil(tok::r_brace, true, true);
171 if (Tok.is(tok::semi))
172 ConsumeToken();
173 return DeclPtrTy();
174 }
175
176 // If we have a declaration or declarator list, handle it.
177 if (isDeclarationAfterDeclarator()) {
178 // Parse this declaration.
Douglas Gregore542c862009-06-23 23:11:28 +0000179 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
180 TemplateInfo);
Douglas Gregor1426e532009-05-12 21:31:51 +0000181
182 if (Tok.is(tok::comma)) {
183 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000184 << (int)TemplateInfo.Kind;
Douglas Gregor1426e532009-05-12 21:31:51 +0000185 SkipUntil(tok::semi, true, false);
186 return ThisDecl;
187 }
188
189 // Eat the semi colon after the declaration.
John McCall5c15fe12009-07-31 02:20:35 +0000190 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
Douglas Gregor1426e532009-05-12 21:31:51 +0000191 return ThisDecl;
192 }
193
194 if (DeclaratorInfo.isFunctionDeclarator() &&
195 isStartOfFunctionDefinition()) {
196 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
197 Diag(Tok, diag::err_function_declared_typedef);
198
199 if (Tok.is(tok::l_brace)) {
200 // This recovery skips the entire function body. It would be nice
201 // to simply call ParseFunctionDefinition() below, however Sema
202 // assumes the declarator represents a function, not a typedef.
203 ConsumeBrace();
204 SkipUntil(tok::r_brace, true);
205 } else {
206 SkipUntil(tok::semi);
207 }
208 return DeclPtrTy();
209 }
Douglas Gregor52591bf2009-06-24 00:54:41 +0000210 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregor1426e532009-05-12 21:31:51 +0000211 }
212
213 if (DeclaratorInfo.isFunctionDeclarator())
214 Diag(Tok, diag::err_expected_fn_body);
215 else
216 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
217 SkipUntil(tok::semi);
218 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000219}
220
221/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000222/// angle brackets. Depth is the depth of this template-parameter-list, which
223/// is the number of template headers directly enclosing this template header.
224/// TemplateParams is the current list of template parameters we're building.
225/// The template parameter we parse will be added to this list. LAngleLoc and
226/// RAngleLoc will receive the positions of the '<' and '>', respectively,
227/// that enclose this template parameter list.
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000228///
229/// \returns true if an error occurred, false otherwise.
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000230bool Parser::ParseTemplateParameters(unsigned Depth,
231 TemplateParameterList &TemplateParams,
232 SourceLocation &LAngleLoc,
233 SourceLocation &RAngleLoc) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000234 // Get the template parameter list.
235 if(!Tok.is(tok::less)) {
236 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000237 return true;
Douglas Gregoradcac882008-12-01 23:54:00 +0000238 }
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000239 LAngleLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000240
241 // Try to parse the template parameter list.
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000242 if (Tok.is(tok::greater))
243 RAngleLoc = ConsumeToken();
244 else if(ParseTemplateParameterList(Depth, TemplateParams)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000245 if(!Tok.is(tok::greater)) {
246 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000247 return true;
Douglas Gregoradcac882008-12-01 23:54:00 +0000248 }
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000249 RAngleLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000250 }
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000251 return false;
Douglas Gregoradcac882008-12-01 23:54:00 +0000252}
253
254/// ParseTemplateParameterList - Parse a template parameter list. If
255/// the parsing fails badly (i.e., closing bracket was left out), this
256/// will try to put the token stream in a reasonable position (closing
257/// a statement, etc.) and return false.
258///
259/// template-parameter-list: [C++ temp]
260/// template-parameter
261/// template-parameter-list ',' template-parameter
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000262bool
263Parser::ParseTemplateParameterList(unsigned Depth,
264 TemplateParameterList &TemplateParams) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000265 while(1) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000266 if (DeclPtrTy TmpParam
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000267 = ParseTemplateParameter(Depth, TemplateParams.size())) {
268 TemplateParams.push_back(TmpParam);
269 } else {
Douglas Gregoradcac882008-12-01 23:54:00 +0000270 // If we failed to parse a template parameter, skip until we find
271 // a comma or closing brace.
272 SkipUntil(tok::comma, tok::greater, true, true);
273 }
274
275 // Did we find a comma or the end of the template parmeter list?
276 if(Tok.is(tok::comma)) {
277 ConsumeToken();
278 } else if(Tok.is(tok::greater)) {
279 // Don't consume this... that's done by template parser.
280 break;
281 } else {
282 // Somebody probably forgot to close the template. Skip ahead and
283 // try to get out of the expression. This error is currently
284 // subsumed by whatever goes on in ParseTemplateParameter.
285 // TODO: This could match >>, and it would be nice to avoid those
286 // silly errors with template <vec<T>>.
287 // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
288 SkipUntil(tok::greater, true, true);
289 return false;
290 }
291 }
292 return true;
293}
294
295/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
296///
297/// template-parameter: [C++ temp.param]
298/// type-parameter
299/// parameter-declaration
300///
301/// type-parameter: (see below)
Anders Carlssonce5635a2009-06-12 23:09:56 +0000302/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000303/// 'class' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000304/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000305/// 'typename' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000306/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000307/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattnerb28317a2009-03-28 19:18:32 +0000308Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000309Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Chris Lattner532e19b2009-01-04 23:51:17 +0000310 if(Tok.is(tok::kw_class) ||
311 (Tok.is(tok::kw_typename) &&
312 // FIXME: Next token has not been annotated!
Chris Lattnerb31757b2009-01-06 05:06:21 +0000313 NextToken().isNot(tok::annot_typename))) {
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000314 return ParseTypeParameter(Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000315 }
Chris Lattner532e19b2009-01-04 23:51:17 +0000316
317 if(Tok.is(tok::kw_template))
318 return ParseTemplateTemplateParameter(Depth, Position);
319
320 // If it's none of the above, then it must be a parameter declaration.
321 // NOTE: This will pick up errors in the closure of the template parameter
322 // list (e.g., template < ; Check here to implement >> style closures.
323 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000324}
325
326/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
327/// Other kinds of template parameters are parsed in
328/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
329///
330/// type-parameter: [C++ temp.param]
Anders Carlssonce5635a2009-06-12 23:09:56 +0000331/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000332/// 'class' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000333/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000334/// 'typename' identifier[opt] '=' type-id
Chris Lattnerb28317a2009-03-28 19:18:32 +0000335Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregor26236e82008-12-02 00:41:28 +0000336 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
337 "A type-parameter starts with 'class' or 'typename'");
338
339 // Consume the 'class' or 'typename' keyword.
340 bool TypenameKeyword = Tok.is(tok::kw_typename);
341 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000342
Anders Carlsson941df7d2009-06-12 19:58:00 +0000343 // Grab the ellipsis (if given).
344 bool Ellipsis = false;
345 SourceLocation EllipsisLoc;
Anders Carlssonce5635a2009-06-12 23:09:56 +0000346 if (Tok.is(tok::ellipsis)) {
Anders Carlsson941df7d2009-06-12 19:58:00 +0000347 Ellipsis = true;
348 EllipsisLoc = ConsumeToken();
Anders Carlssonce5635a2009-06-12 23:09:56 +0000349
350 if (!getLang().CPlusPlus0x)
351 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson941df7d2009-06-12 19:58:00 +0000352 }
353
Douglas Gregoradcac882008-12-01 23:54:00 +0000354 // Grab the template parameter name (if given)
Douglas Gregor26236e82008-12-02 00:41:28 +0000355 SourceLocation NameLoc;
356 IdentifierInfo* ParamName = 0;
Douglas Gregoradcac882008-12-01 23:54:00 +0000357 if(Tok.is(tok::identifier)) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000358 ParamName = Tok.getIdentifierInfo();
359 NameLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000360 } else if(Tok.is(tok::equal) || Tok.is(tok::comma) ||
361 Tok.is(tok::greater)) {
362 // Unnamed template parameter. Don't have to do anything here, just
363 // don't consume this token.
364 } else {
365 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000366 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000367 }
368
Chris Lattnerb28317a2009-03-28 19:18:32 +0000369 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson941df7d2009-06-12 19:58:00 +0000370 Ellipsis, EllipsisLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000371 KeyLoc, ParamName, NameLoc,
372 Depth, Position);
Douglas Gregor26236e82008-12-02 00:41:28 +0000373
Douglas Gregoradcac882008-12-01 23:54:00 +0000374 // Grab a default type id (if given).
Douglas Gregoradcac882008-12-01 23:54:00 +0000375 if(Tok.is(tok::equal)) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000376 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregord684b002009-02-10 19:49:53 +0000377 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +0000378 TypeResult DefaultType = ParseTypeName();
379 if (!DefaultType.isInvalid())
Douglas Gregord684b002009-02-10 19:49:53 +0000380 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +0000381 DefaultType.get());
Douglas Gregoradcac882008-12-01 23:54:00 +0000382 }
383
Douglas Gregor26236e82008-12-02 00:41:28 +0000384 return TypeParam;
Douglas Gregoradcac882008-12-01 23:54:00 +0000385}
386
387/// ParseTemplateTemplateParameter - Handle the parsing of template
388/// template parameters.
389///
390/// type-parameter: [C++ temp.param]
391/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
392/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattnerb28317a2009-03-28 19:18:32 +0000393Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000394Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000395 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
396
397 // Handle the template <...> part.
398 SourceLocation TemplateLoc = ConsumeToken();
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000399 TemplateParameterList TemplateParams;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000400 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor68c69932009-02-10 19:52:54 +0000401 {
402 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000403 if(ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
404 RAngleLoc)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000405 return DeclPtrTy();
Douglas Gregor68c69932009-02-10 19:52:54 +0000406 }
Douglas Gregoradcac882008-12-01 23:54:00 +0000407 }
408
409 // Generate a meaningful error if the user forgot to put class before the
410 // identifier, comma, or greater.
411 if(!Tok.is(tok::kw_class)) {
412 Diag(Tok.getLocation(), diag::err_expected_class_before)
413 << PP.getSpelling(Tok);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000414 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000415 }
416 SourceLocation ClassLoc = ConsumeToken();
417
418 // Get the identifier, if given.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000419 SourceLocation NameLoc;
420 IdentifierInfo* ParamName = 0;
Douglas Gregoradcac882008-12-01 23:54:00 +0000421 if(Tok.is(tok::identifier)) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000422 ParamName = Tok.getIdentifierInfo();
423 NameLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000424 } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
425 // Unnamed template parameter. Don't have to do anything here, just
426 // don't consume this token.
427 } else {
428 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000429 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000430 }
431
Douglas Gregorddc29e12009-02-06 22:42:48 +0000432 TemplateParamsTy *ParamList =
433 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
434 TemplateLoc, LAngleLoc,
435 &TemplateParams[0],
436 TemplateParams.size(),
437 RAngleLoc);
438
Chris Lattnerb28317a2009-03-28 19:18:32 +0000439 Parser::DeclPtrTy Param
Douglas Gregord684b002009-02-10 19:49:53 +0000440 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
441 ParamList, ParamName,
442 NameLoc, Depth, Position);
443
444 // Get the a default value, if given.
445 if (Tok.is(tok::equal)) {
446 SourceLocation EqualLoc = ConsumeToken();
447 OwningExprResult DefaultExpr = ParseCXXIdExpression();
448 if (DefaultExpr.isInvalid())
449 return Param;
450 else if (Param)
451 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc,
452 move(DefaultExpr));
453 }
454
455 return Param;
Douglas Gregoradcac882008-12-01 23:54:00 +0000456}
457
458/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
459/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000460///
Douglas Gregoradcac882008-12-01 23:54:00 +0000461/// template-parameter:
462/// ...
463/// parameter-declaration
464///
465/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
466/// but that didn't work out to well. Instead, this tries to recrate the basic
467/// parsing of parameter declarations, but tries to constrain it for template
468/// parameters.
Douglas Gregor26236e82008-12-02 00:41:28 +0000469/// FIXME: We need to make a ParseParameterDeclaration that works for
470/// non-type template parameters and normal function parameters.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000471Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000472Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000473 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregoradcac882008-12-01 23:54:00 +0000474
475 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregor26236e82008-12-02 00:41:28 +0000476 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregoradcac882008-12-01 23:54:00 +0000477 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregor26236e82008-12-02 00:41:28 +0000478 DeclSpec DS;
479 ParseDeclarationSpecifiers(DS);
Douglas Gregoradcac882008-12-01 23:54:00 +0000480
481 // Parse this as a typename.
Douglas Gregor26236e82008-12-02 00:41:28 +0000482 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
483 ParseDeclarator(ParamDecl);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000484 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000485 // This probably shouldn't happen - and it's more of a Sema thing, but
486 // basically we didn't parse the type name because we couldn't associate
487 // it with an AST node. we should just skip to the comma or greater.
488 // TODO: This is currently a placeholder for some kind of Sema Error.
489 Diag(Tok.getLocation(), diag::err_parse_error);
490 SkipUntil(tok::comma, tok::greater, true, true);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000491 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000492 }
493
Douglas Gregor26236e82008-12-02 00:41:28 +0000494 // Create the parameter.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000495 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
496 Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000497
Douglas Gregord684b002009-02-10 19:49:53 +0000498 // If there is a default value, parse it.
Chris Lattner7452c6f2009-01-05 01:24:05 +0000499 if (Tok.is(tok::equal)) {
Douglas Gregord684b002009-02-10 19:49:53 +0000500 SourceLocation EqualLoc = ConsumeToken();
501
502 // C++ [temp.param]p15:
503 // When parsing a default template-argument for a non-type
504 // template-parameter, the first non-nested > is taken as the
505 // end of the template-parameter-list rather than a greater-than
506 // operator.
507 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
508
509 OwningExprResult DefaultArg = ParseAssignmentExpression();
510 if (DefaultArg.isInvalid())
511 SkipUntil(tok::comma, tok::greater, true, true);
512 else if (Param)
513 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
514 move(DefaultArg));
Douglas Gregoradcac882008-12-01 23:54:00 +0000515 }
516
Douglas Gregor26236e82008-12-02 00:41:28 +0000517 return Param;
Douglas Gregoradcac882008-12-01 23:54:00 +0000518}
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000519
Douglas Gregorcc636682009-02-17 23:15:12 +0000520/// \brief Parses a template-id that after the template name has
521/// already been parsed.
522///
523/// This routine takes care of parsing the enclosed template argument
524/// list ('<' template-parameter-list [opt] '>') and placing the
525/// results into a form that can be transferred to semantic analysis.
526///
527/// \param Template the template declaration produced by isTemplateName
528///
529/// \param TemplateNameLoc the source location of the template name
530///
531/// \param SS if non-NULL, the nested-name-specifier preceding the
532/// template name.
533///
534/// \param ConsumeLastToken if true, then we will consume the last
535/// token that forms the template-id. Otherwise, we will leave the
536/// last token in the stream (e.g., so that it can be replaced with an
537/// annotation token).
538bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000539Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Douglas Gregorcc636682009-02-17 23:15:12 +0000540 SourceLocation TemplateNameLoc,
541 const CXXScopeSpec *SS,
542 bool ConsumeLastToken,
543 SourceLocation &LAngleLoc,
544 TemplateArgList &TemplateArgs,
545 TemplateArgIsTypeList &TemplateArgIsType,
546 TemplateArgLocationList &TemplateArgLocations,
547 SourceLocation &RAngleLoc) {
548 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
549
550 // Consume the '<'.
551 LAngleLoc = ConsumeToken();
552
553 // Parse the optional template-argument-list.
554 bool Invalid = false;
555 {
556 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
557 if (Tok.isNot(tok::greater))
558 Invalid = ParseTemplateArgumentList(TemplateArgs, TemplateArgIsType,
559 TemplateArgLocations);
560
561 if (Invalid) {
562 // Try to find the closing '>'.
563 SkipUntil(tok::greater, true, !ConsumeLastToken);
564
565 return true;
566 }
567 }
568
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000569 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
Douglas Gregorcc636682009-02-17 23:15:12 +0000570 return true;
571
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000572 // Determine the location of the '>' or '>>'. Only consume this
573 // token if the caller asked us to.
Douglas Gregorcc636682009-02-17 23:15:12 +0000574 RAngleLoc = Tok.getLocation();
575
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000576 if (Tok.is(tok::greatergreater)) {
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000577 if (!getLang().CPlusPlus0x) {
578 const char *ReplaceStr = "> >";
579 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
580 ReplaceStr = "> > ";
581
582 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregorb2fb6de2009-02-27 17:53:17 +0000583 << CodeModificationHint::CreateReplacement(
584 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000585 }
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000586
587 Tok.setKind(tok::greater);
588 if (!ConsumeLastToken) {
589 // Since we're not supposed to consume the '>>' token, we need
590 // to insert a second '>' token after the first.
591 PP.EnterToken(Tok);
592 }
593 } else if (ConsumeLastToken)
Douglas Gregorcc636682009-02-17 23:15:12 +0000594 ConsumeToken();
595
596 return false;
597}
598
Douglas Gregor39a8de12009-02-25 19:37:18 +0000599/// \brief Replace the tokens that form a simple-template-id with an
600/// annotation token containing the complete template-id.
601///
602/// The first token in the stream must be the name of a template that
603/// is followed by a '<'. This routine will parse the complete
604/// simple-template-id and replace the tokens with a single annotation
605/// token with one of two different kinds: if the template-id names a
606/// type (and \p AllowTypeAnnotation is true), the annotation token is
607/// a type annotation that includes the optional nested-name-specifier
608/// (\p SS). Otherwise, the annotation token is a template-id
609/// annotation that does not include the optional
610/// nested-name-specifier.
611///
612/// \param Template the declaration of the template named by the first
613/// token (an identifier), as returned from \c Action::isTemplateName().
614///
615/// \param TemplateNameKind the kind of template that \p Template
616/// refers to, as returned from \c Action::isTemplateName().
617///
618/// \param SS if non-NULL, the nested-name-specifier that precedes
619/// this template name.
620///
621/// \param TemplateKWLoc if valid, specifies that this template-id
622/// annotation was preceded by the 'template' keyword and gives the
623/// location of that keyword. If invalid (the default), then this
624/// template-id was not preceded by a 'template' keyword.
625///
626/// \param AllowTypeAnnotation if true (the default), then a
627/// simple-template-id that refers to a class template, template
628/// template parameter, or other template that produces a type will be
629/// replaced with a type annotation token. Otherwise, the
630/// simple-template-id is always replaced with a template-id
631/// annotation token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000632///
633/// If an unrecoverable parse error occurs and no annotation token can be
634/// formed, this function returns true.
635///
636bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000637 const CXXScopeSpec *SS,
638 SourceLocation TemplateKWLoc,
639 bool AllowTypeAnnotation) {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000640 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
641 assert(Template && Tok.is(tok::identifier) && NextToken().is(tok::less) &&
642 "Parser isn't at the beginning of a template-id");
643
644 // Consume the template-name.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000645 IdentifierInfo *Name = Tok.getIdentifierInfo();
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000646 SourceLocation TemplateNameLoc = ConsumeToken();
647
Douglas Gregorcc636682009-02-17 23:15:12 +0000648 // Parse the enclosed template argument list.
649 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000650 TemplateArgList TemplateArgs;
651 TemplateArgIsTypeList TemplateArgIsType;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000652 TemplateArgLocationList TemplateArgLocations;
Douglas Gregorcc636682009-02-17 23:15:12 +0000653 bool Invalid = ParseTemplateIdAfterTemplateName(Template, TemplateNameLoc,
654 SS, false, LAngleLoc,
655 TemplateArgs,
656 TemplateArgIsType,
657 TemplateArgLocations,
658 RAngleLoc);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000659
660 if (Invalid) {
661 // If we failed to parse the template ID but skipped ahead to a >, we're not
662 // going to be able to form a token annotation. Eat the '>' if present.
663 if (Tok.is(tok::greater))
664 ConsumeToken();
665 return true;
666 }
Douglas Gregorc15cb382009-02-09 23:23:08 +0000667
Jay Foadbeaaccd2009-05-21 09:52:38 +0000668 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
669 TemplateArgIsType.data(),
Douglas Gregorcc636682009-02-17 23:15:12 +0000670 TemplateArgs.size());
Douglas Gregorf02da892009-02-09 21:04:56 +0000671
Douglas Gregor55f6b142009-02-09 18:46:07 +0000672 // Build the annotation token.
Douglas Gregorc45c2322009-03-31 00:43:58 +0000673 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000674 Action::TypeResult Type
Douglas Gregor7532dc62009-03-30 22:58:21 +0000675 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
676 LAngleLoc, TemplateArgsPtr,
677 &TemplateArgLocations[0],
678 RAngleLoc);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000679 if (Type.isInvalid()) {
680 // If we failed to parse the template ID but skipped ahead to a >, we're not
681 // going to be able to form a token annotation. Eat the '>' if present.
682 if (Tok.is(tok::greater))
683 ConsumeToken();
684 return true;
685 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000686
687 Tok.setKind(tok::annot_typename);
688 Tok.setAnnotationValue(Type.get());
Douglas Gregor39a8de12009-02-25 19:37:18 +0000689 if (SS && SS->isNotEmpty())
690 Tok.setLocation(SS->getBeginLoc());
691 else if (TemplateKWLoc.isValid())
692 Tok.setLocation(TemplateKWLoc);
693 else
694 Tok.setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +0000695 } else {
Douglas Gregorc45c2322009-03-31 00:43:58 +0000696 // Build a template-id annotation token that can be processed
697 // later.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000698 Tok.setKind(tok::annot_template_id);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000699 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000700 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor55f6b142009-02-09 18:46:07 +0000701 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000702 TemplateId->Name = Name;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000703 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000704 TemplateId->Kind = TNK;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000705 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000706 TemplateId->RAngleLoc = RAngleLoc;
707 void **Args = TemplateId->getTemplateArgs();
708 bool *ArgIsType = TemplateId->getTemplateArgIsType();
709 SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations();
710 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000711 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor39a8de12009-02-25 19:37:18 +0000712 ArgIsType[Arg] = TemplateArgIsType[Arg];
713 ArgLocs[Arg] = TemplateArgLocations[Arg];
714 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000715 Tok.setAnnotationValue(TemplateId);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000716 if (TemplateKWLoc.isValid())
717 Tok.setLocation(TemplateKWLoc);
718 else
719 Tok.setLocation(TemplateNameLoc);
720
721 TemplateArgsPtr.release();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000722 }
723
724 // Common fields for the annotation token
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000725 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000726
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000727 // In case the tokens were cached, have Preprocessor replace them with the
728 // annotation token.
729 PP.AnnotateCachedTokens(Tok);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000730 return false;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000731}
732
Douglas Gregor39a8de12009-02-25 19:37:18 +0000733/// \brief Replaces a template-id annotation token with a type
734/// annotation token.
735///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000736/// If there was a failure when forming the type from the template-id,
737/// a type annotation token will still be created, but will have a
738/// NULL type pointer to signify an error.
739void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000740 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
741
742 TemplateIdAnnotation *TemplateId
743 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000744 assert((TemplateId->Kind == TNK_Type_template ||
745 TemplateId->Kind == TNK_Dependent_template_name) &&
746 "Only works for type and dependent templates");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000747
748 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
749 TemplateId->getTemplateArgs(),
750 TemplateId->getTemplateArgIsType(),
751 TemplateId->NumArgs);
752
753 Action::TypeResult Type
Douglas Gregor7532dc62009-03-30 22:58:21 +0000754 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
755 TemplateId->TemplateNameLoc,
756 TemplateId->LAngleLoc,
757 TemplateArgsPtr,
758 TemplateId->getTemplateArgLocations(),
759 TemplateId->RAngleLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000760 // Create the new "type" annotation token.
761 Tok.setKind(tok::annot_typename);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000762 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor39a8de12009-02-25 19:37:18 +0000763 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
764 Tok.setLocation(SS->getBeginLoc());
765
766 // We might be backtracking, in which case we need to replace the
767 // template-id annotation token with the type annotation within the
768 // set of cached tokens. That way, we won't try to form the same
769 // class template specialization again.
770 PP.ReplaceLastTokenWithAnnotation(Tok);
771 TemplateId->Destroy();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000772}
773
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000774/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
775///
776/// template-argument: [C++ 14.2]
Douglas Gregorac7610d2009-06-22 20:57:11 +0000777/// constant-expression
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000778/// type-id
779/// id-expression
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000780void *Parser::ParseTemplateArgument(bool &ArgIsType) {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000781 // C++ [temp.arg]p2:
782 // In a template-argument, an ambiguity between a type-id and an
783 // expression is resolved to a type-id, regardless of the form of
784 // the corresponding template-parameter.
785 //
786 // Therefore, we initially try to parse a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000787 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000788 ArgIsType = true;
Douglas Gregor809070a2009-02-18 17:45:20 +0000789 TypeResult TypeArg = ParseTypeName();
790 if (TypeArg.isInvalid())
791 return 0;
792 return TypeArg.get();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000793 }
794
Douglas Gregorac7610d2009-06-22 20:57:11 +0000795 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregorc15cb382009-02-09 23:23:08 +0000796 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000797 return 0;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000798
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000799 ArgIsType = false;
800 return ExprArg.release();
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000801}
802
803/// ParseTemplateArgumentList - Parse a C++ template-argument-list
804/// (C++ [temp.names]). Returns true if there was an error.
805///
806/// template-argument-list: [C++ 14.2]
807/// template-argument
808/// template-argument-list ',' template-argument
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000809bool
810Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs,
Douglas Gregorc15cb382009-02-09 23:23:08 +0000811 TemplateArgIsTypeList &TemplateArgIsType,
812 TemplateArgLocationList &TemplateArgLocations) {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000813 while (true) {
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000814 bool IsType = false;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000815 SourceLocation Loc = Tok.getLocation();
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000816 void *Arg = ParseTemplateArgument(IsType);
817 if (Arg) {
818 TemplateArgs.push_back(Arg);
819 TemplateArgIsType.push_back(IsType);
Douglas Gregorc15cb382009-02-09 23:23:08 +0000820 TemplateArgLocations.push_back(Loc);
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000821 } else {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000822 SkipUntil(tok::comma, tok::greater, true, true);
823 return true;
824 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000825
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000826 // If the next token is a comma, consume it and keep reading
827 // arguments.
828 if (Tok.isNot(tok::comma)) break;
829
830 // Consume the comma.
831 ConsumeToken();
832 }
833
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000834 return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater);
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000835}
836
Douglas Gregor1426e532009-05-12 21:31:51 +0000837/// \brief Parse a C++ explicit template instantiation
838/// (C++ [temp.explicit]).
839///
840/// explicit-instantiation:
841/// 'template' declaration
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000842Parser::DeclPtrTy
843Parser::ParseExplicitInstantiation(SourceLocation TemplateLoc,
844 SourceLocation &DeclEnd) {
845 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
846 ParsedTemplateInfo(TemplateLoc),
847 DeclEnd, AS_none);
Douglas Gregor1426e532009-05-12 21:31:51 +0000848}