blob: a647720fb955e06011d2b0e3c92b779dfc958492 [file] [log] [blame]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001//===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements parsing of C++ templates.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregoreb31f392008-12-01 23:54:00 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
Douglas Gregora3dff8e2009-08-24 23:03:25 +000018#include "llvm/Support/Compiler.h"
Douglas Gregoreb31f392008-12-01 23:54:00 +000019using namespace clang;
20
Douglas Gregor1b57ff32009-05-12 23:25:50 +000021/// \brief Parse a template declaration, explicit instantiation, or
22/// explicit specialization.
23Parser::DeclPtrTy
24Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
25 SourceLocation &DeclEnd,
26 AccessSpecifier AS) {
27 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less))
Mike Stump11289f42009-09-09 15:08:12 +000028 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
Douglas Gregor43e75172009-09-04 06:33:52 +000029 DeclEnd);
Douglas Gregor1b57ff32009-05-12 23:25:50 +000030
31 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
32}
33
Douglas Gregora3dff8e2009-08-24 23:03:25 +000034/// \brief RAII class that manages the template parameter depth.
35namespace {
36 class VISIBILITY_HIDDEN TemplateParameterDepthCounter {
37 unsigned &Depth;
38 unsigned AddedLevels;
39
40 public:
Mike Stump11289f42009-09-09 15:08:12 +000041 explicit TemplateParameterDepthCounter(unsigned &Depth)
Douglas Gregora3dff8e2009-08-24 23:03:25 +000042 : Depth(Depth), AddedLevels(0) { }
Mike Stump11289f42009-09-09 15:08:12 +000043
Douglas Gregora3dff8e2009-08-24 23:03:25 +000044 ~TemplateParameterDepthCounter() {
45 Depth -= AddedLevels;
46 }
Mike Stump11289f42009-09-09 15:08:12 +000047
48 void operator++() {
Douglas Gregora3dff8e2009-08-24 23:03:25 +000049 ++Depth;
50 ++AddedLevels;
51 }
Mike Stump11289f42009-09-09 15:08:12 +000052
Douglas Gregora3dff8e2009-08-24 23:03:25 +000053 operator unsigned() const { return Depth; }
54 };
55}
56
Douglas Gregor67a65642009-02-17 23:15:12 +000057/// \brief Parse a template declaration or an explicit specialization.
58///
59/// Template declarations include one or more template parameter lists
60/// and either the function or class template declaration. Explicit
61/// specializations contain one or more 'template < >' prefixes
62/// followed by a (possibly templated) declaration. Since the
63/// syntactic form of both features is nearly identical, we parse all
64/// of the template headers together and let semantic analysis sort
65/// the declarations from the explicit specializations.
Douglas Gregoreb31f392008-12-01 23:54:00 +000066///
67/// template-declaration: [C++ temp]
68/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregor67a65642009-02-17 23:15:12 +000069///
70/// explicit-specialization: [ C++ temp.expl.spec]
71/// 'template' '<' '>' declaration
Chris Lattner83f095c2009-03-28 19:18:32 +000072Parser::DeclPtrTy
Anders Carlssondfbbdf62009-03-26 00:52:18 +000073Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +000074 SourceLocation &DeclEnd,
Anders Carlssondfbbdf62009-03-26 00:52:18 +000075 AccessSpecifier AS) {
Mike Stump11289f42009-09-09 15:08:12 +000076 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
77 "Token does not start a template declaration.");
78
Douglas Gregorf5586182008-12-02 00:41:28 +000079 // Enter template-parameter scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +000080 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregorf5586182008-12-02 00:41:28 +000081
Douglas Gregorb9bd8a92008-12-24 02:52:09 +000082 // Parse multiple levels of template headers within this template
83 // parameter scope, e.g.,
84 //
85 // template<typename T>
86 // template<typename U>
87 // class A<T>::B { ... };
88 //
89 // We parse multiple levels non-recursively so that we can build a
90 // single data structure containing all of the template parameter
Douglas Gregor67a65642009-02-17 23:15:12 +000091 // lists to easily differentiate between the case above and:
Douglas Gregorb9bd8a92008-12-24 02:52:09 +000092 //
93 // template<typename T>
94 // class A {
95 // template<typename U> class B;
96 // };
97 //
98 // In the first case, the action for declaring A<T>::B receives
99 // both template parameter lists. In the second case, the action for
100 // defining A<T>::B receives just the inner template parameter list
101 // (and retrieves the outer template parameter list from its
102 // context).
Douglas Gregor468535e2009-08-20 18:46:05 +0000103 bool isSpecialization = true;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000104 bool LastParamListWasEmpty = false;
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000105 TemplateParameterLists ParamLists;
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000106 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000107 do {
108 // Consume the 'export', if any.
109 SourceLocation ExportLoc;
110 if (Tok.is(tok::kw_export)) {
111 ExportLoc = ConsumeToken();
112 }
113
114 // Consume the 'template', which should be here.
115 SourceLocation TemplateLoc;
116 if (Tok.is(tok::kw_template)) {
117 TemplateLoc = ConsumeToken();
118 } else {
119 Diag(Tok.getLocation(), diag::err_expected_template);
Chris Lattner83f095c2009-03-28 19:18:32 +0000120 return DeclPtrTy();
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000121 }
Mike Stump11289f42009-09-09 15:08:12 +0000122
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000123 // Parse the '<' template-parameter-list '>'
124 SourceLocation LAngleLoc, RAngleLoc;
125 TemplateParameterList TemplateParams;
Mike Stump11289f42009-09-09 15:08:12 +0000126 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000127 RAngleLoc)) {
128 // Skip until the semi-colon or a }.
129 SkipUntil(tok::r_brace, true, true);
130 if (Tok.is(tok::semi))
131 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000132 return DeclPtrTy();
Douglas Gregore93e46c2009-07-22 23:48:44 +0000133 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000134
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000135 ParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000136 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
137 TemplateLoc, LAngleLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000138 TemplateParams.data(),
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000139 TemplateParams.size(), RAngleLoc));
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000140
141 if (!TemplateParams.empty()) {
142 isSpecialization = false;
143 ++Depth;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000144 } else {
145 LastParamListWasEmpty = true;
Mike Stump11289f42009-09-09 15:08:12 +0000146 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000147 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
148
149 // Parse the actual template declaration.
Mike Stump11289f42009-09-09 15:08:12 +0000150 return ParseSingleDeclarationAfterTemplate(Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000151 ParsedTemplateInfo(&ParamLists,
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000152 isSpecialization,
153 LastParamListWasEmpty),
Douglas Gregor23996282009-05-12 21:31:51 +0000154 DeclEnd, AS);
155}
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000156
Douglas Gregor23996282009-05-12 21:31:51 +0000157/// \brief Parse a single declaration that declares a template,
158/// template specialization, or explicit instantiation of a template.
159///
160/// \param TemplateParams if non-NULL, the template parameter lists
161/// that preceded this declaration. In this case, the declaration is a
162/// template declaration, out-of-line definition of a template, or an
163/// explicit template specialization. When NULL, the declaration is an
164/// explicit template instantiation.
165///
166/// \param TemplateLoc when TemplateParams is NULL, the location of
167/// the 'template' keyword that indicates that we have an explicit
168/// template instantiation.
169///
170/// \param DeclEnd will receive the source location of the last token
171/// within this declaration.
172///
173/// \param AS the access specifier associated with this
174/// declaration. Will be AS_none for namespace-scope declarations.
175///
176/// \returns the new declaration.
Mike Stump11289f42009-09-09 15:08:12 +0000177Parser::DeclPtrTy
Douglas Gregor23996282009-05-12 21:31:51 +0000178Parser::ParseSingleDeclarationAfterTemplate(
179 unsigned Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000180 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor23996282009-05-12 21:31:51 +0000181 SourceLocation &DeclEnd,
182 AccessSpecifier AS) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000183 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
184 "Template information required");
185
Douglas Gregor3447e762009-08-20 22:52:58 +0000186 if (Context == Declarator::MemberContext) {
187 // We are parsing a member template.
188 ParseCXXClassMemberDeclaration(AS, TemplateInfo);
189 return DeclPtrTy::make((void*)0);
190 }
Mike Stump11289f42009-09-09 15:08:12 +0000191
Douglas Gregor23996282009-05-12 21:31:51 +0000192 // Parse the declaration specifiers.
193 DeclSpec DS;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000194 ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
Douglas Gregor23996282009-05-12 21:31:51 +0000195
196 if (Tok.is(tok::semi)) {
197 DeclEnd = ConsumeToken();
198 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
199 }
200
201 // Parse the declarator.
202 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
203 ParseDeclarator(DeclaratorInfo);
204 // Error parsing the declarator?
205 if (!DeclaratorInfo.hasName()) {
206 // If so, skip until the semi-colon or a }.
207 SkipUntil(tok::r_brace, true, true);
208 if (Tok.is(tok::semi))
209 ConsumeToken();
210 return DeclPtrTy();
211 }
Mike Stump11289f42009-09-09 15:08:12 +0000212
Douglas Gregor23996282009-05-12 21:31:51 +0000213 // If we have a declaration or declarator list, handle it.
214 if (isDeclarationAfterDeclarator()) {
215 // Parse this declaration.
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000216 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
217 TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000218
219 if (Tok.is(tok::comma)) {
220 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000221 << (int)TemplateInfo.Kind;
Douglas Gregor23996282009-05-12 21:31:51 +0000222 SkipUntil(tok::semi, true, false);
223 return ThisDecl;
224 }
225
226 // Eat the semi colon after the declaration.
John McCallef50e992009-07-31 02:20:35 +0000227 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
Douglas Gregor23996282009-05-12 21:31:51 +0000228 return ThisDecl;
229 }
230
231 if (DeclaratorInfo.isFunctionDeclarator() &&
232 isStartOfFunctionDefinition()) {
233 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
234 Diag(Tok, diag::err_function_declared_typedef);
235
236 if (Tok.is(tok::l_brace)) {
237 // This recovery skips the entire function body. It would be nice
238 // to simply call ParseFunctionDefinition() below, however Sema
239 // assumes the declarator represents a function, not a typedef.
240 ConsumeBrace();
241 SkipUntil(tok::r_brace, true);
242 } else {
243 SkipUntil(tok::semi);
244 }
245 return DeclPtrTy();
246 }
Douglas Gregor17a7c122009-06-24 00:54:41 +0000247 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000248 }
249
250 if (DeclaratorInfo.isFunctionDeclarator())
251 Diag(Tok, diag::err_expected_fn_body);
252 else
253 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
254 SkipUntil(tok::semi);
255 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000256}
257
258/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000259/// angle brackets. Depth is the depth of this template-parameter-list, which
260/// is the number of template headers directly enclosing this template header.
261/// TemplateParams is the current list of template parameters we're building.
262/// The template parameter we parse will be added to this list. LAngleLoc and
Mike Stump11289f42009-09-09 15:08:12 +0000263/// RAngleLoc will receive the positions of the '<' and '>', respectively,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000264/// that enclose this template parameter list.
Douglas Gregore93e46c2009-07-22 23:48:44 +0000265///
266/// \returns true if an error occurred, false otherwise.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000267bool Parser::ParseTemplateParameters(unsigned Depth,
268 TemplateParameterList &TemplateParams,
269 SourceLocation &LAngleLoc,
270 SourceLocation &RAngleLoc) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000271 // Get the template parameter list.
Mike Stump11289f42009-09-09 15:08:12 +0000272 if (!Tok.is(tok::less)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000273 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregore93e46c2009-07-22 23:48:44 +0000274 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000275 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000276 LAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000277
Douglas Gregoreb31f392008-12-01 23:54:00 +0000278 // Try to parse the template parameter list.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000279 if (Tok.is(tok::greater))
280 RAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000281 else if (ParseTemplateParameterList(Depth, TemplateParams)) {
282 if (!Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000283 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregore93e46c2009-07-22 23:48:44 +0000284 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000285 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000286 RAngleLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000287 }
Douglas Gregore93e46c2009-07-22 23:48:44 +0000288 return false;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000289}
290
291/// ParseTemplateParameterList - Parse a template parameter list. If
292/// the parsing fails badly (i.e., closing bracket was left out), this
293/// will try to put the token stream in a reasonable position (closing
Mike Stump11289f42009-09-09 15:08:12 +0000294/// a statement, etc.) and return false.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000295///
296/// template-parameter-list: [C++ temp]
297/// template-parameter
298/// template-parameter-list ',' template-parameter
Mike Stump11289f42009-09-09 15:08:12 +0000299bool
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000300Parser::ParseTemplateParameterList(unsigned Depth,
301 TemplateParameterList &TemplateParams) {
Mike Stump11289f42009-09-09 15:08:12 +0000302 while (1) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000303 if (DeclPtrTy TmpParam
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000304 = ParseTemplateParameter(Depth, TemplateParams.size())) {
305 TemplateParams.push_back(TmpParam);
306 } else {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000307 // If we failed to parse a template parameter, skip until we find
308 // a comma or closing brace.
309 SkipUntil(tok::comma, tok::greater, true, true);
310 }
Mike Stump11289f42009-09-09 15:08:12 +0000311
Douglas Gregoreb31f392008-12-01 23:54:00 +0000312 // Did we find a comma or the end of the template parmeter list?
Mike Stump11289f42009-09-09 15:08:12 +0000313 if (Tok.is(tok::comma)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000314 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000315 } else if (Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000316 // Don't consume this... that's done by template parser.
317 break;
318 } else {
319 // Somebody probably forgot to close the template. Skip ahead and
320 // try to get out of the expression. This error is currently
321 // subsumed by whatever goes on in ParseTemplateParameter.
322 // TODO: This could match >>, and it would be nice to avoid those
323 // silly errors with template <vec<T>>.
324 // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
325 SkipUntil(tok::greater, true, true);
326 return false;
327 }
328 }
329 return true;
330}
331
332/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
333///
334/// template-parameter: [C++ temp.param]
335/// type-parameter
336/// parameter-declaration
337///
338/// type-parameter: (see below)
Anders Carlssonf986ba72009-06-12 23:09:56 +0000339/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000340/// 'class' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000341/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000342/// 'typename' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000343/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000344/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Mike Stump11289f42009-09-09 15:08:12 +0000345Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000346Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Mike Stump11289f42009-09-09 15:08:12 +0000347 if (Tok.is(tok::kw_class) ||
348 (Tok.is(tok::kw_typename) &&
349 // FIXME: Next token has not been annotated!
350 NextToken().isNot(tok::annot_typename))) {
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000351 return ParseTypeParameter(Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000352 }
Mike Stump11289f42009-09-09 15:08:12 +0000353
354 if (Tok.is(tok::kw_template))
Chris Lattnera21db612009-01-04 23:51:17 +0000355 return ParseTemplateTemplateParameter(Depth, Position);
356
357 // If it's none of the above, then it must be a parameter declaration.
358 // NOTE: This will pick up errors in the closure of the template parameter
359 // list (e.g., template < ; Check here to implement >> style closures.
360 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000361}
362
363/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
364/// Other kinds of template parameters are parsed in
365/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
366///
367/// type-parameter: [C++ temp.param]
Anders Carlssonf986ba72009-06-12 23:09:56 +0000368/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000369/// 'class' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000370/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000371/// 'typename' identifier[opt] '=' type-id
Chris Lattner83f095c2009-03-28 19:18:32 +0000372Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
Douglas Gregorf5586182008-12-02 00:41:28 +0000373 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
Mike Stump11289f42009-09-09 15:08:12 +0000374 "A type-parameter starts with 'class' or 'typename'");
Douglas Gregorf5586182008-12-02 00:41:28 +0000375
376 // Consume the 'class' or 'typename' keyword.
377 bool TypenameKeyword = Tok.is(tok::kw_typename);
378 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000379
Anders Carlsson01e9e932009-06-12 19:58:00 +0000380 // Grab the ellipsis (if given).
381 bool Ellipsis = false;
382 SourceLocation EllipsisLoc;
Anders Carlssonf986ba72009-06-12 23:09:56 +0000383 if (Tok.is(tok::ellipsis)) {
Anders Carlsson01e9e932009-06-12 19:58:00 +0000384 Ellipsis = true;
385 EllipsisLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000386
387 if (!getLang().CPlusPlus0x)
Anders Carlssonf986ba72009-06-12 23:09:56 +0000388 Diag(EllipsisLoc, diag::err_variadic_templates);
Anders Carlsson01e9e932009-06-12 19:58:00 +0000389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
Douglas Gregoreb31f392008-12-01 23:54:00 +0000391 // Grab the template parameter name (if given)
Douglas Gregorf5586182008-12-02 00:41:28 +0000392 SourceLocation NameLoc;
393 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000394 if (Tok.is(tok::identifier)) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000395 ParamName = Tok.getIdentifierInfo();
396 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000397 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
398 Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000399 // Unnamed template parameter. Don't have to do anything here, just
400 // don't consume this token.
401 } else {
402 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +0000403 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattner83f095c2009-03-28 19:18:32 +0000406 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
Anders Carlsson01e9e932009-06-12 19:58:00 +0000407 Ellipsis, EllipsisLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000408 KeyLoc, ParamName, NameLoc,
409 Depth, Position);
Douglas Gregorf5586182008-12-02 00:41:28 +0000410
Douglas Gregoreb31f392008-12-01 23:54:00 +0000411 // Grab a default type id (if given).
Mike Stump11289f42009-09-09 15:08:12 +0000412 if (Tok.is(tok::equal)) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000413 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregordba32632009-02-10 19:49:53 +0000414 SourceLocation DefaultLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +0000415 TypeResult DefaultType = ParseTypeName();
416 if (!DefaultType.isInvalid())
Douglas Gregordba32632009-02-10 19:49:53 +0000417 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +0000418 DefaultType.get());
Douglas Gregoreb31f392008-12-01 23:54:00 +0000419 }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Douglas Gregorf5586182008-12-02 00:41:28 +0000421 return TypeParam;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000422}
423
424/// ParseTemplateTemplateParameter - Handle the parsing of template
Mike Stump11289f42009-09-09 15:08:12 +0000425/// template parameters.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000426///
427/// type-parameter: [C++ temp.param]
428/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
429/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
Chris Lattner83f095c2009-03-28 19:18:32 +0000430Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000431Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000432 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
433
434 // Handle the template <...> part.
435 SourceLocation TemplateLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000436 TemplateParameterList TemplateParams;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000437 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000438 {
439 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Mike Stump11289f42009-09-09 15:08:12 +0000440 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000441 RAngleLoc)) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000442 return DeclPtrTy();
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000443 }
Douglas Gregoreb31f392008-12-01 23:54:00 +0000444 }
445
446 // Generate a meaningful error if the user forgot to put class before the
447 // identifier, comma, or greater.
Mike Stump11289f42009-09-09 15:08:12 +0000448 if (!Tok.is(tok::kw_class)) {
449 Diag(Tok.getLocation(), diag::err_expected_class_before)
Douglas Gregoreb31f392008-12-01 23:54:00 +0000450 << PP.getSpelling(Tok);
Chris Lattner83f095c2009-03-28 19:18:32 +0000451 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000452 }
453 SourceLocation ClassLoc = ConsumeToken();
454
455 // Get the identifier, if given.
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000456 SourceLocation NameLoc;
457 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000458 if (Tok.is(tok::identifier)) {
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000459 ParamName = Tok.getIdentifierInfo();
460 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000461 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000462 // Unnamed template parameter. Don't have to do anything here, just
463 // don't consume this token.
464 } else {
465 Diag(Tok.getLocation(), diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +0000466 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000467 }
468
Mike Stump11289f42009-09-09 15:08:12 +0000469 TemplateParamsTy *ParamList =
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000470 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
471 TemplateLoc, LAngleLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000472 &TemplateParams[0],
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000473 TemplateParams.size(),
474 RAngleLoc);
475
Chris Lattner83f095c2009-03-28 19:18:32 +0000476 Parser::DeclPtrTy Param
Douglas Gregordba32632009-02-10 19:49:53 +0000477 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
478 ParamList, ParamName,
479 NameLoc, Depth, Position);
480
481 // Get the a default value, if given.
482 if (Tok.is(tok::equal)) {
483 SourceLocation EqualLoc = ConsumeToken();
484 OwningExprResult DefaultExpr = ParseCXXIdExpression();
485 if (DefaultExpr.isInvalid())
486 return Param;
487 else if (Param)
488 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc,
489 move(DefaultExpr));
490 }
491
492 return Param;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000493}
494
495/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
Mike Stump11289f42009-09-09 15:08:12 +0000496/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000497///
Douglas Gregoreb31f392008-12-01 23:54:00 +0000498/// template-parameter:
499/// ...
500/// parameter-declaration
501///
502/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
503/// but that didn't work out to well. Instead, this tries to recrate the basic
504/// parsing of parameter declarations, but tries to constrain it for template
505/// parameters.
Douglas Gregorf5586182008-12-02 00:41:28 +0000506/// FIXME: We need to make a ParseParameterDeclaration that works for
507/// non-type template parameters and normal function parameters.
Mike Stump11289f42009-09-09 15:08:12 +0000508Parser::DeclPtrTy
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000509Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000510 SourceLocation StartLoc = Tok.getLocation();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000511
512 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregorf5586182008-12-02 00:41:28 +0000513 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregoreb31f392008-12-01 23:54:00 +0000514 // declarators (parts of declarators?) are accepted for parameters.
Douglas Gregorf5586182008-12-02 00:41:28 +0000515 DeclSpec DS;
516 ParseDeclarationSpecifiers(DS);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000517
518 // Parse this as a typename.
Douglas Gregorf5586182008-12-02 00:41:28 +0000519 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
520 ParseDeclarator(ParamDecl);
Chris Lattnerb5134c02009-01-05 01:24:05 +0000521 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000522 // This probably shouldn't happen - and it's more of a Sema thing, but
523 // basically we didn't parse the type name because we couldn't associate
524 // it with an AST node. we should just skip to the comma or greater.
525 // TODO: This is currently a placeholder for some kind of Sema Error.
526 Diag(Tok.getLocation(), diag::err_parse_error);
527 SkipUntil(tok::comma, tok::greater, true, true);
Chris Lattner83f095c2009-03-28 19:18:32 +0000528 return DeclPtrTy();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000529 }
530
Mike Stump11289f42009-09-09 15:08:12 +0000531 // Create the parameter.
Chris Lattner83f095c2009-03-28 19:18:32 +0000532 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
533 Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000534
Douglas Gregordba32632009-02-10 19:49:53 +0000535 // If there is a default value, parse it.
Chris Lattnerb5134c02009-01-05 01:24:05 +0000536 if (Tok.is(tok::equal)) {
Douglas Gregordba32632009-02-10 19:49:53 +0000537 SourceLocation EqualLoc = ConsumeToken();
538
539 // C++ [temp.param]p15:
540 // When parsing a default template-argument for a non-type
541 // template-parameter, the first non-nested > is taken as the
542 // end of the template-parameter-list rather than a greater-than
543 // operator.
Mike Stump11289f42009-09-09 15:08:12 +0000544 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Douglas Gregordba32632009-02-10 19:49:53 +0000545
546 OwningExprResult DefaultArg = ParseAssignmentExpression();
547 if (DefaultArg.isInvalid())
548 SkipUntil(tok::comma, tok::greater, true, true);
549 else if (Param)
Mike Stump11289f42009-09-09 15:08:12 +0000550 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
Douglas Gregordba32632009-02-10 19:49:53 +0000551 move(DefaultArg));
Douglas Gregoreb31f392008-12-01 23:54:00 +0000552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Douglas Gregorf5586182008-12-02 00:41:28 +0000554 return Param;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000555}
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000556
Douglas Gregor67a65642009-02-17 23:15:12 +0000557/// \brief Parses a template-id that after the template name has
558/// already been parsed.
559///
560/// This routine takes care of parsing the enclosed template argument
561/// list ('<' template-parameter-list [opt] '>') and placing the
562/// results into a form that can be transferred to semantic analysis.
563///
564/// \param Template the template declaration produced by isTemplateName
565///
566/// \param TemplateNameLoc the source location of the template name
567///
568/// \param SS if non-NULL, the nested-name-specifier preceding the
569/// template name.
570///
571/// \param ConsumeLastToken if true, then we will consume the last
572/// token that forms the template-id. Otherwise, we will leave the
573/// last token in the stream (e.g., so that it can be replaced with an
574/// annotation token).
Mike Stump11289f42009-09-09 15:08:12 +0000575bool
Douglas Gregordc572a32009-03-30 22:58:21 +0000576Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Mike Stump11289f42009-09-09 15:08:12 +0000577 SourceLocation TemplateNameLoc,
Douglas Gregor67a65642009-02-17 23:15:12 +0000578 const CXXScopeSpec *SS,
579 bool ConsumeLastToken,
580 SourceLocation &LAngleLoc,
581 TemplateArgList &TemplateArgs,
582 TemplateArgIsTypeList &TemplateArgIsType,
583 TemplateArgLocationList &TemplateArgLocations,
584 SourceLocation &RAngleLoc) {
585 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
586
587 // Consume the '<'.
588 LAngleLoc = ConsumeToken();
589
590 // Parse the optional template-argument-list.
591 bool Invalid = false;
592 {
593 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
594 if (Tok.isNot(tok::greater))
595 Invalid = ParseTemplateArgumentList(TemplateArgs, TemplateArgIsType,
596 TemplateArgLocations);
597
598 if (Invalid) {
599 // Try to find the closing '>'.
600 SkipUntil(tok::greater, true, !ConsumeLastToken);
601
602 return true;
603 }
604 }
605
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000606 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
Douglas Gregor67a65642009-02-17 23:15:12 +0000607 return true;
608
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000609 // Determine the location of the '>' or '>>'. Only consume this
610 // token if the caller asked us to.
Douglas Gregor67a65642009-02-17 23:15:12 +0000611 RAngleLoc = Tok.getLocation();
612
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000613 if (Tok.is(tok::greatergreater)) {
Douglas Gregor87f95b02009-02-26 21:00:50 +0000614 if (!getLang().CPlusPlus0x) {
615 const char *ReplaceStr = "> >";
616 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
617 ReplaceStr = "> > ";
618
619 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregor96977da2009-02-27 17:53:17 +0000620 << CodeModificationHint::CreateReplacement(
621 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000622 }
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000623
624 Tok.setKind(tok::greater);
625 if (!ConsumeLastToken) {
626 // Since we're not supposed to consume the '>>' token, we need
627 // to insert a second '>' token after the first.
628 PP.EnterToken(Tok);
629 }
630 } else if (ConsumeLastToken)
Douglas Gregor67a65642009-02-17 23:15:12 +0000631 ConsumeToken();
632
633 return false;
634}
Mike Stump11289f42009-09-09 15:08:12 +0000635
Douglas Gregor7f741122009-02-25 19:37:18 +0000636/// \brief Replace the tokens that form a simple-template-id with an
637/// annotation token containing the complete template-id.
638///
639/// The first token in the stream must be the name of a template that
640/// is followed by a '<'. This routine will parse the complete
641/// simple-template-id and replace the tokens with a single annotation
642/// token with one of two different kinds: if the template-id names a
643/// type (and \p AllowTypeAnnotation is true), the annotation token is
644/// a type annotation that includes the optional nested-name-specifier
645/// (\p SS). Otherwise, the annotation token is a template-id
646/// annotation that does not include the optional
647/// nested-name-specifier.
648///
649/// \param Template the declaration of the template named by the first
650/// token (an identifier), as returned from \c Action::isTemplateName().
651///
652/// \param TemplateNameKind the kind of template that \p Template
653/// refers to, as returned from \c Action::isTemplateName().
654///
655/// \param SS if non-NULL, the nested-name-specifier that precedes
656/// this template name.
657///
658/// \param TemplateKWLoc if valid, specifies that this template-id
659/// annotation was preceded by the 'template' keyword and gives the
660/// location of that keyword. If invalid (the default), then this
661/// template-id was not preceded by a 'template' keyword.
662///
663/// \param AllowTypeAnnotation if true (the default), then a
664/// simple-template-id that refers to a class template, template
665/// template parameter, or other template that produces a type will be
666/// replaced with a type annotation token. Otherwise, the
667/// simple-template-id is always replaced with a template-id
668/// annotation token.
Chris Lattner5558e9f2009-06-26 04:27:47 +0000669///
670/// If an unrecoverable parse error occurs and no annotation token can be
671/// formed, this function returns true.
672///
673bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Mike Stump11289f42009-09-09 15:08:12 +0000674 const CXXScopeSpec *SS,
Douglas Gregor71395fa2009-11-04 00:56:37 +0000675 UnqualifiedId &TemplateName,
Douglas Gregor7f741122009-02-25 19:37:18 +0000676 SourceLocation TemplateKWLoc,
677 bool AllowTypeAnnotation) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000678 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
Douglas Gregor71395fa2009-11-04 00:56:37 +0000679 assert(Template && Tok.is(tok::less) &&
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000680 "Parser isn't at the beginning of a template-id");
681
682 // Consume the template-name.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000683 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000684
Douglas Gregor67a65642009-02-17 23:15:12 +0000685 // Parse the enclosed template argument list.
686 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor67b556a2009-02-09 19:34:22 +0000687 TemplateArgList TemplateArgs;
688 TemplateArgIsTypeList TemplateArgIsType;
Douglas Gregord32e0282009-02-09 23:23:08 +0000689 TemplateArgLocationList TemplateArgLocations;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000690 bool Invalid = ParseTemplateIdAfterTemplateName(Template,
691 TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000692 SS, false, LAngleLoc,
693 TemplateArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000694 TemplateArgIsType,
695 TemplateArgLocations,
696 RAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000697
Chris Lattner5558e9f2009-06-26 04:27:47 +0000698 if (Invalid) {
699 // If we failed to parse the template ID but skipped ahead to a >, we're not
700 // going to be able to form a token annotation. Eat the '>' if present.
701 if (Tok.is(tok::greater))
702 ConsumeToken();
703 return true;
704 }
Douglas Gregord32e0282009-02-09 23:23:08 +0000705
Jay Foad7d0479f2009-05-21 09:52:38 +0000706 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
707 TemplateArgIsType.data(),
Douglas Gregor67a65642009-02-17 23:15:12 +0000708 TemplateArgs.size());
Douglas Gregor0db4ccd2009-02-09 21:04:56 +0000709
Douglas Gregor8bf42052009-02-09 18:46:07 +0000710 // Build the annotation token.
Douglas Gregorb67535d2009-03-31 00:43:58 +0000711 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
Mike Stump11289f42009-09-09 15:08:12 +0000712 Action::TypeResult Type
Douglas Gregordc572a32009-03-30 22:58:21 +0000713 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
714 LAngleLoc, TemplateArgsPtr,
715 &TemplateArgLocations[0],
716 RAngleLoc);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000717 if (Type.isInvalid()) {
718 // If we failed to parse the template ID but skipped ahead to a >, we're not
719 // going to be able to form a token annotation. Eat the '>' if present.
720 if (Tok.is(tok::greater))
721 ConsumeToken();
722 return true;
723 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000724
725 Tok.setKind(tok::annot_typename);
726 Tok.setAnnotationValue(Type.get());
Douglas Gregor7f741122009-02-25 19:37:18 +0000727 if (SS && SS->isNotEmpty())
728 Tok.setLocation(SS->getBeginLoc());
729 else if (TemplateKWLoc.isValid())
730 Tok.setLocation(TemplateKWLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000731 else
Douglas Gregor7f741122009-02-25 19:37:18 +0000732 Tok.setLocation(TemplateNameLoc);
Douglas Gregor67a65642009-02-17 23:15:12 +0000733 } else {
Douglas Gregorb67535d2009-03-31 00:43:58 +0000734 // Build a template-id annotation token that can be processed
735 // later.
Douglas Gregor7f741122009-02-25 19:37:18 +0000736 Tok.setKind(tok::annot_template_id);
Mike Stump11289f42009-09-09 15:08:12 +0000737 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000738 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor8bf42052009-02-09 18:46:07 +0000739 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000740 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
741 TemplateId->Name = TemplateName.Identifier;
742 TemplateId->Operator = OO_None;
743 } else {
744 TemplateId->Name = 0;
745 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
746 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000747 TemplateId->Template = Template.getAs<void*>();
Douglas Gregor7f741122009-02-25 19:37:18 +0000748 TemplateId->Kind = TNK;
Douglas Gregor8bf42052009-02-09 18:46:07 +0000749 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000750 TemplateId->RAngleLoc = RAngleLoc;
751 void **Args = TemplateId->getTemplateArgs();
752 bool *ArgIsType = TemplateId->getTemplateArgIsType();
753 SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations();
754 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) {
Douglas Gregor8bf42052009-02-09 18:46:07 +0000755 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7f741122009-02-25 19:37:18 +0000756 ArgIsType[Arg] = TemplateArgIsType[Arg];
757 ArgLocs[Arg] = TemplateArgLocations[Arg];
758 }
Douglas Gregor8bf42052009-02-09 18:46:07 +0000759 Tok.setAnnotationValue(TemplateId);
Douglas Gregor7f741122009-02-25 19:37:18 +0000760 if (TemplateKWLoc.isValid())
761 Tok.setLocation(TemplateKWLoc);
762 else
763 Tok.setLocation(TemplateNameLoc);
764
765 TemplateArgsPtr.release();
Douglas Gregor8bf42052009-02-09 18:46:07 +0000766 }
767
768 // Common fields for the annotation token
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000769 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000770
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000771 // In case the tokens were cached, have Preprocessor replace them with the
772 // annotation token.
773 PP.AnnotateCachedTokens(Tok);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000774 return false;
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000775}
776
Douglas Gregor7f741122009-02-25 19:37:18 +0000777/// \brief Replaces a template-id annotation token with a type
778/// annotation token.
779///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000780/// If there was a failure when forming the type from the template-id,
781/// a type annotation token will still be created, but will have a
782/// NULL type pointer to signify an error.
783void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000784 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
785
Mike Stump11289f42009-09-09 15:08:12 +0000786 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000787 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000788 assert((TemplateId->Kind == TNK_Type_template ||
789 TemplateId->Kind == TNK_Dependent_template_name) &&
790 "Only works for type and dependent templates");
Mike Stump11289f42009-09-09 15:08:12 +0000791
792 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000793 TemplateId->getTemplateArgs(),
794 TemplateId->getTemplateArgIsType(),
795 TemplateId->NumArgs);
796
Mike Stump11289f42009-09-09 15:08:12 +0000797 Action::TypeResult Type
Douglas Gregordc572a32009-03-30 22:58:21 +0000798 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
799 TemplateId->TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000800 TemplateId->LAngleLoc,
Douglas Gregordc572a32009-03-30 22:58:21 +0000801 TemplateArgsPtr,
802 TemplateId->getTemplateArgLocations(),
803 TemplateId->RAngleLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +0000804 // Create the new "type" annotation token.
805 Tok.setKind(tok::annot_typename);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000806 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
Douglas Gregor7f741122009-02-25 19:37:18 +0000807 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
808 Tok.setLocation(SS->getBeginLoc());
809
810 // We might be backtracking, in which case we need to replace the
811 // template-id annotation token with the type annotation within the
812 // set of cached tokens. That way, we won't try to form the same
813 // class template specialization again.
814 PP.ReplaceLastTokenWithAnnotation(Tok);
815 TemplateId->Destroy();
Douglas Gregor7f741122009-02-25 19:37:18 +0000816}
817
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000818/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
819///
820/// template-argument: [C++ 14.2]
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000821/// constant-expression
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000822/// type-id
823/// id-expression
Douglas Gregor67b556a2009-02-09 19:34:22 +0000824void *Parser::ParseTemplateArgument(bool &ArgIsType) {
Douglas Gregor8bf42052009-02-09 18:46:07 +0000825 // C++ [temp.arg]p2:
826 // In a template-argument, an ambiguity between a type-id and an
827 // expression is resolved to a type-id, regardless of the form of
828 // the corresponding template-parameter.
829 //
830 // Therefore, we initially try to parse a type-id.
Douglas Gregor97f34572009-02-10 00:53:15 +0000831 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregor67b556a2009-02-09 19:34:22 +0000832 ArgIsType = true;
Douglas Gregor220cac52009-02-18 17:45:20 +0000833 TypeResult TypeArg = ParseTypeName();
834 if (TypeArg.isInvalid())
835 return 0;
836 return TypeArg.get();
Douglas Gregor8bf42052009-02-09 18:46:07 +0000837 }
838
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000839 OwningExprResult ExprArg = ParseConstantExpression();
Douglas Gregord32e0282009-02-09 23:23:08 +0000840 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregor67b556a2009-02-09 19:34:22 +0000841 return 0;
Douglas Gregor8bf42052009-02-09 18:46:07 +0000842
Douglas Gregor67b556a2009-02-09 19:34:22 +0000843 ArgIsType = false;
844 return ExprArg.release();
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000845}
846
847/// ParseTemplateArgumentList - Parse a C++ template-argument-list
848/// (C++ [temp.names]). Returns true if there was an error.
849///
850/// template-argument-list: [C++ 14.2]
851/// template-argument
852/// template-argument-list ',' template-argument
Mike Stump11289f42009-09-09 15:08:12 +0000853bool
Douglas Gregor67b556a2009-02-09 19:34:22 +0000854Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs,
Douglas Gregord32e0282009-02-09 23:23:08 +0000855 TemplateArgIsTypeList &TemplateArgIsType,
856 TemplateArgLocationList &TemplateArgLocations) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000857 while (true) {
Douglas Gregor67b556a2009-02-09 19:34:22 +0000858 bool IsType = false;
Douglas Gregord32e0282009-02-09 23:23:08 +0000859 SourceLocation Loc = Tok.getLocation();
Douglas Gregor67b556a2009-02-09 19:34:22 +0000860 void *Arg = ParseTemplateArgument(IsType);
861 if (Arg) {
862 TemplateArgs.push_back(Arg);
863 TemplateArgIsType.push_back(IsType);
Douglas Gregord32e0282009-02-09 23:23:08 +0000864 TemplateArgLocations.push_back(Loc);
Douglas Gregor67b556a2009-02-09 19:34:22 +0000865 } else {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000866 SkipUntil(tok::comma, tok::greater, true, true);
867 return true;
868 }
Douglas Gregor67b556a2009-02-09 19:34:22 +0000869
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000870 // If the next token is a comma, consume it and keep reading
871 // arguments.
872 if (Tok.isNot(tok::comma)) break;
873
874 // Consume the comma.
875 ConsumeToken();
876 }
877
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000878 return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000879}
880
Mike Stump11289f42009-09-09 15:08:12 +0000881/// \brief Parse a C++ explicit template instantiation
Douglas Gregor23996282009-05-12 21:31:51 +0000882/// (C++ [temp.explicit]).
883///
884/// explicit-instantiation:
Douglas Gregor43e75172009-09-04 06:33:52 +0000885/// 'extern' [opt] 'template' declaration
886///
887/// Note that the 'extern' is a GNU extension and C++0x feature.
Mike Stump11289f42009-09-09 15:08:12 +0000888Parser::DeclPtrTy
Douglas Gregor43e75172009-09-04 06:33:52 +0000889Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
890 SourceLocation TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000891 SourceLocation &DeclEnd) {
Mike Stump11289f42009-09-09 15:08:12 +0000892 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
Douglas Gregor43e75172009-09-04 06:33:52 +0000893 ParsedTemplateInfo(ExternLoc,
894 TemplateLoc),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000895 DeclEnd, AS_none);
Douglas Gregor23996282009-05-12 21:31:51 +0000896}