blob: 40de81a883f2741209509f2355f325215309d7a7 [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 Gregord53d0582009-08-24 23:03:25 +000018#include "llvm/Support/Compiler.h"
Douglas Gregorb3bec712008-12-01 23:54:00 +000019using namespace clang;
20
Douglas Gregora9db0fa2009-05-12 23:25:50 +000021/// \brief Parse a template declaration, explicit instantiation, or
22/// explicit specialization.
23Parser::DeclPtrTy
24Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
25 SourceLocation &DeclEnd,
26 AccessSpecifier AS) {
27 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less))
Douglas Gregor7a374722009-09-04 06:33:52 +000028 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
29 DeclEnd);
Douglas Gregora9db0fa2009-05-12 23:25:50 +000030
31 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
32}
33
Douglas Gregord53d0582009-08-24 23:03:25 +000034/// \brief RAII class that manages the template parameter depth.
35namespace {
36 class VISIBILITY_HIDDEN TemplateParameterDepthCounter {
37 unsigned &Depth;
38 unsigned AddedLevels;
39
40 public:
41 explicit TemplateParameterDepthCounter(unsigned &Depth)
42 : Depth(Depth), AddedLevels(0) { }
43
44 ~TemplateParameterDepthCounter() {
45 Depth -= AddedLevels;
46 }
47
48 void operator++() {
49 ++Depth;
50 ++AddedLevels;
51 }
52
53 operator unsigned() const { return Depth; }
54 };
55}
56
Douglas Gregora08b6c72009-02-17 23:15:12 +000057/// \brief Parse a template declaration or an explicit specialization.
58///
59/// Template declarations include one or more template parameter lists
60/// and either the function or class template declaration. Explicit
61/// specializations contain one or more 'template < >' prefixes
62/// followed by a (possibly templated) declaration. Since the
63/// syntactic form of both features is nearly identical, we parse all
64/// of the template headers together and let semantic analysis sort
65/// the declarations from the explicit specializations.
Douglas Gregorb3bec712008-12-01 23:54:00 +000066///
67/// template-declaration: [C++ temp]
68/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregora08b6c72009-02-17 23:15:12 +000069///
70/// explicit-specialization: [ C++ temp.expl.spec]
71/// 'template' '<' '>' declaration
Chris Lattner5261d0c2009-03-28 19:18:32 +000072Parser::DeclPtrTy
Anders Carlssoned20fb92009-03-26 00:52:18 +000073Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +000074 SourceLocation &DeclEnd,
Anders Carlssoned20fb92009-03-26 00:52:18 +000075 AccessSpecifier AS) {
Douglas Gregorb3bec712008-12-01 23:54:00 +000076 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
77 "Token does not start a template declaration.");
78
Douglas Gregor8e7f9572008-12-02 00:41:28 +000079 // Enter template-parameter scope.
Douglas Gregor95d40792008-12-10 06:34:36 +000080 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregor8e7f9572008-12-02 00:41:28 +000081
Douglas Gregor52473432008-12-24 02:52:09 +000082 // Parse multiple levels of template headers within this template
83 // parameter scope, e.g.,
84 //
85 // template<typename T>
86 // template<typename U>
87 // class A<T>::B { ... };
88 //
89 // We parse multiple levels non-recursively so that we can build a
90 // single data structure containing all of the template parameter
Douglas Gregora08b6c72009-02-17 23:15:12 +000091 // lists to easily differentiate between the case above and:
Douglas Gregor52473432008-12-24 02:52:09 +000092 //
93 // template<typename T>
94 // class A {
95 // template<typename U> class B;
96 // };
97 //
98 // In the first case, the action for declaring A<T>::B receives
99 // both template parameter lists. In the second case, the action for
100 // defining A<T>::B receives just the inner template parameter list
101 // (and retrieves the outer template parameter list from its
102 // context).
Douglas Gregord022d712009-08-20 18:46:05 +0000103 bool isSpecialization = true;
Douglas Gregor52473432008-12-24 02:52:09 +0000104 TemplateParameterLists ParamLists;
Douglas Gregord53d0582009-08-24 23:03:25 +0000105 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
Douglas Gregor52473432008-12-24 02:52:09 +0000106 do {
107 // Consume the 'export', if any.
108 SourceLocation ExportLoc;
109 if (Tok.is(tok::kw_export)) {
110 ExportLoc = ConsumeToken();
111 }
112
113 // Consume the 'template', which should be here.
114 SourceLocation TemplateLoc;
115 if (Tok.is(tok::kw_template)) {
116 TemplateLoc = ConsumeToken();
117 } else {
118 Diag(Tok.getLocation(), diag::err_expected_template);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000119 return DeclPtrTy();
Douglas Gregor52473432008-12-24 02:52:09 +0000120 }
121
122 // Parse the '<' template-parameter-list '>'
123 SourceLocation LAngleLoc, RAngleLoc;
124 TemplateParameterList TemplateParams;
Douglas Gregord53d0582009-08-24 23:03:25 +0000125 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
Douglas Gregor84a20812009-07-22 23:48:44 +0000126 RAngleLoc)) {
127 // Skip until the semi-colon or a }.
128 SkipUntil(tok::r_brace, true, true);
129 if (Tok.is(tok::semi))
130 ConsumeToken();
131 return DeclPtrTy();
132 }
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000133
Douglas Gregor52473432008-12-24 02:52:09 +0000134 ParamLists.push_back(
Douglas Gregord53d0582009-08-24 23:03:25 +0000135 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
Douglas Gregor52473432008-12-24 02:52:09 +0000136 TemplateLoc, LAngleLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +0000137 TemplateParams.data(),
Douglas Gregor52473432008-12-24 02:52:09 +0000138 TemplateParams.size(), RAngleLoc));
Douglas Gregord53d0582009-08-24 23:03:25 +0000139
140 if (!TemplateParams.empty()) {
141 isSpecialization = false;
142 ++Depth;
143 }
Douglas Gregor52473432008-12-24 02:52:09 +0000144 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
145
146 // Parse the actual template declaration.
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000147 return ParseSingleDeclarationAfterTemplate(Context,
148 ParsedTemplateInfo(&ParamLists,
Douglas Gregord022d712009-08-20 18:46:05 +0000149 isSpecialization),
Douglas Gregore3298aa2009-05-12 21:31:51 +0000150 DeclEnd, AS);
151}
Chris Lattnera17991f2009-03-29 16:50:03 +0000152
Douglas Gregore3298aa2009-05-12 21:31:51 +0000153/// \brief Parse a single declaration that declares a template,
154/// template specialization, or explicit instantiation of a template.
155///
156/// \param TemplateParams if non-NULL, the template parameter lists
157/// that preceded this declaration. In this case, the declaration is a
158/// template declaration, out-of-line definition of a template, or an
159/// explicit template specialization. When NULL, the declaration is an
160/// explicit template instantiation.
161///
162/// \param TemplateLoc when TemplateParams is NULL, the location of
163/// the 'template' keyword that indicates that we have an explicit
164/// template instantiation.
165///
166/// \param DeclEnd will receive the source location of the last token
167/// within this declaration.
168///
169/// \param AS the access specifier associated with this
170/// declaration. Will be AS_none for namespace-scope declarations.
171///
172/// \returns the new declaration.
173Parser::DeclPtrTy
174Parser::ParseSingleDeclarationAfterTemplate(
175 unsigned Context,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000176 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregore3298aa2009-05-12 21:31:51 +0000177 SourceLocation &DeclEnd,
178 AccessSpecifier AS) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000179 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
180 "Template information required");
181
Douglas Gregor398a8012009-08-20 22:52:58 +0000182 if (Context == Declarator::MemberContext) {
183 // We are parsing a member template.
184 ParseCXXClassMemberDeclaration(AS, TemplateInfo);
185 return DeclPtrTy::make((void*)0);
186 }
187
Douglas Gregore3298aa2009-05-12 21:31:51 +0000188 // Parse the declaration specifiers.
189 DeclSpec DS;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000190 ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000191
192 if (Tok.is(tok::semi)) {
193 DeclEnd = ConsumeToken();
194 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
195 }
196
197 // Parse the declarator.
198 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
199 ParseDeclarator(DeclaratorInfo);
200 // Error parsing the declarator?
201 if (!DeclaratorInfo.hasName()) {
202 // If so, skip until the semi-colon or a }.
203 SkipUntil(tok::r_brace, true, true);
204 if (Tok.is(tok::semi))
205 ConsumeToken();
206 return DeclPtrTy();
207 }
208
209 // If we have a declaration or declarator list, handle it.
210 if (isDeclarationAfterDeclarator()) {
211 // Parse this declaration.
Douglas Gregor2ae1d772009-06-23 23:11:28 +0000212 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
213 TemplateInfo);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000214
215 if (Tok.is(tok::comma)) {
216 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000217 << (int)TemplateInfo.Kind;
Douglas Gregore3298aa2009-05-12 21:31:51 +0000218 SkipUntil(tok::semi, true, false);
219 return ThisDecl;
220 }
221
222 // Eat the semi colon after the declaration.
John McCallfcb32f42009-07-31 02:20:35 +0000223 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000224 return ThisDecl;
225 }
226
227 if (DeclaratorInfo.isFunctionDeclarator() &&
228 isStartOfFunctionDefinition()) {
229 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
230 Diag(Tok, diag::err_function_declared_typedef);
231
232 if (Tok.is(tok::l_brace)) {
233 // This recovery skips the entire function body. It would be nice
234 // to simply call ParseFunctionDefinition() below, however Sema
235 // assumes the declarator represents a function, not a typedef.
236 ConsumeBrace();
237 SkipUntil(tok::r_brace, true);
238 } else {
239 SkipUntil(tok::semi);
240 }
241 return DeclPtrTy();
242 }
Douglas Gregor19d10652009-06-24 00:54:41 +0000243 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000244 }
245
246 if (DeclaratorInfo.isFunctionDeclarator())
247 Diag(Tok, diag::err_expected_fn_body);
248 else
249 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
250 SkipUntil(tok::semi);
251 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000252}
253
254/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregor279272e2009-02-04 19:02:06 +0000255/// angle brackets. Depth is the depth of this template-parameter-list, which
256/// is the number of template headers directly enclosing this template header.
257/// TemplateParams is the current list of template parameters we're building.
258/// The template parameter we parse will be added to this list. LAngleLoc and
259/// RAngleLoc will receive the positions of the '<' and '>', respectively,
260/// that enclose this template parameter list.
Douglas Gregor84a20812009-07-22 23:48:44 +0000261///
262/// \returns true if an error occurred, false otherwise.
Douglas Gregor52473432008-12-24 02:52:09 +0000263bool Parser::ParseTemplateParameters(unsigned Depth,
264 TemplateParameterList &TemplateParams,
265 SourceLocation &LAngleLoc,
266 SourceLocation &RAngleLoc) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000267 // Get the template parameter list.
268 if(!Tok.is(tok::less)) {
269 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregor84a20812009-07-22 23:48:44 +0000270 return true;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000271 }
Douglas Gregor52473432008-12-24 02:52:09 +0000272 LAngleLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000273
274 // Try to parse the template parameter list.
Douglas Gregor52473432008-12-24 02:52:09 +0000275 if (Tok.is(tok::greater))
276 RAngleLoc = ConsumeToken();
277 else if(ParseTemplateParameterList(Depth, TemplateParams)) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000278 if(!Tok.is(tok::greater)) {
279 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregor84a20812009-07-22 23:48:44 +0000280 return true;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000281 }
Douglas Gregor52473432008-12-24 02:52:09 +0000282 RAngleLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000283 }
Douglas Gregor84a20812009-07-22 23:48:44 +0000284 return false;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000285}
286
287/// ParseTemplateParameterList - Parse a template parameter list. If
288/// the parsing fails badly (i.e., closing bracket was left out), this
289/// will try to put the token stream in a reasonable position (closing
290/// a statement, etc.) and return false.
291///
292/// template-parameter-list: [C++ temp]
293/// template-parameter
294/// template-parameter-list ',' template-parameter
Douglas Gregor52473432008-12-24 02:52:09 +0000295bool
296Parser::ParseTemplateParameterList(unsigned Depth,
297 TemplateParameterList &TemplateParams) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000298 while(1) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000299 if (DeclPtrTy TmpParam
Douglas Gregor52473432008-12-24 02:52:09 +0000300 = ParseTemplateParameter(Depth, TemplateParams.size())) {
301 TemplateParams.push_back(TmpParam);
302 } else {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000303 // If we failed to parse a template parameter, skip until we find
304 // a comma or closing brace.
305 SkipUntil(tok::comma, tok::greater, true, true);
306 }
307
308 // Did we find a comma or the end of the template parmeter list?
309 if(Tok.is(tok::comma)) {
310 ConsumeToken();
311 } else if(Tok.is(tok::greater)) {
312 // Don't consume this... that's done by template parser.
313 break;
314 } else {
315 // Somebody probably forgot to close the template. Skip ahead and
316 // try to get out of the expression. This error is currently
317 // subsumed by whatever goes on in ParseTemplateParameter.
318 // TODO: This could match >>, and it would be nice to avoid those
319 // silly errors with template <vec<T>>.
320 // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
321 SkipUntil(tok::greater, true, true);
322 return false;
323 }
324 }
325 return true;
326}
327
328/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
329///
330/// template-parameter: [C++ temp.param]
331/// type-parameter
332/// parameter-declaration
333///
334/// type-parameter: (see below)
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000335/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000336/// 'class' identifier[opt] '=' type-id
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000337/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000338/// 'typename' identifier[opt] '=' type-id
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000339/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000340/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattner5261d0c2009-03-28 19:18:32 +0000341Parser::DeclPtrTy
Douglas Gregor52473432008-12-24 02:52:09 +0000342Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Chris Lattner4e7a4202009-01-04 23:51:17 +0000343 if(Tok.is(tok::kw_class) ||
344 (Tok.is(tok::kw_typename) &&
345 // FIXME: Next token has not been annotated!
Chris Lattner5d7eace2009-01-06 05:06:21 +0000346 NextToken().isNot(tok::annot_typename))) {
Douglas Gregor52473432008-12-24 02:52:09 +0000347 return ParseTypeParameter(Depth, Position);
Douglas Gregorb3bec712008-12-01 23:54:00 +0000348 }
Chris Lattner4e7a4202009-01-04 23:51:17 +0000349
350 if(Tok.is(tok::kw_template))
351 return ParseTemplateTemplateParameter(Depth, Position);
352
353 // If it's none of the above, then it must be a parameter declaration.
354 // NOTE: This will pick up errors in the closure of the template parameter
355 // list (e.g., template < ; Check here to implement >> style closures.
356 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregorb3bec712008-12-01 23:54:00 +0000357}
358
359/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
360/// Other kinds of template parameters are parsed in
361/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
362///
363/// type-parameter: [C++ temp.param]
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000364/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000365/// 'class' identifier[opt] '=' type-id
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000366/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000367/// 'typename' identifier[opt] '=' type-id
Chris Lattner5261d0c2009-03-28 19:18:32 +0000368Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000369 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
370 "A type-parameter starts with 'class' or 'typename'");
371
372 // Consume the 'class' or 'typename' keyword.
373 bool TypenameKeyword = Tok.is(tok::kw_typename);
374 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000375
Anders Carlsson9c85fa02009-06-12 19:58:00 +0000376 // Grab the ellipsis (if given).
377 bool Ellipsis = false;
378 SourceLocation EllipsisLoc;
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000379 if (Tok.is(tok::ellipsis)) {
Anders Carlsson9c85fa02009-06-12 19:58:00 +0000380 Ellipsis = true;
381 EllipsisLoc = ConsumeToken();
Anders Carlsson647d1ab2009-06-12 23:09:56 +0000382
383 if (!getLang().CPlusPlus0x)
384 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson9c85fa02009-06-12 19:58:00 +0000385 }
386
Douglas Gregorb3bec712008-12-01 23:54:00 +0000387 // Grab the template parameter name (if given)
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000388 SourceLocation NameLoc;
389 IdentifierInfo* ParamName = 0;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000390 if(Tok.is(tok::identifier)) {
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000391 ParamName = Tok.getIdentifierInfo();
392 NameLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000393 } else if(Tok.is(tok::equal) || Tok.is(tok::comma) ||
394 Tok.is(tok::greater)) {
395 // Unnamed template parameter. Don't have to do anything here, just
396 // don't consume this token.
397 } else {
398 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000399 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000400 }
401
Chris Lattner5261d0c2009-03-28 19:18:32 +0000402 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson9c85fa02009-06-12 19:58:00 +0000403 Ellipsis, EllipsisLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000404 KeyLoc, ParamName, NameLoc,
405 Depth, Position);
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000406
Douglas Gregorb3bec712008-12-01 23:54:00 +0000407 // Grab a default type id (if given).
Douglas Gregorb3bec712008-12-01 23:54:00 +0000408 if(Tok.is(tok::equal)) {
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000409 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000410 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000411 TypeResult DefaultType = ParseTypeName();
412 if (!DefaultType.isInvalid())
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000413 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000414 DefaultType.get());
Douglas Gregorb3bec712008-12-01 23:54:00 +0000415 }
416
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000417 return TypeParam;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000418}
419
420/// ParseTemplateTemplateParameter - Handle the parsing of template
421/// template parameters.
422///
423/// type-parameter: [C++ temp.param]
424/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
425/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattner5261d0c2009-03-28 19:18:32 +0000426Parser::DeclPtrTy
Douglas Gregor52473432008-12-24 02:52:09 +0000427Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000428 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
429
430 // Handle the template <...> part.
431 SourceLocation TemplateLoc = ConsumeToken();
Douglas Gregor52473432008-12-24 02:52:09 +0000432 TemplateParameterList TemplateParams;
Douglas Gregord406b032009-02-06 22:42:48 +0000433 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor77f7ced2009-02-10 19:52:54 +0000434 {
435 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregor84a20812009-07-22 23:48:44 +0000436 if(ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
437 RAngleLoc)) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000438 return DeclPtrTy();
Douglas Gregor77f7ced2009-02-10 19:52:54 +0000439 }
Douglas Gregorb3bec712008-12-01 23:54:00 +0000440 }
441
442 // Generate a meaningful error if the user forgot to put class before the
443 // identifier, comma, or greater.
444 if(!Tok.is(tok::kw_class)) {
445 Diag(Tok.getLocation(), diag::err_expected_class_before)
446 << PP.getSpelling(Tok);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000447 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000448 }
449 SourceLocation ClassLoc = ConsumeToken();
450
451 // Get the identifier, if given.
Douglas Gregor279272e2009-02-04 19:02:06 +0000452 SourceLocation NameLoc;
453 IdentifierInfo* ParamName = 0;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000454 if(Tok.is(tok::identifier)) {
Douglas Gregor279272e2009-02-04 19:02:06 +0000455 ParamName = Tok.getIdentifierInfo();
456 NameLoc = ConsumeToken();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000457 } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
458 // Unnamed template parameter. Don't have to do anything here, just
459 // don't consume this token.
460 } else {
461 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000462 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000463 }
464
Douglas Gregord406b032009-02-06 22:42:48 +0000465 TemplateParamsTy *ParamList =
466 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
467 TemplateLoc, LAngleLoc,
468 &TemplateParams[0],
469 TemplateParams.size(),
470 RAngleLoc);
471
Chris Lattner5261d0c2009-03-28 19:18:32 +0000472 Parser::DeclPtrTy Param
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000473 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
474 ParamList, ParamName,
475 NameLoc, Depth, Position);
476
477 // Get the a default value, if given.
478 if (Tok.is(tok::equal)) {
479 SourceLocation EqualLoc = ConsumeToken();
480 OwningExprResult DefaultExpr = ParseCXXIdExpression();
481 if (DefaultExpr.isInvalid())
482 return Param;
483 else if (Param)
484 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc,
485 move(DefaultExpr));
486 }
487
488 return Param;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000489}
490
491/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
492/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregor2fa10442008-12-18 19:37:40 +0000493///
Douglas Gregorb3bec712008-12-01 23:54:00 +0000494/// template-parameter:
495/// ...
496/// parameter-declaration
497///
498/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
499/// but that didn't work out to well. Instead, this tries to recrate the basic
500/// parsing of parameter declarations, but tries to constrain it for template
501/// parameters.
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000502/// FIXME: We need to make a ParseParameterDeclaration that works for
503/// non-type template parameters and normal function parameters.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000504Parser::DeclPtrTy
Douglas Gregor52473432008-12-24 02:52:09 +0000505Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000506 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000507
508 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000509 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregorb3bec712008-12-01 23:54:00 +0000510 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000511 DeclSpec DS;
512 ParseDeclarationSpecifiers(DS);
Douglas Gregorb3bec712008-12-01 23:54:00 +0000513
514 // Parse this as a typename.
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000515 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
516 ParseDeclarator(ParamDecl);
Chris Lattner8376d2e2009-01-05 01:24:05 +0000517 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000518 // This probably shouldn't happen - and it's more of a Sema thing, but
519 // basically we didn't parse the type name because we couldn't associate
520 // it with an AST node. we should just skip to the comma or greater.
521 // TODO: This is currently a placeholder for some kind of Sema Error.
522 Diag(Tok.getLocation(), diag::err_parse_error);
523 SkipUntil(tok::comma, tok::greater, true, true);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000524 return DeclPtrTy();
Douglas Gregorb3bec712008-12-01 23:54:00 +0000525 }
526
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000527 // Create the parameter.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000528 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
529 Depth, Position);
Douglas Gregorb3bec712008-12-01 23:54:00 +0000530
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000531 // If there is a default value, parse it.
Chris Lattner8376d2e2009-01-05 01:24:05 +0000532 if (Tok.is(tok::equal)) {
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000533 SourceLocation EqualLoc = ConsumeToken();
534
535 // C++ [temp.param]p15:
536 // When parsing a default template-argument for a non-type
537 // template-parameter, the first non-nested > is taken as the
538 // end of the template-parameter-list rather than a greater-than
539 // operator.
540 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
541
542 OwningExprResult DefaultArg = ParseAssignmentExpression();
543 if (DefaultArg.isInvalid())
544 SkipUntil(tok::comma, tok::greater, true, true);
545 else if (Param)
546 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
547 move(DefaultArg));
Douglas Gregorb3bec712008-12-01 23:54:00 +0000548 }
549
Douglas Gregor8e7f9572008-12-02 00:41:28 +0000550 return Param;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000551}
Douglas Gregor2fa10442008-12-18 19:37:40 +0000552
Douglas Gregora08b6c72009-02-17 23:15:12 +0000553/// \brief Parses a template-id that after the template name has
554/// already been parsed.
555///
556/// This routine takes care of parsing the enclosed template argument
557/// list ('<' template-parameter-list [opt] '>') and placing the
558/// results into a form that can be transferred to semantic analysis.
559///
560/// \param Template the template declaration produced by isTemplateName
561///
562/// \param TemplateNameLoc the source location of the template name
563///
564/// \param SS if non-NULL, the nested-name-specifier preceding the
565/// template name.
566///
567/// \param ConsumeLastToken if true, then we will consume the last
568/// token that forms the template-id. Otherwise, we will leave the
569/// last token in the stream (e.g., so that it can be replaced with an
570/// annotation token).
571bool
Douglas Gregordd13e842009-03-30 22:58:21 +0000572Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000573 SourceLocation TemplateNameLoc,
574 const CXXScopeSpec *SS,
575 bool ConsumeLastToken,
576 SourceLocation &LAngleLoc,
577 TemplateArgList &TemplateArgs,
578 TemplateArgIsTypeList &TemplateArgIsType,
579 TemplateArgLocationList &TemplateArgLocations,
580 SourceLocation &RAngleLoc) {
581 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
582
583 // Consume the '<'.
584 LAngleLoc = ConsumeToken();
585
586 // Parse the optional template-argument-list.
587 bool Invalid = false;
588 {
589 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
590 if (Tok.isNot(tok::greater))
591 Invalid = ParseTemplateArgumentList(TemplateArgs, TemplateArgIsType,
592 TemplateArgLocations);
593
594 if (Invalid) {
595 // Try to find the closing '>'.
596 SkipUntil(tok::greater, true, !ConsumeLastToken);
597
598 return true;
599 }
600 }
601
Douglas Gregorf2d87392009-02-25 23:02:36 +0000602 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
Douglas Gregora08b6c72009-02-17 23:15:12 +0000603 return true;
604
Douglas Gregorf2d87392009-02-25 23:02:36 +0000605 // Determine the location of the '>' or '>>'. Only consume this
606 // token if the caller asked us to.
Douglas Gregora08b6c72009-02-17 23:15:12 +0000607 RAngleLoc = Tok.getLocation();
608
Douglas Gregorf2d87392009-02-25 23:02:36 +0000609 if (Tok.is(tok::greatergreater)) {
Douglas Gregor3bb30002009-02-26 21:00:50 +0000610 if (!getLang().CPlusPlus0x) {
611 const char *ReplaceStr = "> >";
612 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
613 ReplaceStr = "> > ";
614
615 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregor61be3602009-02-27 17:53:17 +0000616 << CodeModificationHint::CreateReplacement(
617 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor3bb30002009-02-26 21:00:50 +0000618 }
Douglas Gregorf2d87392009-02-25 23:02:36 +0000619
620 Tok.setKind(tok::greater);
621 if (!ConsumeLastToken) {
622 // Since we're not supposed to consume the '>>' token, we need
623 // to insert a second '>' token after the first.
624 PP.EnterToken(Tok);
625 }
626 } else if (ConsumeLastToken)
Douglas Gregora08b6c72009-02-17 23:15:12 +0000627 ConsumeToken();
628
629 return false;
630}
631
Douglas Gregor0c281a82009-02-25 19:37:18 +0000632/// \brief Replace the tokens that form a simple-template-id with an
633/// annotation token containing the complete template-id.
634///
635/// The first token in the stream must be the name of a template that
636/// is followed by a '<'. This routine will parse the complete
637/// simple-template-id and replace the tokens with a single annotation
638/// token with one of two different kinds: if the template-id names a
639/// type (and \p AllowTypeAnnotation is true), the annotation token is
640/// a type annotation that includes the optional nested-name-specifier
641/// (\p SS). Otherwise, the annotation token is a template-id
642/// annotation that does not include the optional
643/// nested-name-specifier.
644///
645/// \param Template the declaration of the template named by the first
646/// token (an identifier), as returned from \c Action::isTemplateName().
647///
648/// \param TemplateNameKind the kind of template that \p Template
649/// refers to, as returned from \c Action::isTemplateName().
650///
651/// \param SS if non-NULL, the nested-name-specifier that precedes
652/// this template name.
653///
654/// \param TemplateKWLoc if valid, specifies that this template-id
655/// annotation was preceded by the 'template' keyword and gives the
656/// location of that keyword. If invalid (the default), then this
657/// template-id was not preceded by a 'template' keyword.
658///
659/// \param AllowTypeAnnotation if true (the default), then a
660/// simple-template-id that refers to a class template, template
661/// template parameter, or other template that produces a type will be
662/// replaced with a type annotation token. Otherwise, the
663/// simple-template-id is always replaced with a template-id
664/// annotation token.
Chris Lattner8eabc062009-06-26 04:27:47 +0000665///
666/// If an unrecoverable parse error occurs and no annotation token can be
667/// formed, this function returns true.
668///
669bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000670 const CXXScopeSpec *SS,
671 SourceLocation TemplateKWLoc,
672 bool AllowTypeAnnotation) {
Douglas Gregor2fa10442008-12-18 19:37:40 +0000673 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
674 assert(Template && Tok.is(tok::identifier) && NextToken().is(tok::less) &&
675 "Parser isn't at the beginning of a template-id");
676
677 // Consume the template-name.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000678 IdentifierInfo *Name = Tok.getIdentifierInfo();
Douglas Gregor2fa10442008-12-18 19:37:40 +0000679 SourceLocation TemplateNameLoc = ConsumeToken();
680
Douglas Gregora08b6c72009-02-17 23:15:12 +0000681 // Parse the enclosed template argument list.
682 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor6f37b582009-02-09 19:34:22 +0000683 TemplateArgList TemplateArgs;
684 TemplateArgIsTypeList TemplateArgIsType;
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000685 TemplateArgLocationList TemplateArgLocations;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000686 bool Invalid = ParseTemplateIdAfterTemplateName(Template, TemplateNameLoc,
687 SS, false, LAngleLoc,
688 TemplateArgs,
689 TemplateArgIsType,
690 TemplateArgLocations,
691 RAngleLoc);
Chris Lattner8eabc062009-06-26 04:27:47 +0000692
693 if (Invalid) {
694 // If we failed to parse the template ID but skipped ahead to a >, we're not
695 // going to be able to form a token annotation. Eat the '>' if present.
696 if (Tok.is(tok::greater))
697 ConsumeToken();
698 return true;
699 }
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000700
Jay Foad9e6bef42009-05-21 09:52:38 +0000701 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
702 TemplateArgIsType.data(),
Douglas Gregora08b6c72009-02-17 23:15:12 +0000703 TemplateArgs.size());
Douglas Gregoraf0d0092009-02-09 21:04:56 +0000704
Douglas Gregor8e458f42009-02-09 18:46:07 +0000705 // Build the annotation token.
Douglas Gregoraabb8502009-03-31 00:43:58 +0000706 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000707 Action::TypeResult Type
Douglas Gregordd13e842009-03-30 22:58:21 +0000708 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
709 LAngleLoc, TemplateArgsPtr,
710 &TemplateArgLocations[0],
711 RAngleLoc);
Chris Lattner8eabc062009-06-26 04:27:47 +0000712 if (Type.isInvalid()) {
713 // If we failed to parse the template ID but skipped ahead to a >, we're not
714 // going to be able to form a token annotation. Eat the '>' if present.
715 if (Tok.is(tok::greater))
716 ConsumeToken();
717 return true;
718 }
Douglas Gregora08b6c72009-02-17 23:15:12 +0000719
720 Tok.setKind(tok::annot_typename);
721 Tok.setAnnotationValue(Type.get());
Douglas Gregor0c281a82009-02-25 19:37:18 +0000722 if (SS && SS->isNotEmpty())
723 Tok.setLocation(SS->getBeginLoc());
724 else if (TemplateKWLoc.isValid())
725 Tok.setLocation(TemplateKWLoc);
726 else
727 Tok.setLocation(TemplateNameLoc);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000728 } else {
Douglas Gregoraabb8502009-03-31 00:43:58 +0000729 // Build a template-id annotation token that can be processed
730 // later.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000731 Tok.setKind(tok::annot_template_id);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000732 TemplateIdAnnotation *TemplateId
Douglas Gregor0c281a82009-02-25 19:37:18 +0000733 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor8e458f42009-02-09 18:46:07 +0000734 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000735 TemplateId->Name = Name;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000736 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000737 TemplateId->Kind = TNK;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000738 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000739 TemplateId->RAngleLoc = RAngleLoc;
740 void **Args = TemplateId->getTemplateArgs();
741 bool *ArgIsType = TemplateId->getTemplateArgIsType();
742 SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations();
743 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) {
Douglas Gregor8e458f42009-02-09 18:46:07 +0000744 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor0c281a82009-02-25 19:37:18 +0000745 ArgIsType[Arg] = TemplateArgIsType[Arg];
746 ArgLocs[Arg] = TemplateArgLocations[Arg];
747 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000748 Tok.setAnnotationValue(TemplateId);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000749 if (TemplateKWLoc.isValid())
750 Tok.setLocation(TemplateKWLoc);
751 else
752 Tok.setLocation(TemplateNameLoc);
753
754 TemplateArgsPtr.release();
Douglas Gregor8e458f42009-02-09 18:46:07 +0000755 }
756
757 // Common fields for the annotation token
Douglas Gregor2fa10442008-12-18 19:37:40 +0000758 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregor2fa10442008-12-18 19:37:40 +0000759
Douglas Gregor2fa10442008-12-18 19:37:40 +0000760 // In case the tokens were cached, have Preprocessor replace them with the
761 // annotation token.
762 PP.AnnotateCachedTokens(Tok);
Chris Lattner8eabc062009-06-26 04:27:47 +0000763 return false;
Douglas Gregor2fa10442008-12-18 19:37:40 +0000764}
765
Douglas Gregor0c281a82009-02-25 19:37:18 +0000766/// \brief Replaces a template-id annotation token with a type
767/// annotation token.
768///
Douglas Gregord7cb0372009-04-01 21:51:26 +0000769/// If there was a failure when forming the type from the template-id,
770/// a type annotation token will still be created, but will have a
771/// NULL type pointer to signify an error.
772void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000773 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
774
775 TemplateIdAnnotation *TemplateId
776 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000777 assert((TemplateId->Kind == TNK_Type_template ||
778 TemplateId->Kind == TNK_Dependent_template_name) &&
779 "Only works for type and dependent templates");
Douglas Gregor0c281a82009-02-25 19:37:18 +0000780
781 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
782 TemplateId->getTemplateArgs(),
783 TemplateId->getTemplateArgIsType(),
784 TemplateId->NumArgs);
785
786 Action::TypeResult Type
Douglas Gregordd13e842009-03-30 22:58:21 +0000787 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
788 TemplateId->TemplateNameLoc,
789 TemplateId->LAngleLoc,
790 TemplateArgsPtr,
791 TemplateId->getTemplateArgLocations(),
792 TemplateId->RAngleLoc);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000793 // Create the new "type" annotation token.
794 Tok.setKind(tok::annot_typename);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000795 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor0c281a82009-02-25 19:37:18 +0000796 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
797 Tok.setLocation(SS->getBeginLoc());
798
799 // We might be backtracking, in which case we need to replace the
800 // template-id annotation token with the type annotation within the
801 // set of cached tokens. That way, we won't try to form the same
802 // class template specialization again.
803 PP.ReplaceLastTokenWithAnnotation(Tok);
804 TemplateId->Destroy();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000805}
806
Douglas Gregor2fa10442008-12-18 19:37:40 +0000807/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
808///
809/// template-argument: [C++ 14.2]
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000810/// constant-expression
Douglas Gregor2fa10442008-12-18 19:37:40 +0000811/// type-id
812/// id-expression
Douglas Gregor6f37b582009-02-09 19:34:22 +0000813void *Parser::ParseTemplateArgument(bool &ArgIsType) {
Douglas Gregor8e458f42009-02-09 18:46:07 +0000814 // C++ [temp.arg]p2:
815 // In a template-argument, an ambiguity between a type-id and an
816 // expression is resolved to a type-id, regardless of the form of
817 // the corresponding template-parameter.
818 //
819 // Therefore, we initially try to parse a type-id.
Douglas Gregor341ac792009-02-10 00:53:15 +0000820 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregor6f37b582009-02-09 19:34:22 +0000821 ArgIsType = true;
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000822 TypeResult TypeArg = ParseTypeName();
823 if (TypeArg.isInvalid())
824 return 0;
825 return TypeArg.get();
Douglas Gregor8e458f42009-02-09 18:46:07 +0000826 }
827
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000828 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000829 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregor6f37b582009-02-09 19:34:22 +0000830 return 0;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000831
Douglas Gregor6f37b582009-02-09 19:34:22 +0000832 ArgIsType = false;
833 return ExprArg.release();
Douglas Gregor2fa10442008-12-18 19:37:40 +0000834}
835
836/// ParseTemplateArgumentList - Parse a C++ template-argument-list
837/// (C++ [temp.names]). Returns true if there was an error.
838///
839/// template-argument-list: [C++ 14.2]
840/// template-argument
841/// template-argument-list ',' template-argument
Douglas Gregor6f37b582009-02-09 19:34:22 +0000842bool
843Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs,
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000844 TemplateArgIsTypeList &TemplateArgIsType,
845 TemplateArgLocationList &TemplateArgLocations) {
Douglas Gregor2fa10442008-12-18 19:37:40 +0000846 while (true) {
Douglas Gregor6f37b582009-02-09 19:34:22 +0000847 bool IsType = false;
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000848 SourceLocation Loc = Tok.getLocation();
Douglas Gregor6f37b582009-02-09 19:34:22 +0000849 void *Arg = ParseTemplateArgument(IsType);
850 if (Arg) {
851 TemplateArgs.push_back(Arg);
852 TemplateArgIsType.push_back(IsType);
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000853 TemplateArgLocations.push_back(Loc);
Douglas Gregor6f37b582009-02-09 19:34:22 +0000854 } else {
Douglas Gregor2fa10442008-12-18 19:37:40 +0000855 SkipUntil(tok::comma, tok::greater, true, true);
856 return true;
857 }
Douglas Gregor6f37b582009-02-09 19:34:22 +0000858
Douglas Gregor2fa10442008-12-18 19:37:40 +0000859 // If the next token is a comma, consume it and keep reading
860 // arguments.
861 if (Tok.isNot(tok::comma)) break;
862
863 // Consume the comma.
864 ConsumeToken();
865 }
866
Douglas Gregorf2d87392009-02-25 23:02:36 +0000867 return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater);
Douglas Gregor2fa10442008-12-18 19:37:40 +0000868}
869
Douglas Gregore3298aa2009-05-12 21:31:51 +0000870/// \brief Parse a C++ explicit template instantiation
871/// (C++ [temp.explicit]).
872///
873/// explicit-instantiation:
Douglas Gregor7a374722009-09-04 06:33:52 +0000874/// 'extern' [opt] 'template' declaration
875///
876/// Note that the 'extern' is a GNU extension and C++0x feature.
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000877Parser::DeclPtrTy
Douglas Gregor7a374722009-09-04 06:33:52 +0000878Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
879 SourceLocation TemplateLoc,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000880 SourceLocation &DeclEnd) {
881 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
Douglas Gregor7a374722009-09-04 06:33:52 +0000882 ParsedTemplateInfo(ExternLoc,
883 TemplateLoc),
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000884 DeclEnd, AS_none);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000885}