blob: 8b8af99ec6d773fab543f13483fa5d820ac7d5c2 [file] [log] [blame]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001//===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements parsing of C++ templates.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregoreb31f392008-12-01 23:54:00 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
Douglas Gregorb53edfb2009-11-10 19:49:08 +000018#include "clang/Parse/Template.h"
Chris Lattnerb2434d52009-12-10 00:45:15 +000019#include "RAIIObjectsForParser.h"
Douglas Gregoreb31f392008-12-01 23:54:00 +000020using namespace clang;
21
Douglas Gregor1b57ff32009-05-12 23:25:50 +000022/// \brief Parse a template declaration, explicit instantiation, or
23/// explicit specialization.
24Parser::DeclPtrTy
25Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
26 SourceLocation &DeclEnd,
27 AccessSpecifier AS) {
28 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less))
Mike Stump11289f42009-09-09 15:08:12 +000029 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
Douglas Gregor43e75172009-09-04 06:33:52 +000030 DeclEnd);
Douglas Gregor1b57ff32009-05-12 23:25:50 +000031
32 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
33}
34
Douglas Gregora3dff8e2009-08-24 23:03:25 +000035/// \brief RAII class that manages the template parameter depth.
36namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +000037 class TemplateParameterDepthCounter {
Douglas Gregora3dff8e2009-08-24 23:03:25 +000038 unsigned &Depth;
39 unsigned AddedLevels;
40
41 public:
Mike Stump11289f42009-09-09 15:08:12 +000042 explicit TemplateParameterDepthCounter(unsigned &Depth)
Douglas Gregora3dff8e2009-08-24 23:03:25 +000043 : Depth(Depth), AddedLevels(0) { }
Mike Stump11289f42009-09-09 15:08:12 +000044
Douglas Gregora3dff8e2009-08-24 23:03:25 +000045 ~TemplateParameterDepthCounter() {
46 Depth -= AddedLevels;
47 }
Mike Stump11289f42009-09-09 15:08:12 +000048
49 void operator++() {
Douglas Gregora3dff8e2009-08-24 23:03:25 +000050 ++Depth;
51 ++AddedLevels;
52 }
Mike Stump11289f42009-09-09 15:08:12 +000053
Douglas Gregora3dff8e2009-08-24 23:03:25 +000054 operator unsigned() const { return Depth; }
55 };
56}
57
Douglas Gregor67a65642009-02-17 23:15:12 +000058/// \brief Parse a template declaration or an explicit specialization.
59///
60/// Template declarations include one or more template parameter lists
61/// and either the function or class template declaration. Explicit
62/// specializations contain one or more 'template < >' prefixes
63/// followed by a (possibly templated) declaration. Since the
64/// syntactic form of both features is nearly identical, we parse all
65/// of the template headers together and let semantic analysis sort
66/// the declarations from the explicit specializations.
Douglas Gregoreb31f392008-12-01 23:54:00 +000067///
68/// template-declaration: [C++ temp]
69/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregor67a65642009-02-17 23:15:12 +000070///
71/// explicit-specialization: [ C++ temp.expl.spec]
72/// 'template' '<' '>' declaration
Chris Lattner83f095c2009-03-28 19:18:32 +000073Parser::DeclPtrTy
Anders Carlssondfbbdf62009-03-26 00:52:18 +000074Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +000075 SourceLocation &DeclEnd,
Anders Carlssondfbbdf62009-03-26 00:52:18 +000076 AccessSpecifier AS) {
Mike Stump11289f42009-09-09 15:08:12 +000077 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
78 "Token does not start a template declaration.");
79
Douglas Gregorf5586182008-12-02 00:41:28 +000080 // Enter template-parameter scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +000081 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregorf5586182008-12-02 00:41:28 +000082
Douglas Gregorb9bd8a92008-12-24 02:52:09 +000083 // Parse multiple levels of template headers within this template
84 // parameter scope, e.g.,
85 //
86 // template<typename T>
87 // template<typename U>
88 // class A<T>::B { ... };
89 //
90 // We parse multiple levels non-recursively so that we can build a
91 // single data structure containing all of the template parameter
Douglas Gregor67a65642009-02-17 23:15:12 +000092 // lists to easily differentiate between the case above and:
Douglas Gregorb9bd8a92008-12-24 02:52:09 +000093 //
94 // template<typename T>
95 // class A {
96 // template<typename U> class B;
97 // };
98 //
99 // In the first case, the action for declaring A<T>::B receives
100 // both template parameter lists. In the second case, the action for
101 // defining A<T>::B receives just the inner template parameter list
102 // (and retrieves the outer template parameter list from its
103 // context).
Douglas Gregor468535e2009-08-20 18:46:05 +0000104 bool isSpecialization = true;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000105 bool LastParamListWasEmpty = false;
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000106 TemplateParameterLists ParamLists;
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000107 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000108 do {
109 // Consume the 'export', if any.
110 SourceLocation ExportLoc;
111 if (Tok.is(tok::kw_export)) {
112 ExportLoc = ConsumeToken();
113 }
114
115 // Consume the 'template', which should be here.
116 SourceLocation TemplateLoc;
117 if (Tok.is(tok::kw_template)) {
118 TemplateLoc = ConsumeToken();
119 } else {
120 Diag(Tok.getLocation(), diag::err_expected_template);
Chris Lattner83f095c2009-03-28 19:18:32 +0000121 return DeclPtrTy();
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000124 // Parse the '<' template-parameter-list '>'
125 SourceLocation LAngleLoc, RAngleLoc;
126 TemplateParameterList TemplateParams;
Mike Stump11289f42009-09-09 15:08:12 +0000127 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000128 RAngleLoc)) {
129 // Skip until the semi-colon or a }.
130 SkipUntil(tok::r_brace, true, true);
131 if (Tok.is(tok::semi))
132 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000133 return DeclPtrTy();
Douglas Gregore93e46c2009-07-22 23:48:44 +0000134 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000135
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000136 ParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000137 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
138 TemplateLoc, LAngleLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000139 TemplateParams.data(),
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000140 TemplateParams.size(), RAngleLoc));
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000141
142 if (!TemplateParams.empty()) {
143 isSpecialization = false;
144 ++Depth;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000145 } else {
146 LastParamListWasEmpty = true;
Mike Stump11289f42009-09-09 15:08:12 +0000147 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000148 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
149
150 // Parse the actual template declaration.
Mike Stump11289f42009-09-09 15:08:12 +0000151 return ParseSingleDeclarationAfterTemplate(Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000152 ParsedTemplateInfo(&ParamLists,
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000153 isSpecialization,
154 LastParamListWasEmpty),
Douglas Gregor23996282009-05-12 21:31:51 +0000155 DeclEnd, AS);
156}
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000157
Douglas Gregor23996282009-05-12 21:31:51 +0000158/// \brief Parse a single declaration that declares a template,
159/// template specialization, or explicit instantiation of a template.
160///
161/// \param TemplateParams if non-NULL, the template parameter lists
162/// that preceded this declaration. In this case, the declaration is a
163/// template declaration, out-of-line definition of a template, or an
164/// explicit template specialization. When NULL, the declaration is an
165/// explicit template instantiation.
166///
167/// \param TemplateLoc when TemplateParams is NULL, the location of
168/// the 'template' keyword that indicates that we have an explicit
169/// template instantiation.
170///
171/// \param DeclEnd will receive the source location of the last token
172/// within this declaration.
173///
174/// \param AS the access specifier associated with this
175/// declaration. Will be AS_none for namespace-scope declarations.
176///
177/// \returns the new declaration.
Mike Stump11289f42009-09-09 15:08:12 +0000178Parser::DeclPtrTy
Douglas Gregor23996282009-05-12 21:31:51 +0000179Parser::ParseSingleDeclarationAfterTemplate(
180 unsigned Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000181 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor23996282009-05-12 21:31:51 +0000182 SourceLocation &DeclEnd,
183 AccessSpecifier AS) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000184 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
185 "Template information required");
186
Douglas Gregor3447e762009-08-20 22:52:58 +0000187 if (Context == Declarator::MemberContext) {
188 // We are parsing a member template.
189 ParseCXXClassMemberDeclaration(AS, TemplateInfo);
190 return DeclPtrTy::make((void*)0);
191 }
Mike Stump11289f42009-09-09 15:08:12 +0000192
Douglas Gregor23996282009-05-12 21:31:51 +0000193 // Parse the declaration specifiers.
John McCall28a6aea2009-11-04 02:18:39 +0000194 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000195
196 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
197 DS.AddAttributes(ParseCXX0XAttributes().AttrList);
198
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000199 ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
Douglas Gregor23996282009-05-12 21:31:51 +0000200
201 if (Tok.is(tok::semi)) {
202 DeclEnd = ConsumeToken();
John McCall28a6aea2009-11-04 02:18:39 +0000203 DeclPtrTy Decl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
204 DS.complete(Decl);
205 return Decl;
Douglas Gregor23996282009-05-12 21:31:51 +0000206 }
207
208 // Parse the declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000209 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
Douglas Gregor23996282009-05-12 21:31:51 +0000210 ParseDeclarator(DeclaratorInfo);
211 // Error parsing the declarator?
212 if (!DeclaratorInfo.hasName()) {
213 // If so, skip until the semi-colon or a }.
214 SkipUntil(tok::r_brace, true, true);
215 if (Tok.is(tok::semi))
216 ConsumeToken();
217 return DeclPtrTy();
218 }
Mike Stump11289f42009-09-09 15:08:12 +0000219
Douglas Gregor23996282009-05-12 21:31:51 +0000220 // If we have a declaration or declarator list, handle it.
221 if (isDeclarationAfterDeclarator()) {
222 // Parse this declaration.
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000223 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
224 TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000225
226 if (Tok.is(tok::comma)) {
227 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000228 << (int)TemplateInfo.Kind;
Douglas Gregor23996282009-05-12 21:31:51 +0000229 SkipUntil(tok::semi, true, false);
230 return ThisDecl;
231 }
232
233 // Eat the semi colon after the declaration.
John McCallef50e992009-07-31 02:20:35 +0000234 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
John McCall28a6aea2009-11-04 02:18:39 +0000235 DS.complete(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +0000236 return ThisDecl;
237 }
238
239 if (DeclaratorInfo.isFunctionDeclarator() &&
240 isStartOfFunctionDefinition()) {
241 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
242 Diag(Tok, diag::err_function_declared_typedef);
243
244 if (Tok.is(tok::l_brace)) {
245 // This recovery skips the entire function body. It would be nice
246 // to simply call ParseFunctionDefinition() below, however Sema
247 // assumes the declarator represents a function, not a typedef.
248 ConsumeBrace();
249 SkipUntil(tok::r_brace, true);
250 } else {
251 SkipUntil(tok::semi);
252 }
253 return DeclPtrTy();
254 }
Douglas Gregor17a7c122009-06-24 00:54:41 +0000255 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000256 }
257
258 if (DeclaratorInfo.isFunctionDeclarator())
259 Diag(Tok, diag::err_expected_fn_body);
260 else
261 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
262 SkipUntil(tok::semi);
263 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000264}
265
266/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000267/// angle brackets. Depth is the depth of this template-parameter-list, which
268/// is the number of template headers directly enclosing this template header.
269/// TemplateParams is the current list of template parameters we're building.
270/// The template parameter we parse will be added to this list. LAngleLoc and
Mike Stump11289f42009-09-09 15:08:12 +0000271/// RAngleLoc will receive the positions of the '<' and '>', respectively,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000272/// that enclose this template parameter list.
Douglas Gregore93e46c2009-07-22 23:48:44 +0000273///
274/// \returns true if an error occurred, false otherwise.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000275bool Parser::ParseTemplateParameters(unsigned Depth,
276 TemplateParameterList &TemplateParams,
277 SourceLocation &LAngleLoc,
278 SourceLocation &RAngleLoc) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000279 // Get the template parameter list.
Mike Stump11289f42009-09-09 15:08:12 +0000280 if (!Tok.is(tok::less)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000281 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregore93e46c2009-07-22 23:48:44 +0000282 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000283 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000284 LAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000285
Douglas Gregoreb31f392008-12-01 23:54:00 +0000286 // Try to parse the template parameter list.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000287 if (Tok.is(tok::greater))
288 RAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000289 else if (ParseTemplateParameterList(Depth, TemplateParams)) {
290 if (!Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000291 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregore93e46c2009-07-22 23:48:44 +0000292 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000293 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000294 RAngleLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000295 }
Douglas Gregore93e46c2009-07-22 23:48:44 +0000296 return false;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000297}
298
299/// ParseTemplateParameterList - Parse a template parameter list. If
300/// the parsing fails badly (i.e., closing bracket was left out), this
301/// will try to put the token stream in a reasonable position (closing
Mike Stump11289f42009-09-09 15:08:12 +0000302/// a statement, etc.) and return false.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000303///
304/// template-parameter-list: [C++ temp]
305/// template-parameter
306/// template-parameter-list ',' template-parameter
Mike Stump11289f42009-09-09 15:08:12 +0000307bool
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000308Parser::ParseTemplateParameterList(unsigned Depth,
309 TemplateParameterList &TemplateParams) {
Mike Stump11289f42009-09-09 15:08:12 +0000310 while (1) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000311 if (DeclPtrTy TmpParam
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000312 = ParseTemplateParameter(Depth, TemplateParams.size())) {
313 TemplateParams.push_back(TmpParam);
314 } else {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000315 // If we failed to parse a template parameter, skip until we find
316 // a comma or closing brace.
317 SkipUntil(tok::comma, tok::greater, true, true);
318 }
Mike Stump11289f42009-09-09 15:08:12 +0000319
Douglas Gregoreb31f392008-12-01 23:54:00 +0000320 // Did we find a comma or the end of the template parmeter list?
Mike Stump11289f42009-09-09 15:08:12 +0000321 if (Tok.is(tok::comma)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000322 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000323 } else if (Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000324 // Don't consume this... that's done by template parser.
325 break;
326 } else {
327 // Somebody probably forgot to close the template. Skip ahead and
328 // try to get out of the expression. This error is currently
329 // subsumed by whatever goes on in ParseTemplateParameter.
330 // TODO: This could match >>, and it would be nice to avoid those
331 // silly errors with template <vec<T>>.
332 // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
333 SkipUntil(tok::greater, true, true);
334 return false;
335 }
336 }
337 return true;
338}
339
Douglas Gregor26aedb72009-11-21 02:07:55 +0000340/// \brief Determine whether the parser is at the start of a template
341/// type parameter.
342bool Parser::isStartOfTemplateTypeParameter() {
343 if (Tok.is(tok::kw_class))
344 return true;
345
346 if (Tok.isNot(tok::kw_typename))
347 return false;
348
349 // C++ [temp.param]p2:
350 // There is no semantic difference between class and typename in a
351 // template-parameter. typename followed by an unqualified-id
352 // names a template type parameter. typename followed by a
353 // qualified-id denotes the type in a non-type
354 // parameter-declaration.
355 Token Next = NextToken();
356
357 // If we have an identifier, skip over it.
358 if (Next.getKind() == tok::identifier)
359 Next = GetLookAheadToken(2);
360
361 switch (Next.getKind()) {
362 case tok::equal:
363 case tok::comma:
364 case tok::greater:
365 case tok::greatergreater:
366 case tok::ellipsis:
367 return true;
368
369 default:
370 return false;
371 }
372}
373
Douglas Gregoreb31f392008-12-01 23:54:00 +0000374/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
375///
376/// template-parameter: [C++ temp.param]
377/// type-parameter
378/// parameter-declaration
379///
380/// type-parameter: (see below)
Anders Carlssonf986ba72009-06-12 23:09:56 +0000381/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000382/// 'class' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000383/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000384/// 'typename' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000385/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000386/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Mike Stump11289f42009-09-09 15:08:12 +0000387Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000388Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor26aedb72009-11-21 02:07:55 +0000389 if (isStartOfTemplateTypeParameter())
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000390 return ParseTypeParameter(Depth, Position);
Mike Stump11289f42009-09-09 15:08:12 +0000391
392 if (Tok.is(tok::kw_template))
Chris Lattnera21db612009-01-04 23:51:17 +0000393 return ParseTemplateTemplateParameter(Depth, Position);
394
395 // If it's none of the above, then it must be a parameter declaration.
396 // NOTE: This will pick up errors in the closure of the template parameter
397 // list (e.g., template < ; Check here to implement >> style closures.
398 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000399}
400
401/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
402/// Other kinds of template parameters are parsed in
403/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
404///
405/// type-parameter: [C++ temp.param]
Anders Carlssonf986ba72009-06-12 23:09:56 +0000406/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000407/// 'class' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000408/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000409/// 'typename' identifier[opt] '=' type-id
Chris Lattner83f095c2009-03-28 19:18:32 +0000410Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregorf5586182008-12-02 00:41:28 +0000411 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
Mike Stump11289f42009-09-09 15:08:12 +0000412 "A type-parameter starts with 'class' or 'typename'");
Douglas Gregorf5586182008-12-02 00:41:28 +0000413
414 // Consume the 'class' or 'typename' keyword.
415 bool TypenameKeyword = Tok.is(tok::kw_typename);
416 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000417
Anders Carlsson01e9e932009-06-12 19:58:00 +0000418 // Grab the ellipsis (if given).
419 bool Ellipsis = false;
420 SourceLocation EllipsisLoc;
Anders Carlssonf986ba72009-06-12 23:09:56 +0000421 if (Tok.is(tok::ellipsis)) {
Anders Carlsson01e9e932009-06-12 19:58:00 +0000422 Ellipsis = true;
423 EllipsisLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000424
425 if (!getLang().CPlusPlus0x)
Anders Carlssonf986ba72009-06-12 23:09:56 +0000426 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson01e9e932009-06-12 19:58:00 +0000427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Douglas Gregoreb31f392008-12-01 23:54:00 +0000429 // Grab the template parameter name (if given)
Douglas Gregorf5586182008-12-02 00:41:28 +0000430 SourceLocation NameLoc;
431 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000432 if (Tok.is(tok::identifier)) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000433 ParamName = Tok.getIdentifierInfo();
434 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000435 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
436 Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000437 // Unnamed template parameter. Don't have to do anything here, just
438 // don't consume this token.
439 } else {
440 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +0000441 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattner83f095c2009-03-28 19:18:32 +0000444 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson01e9e932009-06-12 19:58:00 +0000445 Ellipsis, EllipsisLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000446 KeyLoc, ParamName, NameLoc,
447 Depth, Position);
Douglas Gregorf5586182008-12-02 00:41:28 +0000448
Douglas Gregoreb31f392008-12-01 23:54:00 +0000449 // Grab a default type id (if given).
Mike Stump11289f42009-09-09 15:08:12 +0000450 if (Tok.is(tok::equal)) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000451 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregordba32632009-02-10 19:49:53 +0000452 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +0000453 TypeResult DefaultType = ParseTypeName();
454 if (!DefaultType.isInvalid())
Douglas Gregordba32632009-02-10 19:49:53 +0000455 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +0000456 DefaultType.get());
Douglas Gregoreb31f392008-12-01 23:54:00 +0000457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
Douglas Gregorf5586182008-12-02 00:41:28 +0000459 return TypeParam;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000460}
461
462/// ParseTemplateTemplateParameter - Handle the parsing of template
Mike Stump11289f42009-09-09 15:08:12 +0000463/// template parameters.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000464///
465/// type-parameter: [C++ temp.param]
466/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
467/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattner83f095c2009-03-28 19:18:32 +0000468Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000469Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000470 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
471
472 // Handle the template <...> part.
473 SourceLocation TemplateLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000474 TemplateParameterList TemplateParams;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000475 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000476 {
477 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Mike Stump11289f42009-09-09 15:08:12 +0000478 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000479 RAngleLoc)) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000480 return DeclPtrTy();
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000481 }
Douglas Gregoreb31f392008-12-01 23:54:00 +0000482 }
483
484 // Generate a meaningful error if the user forgot to put class before the
485 // identifier, comma, or greater.
Mike Stump11289f42009-09-09 15:08:12 +0000486 if (!Tok.is(tok::kw_class)) {
487 Diag(Tok.getLocation(), diag::err_expected_class_before)
Douglas Gregoreb31f392008-12-01 23:54:00 +0000488 << PP.getSpelling(Tok);
Chris Lattner83f095c2009-03-28 19:18:32 +0000489 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000490 }
491 SourceLocation ClassLoc = ConsumeToken();
492
493 // Get the identifier, if given.
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000494 SourceLocation NameLoc;
495 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000496 if (Tok.is(tok::identifier)) {
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000497 ParamName = Tok.getIdentifierInfo();
498 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000499 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000500 // Unnamed template parameter. Don't have to do anything here, just
501 // don't consume this token.
502 } else {
503 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +0000504 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000505 }
506
Mike Stump11289f42009-09-09 15:08:12 +0000507 TemplateParamsTy *ParamList =
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000508 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
509 TemplateLoc, LAngleLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000510 &TemplateParams[0],
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000511 TemplateParams.size(),
512 RAngleLoc);
513
Chris Lattner83f095c2009-03-28 19:18:32 +0000514 Parser::DeclPtrTy Param
Douglas Gregordba32632009-02-10 19:49:53 +0000515 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
516 ParamList, ParamName,
517 NameLoc, Depth, Position);
518
519 // Get the a default value, if given.
520 if (Tok.is(tok::equal)) {
521 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000522 ParsedTemplateArgument Default = ParseTemplateTemplateArgument();
523 if (Default.isInvalid()) {
524 Diag(Tok.getLocation(),
525 diag::err_default_template_template_parameter_not_template);
Nuno Lopes221c1fd2009-12-10 00:07:02 +0000526 static const tok::TokenKind EndToks[] = {
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000527 tok::comma, tok::greater, tok::greatergreater
528 };
529 SkipUntil(EndToks, 3, true, true);
Douglas Gregordba32632009-02-10 19:49:53 +0000530 return Param;
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000531 } else if (Param)
532 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc, Default);
Douglas Gregordba32632009-02-10 19:49:53 +0000533 }
534
535 return Param;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000536}
537
538/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
Mike Stump11289f42009-09-09 15:08:12 +0000539/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000540///
Douglas Gregoreb31f392008-12-01 23:54:00 +0000541/// template-parameter:
542/// ...
543/// parameter-declaration
544///
545/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
546/// but that didn't work out to well. Instead, this tries to recrate the basic
547/// parsing of parameter declarations, but tries to constrain it for template
548/// parameters.
Douglas Gregorf5586182008-12-02 00:41:28 +0000549/// FIXME: We need to make a ParseParameterDeclaration that works for
550/// non-type template parameters and normal function parameters.
Mike Stump11289f42009-09-09 15:08:12 +0000551Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000552Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000553 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000554
555 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregorf5586182008-12-02 00:41:28 +0000556 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregoreb31f392008-12-01 23:54:00 +0000557 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregorf5586182008-12-02 00:41:28 +0000558 DeclSpec DS;
559 ParseDeclarationSpecifiers(DS);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000560
561 // Parse this as a typename.
Douglas Gregorf5586182008-12-02 00:41:28 +0000562 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
563 ParseDeclarator(ParamDecl);
Chris Lattnerb5134c02009-01-05 01:24:05 +0000564 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000565 // This probably shouldn't happen - and it's more of a Sema thing, but
566 // basically we didn't parse the type name because we couldn't associate
567 // it with an AST node. we should just skip to the comma or greater.
568 // TODO: This is currently a placeholder for some kind of Sema Error.
569 Diag(Tok.getLocation(), diag::err_parse_error);
570 SkipUntil(tok::comma, tok::greater, true, true);
Chris Lattner83f095c2009-03-28 19:18:32 +0000571 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000572 }
573
Mike Stump11289f42009-09-09 15:08:12 +0000574 // Create the parameter.
Chris Lattner83f095c2009-03-28 19:18:32 +0000575 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
576 Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000577
Douglas Gregordba32632009-02-10 19:49:53 +0000578 // If there is a default value, parse it.
Chris Lattnerb5134c02009-01-05 01:24:05 +0000579 if (Tok.is(tok::equal)) {
Douglas Gregordba32632009-02-10 19:49:53 +0000580 SourceLocation EqualLoc = ConsumeToken();
581
582 // C++ [temp.param]p15:
583 // When parsing a default template-argument for a non-type
584 // template-parameter, the first non-nested > is taken as the
585 // end of the template-parameter-list rather than a greater-than
586 // operator.
Mike Stump11289f42009-09-09 15:08:12 +0000587 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Douglas Gregordba32632009-02-10 19:49:53 +0000588
589 OwningExprResult DefaultArg = ParseAssignmentExpression();
590 if (DefaultArg.isInvalid())
591 SkipUntil(tok::comma, tok::greater, true, true);
592 else if (Param)
Mike Stump11289f42009-09-09 15:08:12 +0000593 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
Douglas Gregordba32632009-02-10 19:49:53 +0000594 move(DefaultArg));
Douglas Gregoreb31f392008-12-01 23:54:00 +0000595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Douglas Gregorf5586182008-12-02 00:41:28 +0000597 return Param;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000598}
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000599
Douglas Gregor67a65642009-02-17 23:15:12 +0000600/// \brief Parses a template-id that after the template name has
601/// already been parsed.
602///
603/// This routine takes care of parsing the enclosed template argument
604/// list ('<' template-parameter-list [opt] '>') and placing the
605/// results into a form that can be transferred to semantic analysis.
606///
607/// \param Template the template declaration produced by isTemplateName
608///
609/// \param TemplateNameLoc the source location of the template name
610///
611/// \param SS if non-NULL, the nested-name-specifier preceding the
612/// template name.
613///
614/// \param ConsumeLastToken if true, then we will consume the last
615/// token that forms the template-id. Otherwise, we will leave the
616/// last token in the stream (e.g., so that it can be replaced with an
617/// annotation token).
Mike Stump11289f42009-09-09 15:08:12 +0000618bool
Douglas Gregordc572a32009-03-30 22:58:21 +0000619Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Mike Stump11289f42009-09-09 15:08:12 +0000620 SourceLocation TemplateNameLoc,
Douglas Gregor67a65642009-02-17 23:15:12 +0000621 const CXXScopeSpec *SS,
622 bool ConsumeLastToken,
623 SourceLocation &LAngleLoc,
624 TemplateArgList &TemplateArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000625 SourceLocation &RAngleLoc) {
626 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
627
628 // Consume the '<'.
629 LAngleLoc = ConsumeToken();
630
631 // Parse the optional template-argument-list.
632 bool Invalid = false;
633 {
634 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
635 if (Tok.isNot(tok::greater))
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000636 Invalid = ParseTemplateArgumentList(TemplateArgs);
Douglas Gregor67a65642009-02-17 23:15:12 +0000637
638 if (Invalid) {
639 // Try to find the closing '>'.
640 SkipUntil(tok::greater, true, !ConsumeLastToken);
641
642 return true;
643 }
644 }
645
Eli Friedmanaffd5fd2009-12-27 22:31:18 +0000646 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) {
647 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregor67a65642009-02-17 23:15:12 +0000648 return true;
Eli Friedmanaffd5fd2009-12-27 22:31:18 +0000649 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000650
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000651 // Determine the location of the '>' or '>>'. Only consume this
652 // token if the caller asked us to.
Douglas Gregor67a65642009-02-17 23:15:12 +0000653 RAngleLoc = Tok.getLocation();
654
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000655 if (Tok.is(tok::greatergreater)) {
Douglas Gregor87f95b02009-02-26 21:00:50 +0000656 if (!getLang().CPlusPlus0x) {
657 const char *ReplaceStr = "> >";
658 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
659 ReplaceStr = "> > ";
660
661 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregor96977da2009-02-27 17:53:17 +0000662 << CodeModificationHint::CreateReplacement(
663 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000664 }
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000665
666 Tok.setKind(tok::greater);
667 if (!ConsumeLastToken) {
668 // Since we're not supposed to consume the '>>' token, we need
669 // to insert a second '>' token after the first.
670 PP.EnterToken(Tok);
671 }
672 } else if (ConsumeLastToken)
Douglas Gregor67a65642009-02-17 23:15:12 +0000673 ConsumeToken();
674
675 return false;
676}
Mike Stump11289f42009-09-09 15:08:12 +0000677
Douglas Gregor7f741122009-02-25 19:37:18 +0000678/// \brief Replace the tokens that form a simple-template-id with an
679/// annotation token containing the complete template-id.
680///
681/// The first token in the stream must be the name of a template that
682/// is followed by a '<'. This routine will parse the complete
683/// simple-template-id and replace the tokens with a single annotation
684/// token with one of two different kinds: if the template-id names a
685/// type (and \p AllowTypeAnnotation is true), the annotation token is
686/// a type annotation that includes the optional nested-name-specifier
687/// (\p SS). Otherwise, the annotation token is a template-id
688/// annotation that does not include the optional
689/// nested-name-specifier.
690///
691/// \param Template the declaration of the template named by the first
692/// token (an identifier), as returned from \c Action::isTemplateName().
693///
694/// \param TemplateNameKind the kind of template that \p Template
695/// refers to, as returned from \c Action::isTemplateName().
696///
697/// \param SS if non-NULL, the nested-name-specifier that precedes
698/// this template name.
699///
700/// \param TemplateKWLoc if valid, specifies that this template-id
701/// annotation was preceded by the 'template' keyword and gives the
702/// location of that keyword. If invalid (the default), then this
703/// template-id was not preceded by a 'template' keyword.
704///
705/// \param AllowTypeAnnotation if true (the default), then a
706/// simple-template-id that refers to a class template, template
707/// template parameter, or other template that produces a type will be
708/// replaced with a type annotation token. Otherwise, the
709/// simple-template-id is always replaced with a template-id
710/// annotation token.
Chris Lattner5558e9f2009-06-26 04:27:47 +0000711///
712/// If an unrecoverable parse error occurs and no annotation token can be
713/// formed, this function returns true.
714///
715bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Mike Stump11289f42009-09-09 15:08:12 +0000716 const CXXScopeSpec *SS,
Douglas Gregor71395fa2009-11-04 00:56:37 +0000717 UnqualifiedId &TemplateName,
Douglas Gregor7f741122009-02-25 19:37:18 +0000718 SourceLocation TemplateKWLoc,
719 bool AllowTypeAnnotation) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000720 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
Douglas Gregor71395fa2009-11-04 00:56:37 +0000721 assert(Template && Tok.is(tok::less) &&
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000722 "Parser isn't at the beginning of a template-id");
723
724 // Consume the template-name.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000725 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000726
Douglas Gregor67a65642009-02-17 23:15:12 +0000727 // Parse the enclosed template argument list.
728 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor67b556a2009-02-09 19:34:22 +0000729 TemplateArgList TemplateArgs;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000730 bool Invalid = ParseTemplateIdAfterTemplateName(Template,
731 TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000732 SS, false, LAngleLoc,
733 TemplateArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000734 RAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000735
Chris Lattner5558e9f2009-06-26 04:27:47 +0000736 if (Invalid) {
737 // If we failed to parse the template ID but skipped ahead to a >, we're not
738 // going to be able to form a token annotation. Eat the '>' if present.
739 if (Tok.is(tok::greater))
740 ConsumeToken();
741 return true;
742 }
Douglas Gregord32e0282009-02-09 23:23:08 +0000743
Jay Foad7d0479f2009-05-21 09:52:38 +0000744 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor67a65642009-02-17 23:15:12 +0000745 TemplateArgs.size());
Douglas Gregor0db4ccd2009-02-09 21:04:56 +0000746
Douglas Gregor8bf42052009-02-09 18:46:07 +0000747 // Build the annotation token.
Douglas Gregorb67535d2009-03-31 00:43:58 +0000748 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Mike Stump11289f42009-09-09 15:08:12 +0000749 Action::TypeResult Type
Douglas Gregordc572a32009-03-30 22:58:21 +0000750 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
751 LAngleLoc, TemplateArgsPtr,
Douglas Gregordc572a32009-03-30 22:58:21 +0000752 RAngleLoc);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000753 if (Type.isInvalid()) {
754 // If we failed to parse the template ID but skipped ahead to a >, we're not
755 // going to be able to form a token annotation. Eat the '>' if present.
756 if (Tok.is(tok::greater))
757 ConsumeToken();
758 return true;
759 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000760
761 Tok.setKind(tok::annot_typename);
762 Tok.setAnnotationValue(Type.get());
Douglas Gregor7f741122009-02-25 19:37:18 +0000763 if (SS && SS->isNotEmpty())
764 Tok.setLocation(SS->getBeginLoc());
765 else if (TemplateKWLoc.isValid())
766 Tok.setLocation(TemplateKWLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000767 else
Douglas Gregor7f741122009-02-25 19:37:18 +0000768 Tok.setLocation(TemplateNameLoc);
Douglas Gregor67a65642009-02-17 23:15:12 +0000769 } else {
Douglas Gregorb67535d2009-03-31 00:43:58 +0000770 // Build a template-id annotation token that can be processed
771 // later.
Douglas Gregor7f741122009-02-25 19:37:18 +0000772 Tok.setKind(tok::annot_template_id);
Mike Stump11289f42009-09-09 15:08:12 +0000773 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000774 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor8bf42052009-02-09 18:46:07 +0000775 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000776 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
777 TemplateId->Name = TemplateName.Identifier;
778 TemplateId->Operator = OO_None;
779 } else {
780 TemplateId->Name = 0;
781 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
782 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000783 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor7f741122009-02-25 19:37:18 +0000784 TemplateId->Kind = TNK;
Douglas Gregor8bf42052009-02-09 18:46:07 +0000785 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000786 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000787 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
788 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
Douglas Gregor8bf42052009-02-09 18:46:07 +0000789 Args[Arg] = TemplateArgs[Arg];
790 Tok.setAnnotationValue(TemplateId);
Douglas Gregor7f741122009-02-25 19:37:18 +0000791 if (TemplateKWLoc.isValid())
792 Tok.setLocation(TemplateKWLoc);
793 else
794 Tok.setLocation(TemplateNameLoc);
795
796 TemplateArgsPtr.release();
Douglas Gregor8bf42052009-02-09 18:46:07 +0000797 }
798
799 // Common fields for the annotation token
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000800 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000801
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000802 // In case the tokens were cached, have Preprocessor replace them with the
803 // annotation token.
804 PP.AnnotateCachedTokens(Tok);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000805 return false;
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000806}
807
Douglas Gregor7f741122009-02-25 19:37:18 +0000808/// \brief Replaces a template-id annotation token with a type
809/// annotation token.
810///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000811/// If there was a failure when forming the type from the template-id,
812/// a type annotation token will still be created, but will have a
813/// NULL type pointer to signify an error.
814void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000815 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
816
Mike Stump11289f42009-09-09 15:08:12 +0000817 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000818 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000819 assert((TemplateId->Kind == TNK_Type_template ||
820 TemplateId->Kind == TNK_Dependent_template_name) &&
821 "Only works for type and dependent templates");
Mike Stump11289f42009-09-09 15:08:12 +0000822
823 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000824 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000825 TemplateId->NumArgs);
826
Mike Stump11289f42009-09-09 15:08:12 +0000827 Action::TypeResult Type
Douglas Gregordc572a32009-03-30 22:58:21 +0000828 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
829 TemplateId->TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000830 TemplateId->LAngleLoc,
Douglas Gregordc572a32009-03-30 22:58:21 +0000831 TemplateArgsPtr,
Douglas Gregordc572a32009-03-30 22:58:21 +0000832 TemplateId->RAngleLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +0000833 // Create the new "type" annotation token.
834 Tok.setKind(tok::annot_typename);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000835 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor7f741122009-02-25 19:37:18 +0000836 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
837 Tok.setLocation(SS->getBeginLoc());
Douglas Gregor35522592009-11-04 18:18:19 +0000838 Tok.setAnnotationEndLoc(TemplateId->TemplateNameLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +0000839
Douglas Gregor35522592009-11-04 18:18:19 +0000840 // Replace the template-id annotation token, and possible the scope-specifier
841 // that precedes it, with the typename annotation token.
842 PP.AnnotateCachedTokens(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +0000843 TemplateId->Destroy();
Douglas Gregor7f741122009-02-25 19:37:18 +0000844}
845
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000846/// \brief Determine whether the given token can end a template argument.
Benjamin Kramer2c9a91c2009-11-10 21:29:56 +0000847static bool isEndOfTemplateArgument(Token Tok) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000848 return Tok.is(tok::comma) || Tok.is(tok::greater) ||
849 Tok.is(tok::greatergreater);
850}
851
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000852/// \brief Parse a C++ template template argument.
853ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
854 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
855 !Tok.is(tok::annot_cxxscope))
856 return ParsedTemplateArgument();
857
858 // C++0x [temp.arg.template]p1:
859 // A template-argument for a template template-parameter shall be the name
860 // of a class template or a template alias, expressed as id-expression.
861 //
Douglas Gregorc9984092009-11-12 00:03:40 +0000862 // We parse an id-expression that refers to a class template or template
863 // alias. The grammar we parse is:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000864 //
865 // nested-name-specifier[opt] template[opt] identifier
866 //
867 // followed by a token that terminates a template argument, such as ',',
868 // '>', or (in some cases) '>>'.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000869 CXXScopeSpec SS; // nested-name-specifier, if present
870 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
871 /*EnteringContext=*/false);
872
873 if (SS.isSet() && Tok.is(tok::kw_template)) {
874 // Parse the optional 'template' keyword following the
875 // nested-name-specifier.
876 SourceLocation TemplateLoc = ConsumeToken();
877
878 if (Tok.is(tok::identifier)) {
879 // We appear to have a dependent template name.
880 UnqualifiedId Name;
881 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
882 ConsumeToken(); // the identifier
883
884 // If the next token signals the end of a template argument,
885 // then we have a dependent template name that could be a template
886 // template argument.
887 if (isEndOfTemplateArgument(Tok)) {
888 TemplateTy Template
889 = Actions.ActOnDependentTemplateName(TemplateLoc, SS, Name,
Douglas Gregorade9bcd2009-11-20 23:39:24 +0000890 /*ObjectType=*/0,
891 /*EnteringContext=*/false);
Douglas Gregorc9984092009-11-12 00:03:40 +0000892 if (Template.get())
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000893 return ParsedTemplateArgument(SS, Template, Name.StartLocation);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000894 }
895 }
896 } else if (Tok.is(tok::identifier)) {
897 // We may have a (non-dependent) template name.
898 TemplateTy Template;
899 UnqualifiedId Name;
900 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
901 ConsumeToken(); // the identifier
902
903 if (isEndOfTemplateArgument(Tok)) {
904 TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS, Name,
905 /*ObjectType=*/0,
906 /*EnteringContext=*/false,
907 Template);
908 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
909 // We have an id-expression that refers to a class template or
910 // (C++0x) template alias.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000911 return ParsedTemplateArgument(SS, Template, Name.StartLocation);
912 }
913 }
914 }
915
Douglas Gregorc9984092009-11-12 00:03:40 +0000916 // We don't have a template template argument.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000917 return ParsedTemplateArgument();
918}
919
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000920/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
921///
922/// template-argument: [C++ 14.2]
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000923/// constant-expression
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000924/// type-id
925/// id-expression
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000926ParsedTemplateArgument Parser::ParseTemplateArgument() {
Douglas Gregor8bf42052009-02-09 18:46:07 +0000927 // C++ [temp.arg]p2:
928 // In a template-argument, an ambiguity between a type-id and an
929 // expression is resolved to a type-id, regardless of the form of
930 // the corresponding template-parameter.
931 //
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000932 // Therefore, we initially try to parse a type-id.
Douglas Gregor97f34572009-02-10 00:53:15 +0000933 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000934 SourceLocation Loc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +0000935 TypeResult TypeArg = ParseTypeName();
936 if (TypeArg.isInvalid())
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000937 return ParsedTemplateArgument();
938
939 return ParsedTemplateArgument(ParsedTemplateArgument::Type, TypeArg.get(),
940 Loc);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000941 }
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000942
943 // Try to parse a template template argument.
Douglas Gregorc9984092009-11-12 00:03:40 +0000944 {
945 TentativeParsingAction TPA(*this);
946
947 ParsedTemplateArgument TemplateTemplateArgument
948 = ParseTemplateTemplateArgument();
949 if (!TemplateTemplateArgument.isInvalid()) {
950 TPA.Commit();
951 return TemplateTemplateArgument;
952 }
953
954 // Revert this tentative parse to parse a non-type template argument.
955 TPA.Revert();
956 }
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000957
958 // Parse a non-type template argument.
959 SourceLocation Loc = Tok.getLocation();
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000960 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregord32e0282009-02-09 23:23:08 +0000961 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000962 return ParsedTemplateArgument();
Douglas Gregor8bf42052009-02-09 18:46:07 +0000963
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000964 return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
965 ExprArg.release(), Loc);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000966}
967
968/// ParseTemplateArgumentList - Parse a C++ template-argument-list
969/// (C++ [temp.names]). Returns true if there was an error.
970///
971/// template-argument-list: [C++ 14.2]
972/// template-argument
973/// template-argument-list ',' template-argument
Mike Stump11289f42009-09-09 15:08:12 +0000974bool
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000975Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000976 while (true) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000977 ParsedTemplateArgument Arg = ParseTemplateArgument();
978 if (Arg.isInvalid()) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000979 SkipUntil(tok::comma, tok::greater, true, true);
980 return true;
981 }
Douglas Gregor67b556a2009-02-09 19:34:22 +0000982
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000983 // Save this template argument.
984 TemplateArgs.push_back(Arg);
985
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000986 // If the next token is a comma, consume it and keep reading
987 // arguments.
988 if (Tok.isNot(tok::comma)) break;
989
990 // Consume the comma.
991 ConsumeToken();
992 }
993
Eli Friedmanaffd5fd2009-12-27 22:31:18 +0000994 return false;
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000995}
996
Mike Stump11289f42009-09-09 15:08:12 +0000997/// \brief Parse a C++ explicit template instantiation
Douglas Gregor23996282009-05-12 21:31:51 +0000998/// (C++ [temp.explicit]).
999///
1000/// explicit-instantiation:
Douglas Gregor43e75172009-09-04 06:33:52 +00001001/// 'extern' [opt] 'template' declaration
1002///
1003/// Note that the 'extern' is a GNU extension and C++0x feature.
Mike Stump11289f42009-09-09 15:08:12 +00001004Parser::DeclPtrTy
Douglas Gregor43e75172009-09-04 06:33:52 +00001005Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
1006 SourceLocation TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001007 SourceLocation &DeclEnd) {
Mike Stump11289f42009-09-09 15:08:12 +00001008 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
Douglas Gregor43e75172009-09-04 06:33:52 +00001009 ParsedTemplateInfo(ExternLoc,
1010 TemplateLoc),
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001011 DeclEnd, AS_none);
Douglas Gregor23996282009-05-12 21:31:51 +00001012}