blob: 4fcd8b346c73adaef3da4493c175405ac4f8b1c4 [file] [log] [blame]
Faisal Vali6a79ca12013-06-08 19:39: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"
15#include "RAIIObjectsForParser.h"
16#include "clang/AST/ASTConsumer.h"
Richard Smithb0b68012015-05-11 23:09:06 +000017#include "clang/AST/ASTContext.h"
Faisal Vali6a79ca12013-06-08 19:39:00 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/Parse/ParseDiagnostic.h"
20#include "clang/Sema/DeclSpec.h"
21#include "clang/Sema/ParsedTemplate.h"
22#include "clang/Sema/Scope.h"
23using namespace clang;
24
25/// \brief Parse a template declaration, explicit instantiation, or
26/// explicit specialization.
27Decl *
28Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
29 SourceLocation &DeclEnd,
30 AccessSpecifier AS,
31 AttributeList *AccessAttrs) {
32 ObjCDeclContextSwitch ObjCDC(*this);
33
34 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
35 return ParseExplicitInstantiation(Context,
36 SourceLocation(), ConsumeToken(),
37 DeclEnd, AS);
38 }
39 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS,
40 AccessAttrs);
41}
42
43
44
45/// \brief Parse a template declaration or an explicit specialization.
46///
47/// Template declarations include one or more template parameter lists
48/// and either the function or class template declaration. Explicit
49/// specializations contain one or more 'template < >' prefixes
50/// followed by a (possibly templated) declaration. Since the
51/// syntactic form of both features is nearly identical, we parse all
52/// of the template headers together and let semantic analysis sort
53/// the declarations from the explicit specializations.
54///
55/// template-declaration: [C++ temp]
56/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
57///
58/// explicit-specialization: [ C++ temp.expl.spec]
59/// 'template' '<' '>' declaration
60Decl *
61Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
62 SourceLocation &DeclEnd,
63 AccessSpecifier AS,
64 AttributeList *AccessAttrs) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +000065 assert(Tok.isOneOf(tok::kw_export, tok::kw_template) &&
Faisal Vali6a79ca12013-06-08 19:39:00 +000066 "Token does not start a template declaration.");
67
68 // Enter template-parameter scope.
69 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
70
71 // Tell the action that names should be checked in the context of
72 // the declaration to come.
73 ParsingDeclRAIIObject
74 ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
75
76 // Parse multiple levels of template headers within this template
77 // parameter scope, e.g.,
78 //
79 // template<typename T>
80 // template<typename U>
81 // class A<T>::B { ... };
82 //
83 // We parse multiple levels non-recursively so that we can build a
84 // single data structure containing all of the template parameter
85 // lists to easily differentiate between the case above and:
86 //
87 // template<typename T>
88 // class A {
89 // template<typename U> class B;
90 // };
91 //
92 // In the first case, the action for declaring A<T>::B receives
93 // both template parameter lists. In the second case, the action for
94 // defining A<T>::B receives just the inner template parameter list
95 // (and retrieves the outer template parameter list from its
96 // context).
97 bool isSpecialization = true;
98 bool LastParamListWasEmpty = false;
99 TemplateParameterLists ParamLists;
100 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
101
102 do {
103 // Consume the 'export', if any.
104 SourceLocation ExportLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000105 TryConsumeToken(tok::kw_export, ExportLoc);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000106
107 // Consume the 'template', which should be here.
108 SourceLocation TemplateLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000109 if (!TryConsumeToken(tok::kw_template, TemplateLoc)) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000110 Diag(Tok.getLocation(), diag::err_expected_template);
Craig Topper161e4db2014-05-21 06:02:52 +0000111 return nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000112 }
113
114 // Parse the '<' template-parameter-list '>'
115 SourceLocation LAngleLoc, RAngleLoc;
116 SmallVector<Decl*, 4> TemplateParams;
117 if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(),
118 TemplateParams, LAngleLoc, RAngleLoc)) {
Hubert Tongec3cb572015-06-25 00:23:39 +0000119 // Skip until the semi-colon or a '}'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000120 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000121 TryConsumeToken(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000122 return nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000123 }
124
125 ParamLists.push_back(
126 Actions.ActOnTemplateParameterList(CurTemplateDepthTracker.getDepth(),
127 ExportLoc,
128 TemplateLoc, LAngleLoc,
129 TemplateParams.data(),
130 TemplateParams.size(), RAngleLoc));
131
132 if (!TemplateParams.empty()) {
133 isSpecialization = false;
134 ++CurTemplateDepthTracker;
Hubert Tongec3cb572015-06-25 00:23:39 +0000135
136 if (TryConsumeToken(tok::kw_requires)) {
137 ExprResult ER =
138 Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression());
139 if (!ER.isUsable()) {
140 // Skip until the semi-colon or a '}'.
141 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
142 TryConsumeToken(tok::semi);
143 return nullptr;
144 }
145 }
Faisal Vali6a79ca12013-06-08 19:39:00 +0000146 } else {
147 LastParamListWasEmpty = true;
148 }
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000149 } while (Tok.isOneOf(tok::kw_export, tok::kw_template));
Faisal Vali6a79ca12013-06-08 19:39:00 +0000150
151 // Parse the actual template declaration.
152 return ParseSingleDeclarationAfterTemplate(Context,
153 ParsedTemplateInfo(&ParamLists,
154 isSpecialization,
155 LastParamListWasEmpty),
156 ParsingTemplateParams,
157 DeclEnd, AS, AccessAttrs);
158}
159
160/// \brief Parse a single declaration that declares a template,
161/// template specialization, or explicit instantiation of a template.
162///
163/// \param DeclEnd will receive the source location of the last token
164/// within this declaration.
165///
166/// \param AS the access specifier associated with this
167/// declaration. Will be AS_none for namespace-scope declarations.
168///
169/// \returns the new declaration.
170Decl *
171Parser::ParseSingleDeclarationAfterTemplate(
172 unsigned Context,
173 const ParsedTemplateInfo &TemplateInfo,
174 ParsingDeclRAIIObject &DiagsFromTParams,
175 SourceLocation &DeclEnd,
176 AccessSpecifier AS,
177 AttributeList *AccessAttrs) {
178 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
179 "Template information required");
180
Aaron Ballmane7c544d2014-08-04 20:28:35 +0000181 if (Tok.is(tok::kw_static_assert)) {
182 // A static_assert declaration may not be templated.
183 Diag(Tok.getLocation(), diag::err_templated_invalid_declaration)
184 << TemplateInfo.getSourceRange();
185 // Parse the static_assert declaration to improve error recovery.
186 return ParseStaticAssertDeclaration(DeclEnd);
187 }
188
Faisal Vali6a79ca12013-06-08 19:39:00 +0000189 if (Context == Declarator::MemberContext) {
190 // We are parsing a member template.
191 ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
192 &DiagsFromTParams);
Craig Topper161e4db2014-05-21 06:02:52 +0000193 return nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000194 }
195
196 ParsedAttributesWithRange prefixAttrs(AttrFactory);
197 MaybeParseCXX11Attributes(prefixAttrs);
198
199 if (Tok.is(tok::kw_using))
200 return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
201 prefixAttrs);
202
203 // Parse the declaration specifiers, stealing any diagnostics from
204 // the template parameters.
205 ParsingDeclSpec DS(*this, &DiagsFromTParams);
206
207 ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
208 getDeclSpecContextFromDeclaratorContext(Context));
209
210 if (Tok.is(tok::semi)) {
211 ProhibitAttributes(prefixAttrs);
212 DeclEnd = ConsumeToken();
213 Decl *Decl = Actions.ParsedFreeStandingDeclSpec(
214 getCurScope(), AS, DS,
215 TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams
216 : MultiTemplateParamsArg(),
217 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation);
218 DS.complete(Decl);
219 return Decl;
220 }
221
222 // Move the attributes from the prefix into the DS.
223 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
224 ProhibitAttributes(prefixAttrs);
225 else
226 DS.takeAttributesFrom(prefixAttrs);
227
228 // Parse the declarator.
229 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
230 ParseDeclarator(DeclaratorInfo);
231 // Error parsing the declarator?
232 if (!DeclaratorInfo.hasName()) {
233 // If so, skip until the semi-colon or a }.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000234 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000235 if (Tok.is(tok::semi))
236 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +0000237 return nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000238 }
239
240 LateParsedAttrList LateParsedAttrs(true);
241 if (DeclaratorInfo.isFunctionDeclarator())
242 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
243
244 if (DeclaratorInfo.isFunctionDeclarator() &&
245 isStartOfFunctionDefinition(DeclaratorInfo)) {
Reid Klecknerd61a3112014-12-15 23:16:32 +0000246
247 // Function definitions are only allowed at file scope and in C++ classes.
248 // The C++ inline method definition case is handled elsewhere, so we only
249 // need to handle the file scope definition case.
250 if (Context != Declarator::FileContext) {
251 Diag(Tok, diag::err_function_definition_not_allowed);
252 SkipMalformedDecl();
253 return nullptr;
254 }
255
Faisal Vali6a79ca12013-06-08 19:39:00 +0000256 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
257 // Recover by ignoring the 'typedef'. This was probably supposed to be
258 // the 'typename' keyword, which we should have already suggested adding
259 // if it's appropriate.
260 Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef)
261 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
262 DS.ClearStorageClassSpecs();
263 }
Larisse Voufo725de3e2013-06-21 00:08:46 +0000264
265 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
266 if (DeclaratorInfo.getName().getKind() != UnqualifiedId::IK_TemplateId) {
267 // If the declarator-id is not a template-id, issue a diagnostic and
268 // recover by ignoring the 'template' keyword.
269 Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000270 return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(),
271 &LateParsedAttrs);
Larisse Voufo725de3e2013-06-21 00:08:46 +0000272 } else {
273 SourceLocation LAngleLoc
274 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000275 Diag(DeclaratorInfo.getIdentifierLoc(),
Larisse Voufo725de3e2013-06-21 00:08:46 +0000276 diag::err_explicit_instantiation_with_definition)
Larisse Voufo39a1e502013-08-06 01:03:05 +0000277 << SourceRange(TemplateInfo.TemplateLoc)
278 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Larisse Voufo725de3e2013-06-21 00:08:46 +0000279
Larisse Voufo39a1e502013-08-06 01:03:05 +0000280 // Recover as if it were an explicit specialization.
Larisse Voufob9bbaba2013-06-22 13:56:11 +0000281 TemplateParameterLists FakedParamLists;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000282 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper161e4db2014-05-21 06:02:52 +0000283 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
284 0, LAngleLoc));
Larisse Voufo725de3e2013-06-21 00:08:46 +0000285
Larisse Voufo39a1e502013-08-06 01:03:05 +0000286 return ParseFunctionDefinition(
287 DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists,
288 /*isSpecialization=*/true,
289 /*LastParamListWasEmpty=*/true),
290 &LateParsedAttrs);
Larisse Voufo725de3e2013-06-21 00:08:46 +0000291 }
292 }
Faisal Vali6a79ca12013-06-08 19:39:00 +0000293 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo,
Larisse Voufo39a1e502013-08-06 01:03:05 +0000294 &LateParsedAttrs);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000295 }
296
297 // Parse this declaration.
298 Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
299 TemplateInfo);
300
301 if (Tok.is(tok::comma)) {
302 Diag(Tok, diag::err_multiple_template_declarators)
303 << (int)TemplateInfo.Kind;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000304 SkipUntil(tok::semi);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000305 return ThisDecl;
306 }
307
308 // Eat the semi colon after the declaration.
309 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
310 if (LateParsedAttrs.size() > 0)
311 ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false);
312 DeclaratorInfo.complete(ThisDecl);
313 return ThisDecl;
314}
315
316/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
317/// angle brackets. Depth is the depth of this template-parameter-list, which
318/// is the number of template headers directly enclosing this template header.
319/// TemplateParams is the current list of template parameters we're building.
320/// The template parameter we parse will be added to this list. LAngleLoc and
321/// RAngleLoc will receive the positions of the '<' and '>', respectively,
322/// that enclose this template parameter list.
323///
324/// \returns true if an error occurred, false otherwise.
325bool Parser::ParseTemplateParameters(unsigned Depth,
326 SmallVectorImpl<Decl*> &TemplateParams,
327 SourceLocation &LAngleLoc,
328 SourceLocation &RAngleLoc) {
329 // Get the template parameter list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000330 if (!TryConsumeToken(tok::less, LAngleLoc)) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000331 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
332 return true;
333 }
Faisal Vali6a79ca12013-06-08 19:39:00 +0000334
335 // Try to parse the template parameter list.
336 bool Failed = false;
337 if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater))
338 Failed = ParseTemplateParameterList(Depth, TemplateParams);
339
340 if (Tok.is(tok::greatergreater)) {
341 // No diagnostic required here: a template-parameter-list can only be
342 // followed by a declaration or, for a template template parameter, the
343 // 'class' keyword. Therefore, the second '>' will be diagnosed later.
344 // This matters for elegant diagnosis of:
345 // template<template<typename>> struct S;
346 Tok.setKind(tok::greater);
347 RAngleLoc = Tok.getLocation();
348 Tok.setLocation(Tok.getLocation().getLocWithOffset(1));
Alp Toker383d2c42014-01-01 03:08:43 +0000349 } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) {
350 Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000351 return true;
352 }
353 return false;
354}
355
356/// ParseTemplateParameterList - Parse a template parameter list. If
357/// the parsing fails badly (i.e., closing bracket was left out), this
358/// will try to put the token stream in a reasonable position (closing
359/// a statement, etc.) and return false.
360///
361/// template-parameter-list: [C++ temp]
362/// template-parameter
363/// template-parameter-list ',' template-parameter
364bool
365Parser::ParseTemplateParameterList(unsigned Depth,
366 SmallVectorImpl<Decl*> &TemplateParams) {
367 while (1) {
368 if (Decl *TmpParam
369 = ParseTemplateParameter(Depth, TemplateParams.size())) {
370 TemplateParams.push_back(TmpParam);
371 } else {
372 // If we failed to parse a template parameter, skip until we find
373 // a comma or closing brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000374 SkipUntil(tok::comma, tok::greater, tok::greatergreater,
375 StopAtSemi | StopBeforeMatch);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000376 }
377
378 // Did we find a comma or the end of the template parameter list?
379 if (Tok.is(tok::comma)) {
380 ConsumeToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000381 } else if (Tok.isOneOf(tok::greater, tok::greatergreater)) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000382 // Don't consume this... that's done by template parser.
383 break;
384 } else {
385 // Somebody probably forgot to close the template. Skip ahead and
386 // try to get out of the expression. This error is currently
387 // subsumed by whatever goes on in ParseTemplateParameter.
388 Diag(Tok.getLocation(), diag::err_expected_comma_greater);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000389 SkipUntil(tok::comma, tok::greater, tok::greatergreater,
390 StopAtSemi | StopBeforeMatch);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000391 return false;
392 }
393 }
394 return true;
395}
396
397/// \brief Determine whether the parser is at the start of a template
398/// type parameter.
399bool Parser::isStartOfTemplateTypeParameter() {
400 if (Tok.is(tok::kw_class)) {
401 // "class" may be the start of an elaborated-type-specifier or a
402 // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter.
403 switch (NextToken().getKind()) {
404 case tok::equal:
405 case tok::comma:
406 case tok::greater:
407 case tok::greatergreater:
408 case tok::ellipsis:
409 return true;
410
411 case tok::identifier:
412 // This may be either a type-parameter or an elaborated-type-specifier.
413 // We have to look further.
414 break;
415
416 default:
417 return false;
418 }
419
420 switch (GetLookAheadToken(2).getKind()) {
421 case tok::equal:
422 case tok::comma:
423 case tok::greater:
424 case tok::greatergreater:
425 return true;
426
427 default:
428 return false;
429 }
430 }
431
432 if (Tok.isNot(tok::kw_typename))
433 return false;
434
435 // C++ [temp.param]p2:
436 // There is no semantic difference between class and typename in a
437 // template-parameter. typename followed by an unqualified-id
438 // names a template type parameter. typename followed by a
439 // qualified-id denotes the type in a non-type
440 // parameter-declaration.
441 Token Next = NextToken();
442
443 // If we have an identifier, skip over it.
444 if (Next.getKind() == tok::identifier)
445 Next = GetLookAheadToken(2);
446
447 switch (Next.getKind()) {
448 case tok::equal:
449 case tok::comma:
450 case tok::greater:
451 case tok::greatergreater:
452 case tok::ellipsis:
453 return true;
454
455 default:
456 return false;
457 }
458}
459
460/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
461///
462/// template-parameter: [C++ temp.param]
463/// type-parameter
464/// parameter-declaration
465///
466/// type-parameter: (see below)
467/// 'class' ...[opt] identifier[opt]
468/// 'class' identifier[opt] '=' type-id
469/// 'typename' ...[opt] identifier[opt]
470/// 'typename' identifier[opt] '=' type-id
471/// 'template' '<' template-parameter-list '>'
472/// 'class' ...[opt] identifier[opt]
473/// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
474/// = id-expression
475Decl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
476 if (isStartOfTemplateTypeParameter())
477 return ParseTypeParameter(Depth, Position);
478
479 if (Tok.is(tok::kw_template))
480 return ParseTemplateTemplateParameter(Depth, Position);
481
482 // If it's none of the above, then it must be a parameter declaration.
483 // NOTE: This will pick up errors in the closure of the template parameter
484 // list (e.g., template < ; Check here to implement >> style closures.
485 return ParseNonTypeTemplateParameter(Depth, Position);
486}
487
488/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
489/// Other kinds of template parameters are parsed in
490/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
491///
492/// type-parameter: [C++ temp.param]
493/// 'class' ...[opt][C++0x] identifier[opt]
494/// 'class' identifier[opt] '=' type-id
495/// 'typename' ...[opt][C++0x] identifier[opt]
496/// 'typename' identifier[opt] '=' type-id
497Decl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000498 assert(Tok.isOneOf(tok::kw_class, tok::kw_typename) &&
Faisal Vali6a79ca12013-06-08 19:39:00 +0000499 "A type-parameter starts with 'class' or 'typename'");
500
501 // Consume the 'class' or 'typename' keyword.
502 bool TypenameKeyword = Tok.is(tok::kw_typename);
503 SourceLocation KeyLoc = ConsumeToken();
504
505 // Grab the ellipsis (if given).
Faisal Vali6a79ca12013-06-08 19:39:00 +0000506 SourceLocation EllipsisLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000507 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000508 Diag(EllipsisLoc,
509 getLangOpts().CPlusPlus11
510 ? diag::warn_cxx98_compat_variadic_templates
511 : diag::ext_variadic_templates);
512 }
513
514 // Grab the template parameter name (if given)
515 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000516 IdentifierInfo *ParamName = nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000517 if (Tok.is(tok::identifier)) {
518 ParamName = Tok.getIdentifierInfo();
519 NameLoc = ConsumeToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000520 } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
521 tok::greatergreater)) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000522 // Unnamed template parameter. Don't have to do anything here, just
523 // don't consume this token.
524 } else {
Alp Tokerec543272013-12-24 09:48:30 +0000525 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +0000526 return nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000527 }
528
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +0000529 // Recover from misplaced ellipsis.
530 bool AlreadyHasEllipsis = EllipsisLoc.isValid();
531 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
532 DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
533
Faisal Vali6a79ca12013-06-08 19:39:00 +0000534 // Grab a default argument (if available).
535 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
536 // we introduce the type parameter into the local scope.
537 SourceLocation EqualLoc;
538 ParsedType DefaultArg;
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000539 if (TryConsumeToken(tok::equal, EqualLoc))
Craig Topper161e4db2014-05-21 06:02:52 +0000540 DefaultArg = ParseTypeName(/*Range=*/nullptr,
Faisal Vali6a79ca12013-06-08 19:39:00 +0000541 Declarator::TemplateTypeArgContext).get();
Faisal Vali6a79ca12013-06-08 19:39:00 +0000542
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +0000543 return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, EllipsisLoc,
544 KeyLoc, ParamName, NameLoc, Depth, Position,
545 EqualLoc, DefaultArg);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000546}
547
548/// ParseTemplateTemplateParameter - Handle the parsing of template
549/// template parameters.
550///
551/// type-parameter: [C++ temp.param]
Richard Smith78e1ca62014-06-16 15:51:22 +0000552/// 'template' '<' template-parameter-list '>' type-parameter-key
Faisal Vali6a79ca12013-06-08 19:39:00 +0000553/// ...[opt] identifier[opt]
Richard Smith78e1ca62014-06-16 15:51:22 +0000554/// 'template' '<' template-parameter-list '>' type-parameter-key
555/// identifier[opt] = id-expression
556/// type-parameter-key:
557/// 'class'
558/// 'typename' [C++1z]
Faisal Vali6a79ca12013-06-08 19:39:00 +0000559Decl *
560Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
561 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
562
563 // Handle the template <...> part.
564 SourceLocation TemplateLoc = ConsumeToken();
565 SmallVector<Decl*,8> TemplateParams;
566 SourceLocation LAngleLoc, RAngleLoc;
567 {
568 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
569 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
570 RAngleLoc)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000571 return nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000572 }
573 }
574
Richard Smith78e1ca62014-06-16 15:51:22 +0000575 // Provide an ExtWarn if the C++1z feature of using 'typename' here is used.
Faisal Vali6a79ca12013-06-08 19:39:00 +0000576 // Generate a meaningful error if the user forgot to put class before the
577 // identifier, comma, or greater. Provide a fixit if the identifier, comma,
Richard Smith78e1ca62014-06-16 15:51:22 +0000578 // or greater appear immediately or after 'struct'. In the latter case,
579 // replace the keyword with 'class'.
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000580 if (!TryConsumeToken(tok::kw_class)) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000581 bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct);
Richard Smith78e1ca62014-06-16 15:51:22 +0000582 const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok;
583 if (Tok.is(tok::kw_typename)) {
584 Diag(Tok.getLocation(),
585 getLangOpts().CPlusPlus1z
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000586 ? diag::warn_cxx14_compat_template_template_param_typename
Richard Smith78e1ca62014-06-16 15:51:22 +0000587 : diag::ext_template_template_param_typename)
588 << (!getLangOpts().CPlusPlus1z
589 ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
590 : FixItHint());
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000591 } else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater,
592 tok::greatergreater, tok::ellipsis)) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000593 Diag(Tok.getLocation(), diag::err_class_on_template_template_param)
594 << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
595 : FixItHint::CreateInsertion(Tok.getLocation(), "class "));
Richard Smith78e1ca62014-06-16 15:51:22 +0000596 } else
Faisal Vali6a79ca12013-06-08 19:39:00 +0000597 Diag(Tok.getLocation(), diag::err_class_on_template_template_param);
598
599 if (Replace)
600 ConsumeToken();
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000601 }
Faisal Vali6a79ca12013-06-08 19:39:00 +0000602
603 // Parse the ellipsis, if given.
604 SourceLocation EllipsisLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000605 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Faisal Vali6a79ca12013-06-08 19:39:00 +0000606 Diag(EllipsisLoc,
607 getLangOpts().CPlusPlus11
608 ? diag::warn_cxx98_compat_variadic_templates
609 : diag::ext_variadic_templates);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000610
611 // Get the identifier, if given.
612 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000613 IdentifierInfo *ParamName = nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000614 if (Tok.is(tok::identifier)) {
615 ParamName = Tok.getIdentifierInfo();
616 NameLoc = ConsumeToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000617 } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
618 tok::greatergreater)) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000619 // Unnamed template parameter. Don't have to do anything here, just
620 // don't consume this token.
621 } else {
Alp Tokerec543272013-12-24 09:48:30 +0000622 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +0000623 return nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000624 }
625
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +0000626 // Recover from misplaced ellipsis.
627 bool AlreadyHasEllipsis = EllipsisLoc.isValid();
628 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
629 DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
630
Faisal Vali6a79ca12013-06-08 19:39:00 +0000631 TemplateParameterList *ParamList =
632 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
633 TemplateLoc, LAngleLoc,
634 TemplateParams.data(),
635 TemplateParams.size(),
636 RAngleLoc);
637
638 // Grab a default argument (if available).
639 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
640 // we introduce the template parameter into the local scope.
641 SourceLocation EqualLoc;
642 ParsedTemplateArgument DefaultArg;
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000643 if (TryConsumeToken(tok::equal, EqualLoc)) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000644 DefaultArg = ParseTemplateTemplateArgument();
645 if (DefaultArg.isInvalid()) {
646 Diag(Tok.getLocation(),
647 diag::err_default_template_template_parameter_not_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000648 SkipUntil(tok::comma, tok::greater, tok::greatergreater,
649 StopAtSemi | StopBeforeMatch);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000650 }
651 }
652
653 return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
654 ParamList, EllipsisLoc,
655 ParamName, NameLoc, Depth,
656 Position, EqualLoc, DefaultArg);
657}
658
659/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
660/// template parameters (e.g., in "template<int Size> class array;").
661///
662/// template-parameter:
663/// ...
664/// parameter-declaration
665Decl *
666Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
667 // Parse the declaration-specifiers (i.e., the type).
668 // FIXME: The type should probably be restricted in some way... Not all
669 // declarators (parts of declarators?) are accepted for parameters.
670 DeclSpec DS(AttrFactory);
671 ParseDeclarationSpecifiers(DS);
672
673 // Parse this as a typename.
674 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
675 ParseDeclarator(ParamDecl);
676 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
677 Diag(Tok.getLocation(), diag::err_expected_template_parameter);
Craig Topper161e4db2014-05-21 06:02:52 +0000678 return nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000679 }
680
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +0000681 // Recover from misplaced ellipsis.
682 SourceLocation EllipsisLoc;
683 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
684 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl);
685
Faisal Vali6a79ca12013-06-08 19:39:00 +0000686 // If there is a default value, parse it.
687 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
688 // we introduce the template parameter into the local scope.
689 SourceLocation EqualLoc;
690 ExprResult DefaultArg;
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000691 if (TryConsumeToken(tok::equal, EqualLoc)) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000692 // C++ [temp.param]p15:
693 // When parsing a default template-argument for a non-type
694 // template-parameter, the first non-nested > is taken as the
695 // end of the template-parameter-list rather than a greater-than
696 // operator.
697 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
Faisal Vali48401eb2015-11-19 19:20:17 +0000698 EnterExpressionEvaluationContext ConstantEvaluated(Actions,
699 Sema::ConstantEvaluated);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000700
Kaelyn Takata999dd852014-12-02 23:32:20 +0000701 DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Faisal Vali6a79ca12013-06-08 19:39:00 +0000702 if (DefaultArg.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +0000703 SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000704 }
705
706 // Create the parameter.
707 return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
708 Depth, Position, EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000709 DefaultArg.get());
Faisal Vali6a79ca12013-06-08 19:39:00 +0000710}
711
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +0000712void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,
713 SourceLocation CorrectLoc,
714 bool AlreadyHasEllipsis,
715 bool IdentifierHasName) {
716 FixItHint Insertion;
717 if (!AlreadyHasEllipsis)
718 Insertion = FixItHint::CreateInsertion(CorrectLoc, "...");
719 Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
720 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion
721 << !IdentifierHasName;
722}
723
724void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,
725 Declarator &D) {
726 assert(EllipsisLoc.isValid());
727 bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid();
728 if (!AlreadyHasEllipsis)
729 D.setEllipsisLoc(EllipsisLoc);
730 DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(),
731 AlreadyHasEllipsis, D.hasName());
732}
733
Faisal Vali6a79ca12013-06-08 19:39:00 +0000734/// \brief Parses a '>' at the end of a template list.
735///
736/// If this function encounters '>>', '>>>', '>=', or '>>=', it tries
737/// to determine if these tokens were supposed to be a '>' followed by
738/// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary.
739///
740/// \param RAngleLoc the location of the consumed '>'.
741///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000742/// \param ConsumeLastToken if true, the '>' is consumed.
743///
744/// \param ObjCGenericList if true, this is the '>' closing an Objective-C
745/// type parameter or type argument list, rather than a C++ template parameter
746/// or argument list.
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000747///
748/// \returns true, if current token does not start with '>', false otherwise.
Faisal Vali6a79ca12013-06-08 19:39:00 +0000749bool Parser::ParseGreaterThanInTemplateList(SourceLocation &RAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000750 bool ConsumeLastToken,
751 bool ObjCGenericList) {
Faisal Vali6a79ca12013-06-08 19:39:00 +0000752 // What will be left once we've consumed the '>'.
753 tok::TokenKind RemainingToken;
754 const char *ReplacementStr = "> >";
755
756 switch (Tok.getKind()) {
757 default:
Alp Toker383d2c42014-01-01 03:08:43 +0000758 Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
Faisal Vali6a79ca12013-06-08 19:39:00 +0000759 return true;
760
761 case tok::greater:
762 // Determine the location of the '>' token. Only consume this token
763 // if the caller asked us to.
764 RAngleLoc = Tok.getLocation();
765 if (ConsumeLastToken)
766 ConsumeToken();
767 return false;
768
769 case tok::greatergreater:
770 RemainingToken = tok::greater;
771 break;
772
773 case tok::greatergreatergreater:
774 RemainingToken = tok::greatergreater;
775 break;
776
777 case tok::greaterequal:
778 RemainingToken = tok::equal;
779 ReplacementStr = "> =";
780 break;
781
782 case tok::greatergreaterequal:
783 RemainingToken = tok::greaterequal;
784 break;
785 }
786
787 // This template-id is terminated by a token which starts with a '>'. Outside
788 // C++11, this is now error recovery, and in C++11, this is error recovery if
Eli Bendersky36a61932014-06-20 13:09:59 +0000789 // the token isn't '>>' or '>>>'.
790 // '>>>' is for CUDA, where this sequence of characters is parsed into
791 // tok::greatergreatergreater, rather than two separate tokens.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000792 //
793 // We always allow this for Objective-C type parameter and type argument
794 // lists.
Faisal Vali6a79ca12013-06-08 19:39:00 +0000795 RAngleLoc = Tok.getLocation();
Faisal Vali6a79ca12013-06-08 19:39:00 +0000796 Token Next = NextToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000797 if (!ObjCGenericList) {
798 // The source range of the '>>' or '>=' at the start of the token.
799 CharSourceRange ReplacementRange =
800 CharSourceRange::getCharRange(RAngleLoc,
801 Lexer::AdvanceToTokenCharacter(RAngleLoc, 2, PP.getSourceManager(),
802 getLangOpts()));
Faisal Vali6a79ca12013-06-08 19:39:00 +0000803
Douglas Gregor85f3f952015-07-07 03:57:15 +0000804 // A hint to put a space between the '>>'s. In order to make the hint as
805 // clear as possible, we include the characters either side of the space in
806 // the replacement, rather than just inserting a space at SecondCharLoc.
807 FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange,
808 ReplacementStr);
809
810 // A hint to put another space after the token, if it would otherwise be
811 // lexed differently.
812 FixItHint Hint2;
813 if ((RemainingToken == tok::greater ||
814 RemainingToken == tok::greatergreater) &&
815 (Next.isOneOf(tok::greater, tok::greatergreater,
816 tok::greatergreatergreater, tok::equal,
817 tok::greaterequal, tok::greatergreaterequal,
818 tok::equalequal)) &&
819 areTokensAdjacent(Tok, Next))
820 Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " ");
821
822 unsigned DiagId = diag::err_two_right_angle_brackets_need_space;
823 if (getLangOpts().CPlusPlus11 &&
824 (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater)))
825 DiagId = diag::warn_cxx98_compat_two_right_angle_brackets;
826 else if (Tok.is(tok::greaterequal))
827 DiagId = diag::err_right_angle_bracket_equal_needs_space;
828 Diag(Tok.getLocation(), DiagId) << Hint1 << Hint2;
829 }
Faisal Vali6a79ca12013-06-08 19:39:00 +0000830
831 // Strip the initial '>' from the token.
832 if (RemainingToken == tok::equal && Next.is(tok::equal) &&
833 areTokensAdjacent(Tok, Next)) {
834 // Join two adjacent '=' tokens into one, for cases like:
835 // void (*p)() = f<int>;
836 // return f<int>==p;
837 ConsumeToken();
838 Tok.setKind(tok::equalequal);
839 Tok.setLength(Tok.getLength() + 1);
840 } else {
841 Tok.setKind(RemainingToken);
842 Tok.setLength(Tok.getLength() - 1);
843 }
844 Tok.setLocation(Lexer::AdvanceToTokenCharacter(RAngleLoc, 1,
845 PP.getSourceManager(),
846 getLangOpts()));
847
848 if (!ConsumeLastToken) {
849 // Since we're not supposed to consume the '>' token, we need to push
850 // this token and revert the current token back to the '>'.
851 PP.EnterToken(Tok);
852 Tok.setKind(tok::greater);
853 Tok.setLength(1);
854 Tok.setLocation(RAngleLoc);
855 }
856 return false;
857}
858
859
860/// \brief Parses a template-id that after the template name has
861/// already been parsed.
862///
863/// This routine takes care of parsing the enclosed template argument
864/// list ('<' template-parameter-list [opt] '>') and placing the
865/// results into a form that can be transferred to semantic analysis.
866///
867/// \param Template the template declaration produced by isTemplateName
868///
869/// \param TemplateNameLoc the source location of the template name
870///
871/// \param SS if non-NULL, the nested-name-specifier preceding the
872/// template name.
873///
874/// \param ConsumeLastToken if true, then we will consume the last
875/// token that forms the template-id. Otherwise, we will leave the
876/// last token in the stream (e.g., so that it can be replaced with an
877/// annotation token).
878bool
879Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
880 SourceLocation TemplateNameLoc,
881 const CXXScopeSpec &SS,
882 bool ConsumeLastToken,
883 SourceLocation &LAngleLoc,
884 TemplateArgList &TemplateArgs,
885 SourceLocation &RAngleLoc) {
886 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
887
888 // Consume the '<'.
889 LAngleLoc = ConsumeToken();
890
891 // Parse the optional template-argument-list.
892 bool Invalid = false;
893 {
894 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
895 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
896 Invalid = ParseTemplateArgumentList(TemplateArgs);
897
898 if (Invalid) {
899 // Try to find the closing '>'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000900 if (ConsumeLastToken)
901 SkipUntil(tok::greater, StopAtSemi);
902 else
903 SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000904 return true;
905 }
906 }
907
Douglas Gregor85f3f952015-07-07 03:57:15 +0000908 return ParseGreaterThanInTemplateList(RAngleLoc, ConsumeLastToken,
909 /*ObjCGenericList=*/false);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000910}
911
912/// \brief Replace the tokens that form a simple-template-id with an
913/// annotation token containing the complete template-id.
914///
915/// The first token in the stream must be the name of a template that
916/// is followed by a '<'. This routine will parse the complete
917/// simple-template-id and replace the tokens with a single annotation
918/// token with one of two different kinds: if the template-id names a
919/// type (and \p AllowTypeAnnotation is true), the annotation token is
920/// a type annotation that includes the optional nested-name-specifier
921/// (\p SS). Otherwise, the annotation token is a template-id
922/// annotation that does not include the optional
923/// nested-name-specifier.
924///
925/// \param Template the declaration of the template named by the first
926/// token (an identifier), as returned from \c Action::isTemplateName().
927///
928/// \param TNK the kind of template that \p Template
929/// refers to, as returned from \c Action::isTemplateName().
930///
931/// \param SS if non-NULL, the nested-name-specifier that precedes
932/// this template name.
933///
934/// \param TemplateKWLoc if valid, specifies that this template-id
935/// annotation was preceded by the 'template' keyword and gives the
936/// location of that keyword. If invalid (the default), then this
937/// template-id was not preceded by a 'template' keyword.
938///
939/// \param AllowTypeAnnotation if true (the default), then a
940/// simple-template-id that refers to a class template, template
941/// template parameter, or other template that produces a type will be
942/// replaced with a type annotation token. Otherwise, the
943/// simple-template-id is always replaced with a template-id
944/// annotation token.
945///
946/// If an unrecoverable parse error occurs and no annotation token can be
947/// formed, this function returns true.
948///
949bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
950 CXXScopeSpec &SS,
951 SourceLocation TemplateKWLoc,
952 UnqualifiedId &TemplateName,
953 bool AllowTypeAnnotation) {
954 assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++");
955 assert(Template && Tok.is(tok::less) &&
956 "Parser isn't at the beginning of a template-id");
957
958 // Consume the template-name.
959 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
960
961 // Parse the enclosed template argument list.
962 SourceLocation LAngleLoc, RAngleLoc;
963 TemplateArgList TemplateArgs;
964 bool Invalid = ParseTemplateIdAfterTemplateName(Template,
965 TemplateNameLoc,
966 SS, false, LAngleLoc,
967 TemplateArgs,
968 RAngleLoc);
969
970 if (Invalid) {
971 // If we failed to parse the template ID but skipped ahead to a >, we're not
972 // going to be able to form a token annotation. Eat the '>' if present.
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000973 TryConsumeToken(tok::greater);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000974 return true;
975 }
976
977 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
978
979 // Build the annotation token.
980 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
981 TypeResult Type
982 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
983 Template, TemplateNameLoc,
984 LAngleLoc, TemplateArgsPtr, RAngleLoc);
985 if (Type.isInvalid()) {
986 // If we failed to parse the template ID but skipped ahead to a >, we're not
987 // going to be able to form a token annotation. Eat the '>' if present.
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000988 TryConsumeToken(tok::greater);
Faisal Vali6a79ca12013-06-08 19:39:00 +0000989 return true;
990 }
991
992 Tok.setKind(tok::annot_typename);
993 setTypeAnnotation(Tok, Type.get());
994 if (SS.isNotEmpty())
995 Tok.setLocation(SS.getBeginLoc());
996 else if (TemplateKWLoc.isValid())
997 Tok.setLocation(TemplateKWLoc);
998 else
999 Tok.setLocation(TemplateNameLoc);
1000 } else {
1001 // Build a template-id annotation token that can be processed
1002 // later.
1003 Tok.setKind(tok::annot_template_id);
1004 TemplateIdAnnotation *TemplateId
1005 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
1006 TemplateId->TemplateNameLoc = TemplateNameLoc;
1007 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
1008 TemplateId->Name = TemplateName.Identifier;
1009 TemplateId->Operator = OO_None;
1010 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001011 TemplateId->Name = nullptr;
Faisal Vali6a79ca12013-06-08 19:39:00 +00001012 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
1013 }
1014 TemplateId->SS = SS;
1015 TemplateId->TemplateKWLoc = TemplateKWLoc;
1016 TemplateId->Template = Template;
1017 TemplateId->Kind = TNK;
1018 TemplateId->LAngleLoc = LAngleLoc;
1019 TemplateId->RAngleLoc = RAngleLoc;
1020 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
1021 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
1022 Args[Arg] = ParsedTemplateArgument(TemplateArgs[Arg]);
1023 Tok.setAnnotationValue(TemplateId);
1024 if (TemplateKWLoc.isValid())
1025 Tok.setLocation(TemplateKWLoc);
1026 else
1027 Tok.setLocation(TemplateNameLoc);
1028 }
1029
1030 // Common fields for the annotation token
1031 Tok.setAnnotationEndLoc(RAngleLoc);
1032
1033 // In case the tokens were cached, have Preprocessor replace them with the
1034 // annotation token.
1035 PP.AnnotateCachedTokens(Tok);
1036 return false;
1037}
1038
1039/// \brief Replaces a template-id annotation token with a type
1040/// annotation token.
1041///
1042/// If there was a failure when forming the type from the template-id,
1043/// a type annotation token will still be created, but will have a
1044/// NULL type pointer to signify an error.
1045void Parser::AnnotateTemplateIdTokenAsType() {
1046 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
1047
1048 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1049 assert((TemplateId->Kind == TNK_Type_template ||
1050 TemplateId->Kind == TNK_Dependent_template_name) &&
1051 "Only works for type and dependent templates");
1052
1053 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1054 TemplateId->NumArgs);
1055
1056 TypeResult Type
1057 = Actions.ActOnTemplateIdType(TemplateId->SS,
1058 TemplateId->TemplateKWLoc,
1059 TemplateId->Template,
1060 TemplateId->TemplateNameLoc,
1061 TemplateId->LAngleLoc,
1062 TemplateArgsPtr,
1063 TemplateId->RAngleLoc);
1064 // Create the new "type" annotation token.
1065 Tok.setKind(tok::annot_typename);
1066 setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get());
1067 if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name.
1068 Tok.setLocation(TemplateId->SS.getBeginLoc());
1069 // End location stays the same
1070
1071 // Replace the template-id annotation token, and possible the scope-specifier
1072 // that precedes it, with the typename annotation token.
1073 PP.AnnotateCachedTokens(Tok);
1074}
1075
1076/// \brief Determine whether the given token can end a template argument.
1077static bool isEndOfTemplateArgument(Token Tok) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001078 return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001079}
1080
1081/// \brief Parse a C++ template template argument.
1082ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
1083 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
1084 !Tok.is(tok::annot_cxxscope))
1085 return ParsedTemplateArgument();
1086
1087 // C++0x [temp.arg.template]p1:
1088 // A template-argument for a template template-parameter shall be the name
1089 // of a class template or an alias template, expressed as id-expression.
1090 //
1091 // We parse an id-expression that refers to a class template or alias
1092 // template. The grammar we parse is:
1093 //
1094 // nested-name-specifier[opt] template[opt] identifier ...[opt]
1095 //
1096 // followed by a token that terminates a template argument, such as ',',
1097 // '>', or (in some cases) '>>'.
1098 CXXScopeSpec SS; // nested-name-specifier, if present
1099 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1100 /*EnteringContext=*/false);
1101
1102 ParsedTemplateArgument Result;
1103 SourceLocation EllipsisLoc;
1104 if (SS.isSet() && Tok.is(tok::kw_template)) {
1105 // Parse the optional 'template' keyword following the
1106 // nested-name-specifier.
1107 SourceLocation TemplateKWLoc = ConsumeToken();
1108
1109 if (Tok.is(tok::identifier)) {
1110 // We appear to have a dependent template name.
1111 UnqualifiedId Name;
1112 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1113 ConsumeToken(); // the identifier
Alp Toker094e5212014-01-05 03:27:11 +00001114
1115 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1116
Faisal Vali6a79ca12013-06-08 19:39:00 +00001117 // If the next token signals the end of a template argument,
1118 // then we have a dependent template name that could be a template
1119 // template argument.
1120 TemplateTy Template;
1121 if (isEndOfTemplateArgument(Tok) &&
1122 Actions.ActOnDependentTemplateName(getCurScope(),
1123 SS, TemplateKWLoc, Name,
1124 /*ObjectType=*/ ParsedType(),
1125 /*EnteringContext=*/false,
1126 Template))
1127 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
1128 }
1129 } else if (Tok.is(tok::identifier)) {
1130 // We may have a (non-dependent) template name.
1131 TemplateTy Template;
1132 UnqualifiedId Name;
1133 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1134 ConsumeToken(); // the identifier
Alp Tokera3ebe6e2013-12-17 14:12:37 +00001135
1136 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001137
1138 if (isEndOfTemplateArgument(Tok)) {
1139 bool MemberOfUnknownSpecialization;
1140 TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
1141 /*hasTemplateKeyword=*/false,
1142 Name,
1143 /*ObjectType=*/ ParsedType(),
1144 /*EnteringContext=*/false,
1145 Template,
1146 MemberOfUnknownSpecialization);
1147 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
1148 // We have an id-expression that refers to a class template or
1149 // (C++0x) alias template.
1150 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
1151 }
1152 }
1153 }
1154
1155 // If this is a pack expansion, build it as such.
1156 if (EllipsisLoc.isValid() && !Result.isInvalid())
1157 Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
1158
1159 return Result;
1160}
1161
1162/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
1163///
1164/// template-argument: [C++ 14.2]
1165/// constant-expression
1166/// type-id
1167/// id-expression
1168ParsedTemplateArgument Parser::ParseTemplateArgument() {
1169 // C++ [temp.arg]p2:
1170 // In a template-argument, an ambiguity between a type-id and an
1171 // expression is resolved to a type-id, regardless of the form of
1172 // the corresponding template-parameter.
1173 //
1174 // Therefore, we initially try to parse a type-id.
1175 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
1176 SourceLocation Loc = Tok.getLocation();
Craig Topper161e4db2014-05-21 06:02:52 +00001177 TypeResult TypeArg = ParseTypeName(/*Range=*/nullptr,
Faisal Vali6a79ca12013-06-08 19:39:00 +00001178 Declarator::TemplateTypeArgContext);
1179 if (TypeArg.isInvalid())
1180 return ParsedTemplateArgument();
1181
1182 return ParsedTemplateArgument(ParsedTemplateArgument::Type,
1183 TypeArg.get().getAsOpaquePtr(),
1184 Loc);
1185 }
1186
1187 // Try to parse a template template argument.
1188 {
1189 TentativeParsingAction TPA(*this);
1190
1191 ParsedTemplateArgument TemplateTemplateArgument
1192 = ParseTemplateTemplateArgument();
1193 if (!TemplateTemplateArgument.isInvalid()) {
1194 TPA.Commit();
1195 return TemplateTemplateArgument;
1196 }
1197
1198 // Revert this tentative parse to parse a non-type template argument.
1199 TPA.Revert();
1200 }
1201
1202 // Parse a non-type template argument.
1203 SourceLocation Loc = Tok.getLocation();
1204 ExprResult ExprArg = ParseConstantExpression(MaybeTypeCast);
1205 if (ExprArg.isInvalid() || !ExprArg.get())
1206 return ParsedTemplateArgument();
1207
1208 return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001209 ExprArg.get(), Loc);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001210}
1211
1212/// \brief Determine whether the current tokens can only be parsed as a
1213/// template argument list (starting with the '<') and never as a '<'
1214/// expression.
1215bool Parser::IsTemplateArgumentList(unsigned Skip) {
1216 struct AlwaysRevertAction : TentativeParsingAction {
1217 AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { }
1218 ~AlwaysRevertAction() { Revert(); }
1219 } Tentative(*this);
1220
1221 while (Skip) {
1222 ConsumeToken();
1223 --Skip;
1224 }
1225
1226 // '<'
Alp Tokera3ebe6e2013-12-17 14:12:37 +00001227 if (!TryConsumeToken(tok::less))
Faisal Vali6a79ca12013-06-08 19:39:00 +00001228 return false;
Faisal Vali6a79ca12013-06-08 19:39:00 +00001229
1230 // An empty template argument list.
1231 if (Tok.is(tok::greater))
1232 return true;
1233
1234 // See whether we have declaration specifiers, which indicate a type.
Richard Smithee390432014-05-16 01:56:53 +00001235 while (isCXXDeclarationSpecifier() == TPResult::True)
Faisal Vali6a79ca12013-06-08 19:39:00 +00001236 ConsumeToken();
1237
1238 // If we have a '>' or a ',' then this is a template argument list.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001239 return Tok.isOneOf(tok::greater, tok::comma);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001240}
1241
1242/// ParseTemplateArgumentList - Parse a C++ template-argument-list
1243/// (C++ [temp.names]). Returns true if there was an error.
1244///
1245/// template-argument-list: [C++ 14.2]
1246/// template-argument
1247/// template-argument-list ',' template-argument
1248bool
1249Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
1250 // Template argument lists are constant-evaluation contexts.
1251 EnterExpressionEvaluationContext EvalContext(Actions,Sema::ConstantEvaluated);
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +00001252 ColonProtectionRAIIObject ColonProtection(*this, false);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001253
Alp Tokera3ebe6e2013-12-17 14:12:37 +00001254 do {
Faisal Vali6a79ca12013-06-08 19:39:00 +00001255 ParsedTemplateArgument Arg = ParseTemplateArgument();
Alp Tokera3ebe6e2013-12-17 14:12:37 +00001256 SourceLocation EllipsisLoc;
1257 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Faisal Vali6a79ca12013-06-08 19:39:00 +00001258 Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001259
1260 if (Arg.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001261 SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001262 return true;
1263 }
1264
1265 // Save this template argument.
1266 TemplateArgs.push_back(Arg);
1267
1268 // If the next token is a comma, consume it and keep reading
1269 // arguments.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00001270 } while (TryConsumeToken(tok::comma));
Faisal Vali6a79ca12013-06-08 19:39:00 +00001271
1272 return false;
1273}
1274
1275/// \brief Parse a C++ explicit template instantiation
1276/// (C++ [temp.explicit]).
1277///
1278/// explicit-instantiation:
1279/// 'extern' [opt] 'template' declaration
1280///
1281/// Note that the 'extern' is a GNU extension and C++11 feature.
1282Decl *Parser::ParseExplicitInstantiation(unsigned Context,
1283 SourceLocation ExternLoc,
1284 SourceLocation TemplateLoc,
1285 SourceLocation &DeclEnd,
1286 AccessSpecifier AS) {
1287 // This isn't really required here.
1288 ParsingDeclRAIIObject
1289 ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
1290
1291 return ParseSingleDeclarationAfterTemplate(Context,
1292 ParsedTemplateInfo(ExternLoc,
1293 TemplateLoc),
1294 ParsingTemplateParams,
1295 DeclEnd, AS);
1296}
1297
1298SourceRange Parser::ParsedTemplateInfo::getSourceRange() const {
1299 if (TemplateParams)
1300 return getTemplateParamsRange(TemplateParams->data(),
1301 TemplateParams->size());
1302
1303 SourceRange R(TemplateLoc);
1304 if (ExternLoc.isValid())
1305 R.setBegin(ExternLoc);
1306 return R;
1307}
1308
Richard Smithe40f2ba2013-08-07 21:41:30 +00001309void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) {
1310 ((Parser *)P)->ParseLateTemplatedFuncDef(LPT);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001311}
1312
1313/// \brief Late parse a C++ function template in Microsoft mode.
Richard Smithe40f2ba2013-08-07 21:41:30 +00001314void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
David Majnemerf0a84f22013-08-16 08:29:13 +00001315 if (!LPT.D)
Faisal Vali6a79ca12013-06-08 19:39:00 +00001316 return;
1317
1318 // Get the FunctionDecl.
Alp Tokera2794f92014-01-22 07:29:52 +00001319 FunctionDecl *FunD = LPT.D->getAsFunction();
Faisal Vali6a79ca12013-06-08 19:39:00 +00001320 // Track template parameter depth.
1321 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
1322
1323 // To restore the context after late parsing.
Richard Smithb0b68012015-05-11 23:09:06 +00001324 Sema::ContextRAII GlobalSavedContext(
1325 Actions, Actions.Context.getTranslationUnitDecl());
Faisal Vali6a79ca12013-06-08 19:39:00 +00001326
1327 SmallVector<ParseScope*, 4> TemplateParamScopeStack;
1328
1329 // Get the list of DeclContexts to reenter.
1330 SmallVector<DeclContext*, 4> DeclContextsToReenter;
Hans Wennborgb6d4e8c2014-05-02 02:01:07 +00001331 DeclContext *DD = FunD;
Faisal Vali6a79ca12013-06-08 19:39:00 +00001332 while (DD && !DD->isTranslationUnit()) {
1333 DeclContextsToReenter.push_back(DD);
1334 DD = DD->getLexicalParent();
1335 }
1336
1337 // Reenter template scopes from outermost to innermost.
Craig Topper61ac9062013-07-08 03:55:09 +00001338 SmallVectorImpl<DeclContext *>::reverse_iterator II =
Faisal Vali6a79ca12013-06-08 19:39:00 +00001339 DeclContextsToReenter.rbegin();
1340 for (; II != DeclContextsToReenter.rend(); ++II) {
Hans Wennborgb6d4e8c2014-05-02 02:01:07 +00001341 TemplateParamScopeStack.push_back(new ParseScope(this,
1342 Scope::TemplateParamScope));
1343 unsigned NumParamLists =
1344 Actions.ActOnReenterTemplateScope(getCurScope(), cast<Decl>(*II));
1345 CurTemplateDepthTracker.addDepth(NumParamLists);
1346 if (*II != FunD) {
1347 TemplateParamScopeStack.push_back(new ParseScope(this, Scope::DeclScope));
1348 Actions.PushDeclContext(Actions.getCurScope(), *II);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001349 }
Faisal Vali6a79ca12013-06-08 19:39:00 +00001350 }
Faisal Vali6a79ca12013-06-08 19:39:00 +00001351
Richard Smithe40f2ba2013-08-07 21:41:30 +00001352 assert(!LPT.Toks.empty() && "Empty body!");
Faisal Vali6a79ca12013-06-08 19:39:00 +00001353
1354 // Append the current token at the end of the new token stream so that it
1355 // doesn't get lost.
Richard Smithe40f2ba2013-08-07 21:41:30 +00001356 LPT.Toks.push_back(Tok);
1357 PP.EnterTokenStream(LPT.Toks.data(), LPT.Toks.size(), true, false);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001358
1359 // Consume the previously pushed token.
1360 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001361 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) &&
1362 "Inline method not starting with '{', ':' or 'try'");
Faisal Vali6a79ca12013-06-08 19:39:00 +00001363
1364 // Parse the method body. Function body parsing code is similar enough
1365 // to be re-used for method bodies as well.
1366 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
1367
1368 // Recreate the containing function DeclContext.
Nico Weber55048cf2014-08-15 22:15:00 +00001369 Sema::ContextRAII FunctionSavedContext(Actions,
1370 Actions.getContainingDC(FunD));
Faisal Vali6a79ca12013-06-08 19:39:00 +00001371
1372 Actions.ActOnStartOfFunctionDef(getCurScope(), FunD);
1373
1374 if (Tok.is(tok::kw_try)) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00001375 ParseFunctionTryBlock(LPT.D, FnScope);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001376 } else {
1377 if (Tok.is(tok::colon))
Richard Smithe40f2ba2013-08-07 21:41:30 +00001378 ParseConstructorInitializer(LPT.D);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001379 else
Richard Smithe40f2ba2013-08-07 21:41:30 +00001380 Actions.ActOnDefaultCtorInitializers(LPT.D);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001381
1382 if (Tok.is(tok::l_brace)) {
Alp Tokera2794f92014-01-22 07:29:52 +00001383 assert((!isa<FunctionTemplateDecl>(LPT.D) ||
1384 cast<FunctionTemplateDecl>(LPT.D)
1385 ->getTemplateParameters()
Hans Wennborgb6d4e8c2014-05-02 02:01:07 +00001386 ->getDepth() == TemplateParameterDepth - 1) &&
Faisal Vali6a79ca12013-06-08 19:39:00 +00001387 "TemplateParameterDepth should be greater than the depth of "
1388 "current template being instantiated!");
Richard Smithe40f2ba2013-08-07 21:41:30 +00001389 ParseFunctionStatementBody(LPT.D, FnScope);
1390 Actions.UnmarkAsLateParsedTemplate(FunD);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001391 } else
Craig Topper161e4db2014-05-21 06:02:52 +00001392 Actions.ActOnFinishFunctionBody(LPT.D, nullptr);
Faisal Vali6a79ca12013-06-08 19:39:00 +00001393 }
1394
1395 // Exit scopes.
1396 FnScope.Exit();
Craig Topper61ac9062013-07-08 03:55:09 +00001397 SmallVectorImpl<ParseScope *>::reverse_iterator I =
Faisal Vali6a79ca12013-06-08 19:39:00 +00001398 TemplateParamScopeStack.rbegin();
1399 for (; I != TemplateParamScopeStack.rend(); ++I)
1400 delete *I;
Faisal Vali6a79ca12013-06-08 19:39:00 +00001401}
1402
1403/// \brief Lex a delayed template function for late parsing.
1404void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
1405 tok::TokenKind kind = Tok.getKind();
1406 if (!ConsumeAndStoreFunctionPrologue(Toks)) {
1407 // Consume everything up to (and including) the matching right brace.
1408 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1409 }
1410
1411 // If we're in a function-try-block, we need to store all the catch blocks.
1412 if (kind == tok::kw_try) {
1413 while (Tok.is(tok::kw_catch)) {
1414 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1415 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1416 }
1417 }
1418}