blob: b2b7ed06e223e87bc892b21d2ebef26bc34b84cc [file] [log] [blame]
Douglas Gregorb3bec712008-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 Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregorb3bec712008-12-01 23:54:00 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
Douglas Gregorb3bec712008-12-01 23:54:00 +000018using namespace clang;
19
Douglas Gregora9db0fa2009-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 Gregora08b6c72009-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 Gregorb3bec712008-12-01 23:54:00 +000041///
42/// template-declaration: [C++ temp]
43/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregora08b6c72009-02-17 23:15:12 +000044///
45/// explicit-specialization: [ C++ temp.expl.spec]
46/// 'template' '<' '>' declaration
Chris Lattner5261d0c2009-03-28 19:18:32 +000047Parser::DeclPtrTy
Anders Carlssoned20fb92009-03-26 00:52:18 +000048Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +000049 SourceLocation &DeclEnd,
Anders Carlssoned20fb92009-03-26 00:52:18 +000050 AccessSpecifier AS) {
Douglas Gregorb3bec712008-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 Gregor8e7f9572008-12-02 00:41:28 +000054 // Enter template-parameter scope.
Douglas Gregor95d40792008-12-10 06:34:36 +000055 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregor8e7f9572008-12-02 00:41:28 +000056
Douglas Gregor52473432008-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 Gregora08b6c72009-02-17 23:15:12 +000066 // lists to easily differentiate between the case above and:
Douglas Gregor52473432008-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 Gregord022d712009-08-20 18:46:05 +000078 bool isSpecialization = true;
Douglas Gregor52473432008-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 Lattner5261d0c2009-03-28 19:18:32 +000093 return DeclPtrTy();
Douglas Gregor52473432008-12-24 02:52:09 +000094 }
95
96 // Parse the '<' template-parameter-list '>'
97 SourceLocation LAngleLoc, RAngleLoc;
98 TemplateParameterList TemplateParams;
Douglas Gregor84a20812009-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 Gregora9db0fa2009-05-12 23:25:50 +0000108 if (!TemplateParams.empty())
Douglas Gregord022d712009-08-20 18:46:05 +0000109 isSpecialization = false;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000110
Douglas Gregor52473432008-12-24 02:52:09 +0000111 ParamLists.push_back(
112 Actions.ActOnTemplateParameterList(ParamLists.size(), ExportLoc,
113 TemplateLoc, LAngleLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +0000114 TemplateParams.data(),
Douglas Gregor52473432008-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 Gregora9db0fa2009-05-12 23:25:50 +0000119 return ParseSingleDeclarationAfterTemplate(Context,
120 ParsedTemplateInfo(&ParamLists,
Douglas Gregord022d712009-08-20 18:46:05 +0000121 isSpecialization),
Douglas Gregore3298aa2009-05-12 21:31:51 +0000122 DeclEnd, AS);
123}
Chris Lattnera17991f2009-03-29 16:50:03 +0000124
Douglas Gregore3298aa2009-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 Gregora9db0fa2009-05-12 23:25:50 +0000148 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregore3298aa2009-05-12 21:31:51 +0000149 SourceLocation &DeclEnd,
150 AccessSpecifier AS) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000151 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
152 "Template information required");
153
Douglas Gregor398a8012009-08-20 22:52:58 +0000154 if (Context == Declarator::MemberContext) {
155 // We are parsing a member template.
156 ParseCXXClassMemberDeclaration(AS, TemplateInfo);
157 return DeclPtrTy::make((void*)0);
158 }
159
Douglas Gregore3298aa2009-05-12 21:31:51 +0000160 // Parse the declaration specifiers.
161 DeclSpec DS;
162 // FIXME: Pass TemplateLoc through for explicit template instantiations
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000163 ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000164
165 if (Tok.is(tok::semi)) {
166 DeclEnd = ConsumeToken();
167 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
168 }
169
170 // Parse the declarator.
171 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
172 ParseDeclarator(DeclaratorInfo);
173 // Error parsing the declarator?
174 if (!DeclaratorInfo.hasName()) {
175 // If so, skip until the semi-colon or a }.
176 SkipUntil(tok::r_brace, true, true);
177 if (Tok.is(tok::semi))
178 ConsumeToken();
179 return DeclPtrTy();
180 }
181
182 // If we have a declaration or declarator list, handle it.
183 if (isDeclarationAfterDeclarator()) {
184 // Parse this declaration.
Douglas Gregor2ae1d772009-06-23 23:11:28 +0000185 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
186 TemplateInfo);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000187
188 if (Tok.is(tok::comma)) {
189 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000190 << (int)TemplateInfo.Kind;
Douglas Gregore3298aa2009-05-12 21:31:51 +0000191 SkipUntil(tok::semi, true, false);
192 return ThisDecl;
193 }
194
195 // Eat the semi colon after the declaration.
John McCallfcb32f42009-07-31 02:20:35 +0000196 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000197 return ThisDecl;
198 }
199
200 if (DeclaratorInfo.isFunctionDeclarator() &&
201 isStartOfFunctionDefinition()) {
202 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
203 Diag(Tok, diag::err_function_declared_typedef);
204
205 if (Tok.is(tok::l_brace)) {
206 // This recovery skips the entire function body. It would be nice
207 // to simply call ParseFunctionDefinition() below, however Sema
208 // assumes the declarator represents a function, not a typedef.
209 ConsumeBrace();
210 SkipUntil(tok::r_brace, true);
211 } else {
212 SkipUntil(tok::semi);
213 }
214 return DeclPtrTy();
215 }
Douglas Gregor19d10652009-06-24 00:54:41 +0000216 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000217 }
218
219 if (DeclaratorInfo.isFunctionDeclarator())
220 Diag(Tok, diag::err_expected_fn_body);
221 else
222 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
223 SkipUntil(tok::semi);
224 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000225}
226
227/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregor279272e2009-02-04 19:02:06 +0000228/// angle brackets. Depth is the depth of this template-parameter-list, which
229/// is the number of template headers directly enclosing this template header.
230/// TemplateParams is the current list of template parameters we're building.
231/// The template parameter we parse will be added to this list. LAngleLoc and
232/// RAngleLoc will receive the positions of the '<' and '>', respectively,
233/// that enclose this template parameter list.
Douglas Gregor84a20812009-07-22 23:48:44 +0000234///
235/// \returns true if an error occurred, false otherwise.
Douglas Gregor52473432008-12-24 02:52:09 +0000236bool Parser::ParseTemplateParameters(unsigned Depth,
237 TemplateParameterList &TemplateParams,
238 SourceLocation &LAngleLoc,
239 SourceLocation &RAngleLoc) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000240 // Get the template parameter list.
241 if(!Tok.is(tok::less)) {
242 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregor84a20812009-07-22 23:48:44 +0000243 return true;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000244 }
Douglas Gregor52473432008-12-24 02:52:09 +0000245 LAngleLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000246
247 // Try to parse the template parameter list.
Douglas Gregor52473432008-12-24 02:52:09 +0000248 if (Tok.is(tok::greater))
249 RAngleLoc = ConsumeToken();
250 else if(ParseTemplateParameterList(Depth, TemplateParams)) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000251 if(!Tok.is(tok::greater)) {
252 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregor84a20812009-07-22 23:48:44 +0000253 return true;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000254 }
Douglas Gregor52473432008-12-24 02:52:09 +0000255 RAngleLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000256 }
Douglas Gregor84a20812009-07-22 23:48:44 +0000257 return false;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000258}
259
260/// ParseTemplateParameterList - Parse a template parameter list. If
261/// the parsing fails badly (i.e., closing bracket was left out), this
262/// will try to put the token stream in a reasonable position (closing
263/// a statement, etc.) and return false.
264///
265/// template-parameter-list: [C++ temp]
266/// template-parameter
267/// template-parameter-list ',' template-parameter
Douglas Gregor52473432008-12-24 02:52:09 +0000268bool
269Parser::ParseTemplateParameterList(unsigned Depth,
270 TemplateParameterList &TemplateParams) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000271 while(1) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000272 if (DeclPtrTy TmpParam
Douglas Gregor52473432008-12-24 02:52:09 +0000273 = ParseTemplateParameter(Depth, TemplateParams.size())) {
274 TemplateParams.push_back(TmpParam);
275 } else {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000276 // If we failed to parse a template parameter, skip until we find
277 // a comma or closing brace.
278 SkipUntil(tok::comma, tok::greater, true, true);
279 }
280
281 // Did we find a comma or the end of the template parmeter list?
282 if(Tok.is(tok::comma)) {
283 ConsumeToken();
284 } else if(Tok.is(tok::greater)) {
285 // Don't consume this... that's done by template parser.
286 break;
287 } else {
288 // Somebody probably forgot to close the template. Skip ahead and
289 // try to get out of the expression. This error is currently
290 // subsumed by whatever goes on in ParseTemplateParameter.
291 // TODO: This could match >>, and it would be nice to avoid those
292 // silly errors with template <vec<T>>.
293 // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
294 SkipUntil(tok::greater, true, true);
295 return false;
296 }
297 }
298 return true;
299}
300
301/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
302///
303/// template-parameter: [C++ temp.param]
304/// type-parameter
305/// parameter-declaration
306///
307/// type-parameter: (see below)
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000308/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000309/// 'class' identifier[opt] '=' type-id
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000310/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000311/// 'typename' identifier[opt] '=' type-id
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000312/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000313/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattner5261d0c2009-03-28 19:18:32 +0000314Parser::DeclPtrTy
Douglas Gregor52473432008-12-24 02:52:09 +0000315Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Chris Lattner4e7a4202009-01-04 23:51:17 +0000316 if(Tok.is(tok::kw_class) ||
317 (Tok.is(tok::kw_typename) &&
318 // FIXME: Next token has not been annotated!
Chris Lattner5d7eace2009-01-06 05:06:21 +0000319 NextToken().isNot(tok::annot_typename))) {
Douglas Gregor52473432008-12-24 02:52:09 +0000320 return ParseTypeParameter(Depth, Position);
Douglas Gregorb3bec712008-12-01 23:54:00 +0000321 }
Chris Lattner4e7a4202009-01-04 23:51:17 +0000322
323 if(Tok.is(tok::kw_template))
324 return ParseTemplateTemplateParameter(Depth, Position);
325
326 // If it's none of the above, then it must be a parameter declaration.
327 // NOTE: This will pick up errors in the closure of the template parameter
328 // list (e.g., template < ; Check here to implement >> style closures.
329 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregorb3bec712008-12-01 23:54:00 +0000330}
331
332/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
333/// Other kinds of template parameters are parsed in
334/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
335///
336/// type-parameter: [C++ temp.param]
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000337/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000338/// 'class' identifier[opt] '=' type-id
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000339/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000340/// 'typename' identifier[opt] '=' type-id
Chris Lattner5261d0c2009-03-28 19:18:32 +0000341Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000342 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
343 "A type-parameter starts with 'class' or 'typename'");
344
345 // Consume the 'class' or 'typename' keyword.
346 bool TypenameKeyword = Tok.is(tok::kw_typename);
347 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000348
Anders Carlsson9c85fa02009-06-12 19:58:00 +0000349 // Grab the ellipsis (if given).
350 bool Ellipsis = false;
351 SourceLocation EllipsisLoc;
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000352 if (Tok.is(tok::ellipsis)) {
Anders Carlsson9c85fa02009-06-12 19:58:00 +0000353 Ellipsis = true;
354 EllipsisLoc = ConsumeToken();
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000355
356 if (!getLang().CPlusPlus0x)
357 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson9c85fa02009-06-12 19:58:00 +0000358 }
359
Douglas Gregorb3bec712008-12-01 23:54:00 +0000360 // Grab the template parameter name (if given)
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000361 SourceLocation NameLoc;
362 IdentifierInfo* ParamName = 0;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000363 if(Tok.is(tok::identifier)) {
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000364 ParamName = Tok.getIdentifierInfo();
365 NameLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000366 } else if(Tok.is(tok::equal) || Tok.is(tok::comma) ||
367 Tok.is(tok::greater)) {
368 // Unnamed template parameter. Don't have to do anything here, just
369 // don't consume this token.
370 } else {
371 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000372 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000373 }
374
Chris Lattner5261d0c2009-03-28 19:18:32 +0000375 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson9c85fa02009-06-12 19:58:00 +0000376 Ellipsis, EllipsisLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000377 KeyLoc, ParamName, NameLoc,
378 Depth, Position);
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000379
Douglas Gregorb3bec712008-12-01 23:54:00 +0000380 // Grab a default type id (if given).
Douglas Gregorb3bec712008-12-01 23:54:00 +0000381 if(Tok.is(tok::equal)) {
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000382 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000383 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000384 TypeResult DefaultType = ParseTypeName();
385 if (!DefaultType.isInvalid())
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000386 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000387 DefaultType.get());
Douglas Gregorb3bec712008-12-01 23:54:00 +0000388 }
389
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000390 return TypeParam;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000391}
392
393/// ParseTemplateTemplateParameter - Handle the parsing of template
394/// template parameters.
395///
396/// type-parameter: [C++ temp.param]
397/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
398/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattner5261d0c2009-03-28 19:18:32 +0000399Parser::DeclPtrTy
Douglas Gregor52473432008-12-24 02:52:09 +0000400Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000401 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
402
403 // Handle the template <...> part.
404 SourceLocation TemplateLoc = ConsumeToken();
Douglas Gregor52473432008-12-24 02:52:09 +0000405 TemplateParameterList TemplateParams;
Douglas Gregord406b032009-02-06 22:42:48 +0000406 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor77f7ced2009-02-10 19:52:54 +0000407 {
408 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregor84a20812009-07-22 23:48:44 +0000409 if(ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
410 RAngleLoc)) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000411 return DeclPtrTy();
Douglas Gregor77f7ced2009-02-10 19:52:54 +0000412 }
Douglas Gregorb3bec712008-12-01 23:54:00 +0000413 }
414
415 // Generate a meaningful error if the user forgot to put class before the
416 // identifier, comma, or greater.
417 if(!Tok.is(tok::kw_class)) {
418 Diag(Tok.getLocation(), diag::err_expected_class_before)
419 << PP.getSpelling(Tok);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000420 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000421 }
422 SourceLocation ClassLoc = ConsumeToken();
423
424 // Get the identifier, if given.
Douglas Gregor279272e2009-02-04 19:02:06 +0000425 SourceLocation NameLoc;
426 IdentifierInfo* ParamName = 0;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000427 if(Tok.is(tok::identifier)) {
Douglas Gregor279272e2009-02-04 19:02:06 +0000428 ParamName = Tok.getIdentifierInfo();
429 NameLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000430 } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
431 // Unnamed template parameter. Don't have to do anything here, just
432 // don't consume this token.
433 } else {
434 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000435 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000436 }
437
Douglas Gregord406b032009-02-06 22:42:48 +0000438 TemplateParamsTy *ParamList =
439 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
440 TemplateLoc, LAngleLoc,
441 &TemplateParams[0],
442 TemplateParams.size(),
443 RAngleLoc);
444
Chris Lattner5261d0c2009-03-28 19:18:32 +0000445 Parser::DeclPtrTy Param
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000446 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
447 ParamList, ParamName,
448 NameLoc, Depth, Position);
449
450 // Get the a default value, if given.
451 if (Tok.is(tok::equal)) {
452 SourceLocation EqualLoc = ConsumeToken();
453 OwningExprResult DefaultExpr = ParseCXXIdExpression();
454 if (DefaultExpr.isInvalid())
455 return Param;
456 else if (Param)
457 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc,
458 move(DefaultExpr));
459 }
460
461 return Param;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000462}
463
464/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
465/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregor2fa10442008-12-18 19:37:40 +0000466///
Douglas Gregorb3bec712008-12-01 23:54:00 +0000467/// template-parameter:
468/// ...
469/// parameter-declaration
470///
471/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
472/// but that didn't work out to well. Instead, this tries to recrate the basic
473/// parsing of parameter declarations, but tries to constrain it for template
474/// parameters.
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000475/// FIXME: We need to make a ParseParameterDeclaration that works for
476/// non-type template parameters and normal function parameters.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000477Parser::DeclPtrTy
Douglas Gregor52473432008-12-24 02:52:09 +0000478Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000479 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000480
481 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000482 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregorb3bec712008-12-01 23:54:00 +0000483 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000484 DeclSpec DS;
485 ParseDeclarationSpecifiers(DS);
Douglas Gregorb3bec712008-12-01 23:54:00 +0000486
487 // Parse this as a typename.
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000488 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
489 ParseDeclarator(ParamDecl);
Chris Lattner8376d2e2009-01-05 01:24:05 +0000490 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000491 // This probably shouldn't happen - and it's more of a Sema thing, but
492 // basically we didn't parse the type name because we couldn't associate
493 // it with an AST node. we should just skip to the comma or greater.
494 // TODO: This is currently a placeholder for some kind of Sema Error.
495 Diag(Tok.getLocation(), diag::err_parse_error);
496 SkipUntil(tok::comma, tok::greater, true, true);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000497 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000498 }
499
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000500 // Create the parameter.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000501 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
502 Depth, Position);
Douglas Gregorb3bec712008-12-01 23:54:00 +0000503
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000504 // If there is a default value, parse it.
Chris Lattner8376d2e2009-01-05 01:24:05 +0000505 if (Tok.is(tok::equal)) {
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000506 SourceLocation EqualLoc = ConsumeToken();
507
508 // C++ [temp.param]p15:
509 // When parsing a default template-argument for a non-type
510 // template-parameter, the first non-nested > is taken as the
511 // end of the template-parameter-list rather than a greater-than
512 // operator.
513 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
514
515 OwningExprResult DefaultArg = ParseAssignmentExpression();
516 if (DefaultArg.isInvalid())
517 SkipUntil(tok::comma, tok::greater, true, true);
518 else if (Param)
519 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
520 move(DefaultArg));
Douglas Gregorb3bec712008-12-01 23:54:00 +0000521 }
522
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000523 return Param;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000524}
Douglas Gregor2fa10442008-12-18 19:37:40 +0000525
Douglas Gregora08b6c72009-02-17 23:15:12 +0000526/// \brief Parses a template-id that after the template name has
527/// already been parsed.
528///
529/// This routine takes care of parsing the enclosed template argument
530/// list ('<' template-parameter-list [opt] '>') and placing the
531/// results into a form that can be transferred to semantic analysis.
532///
533/// \param Template the template declaration produced by isTemplateName
534///
535/// \param TemplateNameLoc the source location of the template name
536///
537/// \param SS if non-NULL, the nested-name-specifier preceding the
538/// template name.
539///
540/// \param ConsumeLastToken if true, then we will consume the last
541/// token that forms the template-id. Otherwise, we will leave the
542/// last token in the stream (e.g., so that it can be replaced with an
543/// annotation token).
544bool
Douglas Gregordd13e842009-03-30 22:58:21 +0000545Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000546 SourceLocation TemplateNameLoc,
547 const CXXScopeSpec *SS,
548 bool ConsumeLastToken,
549 SourceLocation &LAngleLoc,
550 TemplateArgList &TemplateArgs,
551 TemplateArgIsTypeList &TemplateArgIsType,
552 TemplateArgLocationList &TemplateArgLocations,
553 SourceLocation &RAngleLoc) {
554 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
555
556 // Consume the '<'.
557 LAngleLoc = ConsumeToken();
558
559 // Parse the optional template-argument-list.
560 bool Invalid = false;
561 {
562 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
563 if (Tok.isNot(tok::greater))
564 Invalid = ParseTemplateArgumentList(TemplateArgs, TemplateArgIsType,
565 TemplateArgLocations);
566
567 if (Invalid) {
568 // Try to find the closing '>'.
569 SkipUntil(tok::greater, true, !ConsumeLastToken);
570
571 return true;
572 }
573 }
574
Douglas Gregorf2d87392009-02-25 23:02:36 +0000575 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
Douglas Gregora08b6c72009-02-17 23:15:12 +0000576 return true;
577
Douglas Gregorf2d87392009-02-25 23:02:36 +0000578 // Determine the location of the '>' or '>>'. Only consume this
579 // token if the caller asked us to.
Douglas Gregora08b6c72009-02-17 23:15:12 +0000580 RAngleLoc = Tok.getLocation();
581
Douglas Gregorf2d87392009-02-25 23:02:36 +0000582 if (Tok.is(tok::greatergreater)) {
Douglas Gregor3bb30002009-02-26 21:00:50 +0000583 if (!getLang().CPlusPlus0x) {
584 const char *ReplaceStr = "> >";
585 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
586 ReplaceStr = "> > ";
587
588 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregor61be3602009-02-27 17:53:17 +0000589 << CodeModificationHint::CreateReplacement(
590 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor3bb30002009-02-26 21:00:50 +0000591 }
Douglas Gregorf2d87392009-02-25 23:02:36 +0000592
593 Tok.setKind(tok::greater);
594 if (!ConsumeLastToken) {
595 // Since we're not supposed to consume the '>>' token, we need
596 // to insert a second '>' token after the first.
597 PP.EnterToken(Tok);
598 }
599 } else if (ConsumeLastToken)
Douglas Gregora08b6c72009-02-17 23:15:12 +0000600 ConsumeToken();
601
602 return false;
603}
604
Douglas Gregor0c281a82009-02-25 19:37:18 +0000605/// \brief Replace the tokens that form a simple-template-id with an
606/// annotation token containing the complete template-id.
607///
608/// The first token in the stream must be the name of a template that
609/// is followed by a '<'. This routine will parse the complete
610/// simple-template-id and replace the tokens with a single annotation
611/// token with one of two different kinds: if the template-id names a
612/// type (and \p AllowTypeAnnotation is true), the annotation token is
613/// a type annotation that includes the optional nested-name-specifier
614/// (\p SS). Otherwise, the annotation token is a template-id
615/// annotation that does not include the optional
616/// nested-name-specifier.
617///
618/// \param Template the declaration of the template named by the first
619/// token (an identifier), as returned from \c Action::isTemplateName().
620///
621/// \param TemplateNameKind the kind of template that \p Template
622/// refers to, as returned from \c Action::isTemplateName().
623///
624/// \param SS if non-NULL, the nested-name-specifier that precedes
625/// this template name.
626///
627/// \param TemplateKWLoc if valid, specifies that this template-id
628/// annotation was preceded by the 'template' keyword and gives the
629/// location of that keyword. If invalid (the default), then this
630/// template-id was not preceded by a 'template' keyword.
631///
632/// \param AllowTypeAnnotation if true (the default), then a
633/// simple-template-id that refers to a class template, template
634/// template parameter, or other template that produces a type will be
635/// replaced with a type annotation token. Otherwise, the
636/// simple-template-id is always replaced with a template-id
637/// annotation token.
Chris Lattner8eabc062009-06-26 04:27:47 +0000638///
639/// If an unrecoverable parse error occurs and no annotation token can be
640/// formed, this function returns true.
641///
642bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000643 const CXXScopeSpec *SS,
644 SourceLocation TemplateKWLoc,
645 bool AllowTypeAnnotation) {
Douglas Gregor2fa10442008-12-18 19:37:40 +0000646 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
647 assert(Template && Tok.is(tok::identifier) && NextToken().is(tok::less) &&
648 "Parser isn't at the beginning of a template-id");
649
650 // Consume the template-name.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000651 IdentifierInfo *Name = Tok.getIdentifierInfo();
Douglas Gregor2fa10442008-12-18 19:37:40 +0000652 SourceLocation TemplateNameLoc = ConsumeToken();
653
Douglas Gregora08b6c72009-02-17 23:15:12 +0000654 // Parse the enclosed template argument list.
655 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor6f37b582009-02-09 19:34:22 +0000656 TemplateArgList TemplateArgs;
657 TemplateArgIsTypeList TemplateArgIsType;
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000658 TemplateArgLocationList TemplateArgLocations;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000659 bool Invalid = ParseTemplateIdAfterTemplateName(Template, TemplateNameLoc,
660 SS, false, LAngleLoc,
661 TemplateArgs,
662 TemplateArgIsType,
663 TemplateArgLocations,
664 RAngleLoc);
Chris Lattner8eabc062009-06-26 04:27:47 +0000665
666 if (Invalid) {
667 // If we failed to parse the template ID but skipped ahead to a >, we're not
668 // going to be able to form a token annotation. Eat the '>' if present.
669 if (Tok.is(tok::greater))
670 ConsumeToken();
671 return true;
672 }
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000673
Jay Foad9e6bef42009-05-21 09:52:38 +0000674 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
675 TemplateArgIsType.data(),
Douglas Gregora08b6c72009-02-17 23:15:12 +0000676 TemplateArgs.size());
Douglas Gregoraf0d0092009-02-09 21:04:56 +0000677
Douglas Gregor8e458f42009-02-09 18:46:07 +0000678 // Build the annotation token.
Douglas Gregoraabb8502009-03-31 00:43:58 +0000679 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000680 Action::TypeResult Type
Douglas Gregordd13e842009-03-30 22:58:21 +0000681 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
682 LAngleLoc, TemplateArgsPtr,
683 &TemplateArgLocations[0],
684 RAngleLoc);
Chris Lattner8eabc062009-06-26 04:27:47 +0000685 if (Type.isInvalid()) {
686 // If we failed to parse the template ID but skipped ahead to a >, we're not
687 // going to be able to form a token annotation. Eat the '>' if present.
688 if (Tok.is(tok::greater))
689 ConsumeToken();
690 return true;
691 }
Douglas Gregora08b6c72009-02-17 23:15:12 +0000692
693 Tok.setKind(tok::annot_typename);
694 Tok.setAnnotationValue(Type.get());
Douglas Gregor0c281a82009-02-25 19:37:18 +0000695 if (SS && SS->isNotEmpty())
696 Tok.setLocation(SS->getBeginLoc());
697 else if (TemplateKWLoc.isValid())
698 Tok.setLocation(TemplateKWLoc);
699 else
700 Tok.setLocation(TemplateNameLoc);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000701 } else {
Douglas Gregoraabb8502009-03-31 00:43:58 +0000702 // Build a template-id annotation token that can be processed
703 // later.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000704 Tok.setKind(tok::annot_template_id);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000705 TemplateIdAnnotation *TemplateId
Douglas Gregor0c281a82009-02-25 19:37:18 +0000706 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor8e458f42009-02-09 18:46:07 +0000707 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000708 TemplateId->Name = Name;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000709 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000710 TemplateId->Kind = TNK;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000711 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000712 TemplateId->RAngleLoc = RAngleLoc;
713 void **Args = TemplateId->getTemplateArgs();
714 bool *ArgIsType = TemplateId->getTemplateArgIsType();
715 SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations();
716 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) {
Douglas Gregor8e458f42009-02-09 18:46:07 +0000717 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor0c281a82009-02-25 19:37:18 +0000718 ArgIsType[Arg] = TemplateArgIsType[Arg];
719 ArgLocs[Arg] = TemplateArgLocations[Arg];
720 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000721 Tok.setAnnotationValue(TemplateId);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000722 if (TemplateKWLoc.isValid())
723 Tok.setLocation(TemplateKWLoc);
724 else
725 Tok.setLocation(TemplateNameLoc);
726
727 TemplateArgsPtr.release();
Douglas Gregor8e458f42009-02-09 18:46:07 +0000728 }
729
730 // Common fields for the annotation token
Douglas Gregor2fa10442008-12-18 19:37:40 +0000731 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregor2fa10442008-12-18 19:37:40 +0000732
Douglas Gregor2fa10442008-12-18 19:37:40 +0000733 // In case the tokens were cached, have Preprocessor replace them with the
734 // annotation token.
735 PP.AnnotateCachedTokens(Tok);
Chris Lattner8eabc062009-06-26 04:27:47 +0000736 return false;
Douglas Gregor2fa10442008-12-18 19:37:40 +0000737}
738
Douglas Gregor0c281a82009-02-25 19:37:18 +0000739/// \brief Replaces a template-id annotation token with a type
740/// annotation token.
741///
Douglas Gregord7cb0372009-04-01 21:51:26 +0000742/// If there was a failure when forming the type from the template-id,
743/// a type annotation token will still be created, but will have a
744/// NULL type pointer to signify an error.
745void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000746 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
747
748 TemplateIdAnnotation *TemplateId
749 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000750 assert((TemplateId->Kind == TNK_Type_template ||
751 TemplateId->Kind == TNK_Dependent_template_name) &&
752 "Only works for type and dependent templates");
Douglas Gregor0c281a82009-02-25 19:37:18 +0000753
754 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
755 TemplateId->getTemplateArgs(),
756 TemplateId->getTemplateArgIsType(),
757 TemplateId->NumArgs);
758
759 Action::TypeResult Type
Douglas Gregordd13e842009-03-30 22:58:21 +0000760 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
761 TemplateId->TemplateNameLoc,
762 TemplateId->LAngleLoc,
763 TemplateArgsPtr,
764 TemplateId->getTemplateArgLocations(),
765 TemplateId->RAngleLoc);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000766 // Create the new "type" annotation token.
767 Tok.setKind(tok::annot_typename);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000768 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor0c281a82009-02-25 19:37:18 +0000769 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
770 Tok.setLocation(SS->getBeginLoc());
771
772 // We might be backtracking, in which case we need to replace the
773 // template-id annotation token with the type annotation within the
774 // set of cached tokens. That way, we won't try to form the same
775 // class template specialization again.
776 PP.ReplaceLastTokenWithAnnotation(Tok);
777 TemplateId->Destroy();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000778}
779
Douglas Gregor2fa10442008-12-18 19:37:40 +0000780/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
781///
782/// template-argument: [C++ 14.2]
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000783/// constant-expression
Douglas Gregor2fa10442008-12-18 19:37:40 +0000784/// type-id
785/// id-expression
Douglas Gregor6f37b582009-02-09 19:34:22 +0000786void *Parser::ParseTemplateArgument(bool &ArgIsType) {
Douglas Gregor8e458f42009-02-09 18:46:07 +0000787 // C++ [temp.arg]p2:
788 // In a template-argument, an ambiguity between a type-id and an
789 // expression is resolved to a type-id, regardless of the form of
790 // the corresponding template-parameter.
791 //
792 // Therefore, we initially try to parse a type-id.
Douglas Gregor341ac792009-02-10 00:53:15 +0000793 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregor6f37b582009-02-09 19:34:22 +0000794 ArgIsType = true;
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000795 TypeResult TypeArg = ParseTypeName();
796 if (TypeArg.isInvalid())
797 return 0;
798 return TypeArg.get();
Douglas Gregor8e458f42009-02-09 18:46:07 +0000799 }
800
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000801 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000802 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregor6f37b582009-02-09 19:34:22 +0000803 return 0;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000804
Douglas Gregor6f37b582009-02-09 19:34:22 +0000805 ArgIsType = false;
806 return ExprArg.release();
Douglas Gregor2fa10442008-12-18 19:37:40 +0000807}
808
809/// ParseTemplateArgumentList - Parse a C++ template-argument-list
810/// (C++ [temp.names]). Returns true if there was an error.
811///
812/// template-argument-list: [C++ 14.2]
813/// template-argument
814/// template-argument-list ',' template-argument
Douglas Gregor6f37b582009-02-09 19:34:22 +0000815bool
816Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs,
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000817 TemplateArgIsTypeList &TemplateArgIsType,
818 TemplateArgLocationList &TemplateArgLocations) {
Douglas Gregor2fa10442008-12-18 19:37:40 +0000819 while (true) {
Douglas Gregor6f37b582009-02-09 19:34:22 +0000820 bool IsType = false;
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000821 SourceLocation Loc = Tok.getLocation();
Douglas Gregor6f37b582009-02-09 19:34:22 +0000822 void *Arg = ParseTemplateArgument(IsType);
823 if (Arg) {
824 TemplateArgs.push_back(Arg);
825 TemplateArgIsType.push_back(IsType);
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000826 TemplateArgLocations.push_back(Loc);
Douglas Gregor6f37b582009-02-09 19:34:22 +0000827 } else {
Douglas Gregor2fa10442008-12-18 19:37:40 +0000828 SkipUntil(tok::comma, tok::greater, true, true);
829 return true;
830 }
Douglas Gregor6f37b582009-02-09 19:34:22 +0000831
Douglas Gregor2fa10442008-12-18 19:37:40 +0000832 // If the next token is a comma, consume it and keep reading
833 // arguments.
834 if (Tok.isNot(tok::comma)) break;
835
836 // Consume the comma.
837 ConsumeToken();
838 }
839
Douglas Gregorf2d87392009-02-25 23:02:36 +0000840 return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater);
Douglas Gregor2fa10442008-12-18 19:37:40 +0000841}
842
Douglas Gregore3298aa2009-05-12 21:31:51 +0000843/// \brief Parse a C++ explicit template instantiation
844/// (C++ [temp.explicit]).
845///
846/// explicit-instantiation:
847/// 'template' declaration
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000848Parser::DeclPtrTy
849Parser::ParseExplicitInstantiation(SourceLocation TemplateLoc,
850 SourceLocation &DeclEnd) {
851 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
852 ParsedTemplateInfo(TemplateLoc),
853 DeclEnd, AS_none);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000854}