blob: d0e63496b51164cb6dd995ed53a114a74117c819 [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"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/ParsedTemplate.h"
18#include "clang/Sema/Scope.h"
Chris Lattnerb2434d52009-12-10 00:45:15 +000019#include "RAIIObjectsForParser.h"
Francois Pichet1c229c02011-04-22 22:18:13 +000020#include "clang/AST/DeclTemplate.h"
21#include "clang/AST/ASTConsumer.h"
Douglas Gregoreb31f392008-12-01 23:54:00 +000022using namespace clang;
23
Douglas Gregor1b57ff32009-05-12 23:25:50 +000024/// \brief Parse a template declaration, explicit instantiation, or
25/// explicit specialization.
John McCall48871652010-08-21 09:40:31 +000026Decl *
Douglas Gregor1b57ff32009-05-12 23:25:50 +000027Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
28 SourceLocation &DeclEnd,
29 AccessSpecifier AS) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000030 Decl *DC = getObjCDeclContext();
31 if (DC)
32 Actions.ActOnObjCContainerFinishDefinition(DC);
33 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
34 Decl *Res = ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
35 DeclEnd);
36 if (DC)
37 Actions.ActOnObjCContainerStartDefinition(DC);
38 return Res;
39 }
40 Decl *Res = ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
41 if (DC)
42 Actions.ActOnObjCContainerStartDefinition(DC);
43 return Res;
Douglas Gregor1b57ff32009-05-12 23:25:50 +000044}
45
Douglas Gregora3dff8e2009-08-24 23:03:25 +000046/// \brief RAII class that manages the template parameter depth.
47namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +000048 class TemplateParameterDepthCounter {
Douglas Gregora3dff8e2009-08-24 23:03:25 +000049 unsigned &Depth;
50 unsigned AddedLevels;
51
52 public:
Mike Stump11289f42009-09-09 15:08:12 +000053 explicit TemplateParameterDepthCounter(unsigned &Depth)
Douglas Gregora3dff8e2009-08-24 23:03:25 +000054 : Depth(Depth), AddedLevels(0) { }
Mike Stump11289f42009-09-09 15:08:12 +000055
Douglas Gregora3dff8e2009-08-24 23:03:25 +000056 ~TemplateParameterDepthCounter() {
57 Depth -= AddedLevels;
58 }
Mike Stump11289f42009-09-09 15:08:12 +000059
60 void operator++() {
Douglas Gregora3dff8e2009-08-24 23:03:25 +000061 ++Depth;
62 ++AddedLevels;
63 }
Mike Stump11289f42009-09-09 15:08:12 +000064
Douglas Gregora3dff8e2009-08-24 23:03:25 +000065 operator unsigned() const { return Depth; }
66 };
67}
68
Douglas Gregor67a65642009-02-17 23:15:12 +000069/// \brief Parse a template declaration or an explicit specialization.
70///
71/// Template declarations include one or more template parameter lists
72/// and either the function or class template declaration. Explicit
73/// specializations contain one or more 'template < >' prefixes
74/// followed by a (possibly templated) declaration. Since the
75/// syntactic form of both features is nearly identical, we parse all
76/// of the template headers together and let semantic analysis sort
77/// the declarations from the explicit specializations.
Douglas Gregoreb31f392008-12-01 23:54:00 +000078///
79/// template-declaration: [C++ temp]
80/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
Douglas Gregor67a65642009-02-17 23:15:12 +000081///
82/// explicit-specialization: [ C++ temp.expl.spec]
83/// 'template' '<' '>' declaration
John McCall48871652010-08-21 09:40:31 +000084Decl *
Anders Carlssondfbbdf62009-03-26 00:52:18 +000085Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +000086 SourceLocation &DeclEnd,
Anders Carlssondfbbdf62009-03-26 00:52:18 +000087 AccessSpecifier AS) {
Mike Stump11289f42009-09-09 15:08:12 +000088 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
89 "Token does not start a template declaration.");
90
Douglas Gregorf5586182008-12-02 00:41:28 +000091 // Enter template-parameter scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +000092 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Douglas Gregorf5586182008-12-02 00:41:28 +000093
John McCall796c2a52010-07-16 08:13:16 +000094 // Tell the action that names should be checked in the context of
95 // the declaration to come.
96 ParsingDeclRAIIObject ParsingTemplateParams(*this);
97
Douglas Gregorb9bd8a92008-12-24 02:52:09 +000098 // Parse multiple levels of template headers within this template
99 // parameter scope, e.g.,
100 //
101 // template<typename T>
102 // template<typename U>
103 // class A<T>::B { ... };
104 //
105 // We parse multiple levels non-recursively so that we can build a
106 // single data structure containing all of the template parameter
Douglas Gregor67a65642009-02-17 23:15:12 +0000107 // lists to easily differentiate between the case above and:
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000108 //
109 // template<typename T>
110 // class A {
111 // template<typename U> class B;
112 // };
113 //
114 // In the first case, the action for declaring A<T>::B receives
115 // both template parameter lists. In the second case, the action for
116 // defining A<T>::B receives just the inner template parameter list
117 // (and retrieves the outer template parameter list from its
118 // context).
Douglas Gregor468535e2009-08-20 18:46:05 +0000119 bool isSpecialization = true;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000120 bool LastParamListWasEmpty = false;
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000121 TemplateParameterLists ParamLists;
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000122 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000123 do {
124 // Consume the 'export', if any.
125 SourceLocation ExportLoc;
126 if (Tok.is(tok::kw_export)) {
127 ExportLoc = ConsumeToken();
128 }
129
130 // Consume the 'template', which should be here.
131 SourceLocation TemplateLoc;
132 if (Tok.is(tok::kw_template)) {
133 TemplateLoc = ConsumeToken();
134 } else {
135 Diag(Tok.getLocation(), diag::err_expected_template);
John McCall48871652010-08-21 09:40:31 +0000136 return 0;
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000137 }
Mike Stump11289f42009-09-09 15:08:12 +0000138
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000139 // Parse the '<' template-parameter-list '>'
140 SourceLocation LAngleLoc, RAngleLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000141 SmallVector<Decl*, 4> TemplateParams;
Mike Stump11289f42009-09-09 15:08:12 +0000142 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000143 RAngleLoc)) {
144 // Skip until the semi-colon or a }.
145 SkipUntil(tok::r_brace, true, true);
146 if (Tok.is(tok::semi))
147 ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000148 return 0;
Douglas Gregore93e46c2009-07-22 23:48:44 +0000149 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000150
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000151 ParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000152 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
153 TemplateLoc, LAngleLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000154 TemplateParams.data(),
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000155 TemplateParams.size(), RAngleLoc));
Douglas Gregora3dff8e2009-08-24 23:03:25 +0000156
157 if (!TemplateParams.empty()) {
158 isSpecialization = false;
159 ++Depth;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000160 } else {
161 LastParamListWasEmpty = true;
Mike Stump11289f42009-09-09 15:08:12 +0000162 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000163 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
164
165 // Parse the actual template declaration.
Mike Stump11289f42009-09-09 15:08:12 +0000166 return ParseSingleDeclarationAfterTemplate(Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000167 ParsedTemplateInfo(&ParamLists,
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000168 isSpecialization,
169 LastParamListWasEmpty),
John McCall796c2a52010-07-16 08:13:16 +0000170 ParsingTemplateParams,
Douglas Gregor23996282009-05-12 21:31:51 +0000171 DeclEnd, AS);
172}
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000173
Douglas Gregor23996282009-05-12 21:31:51 +0000174/// \brief Parse a single declaration that declares a template,
175/// template specialization, or explicit instantiation of a template.
176///
177/// \param TemplateParams if non-NULL, the template parameter lists
178/// that preceded this declaration. In this case, the declaration is a
179/// template declaration, out-of-line definition of a template, or an
180/// explicit template specialization. When NULL, the declaration is an
181/// explicit template instantiation.
182///
183/// \param TemplateLoc when TemplateParams is NULL, the location of
184/// the 'template' keyword that indicates that we have an explicit
185/// template instantiation.
186///
187/// \param DeclEnd will receive the source location of the last token
188/// within this declaration.
189///
190/// \param AS the access specifier associated with this
191/// declaration. Will be AS_none for namespace-scope declarations.
192///
193/// \returns the new declaration.
John McCall48871652010-08-21 09:40:31 +0000194Decl *
Douglas Gregor23996282009-05-12 21:31:51 +0000195Parser::ParseSingleDeclarationAfterTemplate(
196 unsigned Context,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000197 const ParsedTemplateInfo &TemplateInfo,
John McCall796c2a52010-07-16 08:13:16 +0000198 ParsingDeclRAIIObject &DiagsFromTParams,
Douglas Gregor23996282009-05-12 21:31:51 +0000199 SourceLocation &DeclEnd,
200 AccessSpecifier AS) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000201 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
202 "Template information required");
203
Douglas Gregor3447e762009-08-20 22:52:58 +0000204 if (Context == Declarator::MemberContext) {
205 // We are parsing a member template.
John McCall796c2a52010-07-16 08:13:16 +0000206 ParseCXXClassMemberDeclaration(AS, TemplateInfo, &DiagsFromTParams);
John McCall48871652010-08-21 09:40:31 +0000207 return 0;
Douglas Gregor3447e762009-08-20 22:52:58 +0000208 }
Mike Stump11289f42009-09-09 15:08:12 +0000209
John McCall084e83d2011-03-24 11:26:52 +0000210 ParsedAttributesWithRange prefixAttrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000211 MaybeParseCXX0XAttributes(prefixAttrs);
John McCall9b72f892010-11-10 02:40:36 +0000212
213 if (Tok.is(tok::kw_using))
214 return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000215 prefixAttrs);
John McCall9b72f892010-11-10 02:40:36 +0000216
John McCall796c2a52010-07-16 08:13:16 +0000217 // Parse the declaration specifiers, stealing the accumulated
218 // diagnostics from the template parameters.
John McCall084e83d2011-03-24 11:26:52 +0000219 ParsingDeclSpec DS(*this, &DiagsFromTParams);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000220
John McCall53fa7142010-12-24 02:08:15 +0000221 DS.takeAttributesFrom(prefixAttrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000222
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000223 ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
224 getDeclSpecContextFromDeclaratorContext(Context));
Douglas Gregor23996282009-05-12 21:31:51 +0000225
226 if (Tok.is(tok::semi)) {
227 DeclEnd = ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000228 Decl *Decl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
John McCall28a6aea2009-11-04 02:18:39 +0000229 DS.complete(Decl);
230 return Decl;
Douglas Gregor23996282009-05-12 21:31:51 +0000231 }
232
233 // Parse the declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000234 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
Douglas Gregor23996282009-05-12 21:31:51 +0000235 ParseDeclarator(DeclaratorInfo);
236 // Error parsing the declarator?
237 if (!DeclaratorInfo.hasName()) {
238 // If so, skip until the semi-colon or a }.
239 SkipUntil(tok::r_brace, true, true);
240 if (Tok.is(tok::semi))
241 ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000242 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000243 }
Mike Stump11289f42009-09-09 15:08:12 +0000244
Douglas Gregor23996282009-05-12 21:31:51 +0000245 // If we have a declaration or declarator list, handle it.
246 if (isDeclarationAfterDeclarator()) {
247 // Parse this declaration.
John McCall48871652010-08-21 09:40:31 +0000248 Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
249 TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000250
251 if (Tok.is(tok::comma)) {
252 Diag(Tok, diag::err_multiple_template_declarators)
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000253 << (int)TemplateInfo.Kind;
Douglas Gregor23996282009-05-12 21:31:51 +0000254 SkipUntil(tok::semi, true, false);
255 return ThisDecl;
256 }
257
258 // Eat the semi colon after the declaration.
John McCallef50e992009-07-31 02:20:35 +0000259 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
John McCallc1465822011-02-14 07:13:47 +0000260 DeclaratorInfo.complete(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +0000261 return ThisDecl;
262 }
263
264 if (DeclaratorInfo.isFunctionDeclarator() &&
Chris Lattner13901342010-07-11 22:42:07 +0000265 isStartOfFunctionDefinition(DeclaratorInfo)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000266 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
267 Diag(Tok, diag::err_function_declared_typedef);
268
269 if (Tok.is(tok::l_brace)) {
270 // This recovery skips the entire function body. It would be nice
271 // to simply call ParseFunctionDefinition() below, however Sema
272 // assumes the declarator represents a function, not a typedef.
273 ConsumeBrace();
274 SkipUntil(tok::r_brace, true);
275 } else {
276 SkipUntil(tok::semi);
277 }
John McCall48871652010-08-21 09:40:31 +0000278 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000279 }
Douglas Gregor17a7c122009-06-24 00:54:41 +0000280 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
Douglas Gregor23996282009-05-12 21:31:51 +0000281 }
282
283 if (DeclaratorInfo.isFunctionDeclarator())
284 Diag(Tok, diag::err_expected_fn_body);
285 else
286 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
287 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000288 return 0;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000289}
290
291/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000292/// angle brackets. Depth is the depth of this template-parameter-list, which
293/// is the number of template headers directly enclosing this template header.
294/// TemplateParams is the current list of template parameters we're building.
295/// The template parameter we parse will be added to this list. LAngleLoc and
Mike Stump11289f42009-09-09 15:08:12 +0000296/// RAngleLoc will receive the positions of the '<' and '>', respectively,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000297/// that enclose this template parameter list.
Douglas Gregore93e46c2009-07-22 23:48:44 +0000298///
299/// \returns true if an error occurred, false otherwise.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000300bool Parser::ParseTemplateParameters(unsigned Depth,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000301 SmallVectorImpl<Decl*> &TemplateParams,
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000302 SourceLocation &LAngleLoc,
303 SourceLocation &RAngleLoc) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000304 // Get the template parameter list.
Mike Stump11289f42009-09-09 15:08:12 +0000305 if (!Tok.is(tok::less)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000306 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
Douglas Gregore93e46c2009-07-22 23:48:44 +0000307 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000308 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000309 LAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000310
Douglas Gregoreb31f392008-12-01 23:54:00 +0000311 // Try to parse the template parameter list.
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000312 if (Tok.is(tok::greater))
313 RAngleLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000314 else if (ParseTemplateParameterList(Depth, TemplateParams)) {
315 if (!Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000316 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregore93e46c2009-07-22 23:48:44 +0000317 return true;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000318 }
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000319 RAngleLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000320 }
Douglas Gregore93e46c2009-07-22 23:48:44 +0000321 return false;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000322}
323
324/// ParseTemplateParameterList - Parse a template parameter list. If
325/// the parsing fails badly (i.e., closing bracket was left out), this
326/// will try to put the token stream in a reasonable position (closing
Mike Stump11289f42009-09-09 15:08:12 +0000327/// a statement, etc.) and return false.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000328///
329/// template-parameter-list: [C++ temp]
330/// template-parameter
331/// template-parameter-list ',' template-parameter
Mike Stump11289f42009-09-09 15:08:12 +0000332bool
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000333Parser::ParseTemplateParameterList(unsigned Depth,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000334 SmallVectorImpl<Decl*> &TemplateParams) {
Mike Stump11289f42009-09-09 15:08:12 +0000335 while (1) {
John McCall48871652010-08-21 09:40:31 +0000336 if (Decl *TmpParam
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000337 = ParseTemplateParameter(Depth, TemplateParams.size())) {
338 TemplateParams.push_back(TmpParam);
339 } else {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000340 // If we failed to parse a template parameter, skip until we find
341 // a comma or closing brace.
342 SkipUntil(tok::comma, tok::greater, true, true);
343 }
Mike Stump11289f42009-09-09 15:08:12 +0000344
Douglas Gregoreb31f392008-12-01 23:54:00 +0000345 // Did we find a comma or the end of the template parmeter list?
Mike Stump11289f42009-09-09 15:08:12 +0000346 if (Tok.is(tok::comma)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000347 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000348 } else if (Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000349 // Don't consume this... that's done by template parser.
350 break;
351 } else {
352 // Somebody probably forgot to close the template. Skip ahead and
353 // try to get out of the expression. This error is currently
354 // subsumed by whatever goes on in ParseTemplateParameter.
355 // TODO: This could match >>, and it would be nice to avoid those
356 // silly errors with template <vec<T>>.
Douglas Gregorb0484022010-10-15 01:15:58 +0000357 Diag(Tok.getLocation(), diag::err_expected_comma_greater);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000358 SkipUntil(tok::greater, true, true);
359 return false;
360 }
361 }
362 return true;
363}
364
Douglas Gregor26aedb72009-11-21 02:07:55 +0000365/// \brief Determine whether the parser is at the start of a template
366/// type parameter.
367bool Parser::isStartOfTemplateTypeParameter() {
Douglas Gregor71b209d2010-06-04 07:30:15 +0000368 if (Tok.is(tok::kw_class)) {
369 // "class" may be the start of an elaborated-type-specifier or a
370 // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter.
371 switch (NextToken().getKind()) {
372 case tok::equal:
373 case tok::comma:
374 case tok::greater:
375 case tok::greatergreater:
376 case tok::ellipsis:
377 return true;
378
379 case tok::identifier:
380 // This may be either a type-parameter or an elaborated-type-specifier.
381 // We have to look further.
382 break;
383
384 default:
385 return false;
386 }
387
388 switch (GetLookAheadToken(2).getKind()) {
389 case tok::equal:
390 case tok::comma:
391 case tok::greater:
392 case tok::greatergreater:
393 return true;
394
395 default:
396 return false;
397 }
398 }
Douglas Gregor26aedb72009-11-21 02:07:55 +0000399
400 if (Tok.isNot(tok::kw_typename))
401 return false;
402
403 // C++ [temp.param]p2:
404 // There is no semantic difference between class and typename in a
405 // template-parameter. typename followed by an unqualified-id
406 // names a template type parameter. typename followed by a
407 // qualified-id denotes the type in a non-type
408 // parameter-declaration.
409 Token Next = NextToken();
410
411 // If we have an identifier, skip over it.
412 if (Next.getKind() == tok::identifier)
413 Next = GetLookAheadToken(2);
414
415 switch (Next.getKind()) {
416 case tok::equal:
417 case tok::comma:
418 case tok::greater:
419 case tok::greatergreater:
420 case tok::ellipsis:
421 return true;
422
423 default:
424 return false;
425 }
426}
427
Douglas Gregoreb31f392008-12-01 23:54:00 +0000428/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
429///
430/// template-parameter: [C++ temp.param]
431/// type-parameter
432/// parameter-declaration
433///
434/// type-parameter: (see below)
Douglas Gregorf5500772011-01-05 15:48:55 +0000435/// 'class' ...[opt] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000436/// 'class' identifier[opt] '=' type-id
Douglas Gregorf5500772011-01-05 15:48:55 +0000437/// 'typename' ...[opt] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000438/// 'typename' identifier[opt] '=' type-id
Douglas Gregorf5500772011-01-05 15:48:55 +0000439/// 'template' '<' template-parameter-list '>'
440/// 'class' ...[opt] identifier[opt]
441/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
442/// = id-expression
John McCall48871652010-08-21 09:40:31 +0000443Decl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregor26aedb72009-11-21 02:07:55 +0000444 if (isStartOfTemplateTypeParameter())
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000445 return ParseTypeParameter(Depth, Position);
Mike Stump11289f42009-09-09 15:08:12 +0000446
447 if (Tok.is(tok::kw_template))
Chris Lattnera21db612009-01-04 23:51:17 +0000448 return ParseTemplateTemplateParameter(Depth, Position);
449
450 // If it's none of the above, then it must be a parameter declaration.
451 // NOTE: This will pick up errors in the closure of the template parameter
452 // list (e.g., template < ; Check here to implement >> style closures.
453 return ParseNonTypeTemplateParameter(Depth, Position);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000454}
455
456/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
457/// Other kinds of template parameters are parsed in
458/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
459///
460/// type-parameter: [C++ temp.param]
Anders Carlssonf986ba72009-06-12 23:09:56 +0000461/// 'class' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000462/// 'class' identifier[opt] '=' type-id
Anders Carlssonf986ba72009-06-12 23:09:56 +0000463/// 'typename' ...[opt][C++0x] identifier[opt]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000464/// 'typename' identifier[opt] '=' type-id
John McCall48871652010-08-21 09:40:31 +0000465Decl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000466 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
Mike Stump11289f42009-09-09 15:08:12 +0000467 "A type-parameter starts with 'class' or 'typename'");
Douglas Gregorf5586182008-12-02 00:41:28 +0000468
469 // Consume the 'class' or 'typename' keyword.
470 bool TypenameKeyword = Tok.is(tok::kw_typename);
471 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000472
Anders Carlsson01e9e932009-06-12 19:58:00 +0000473 // Grab the ellipsis (if given).
474 bool Ellipsis = false;
475 SourceLocation EllipsisLoc;
Anders Carlssonf986ba72009-06-12 23:09:56 +0000476 if (Tok.is(tok::ellipsis)) {
Anders Carlsson01e9e932009-06-12 19:58:00 +0000477 Ellipsis = true;
478 EllipsisLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000479
480 if (!getLang().CPlusPlus0x)
Douglas Gregorb25d8c32011-01-19 21:59:15 +0000481 Diag(EllipsisLoc, diag::ext_variadic_templates);
Anders Carlsson01e9e932009-06-12 19:58:00 +0000482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Douglas Gregoreb31f392008-12-01 23:54:00 +0000484 // Grab the template parameter name (if given)
Douglas Gregorf5586182008-12-02 00:41:28 +0000485 SourceLocation NameLoc;
486 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000487 if (Tok.is(tok::identifier)) {
Douglas Gregorf5586182008-12-02 00:41:28 +0000488 ParamName = Tok.getIdentifierInfo();
489 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000490 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
491 Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000492 // Unnamed template parameter. Don't have to do anything here, just
493 // don't consume this token.
494 } else {
495 Diag(Tok.getLocation(), diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +0000496 return 0;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000497 }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Douglas Gregordc13ded2010-07-01 00:00:45 +0000499 // Grab a default argument (if available).
500 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
501 // we introduce the type parameter into the local scope.
502 SourceLocation EqualLoc;
John McCallba7bf592010-08-24 05:47:05 +0000503 ParsedType DefaultArg;
Mike Stump11289f42009-09-09 15:08:12 +0000504 if (Tok.is(tok::equal)) {
Douglas Gregordc13ded2010-07-01 00:00:45 +0000505 EqualLoc = ConsumeToken();
506 DefaultArg = ParseTypeName().get();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000507 }
Douglas Gregordc13ded2010-07-01 00:00:45 +0000508
Douglas Gregor0be31a22010-07-02 17:43:08 +0000509 return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, Ellipsis,
Douglas Gregordc13ded2010-07-01 00:00:45 +0000510 EllipsisLoc, KeyLoc, ParamName, NameLoc,
511 Depth, Position, EqualLoc, DefaultArg);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000512}
513
514/// ParseTemplateTemplateParameter - Handle the parsing of template
Mike Stump11289f42009-09-09 15:08:12 +0000515/// template parameters.
Douglas Gregoreb31f392008-12-01 23:54:00 +0000516///
517/// type-parameter: [C++ temp.param]
Douglas Gregorf5500772011-01-05 15:48:55 +0000518/// 'template' '<' template-parameter-list '>' 'class'
519/// ...[opt] identifier[opt]
520/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
521/// = id-expression
John McCall48871652010-08-21 09:40:31 +0000522Decl *
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000523Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000524 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
525
526 // Handle the template <...> part.
527 SourceLocation TemplateLoc = ConsumeToken();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000528 SmallVector<Decl*,8> TemplateParams;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000529 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000530 {
531 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
Mike Stump11289f42009-09-09 15:08:12 +0000532 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000533 RAngleLoc)) {
John McCall48871652010-08-21 09:40:31 +0000534 return 0;
Douglas Gregor85e8b3e2009-02-10 19:52:54 +0000535 }
Douglas Gregoreb31f392008-12-01 23:54:00 +0000536 }
537
538 // Generate a meaningful error if the user forgot to put class before the
539 // identifier, comma, or greater.
Mike Stump11289f42009-09-09 15:08:12 +0000540 if (!Tok.is(tok::kw_class)) {
541 Diag(Tok.getLocation(), diag::err_expected_class_before)
Douglas Gregoreb31f392008-12-01 23:54:00 +0000542 << PP.getSpelling(Tok);
John McCall48871652010-08-21 09:40:31 +0000543 return 0;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000544 }
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000545 ConsumeToken();
Douglas Gregoreb31f392008-12-01 23:54:00 +0000546
Douglas Gregorf5500772011-01-05 15:48:55 +0000547 // Parse the ellipsis, if given.
548 SourceLocation EllipsisLoc;
549 if (Tok.is(tok::ellipsis)) {
550 EllipsisLoc = ConsumeToken();
551
552 if (!getLang().CPlusPlus0x)
Douglas Gregorb25d8c32011-01-19 21:59:15 +0000553 Diag(EllipsisLoc, diag::ext_variadic_templates);
Douglas Gregorf5500772011-01-05 15:48:55 +0000554 }
555
Douglas Gregoreb31f392008-12-01 23:54:00 +0000556 // Get the identifier, if given.
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000557 SourceLocation NameLoc;
558 IdentifierInfo* ParamName = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000559 if (Tok.is(tok::identifier)) {
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000560 ParamName = Tok.getIdentifierInfo();
561 NameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000562 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000563 // Unnamed template parameter. Don't have to do anything here, just
564 // don't consume this token.
565 } else {
566 Diag(Tok.getLocation(), diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +0000567 return 0;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000568 }
569
Mike Stump11289f42009-09-09 15:08:12 +0000570 TemplateParamsTy *ParamList =
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000571 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
572 TemplateLoc, LAngleLoc,
Douglas Gregora02bb372010-10-21 17:26:49 +0000573 TemplateParams.data(),
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000574 TemplateParams.size(),
575 RAngleLoc);
576
Douglas Gregordc13ded2010-07-01 00:00:45 +0000577 // Grab a default argument (if available).
578 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
579 // we introduce the template parameter into the local scope.
580 SourceLocation EqualLoc;
581 ParsedTemplateArgument DefaultArg;
Douglas Gregordba32632009-02-10 19:49:53 +0000582 if (Tok.is(tok::equal)) {
Douglas Gregordc13ded2010-07-01 00:00:45 +0000583 EqualLoc = ConsumeToken();
584 DefaultArg = ParseTemplateTemplateArgument();
585 if (DefaultArg.isInvalid()) {
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000586 Diag(Tok.getLocation(),
587 diag::err_default_template_template_parameter_not_template);
Nuno Lopes221c1fd2009-12-10 00:07:02 +0000588 static const tok::TokenKind EndToks[] = {
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000589 tok::comma, tok::greater, tok::greatergreater
590 };
591 SkipUntil(EndToks, 3, true, true);
Douglas Gregordc13ded2010-07-01 00:00:45 +0000592 }
Douglas Gregordba32632009-02-10 19:49:53 +0000593 }
Douglas Gregordc13ded2010-07-01 00:00:45 +0000594
Douglas Gregor0be31a22010-07-02 17:43:08 +0000595 return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
Douglas Gregorf5500772011-01-05 15:48:55 +0000596 ParamList, EllipsisLoc,
597 ParamName, NameLoc, Depth,
598 Position, EqualLoc, DefaultArg);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000599}
600
601/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
Mike Stump11289f42009-09-09 15:08:12 +0000602/// template parameters (e.g., in "template<int Size> class array;").
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000603///
Douglas Gregoreb31f392008-12-01 23:54:00 +0000604/// template-parameter:
605/// ...
606/// parameter-declaration
John McCall48871652010-08-21 09:40:31 +0000607Decl *
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000608Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000609 // Parse the declaration-specifiers (i.e., the type).
Douglas Gregorf5586182008-12-02 00:41:28 +0000610 // FIXME: The type should probably be restricted in some way... Not all
Douglas Gregoreb31f392008-12-01 23:54:00 +0000611 // declarators (parts of declarators?) are accepted for parameters.
John McCall084e83d2011-03-24 11:26:52 +0000612 DeclSpec DS(AttrFactory);
Douglas Gregorf5586182008-12-02 00:41:28 +0000613 ParseDeclarationSpecifiers(DS);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000614
615 // Parse this as a typename.
Douglas Gregorf5586182008-12-02 00:41:28 +0000616 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
617 ParseDeclarator(ParamDecl);
John McCallba7bf592010-08-24 05:47:05 +0000618 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000619 // This probably shouldn't happen - and it's more of a Sema thing, but
620 // basically we didn't parse the type name because we couldn't associate
621 // it with an AST node. we should just skip to the comma or greater.
622 // TODO: This is currently a placeholder for some kind of Sema Error.
623 Diag(Tok.getLocation(), diag::err_parse_error);
624 SkipUntil(tok::comma, tok::greater, true, true);
John McCall48871652010-08-21 09:40:31 +0000625 return 0;
Douglas Gregoreb31f392008-12-01 23:54:00 +0000626 }
627
Douglas Gregordba32632009-02-10 19:49:53 +0000628 // If there is a default value, parse it.
Douglas Gregordc13ded2010-07-01 00:00:45 +0000629 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
630 // we introduce the template parameter into the local scope.
631 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +0000632 ExprResult DefaultArg;
Chris Lattnerb5134c02009-01-05 01:24:05 +0000633 if (Tok.is(tok::equal)) {
Douglas Gregordc13ded2010-07-01 00:00:45 +0000634 EqualLoc = ConsumeToken();
Douglas Gregordba32632009-02-10 19:49:53 +0000635
636 // C++ [temp.param]p15:
637 // When parsing a default template-argument for a non-type
638 // template-parameter, the first non-nested > is taken as the
639 // end of the template-parameter-list rather than a greater-than
640 // operator.
Mike Stump11289f42009-09-09 15:08:12 +0000641 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Douglas Gregordba32632009-02-10 19:49:53 +0000642
Douglas Gregordc13ded2010-07-01 00:00:45 +0000643 DefaultArg = ParseAssignmentExpression();
Douglas Gregordba32632009-02-10 19:49:53 +0000644 if (DefaultArg.isInvalid())
645 SkipUntil(tok::comma, tok::greater, true, true);
Douglas Gregoreb31f392008-12-01 23:54:00 +0000646 }
Mike Stump11289f42009-09-09 15:08:12 +0000647
Douglas Gregordc13ded2010-07-01 00:00:45 +0000648 // Create the parameter.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000649 return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
Douglas Gregordc13ded2010-07-01 00:00:45 +0000650 Depth, Position, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +0000651 DefaultArg.take());
Douglas Gregoreb31f392008-12-01 23:54:00 +0000652}
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000653
Douglas Gregor67a65642009-02-17 23:15:12 +0000654/// \brief Parses a template-id that after the template name has
655/// already been parsed.
656///
657/// This routine takes care of parsing the enclosed template argument
658/// list ('<' template-parameter-list [opt] '>') and placing the
659/// results into a form that can be transferred to semantic analysis.
660///
661/// \param Template the template declaration produced by isTemplateName
662///
663/// \param TemplateNameLoc the source location of the template name
664///
665/// \param SS if non-NULL, the nested-name-specifier preceding the
666/// template name.
667///
668/// \param ConsumeLastToken if true, then we will consume the last
669/// token that forms the template-id. Otherwise, we will leave the
670/// last token in the stream (e.g., so that it can be replaced with an
671/// annotation token).
Mike Stump11289f42009-09-09 15:08:12 +0000672bool
Douglas Gregordc572a32009-03-30 22:58:21 +0000673Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
Mike Stump11289f42009-09-09 15:08:12 +0000674 SourceLocation TemplateNameLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +0000675 const CXXScopeSpec &SS,
Douglas Gregor67a65642009-02-17 23:15:12 +0000676 bool ConsumeLastToken,
677 SourceLocation &LAngleLoc,
678 TemplateArgList &TemplateArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000679 SourceLocation &RAngleLoc) {
680 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
681
682 // Consume the '<'.
683 LAngleLoc = ConsumeToken();
684
685 // Parse the optional template-argument-list.
686 bool Invalid = false;
687 {
688 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Douglas Gregor180dda92011-01-11 00:45:18 +0000689 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000690 Invalid = ParseTemplateArgumentList(TemplateArgs);
Douglas Gregor67a65642009-02-17 23:15:12 +0000691
692 if (Invalid) {
693 // Try to find the closing '>'.
694 SkipUntil(tok::greater, true, !ConsumeLastToken);
695
696 return true;
697 }
698 }
699
Eli Friedmanaffd5fd2009-12-27 22:31:18 +0000700 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) {
701 Diag(Tok.getLocation(), diag::err_expected_greater);
Douglas Gregor67a65642009-02-17 23:15:12 +0000702 return true;
Eli Friedmanaffd5fd2009-12-27 22:31:18 +0000703 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000704
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000705 // Determine the location of the '>' or '>>'. Only consume this
706 // token if the caller asked us to.
Douglas Gregor67a65642009-02-17 23:15:12 +0000707 RAngleLoc = Tok.getLocation();
708
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000709 if (Tok.is(tok::greatergreater)) {
Douglas Gregor87f95b02009-02-26 21:00:50 +0000710 if (!getLang().CPlusPlus0x) {
711 const char *ReplaceStr = "> >";
712 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
713 ReplaceStr = "> > ";
714
715 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
Douglas Gregora771f462010-03-31 17:46:05 +0000716 << FixItHint::CreateReplacement(
Douglas Gregor96977da2009-02-27 17:53:17 +0000717 SourceRange(Tok.getLocation()), ReplaceStr);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000718 }
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000719
720 Tok.setKind(tok::greater);
721 if (!ConsumeLastToken) {
722 // Since we're not supposed to consume the '>>' token, we need
723 // to insert a second '>' token after the first.
724 PP.EnterToken(Tok);
725 }
726 } else if (ConsumeLastToken)
Douglas Gregor67a65642009-02-17 23:15:12 +0000727 ConsumeToken();
728
729 return false;
730}
Mike Stump11289f42009-09-09 15:08:12 +0000731
Douglas Gregor7f741122009-02-25 19:37:18 +0000732/// \brief Replace the tokens that form a simple-template-id with an
733/// annotation token containing the complete template-id.
734///
735/// The first token in the stream must be the name of a template that
736/// is followed by a '<'. This routine will parse the complete
737/// simple-template-id and replace the tokens with a single annotation
738/// token with one of two different kinds: if the template-id names a
739/// type (and \p AllowTypeAnnotation is true), the annotation token is
740/// a type annotation that includes the optional nested-name-specifier
741/// (\p SS). Otherwise, the annotation token is a template-id
742/// annotation that does not include the optional
743/// nested-name-specifier.
744///
745/// \param Template the declaration of the template named by the first
746/// token (an identifier), as returned from \c Action::isTemplateName().
747///
748/// \param TemplateNameKind the kind of template that \p Template
749/// refers to, as returned from \c Action::isTemplateName().
750///
751/// \param SS if non-NULL, the nested-name-specifier that precedes
752/// this template name.
753///
754/// \param TemplateKWLoc if valid, specifies that this template-id
755/// annotation was preceded by the 'template' keyword and gives the
756/// location of that keyword. If invalid (the default), then this
757/// template-id was not preceded by a 'template' keyword.
758///
759/// \param AllowTypeAnnotation if true (the default), then a
760/// simple-template-id that refers to a class template, template
761/// template parameter, or other template that produces a type will be
762/// replaced with a type annotation token. Otherwise, the
763/// simple-template-id is always replaced with a template-id
764/// annotation token.
Chris Lattner5558e9f2009-06-26 04:27:47 +0000765///
766/// If an unrecoverable parse error occurs and no annotation token can be
767/// formed, this function returns true.
768///
769bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
Douglas Gregore7c20652011-03-02 00:47:37 +0000770 CXXScopeSpec &SS,
Douglas Gregor71395fa2009-11-04 00:56:37 +0000771 UnqualifiedId &TemplateName,
Douglas Gregor7f741122009-02-25 19:37:18 +0000772 SourceLocation TemplateKWLoc,
773 bool AllowTypeAnnotation) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000774 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
Douglas Gregor71395fa2009-11-04 00:56:37 +0000775 assert(Template && Tok.is(tok::less) &&
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000776 "Parser isn't at the beginning of a template-id");
777
778 // Consume the template-name.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000779 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000780
Douglas Gregor67a65642009-02-17 23:15:12 +0000781 // Parse the enclosed template argument list.
782 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor67b556a2009-02-09 19:34:22 +0000783 TemplateArgList TemplateArgs;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000784 bool Invalid = ParseTemplateIdAfterTemplateName(Template,
785 TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000786 SS, false, LAngleLoc,
787 TemplateArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000788 RAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000789
Chris Lattner5558e9f2009-06-26 04:27:47 +0000790 if (Invalid) {
791 // If we failed to parse the template ID but skipped ahead to a >, we're not
792 // going to be able to form a token annotation. Eat the '>' if present.
793 if (Tok.is(tok::greater))
794 ConsumeToken();
795 return true;
796 }
Douglas Gregord32e0282009-02-09 23:23:08 +0000797
Jay Foad7d0479f2009-05-21 09:52:38 +0000798 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor67a65642009-02-17 23:15:12 +0000799 TemplateArgs.size());
Douglas Gregor0db4ccd2009-02-09 21:04:56 +0000800
Douglas Gregor8bf42052009-02-09 18:46:07 +0000801 // Build the annotation token.
Douglas Gregorb67535d2009-03-31 00:43:58 +0000802 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
John McCallfaf5fb42010-08-26 23:41:50 +0000803 TypeResult Type
Douglas Gregore7c20652011-03-02 00:47:37 +0000804 = Actions.ActOnTemplateIdType(SS,
805 Template, TemplateNameLoc,
Douglas Gregordc572a32009-03-30 22:58:21 +0000806 LAngleLoc, TemplateArgsPtr,
Douglas Gregordc572a32009-03-30 22:58:21 +0000807 RAngleLoc);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000808 if (Type.isInvalid()) {
809 // If we failed to parse the template ID but skipped ahead to a >, we're not
810 // going to be able to form a token annotation. Eat the '>' if present.
811 if (Tok.is(tok::greater))
812 ConsumeToken();
813 return true;
814 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000815
816 Tok.setKind(tok::annot_typename);
John McCallba7bf592010-08-24 05:47:05 +0000817 setTypeAnnotation(Tok, Type.get());
Douglas Gregore7c20652011-03-02 00:47:37 +0000818 if (SS.isNotEmpty())
819 Tok.setLocation(SS.getBeginLoc());
Douglas Gregor7f741122009-02-25 19:37:18 +0000820 else if (TemplateKWLoc.isValid())
821 Tok.setLocation(TemplateKWLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000822 else
Douglas Gregor7f741122009-02-25 19:37:18 +0000823 Tok.setLocation(TemplateNameLoc);
Douglas Gregor67a65642009-02-17 23:15:12 +0000824 } else {
Douglas Gregorb67535d2009-03-31 00:43:58 +0000825 // Build a template-id annotation token that can be processed
826 // later.
Douglas Gregor7f741122009-02-25 19:37:18 +0000827 Tok.setKind(tok::annot_template_id);
Mike Stump11289f42009-09-09 15:08:12 +0000828 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000829 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
Douglas Gregor8bf42052009-02-09 18:46:07 +0000830 TemplateId->TemplateNameLoc = TemplateNameLoc;
Douglas Gregor71395fa2009-11-04 00:56:37 +0000831 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
832 TemplateId->Name = TemplateName.Identifier;
833 TemplateId->Operator = OO_None;
834 } else {
835 TemplateId->Name = 0;
836 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
837 }
Douglas Gregore7c20652011-03-02 00:47:37 +0000838 TemplateId->SS = SS;
John McCall3e56fd42010-08-23 07:28:44 +0000839 TemplateId->Template = Template;
Douglas Gregor7f741122009-02-25 19:37:18 +0000840 TemplateId->Kind = TNK;
Douglas Gregor8bf42052009-02-09 18:46:07 +0000841 TemplateId->LAngleLoc = LAngleLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000842 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000843 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
844 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
Douglas Gregor869ad452011-02-24 17:54:50 +0000845 Args[Arg] = ParsedTemplateArgument(TemplateArgs[Arg]);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000846 Tok.setAnnotationValue(TemplateId);
Douglas Gregor7f741122009-02-25 19:37:18 +0000847 if (TemplateKWLoc.isValid())
848 Tok.setLocation(TemplateKWLoc);
849 else
850 Tok.setLocation(TemplateNameLoc);
851
852 TemplateArgsPtr.release();
Douglas Gregor8bf42052009-02-09 18:46:07 +0000853 }
854
855 // Common fields for the annotation token
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000856 Tok.setAnnotationEndLoc(RAngleLoc);
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000857
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000858 // In case the tokens were cached, have Preprocessor replace them with the
859 // annotation token.
860 PP.AnnotateCachedTokens(Tok);
Chris Lattner5558e9f2009-06-26 04:27:47 +0000861 return false;
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000862}
863
Douglas Gregor7f741122009-02-25 19:37:18 +0000864/// \brief Replaces a template-id annotation token with a type
865/// annotation token.
866///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000867/// If there was a failure when forming the type from the template-id,
868/// a type annotation token will still be created, but will have a
869/// NULL type pointer to signify an error.
Douglas Gregore7c20652011-03-02 00:47:37 +0000870void Parser::AnnotateTemplateIdTokenAsType() {
Douglas Gregor7f741122009-02-25 19:37:18 +0000871 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
872
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000873 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +0000874 assert((TemplateId->Kind == TNK_Type_template ||
875 TemplateId->Kind == TNK_Dependent_template_name) &&
876 "Only works for type and dependent templates");
Mike Stump11289f42009-09-09 15:08:12 +0000877
878 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000879 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000880 TemplateId->NumArgs);
881
John McCallfaf5fb42010-08-26 23:41:50 +0000882 TypeResult Type
Douglas Gregore7c20652011-03-02 00:47:37 +0000883 = Actions.ActOnTemplateIdType(TemplateId->SS,
884 TemplateId->Template,
Douglas Gregordc572a32009-03-30 22:58:21 +0000885 TemplateId->TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000886 TemplateId->LAngleLoc,
Douglas Gregordc572a32009-03-30 22:58:21 +0000887 TemplateArgsPtr,
Douglas Gregordc572a32009-03-30 22:58:21 +0000888 TemplateId->RAngleLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +0000889 // Create the new "type" annotation token.
890 Tok.setKind(tok::annot_typename);
John McCallba7bf592010-08-24 05:47:05 +0000891 setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get());
Douglas Gregore7c20652011-03-02 00:47:37 +0000892 if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name.
893 Tok.setLocation(TemplateId->SS.getBeginLoc());
Sebastian Redlb0e3e1b2010-02-08 19:35:18 +0000894 // End location stays the same
Douglas Gregor7f741122009-02-25 19:37:18 +0000895
Douglas Gregor35522592009-11-04 18:18:19 +0000896 // Replace the template-id annotation token, and possible the scope-specifier
897 // that precedes it, with the typename annotation token.
898 PP.AnnotateCachedTokens(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +0000899}
900
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000901/// \brief Determine whether the given token can end a template argument.
Benjamin Kramer2c9a91c2009-11-10 21:29:56 +0000902static bool isEndOfTemplateArgument(Token Tok) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000903 return Tok.is(tok::comma) || Tok.is(tok::greater) ||
904 Tok.is(tok::greatergreater);
905}
906
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000907/// \brief Parse a C++ template template argument.
908ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
909 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
910 !Tok.is(tok::annot_cxxscope))
911 return ParsedTemplateArgument();
912
913 // C++0x [temp.arg.template]p1:
914 // A template-argument for a template template-parameter shall be the name
Richard Smith3f1b5d02011-05-05 21:57:07 +0000915 // of a class template or an alias template, expressed as id-expression.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000916 //
Richard Smith3f1b5d02011-05-05 21:57:07 +0000917 // We parse an id-expression that refers to a class template or alias
918 // template. The grammar we parse is:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000919 //
Douglas Gregore9a80352011-01-05 17:33:50 +0000920 // nested-name-specifier[opt] template[opt] identifier ...[opt]
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000921 //
922 // followed by a token that terminates a template argument, such as ',',
923 // '>', or (in some cases) '>>'.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000924 CXXScopeSpec SS; // nested-name-specifier, if present
John McCallba7bf592010-08-24 05:47:05 +0000925 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000926 /*EnteringContext=*/false);
927
Douglas Gregore9a80352011-01-05 17:33:50 +0000928 ParsedTemplateArgument Result;
929 SourceLocation EllipsisLoc;
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000930 if (SS.isSet() && Tok.is(tok::kw_template)) {
931 // Parse the optional 'template' keyword following the
932 // nested-name-specifier.
933 SourceLocation TemplateLoc = ConsumeToken();
934
935 if (Tok.is(tok::identifier)) {
936 // We appear to have a dependent template name.
937 UnqualifiedId Name;
938 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
939 ConsumeToken(); // the identifier
940
Douglas Gregore9a80352011-01-05 17:33:50 +0000941 // Parse the ellipsis.
942 if (Tok.is(tok::ellipsis))
943 EllipsisLoc = ConsumeToken();
944
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000945 // If the next token signals the end of a template argument,
946 // then we have a dependent template name that could be a template
947 // template argument.
Douglas Gregorbb119652010-06-16 23:00:59 +0000948 TemplateTy Template;
949 if (isEndOfTemplateArgument(Tok) &&
John McCallba7bf592010-08-24 05:47:05 +0000950 Actions.ActOnDependentTemplateName(getCurScope(), TemplateLoc,
951 SS, Name,
952 /*ObjectType=*/ ParsedType(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000953 /*EnteringContext=*/false,
954 Template))
Douglas Gregore9a80352011-01-05 17:33:50 +0000955 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
Douglas Gregorbb119652010-06-16 23:00:59 +0000956 }
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000957 } else if (Tok.is(tok::identifier)) {
958 // We may have a (non-dependent) template name.
959 TemplateTy Template;
960 UnqualifiedId Name;
961 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
962 ConsumeToken(); // the identifier
963
Douglas Gregore9a80352011-01-05 17:33:50 +0000964 // Parse the ellipsis.
965 if (Tok.is(tok::ellipsis))
966 EllipsisLoc = ConsumeToken();
967
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000968 if (isEndOfTemplateArgument(Tok)) {
Douglas Gregor786123d2010-05-21 23:18:07 +0000969 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000970 TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
971 /*hasTemplateKeyword=*/false,
972 Name,
John McCallba7bf592010-08-24 05:47:05 +0000973 /*ObjectType=*/ ParsedType(),
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000974 /*EnteringContext=*/false,
Douglas Gregor786123d2010-05-21 23:18:07 +0000975 Template,
976 MemberOfUnknownSpecialization);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000977 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
978 // We have an id-expression that refers to a class template or
Richard Smith3f1b5d02011-05-05 21:57:07 +0000979 // (C++0x) alias template.
Douglas Gregore9a80352011-01-05 17:33:50 +0000980 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000981 }
982 }
983 }
984
Douglas Gregore9a80352011-01-05 17:33:50 +0000985 // If this is a pack expansion, build it as such.
986 if (EllipsisLoc.isValid() && !Result.isInvalid())
987 Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
988
989 return Result;
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000990}
991
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000992/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
993///
994/// template-argument: [C++ 14.2]
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000995/// constant-expression
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000996/// type-id
997/// id-expression
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000998ParsedTemplateArgument Parser::ParseTemplateArgument() {
Douglas Gregor8bf42052009-02-09 18:46:07 +0000999 // C++ [temp.arg]p2:
1000 // In a template-argument, an ambiguity between a type-id and an
1001 // expression is resolved to a type-id, regardless of the form of
1002 // the corresponding template-parameter.
1003 //
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001004 // Therefore, we initially try to parse a type-id.
Douglas Gregor97f34572009-02-10 00:53:15 +00001005 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001006 SourceLocation Loc = Tok.getLocation();
Douglas Gregor205d5e32011-01-31 16:09:46 +00001007 TypeResult TypeArg = ParseTypeName(/*Range=*/0,
1008 Declarator::TemplateTypeArgContext);
Douglas Gregor220cac52009-02-18 17:45:20 +00001009 if (TypeArg.isInvalid())
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001010 return ParsedTemplateArgument();
1011
John McCallba7bf592010-08-24 05:47:05 +00001012 return ParsedTemplateArgument(ParsedTemplateArgument::Type,
1013 TypeArg.get().getAsOpaquePtr(),
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001014 Loc);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001015 }
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001016
1017 // Try to parse a template template argument.
Douglas Gregorc9984092009-11-12 00:03:40 +00001018 {
1019 TentativeParsingAction TPA(*this);
1020
1021 ParsedTemplateArgument TemplateTemplateArgument
1022 = ParseTemplateTemplateArgument();
1023 if (!TemplateTemplateArgument.isInvalid()) {
1024 TPA.Commit();
1025 return TemplateTemplateArgument;
1026 }
1027
1028 // Revert this tentative parse to parse a non-type template argument.
1029 TPA.Revert();
1030 }
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001031
1032 // Parse a non-type template argument.
1033 SourceLocation Loc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001034 ExprResult ExprArg = ParseConstantExpression();
Douglas Gregord32e0282009-02-09 23:23:08 +00001035 if (ExprArg.isInvalid() || !ExprArg.get())
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001036 return ParsedTemplateArgument();
Douglas Gregor8bf42052009-02-09 18:46:07 +00001037
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001038 return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
1039 ExprArg.release(), Loc);
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001040}
1041
Douglas Gregor786123d2010-05-21 23:18:07 +00001042/// \brief Determine whether the current tokens can only be parsed as a
1043/// template argument list (starting with the '<') and never as a '<'
1044/// expression.
Douglas Gregor20c38a72010-05-21 23:43:39 +00001045bool Parser::IsTemplateArgumentList(unsigned Skip) {
Douglas Gregor786123d2010-05-21 23:18:07 +00001046 struct AlwaysRevertAction : TentativeParsingAction {
1047 AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { }
1048 ~AlwaysRevertAction() { Revert(); }
1049 } Tentative(*this);
1050
Douglas Gregor20c38a72010-05-21 23:43:39 +00001051 while (Skip) {
1052 ConsumeToken();
1053 --Skip;
1054 }
1055
Douglas Gregor786123d2010-05-21 23:18:07 +00001056 // '<'
1057 if (!Tok.is(tok::less))
1058 return false;
1059 ConsumeToken();
1060
1061 // An empty template argument list.
1062 if (Tok.is(tok::greater))
1063 return true;
1064
1065 // See whether we have declaration specifiers, which indicate a type.
1066 while (isCXXDeclarationSpecifier() == TPResult::True())
1067 ConsumeToken();
1068
1069 // If we have a '>' or a ',' then this is a template argument list.
1070 return Tok.is(tok::greater) || Tok.is(tok::comma);
1071}
1072
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001073/// ParseTemplateArgumentList - Parse a C++ template-argument-list
1074/// (C++ [temp.names]). Returns true if there was an error.
1075///
1076/// template-argument-list: [C++ 14.2]
1077/// template-argument
1078/// template-argument-list ',' template-argument
Mike Stump11289f42009-09-09 15:08:12 +00001079bool
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001080Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001081 while (true) {
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001082 ParsedTemplateArgument Arg = ParseTemplateArgument();
Douglas Gregord2fa7662010-12-20 02:24:11 +00001083 if (Tok.is(tok::ellipsis)) {
1084 SourceLocation EllipsisLoc = ConsumeToken();
1085 Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
1086 }
1087
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001088 if (Arg.isInvalid()) {
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001089 SkipUntil(tok::comma, tok::greater, true, true);
1090 return true;
1091 }
Douglas Gregor67b556a2009-02-09 19:34:22 +00001092
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001093 // Save this template argument.
1094 TemplateArgs.push_back(Arg);
1095
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001096 // If the next token is a comma, consume it and keep reading
1097 // arguments.
1098 if (Tok.isNot(tok::comma)) break;
1099
1100 // Consume the comma.
1101 ConsumeToken();
1102 }
1103
Eli Friedmanaffd5fd2009-12-27 22:31:18 +00001104 return false;
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001105}
1106
Mike Stump11289f42009-09-09 15:08:12 +00001107/// \brief Parse a C++ explicit template instantiation
Douglas Gregor23996282009-05-12 21:31:51 +00001108/// (C++ [temp.explicit]).
1109///
1110/// explicit-instantiation:
Douglas Gregor43e75172009-09-04 06:33:52 +00001111/// 'extern' [opt] 'template' declaration
1112///
1113/// Note that the 'extern' is a GNU extension and C++0x feature.
John McCall48871652010-08-21 09:40:31 +00001114Decl *Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
1115 SourceLocation TemplateLoc,
1116 SourceLocation &DeclEnd) {
John McCall796c2a52010-07-16 08:13:16 +00001117 // This isn't really required here.
1118 ParsingDeclRAIIObject ParsingTemplateParams(*this);
1119
Mike Stump11289f42009-09-09 15:08:12 +00001120 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
Douglas Gregor43e75172009-09-04 06:33:52 +00001121 ParsedTemplateInfo(ExternLoc,
1122 TemplateLoc),
John McCall796c2a52010-07-16 08:13:16 +00001123 ParsingTemplateParams,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001124 DeclEnd, AS_none);
Douglas Gregor23996282009-05-12 21:31:51 +00001125}
John McCall9b72f892010-11-10 02:40:36 +00001126
1127SourceRange Parser::ParsedTemplateInfo::getSourceRange() const {
1128 if (TemplateParams)
1129 return getTemplateParamsRange(TemplateParams->data(),
1130 TemplateParams->size());
1131
1132 SourceRange R(TemplateLoc);
1133 if (ExternLoc.isValid())
1134 R.setBegin(ExternLoc);
1135 return R;
1136}
Francois Pichet1c229c02011-04-22 22:18:13 +00001137
Francois Picheta7d337d2011-04-23 11:52:20 +00001138void Parser::LateTemplateParserCallback(void *P, const FunctionDecl *FD) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001139 ((Parser*)P)->LateTemplateParser(FD);
1140}
1141
1142
Francois Picheta7d337d2011-04-23 11:52:20 +00001143void Parser::LateTemplateParser(const FunctionDecl *FD) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001144 LateParsedTemplatedFunction *LPT = LateParsedTemplateMap[FD];
1145 if (LPT) {
1146 ParseLateTemplatedFuncDef(*LPT);
1147 return;
1148 }
1149
1150 llvm_unreachable("Late templated function without associated lexed tokens");
1151}
1152
1153/// \brief Late parse a C++ function template in Microsoft mode.
1154void Parser::ParseLateTemplatedFuncDef(LateParsedTemplatedFunction &LMT) {
1155 if(!LMT.D)
1156 return;
1157
1158 // If this is a member template, introduce the template parameter scope.
1159 ParseScope TemplateScope(this, Scope::TemplateParamScope);
1160
1161 // Get the FunctionDecl.
1162 FunctionDecl *FD = 0;
1163 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(LMT.D))
1164 FD = FunTmpl->getTemplatedDecl();
1165 else
1166 FD = cast<FunctionDecl>(LMT.D);
1167
1168 // Reinject the template parameters.
1169 DeclaratorDecl* Declarator = dyn_cast<DeclaratorDecl>(FD);
1170 if (Declarator && Declarator->getNumTemplateParameterLists() != 0) {
1171 Actions.ActOnReenterDeclaratorTemplateScope(getCurScope(), Declarator);
1172 Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D);
1173 } else {
1174 Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D);
1175
1176 DeclContext *DD = FD->getLexicalParent();
1177 while (DD && DD->isRecord()) {
1178 if (ClassTemplatePartialSpecializationDecl* MD =
1179 dyn_cast_or_null<ClassTemplatePartialSpecializationDecl>(DD))
1180 Actions.ActOnReenterTemplateScope(getCurScope(), MD);
1181 else if (CXXRecordDecl* MD = dyn_cast_or_null<CXXRecordDecl>(DD))
1182 Actions.ActOnReenterTemplateScope(getCurScope(),
1183 MD->getDescribedClassTemplate());
1184
1185 DD = DD->getLexicalParent();
1186 }
1187 }
1188 assert(!LMT.Toks.empty() && "Empty body!");
1189
1190 // Append the current token at the end of the new token stream so that it
1191 // doesn't get lost.
1192 LMT.Toks.push_back(Tok);
1193 PP.EnterTokenStream(LMT.Toks.data(), LMT.Toks.size(), true, false);
1194
1195 // Consume the previously pushed token.
1196 ConsumeAnyToken();
1197 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
1198 && "Inline method not starting with '{', ':' or 'try'");
1199
1200 // Parse the method body. Function body parsing code is similar enough
1201 // to be re-used for method bodies as well.
1202 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
1203
1204 // Recreate the DeclContext.
1205 Sema::ContextRAII SavedContext(Actions, Actions.getContainingDC(FD));
1206
1207 if (FunctionTemplateDecl *FunctionTemplate
1208 = dyn_cast_or_null<FunctionTemplateDecl>(LMT.D))
1209 Actions.ActOnStartOfFunctionDef(getCurScope(),
1210 FunctionTemplate->getTemplatedDecl());
1211 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(LMT.D))
1212 Actions.ActOnStartOfFunctionDef(getCurScope(), Function);
1213
1214
1215 if (Tok.is(tok::kw_try)) {
1216 ParseFunctionTryBlock(LMT.D, FnScope);
1217 return;
1218 }
1219 if (Tok.is(tok::colon)) {
1220 ParseConstructorInitializer(LMT.D);
1221
1222 // Error recovery.
1223 if (!Tok.is(tok::l_brace)) {
1224 Actions.ActOnFinishFunctionBody(LMT.D, 0);
1225 return;
1226 }
1227 } else
1228 Actions.ActOnDefaultCtorInitializers(LMT.D);
1229
1230 ParseFunctionStatementBody(LMT.D, FnScope);
1231 Actions.MarkAsLateParsedTemplate(FD, false);
1232
1233 DeclGroupPtrTy grp = Actions.ConvertDeclToDeclGroup(LMT.D);
1234 if (grp)
1235 Actions.getASTConsumer().HandleTopLevelDecl(grp.get());
1236}
1237
1238/// \brief Lex a delayed template function for late parsing.
1239void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
1240 tok::TokenKind kind = Tok.getKind();
1241 // We may have a constructor initializer or function-try-block here.
1242 if (kind == tok::colon || kind == tok::kw_try)
1243 ConsumeAndStoreUntil(tok::l_brace, Toks);
1244 else {
1245 Toks.push_back(Tok);
1246 ConsumeBrace();
1247 }
1248 // Consume everything up to (and including) the matching right brace.
1249 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1250
1251 // If we're in a function-try-block, we need to store all the catch blocks.
1252 if (kind == tok::kw_try) {
1253 while (Tok.is(tok::kw_catch)) {
1254 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1255 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1256 }
1257 }
1258}