blob: b827a48f6a458d861ae805492176618c894e22ca [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"
Douglas Gregoreb31f392008-12-01 23:54:00 +000019using namespace clang;
20
Douglas Gregor1b57ff32009-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 Stump11289f42009-09-09 15:08:12 +000028 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
Douglas Gregor43e75172009-09-04 06:33:52 +000029 DeclEnd);
Douglas Gregor1b57ff32009-05-12 23:25:50 +000030
31 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
32}
33
Douglas Gregora3dff8e2009-08-24 23:03:25 +000034/// \brief RAII class that manages the template parameter depth.
35namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +000036 class TemplateParameterDepthCounter {
Douglas Gregora3dff8e2009-08-24 23:03:25 +000037 unsigned &Depth;
38 unsigned AddedLevels;
39
40 public:
Mike Stump11289f42009-09-09 15:08:12 +000041 explicit TemplateParameterDepthCounter(unsigned &Depth)
Douglas Gregora3dff8e2009-08-24 23:03:25 +000042 : Depth(Depth), AddedLevels(0) { }
Mike Stump11289f42009-09-09 15:08:12 +000043
Douglas Gregora3dff8e2009-08-24 23:03:25 +000044 ~TemplateParameterDepthCounter() {
45 Depth -= AddedLevels;
46 }
Mike Stump11289f42009-09-09 15:08:12 +000047
48 void operator++() {
Douglas Gregora3dff8e2009-08-24 23:03:25 +000049 ++Depth;
50 ++AddedLevels;
51 }
Mike Stump11289f42009-09-09 15:08:12 +000052
Douglas Gregora3dff8e2009-08-24 23:03:25 +000053 operator unsigned() const { return Depth; }
54 };
55}
56
Douglas Gregor67a65642009-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 Gregoreb31f392008-12-01 23:54:00 +000066///
67/// template-declaration: [C++ temp]
68/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregor67a65642009-02-17 23:15:12 +000069///
70/// explicit-specialization: [ C++ temp.expl.spec]
71/// 'template' '<' '>' declaration
Chris Lattner83f095c2009-03-28 19:18:32 +000072Parser::DeclPtrTy
Anders Carlssondfbbdf62009-03-26 00:52:18 +000073Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +000074 SourceLocation &DeclEnd,
Anders Carlssondfbbdf62009-03-26 00:52:18 +000075 AccessSpecifier AS) {
Mike Stump11289f42009-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 Gregorf5586182008-12-02 00:41:28 +000079 // Enter template-parameter scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +000080 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregorf5586182008-12-02 00:41:28 +000081
Douglas Gregorb9bd8a92008-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 Gregor67a65642009-02-17 23:15:12 +000091 // lists to easily differentiate between the case above and:
Douglas Gregorb9bd8a92008-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 Gregor468535e2009-08-20 18:46:05 +0000103 bool isSpecialization = true;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000104 bool LastParamListWasEmpty = false;
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000105 TemplateParameterLists ParamLists;
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000106 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000107 do {
108 // Consume the 'export', if any.
109 SourceLocation ExportLoc;
110 if (Tok.is(tok::kw_export)) {
111 ExportLoc = ConsumeToken();
112 }
113
114 // Consume the 'template', which should be here.
115 SourceLocation TemplateLoc;
116 if (Tok.is(tok::kw_template)) {
117 TemplateLoc = ConsumeToken();
118 } else {
119 Diag(Tok.getLocation(), diag::err_expected_template);
Chris Lattner83f095c2009-03-28 19:18:32 +0000120 return DeclPtrTy();
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000121 }
Mike Stump11289f42009-09-09 15:08:12 +0000122
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000123 // Parse the '<' template-parameter-list '>'
124 SourceLocation LAngleLoc, RAngleLoc;
125 TemplateParameterList TemplateParams;
Mike Stump11289f42009-09-09 15:08:12 +0000126 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000127 RAngleLoc)) {
128 // Skip until the semi-colon or a }.
129 SkipUntil(tok::r_brace, true, true);
130 if (Tok.is(tok::semi))
131 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000132 return DeclPtrTy();
Douglas Gregore93e46c2009-07-22 23:48:44 +0000133 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000134
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000135 ParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000136 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
137 TemplateLoc, LAngleLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000138 TemplateParams.data(),
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000139 TemplateParams.size(), RAngleLoc));
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000140
141 if (!TemplateParams.empty()) {
142 isSpecialization = false;
143 ++Depth;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000144 } else {
145 LastParamListWasEmpty = true;
Mike Stump11289f42009-09-09 15:08:12 +0000146 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000147 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
148
149 // Parse the actual template declaration.
Mike Stump11289f42009-09-09 15:08:12 +0000150 return ParseSingleDeclarationAfterTemplate(Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000151 ParsedTemplateInfo(&ParamLists,
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000152 isSpecialization,
153 LastParamListWasEmpty),
Douglas Gregor23996282009-05-12 21:31:51 +0000154 DeclEnd, AS);
155}
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000156
Douglas Gregor23996282009-05-12 21:31:51 +0000157/// \brief Parse a single declaration that declares a template,
158/// template specialization, or explicit instantiation of a template.
159///
160/// \param TemplateParams if non-NULL, the template parameter lists
161/// that preceded this declaration. In this case, the declaration is a
162/// template declaration, out-of-line definition of a template, or an
163/// explicit template specialization. When NULL, the declaration is an
164/// explicit template instantiation.
165///
166/// \param TemplateLoc when TemplateParams is NULL, the location of
167/// the 'template' keyword that indicates that we have an explicit
168/// template instantiation.
169///
170/// \param DeclEnd will receive the source location of the last token
171/// within this declaration.
172///
173/// \param AS the access specifier associated with this
174/// declaration. Will be AS_none for namespace-scope declarations.
175///
176/// \returns the new declaration.
Mike Stump11289f42009-09-09 15:08:12 +0000177Parser::DeclPtrTy
Douglas Gregor23996282009-05-12 21:31:51 +0000178Parser::ParseSingleDeclarationAfterTemplate(
179 unsigned Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000180 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor23996282009-05-12 21:31:51 +0000181 SourceLocation &DeclEnd,
182 AccessSpecifier AS) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000183 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
184 "Template information required");
185
Douglas Gregor3447e762009-08-20 22:52:58 +0000186 if (Context == Declarator::MemberContext) {
187 // We are parsing a member template.
188 ParseCXXClassMemberDeclaration(AS, TemplateInfo);
189 return DeclPtrTy::make((void*)0);
190 }
Mike Stump11289f42009-09-09 15:08:12 +0000191
Douglas Gregor23996282009-05-12 21:31:51 +0000192 // Parse the declaration specifiers.
John McCall28a6aea2009-11-04 02:18:39 +0000193 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000194
195 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
196 DS.AddAttributes(ParseCXX0XAttributes().AttrList);
197
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000198 ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
Douglas Gregor23996282009-05-12 21:31:51 +0000199
200 if (Tok.is(tok::semi)) {
201 DeclEnd = ConsumeToken();
John McCall28a6aea2009-11-04 02:18:39 +0000202 DeclPtrTy Decl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
203 DS.complete(Decl);
204 return Decl;
Douglas Gregor23996282009-05-12 21:31:51 +0000205 }
206
207 // Parse the declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000208 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
Douglas Gregor23996282009-05-12 21:31:51 +0000209 ParseDeclarator(DeclaratorInfo);
210 // Error parsing the declarator?
211 if (!DeclaratorInfo.hasName()) {
212 // If so, skip until the semi-colon or a }.
213 SkipUntil(tok::r_brace, true, true);
214 if (Tok.is(tok::semi))
215 ConsumeToken();
216 return DeclPtrTy();
217 }
Mike Stump11289f42009-09-09 15:08:12 +0000218
Douglas Gregor23996282009-05-12 21:31:51 +0000219 // If we have a declaration or declarator list, handle it.
220 if (isDeclarationAfterDeclarator()) {
221 // Parse this declaration.
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000222 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
223 TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000224
225 if (Tok.is(tok::comma)) {
226 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000227 << (int)TemplateInfo.Kind;
Douglas Gregor23996282009-05-12 21:31:51 +0000228 SkipUntil(tok::semi, true, false);
229 return ThisDecl;
230 }
231
232 // Eat the semi colon after the declaration.
John McCallef50e992009-07-31 02:20:35 +0000233 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
John McCall28a6aea2009-11-04 02:18:39 +0000234 DS.complete(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +0000235 return ThisDecl;
236 }
237
238 if (DeclaratorInfo.isFunctionDeclarator() &&
239 isStartOfFunctionDefinition()) {
240 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
241 Diag(Tok, diag::err_function_declared_typedef);
242
243 if (Tok.is(tok::l_brace)) {
244 // This recovery skips the entire function body. It would be nice
245 // to simply call ParseFunctionDefinition() below, however Sema
246 // assumes the declarator represents a function, not a typedef.
247 ConsumeBrace();
248 SkipUntil(tok::r_brace, true);
249 } else {
250 SkipUntil(tok::semi);
251 }
252 return DeclPtrTy();
253 }
Douglas Gregor17a7c122009-06-24 00:54:41 +0000254 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000255 }
256
257 if (DeclaratorInfo.isFunctionDeclarator())
258 Diag(Tok, diag::err_expected_fn_body);
259 else
260 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
261 SkipUntil(tok::semi);
262 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000263}
264
265/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000266/// angle brackets. Depth is the depth of this template-parameter-list, which
267/// is the number of template headers directly enclosing this template header.
268/// TemplateParams is the current list of template parameters we're building.
269/// The template parameter we parse will be added to this list. LAngleLoc and
Mike Stump11289f42009-09-09 15:08:12 +0000270/// RAngleLoc will receive the positions of the '<' and '>', respectively,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000271/// that enclose this template parameter list.
Douglas Gregore93e46c2009-07-22 23:48:44 +0000272///
273/// \returns true if an error occurred, false otherwise.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000274bool Parser::ParseTemplateParameters(unsigned Depth,
275 TemplateParameterList &TemplateParams,
276 SourceLocation &LAngleLoc,
277 SourceLocation &RAngleLoc) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000278 // Get the template parameter list.
Mike Stump11289f42009-09-09 15:08:12 +0000279 if (!Tok.is(tok::less)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000280 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregore93e46c2009-07-22 23:48:44 +0000281 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000282 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000283 LAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000284
Douglas Gregoreb31f392008-12-01 23:54:00 +0000285 // Try to parse the template parameter list.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000286 if (Tok.is(tok::greater))
287 RAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000288 else if (ParseTemplateParameterList(Depth, TemplateParams)) {
289 if (!Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000290 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregore93e46c2009-07-22 23:48:44 +0000291 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000292 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000293 RAngleLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000294 }
Douglas Gregore93e46c2009-07-22 23:48:44 +0000295 return false;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000296}
297
298/// ParseTemplateParameterList - Parse a template parameter list. If
299/// the parsing fails badly (i.e., closing bracket was left out), this
300/// will try to put the token stream in a reasonable position (closing
Mike Stump11289f42009-09-09 15:08:12 +0000301/// a statement, etc.) and return false.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000302///
303/// template-parameter-list: [C++ temp]
304/// template-parameter
305/// template-parameter-list ',' template-parameter
Mike Stump11289f42009-09-09 15:08:12 +0000306bool
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000307Parser::ParseTemplateParameterList(unsigned Depth,
308 TemplateParameterList &TemplateParams) {
Mike Stump11289f42009-09-09 15:08:12 +0000309 while (1) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000310 if (DeclPtrTy TmpParam
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000311 = ParseTemplateParameter(Depth, TemplateParams.size())) {
312 TemplateParams.push_back(TmpParam);
313 } else {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000314 // If we failed to parse a template parameter, skip until we find
315 // a comma or closing brace.
316 SkipUntil(tok::comma, tok::greater, true, true);
317 }
Mike Stump11289f42009-09-09 15:08:12 +0000318
Douglas Gregoreb31f392008-12-01 23:54:00 +0000319 // Did we find a comma or the end of the template parmeter list?
Mike Stump11289f42009-09-09 15:08:12 +0000320 if (Tok.is(tok::comma)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000321 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000322 } else if (Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000323 // Don't consume this... that's done by template parser.
324 break;
325 } else {
326 // Somebody probably forgot to close the template. Skip ahead and
327 // try to get out of the expression. This error is currently
328 // subsumed by whatever goes on in ParseTemplateParameter.
329 // TODO: This could match >>, and it would be nice to avoid those
330 // silly errors with template <vec<T>>.
331 // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
332 SkipUntil(tok::greater, true, true);
333 return false;
334 }
335 }
336 return true;
337}
338
Douglas Gregor26aedb72009-11-21 02:07:55 +0000339/// \brief Determine whether the parser is at the start of a template
340/// type parameter.
341bool Parser::isStartOfTemplateTypeParameter() {
342 if (Tok.is(tok::kw_class))
343 return true;
344
345 if (Tok.isNot(tok::kw_typename))
346 return false;
347
348 // C++ [temp.param]p2:
349 // There is no semantic difference between class and typename in a
350 // template-parameter. typename followed by an unqualified-id
351 // names a template type parameter. typename followed by a
352 // qualified-id denotes the type in a non-type
353 // parameter-declaration.
354 Token Next = NextToken();
355
356 // If we have an identifier, skip over it.
357 if (Next.getKind() == tok::identifier)
358 Next = GetLookAheadToken(2);
359
360 switch (Next.getKind()) {
361 case tok::equal:
362 case tok::comma:
363 case tok::greater:
364 case tok::greatergreater:
365 case tok::ellipsis:
366 return true;
367
368 default:
369 return false;
370 }
371}
372
Douglas Gregoreb31f392008-12-01 23:54:00 +0000373/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
374///
375/// template-parameter: [C++ temp.param]
376/// type-parameter
377/// parameter-declaration
378///
379/// type-parameter: (see below)
Anders Carlssonf986ba72009-06-12 23:09:56 +0000380/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000381/// 'class' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000382/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000383/// 'typename' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000384/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000385/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Mike Stump11289f42009-09-09 15:08:12 +0000386Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000387Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor26aedb72009-11-21 02:07:55 +0000388 if (isStartOfTemplateTypeParameter())
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000389 return ParseTypeParameter(Depth, Position);
Mike Stump11289f42009-09-09 15:08:12 +0000390
391 if (Tok.is(tok::kw_template))
Chris Lattnera21db612009-01-04 23:51:17 +0000392 return ParseTemplateTemplateParameter(Depth, Position);
393
394 // If it's none of the above, then it must be a parameter declaration.
395 // NOTE: This will pick up errors in the closure of the template parameter
396 // list (e.g., template < ; Check here to implement >> style closures.
397 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000398}
399
400/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
401/// Other kinds of template parameters are parsed in
402/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
403///
404/// type-parameter: [C++ temp.param]
Anders Carlssonf986ba72009-06-12 23:09:56 +0000405/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000406/// 'class' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000407/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000408/// 'typename' identifier[opt] '=' type-id
Chris Lattner83f095c2009-03-28 19:18:32 +0000409Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregorf5586182008-12-02 00:41:28 +0000410 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
Mike Stump11289f42009-09-09 15:08:12 +0000411 "A type-parameter starts with 'class' or 'typename'");
Douglas Gregorf5586182008-12-02 00:41:28 +0000412
413 // Consume the 'class' or 'typename' keyword.
414 bool TypenameKeyword = Tok.is(tok::kw_typename);
415 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000416
Anders Carlsson01e9e932009-06-12 19:58:00 +0000417 // Grab the ellipsis (if given).
418 bool Ellipsis = false;
419 SourceLocation EllipsisLoc;
Anders Carlssonf986ba72009-06-12 23:09:56 +0000420 if (Tok.is(tok::ellipsis)) {
Anders Carlsson01e9e932009-06-12 19:58:00 +0000421 Ellipsis = true;
422 EllipsisLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000423
424 if (!getLang().CPlusPlus0x)
Anders Carlssonf986ba72009-06-12 23:09:56 +0000425 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson01e9e932009-06-12 19:58:00 +0000426 }
Mike Stump11289f42009-09-09 15:08:12 +0000427
Douglas Gregoreb31f392008-12-01 23:54:00 +0000428 // Grab the template parameter name (if given)
Douglas Gregorf5586182008-12-02 00:41:28 +0000429 SourceLocation NameLoc;
430 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000431 if (Tok.is(tok::identifier)) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000432 ParamName = Tok.getIdentifierInfo();
433 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000434 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
435 Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000436 // Unnamed template parameter. Don't have to do anything here, just
437 // don't consume this token.
438 } else {
439 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +0000440 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000441 }
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chris Lattner83f095c2009-03-28 19:18:32 +0000443 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson01e9e932009-06-12 19:58:00 +0000444 Ellipsis, EllipsisLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000445 KeyLoc, ParamName, NameLoc,
446 Depth, Position);
Douglas Gregorf5586182008-12-02 00:41:28 +0000447
Douglas Gregoreb31f392008-12-01 23:54:00 +0000448 // Grab a default type id (if given).
Mike Stump11289f42009-09-09 15:08:12 +0000449 if (Tok.is(tok::equal)) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000450 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregordba32632009-02-10 19:49:53 +0000451 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +0000452 TypeResult DefaultType = ParseTypeName();
453 if (!DefaultType.isInvalid())
Douglas Gregordba32632009-02-10 19:49:53 +0000454 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +0000455 DefaultType.get());
Douglas Gregoreb31f392008-12-01 23:54:00 +0000456 }
Mike Stump11289f42009-09-09 15:08:12 +0000457
Douglas Gregorf5586182008-12-02 00:41:28 +0000458 return TypeParam;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000459}
460
461/// ParseTemplateTemplateParameter - Handle the parsing of template
Mike Stump11289f42009-09-09 15:08:12 +0000462/// template parameters.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000463///
464/// type-parameter: [C++ temp.param]
465/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
466/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattner83f095c2009-03-28 19:18:32 +0000467Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000468Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000469 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
470
471 // Handle the template <...> part.
472 SourceLocation TemplateLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000473 TemplateParameterList TemplateParams;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000474 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000475 {
476 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Mike Stump11289f42009-09-09 15:08:12 +0000477 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000478 RAngleLoc)) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000479 return DeclPtrTy();
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000480 }
Douglas Gregoreb31f392008-12-01 23:54:00 +0000481 }
482
483 // Generate a meaningful error if the user forgot to put class before the
484 // identifier, comma, or greater.
Mike Stump11289f42009-09-09 15:08:12 +0000485 if (!Tok.is(tok::kw_class)) {
486 Diag(Tok.getLocation(), diag::err_expected_class_before)
Douglas Gregoreb31f392008-12-01 23:54:00 +0000487 << PP.getSpelling(Tok);
Chris Lattner83f095c2009-03-28 19:18:32 +0000488 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000489 }
490 SourceLocation ClassLoc = ConsumeToken();
491
492 // Get the identifier, if given.
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000493 SourceLocation NameLoc;
494 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000495 if (Tok.is(tok::identifier)) {
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000496 ParamName = Tok.getIdentifierInfo();
497 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000498 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000499 // Unnamed template parameter. Don't have to do anything here, just
500 // don't consume this token.
501 } else {
502 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +0000503 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000504 }
505
Mike Stump11289f42009-09-09 15:08:12 +0000506 TemplateParamsTy *ParamList =
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000507 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
508 TemplateLoc, LAngleLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000509 &TemplateParams[0],
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000510 TemplateParams.size(),
511 RAngleLoc);
512
Chris Lattner83f095c2009-03-28 19:18:32 +0000513 Parser::DeclPtrTy Param
Douglas Gregordba32632009-02-10 19:49:53 +0000514 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
515 ParamList, ParamName,
516 NameLoc, Depth, Position);
517
518 // Get the a default value, if given.
519 if (Tok.is(tok::equal)) {
520 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000521 ParsedTemplateArgument Default = ParseTemplateTemplateArgument();
522 if (Default.isInvalid()) {
523 Diag(Tok.getLocation(),
524 diag::err_default_template_template_parameter_not_template);
Nuno Lopes221c1fd2009-12-10 00:07:02 +0000525 static const tok::TokenKind EndToks[] = {
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000526 tok::comma, tok::greater, tok::greatergreater
527 };
528 SkipUntil(EndToks, 3, true, true);
Douglas Gregordba32632009-02-10 19:49:53 +0000529 return Param;
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000530 } else if (Param)
531 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc, Default);
Douglas Gregordba32632009-02-10 19:49:53 +0000532 }
533
534 return Param;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000535}
536
537/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
Mike Stump11289f42009-09-09 15:08:12 +0000538/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000539///
Douglas Gregoreb31f392008-12-01 23:54:00 +0000540/// template-parameter:
541/// ...
542/// parameter-declaration
543///
544/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
545/// but that didn't work out to well. Instead, this tries to recrate the basic
546/// parsing of parameter declarations, but tries to constrain it for template
547/// parameters.
Douglas Gregorf5586182008-12-02 00:41:28 +0000548/// FIXME: We need to make a ParseParameterDeclaration that works for
549/// non-type template parameters and normal function parameters.
Mike Stump11289f42009-09-09 15:08:12 +0000550Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000551Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000552 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000553
554 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregorf5586182008-12-02 00:41:28 +0000555 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregoreb31f392008-12-01 23:54:00 +0000556 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregorf5586182008-12-02 00:41:28 +0000557 DeclSpec DS;
558 ParseDeclarationSpecifiers(DS);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000559
560 // Parse this as a typename.
Douglas Gregorf5586182008-12-02 00:41:28 +0000561 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
562 ParseDeclarator(ParamDecl);
Chris Lattnerb5134c02009-01-05 01:24:05 +0000563 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000564 // This probably shouldn't happen - and it's more of a Sema thing, but
565 // basically we didn't parse the type name because we couldn't associate
566 // it with an AST node. we should just skip to the comma or greater.
567 // TODO: This is currently a placeholder for some kind of Sema Error.
568 Diag(Tok.getLocation(), diag::err_parse_error);
569 SkipUntil(tok::comma, tok::greater, true, true);
Chris Lattner83f095c2009-03-28 19:18:32 +0000570 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000571 }
572
Mike Stump11289f42009-09-09 15:08:12 +0000573 // Create the parameter.
Chris Lattner83f095c2009-03-28 19:18:32 +0000574 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
575 Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000576
Douglas Gregordba32632009-02-10 19:49:53 +0000577 // If there is a default value, parse it.
Chris Lattnerb5134c02009-01-05 01:24:05 +0000578 if (Tok.is(tok::equal)) {
Douglas Gregordba32632009-02-10 19:49:53 +0000579 SourceLocation EqualLoc = ConsumeToken();
580
581 // C++ [temp.param]p15:
582 // When parsing a default template-argument for a non-type
583 // template-parameter, the first non-nested > is taken as the
584 // end of the template-parameter-list rather than a greater-than
585 // operator.
Mike Stump11289f42009-09-09 15:08:12 +0000586 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Douglas Gregordba32632009-02-10 19:49:53 +0000587
588 OwningExprResult DefaultArg = ParseAssignmentExpression();
589 if (DefaultArg.isInvalid())
590 SkipUntil(tok::comma, tok::greater, true, true);
591 else if (Param)
Mike Stump11289f42009-09-09 15:08:12 +0000592 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
Douglas Gregordba32632009-02-10 19:49:53 +0000593 move(DefaultArg));
Douglas Gregoreb31f392008-12-01 23:54:00 +0000594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Douglas Gregorf5586182008-12-02 00:41:28 +0000596 return Param;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000597}
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000598
Douglas Gregor67a65642009-02-17 23:15:12 +0000599/// \brief Parses a template-id that after the template name has
600/// already been parsed.
601///
602/// This routine takes care of parsing the enclosed template argument
603/// list ('<' template-parameter-list [opt] '>') and placing the
604/// results into a form that can be transferred to semantic analysis.
605///
606/// \param Template the template declaration produced by isTemplateName
607///
608/// \param TemplateNameLoc the source location of the template name
609///
610/// \param SS if non-NULL, the nested-name-specifier preceding the
611/// template name.
612///
613/// \param ConsumeLastToken if true, then we will consume the last
614/// token that forms the template-id. Otherwise, we will leave the
615/// last token in the stream (e.g., so that it can be replaced with an
616/// annotation token).
Mike Stump11289f42009-09-09 15:08:12 +0000617bool
Douglas Gregordc572a32009-03-30 22:58:21 +0000618Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Mike Stump11289f42009-09-09 15:08:12 +0000619 SourceLocation TemplateNameLoc,
Douglas Gregor67a65642009-02-17 23:15:12 +0000620 const CXXScopeSpec *SS,
621 bool ConsumeLastToken,
622 SourceLocation &LAngleLoc,
623 TemplateArgList &TemplateArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000624 SourceLocation &RAngleLoc) {
625 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
626
627 // Consume the '<'.
628 LAngleLoc = ConsumeToken();
629
630 // Parse the optional template-argument-list.
631 bool Invalid = false;
632 {
633 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
634 if (Tok.isNot(tok::greater))
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000635 Invalid = ParseTemplateArgumentList(TemplateArgs);
Douglas Gregor67a65642009-02-17 23:15:12 +0000636
637 if (Invalid) {
638 // Try to find the closing '>'.
639 SkipUntil(tok::greater, true, !ConsumeLastToken);
640
641 return true;
642 }
643 }
644
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000645 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
Douglas Gregor67a65642009-02-17 23:15:12 +0000646 return true;
647
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000648 // Determine the location of the '>' or '>>'. Only consume this
649 // token if the caller asked us to.
Douglas Gregor67a65642009-02-17 23:15:12 +0000650 RAngleLoc = Tok.getLocation();
651
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000652 if (Tok.is(tok::greatergreater)) {
Douglas Gregor87f95b02009-02-26 21:00:50 +0000653 if (!getLang().CPlusPlus0x) {
654 const char *ReplaceStr = "> >";
655 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
656 ReplaceStr = "> > ";
657
658 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregor96977da2009-02-27 17:53:17 +0000659 << CodeModificationHint::CreateReplacement(
660 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000661 }
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000662
663 Tok.setKind(tok::greater);
664 if (!ConsumeLastToken) {
665 // Since we're not supposed to consume the '>>' token, we need
666 // to insert a second '>' token after the first.
667 PP.EnterToken(Tok);
668 }
669 } else if (ConsumeLastToken)
Douglas Gregor67a65642009-02-17 23:15:12 +0000670 ConsumeToken();
671
672 return false;
673}
Mike Stump11289f42009-09-09 15:08:12 +0000674
Douglas Gregor7f741122009-02-25 19:37:18 +0000675/// \brief Replace the tokens that form a simple-template-id with an
676/// annotation token containing the complete template-id.
677///
678/// The first token in the stream must be the name of a template that
679/// is followed by a '<'. This routine will parse the complete
680/// simple-template-id and replace the tokens with a single annotation
681/// token with one of two different kinds: if the template-id names a
682/// type (and \p AllowTypeAnnotation is true), the annotation token is
683/// a type annotation that includes the optional nested-name-specifier
684/// (\p SS). Otherwise, the annotation token is a template-id
685/// annotation that does not include the optional
686/// nested-name-specifier.
687///
688/// \param Template the declaration of the template named by the first
689/// token (an identifier), as returned from \c Action::isTemplateName().
690///
691/// \param TemplateNameKind the kind of template that \p Template
692/// refers to, as returned from \c Action::isTemplateName().
693///
694/// \param SS if non-NULL, the nested-name-specifier that precedes
695/// this template name.
696///
697/// \param TemplateKWLoc if valid, specifies that this template-id
698/// annotation was preceded by the 'template' keyword and gives the
699/// location of that keyword. If invalid (the default), then this
700/// template-id was not preceded by a 'template' keyword.
701///
702/// \param AllowTypeAnnotation if true (the default), then a
703/// simple-template-id that refers to a class template, template
704/// template parameter, or other template that produces a type will be
705/// replaced with a type annotation token. Otherwise, the
706/// simple-template-id is always replaced with a template-id
707/// annotation token.
Chris Lattner5558e9f2009-06-26 04:27:47 +0000708///
709/// If an unrecoverable parse error occurs and no annotation token can be
710/// formed, this function returns true.
711///
712bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Mike Stump11289f42009-09-09 15:08:12 +0000713 const CXXScopeSpec *SS,
Douglas Gregor71395fa2009-11-04 00:56:37 +0000714 UnqualifiedId &TemplateName,
Douglas Gregor7f741122009-02-25 19:37:18 +0000715 SourceLocation TemplateKWLoc,
716 bool AllowTypeAnnotation) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000717 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
Douglas Gregor71395fa2009-11-04 00:56:37 +0000718 assert(Template && Tok.is(tok::less) &&
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000719 "Parser isn't at the beginning of a template-id");
720
721 // Consume the template-name.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000722 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000723
Douglas Gregor67a65642009-02-17 23:15:12 +0000724 // Parse the enclosed template argument list.
725 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor67b556a2009-02-09 19:34:22 +0000726 TemplateArgList TemplateArgs;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000727 bool Invalid = ParseTemplateIdAfterTemplateName(Template,
728 TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000729 SS, false, LAngleLoc,
730 TemplateArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000731 RAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000732
Chris Lattner5558e9f2009-06-26 04:27:47 +0000733 if (Invalid) {
734 // If we failed to parse the template ID but skipped ahead to a >, we're not
735 // going to be able to form a token annotation. Eat the '>' if present.
736 if (Tok.is(tok::greater))
737 ConsumeToken();
738 return true;
739 }
Douglas Gregord32e0282009-02-09 23:23:08 +0000740
Jay Foad7d0479f2009-05-21 09:52:38 +0000741 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor67a65642009-02-17 23:15:12 +0000742 TemplateArgs.size());
Douglas Gregor0db4ccd2009-02-09 21:04:56 +0000743
Douglas Gregor8bf42052009-02-09 18:46:07 +0000744 // Build the annotation token.
Douglas Gregorb67535d2009-03-31 00:43:58 +0000745 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Mike Stump11289f42009-09-09 15:08:12 +0000746 Action::TypeResult Type
Douglas Gregordc572a32009-03-30 22:58:21 +0000747 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
748 LAngleLoc, TemplateArgsPtr,
Douglas Gregordc572a32009-03-30 22:58:21 +0000749 RAngleLoc);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000750 if (Type.isInvalid()) {
751 // If we failed to parse the template ID but skipped ahead to a >, we're not
752 // going to be able to form a token annotation. Eat the '>' if present.
753 if (Tok.is(tok::greater))
754 ConsumeToken();
755 return true;
756 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000757
758 Tok.setKind(tok::annot_typename);
759 Tok.setAnnotationValue(Type.get());
Douglas Gregor7f741122009-02-25 19:37:18 +0000760 if (SS && SS->isNotEmpty())
761 Tok.setLocation(SS->getBeginLoc());
762 else if (TemplateKWLoc.isValid())
763 Tok.setLocation(TemplateKWLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000764 else
Douglas Gregor7f741122009-02-25 19:37:18 +0000765 Tok.setLocation(TemplateNameLoc);
Douglas Gregor67a65642009-02-17 23:15:12 +0000766 } else {
Douglas Gregorb67535d2009-03-31 00:43:58 +0000767 // Build a template-id annotation token that can be processed
768 // later.
Douglas Gregor7f741122009-02-25 19:37:18 +0000769 Tok.setKind(tok::annot_template_id);
Mike Stump11289f42009-09-09 15:08:12 +0000770 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000771 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor8bf42052009-02-09 18:46:07 +0000772 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000773 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
774 TemplateId->Name = TemplateName.Identifier;
775 TemplateId->Operator = OO_None;
776 } else {
777 TemplateId->Name = 0;
778 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
779 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000780 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor7f741122009-02-25 19:37:18 +0000781 TemplateId->Kind = TNK;
Douglas Gregor8bf42052009-02-09 18:46:07 +0000782 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000783 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000784 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
785 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
Douglas Gregor8bf42052009-02-09 18:46:07 +0000786 Args[Arg] = TemplateArgs[Arg];
787 Tok.setAnnotationValue(TemplateId);
Douglas Gregor7f741122009-02-25 19:37:18 +0000788 if (TemplateKWLoc.isValid())
789 Tok.setLocation(TemplateKWLoc);
790 else
791 Tok.setLocation(TemplateNameLoc);
792
793 TemplateArgsPtr.release();
Douglas Gregor8bf42052009-02-09 18:46:07 +0000794 }
795
796 // Common fields for the annotation token
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000797 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000798
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000799 // In case the tokens were cached, have Preprocessor replace them with the
800 // annotation token.
801 PP.AnnotateCachedTokens(Tok);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000802 return false;
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000803}
804
Douglas Gregor7f741122009-02-25 19:37:18 +0000805/// \brief Replaces a template-id annotation token with a type
806/// annotation token.
807///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000808/// If there was a failure when forming the type from the template-id,
809/// a type annotation token will still be created, but will have a
810/// NULL type pointer to signify an error.
811void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000812 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
813
Mike Stump11289f42009-09-09 15:08:12 +0000814 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000815 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000816 assert((TemplateId->Kind == TNK_Type_template ||
817 TemplateId->Kind == TNK_Dependent_template_name) &&
818 "Only works for type and dependent templates");
Mike Stump11289f42009-09-09 15:08:12 +0000819
820 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000821 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000822 TemplateId->NumArgs);
823
Mike Stump11289f42009-09-09 15:08:12 +0000824 Action::TypeResult Type
Douglas Gregordc572a32009-03-30 22:58:21 +0000825 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
826 TemplateId->TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000827 TemplateId->LAngleLoc,
Douglas Gregordc572a32009-03-30 22:58:21 +0000828 TemplateArgsPtr,
Douglas Gregordc572a32009-03-30 22:58:21 +0000829 TemplateId->RAngleLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +0000830 // Create the new "type" annotation token.
831 Tok.setKind(tok::annot_typename);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000832 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor7f741122009-02-25 19:37:18 +0000833 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
834 Tok.setLocation(SS->getBeginLoc());
Douglas Gregor35522592009-11-04 18:18:19 +0000835 Tok.setAnnotationEndLoc(TemplateId->TemplateNameLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +0000836
Douglas Gregor35522592009-11-04 18:18:19 +0000837 // Replace the template-id annotation token, and possible the scope-specifier
838 // that precedes it, with the typename annotation token.
839 PP.AnnotateCachedTokens(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +0000840 TemplateId->Destroy();
Douglas Gregor7f741122009-02-25 19:37:18 +0000841}
842
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000843/// \brief Determine whether the given token can end a template argument.
Benjamin Kramer2c9a91c2009-11-10 21:29:56 +0000844static bool isEndOfTemplateArgument(Token Tok) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000845 return Tok.is(tok::comma) || Tok.is(tok::greater) ||
846 Tok.is(tok::greatergreater);
847}
848
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000849/// \brief Parse a C++ template template argument.
850ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
851 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
852 !Tok.is(tok::annot_cxxscope))
853 return ParsedTemplateArgument();
854
855 // C++0x [temp.arg.template]p1:
856 // A template-argument for a template template-parameter shall be the name
857 // of a class template or a template alias, expressed as id-expression.
858 //
Douglas Gregorc9984092009-11-12 00:03:40 +0000859 // We parse an id-expression that refers to a class template or template
860 // alias. The grammar we parse is:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000861 //
862 // nested-name-specifier[opt] template[opt] identifier
863 //
864 // followed by a token that terminates a template argument, such as ',',
865 // '>', or (in some cases) '>>'.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000866 CXXScopeSpec SS; // nested-name-specifier, if present
867 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
868 /*EnteringContext=*/false);
869
870 if (SS.isSet() && Tok.is(tok::kw_template)) {
871 // Parse the optional 'template' keyword following the
872 // nested-name-specifier.
873 SourceLocation TemplateLoc = ConsumeToken();
874
875 if (Tok.is(tok::identifier)) {
876 // We appear to have a dependent template name.
877 UnqualifiedId Name;
878 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
879 ConsumeToken(); // the identifier
880
881 // If the next token signals the end of a template argument,
882 // then we have a dependent template name that could be a template
883 // template argument.
884 if (isEndOfTemplateArgument(Tok)) {
885 TemplateTy Template
886 = Actions.ActOnDependentTemplateName(TemplateLoc, SS, Name,
Douglas Gregorade9bcd2009-11-20 23:39:24 +0000887 /*ObjectType=*/0,
888 /*EnteringContext=*/false);
Douglas Gregorc9984092009-11-12 00:03:40 +0000889 if (Template.get())
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000890 return ParsedTemplateArgument(SS, Template, Name.StartLocation);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000891 }
892 }
893 } else if (Tok.is(tok::identifier)) {
894 // We may have a (non-dependent) template name.
895 TemplateTy Template;
896 UnqualifiedId Name;
897 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
898 ConsumeToken(); // the identifier
899
900 if (isEndOfTemplateArgument(Tok)) {
901 TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS, Name,
902 /*ObjectType=*/0,
903 /*EnteringContext=*/false,
904 Template);
905 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
906 // We have an id-expression that refers to a class template or
907 // (C++0x) template alias.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000908 return ParsedTemplateArgument(SS, Template, Name.StartLocation);
909 }
910 }
911 }
912
Douglas Gregorc9984092009-11-12 00:03:40 +0000913 // We don't have a template template argument.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000914 return ParsedTemplateArgument();
915}
916
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000917/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
918///
919/// template-argument: [C++ 14.2]
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000920/// constant-expression
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000921/// type-id
922/// id-expression
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000923ParsedTemplateArgument Parser::ParseTemplateArgument() {
Douglas Gregor8bf42052009-02-09 18:46:07 +0000924 // C++ [temp.arg]p2:
925 // In a template-argument, an ambiguity between a type-id and an
926 // expression is resolved to a type-id, regardless of the form of
927 // the corresponding template-parameter.
928 //
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000929 // Therefore, we initially try to parse a type-id.
Douglas Gregor97f34572009-02-10 00:53:15 +0000930 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000931 SourceLocation Loc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +0000932 TypeResult TypeArg = ParseTypeName();
933 if (TypeArg.isInvalid())
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000934 return ParsedTemplateArgument();
935
936 return ParsedTemplateArgument(ParsedTemplateArgument::Type, TypeArg.get(),
937 Loc);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000938 }
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000939
940 // Try to parse a template template argument.
Douglas Gregorc9984092009-11-12 00:03:40 +0000941 {
942 TentativeParsingAction TPA(*this);
943
944 ParsedTemplateArgument TemplateTemplateArgument
945 = ParseTemplateTemplateArgument();
946 if (!TemplateTemplateArgument.isInvalid()) {
947 TPA.Commit();
948 return TemplateTemplateArgument;
949 }
950
951 // Revert this tentative parse to parse a non-type template argument.
952 TPA.Revert();
953 }
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000954
955 // Parse a non-type template argument.
956 SourceLocation Loc = Tok.getLocation();
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000957 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregord32e0282009-02-09 23:23:08 +0000958 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000959 return ParsedTemplateArgument();
Douglas Gregor8bf42052009-02-09 18:46:07 +0000960
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000961 return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
962 ExprArg.release(), Loc);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000963}
964
965/// ParseTemplateArgumentList - Parse a C++ template-argument-list
966/// (C++ [temp.names]). Returns true if there was an error.
967///
968/// template-argument-list: [C++ 14.2]
969/// template-argument
970/// template-argument-list ',' template-argument
Mike Stump11289f42009-09-09 15:08:12 +0000971bool
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000972Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000973 while (true) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000974 ParsedTemplateArgument Arg = ParseTemplateArgument();
975 if (Arg.isInvalid()) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000976 SkipUntil(tok::comma, tok::greater, true, true);
977 return true;
978 }
Douglas Gregor67b556a2009-02-09 19:34:22 +0000979
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000980 // Save this template argument.
981 TemplateArgs.push_back(Arg);
982
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000983 // If the next token is a comma, consume it and keep reading
984 // arguments.
985 if (Tok.isNot(tok::comma)) break;
986
987 // Consume the comma.
988 ConsumeToken();
989 }
990
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000991 return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000992}
993
Mike Stump11289f42009-09-09 15:08:12 +0000994/// \brief Parse a C++ explicit template instantiation
Douglas Gregor23996282009-05-12 21:31:51 +0000995/// (C++ [temp.explicit]).
996///
997/// explicit-instantiation:
Douglas Gregor43e75172009-09-04 06:33:52 +0000998/// 'extern' [opt] 'template' declaration
999///
1000/// Note that the 'extern' is a GNU extension and C++0x feature.
Mike Stump11289f42009-09-09 15:08:12 +00001001Parser::DeclPtrTy
Douglas Gregor43e75172009-09-04 06:33:52 +00001002Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
1003 SourceLocation TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001004 SourceLocation &DeclEnd) {
Mike Stump11289f42009-09-09 15:08:12 +00001005 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
Douglas Gregor43e75172009-09-04 06:33:52 +00001006 ParsedTemplateInfo(ExternLoc,
1007 TemplateLoc),
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001008 DeclEnd, AS_none);
Douglas Gregor23996282009-05-12 21:31:51 +00001009}