blob: 045acd86ad0f2d46cf93f92044f90b9880c96714 [file] [log] [blame]
Douglas Gregoradcac882008-12-01 23:54:00 +00001//===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements parsing of C++ templates.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregoradcac882008-12-01 23:54:00 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
Douglas Gregorc3058332009-08-24 23:03:25 +000018#include "llvm/Support/Compiler.h"
Douglas Gregoradcac882008-12-01 23:54:00 +000019using namespace clang;
20
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000021/// \brief Parse a template declaration, explicit instantiation, or
22/// explicit specialization.
23Parser::DeclPtrTy
24Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
25 SourceLocation &DeclEnd,
26 AccessSpecifier AS) {
27 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less))
Mike Stump1eb44332009-09-09 15:08:12 +000028 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
Douglas Gregor45f96552009-09-04 06:33:52 +000029 DeclEnd);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000030
31 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
32}
33
Douglas Gregorc3058332009-08-24 23:03:25 +000034/// \brief RAII class that manages the template parameter depth.
35namespace {
36 class VISIBILITY_HIDDEN TemplateParameterDepthCounter {
37 unsigned &Depth;
38 unsigned AddedLevels;
39
40 public:
Mike Stump1eb44332009-09-09 15:08:12 +000041 explicit TemplateParameterDepthCounter(unsigned &Depth)
Douglas Gregorc3058332009-08-24 23:03:25 +000042 : Depth(Depth), AddedLevels(0) { }
Mike Stump1eb44332009-09-09 15:08:12 +000043
Douglas Gregorc3058332009-08-24 23:03:25 +000044 ~TemplateParameterDepthCounter() {
45 Depth -= AddedLevels;
46 }
Mike Stump1eb44332009-09-09 15:08:12 +000047
48 void operator++() {
Douglas Gregorc3058332009-08-24 23:03:25 +000049 ++Depth;
50 ++AddedLevels;
51 }
Mike Stump1eb44332009-09-09 15:08:12 +000052
Douglas Gregorc3058332009-08-24 23:03:25 +000053 operator unsigned() const { return Depth; }
54 };
55}
56
Douglas Gregorcc636682009-02-17 23:15:12 +000057/// \brief Parse a template declaration or an explicit specialization.
58///
59/// Template declarations include one or more template parameter lists
60/// and either the function or class template declaration. Explicit
61/// specializations contain one or more 'template < >' prefixes
62/// followed by a (possibly templated) declaration. Since the
63/// syntactic form of both features is nearly identical, we parse all
64/// of the template headers together and let semantic analysis sort
65/// the declarations from the explicit specializations.
Douglas Gregoradcac882008-12-01 23:54:00 +000066///
67/// template-declaration: [C++ temp]
68/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregorcc636682009-02-17 23:15:12 +000069///
70/// explicit-specialization: [ C++ temp.expl.spec]
71/// 'template' '<' '>' declaration
Chris Lattnerb28317a2009-03-28 19:18:32 +000072Parser::DeclPtrTy
Anders Carlsson5aeccdb2009-03-26 00:52:18 +000073Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +000074 SourceLocation &DeclEnd,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +000075 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +000076 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
77 "Token does not start a template declaration.");
78
Douglas Gregor26236e82008-12-02 00:41:28 +000079 // Enter template-parameter scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +000080 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregor26236e82008-12-02 00:41:28 +000081
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +000082 // Parse multiple levels of template headers within this template
83 // parameter scope, e.g.,
84 //
85 // template<typename T>
86 // template<typename U>
87 // class A<T>::B { ... };
88 //
89 // We parse multiple levels non-recursively so that we can build a
90 // single data structure containing all of the template parameter
Douglas Gregorcc636682009-02-17 23:15:12 +000091 // lists to easily differentiate between the case above and:
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +000092 //
93 // template<typename T>
94 // class A {
95 // template<typename U> class B;
96 // };
97 //
98 // In the first case, the action for declaring A<T>::B receives
99 // both template parameter lists. In the second case, the action for
100 // defining A<T>::B receives just the inner template parameter list
101 // (and retrieves the outer template parameter list from its
102 // context).
Douglas Gregor0f499d92009-08-20 18:46:05 +0000103 bool isSpecialization = true;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000104 bool LastParamListWasEmpty = false;
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000105 TemplateParameterLists ParamLists;
Douglas Gregorc3058332009-08-24 23:03:25 +0000106 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
Douglas Gregorc4b4e7b2008-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 Lattnerb28317a2009-03-28 19:18:32 +0000120 return DeclPtrTy();
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000123 // Parse the '<' template-parameter-list '>'
124 SourceLocation LAngleLoc, RAngleLoc;
125 TemplateParameterList TemplateParams;
Mike Stump1eb44332009-09-09 15:08:12 +0000126 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
Douglas Gregor7cdbc582009-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 Stump1eb44332009-09-09 15:08:12 +0000132 return DeclPtrTy();
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000133 }
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000134
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000135 ParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000136 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
137 TemplateLoc, LAngleLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000138 TemplateParams.data(),
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000139 TemplateParams.size(), RAngleLoc));
Douglas Gregorc3058332009-08-24 23:03:25 +0000140
141 if (!TemplateParams.empty()) {
142 isSpecialization = false;
143 ++Depth;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000144 } else {
145 LastParamListWasEmpty = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000146 }
Douglas Gregorc4b4e7b2008-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 Stump1eb44332009-09-09 15:08:12 +0000150 return ParseSingleDeclarationAfterTemplate(Context,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000151 ParsedTemplateInfo(&ParamLists,
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000152 isSpecialization,
153 LastParamListWasEmpty),
Douglas Gregor1426e532009-05-12 21:31:51 +0000154 DeclEnd, AS);
155}
Chris Lattner682bf922009-03-29 16:50:03 +0000156
Douglas Gregor1426e532009-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 Stump1eb44332009-09-09 15:08:12 +0000177Parser::DeclPtrTy
Douglas Gregor1426e532009-05-12 21:31:51 +0000178Parser::ParseSingleDeclarationAfterTemplate(
179 unsigned Context,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000180 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor1426e532009-05-12 21:31:51 +0000181 SourceLocation &DeclEnd,
182 AccessSpecifier AS) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000183 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
184 "Template information required");
185
Douglas Gregor37b372b2009-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 Stump1eb44332009-09-09 15:08:12 +0000191
Douglas Gregor1426e532009-05-12 21:31:51 +0000192 // Parse the declaration specifiers.
John McCall54abf7d2009-11-04 02:18:39 +0000193 ParsingDeclSpec DS(*this);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000194 ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
Douglas Gregor1426e532009-05-12 21:31:51 +0000195
196 if (Tok.is(tok::semi)) {
197 DeclEnd = ConsumeToken();
John McCall54abf7d2009-11-04 02:18:39 +0000198 DeclPtrTy Decl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
199 DS.complete(Decl);
200 return Decl;
Douglas Gregor1426e532009-05-12 21:31:51 +0000201 }
202
203 // Parse the declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000204 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
Douglas Gregor1426e532009-05-12 21:31:51 +0000205 ParseDeclarator(DeclaratorInfo);
206 // Error parsing the declarator?
207 if (!DeclaratorInfo.hasName()) {
208 // If so, skip until the semi-colon or a }.
209 SkipUntil(tok::r_brace, true, true);
210 if (Tok.is(tok::semi))
211 ConsumeToken();
212 return DeclPtrTy();
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Douglas Gregor1426e532009-05-12 21:31:51 +0000215 // If we have a declaration or declarator list, handle it.
216 if (isDeclarationAfterDeclarator()) {
217 // Parse this declaration.
Douglas Gregore542c862009-06-23 23:11:28 +0000218 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
219 TemplateInfo);
Douglas Gregor1426e532009-05-12 21:31:51 +0000220
221 if (Tok.is(tok::comma)) {
222 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000223 << (int)TemplateInfo.Kind;
Douglas Gregor1426e532009-05-12 21:31:51 +0000224 SkipUntil(tok::semi, true, false);
225 return ThisDecl;
226 }
227
228 // Eat the semi colon after the declaration.
John McCall5c15fe12009-07-31 02:20:35 +0000229 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
John McCall54abf7d2009-11-04 02:18:39 +0000230 DS.complete(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +0000231 return ThisDecl;
232 }
233
234 if (DeclaratorInfo.isFunctionDeclarator() &&
235 isStartOfFunctionDefinition()) {
236 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
237 Diag(Tok, diag::err_function_declared_typedef);
238
239 if (Tok.is(tok::l_brace)) {
240 // This recovery skips the entire function body. It would be nice
241 // to simply call ParseFunctionDefinition() below, however Sema
242 // assumes the declarator represents a function, not a typedef.
243 ConsumeBrace();
244 SkipUntil(tok::r_brace, true);
245 } else {
246 SkipUntil(tok::semi);
247 }
248 return DeclPtrTy();
249 }
Douglas Gregor52591bf2009-06-24 00:54:41 +0000250 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregor1426e532009-05-12 21:31:51 +0000251 }
252
253 if (DeclaratorInfo.isFunctionDeclarator())
254 Diag(Tok, diag::err_expected_fn_body);
255 else
256 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
257 SkipUntil(tok::semi);
258 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000259}
260
261/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000262/// angle brackets. Depth is the depth of this template-parameter-list, which
263/// is the number of template headers directly enclosing this template header.
264/// TemplateParams is the current list of template parameters we're building.
265/// The template parameter we parse will be added to this list. LAngleLoc and
Mike Stump1eb44332009-09-09 15:08:12 +0000266/// RAngleLoc will receive the positions of the '<' and '>', respectively,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000267/// that enclose this template parameter list.
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000268///
269/// \returns true if an error occurred, false otherwise.
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000270bool Parser::ParseTemplateParameters(unsigned Depth,
271 TemplateParameterList &TemplateParams,
272 SourceLocation &LAngleLoc,
273 SourceLocation &RAngleLoc) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000274 // Get the template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +0000275 if (!Tok.is(tok::less)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000276 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000277 return true;
Douglas Gregoradcac882008-12-01 23:54:00 +0000278 }
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000279 LAngleLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Douglas Gregoradcac882008-12-01 23:54:00 +0000281 // Try to parse the template parameter list.
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000282 if (Tok.is(tok::greater))
283 RAngleLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000284 else if (ParseTemplateParameterList(Depth, TemplateParams)) {
285 if (!Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000286 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000287 return true;
Douglas Gregoradcac882008-12-01 23:54:00 +0000288 }
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000289 RAngleLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000290 }
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000291 return false;
Douglas Gregoradcac882008-12-01 23:54:00 +0000292}
293
294/// ParseTemplateParameterList - Parse a template parameter list. If
295/// the parsing fails badly (i.e., closing bracket was left out), this
296/// will try to put the token stream in a reasonable position (closing
Mike Stump1eb44332009-09-09 15:08:12 +0000297/// a statement, etc.) and return false.
Douglas Gregoradcac882008-12-01 23:54:00 +0000298///
299/// template-parameter-list: [C++ temp]
300/// template-parameter
301/// template-parameter-list ',' template-parameter
Mike Stump1eb44332009-09-09 15:08:12 +0000302bool
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000303Parser::ParseTemplateParameterList(unsigned Depth,
304 TemplateParameterList &TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000305 while (1) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000306 if (DeclPtrTy TmpParam
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000307 = ParseTemplateParameter(Depth, TemplateParams.size())) {
308 TemplateParams.push_back(TmpParam);
309 } else {
Douglas Gregoradcac882008-12-01 23:54:00 +0000310 // If we failed to parse a template parameter, skip until we find
311 // a comma or closing brace.
312 SkipUntil(tok::comma, tok::greater, true, true);
313 }
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Douglas Gregoradcac882008-12-01 23:54:00 +0000315 // Did we find a comma or the end of the template parmeter list?
Mike Stump1eb44332009-09-09 15:08:12 +0000316 if (Tok.is(tok::comma)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000317 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000318 } else if (Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000319 // Don't consume this... that's done by template parser.
320 break;
321 } else {
322 // Somebody probably forgot to close the template. Skip ahead and
323 // try to get out of the expression. This error is currently
324 // subsumed by whatever goes on in ParseTemplateParameter.
325 // TODO: This could match >>, and it would be nice to avoid those
326 // silly errors with template <vec<T>>.
327 // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
328 SkipUntil(tok::greater, true, true);
329 return false;
330 }
331 }
332 return true;
333}
334
335/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
336///
337/// template-parameter: [C++ temp.param]
338/// type-parameter
339/// parameter-declaration
340///
341/// type-parameter: (see below)
Anders Carlssonce5635a2009-06-12 23:09:56 +0000342/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000343/// 'class' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000344/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000345/// 'typename' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000346/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000347/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Mike Stump1eb44332009-09-09 15:08:12 +0000348Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000349Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Mike Stump1eb44332009-09-09 15:08:12 +0000350 if (Tok.is(tok::kw_class) ||
351 (Tok.is(tok::kw_typename) &&
352 // FIXME: Next token has not been annotated!
353 NextToken().isNot(tok::annot_typename))) {
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000354 return ParseTypeParameter(Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
357 if (Tok.is(tok::kw_template))
Chris Lattner532e19b2009-01-04 23:51:17 +0000358 return ParseTemplateTemplateParameter(Depth, Position);
359
360 // If it's none of the above, then it must be a parameter declaration.
361 // NOTE: This will pick up errors in the closure of the template parameter
362 // list (e.g., template < ; Check here to implement >> style closures.
363 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000364}
365
366/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
367/// Other kinds of template parameters are parsed in
368/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
369///
370/// type-parameter: [C++ temp.param]
Anders Carlssonce5635a2009-06-12 23:09:56 +0000371/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000372/// 'class' identifier[opt] '=' type-id
Anders Carlssonce5635a2009-06-12 23:09:56 +0000373/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoradcac882008-12-01 23:54:00 +0000374/// 'typename' identifier[opt] '=' type-id
Chris Lattnerb28317a2009-03-28 19:18:32 +0000375Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregor26236e82008-12-02 00:41:28 +0000376 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000377 "A type-parameter starts with 'class' or 'typename'");
Douglas Gregor26236e82008-12-02 00:41:28 +0000378
379 // Consume the 'class' or 'typename' keyword.
380 bool TypenameKeyword = Tok.is(tok::kw_typename);
381 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregoradcac882008-12-01 23:54:00 +0000382
Anders Carlsson941df7d2009-06-12 19:58:00 +0000383 // Grab the ellipsis (if given).
384 bool Ellipsis = false;
385 SourceLocation EllipsisLoc;
Anders Carlssonce5635a2009-06-12 23:09:56 +0000386 if (Tok.is(tok::ellipsis)) {
Anders Carlsson941df7d2009-06-12 19:58:00 +0000387 Ellipsis = true;
388 EllipsisLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000389
390 if (!getLang().CPlusPlus0x)
Anders Carlssonce5635a2009-06-12 23:09:56 +0000391 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson941df7d2009-06-12 19:58:00 +0000392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Douglas Gregoradcac882008-12-01 23:54:00 +0000394 // Grab the template parameter name (if given)
Douglas Gregor26236e82008-12-02 00:41:28 +0000395 SourceLocation NameLoc;
396 IdentifierInfo* ParamName = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000397 if (Tok.is(tok::identifier)) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000398 ParamName = Tok.getIdentifierInfo();
399 NameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000400 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
401 Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000402 // Unnamed template parameter. Don't have to do anything here, just
403 // don't consume this token.
404 } else {
405 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000406 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattnerb28317a2009-03-28 19:18:32 +0000409 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson941df7d2009-06-12 19:58:00 +0000410 Ellipsis, EllipsisLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000411 KeyLoc, ParamName, NameLoc,
412 Depth, Position);
Douglas Gregor26236e82008-12-02 00:41:28 +0000413
Douglas Gregoradcac882008-12-01 23:54:00 +0000414 // Grab a default type id (if given).
Mike Stump1eb44332009-09-09 15:08:12 +0000415 if (Tok.is(tok::equal)) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000416 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregord684b002009-02-10 19:49:53 +0000417 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +0000418 TypeResult DefaultType = ParseTypeName();
419 if (!DefaultType.isInvalid())
Douglas Gregord684b002009-02-10 19:49:53 +0000420 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +0000421 DefaultType.get());
Douglas Gregoradcac882008-12-01 23:54:00 +0000422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Douglas Gregor26236e82008-12-02 00:41:28 +0000424 return TypeParam;
Douglas Gregoradcac882008-12-01 23:54:00 +0000425}
426
427/// ParseTemplateTemplateParameter - Handle the parsing of template
Mike Stump1eb44332009-09-09 15:08:12 +0000428/// template parameters.
Douglas Gregoradcac882008-12-01 23:54:00 +0000429///
430/// type-parameter: [C++ temp.param]
431/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
432/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattnerb28317a2009-03-28 19:18:32 +0000433Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000434Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000435 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
436
437 // Handle the template <...> part.
438 SourceLocation TemplateLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000439 TemplateParameterList TemplateParams;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000440 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor68c69932009-02-10 19:52:54 +0000441 {
442 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000443 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000444 RAngleLoc)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000445 return DeclPtrTy();
Douglas Gregor68c69932009-02-10 19:52:54 +0000446 }
Douglas Gregoradcac882008-12-01 23:54:00 +0000447 }
448
449 // Generate a meaningful error if the user forgot to put class before the
450 // identifier, comma, or greater.
Mike Stump1eb44332009-09-09 15:08:12 +0000451 if (!Tok.is(tok::kw_class)) {
452 Diag(Tok.getLocation(), diag::err_expected_class_before)
Douglas Gregoradcac882008-12-01 23:54:00 +0000453 << PP.getSpelling(Tok);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000454 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000455 }
456 SourceLocation ClassLoc = ConsumeToken();
457
458 // Get the identifier, if given.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000459 SourceLocation NameLoc;
460 IdentifierInfo* ParamName = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000461 if (Tok.is(tok::identifier)) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000462 ParamName = Tok.getIdentifierInfo();
463 NameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000464 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000465 // Unnamed template parameter. Don't have to do anything here, just
466 // don't consume this token.
467 } else {
468 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000469 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000470 }
471
Mike Stump1eb44332009-09-09 15:08:12 +0000472 TemplateParamsTy *ParamList =
Douglas Gregorddc29e12009-02-06 22:42:48 +0000473 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
474 TemplateLoc, LAngleLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000475 &TemplateParams[0],
Douglas Gregorddc29e12009-02-06 22:42:48 +0000476 TemplateParams.size(),
477 RAngleLoc);
478
Chris Lattnerb28317a2009-03-28 19:18:32 +0000479 Parser::DeclPtrTy Param
Douglas Gregord684b002009-02-10 19:49:53 +0000480 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
481 ParamList, ParamName,
482 NameLoc, Depth, Position);
483
484 // Get the a default value, if given.
485 if (Tok.is(tok::equal)) {
486 SourceLocation EqualLoc = ConsumeToken();
487 OwningExprResult DefaultExpr = ParseCXXIdExpression();
488 if (DefaultExpr.isInvalid())
489 return Param;
490 else if (Param)
491 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc,
492 move(DefaultExpr));
493 }
494
495 return Param;
Douglas Gregoradcac882008-12-01 23:54:00 +0000496}
497
498/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
Mike Stump1eb44332009-09-09 15:08:12 +0000499/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000500///
Douglas Gregoradcac882008-12-01 23:54:00 +0000501/// template-parameter:
502/// ...
503/// parameter-declaration
504///
505/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
506/// but that didn't work out to well. Instead, this tries to recrate the basic
507/// parsing of parameter declarations, but tries to constrain it for template
508/// parameters.
Douglas Gregor26236e82008-12-02 00:41:28 +0000509/// FIXME: We need to make a ParseParameterDeclaration that works for
510/// non-type template parameters and normal function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +0000511Parser::DeclPtrTy
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000512Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor26236e82008-12-02 00:41:28 +0000513 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregoradcac882008-12-01 23:54:00 +0000514
515 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregor26236e82008-12-02 00:41:28 +0000516 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregoradcac882008-12-01 23:54:00 +0000517 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregor26236e82008-12-02 00:41:28 +0000518 DeclSpec DS;
519 ParseDeclarationSpecifiers(DS);
Douglas Gregoradcac882008-12-01 23:54:00 +0000520
521 // Parse this as a typename.
Douglas Gregor26236e82008-12-02 00:41:28 +0000522 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
523 ParseDeclarator(ParamDecl);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000524 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000525 // This probably shouldn't happen - and it's more of a Sema thing, but
526 // basically we didn't parse the type name because we couldn't associate
527 // it with an AST node. we should just skip to the comma or greater.
528 // TODO: This is currently a placeholder for some kind of Sema Error.
529 Diag(Tok.getLocation(), diag::err_parse_error);
530 SkipUntil(tok::comma, tok::greater, true, true);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000531 return DeclPtrTy();
Douglas Gregoradcac882008-12-01 23:54:00 +0000532 }
533
Mike Stump1eb44332009-09-09 15:08:12 +0000534 // Create the parameter.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000535 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
536 Depth, Position);
Douglas Gregoradcac882008-12-01 23:54:00 +0000537
Douglas Gregord684b002009-02-10 19:49:53 +0000538 // If there is a default value, parse it.
Chris Lattner7452c6f2009-01-05 01:24:05 +0000539 if (Tok.is(tok::equal)) {
Douglas Gregord684b002009-02-10 19:49:53 +0000540 SourceLocation EqualLoc = ConsumeToken();
541
542 // C++ [temp.param]p15:
543 // When parsing a default template-argument for a non-type
544 // template-parameter, the first non-nested > is taken as the
545 // end of the template-parameter-list rather than a greater-than
546 // operator.
Mike Stump1eb44332009-09-09 15:08:12 +0000547 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000548
549 OwningExprResult DefaultArg = ParseAssignmentExpression();
550 if (DefaultArg.isInvalid())
551 SkipUntil(tok::comma, tok::greater, true, true);
552 else if (Param)
Mike Stump1eb44332009-09-09 15:08:12 +0000553 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
Douglas Gregord684b002009-02-10 19:49:53 +0000554 move(DefaultArg));
Douglas Gregoradcac882008-12-01 23:54:00 +0000555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor26236e82008-12-02 00:41:28 +0000557 return Param;
Douglas Gregoradcac882008-12-01 23:54:00 +0000558}
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000559
Douglas Gregorcc636682009-02-17 23:15:12 +0000560/// \brief Parses a template-id that after the template name has
561/// already been parsed.
562///
563/// This routine takes care of parsing the enclosed template argument
564/// list ('<' template-parameter-list [opt] '>') and placing the
565/// results into a form that can be transferred to semantic analysis.
566///
567/// \param Template the template declaration produced by isTemplateName
568///
569/// \param TemplateNameLoc the source location of the template name
570///
571/// \param SS if non-NULL, the nested-name-specifier preceding the
572/// template name.
573///
574/// \param ConsumeLastToken if true, then we will consume the last
575/// token that forms the template-id. Otherwise, we will leave the
576/// last token in the stream (e.g., so that it can be replaced with an
577/// annotation token).
Mike Stump1eb44332009-09-09 15:08:12 +0000578bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000579Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000580 SourceLocation TemplateNameLoc,
Douglas Gregorcc636682009-02-17 23:15:12 +0000581 const CXXScopeSpec *SS,
582 bool ConsumeLastToken,
583 SourceLocation &LAngleLoc,
584 TemplateArgList &TemplateArgs,
585 TemplateArgIsTypeList &TemplateArgIsType,
586 TemplateArgLocationList &TemplateArgLocations,
587 SourceLocation &RAngleLoc) {
588 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
589
590 // Consume the '<'.
591 LAngleLoc = ConsumeToken();
592
593 // Parse the optional template-argument-list.
594 bool Invalid = false;
595 {
596 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
597 if (Tok.isNot(tok::greater))
598 Invalid = ParseTemplateArgumentList(TemplateArgs, TemplateArgIsType,
599 TemplateArgLocations);
600
601 if (Invalid) {
602 // Try to find the closing '>'.
603 SkipUntil(tok::greater, true, !ConsumeLastToken);
604
605 return true;
606 }
607 }
608
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000609 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
Douglas Gregorcc636682009-02-17 23:15:12 +0000610 return true;
611
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000612 // Determine the location of the '>' or '>>'. Only consume this
613 // token if the caller asked us to.
Douglas Gregorcc636682009-02-17 23:15:12 +0000614 RAngleLoc = Tok.getLocation();
615
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000616 if (Tok.is(tok::greatergreater)) {
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000617 if (!getLang().CPlusPlus0x) {
618 const char *ReplaceStr = "> >";
619 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
620 ReplaceStr = "> > ";
621
622 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregorb2fb6de2009-02-27 17:53:17 +0000623 << CodeModificationHint::CreateReplacement(
624 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000625 }
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000626
627 Tok.setKind(tok::greater);
628 if (!ConsumeLastToken) {
629 // Since we're not supposed to consume the '>>' token, we need
630 // to insert a second '>' token after the first.
631 PP.EnterToken(Tok);
632 }
633 } else if (ConsumeLastToken)
Douglas Gregorcc636682009-02-17 23:15:12 +0000634 ConsumeToken();
635
636 return false;
637}
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Douglas Gregor39a8de12009-02-25 19:37:18 +0000639/// \brief Replace the tokens that form a simple-template-id with an
640/// annotation token containing the complete template-id.
641///
642/// The first token in the stream must be the name of a template that
643/// is followed by a '<'. This routine will parse the complete
644/// simple-template-id and replace the tokens with a single annotation
645/// token with one of two different kinds: if the template-id names a
646/// type (and \p AllowTypeAnnotation is true), the annotation token is
647/// a type annotation that includes the optional nested-name-specifier
648/// (\p SS). Otherwise, the annotation token is a template-id
649/// annotation that does not include the optional
650/// nested-name-specifier.
651///
652/// \param Template the declaration of the template named by the first
653/// token (an identifier), as returned from \c Action::isTemplateName().
654///
655/// \param TemplateNameKind the kind of template that \p Template
656/// refers to, as returned from \c Action::isTemplateName().
657///
658/// \param SS if non-NULL, the nested-name-specifier that precedes
659/// this template name.
660///
661/// \param TemplateKWLoc if valid, specifies that this template-id
662/// annotation was preceded by the 'template' keyword and gives the
663/// location of that keyword. If invalid (the default), then this
664/// template-id was not preceded by a 'template' keyword.
665///
666/// \param AllowTypeAnnotation if true (the default), then a
667/// simple-template-id that refers to a class template, template
668/// template parameter, or other template that produces a type will be
669/// replaced with a type annotation token. Otherwise, the
670/// simple-template-id is always replaced with a template-id
671/// annotation token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000672///
673/// If an unrecoverable parse error occurs and no annotation token can be
674/// formed, this function returns true.
675///
676bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Mike Stump1eb44332009-09-09 15:08:12 +0000677 const CXXScopeSpec *SS,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000678 UnqualifiedId &TemplateName,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000679 SourceLocation TemplateKWLoc,
680 bool AllowTypeAnnotation) {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000681 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000682 assert(Template && Tok.is(tok::less) &&
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000683 "Parser isn't at the beginning of a template-id");
684
685 // Consume the template-name.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000686 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000687
Douglas Gregorcc636682009-02-17 23:15:12 +0000688 // Parse the enclosed template argument list.
689 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000690 TemplateArgList TemplateArgs;
691 TemplateArgIsTypeList TemplateArgIsType;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000692 TemplateArgLocationList TemplateArgLocations;
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000693 bool Invalid = ParseTemplateIdAfterTemplateName(Template,
694 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000695 SS, false, LAngleLoc,
696 TemplateArgs,
Douglas Gregorcc636682009-02-17 23:15:12 +0000697 TemplateArgIsType,
698 TemplateArgLocations,
699 RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000701 if (Invalid) {
702 // If we failed to parse the template ID but skipped ahead to a >, we're not
703 // going to be able to form a token annotation. Eat the '>' if present.
704 if (Tok.is(tok::greater))
705 ConsumeToken();
706 return true;
707 }
Douglas Gregorc15cb382009-02-09 23:23:08 +0000708
Jay Foadbeaaccd2009-05-21 09:52:38 +0000709 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
710 TemplateArgIsType.data(),
Douglas Gregorcc636682009-02-17 23:15:12 +0000711 TemplateArgs.size());
Douglas Gregorf02da892009-02-09 21:04:56 +0000712
Douglas Gregor55f6b142009-02-09 18:46:07 +0000713 // Build the annotation token.
Douglas Gregorc45c2322009-03-31 00:43:58 +0000714 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Mike Stump1eb44332009-09-09 15:08:12 +0000715 Action::TypeResult Type
Douglas Gregor7532dc62009-03-30 22:58:21 +0000716 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
717 LAngleLoc, TemplateArgsPtr,
718 &TemplateArgLocations[0],
719 RAngleLoc);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000720 if (Type.isInvalid()) {
721 // If we failed to parse the template ID but skipped ahead to a >, we're not
722 // going to be able to form a token annotation. Eat the '>' if present.
723 if (Tok.is(tok::greater))
724 ConsumeToken();
725 return true;
726 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000727
728 Tok.setKind(tok::annot_typename);
729 Tok.setAnnotationValue(Type.get());
Douglas Gregor39a8de12009-02-25 19:37:18 +0000730 if (SS && SS->isNotEmpty())
731 Tok.setLocation(SS->getBeginLoc());
732 else if (TemplateKWLoc.isValid())
733 Tok.setLocation(TemplateKWLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000734 else
Douglas Gregor39a8de12009-02-25 19:37:18 +0000735 Tok.setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +0000736 } else {
Douglas Gregorc45c2322009-03-31 00:43:58 +0000737 // Build a template-id annotation token that can be processed
738 // later.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000739 Tok.setKind(tok::annot_template_id);
Mike Stump1eb44332009-09-09 15:08:12 +0000740 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000741 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor55f6b142009-02-09 18:46:07 +0000742 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000743 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
744 TemplateId->Name = TemplateName.Identifier;
745 TemplateId->Operator = OO_None;
746 } else {
747 TemplateId->Name = 0;
748 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
749 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000750 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000751 TemplateId->Kind = TNK;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000752 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000753 TemplateId->RAngleLoc = RAngleLoc;
754 void **Args = TemplateId->getTemplateArgs();
755 bool *ArgIsType = TemplateId->getTemplateArgIsType();
756 SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations();
757 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000758 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor39a8de12009-02-25 19:37:18 +0000759 ArgIsType[Arg] = TemplateArgIsType[Arg];
760 ArgLocs[Arg] = TemplateArgLocations[Arg];
761 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000762 Tok.setAnnotationValue(TemplateId);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000763 if (TemplateKWLoc.isValid())
764 Tok.setLocation(TemplateKWLoc);
765 else
766 Tok.setLocation(TemplateNameLoc);
767
768 TemplateArgsPtr.release();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000769 }
770
771 // Common fields for the annotation token
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000772 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000773
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000774 // In case the tokens were cached, have Preprocessor replace them with the
775 // annotation token.
776 PP.AnnotateCachedTokens(Tok);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000777 return false;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000778}
779
Douglas Gregor39a8de12009-02-25 19:37:18 +0000780/// \brief Replaces a template-id annotation token with a type
781/// annotation token.
782///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000783/// If there was a failure when forming the type from the template-id,
784/// a type annotation token will still be created, but will have a
785/// NULL type pointer to signify an error.
786void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000787 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
788
Mike Stump1eb44332009-09-09 15:08:12 +0000789 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000790 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000791 assert((TemplateId->Kind == TNK_Type_template ||
792 TemplateId->Kind == TNK_Dependent_template_name) &&
793 "Only works for type and dependent templates");
Mike Stump1eb44332009-09-09 15:08:12 +0000794
795 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000796 TemplateId->getTemplateArgs(),
797 TemplateId->getTemplateArgIsType(),
798 TemplateId->NumArgs);
799
Mike Stump1eb44332009-09-09 15:08:12 +0000800 Action::TypeResult Type
Douglas Gregor7532dc62009-03-30 22:58:21 +0000801 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
802 TemplateId->TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000803 TemplateId->LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000804 TemplateArgsPtr,
805 TemplateId->getTemplateArgLocations(),
806 TemplateId->RAngleLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000807 // Create the new "type" annotation token.
808 Tok.setKind(tok::annot_typename);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000809 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor39a8de12009-02-25 19:37:18 +0000810 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
811 Tok.setLocation(SS->getBeginLoc());
812
813 // We might be backtracking, in which case we need to replace the
814 // template-id annotation token with the type annotation within the
815 // set of cached tokens. That way, we won't try to form the same
816 // class template specialization again.
817 PP.ReplaceLastTokenWithAnnotation(Tok);
818 TemplateId->Destroy();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000819}
820
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000821/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
822///
823/// template-argument: [C++ 14.2]
Douglas Gregorac7610d2009-06-22 20:57:11 +0000824/// constant-expression
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000825/// type-id
826/// id-expression
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000827void *Parser::ParseTemplateArgument(bool &ArgIsType) {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000828 // C++ [temp.arg]p2:
829 // In a template-argument, an ambiguity between a type-id and an
830 // expression is resolved to a type-id, regardless of the form of
831 // the corresponding template-parameter.
832 //
833 // Therefore, we initially try to parse a type-id.
Douglas Gregor8b642592009-02-10 00:53:15 +0000834 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000835 ArgIsType = true;
Douglas Gregor809070a2009-02-18 17:45:20 +0000836 TypeResult TypeArg = ParseTypeName();
837 if (TypeArg.isInvalid())
838 return 0;
839 return TypeArg.get();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000840 }
841
Douglas Gregorac7610d2009-06-22 20:57:11 +0000842 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregorc15cb382009-02-09 23:23:08 +0000843 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000844 return 0;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000845
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000846 ArgIsType = false;
847 return ExprArg.release();
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000848}
849
850/// ParseTemplateArgumentList - Parse a C++ template-argument-list
851/// (C++ [temp.names]). Returns true if there was an error.
852///
853/// template-argument-list: [C++ 14.2]
854/// template-argument
855/// template-argument-list ',' template-argument
Mike Stump1eb44332009-09-09 15:08:12 +0000856bool
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000857Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs,
Douglas Gregorc15cb382009-02-09 23:23:08 +0000858 TemplateArgIsTypeList &TemplateArgIsType,
859 TemplateArgLocationList &TemplateArgLocations) {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000860 while (true) {
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000861 bool IsType = false;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000862 SourceLocation Loc = Tok.getLocation();
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000863 void *Arg = ParseTemplateArgument(IsType);
864 if (Arg) {
865 TemplateArgs.push_back(Arg);
866 TemplateArgIsType.push_back(IsType);
Douglas Gregorc15cb382009-02-09 23:23:08 +0000867 TemplateArgLocations.push_back(Loc);
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000868 } else {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000869 SkipUntil(tok::comma, tok::greater, true, true);
870 return true;
871 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000872
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000873 // If the next token is a comma, consume it and keep reading
874 // arguments.
875 if (Tok.isNot(tok::comma)) break;
876
877 // Consume the comma.
878 ConsumeToken();
879 }
880
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000881 return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater);
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000882}
883
Mike Stump1eb44332009-09-09 15:08:12 +0000884/// \brief Parse a C++ explicit template instantiation
Douglas Gregor1426e532009-05-12 21:31:51 +0000885/// (C++ [temp.explicit]).
886///
887/// explicit-instantiation:
Douglas Gregor45f96552009-09-04 06:33:52 +0000888/// 'extern' [opt] 'template' declaration
889///
890/// Note that the 'extern' is a GNU extension and C++0x feature.
Mike Stump1eb44332009-09-09 15:08:12 +0000891Parser::DeclPtrTy
Douglas Gregor45f96552009-09-04 06:33:52 +0000892Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
893 SourceLocation TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000894 SourceLocation &DeclEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000895 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
Douglas Gregor45f96552009-09-04 06:33:52 +0000896 ParsedTemplateInfo(ExternLoc,
897 TemplateLoc),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000898 DeclEnd, AS_none);
Douglas Gregor1426e532009-05-12 21:31:51 +0000899}