blob: 12f26bfcb94e42d767374dda38c20e427dc902f4 [file] [log] [blame]
Douglas Gregoradcac882008-12-01 23:54:00 +00001//===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements parsing of C++ templates.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregoradcac882008-12-01 23:54:00 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000018#include "clang/Parse/Template.h"
Chris Lattnerde138eb2009-12-10 00:45:15 +000019#include "RAIIObjectsForParser.h"
Douglas Gregoradcac882008-12-01 23:54:00 +000020using namespace clang;
21
Douglas Gregor4d9a16f2009-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 Stump1eb44332009-09-09 15:08:12 +000029 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
Douglas Gregor45f96552009-09-04 06:33:52 +000030 DeclEnd);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000031
32 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
33}
34
Douglas Gregorc3058332009-08-24 23:03:25 +000035/// \brief RAII class that manages the template parameter depth.
36namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000037 class TemplateParameterDepthCounter {
Douglas Gregorc3058332009-08-24 23:03:25 +000038 unsigned &Depth;
39 unsigned AddedLevels;
40
41 public:
Mike Stump1eb44332009-09-09 15:08:12 +000042 explicit TemplateParameterDepthCounter(unsigned &Depth)
Douglas Gregorc3058332009-08-24 23:03:25 +000043 : Depth(Depth), AddedLevels(0) { }
Mike Stump1eb44332009-09-09 15:08:12 +000044
Douglas Gregorc3058332009-08-24 23:03:25 +000045 ~TemplateParameterDepthCounter() {
46 Depth -= AddedLevels;
47 }
Mike Stump1eb44332009-09-09 15:08:12 +000048
49 void operator++() {
Douglas Gregorc3058332009-08-24 23:03:25 +000050 ++Depth;
51 ++AddedLevels;
52 }
Mike Stump1eb44332009-09-09 15:08:12 +000053
Douglas Gregorc3058332009-08-24 23:03:25 +000054 operator unsigned() const { return Depth; }
55 };
56}
57
Douglas Gregorcc636682009-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 Gregoradcac882008-12-01 23:54:00 +000067///
68/// template-declaration: [C++ temp]
69/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregorcc636682009-02-17 23:15:12 +000070///
71/// explicit-specialization: [ C++ temp.expl.spec]
72/// 'template' '<' '>' declaration
Chris Lattnerb28317a2009-03-28 19:18:32 +000073Parser::DeclPtrTy
Anders Carlsson5aeccdb2009-03-26 00:52:18 +000074Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +000075 SourceLocation &DeclEnd,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +000076 AccessSpecifier AS) {
Mike Stump1eb44332009-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 Gregor26236e82008-12-02 00:41:28 +000080 // Enter template-parameter scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +000081 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregor26236e82008-12-02 00:41:28 +000082
Douglas Gregorc4b4e7b2008-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 Gregorcc636682009-02-17 23:15:12 +000092 // lists to easily differentiate between the case above and:
Douglas Gregorc4b4e7b2008-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 Gregor0f499d92009-08-20 18:46:05 +0000104 bool isSpecialization = true;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000105 bool LastParamListWasEmpty = false;
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000106 TemplateParameterLists ParamLists;
Douglas Gregorc3058332009-08-24 23:03:25 +0000107 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
Douglas Gregorc4b4e7b2008-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 Lattnerb28317a2009-03-28 19:18:32 +0000121 return DeclPtrTy();
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000124 // Parse the '<' template-parameter-list '>'
125 SourceLocation LAngleLoc, RAngleLoc;
126 TemplateParameterList TemplateParams;
Mike Stump1eb44332009-09-09 15:08:12 +0000127 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
Douglas Gregor7cdbc582009-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 Stump1eb44332009-09-09 15:08:12 +0000133 return DeclPtrTy();
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000134 }
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000135
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000136 ParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000137 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
138 TemplateLoc, LAngleLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000139 TemplateParams.data(),
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000140 TemplateParams.size(), RAngleLoc));
Douglas Gregorc3058332009-08-24 23:03:25 +0000141
142 if (!TemplateParams.empty()) {
143 isSpecialization = false;
144 ++Depth;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000145 } else {
146 LastParamListWasEmpty = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000147 }
Douglas Gregorc4b4e7b2008-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 Stump1eb44332009-09-09 15:08:12 +0000151 return ParseSingleDeclarationAfterTemplate(Context,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000152 ParsedTemplateInfo(&ParamLists,
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000153 isSpecialization,
154 LastParamListWasEmpty),
Douglas Gregor1426e532009-05-12 21:31:51 +0000155 DeclEnd, AS);
156}
Chris Lattner682bf922009-03-29 16:50:03 +0000157
Douglas Gregor1426e532009-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 Stump1eb44332009-09-09 15:08:12 +0000178Parser::DeclPtrTy
Douglas Gregor1426e532009-05-12 21:31:51 +0000179Parser::ParseSingleDeclarationAfterTemplate(
180 unsigned Context,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000181 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor1426e532009-05-12 21:31:51 +0000182 SourceLocation &DeclEnd,
183 AccessSpecifier AS) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000184 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
185 "Template information required");
186
Douglas Gregor37b372b2009-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 Stump1eb44332009-09-09 15:08:12 +0000192
Douglas Gregor1426e532009-05-12 21:31:51 +0000193 // Parse the declaration specifiers.
John McCall54abf7d2009-11-04 02:18:39 +0000194 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000195
196 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
197 DS.AddAttributes(ParseCXX0XAttributes().AttrList);
198
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000199 ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
200 getDeclSpecContextFromDeclaratorContext(Context));
Douglas Gregor1426e532009-05-12 21:31:51 +0000201
202 if (Tok.is(tok::semi)) {
203 DeclEnd = ConsumeToken();
John McCall54abf7d2009-11-04 02:18:39 +0000204 DeclPtrTy Decl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
205 DS.complete(Decl);
206 return Decl;
Douglas Gregor1426e532009-05-12 21:31:51 +0000207 }
208
209 // Parse the declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000210 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
Douglas Gregor1426e532009-05-12 21:31:51 +0000211 ParseDeclarator(DeclaratorInfo);
212 // Error parsing the declarator?
213 if (!DeclaratorInfo.hasName()) {
214 // If so, skip until the semi-colon or a }.
215 SkipUntil(tok::r_brace, true, true);
216 if (Tok.is(tok::semi))
217 ConsumeToken();
218 return DeclPtrTy();
219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Douglas Gregor1426e532009-05-12 21:31:51 +0000221 // If we have a declaration or declarator list, handle it.
222 if (isDeclarationAfterDeclarator()) {
223 // Parse this declaration.
Douglas Gregore542c862009-06-23 23:11:28 +0000224 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
225 TemplateInfo);
Douglas Gregor1426e532009-05-12 21:31:51 +0000226
227 if (Tok.is(tok::comma)) {
228 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000229 << (int)TemplateInfo.Kind;
Douglas Gregor1426e532009-05-12 21:31:51 +0000230 SkipUntil(tok::semi, true, false);
231 return ThisDecl;
232 }
233
234 // Eat the semi colon after the declaration.
John McCall5c15fe12009-07-31 02:20:35 +0000235 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
John McCall54abf7d2009-11-04 02:18:39 +0000236 DS.complete(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +0000237 return ThisDecl;
238 }
239
240 if (DeclaratorInfo.isFunctionDeclarator() &&
241 isStartOfFunctionDefinition()) {
242 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
243 Diag(Tok, diag::err_function_declared_typedef);
244
245 if (Tok.is(tok::l_brace)) {
246 // This recovery skips the entire function body. It would be nice
247 // to simply call ParseFunctionDefinition() below, however Sema
248 // assumes the declarator represents a function, not a typedef.
249 ConsumeBrace();
250 SkipUntil(tok::r_brace, true);
251 } else {
252 SkipUntil(tok::semi);
253 }
254 return DeclPtrTy();
255 }
Douglas Gregor52591bf2009-06-24 00:54:41 +0000256 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregor1426e532009-05-12 21:31:51 +0000257 }
258
259 if (DeclaratorInfo.isFunctionDeclarator())
260 Diag(Tok, diag::err_expected_fn_body);
261 else
262 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
263 SkipUntil(tok::semi);
264 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000265}
266
267/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000268/// angle brackets. Depth is the depth of this template-parameter-list, which
269/// is the number of template headers directly enclosing this template header.
270/// TemplateParams is the current list of template parameters we're building.
271/// The template parameter we parse will be added to this list. LAngleLoc and
Mike Stump1eb44332009-09-09 15:08:12 +0000272/// RAngleLoc will receive the positions of the '<' and '>', respectively,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000273/// that enclose this template parameter list.
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000274///
275/// \returns true if an error occurred, false otherwise.
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000276bool Parser::ParseTemplateParameters(unsigned Depth,
277 TemplateParameterList &TemplateParams,
278 SourceLocation &LAngleLoc,
279 SourceLocation &RAngleLoc) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000280 // Get the template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +0000281 if (!Tok.is(tok::less)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000282 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000283 return true;
Douglas Gregoradcac882008-12-01 23:54:00 +0000284 }
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000285 LAngleLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Douglas Gregoradcac882008-12-01 23:54:00 +0000287 // Try to parse the template parameter list.
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000288 if (Tok.is(tok::greater))
289 RAngleLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000290 else if (ParseTemplateParameterList(Depth, TemplateParams)) {
291 if (!Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000292 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000293 return true;
Douglas Gregoradcac882008-12-01 23:54:00 +0000294 }
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000295 RAngleLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000296 }
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000297 return false;
Douglas Gregoradcac882008-12-01 23:54:00 +0000298}
299
300/// ParseTemplateParameterList - Parse a template parameter list. If
301/// the parsing fails badly (i.e., closing bracket was left out), this
302/// will try to put the token stream in a reasonable position (closing
Mike Stump1eb44332009-09-09 15:08:12 +0000303/// a statement, etc.) and return false.
Douglas Gregoradcac882008-12-01 23:54:00 +0000304///
305/// template-parameter-list: [C++ temp]
306/// template-parameter
307/// template-parameter-list ',' template-parameter
Mike Stump1eb44332009-09-09 15:08:12 +0000308bool
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000309Parser::ParseTemplateParameterList(unsigned Depth,
310 TemplateParameterList &TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000311 while (1) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000312 if (DeclPtrTy TmpParam
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000313 = ParseTemplateParameter(Depth, TemplateParams.size())) {
314 TemplateParams.push_back(TmpParam);
315 } else {
Douglas Gregoradcac882008-12-01 23:54:00 +0000316 // If we failed to parse a template parameter, skip until we find
317 // a comma or closing brace.
318 SkipUntil(tok::comma, tok::greater, true, true);
319 }
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Douglas Gregoradcac882008-12-01 23:54:00 +0000321 // Did we find a comma or the end of the template parmeter list?
Mike Stump1eb44332009-09-09 15:08:12 +0000322 if (Tok.is(tok::comma)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000323 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000324 } else if (Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000325 // Don't consume this... that's done by template parser.
326 break;
327 } else {
328 // Somebody probably forgot to close the template. Skip ahead and
329 // try to get out of the expression. This error is currently
330 // subsumed by whatever goes on in ParseTemplateParameter.
331 // TODO: This could match >>, and it would be nice to avoid those
332 // silly errors with template <vec<T>>.
333 // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
334 SkipUntil(tok::greater, true, true);
335 return false;
336 }
337 }
338 return true;
339}
340
Douglas Gregor98440b42009-11-21 02:07:55 +0000341/// \brief Determine whether the parser is at the start of a template
342/// type parameter.
343bool Parser::isStartOfTemplateTypeParameter() {
344 if (Tok.is(tok::kw_class))
345 return true;
346
347 if (Tok.isNot(tok::kw_typename))
348 return false;
349
350 // C++ [temp.param]p2:
351 // There is no semantic difference between class and typename in a
352 // template-parameter. typename followed by an unqualified-id
353 // names a template type parameter. typename followed by a
354 // qualified-id denotes the type in a non-type
355 // parameter-declaration.
356 Token Next = NextToken();
357
358 // If we have an identifier, skip over it.
359 if (Next.getKind() == tok::identifier)
360 Next = GetLookAheadToken(2);
361
362 switch (Next.getKind()) {
363 case tok::equal:
364 case tok::comma:
365 case tok::greater:
366 case tok::greatergreater:
367 case tok::ellipsis:
368 return true;
369
370 default:
371 return false;
372 }
373}
374
Douglas Gregoradcac882008-12-01 23:54:00 +0000375/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
376///
377/// template-parameter: [C++ temp.param]
378/// type-parameter
379/// parameter-declaration
380///
381/// type-parameter: (see below)
Anders Carlssonce5635a2009-06-12 23:09:56 +0000382/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000383/// 'class' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000384/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000385/// 'typename' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000386/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000387/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Mike Stump1eb44332009-09-09 15:08:12 +0000388Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000389Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor98440b42009-11-21 02:07:55 +0000390 if (isStartOfTemplateTypeParameter())
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000391 return ParseTypeParameter(Depth, Position);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
393 if (Tok.is(tok::kw_template))
Chris Lattner532e19b2009-01-04 23:51:17 +0000394 return ParseTemplateTemplateParameter(Depth, Position);
395
396 // If it's none of the above, then it must be a parameter declaration.
397 // NOTE: This will pick up errors in the closure of the template parameter
398 // list (e.g., template < ; Check here to implement >> style closures.
399 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000400}
401
402/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
403/// Other kinds of template parameters are parsed in
404/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
405///
406/// type-parameter: [C++ temp.param]
Anders Carlssonce5635a2009-06-12 23:09:56 +0000407/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000408/// 'class' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000409/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000410/// 'typename' identifier[opt] '=' type-id
Chris Lattnerb28317a2009-03-28 19:18:32 +0000411Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregor26236e82008-12-02 00:41:28 +0000412 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000413 "A type-parameter starts with 'class' or 'typename'");
Douglas Gregor26236e82008-12-02 00:41:28 +0000414
415 // Consume the 'class' or 'typename' keyword.
416 bool TypenameKeyword = Tok.is(tok::kw_typename);
417 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000418
Anders Carlsson941df7d2009-06-12 19:58:00 +0000419 // Grab the ellipsis (if given).
420 bool Ellipsis = false;
421 SourceLocation EllipsisLoc;
Anders Carlssonce5635a2009-06-12 23:09:56 +0000422 if (Tok.is(tok::ellipsis)) {
Anders Carlsson941df7d2009-06-12 19:58:00 +0000423 Ellipsis = true;
424 EllipsisLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000425
426 if (!getLang().CPlusPlus0x)
Anders Carlssonce5635a2009-06-12 23:09:56 +0000427 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson941df7d2009-06-12 19:58:00 +0000428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Douglas Gregoradcac882008-12-01 23:54:00 +0000430 // Grab the template parameter name (if given)
Douglas Gregor26236e82008-12-02 00:41:28 +0000431 SourceLocation NameLoc;
432 IdentifierInfo* ParamName = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000433 if (Tok.is(tok::identifier)) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000434 ParamName = Tok.getIdentifierInfo();
435 NameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000436 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
437 Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000438 // Unnamed template parameter. Don't have to do anything here, just
439 // don't consume this token.
440 } else {
441 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000442 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattnerb28317a2009-03-28 19:18:32 +0000445 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson941df7d2009-06-12 19:58:00 +0000446 Ellipsis, EllipsisLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000447 KeyLoc, ParamName, NameLoc,
448 Depth, Position);
Douglas Gregor26236e82008-12-02 00:41:28 +0000449
Douglas Gregoradcac882008-12-01 23:54:00 +0000450 // Grab a default type id (if given).
Mike Stump1eb44332009-09-09 15:08:12 +0000451 if (Tok.is(tok::equal)) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000452 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregord684b002009-02-10 19:49:53 +0000453 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +0000454 TypeResult DefaultType = ParseTypeName();
455 if (!DefaultType.isInvalid())
Douglas Gregord684b002009-02-10 19:49:53 +0000456 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +0000457 DefaultType.get());
Douglas Gregoradcac882008-12-01 23:54:00 +0000458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Douglas Gregor26236e82008-12-02 00:41:28 +0000460 return TypeParam;
Douglas Gregoradcac882008-12-01 23:54:00 +0000461}
462
463/// ParseTemplateTemplateParameter - Handle the parsing of template
Mike Stump1eb44332009-09-09 15:08:12 +0000464/// template parameters.
Douglas Gregoradcac882008-12-01 23:54:00 +0000465///
466/// type-parameter: [C++ temp.param]
467/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
468/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattnerb28317a2009-03-28 19:18:32 +0000469Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000470Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000471 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
472
473 // Handle the template <...> part.
474 SourceLocation TemplateLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000475 TemplateParameterList TemplateParams;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000476 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor68c69932009-02-10 19:52:54 +0000477 {
478 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000479 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000480 RAngleLoc)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000481 return DeclPtrTy();
Douglas Gregor68c69932009-02-10 19:52:54 +0000482 }
Douglas Gregoradcac882008-12-01 23:54:00 +0000483 }
484
485 // Generate a meaningful error if the user forgot to put class before the
486 // identifier, comma, or greater.
Mike Stump1eb44332009-09-09 15:08:12 +0000487 if (!Tok.is(tok::kw_class)) {
488 Diag(Tok.getLocation(), diag::err_expected_class_before)
Douglas Gregoradcac882008-12-01 23:54:00 +0000489 << PP.getSpelling(Tok);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000490 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000491 }
492 SourceLocation ClassLoc = ConsumeToken();
493
494 // Get the identifier, if given.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000495 SourceLocation NameLoc;
496 IdentifierInfo* ParamName = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000497 if (Tok.is(tok::identifier)) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000498 ParamName = Tok.getIdentifierInfo();
499 NameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000500 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000501 // Unnamed template parameter. Don't have to do anything here, just
502 // don't consume this token.
503 } else {
504 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000505 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000506 }
507
Mike Stump1eb44332009-09-09 15:08:12 +0000508 TemplateParamsTy *ParamList =
Douglas Gregorddc29e12009-02-06 22:42:48 +0000509 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
510 TemplateLoc, LAngleLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000511 &TemplateParams[0],
Douglas Gregorddc29e12009-02-06 22:42:48 +0000512 TemplateParams.size(),
513 RAngleLoc);
514
Chris Lattnerb28317a2009-03-28 19:18:32 +0000515 Parser::DeclPtrTy Param
Douglas Gregord684b002009-02-10 19:49:53 +0000516 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
517 ParamList, ParamName,
518 NameLoc, Depth, Position);
519
520 // Get the a default value, if given.
521 if (Tok.is(tok::equal)) {
522 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor788cd062009-11-11 01:00:40 +0000523 ParsedTemplateArgument Default = ParseTemplateTemplateArgument();
524 if (Default.isInvalid()) {
525 Diag(Tok.getLocation(),
526 diag::err_default_template_template_parameter_not_template);
Nuno Lopes68f7a242009-12-10 00:07:02 +0000527 static const tok::TokenKind EndToks[] = {
Douglas Gregor788cd062009-11-11 01:00:40 +0000528 tok::comma, tok::greater, tok::greatergreater
529 };
530 SkipUntil(EndToks, 3, true, true);
Douglas Gregord684b002009-02-10 19:49:53 +0000531 return Param;
Douglas Gregor788cd062009-11-11 01:00:40 +0000532 } else if (Param)
533 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc, Default);
Douglas Gregord684b002009-02-10 19:49:53 +0000534 }
535
536 return Param;
Douglas Gregoradcac882008-12-01 23:54:00 +0000537}
538
539/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
Mike Stump1eb44332009-09-09 15:08:12 +0000540/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000541///
Douglas Gregoradcac882008-12-01 23:54:00 +0000542/// template-parameter:
543/// ...
544/// parameter-declaration
545///
546/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
547/// but that didn't work out to well. Instead, this tries to recrate the basic
548/// parsing of parameter declarations, but tries to constrain it for template
549/// parameters.
Douglas Gregor26236e82008-12-02 00:41:28 +0000550/// FIXME: We need to make a ParseParameterDeclaration that works for
551/// non-type template parameters and normal function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +0000552Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000553Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000554 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregoradcac882008-12-01 23:54:00 +0000555
556 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregor26236e82008-12-02 00:41:28 +0000557 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregoradcac882008-12-01 23:54:00 +0000558 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregor26236e82008-12-02 00:41:28 +0000559 DeclSpec DS;
560 ParseDeclarationSpecifiers(DS);
Douglas Gregoradcac882008-12-01 23:54:00 +0000561
562 // Parse this as a typename.
Douglas Gregor26236e82008-12-02 00:41:28 +0000563 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
564 ParseDeclarator(ParamDecl);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000565 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000566 // This probably shouldn't happen - and it's more of a Sema thing, but
567 // basically we didn't parse the type name because we couldn't associate
568 // it with an AST node. we should just skip to the comma or greater.
569 // TODO: This is currently a placeholder for some kind of Sema Error.
570 Diag(Tok.getLocation(), diag::err_parse_error);
571 SkipUntil(tok::comma, tok::greater, true, true);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000572 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000573 }
574
Mike Stump1eb44332009-09-09 15:08:12 +0000575 // Create the parameter.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000576 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
577 Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000578
Douglas Gregord684b002009-02-10 19:49:53 +0000579 // If there is a default value, parse it.
Chris Lattner7452c6f2009-01-05 01:24:05 +0000580 if (Tok.is(tok::equal)) {
Douglas Gregord684b002009-02-10 19:49:53 +0000581 SourceLocation EqualLoc = ConsumeToken();
582
583 // C++ [temp.param]p15:
584 // When parsing a default template-argument for a non-type
585 // template-parameter, the first non-nested > is taken as the
586 // end of the template-parameter-list rather than a greater-than
587 // operator.
Mike Stump1eb44332009-09-09 15:08:12 +0000588 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000589
590 OwningExprResult DefaultArg = ParseAssignmentExpression();
591 if (DefaultArg.isInvalid())
592 SkipUntil(tok::comma, tok::greater, true, true);
593 else if (Param)
Mike Stump1eb44332009-09-09 15:08:12 +0000594 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
Douglas Gregord684b002009-02-10 19:49:53 +0000595 move(DefaultArg));
Douglas Gregoradcac882008-12-01 23:54:00 +0000596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregor26236e82008-12-02 00:41:28 +0000598 return Param;
Douglas Gregoradcac882008-12-01 23:54:00 +0000599}
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000600
Douglas Gregorcc636682009-02-17 23:15:12 +0000601/// \brief Parses a template-id that after the template name has
602/// already been parsed.
603///
604/// This routine takes care of parsing the enclosed template argument
605/// list ('<' template-parameter-list [opt] '>') and placing the
606/// results into a form that can be transferred to semantic analysis.
607///
608/// \param Template the template declaration produced by isTemplateName
609///
610/// \param TemplateNameLoc the source location of the template name
611///
612/// \param SS if non-NULL, the nested-name-specifier preceding the
613/// template name.
614///
615/// \param ConsumeLastToken if true, then we will consume the last
616/// token that forms the template-id. Otherwise, we will leave the
617/// last token in the stream (e.g., so that it can be replaced with an
618/// annotation token).
Mike Stump1eb44332009-09-09 15:08:12 +0000619bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000620Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000621 SourceLocation TemplateNameLoc,
Douglas Gregorcc636682009-02-17 23:15:12 +0000622 const CXXScopeSpec *SS,
623 bool ConsumeLastToken,
624 SourceLocation &LAngleLoc,
625 TemplateArgList &TemplateArgs,
Douglas Gregorcc636682009-02-17 23:15:12 +0000626 SourceLocation &RAngleLoc) {
627 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
628
629 // Consume the '<'.
630 LAngleLoc = ConsumeToken();
631
632 // Parse the optional template-argument-list.
633 bool Invalid = false;
634 {
635 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
636 if (Tok.isNot(tok::greater))
Douglas Gregor314b97f2009-11-10 19:49:08 +0000637 Invalid = ParseTemplateArgumentList(TemplateArgs);
Douglas Gregorcc636682009-02-17 23:15:12 +0000638
639 if (Invalid) {
640 // Try to find the closing '>'.
641 SkipUntil(tok::greater, true, !ConsumeLastToken);
642
643 return true;
644 }
645 }
646
Eli Friedman64a4eb22009-12-27 22:31:18 +0000647 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) {
648 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregorcc636682009-02-17 23:15:12 +0000649 return true;
Eli Friedman64a4eb22009-12-27 22:31:18 +0000650 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000651
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000652 // Determine the location of the '>' or '>>'. Only consume this
653 // token if the caller asked us to.
Douglas Gregorcc636682009-02-17 23:15:12 +0000654 RAngleLoc = Tok.getLocation();
655
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000656 if (Tok.is(tok::greatergreater)) {
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000657 if (!getLang().CPlusPlus0x) {
658 const char *ReplaceStr = "> >";
659 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
660 ReplaceStr = "> > ";
661
662 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregorb2fb6de2009-02-27 17:53:17 +0000663 << CodeModificationHint::CreateReplacement(
664 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000665 }
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000666
667 Tok.setKind(tok::greater);
668 if (!ConsumeLastToken) {
669 // Since we're not supposed to consume the '>>' token, we need
670 // to insert a second '>' token after the first.
671 PP.EnterToken(Tok);
672 }
673 } else if (ConsumeLastToken)
Douglas Gregorcc636682009-02-17 23:15:12 +0000674 ConsumeToken();
675
676 return false;
677}
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Douglas Gregor39a8de12009-02-25 19:37:18 +0000679/// \brief Replace the tokens that form a simple-template-id with an
680/// annotation token containing the complete template-id.
681///
682/// The first token in the stream must be the name of a template that
683/// is followed by a '<'. This routine will parse the complete
684/// simple-template-id and replace the tokens with a single annotation
685/// token with one of two different kinds: if the template-id names a
686/// type (and \p AllowTypeAnnotation is true), the annotation token is
687/// a type annotation that includes the optional nested-name-specifier
688/// (\p SS). Otherwise, the annotation token is a template-id
689/// annotation that does not include the optional
690/// nested-name-specifier.
691///
692/// \param Template the declaration of the template named by the first
693/// token (an identifier), as returned from \c Action::isTemplateName().
694///
695/// \param TemplateNameKind the kind of template that \p Template
696/// refers to, as returned from \c Action::isTemplateName().
697///
698/// \param SS if non-NULL, the nested-name-specifier that precedes
699/// this template name.
700///
701/// \param TemplateKWLoc if valid, specifies that this template-id
702/// annotation was preceded by the 'template' keyword and gives the
703/// location of that keyword. If invalid (the default), then this
704/// template-id was not preceded by a 'template' keyword.
705///
706/// \param AllowTypeAnnotation if true (the default), then a
707/// simple-template-id that refers to a class template, template
708/// template parameter, or other template that produces a type will be
709/// replaced with a type annotation token. Otherwise, the
710/// simple-template-id is always replaced with a template-id
711/// annotation token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000712///
713/// If an unrecoverable parse error occurs and no annotation token can be
714/// formed, this function returns true.
715///
716bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Mike Stump1eb44332009-09-09 15:08:12 +0000717 const CXXScopeSpec *SS,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000718 UnqualifiedId &TemplateName,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000719 SourceLocation TemplateKWLoc,
720 bool AllowTypeAnnotation) {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000721 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000722 assert(Template && Tok.is(tok::less) &&
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000723 "Parser isn't at the beginning of a template-id");
724
725 // Consume the template-name.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000726 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000727
Douglas Gregorcc636682009-02-17 23:15:12 +0000728 // Parse the enclosed template argument list.
729 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000730 TemplateArgList TemplateArgs;
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000731 bool Invalid = ParseTemplateIdAfterTemplateName(Template,
732 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000733 SS, false, LAngleLoc,
734 TemplateArgs,
Douglas Gregorcc636682009-02-17 23:15:12 +0000735 RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000737 if (Invalid) {
738 // If we failed to parse the template ID but skipped ahead to a >, we're not
739 // going to be able to form a token annotation. Eat the '>' if present.
740 if (Tok.is(tok::greater))
741 ConsumeToken();
742 return true;
743 }
Douglas Gregorc15cb382009-02-09 23:23:08 +0000744
Jay Foadbeaaccd2009-05-21 09:52:38 +0000745 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregorcc636682009-02-17 23:15:12 +0000746 TemplateArgs.size());
Douglas Gregorf02da892009-02-09 21:04:56 +0000747
Douglas Gregor55f6b142009-02-09 18:46:07 +0000748 // Build the annotation token.
Douglas Gregorc45c2322009-03-31 00:43:58 +0000749 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Mike Stump1eb44332009-09-09 15:08:12 +0000750 Action::TypeResult Type
Douglas Gregor7532dc62009-03-30 22:58:21 +0000751 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
752 LAngleLoc, TemplateArgsPtr,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000753 RAngleLoc);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000754 if (Type.isInvalid()) {
755 // If we failed to parse the template ID but skipped ahead to a >, we're not
756 // going to be able to form a token annotation. Eat the '>' if present.
757 if (Tok.is(tok::greater))
758 ConsumeToken();
759 return true;
760 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000761
762 Tok.setKind(tok::annot_typename);
763 Tok.setAnnotationValue(Type.get());
Douglas Gregor39a8de12009-02-25 19:37:18 +0000764 if (SS && SS->isNotEmpty())
765 Tok.setLocation(SS->getBeginLoc());
766 else if (TemplateKWLoc.isValid())
767 Tok.setLocation(TemplateKWLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000768 else
Douglas Gregor39a8de12009-02-25 19:37:18 +0000769 Tok.setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +0000770 } else {
Douglas Gregorc45c2322009-03-31 00:43:58 +0000771 // Build a template-id annotation token that can be processed
772 // later.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000773 Tok.setKind(tok::annot_template_id);
Mike Stump1eb44332009-09-09 15:08:12 +0000774 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000775 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor55f6b142009-02-09 18:46:07 +0000776 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000777 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
778 TemplateId->Name = TemplateName.Identifier;
779 TemplateId->Operator = OO_None;
780 } else {
781 TemplateId->Name = 0;
782 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
783 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000784 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000785 TemplateId->Kind = TNK;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000786 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000787 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000788 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
789 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
Douglas Gregor55f6b142009-02-09 18:46:07 +0000790 Args[Arg] = TemplateArgs[Arg];
791 Tok.setAnnotationValue(TemplateId);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000792 if (TemplateKWLoc.isValid())
793 Tok.setLocation(TemplateKWLoc);
794 else
795 Tok.setLocation(TemplateNameLoc);
796
797 TemplateArgsPtr.release();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000798 }
799
800 // Common fields for the annotation token
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000801 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000802
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000803 // In case the tokens were cached, have Preprocessor replace them with the
804 // annotation token.
805 PP.AnnotateCachedTokens(Tok);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000806 return false;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000807}
808
Douglas Gregor39a8de12009-02-25 19:37:18 +0000809/// \brief Replaces a template-id annotation token with a type
810/// annotation token.
811///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000812/// If there was a failure when forming the type from the template-id,
813/// a type annotation token will still be created, but will have a
814/// NULL type pointer to signify an error.
815void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000816 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
817
Mike Stump1eb44332009-09-09 15:08:12 +0000818 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000819 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000820 assert((TemplateId->Kind == TNK_Type_template ||
821 TemplateId->Kind == TNK_Dependent_template_name) &&
822 "Only works for type and dependent templates");
Mike Stump1eb44332009-09-09 15:08:12 +0000823
824 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000825 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000826 TemplateId->NumArgs);
827
Mike Stump1eb44332009-09-09 15:08:12 +0000828 Action::TypeResult Type
Douglas Gregor7532dc62009-03-30 22:58:21 +0000829 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
830 TemplateId->TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000831 TemplateId->LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000832 TemplateArgsPtr,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000833 TemplateId->RAngleLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000834 // Create the new "type" annotation token.
835 Tok.setKind(tok::annot_typename);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000836 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor39a8de12009-02-25 19:37:18 +0000837 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
838 Tok.setLocation(SS->getBeginLoc());
Sebastian Redl39d67112010-02-08 19:35:18 +0000839 // End location stays the same
Douglas Gregor39a8de12009-02-25 19:37:18 +0000840
Douglas Gregor86235412009-11-04 18:18:19 +0000841 // Replace the template-id annotation token, and possible the scope-specifier
842 // that precedes it, with the typename annotation token.
843 PP.AnnotateCachedTokens(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000844 TemplateId->Destroy();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000845}
846
Douglas Gregor314b97f2009-11-10 19:49:08 +0000847/// \brief Determine whether the given token can end a template argument.
Benjamin Kramer3a4a2b32009-11-10 21:29:56 +0000848static bool isEndOfTemplateArgument(Token Tok) {
Douglas Gregor314b97f2009-11-10 19:49:08 +0000849 return Tok.is(tok::comma) || Tok.is(tok::greater) ||
850 Tok.is(tok::greatergreater);
851}
852
Douglas Gregor788cd062009-11-11 01:00:40 +0000853/// \brief Parse a C++ template template argument.
854ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
855 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
856 !Tok.is(tok::annot_cxxscope))
857 return ParsedTemplateArgument();
858
859 // C++0x [temp.arg.template]p1:
860 // A template-argument for a template template-parameter shall be the name
861 // of a class template or a template alias, expressed as id-expression.
862 //
Douglas Gregoreaf75f42009-11-12 00:03:40 +0000863 // We parse an id-expression that refers to a class template or template
864 // alias. The grammar we parse is:
Douglas Gregor788cd062009-11-11 01:00:40 +0000865 //
866 // nested-name-specifier[opt] template[opt] identifier
867 //
868 // followed by a token that terminates a template argument, such as ',',
869 // '>', or (in some cases) '>>'.
Douglas Gregor788cd062009-11-11 01:00:40 +0000870 CXXScopeSpec SS; // nested-name-specifier, if present
871 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
872 /*EnteringContext=*/false);
873
874 if (SS.isSet() && Tok.is(tok::kw_template)) {
875 // Parse the optional 'template' keyword following the
876 // nested-name-specifier.
877 SourceLocation TemplateLoc = ConsumeToken();
878
879 if (Tok.is(tok::identifier)) {
880 // We appear to have a dependent template name.
881 UnqualifiedId Name;
882 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
883 ConsumeToken(); // the identifier
884
885 // If the next token signals the end of a template argument,
886 // then we have a dependent template name that could be a template
887 // template argument.
888 if (isEndOfTemplateArgument(Tok)) {
889 TemplateTy Template
890 = Actions.ActOnDependentTemplateName(TemplateLoc, SS, Name,
Douglas Gregora481edb2009-11-20 23:39:24 +0000891 /*ObjectType=*/0,
892 /*EnteringContext=*/false);
Douglas Gregoreaf75f42009-11-12 00:03:40 +0000893 if (Template.get())
Douglas Gregor788cd062009-11-11 01:00:40 +0000894 return ParsedTemplateArgument(SS, Template, Name.StartLocation);
Douglas Gregor788cd062009-11-11 01:00:40 +0000895 }
896 }
897 } else if (Tok.is(tok::identifier)) {
898 // We may have a (non-dependent) template name.
899 TemplateTy Template;
900 UnqualifiedId Name;
901 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
902 ConsumeToken(); // the identifier
903
904 if (isEndOfTemplateArgument(Tok)) {
905 TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS, Name,
906 /*ObjectType=*/0,
907 /*EnteringContext=*/false,
908 Template);
909 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
910 // We have an id-expression that refers to a class template or
911 // (C++0x) template alias.
Douglas Gregor788cd062009-11-11 01:00:40 +0000912 return ParsedTemplateArgument(SS, Template, Name.StartLocation);
913 }
914 }
915 }
916
Douglas Gregoreaf75f42009-11-12 00:03:40 +0000917 // We don't have a template template argument.
Douglas Gregor788cd062009-11-11 01:00:40 +0000918 return ParsedTemplateArgument();
919}
920
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000921/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
922///
923/// template-argument: [C++ 14.2]
Douglas Gregorac7610d2009-06-22 20:57:11 +0000924/// constant-expression
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000925/// type-id
926/// id-expression
Douglas Gregor314b97f2009-11-10 19:49:08 +0000927ParsedTemplateArgument Parser::ParseTemplateArgument() {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000928 // C++ [temp.arg]p2:
929 // In a template-argument, an ambiguity between a type-id and an
930 // expression is resolved to a type-id, regardless of the form of
931 // the corresponding template-parameter.
932 //
Douglas Gregor314b97f2009-11-10 19:49:08 +0000933 // Therefore, we initially try to parse a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000934 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregor314b97f2009-11-10 19:49:08 +0000935 SourceLocation Loc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +0000936 TypeResult TypeArg = ParseTypeName();
937 if (TypeArg.isInvalid())
Douglas Gregor314b97f2009-11-10 19:49:08 +0000938 return ParsedTemplateArgument();
939
940 return ParsedTemplateArgument(ParsedTemplateArgument::Type, TypeArg.get(),
941 Loc);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000942 }
Douglas Gregor788cd062009-11-11 01:00:40 +0000943
944 // Try to parse a template template argument.
Douglas Gregoreaf75f42009-11-12 00:03:40 +0000945 {
946 TentativeParsingAction TPA(*this);
947
948 ParsedTemplateArgument TemplateTemplateArgument
949 = ParseTemplateTemplateArgument();
950 if (!TemplateTemplateArgument.isInvalid()) {
951 TPA.Commit();
952 return TemplateTemplateArgument;
953 }
954
955 // Revert this tentative parse to parse a non-type template argument.
956 TPA.Revert();
957 }
Douglas Gregor314b97f2009-11-10 19:49:08 +0000958
959 // Parse a non-type template argument.
960 SourceLocation Loc = Tok.getLocation();
Douglas Gregorac7610d2009-06-22 20:57:11 +0000961 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregorc15cb382009-02-09 23:23:08 +0000962 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregor314b97f2009-11-10 19:49:08 +0000963 return ParsedTemplateArgument();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000964
Douglas Gregor314b97f2009-11-10 19:49:08 +0000965 return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
966 ExprArg.release(), Loc);
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000967}
968
969/// ParseTemplateArgumentList - Parse a C++ template-argument-list
970/// (C++ [temp.names]). Returns true if there was an error.
971///
972/// template-argument-list: [C++ 14.2]
973/// template-argument
974/// template-argument-list ',' template-argument
Mike Stump1eb44332009-09-09 15:08:12 +0000975bool
Douglas Gregor314b97f2009-11-10 19:49:08 +0000976Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000977 while (true) {
Douglas Gregor314b97f2009-11-10 19:49:08 +0000978 ParsedTemplateArgument Arg = ParseTemplateArgument();
979 if (Arg.isInvalid()) {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000980 SkipUntil(tok::comma, tok::greater, true, true);
981 return true;
982 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000983
Douglas Gregor314b97f2009-11-10 19:49:08 +0000984 // Save this template argument.
985 TemplateArgs.push_back(Arg);
986
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000987 // If the next token is a comma, consume it and keep reading
988 // arguments.
989 if (Tok.isNot(tok::comma)) break;
990
991 // Consume the comma.
992 ConsumeToken();
993 }
994
Eli Friedman64a4eb22009-12-27 22:31:18 +0000995 return false;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000996}
997
Mike Stump1eb44332009-09-09 15:08:12 +0000998/// \brief Parse a C++ explicit template instantiation
Douglas Gregor1426e532009-05-12 21:31:51 +0000999/// (C++ [temp.explicit]).
1000///
1001/// explicit-instantiation:
Douglas Gregor45f96552009-09-04 06:33:52 +00001002/// 'extern' [opt] 'template' declaration
1003///
1004/// Note that the 'extern' is a GNU extension and C++0x feature.
Mike Stump1eb44332009-09-09 15:08:12 +00001005Parser::DeclPtrTy
Douglas Gregor45f96552009-09-04 06:33:52 +00001006Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
1007 SourceLocation TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001008 SourceLocation &DeclEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001009 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
Douglas Gregor45f96552009-09-04 06:33:52 +00001010 ParsedTemplateInfo(ExternLoc,
1011 TemplateLoc),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001012 DeclEnd, AS_none);
Douglas Gregor1426e532009-05-12 21:31:51 +00001013}