blob: 8e63fb89db3177fc909ae3ed091df832af69f156 [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 Gregorc3058332009-08-24 23:03:25 +000018#include "llvm/Support/Compiler.h"
Douglas Gregoradcac882008-12-01 23:54:00 +000019using namespace clang;
20
Douglas Gregor4d9a16f2009-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))
Mike Stump1eb44332009-09-09 15:08:12 +000028 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
Douglas Gregor45f96552009-09-04 06:33:52 +000029 DeclEnd);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000030
31 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
32}
33
Douglas Gregorc3058332009-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:
Mike Stump1eb44332009-09-09 15:08:12 +000041 explicit TemplateParameterDepthCounter(unsigned &Depth)
Douglas Gregorc3058332009-08-24 23:03:25 +000042 : Depth(Depth), AddedLevels(0) { }
Mike Stump1eb44332009-09-09 15:08:12 +000043
Douglas Gregorc3058332009-08-24 23:03:25 +000044 ~TemplateParameterDepthCounter() {
45 Depth -= AddedLevels;
46 }
Mike Stump1eb44332009-09-09 15:08:12 +000047
48 void operator++() {
Douglas Gregorc3058332009-08-24 23:03:25 +000049 ++Depth;
50 ++AddedLevels;
51 }
Mike Stump1eb44332009-09-09 15:08:12 +000052
Douglas Gregorc3058332009-08-24 23:03:25 +000053 operator unsigned() const { return Depth; }
54 };
55}
56
Douglas Gregorcc636682009-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 Gregoradcac882008-12-01 23:54:00 +000066///
67/// template-declaration: [C++ temp]
68/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregorcc636682009-02-17 23:15:12 +000069///
70/// explicit-specialization: [ C++ temp.expl.spec]
71/// 'template' '<' '>' declaration
Chris Lattnerb28317a2009-03-28 19:18:32 +000072Parser::DeclPtrTy
Anders Carlsson5aeccdb2009-03-26 00:52:18 +000073Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +000074 SourceLocation &DeclEnd,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +000075 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +000076 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
77 "Token does not start a template declaration.");
78
Douglas Gregor26236e82008-12-02 00:41:28 +000079 // Enter template-parameter scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +000080 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregor26236e82008-12-02 00:41:28 +000081
Douglas Gregorc4b4e7b2008-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 Gregorcc636682009-02-17 23:15:12 +000091 // lists to easily differentiate between the case above and:
Douglas Gregorc4b4e7b2008-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 Gregor0f499d92009-08-20 18:46:05 +0000103 bool isSpecialization = true;
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000104 TemplateParameterLists ParamLists;
Douglas Gregorc3058332009-08-24 23:03:25 +0000105 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
Douglas Gregorc4b4e7b2008-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 Lattnerb28317a2009-03-28 19:18:32 +0000119 return DeclPtrTy();
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000120 }
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000122 // Parse the '<' template-parameter-list '>'
123 SourceLocation LAngleLoc, RAngleLoc;
124 TemplateParameterList TemplateParams;
Mike Stump1eb44332009-09-09 15:08:12 +0000125 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
Douglas Gregor7cdbc582009-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();
Mike Stump1eb44332009-09-09 15:08:12 +0000131 return DeclPtrTy();
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000132 }
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000133
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000134 ParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000135 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
136 TemplateLoc, LAngleLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000137 TemplateParams.data(),
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000138 TemplateParams.size(), RAngleLoc));
Douglas Gregorc3058332009-08-24 23:03:25 +0000139
140 if (!TemplateParams.empty()) {
141 isSpecialization = false;
142 ++Depth;
Mike Stump1eb44332009-09-09 15:08:12 +0000143 }
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000144 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
145
146 // Parse the actual template declaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000147 return ParseSingleDeclarationAfterTemplate(Context,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000148 ParsedTemplateInfo(&ParamLists,
Douglas Gregor0f499d92009-08-20 18:46:05 +0000149 isSpecialization),
Douglas Gregor1426e532009-05-12 21:31:51 +0000150 DeclEnd, AS);
151}
Chris Lattner682bf922009-03-29 16:50:03 +0000152
Douglas Gregor1426e532009-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.
Mike Stump1eb44332009-09-09 15:08:12 +0000173Parser::DeclPtrTy
Douglas Gregor1426e532009-05-12 21:31:51 +0000174Parser::ParseSingleDeclarationAfterTemplate(
175 unsigned Context,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000176 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor1426e532009-05-12 21:31:51 +0000177 SourceLocation &DeclEnd,
178 AccessSpecifier AS) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000179 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
180 "Template information required");
181
Douglas Gregor37b372b2009-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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Douglas Gregor1426e532009-05-12 21:31:51 +0000188 // Parse the declaration specifiers.
189 DeclSpec DS;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000190 ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
Douglas Gregor1426e532009-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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Douglas Gregor1426e532009-05-12 21:31:51 +0000209 // If we have a declaration or declarator list, handle it.
210 if (isDeclarationAfterDeclarator()) {
211 // Parse this declaration.
Douglas Gregore542c862009-06-23 23:11:28 +0000212 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
213 TemplateInfo);
Douglas Gregor1426e532009-05-12 21:31:51 +0000214
215 if (Tok.is(tok::comma)) {
216 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000217 << (int)TemplateInfo.Kind;
Douglas Gregor1426e532009-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 McCall5c15fe12009-07-31 02:20:35 +0000223 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
Douglas Gregor1426e532009-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 Gregor52591bf2009-06-24 00:54:41 +0000243 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregor1426e532009-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 Gregoradcac882008-12-01 23:54:00 +0000252}
253
254/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregoraaba5e32009-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
Mike Stump1eb44332009-09-09 15:08:12 +0000259/// RAngleLoc will receive the positions of the '<' and '>', respectively,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000260/// that enclose this template parameter list.
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000261///
262/// \returns true if an error occurred, false otherwise.
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000263bool Parser::ParseTemplateParameters(unsigned Depth,
264 TemplateParameterList &TemplateParams,
265 SourceLocation &LAngleLoc,
266 SourceLocation &RAngleLoc) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000267 // Get the template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +0000268 if (!Tok.is(tok::less)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000269 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000270 return true;
Douglas Gregoradcac882008-12-01 23:54:00 +0000271 }
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000272 LAngleLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Douglas Gregoradcac882008-12-01 23:54:00 +0000274 // Try to parse the template parameter list.
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000275 if (Tok.is(tok::greater))
276 RAngleLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000277 else if (ParseTemplateParameterList(Depth, TemplateParams)) {
278 if (!Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000279 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000280 return true;
Douglas Gregoradcac882008-12-01 23:54:00 +0000281 }
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000282 RAngleLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000283 }
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000284 return false;
Douglas Gregoradcac882008-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
Mike Stump1eb44332009-09-09 15:08:12 +0000290/// a statement, etc.) and return false.
Douglas Gregoradcac882008-12-01 23:54:00 +0000291///
292/// template-parameter-list: [C++ temp]
293/// template-parameter
294/// template-parameter-list ',' template-parameter
Mike Stump1eb44332009-09-09 15:08:12 +0000295bool
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000296Parser::ParseTemplateParameterList(unsigned Depth,
297 TemplateParameterList &TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000298 while (1) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000299 if (DeclPtrTy TmpParam
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000300 = ParseTemplateParameter(Depth, TemplateParams.size())) {
301 TemplateParams.push_back(TmpParam);
302 } else {
Douglas Gregoradcac882008-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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregoradcac882008-12-01 23:54:00 +0000308 // Did we find a comma or the end of the template parmeter list?
Mike Stump1eb44332009-09-09 15:08:12 +0000309 if (Tok.is(tok::comma)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000310 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000311 } else if (Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000312 // 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 Carlssonce5635a2009-06-12 23:09:56 +0000335/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000336/// 'class' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000337/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000338/// 'typename' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000339/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000340/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Mike Stump1eb44332009-09-09 15:08:12 +0000341Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000342Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Mike Stump1eb44332009-09-09 15:08:12 +0000343 if (Tok.is(tok::kw_class) ||
344 (Tok.is(tok::kw_typename) &&
345 // FIXME: Next token has not been annotated!
346 NextToken().isNot(tok::annot_typename))) {
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000347 return ParseTypeParameter(Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
350 if (Tok.is(tok::kw_template))
Chris Lattner532e19b2009-01-04 23:51:17 +0000351 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 Gregoradcac882008-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 Carlssonce5635a2009-06-12 23:09:56 +0000364/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000365/// 'class' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000366/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000367/// 'typename' identifier[opt] '=' type-id
Chris Lattnerb28317a2009-03-28 19:18:32 +0000368Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregor26236e82008-12-02 00:41:28 +0000369 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000370 "A type-parameter starts with 'class' or 'typename'");
Douglas Gregor26236e82008-12-02 00:41:28 +0000371
372 // Consume the 'class' or 'typename' keyword.
373 bool TypenameKeyword = Tok.is(tok::kw_typename);
374 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000375
Anders Carlsson941df7d2009-06-12 19:58:00 +0000376 // Grab the ellipsis (if given).
377 bool Ellipsis = false;
378 SourceLocation EllipsisLoc;
Anders Carlssonce5635a2009-06-12 23:09:56 +0000379 if (Tok.is(tok::ellipsis)) {
Anders Carlsson941df7d2009-06-12 19:58:00 +0000380 Ellipsis = true;
381 EllipsisLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000382
383 if (!getLang().CPlusPlus0x)
Anders Carlssonce5635a2009-06-12 23:09:56 +0000384 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson941df7d2009-06-12 19:58:00 +0000385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregoradcac882008-12-01 23:54:00 +0000387 // Grab the template parameter name (if given)
Douglas Gregor26236e82008-12-02 00:41:28 +0000388 SourceLocation NameLoc;
389 IdentifierInfo* ParamName = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000390 if (Tok.is(tok::identifier)) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000391 ParamName = Tok.getIdentifierInfo();
392 NameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000393 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
394 Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000395 // 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 Lattnerb28317a2009-03-28 19:18:32 +0000399 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattnerb28317a2009-03-28 19:18:32 +0000402 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson941df7d2009-06-12 19:58:00 +0000403 Ellipsis, EllipsisLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000404 KeyLoc, ParamName, NameLoc,
405 Depth, Position);
Douglas Gregor26236e82008-12-02 00:41:28 +0000406
Douglas Gregoradcac882008-12-01 23:54:00 +0000407 // Grab a default type id (if given).
Mike Stump1eb44332009-09-09 15:08:12 +0000408 if (Tok.is(tok::equal)) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000409 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregord684b002009-02-10 19:49:53 +0000410 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +0000411 TypeResult DefaultType = ParseTypeName();
412 if (!DefaultType.isInvalid())
Douglas Gregord684b002009-02-10 19:49:53 +0000413 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +0000414 DefaultType.get());
Douglas Gregoradcac882008-12-01 23:54:00 +0000415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Douglas Gregor26236e82008-12-02 00:41:28 +0000417 return TypeParam;
Douglas Gregoradcac882008-12-01 23:54:00 +0000418}
419
420/// ParseTemplateTemplateParameter - Handle the parsing of template
Mike Stump1eb44332009-09-09 15:08:12 +0000421/// template parameters.
Douglas Gregoradcac882008-12-01 23:54:00 +0000422///
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 Lattnerb28317a2009-03-28 19:18:32 +0000426Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000427Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoradcac882008-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();
Mike Stump1eb44332009-09-09 15:08:12 +0000432 TemplateParameterList TemplateParams;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000433 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor68c69932009-02-10 19:52:54 +0000434 {
435 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000436 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000437 RAngleLoc)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000438 return DeclPtrTy();
Douglas Gregor68c69932009-02-10 19:52:54 +0000439 }
Douglas Gregoradcac882008-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.
Mike Stump1eb44332009-09-09 15:08:12 +0000444 if (!Tok.is(tok::kw_class)) {
445 Diag(Tok.getLocation(), diag::err_expected_class_before)
Douglas Gregoradcac882008-12-01 23:54:00 +0000446 << PP.getSpelling(Tok);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000447 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000448 }
449 SourceLocation ClassLoc = ConsumeToken();
450
451 // Get the identifier, if given.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000452 SourceLocation NameLoc;
453 IdentifierInfo* ParamName = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000454 if (Tok.is(tok::identifier)) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000455 ParamName = Tok.getIdentifierInfo();
456 NameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000457 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000458 // 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 Lattnerb28317a2009-03-28 19:18:32 +0000462 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000463 }
464
Mike Stump1eb44332009-09-09 15:08:12 +0000465 TemplateParamsTy *ParamList =
Douglas Gregorddc29e12009-02-06 22:42:48 +0000466 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
467 TemplateLoc, LAngleLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000468 &TemplateParams[0],
Douglas Gregorddc29e12009-02-06 22:42:48 +0000469 TemplateParams.size(),
470 RAngleLoc);
471
Chris Lattnerb28317a2009-03-28 19:18:32 +0000472 Parser::DeclPtrTy Param
Douglas Gregord684b002009-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 Gregoradcac882008-12-01 23:54:00 +0000489}
490
491/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
Mike Stump1eb44332009-09-09 15:08:12 +0000492/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000493///
Douglas Gregoradcac882008-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 Gregor26236e82008-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.
Mike Stump1eb44332009-09-09 15:08:12 +0000504Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000505Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000506 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregoradcac882008-12-01 23:54:00 +0000507
508 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregor26236e82008-12-02 00:41:28 +0000509 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregoradcac882008-12-01 23:54:00 +0000510 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregor26236e82008-12-02 00:41:28 +0000511 DeclSpec DS;
512 ParseDeclarationSpecifiers(DS);
Douglas Gregoradcac882008-12-01 23:54:00 +0000513
514 // Parse this as a typename.
Douglas Gregor26236e82008-12-02 00:41:28 +0000515 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
516 ParseDeclarator(ParamDecl);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000517 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregoradcac882008-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 Lattnerb28317a2009-03-28 19:18:32 +0000524 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000525 }
526
Mike Stump1eb44332009-09-09 15:08:12 +0000527 // Create the parameter.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000528 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
529 Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000530
Douglas Gregord684b002009-02-10 19:49:53 +0000531 // If there is a default value, parse it.
Chris Lattner7452c6f2009-01-05 01:24:05 +0000532 if (Tok.is(tok::equal)) {
Douglas Gregord684b002009-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.
Mike Stump1eb44332009-09-09 15:08:12 +0000540 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000541
542 OwningExprResult DefaultArg = ParseAssignmentExpression();
543 if (DefaultArg.isInvalid())
544 SkipUntil(tok::comma, tok::greater, true, true);
545 else if (Param)
Mike Stump1eb44332009-09-09 15:08:12 +0000546 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
Douglas Gregord684b002009-02-10 19:49:53 +0000547 move(DefaultArg));
Douglas Gregoradcac882008-12-01 23:54:00 +0000548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Douglas Gregor26236e82008-12-02 00:41:28 +0000550 return Param;
Douglas Gregoradcac882008-12-01 23:54:00 +0000551}
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000552
Douglas Gregorcc636682009-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).
Mike Stump1eb44332009-09-09 15:08:12 +0000571bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000572Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000573 SourceLocation TemplateNameLoc,
Douglas Gregorcc636682009-02-17 23:15:12 +0000574 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 Gregor3965b7b2009-02-25 23:02:36 +0000602 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
Douglas Gregorcc636682009-02-17 23:15:12 +0000603 return true;
604
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000605 // Determine the location of the '>' or '>>'. Only consume this
606 // token if the caller asked us to.
Douglas Gregorcc636682009-02-17 23:15:12 +0000607 RAngleLoc = Tok.getLocation();
608
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000609 if (Tok.is(tok::greatergreater)) {
Douglas Gregor4b2d3f72009-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 Gregorb2fb6de2009-02-27 17:53:17 +0000616 << CodeModificationHint::CreateReplacement(
617 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000618 }
Douglas Gregor3965b7b2009-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 Gregorcc636682009-02-17 23:15:12 +0000627 ConsumeToken();
628
629 return false;
630}
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Douglas Gregor39a8de12009-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 Lattnerc8e27cc2009-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,
Mike Stump1eb44332009-09-09 15:08:12 +0000670 const CXXScopeSpec *SS,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000671 SourceLocation TemplateKWLoc,
672 bool AllowTypeAnnotation) {
Douglas Gregord6fb7ef2008-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 Gregor39a8de12009-02-25 19:37:18 +0000678 IdentifierInfo *Name = Tok.getIdentifierInfo();
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000679 SourceLocation TemplateNameLoc = ConsumeToken();
680
Douglas Gregorcc636682009-02-17 23:15:12 +0000681 // Parse the enclosed template argument list.
682 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000683 TemplateArgList TemplateArgs;
684 TemplateArgIsTypeList TemplateArgIsType;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000685 TemplateArgLocationList TemplateArgLocations;
Douglas Gregorcc636682009-02-17 23:15:12 +0000686 bool Invalid = ParseTemplateIdAfterTemplateName(Template, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000687 SS, false, LAngleLoc,
688 TemplateArgs,
Douglas Gregorcc636682009-02-17 23:15:12 +0000689 TemplateArgIsType,
690 TemplateArgLocations,
691 RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000693 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 Gregorc15cb382009-02-09 23:23:08 +0000700
Jay Foadbeaaccd2009-05-21 09:52:38 +0000701 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
702 TemplateArgIsType.data(),
Douglas Gregorcc636682009-02-17 23:15:12 +0000703 TemplateArgs.size());
Douglas Gregorf02da892009-02-09 21:04:56 +0000704
Douglas Gregor55f6b142009-02-09 18:46:07 +0000705 // Build the annotation token.
Douglas Gregorc45c2322009-03-31 00:43:58 +0000706 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Mike Stump1eb44332009-09-09 15:08:12 +0000707 Action::TypeResult Type
Douglas Gregor7532dc62009-03-30 22:58:21 +0000708 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
709 LAngleLoc, TemplateArgsPtr,
710 &TemplateArgLocations[0],
711 RAngleLoc);
Chris Lattnerc8e27cc2009-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 Gregorcc636682009-02-17 23:15:12 +0000719
720 Tok.setKind(tok::annot_typename);
721 Tok.setAnnotationValue(Type.get());
Douglas Gregor39a8de12009-02-25 19:37:18 +0000722 if (SS && SS->isNotEmpty())
723 Tok.setLocation(SS->getBeginLoc());
724 else if (TemplateKWLoc.isValid())
725 Tok.setLocation(TemplateKWLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000726 else
Douglas Gregor39a8de12009-02-25 19:37:18 +0000727 Tok.setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +0000728 } else {
Douglas Gregorc45c2322009-03-31 00:43:58 +0000729 // Build a template-id annotation token that can be processed
730 // later.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000731 Tok.setKind(tok::annot_template_id);
Mike Stump1eb44332009-09-09 15:08:12 +0000732 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000733 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor55f6b142009-02-09 18:46:07 +0000734 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000735 TemplateId->Name = Name;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000736 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000737 TemplateId->Kind = TNK;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000738 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor39a8de12009-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 Gregor55f6b142009-02-09 18:46:07 +0000744 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor39a8de12009-02-25 19:37:18 +0000745 ArgIsType[Arg] = TemplateArgIsType[Arg];
746 ArgLocs[Arg] = TemplateArgLocations[Arg];
747 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000748 Tok.setAnnotationValue(TemplateId);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000749 if (TemplateKWLoc.isValid())
750 Tok.setLocation(TemplateKWLoc);
751 else
752 Tok.setLocation(TemplateNameLoc);
753
754 TemplateArgsPtr.release();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000755 }
756
757 // Common fields for the annotation token
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000758 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000759
Douglas Gregord6fb7ef2008-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 Lattnerc8e27cc2009-06-26 04:27:47 +0000763 return false;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000764}
765
Douglas Gregor39a8de12009-02-25 19:37:18 +0000766/// \brief Replaces a template-id annotation token with a type
767/// annotation token.
768///
Douglas Gregor31a19b62009-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 Gregor39a8de12009-02-25 19:37:18 +0000773 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
774
Mike Stump1eb44332009-09-09 15:08:12 +0000775 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000776 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-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");
Mike Stump1eb44332009-09-09 15:08:12 +0000780
781 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000782 TemplateId->getTemplateArgs(),
783 TemplateId->getTemplateArgIsType(),
784 TemplateId->NumArgs);
785
Mike Stump1eb44332009-09-09 15:08:12 +0000786 Action::TypeResult Type
Douglas Gregor7532dc62009-03-30 22:58:21 +0000787 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
788 TemplateId->TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000789 TemplateId->LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000790 TemplateArgsPtr,
791 TemplateId->getTemplateArgLocations(),
792 TemplateId->RAngleLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000793 // Create the new "type" annotation token.
794 Tok.setKind(tok::annot_typename);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000795 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor39a8de12009-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 Gregor39a8de12009-02-25 19:37:18 +0000805}
806
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000807/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
808///
809/// template-argument: [C++ 14.2]
Douglas Gregorac7610d2009-06-22 20:57:11 +0000810/// constant-expression
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000811/// type-id
812/// id-expression
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000813void *Parser::ParseTemplateArgument(bool &ArgIsType) {
Douglas Gregor55f6b142009-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 Gregor8b642592009-02-10 00:53:15 +0000820 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000821 ArgIsType = true;
Douglas Gregor809070a2009-02-18 17:45:20 +0000822 TypeResult TypeArg = ParseTypeName();
823 if (TypeArg.isInvalid())
824 return 0;
825 return TypeArg.get();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000826 }
827
Douglas Gregorac7610d2009-06-22 20:57:11 +0000828 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregorc15cb382009-02-09 23:23:08 +0000829 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000830 return 0;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000831
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000832 ArgIsType = false;
833 return ExprArg.release();
Douglas Gregord6fb7ef2008-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
Mike Stump1eb44332009-09-09 15:08:12 +0000842bool
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000843Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs,
Douglas Gregorc15cb382009-02-09 23:23:08 +0000844 TemplateArgIsTypeList &TemplateArgIsType,
845 TemplateArgLocationList &TemplateArgLocations) {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000846 while (true) {
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000847 bool IsType = false;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000848 SourceLocation Loc = Tok.getLocation();
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000849 void *Arg = ParseTemplateArgument(IsType);
850 if (Arg) {
851 TemplateArgs.push_back(Arg);
852 TemplateArgIsType.push_back(IsType);
Douglas Gregorc15cb382009-02-09 23:23:08 +0000853 TemplateArgLocations.push_back(Loc);
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000854 } else {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000855 SkipUntil(tok::comma, tok::greater, true, true);
856 return true;
857 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000858
Douglas Gregord6fb7ef2008-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 Gregor3965b7b2009-02-25 23:02:36 +0000867 return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater);
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000868}
869
Mike Stump1eb44332009-09-09 15:08:12 +0000870/// \brief Parse a C++ explicit template instantiation
Douglas Gregor1426e532009-05-12 21:31:51 +0000871/// (C++ [temp.explicit]).
872///
873/// explicit-instantiation:
Douglas Gregor45f96552009-09-04 06:33:52 +0000874/// 'extern' [opt] 'template' declaration
875///
876/// Note that the 'extern' is a GNU extension and C++0x feature.
Mike Stump1eb44332009-09-09 15:08:12 +0000877Parser::DeclPtrTy
Douglas Gregor45f96552009-09-04 06:33:52 +0000878Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
879 SourceLocation TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000880 SourceLocation &DeclEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000881 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
Douglas Gregor45f96552009-09-04 06:33:52 +0000882 ParsedTemplateInfo(ExternLoc,
883 TemplateLoc),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000884 DeclEnd, AS_none);
Douglas Gregor1426e532009-05-12 21:31:51 +0000885}