1193326Sed//===--- ParseTemplate.cpp - Template Parsing -----------------------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements parsing of C++ templates. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14193326Sed#include "clang/Parse/Parser.h" 15252723Sdim#include "RAIIObjectsForParser.h" 16252723Sdim#include "clang/AST/ASTConsumer.h" 17252723Sdim#include "clang/AST/DeclTemplate.h" 18193326Sed#include "clang/Parse/ParseDiagnostic.h" 19212904Sdim#include "clang/Sema/DeclSpec.h" 20212904Sdim#include "clang/Sema/ParsedTemplate.h" 21212904Sdim#include "clang/Sema/Scope.h" 22193326Sedusing namespace clang; 23193326Sed 24193326Sed/// \brief Parse a template declaration, explicit instantiation, or 25193326Sed/// explicit specialization. 26212904SdimDecl * 27193326SedParser::ParseDeclarationStartingWithTemplate(unsigned Context, 28193326Sed SourceLocation &DeclEnd, 29226890Sdim AccessSpecifier AS, 30226890Sdim AttributeList *AccessAttrs) { 31226890Sdim ObjCDeclContextSwitch ObjCDC(*this); 32226890Sdim 33226890Sdim if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) { 34235633Sdim return ParseExplicitInstantiation(Context, 35235633Sdim SourceLocation(), ConsumeToken(), 36235633Sdim DeclEnd, AS); 37226890Sdim } 38226890Sdim return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS, 39226890Sdim AccessAttrs); 40193326Sed} 41193326Sed 42198092Srdivacky 43198092Srdivacky 44193326Sed/// \brief Parse a template declaration or an explicit specialization. 45193326Sed/// 46193326Sed/// Template declarations include one or more template parameter lists 47193326Sed/// and either the function or class template declaration. Explicit 48193326Sed/// specializations contain one or more 'template < >' prefixes 49193326Sed/// followed by a (possibly templated) declaration. Since the 50193326Sed/// syntactic form of both features is nearly identical, we parse all 51193326Sed/// of the template headers together and let semantic analysis sort 52193326Sed/// the declarations from the explicit specializations. 53193326Sed/// 54193326Sed/// template-declaration: [C++ temp] 55193326Sed/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration 56193326Sed/// 57193326Sed/// explicit-specialization: [ C++ temp.expl.spec] 58193326Sed/// 'template' '<' '>' declaration 59212904SdimDecl * 60193326SedParser::ParseTemplateDeclarationOrSpecialization(unsigned Context, 61193326Sed SourceLocation &DeclEnd, 62226890Sdim AccessSpecifier AS, 63226890Sdim AttributeList *AccessAttrs) { 64198092Srdivacky assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && 65198092Srdivacky "Token does not start a template declaration."); 66198092Srdivacky 67193326Sed // Enter template-parameter scope. 68193326Sed ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 69193326Sed 70212904Sdim // Tell the action that names should be checked in the context of 71212904Sdim // the declaration to come. 72245431Sdim ParsingDeclRAIIObject 73245431Sdim ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); 74212904Sdim 75193326Sed // Parse multiple levels of template headers within this template 76193326Sed // parameter scope, e.g., 77193326Sed // 78193326Sed // template<typename T> 79193326Sed // template<typename U> 80193326Sed // class A<T>::B { ... }; 81193326Sed // 82193326Sed // We parse multiple levels non-recursively so that we can build a 83193326Sed // single data structure containing all of the template parameter 84193326Sed // lists to easily differentiate between the case above and: 85193326Sed // 86193326Sed // template<typename T> 87193326Sed // class A { 88193326Sed // template<typename U> class B; 89193326Sed // }; 90193326Sed // 91193326Sed // In the first case, the action for declaring A<T>::B receives 92193326Sed // both template parameter lists. In the second case, the action for 93193326Sed // defining A<T>::B receives just the inner template parameter list 94193326Sed // (and retrieves the outer template parameter list from its 95193326Sed // context). 96198092Srdivacky bool isSpecialization = true; 97198893Srdivacky bool LastParamListWasEmpty = false; 98193326Sed TemplateParameterLists ParamLists; 99252723Sdim TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 100252723Sdim 101193326Sed do { 102193326Sed // Consume the 'export', if any. 103193326Sed SourceLocation ExportLoc; 104193326Sed if (Tok.is(tok::kw_export)) { 105193326Sed ExportLoc = ConsumeToken(); 106193326Sed } 107193326Sed 108193326Sed // Consume the 'template', which should be here. 109193326Sed SourceLocation TemplateLoc; 110193326Sed if (Tok.is(tok::kw_template)) { 111193326Sed TemplateLoc = ConsumeToken(); 112193326Sed } else { 113193326Sed Diag(Tok.getLocation(), diag::err_expected_template); 114212904Sdim return 0; 115193326Sed } 116198092Srdivacky 117193326Sed // Parse the '<' template-parameter-list '>' 118193326Sed SourceLocation LAngleLoc, RAngleLoc; 119226890Sdim SmallVector<Decl*, 4> TemplateParams; 120252723Sdim if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(), 121252723Sdim TemplateParams, LAngleLoc, RAngleLoc)) { 122198092Srdivacky // Skip until the semi-colon or a }. 123263509Sdim SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); 124198092Srdivacky if (Tok.is(tok::semi)) 125198092Srdivacky ConsumeToken(); 126212904Sdim return 0; 127198092Srdivacky } 128193326Sed 129193326Sed ParamLists.push_back( 130252723Sdim Actions.ActOnTemplateParameterList(CurTemplateDepthTracker.getDepth(), 131252723Sdim ExportLoc, 132198092Srdivacky TemplateLoc, LAngleLoc, 133193326Sed TemplateParams.data(), 134193326Sed TemplateParams.size(), RAngleLoc)); 135198092Srdivacky 136198092Srdivacky if (!TemplateParams.empty()) { 137198092Srdivacky isSpecialization = false; 138252723Sdim ++CurTemplateDepthTracker; 139198893Srdivacky } else { 140198893Srdivacky LastParamListWasEmpty = true; 141198092Srdivacky } 142193326Sed } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); 143193326Sed 144193326Sed // Parse the actual template declaration. 145198092Srdivacky return ParseSingleDeclarationAfterTemplate(Context, 146193326Sed ParsedTemplateInfo(&ParamLists, 147198893Srdivacky isSpecialization, 148198893Srdivacky LastParamListWasEmpty), 149212904Sdim ParsingTemplateParams, 150226890Sdim DeclEnd, AS, AccessAttrs); 151193326Sed} 152193326Sed 153193326Sed/// \brief Parse a single declaration that declares a template, 154193326Sed/// template specialization, or explicit instantiation of a template. 155193326Sed/// 156193326Sed/// \param DeclEnd will receive the source location of the last token 157193326Sed/// within this declaration. 158193326Sed/// 159193326Sed/// \param AS the access specifier associated with this 160193326Sed/// declaration. Will be AS_none for namespace-scope declarations. 161193326Sed/// 162193326Sed/// \returns the new declaration. 163212904SdimDecl * 164193326SedParser::ParseSingleDeclarationAfterTemplate( 165193326Sed unsigned Context, 166193326Sed const ParsedTemplateInfo &TemplateInfo, 167212904Sdim ParsingDeclRAIIObject &DiagsFromTParams, 168193326Sed SourceLocation &DeclEnd, 169226890Sdim AccessSpecifier AS, 170226890Sdim AttributeList *AccessAttrs) { 171193326Sed assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 172193326Sed "Template information required"); 173193326Sed 174198092Srdivacky if (Context == Declarator::MemberContext) { 175198092Srdivacky // We are parsing a member template. 176226890Sdim ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo, 177226890Sdim &DiagsFromTParams); 178212904Sdim return 0; 179198092Srdivacky } 180198092Srdivacky 181221345Sdim ParsedAttributesWithRange prefixAttrs(AttrFactory); 182252723Sdim MaybeParseCXX11Attributes(prefixAttrs); 183218893Sdim 184218893Sdim if (Tok.is(tok::kw_using)) 185218893Sdim return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd, 186218893Sdim prefixAttrs); 187218893Sdim 188245431Sdim // Parse the declaration specifiers, stealing any diagnostics from 189245431Sdim // the template parameters. 190221345Sdim ParsingDeclSpec DS(*this, &DiagsFromTParams); 191199990Srdivacky 192202379Srdivacky ParseDeclarationSpecifiers(DS, TemplateInfo, AS, 193202379Srdivacky getDeclSpecContextFromDeclaratorContext(Context)); 194193326Sed 195193326Sed if (Tok.is(tok::semi)) { 196252723Sdim ProhibitAttributes(prefixAttrs); 197193326Sed DeclEnd = ConsumeToken(); 198252723Sdim Decl *Decl = Actions.ParsedFreeStandingDeclSpec( 199252723Sdim getCurScope(), AS, DS, 200252723Sdim TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams 201252723Sdim : MultiTemplateParamsArg(), 202252723Sdim TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation); 203198893Srdivacky DS.complete(Decl); 204198893Srdivacky return Decl; 205193326Sed } 206193326Sed 207252723Sdim // Move the attributes from the prefix into the DS. 208252723Sdim if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) 209252723Sdim ProhibitAttributes(prefixAttrs); 210252723Sdim else 211252723Sdim DS.takeAttributesFrom(prefixAttrs); 212252723Sdim 213193326Sed // Parse the declarator. 214198893Srdivacky ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context); 215193326Sed ParseDeclarator(DeclaratorInfo); 216193326Sed // Error parsing the declarator? 217193326Sed if (!DeclaratorInfo.hasName()) { 218193326Sed // If so, skip until the semi-colon or a }. 219263509Sdim SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); 220193326Sed if (Tok.is(tok::semi)) 221193326Sed ConsumeToken(); 222212904Sdim return 0; 223193326Sed } 224198092Srdivacky 225245431Sdim LateParsedAttrList LateParsedAttrs(true); 226235633Sdim if (DeclaratorInfo.isFunctionDeclarator()) 227235633Sdim MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); 228235633Sdim 229193326Sed if (DeclaratorInfo.isFunctionDeclarator() && 230210299Sed isStartOfFunctionDefinition(DeclaratorInfo)) { 231193326Sed if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 232235633Sdim // Recover by ignoring the 'typedef'. This was probably supposed to be 233235633Sdim // the 'typename' keyword, which we should have already suggested adding 234235633Sdim // if it's appropriate. 235235633Sdim Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef) 236235633Sdim << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); 237235633Sdim DS.ClearStorageClassSpecs(); 238193326Sed } 239263509Sdim 240263509Sdim if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { 241263509Sdim if (DeclaratorInfo.getName().getKind() != UnqualifiedId::IK_TemplateId) { 242263509Sdim // If the declarator-id is not a template-id, issue a diagnostic and 243263509Sdim // recover by ignoring the 'template' keyword. 244263509Sdim Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0; 245263509Sdim return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(), 246263509Sdim &LateParsedAttrs); 247263509Sdim } else { 248263509Sdim SourceLocation LAngleLoc 249263509Sdim = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); 250263509Sdim Diag(DeclaratorInfo.getIdentifierLoc(), 251263509Sdim diag::err_explicit_instantiation_with_definition) 252263509Sdim << SourceRange(TemplateInfo.TemplateLoc) 253263509Sdim << FixItHint::CreateInsertion(LAngleLoc, "<>"); 254263509Sdim 255263509Sdim // Recover as if it were an explicit specialization. 256263509Sdim TemplateParameterLists FakedParamLists; 257263509Sdim FakedParamLists.push_back(Actions.ActOnTemplateParameterList( 258263509Sdim 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, 259263509Sdim LAngleLoc)); 260263509Sdim 261263509Sdim return ParseFunctionDefinition( 262263509Sdim DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists, 263263509Sdim /*isSpecialization=*/true, 264263509Sdim /*LastParamListWasEmpty=*/true), 265263509Sdim &LateParsedAttrs); 266263509Sdim } 267263509Sdim } 268235633Sdim return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo, 269235633Sdim &LateParsedAttrs); 270193326Sed } 271193326Sed 272252723Sdim // Parse this declaration. 273252723Sdim Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, 274252723Sdim TemplateInfo); 275252723Sdim 276252723Sdim if (Tok.is(tok::comma)) { 277252723Sdim Diag(Tok, diag::err_multiple_template_declarators) 278252723Sdim << (int)TemplateInfo.Kind; 279263509Sdim SkipUntil(tok::semi); 280252723Sdim return ThisDecl; 281252723Sdim } 282252723Sdim 283252723Sdim // Eat the semi colon after the declaration. 284252723Sdim ExpectAndConsumeSemi(diag::err_expected_semi_declaration); 285252723Sdim if (LateParsedAttrs.size() > 0) 286252723Sdim ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false); 287252723Sdim DeclaratorInfo.complete(ThisDecl); 288252723Sdim return ThisDecl; 289193326Sed} 290193326Sed 291193326Sed/// ParseTemplateParameters - Parses a template-parameter-list enclosed in 292193326Sed/// angle brackets. Depth is the depth of this template-parameter-list, which 293193326Sed/// is the number of template headers directly enclosing this template header. 294193326Sed/// TemplateParams is the current list of template parameters we're building. 295193326Sed/// The template parameter we parse will be added to this list. LAngleLoc and 296198092Srdivacky/// RAngleLoc will receive the positions of the '<' and '>', respectively, 297193326Sed/// that enclose this template parameter list. 298198092Srdivacky/// 299198092Srdivacky/// \returns true if an error occurred, false otherwise. 300193326Sedbool Parser::ParseTemplateParameters(unsigned Depth, 301226890Sdim SmallVectorImpl<Decl*> &TemplateParams, 302193326Sed SourceLocation &LAngleLoc, 303193326Sed SourceLocation &RAngleLoc) { 304193326Sed // Get the template parameter list. 305198092Srdivacky if (!Tok.is(tok::less)) { 306193326Sed Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; 307198092Srdivacky return true; 308193326Sed } 309193326Sed LAngleLoc = ConsumeToken(); 310198092Srdivacky 311193326Sed // Try to parse the template parameter list. 312235633Sdim bool Failed = false; 313235633Sdim if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater)) 314235633Sdim Failed = ParseTemplateParameterList(Depth, TemplateParams); 315235633Sdim 316235633Sdim if (Tok.is(tok::greatergreater)) { 317245431Sdim // No diagnostic required here: a template-parameter-list can only be 318245431Sdim // followed by a declaration or, for a template template parameter, the 319245431Sdim // 'class' keyword. Therefore, the second '>' will be diagnosed later. 320245431Sdim // This matters for elegant diagnosis of: 321245431Sdim // template<template<typename>> struct S; 322235633Sdim Tok.setKind(tok::greater); 323235633Sdim RAngleLoc = Tok.getLocation(); 324235633Sdim Tok.setLocation(Tok.getLocation().getLocWithOffset(1)); 325235633Sdim } else if (Tok.is(tok::greater)) 326193326Sed RAngleLoc = ConsumeToken(); 327235633Sdim else if (Failed) { 328235633Sdim Diag(Tok.getLocation(), diag::err_expected_greater); 329235633Sdim return true; 330193326Sed } 331198092Srdivacky return false; 332193326Sed} 333193326Sed 334193326Sed/// ParseTemplateParameterList - Parse a template parameter list. If 335193326Sed/// the parsing fails badly (i.e., closing bracket was left out), this 336193326Sed/// will try to put the token stream in a reasonable position (closing 337198092Srdivacky/// a statement, etc.) and return false. 338193326Sed/// 339193326Sed/// template-parameter-list: [C++ temp] 340193326Sed/// template-parameter 341193326Sed/// template-parameter-list ',' template-parameter 342198092Srdivackybool 343193326SedParser::ParseTemplateParameterList(unsigned Depth, 344226890Sdim SmallVectorImpl<Decl*> &TemplateParams) { 345198092Srdivacky while (1) { 346212904Sdim if (Decl *TmpParam 347193326Sed = ParseTemplateParameter(Depth, TemplateParams.size())) { 348193326Sed TemplateParams.push_back(TmpParam); 349193326Sed } else { 350193326Sed // If we failed to parse a template parameter, skip until we find 351193326Sed // a comma or closing brace. 352263509Sdim SkipUntil(tok::comma, tok::greater, tok::greatergreater, 353263509Sdim StopAtSemi | StopBeforeMatch); 354193326Sed } 355198092Srdivacky 356252723Sdim // Did we find a comma or the end of the template parameter list? 357198092Srdivacky if (Tok.is(tok::comma)) { 358193326Sed ConsumeToken(); 359235633Sdim } else if (Tok.is(tok::greater) || Tok.is(tok::greatergreater)) { 360193326Sed // Don't consume this... that's done by template parser. 361193326Sed break; 362193326Sed } else { 363193326Sed // Somebody probably forgot to close the template. Skip ahead and 364193326Sed // try to get out of the expression. This error is currently 365193326Sed // subsumed by whatever goes on in ParseTemplateParameter. 366218893Sdim Diag(Tok.getLocation(), diag::err_expected_comma_greater); 367263509Sdim SkipUntil(tok::comma, tok::greater, tok::greatergreater, 368263509Sdim StopAtSemi | StopBeforeMatch); 369193326Sed return false; 370193326Sed } 371193326Sed } 372193326Sed return true; 373193326Sed} 374193326Sed 375199990Srdivacky/// \brief Determine whether the parser is at the start of a template 376199990Srdivacky/// type parameter. 377199990Srdivackybool Parser::isStartOfTemplateTypeParameter() { 378210299Sed if (Tok.is(tok::kw_class)) { 379210299Sed // "class" may be the start of an elaborated-type-specifier or a 380210299Sed // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter. 381210299Sed switch (NextToken().getKind()) { 382210299Sed case tok::equal: 383210299Sed case tok::comma: 384210299Sed case tok::greater: 385210299Sed case tok::greatergreater: 386210299Sed case tok::ellipsis: 387210299Sed return true; 388210299Sed 389210299Sed case tok::identifier: 390210299Sed // This may be either a type-parameter or an elaborated-type-specifier. 391210299Sed // We have to look further. 392210299Sed break; 393210299Sed 394210299Sed default: 395210299Sed return false; 396210299Sed } 397210299Sed 398210299Sed switch (GetLookAheadToken(2).getKind()) { 399210299Sed case tok::equal: 400210299Sed case tok::comma: 401210299Sed case tok::greater: 402210299Sed case tok::greatergreater: 403210299Sed return true; 404210299Sed 405210299Sed default: 406210299Sed return false; 407210299Sed } 408210299Sed } 409199990Srdivacky 410199990Srdivacky if (Tok.isNot(tok::kw_typename)) 411199990Srdivacky return false; 412199990Srdivacky 413199990Srdivacky // C++ [temp.param]p2: 414199990Srdivacky // There is no semantic difference between class and typename in a 415199990Srdivacky // template-parameter. typename followed by an unqualified-id 416199990Srdivacky // names a template type parameter. typename followed by a 417199990Srdivacky // qualified-id denotes the type in a non-type 418199990Srdivacky // parameter-declaration. 419199990Srdivacky Token Next = NextToken(); 420199990Srdivacky 421199990Srdivacky // If we have an identifier, skip over it. 422199990Srdivacky if (Next.getKind() == tok::identifier) 423199990Srdivacky Next = GetLookAheadToken(2); 424199990Srdivacky 425199990Srdivacky switch (Next.getKind()) { 426199990Srdivacky case tok::equal: 427199990Srdivacky case tok::comma: 428199990Srdivacky case tok::greater: 429199990Srdivacky case tok::greatergreater: 430199990Srdivacky case tok::ellipsis: 431199990Srdivacky return true; 432199990Srdivacky 433199990Srdivacky default: 434199990Srdivacky return false; 435199990Srdivacky } 436199990Srdivacky} 437199990Srdivacky 438193326Sed/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). 439193326Sed/// 440193326Sed/// template-parameter: [C++ temp.param] 441193326Sed/// type-parameter 442193326Sed/// parameter-declaration 443193326Sed/// 444193326Sed/// type-parameter: (see below) 445218893Sdim/// 'class' ...[opt] identifier[opt] 446193326Sed/// 'class' identifier[opt] '=' type-id 447218893Sdim/// 'typename' ...[opt] identifier[opt] 448193326Sed/// 'typename' identifier[opt] '=' type-id 449218893Sdim/// 'template' '<' template-parameter-list '>' 450218893Sdim/// 'class' ...[opt] identifier[opt] 451218893Sdim/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] 452218893Sdim/// = id-expression 453212904SdimDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { 454199990Srdivacky if (isStartOfTemplateTypeParameter()) 455193326Sed return ParseTypeParameter(Depth, Position); 456198092Srdivacky 457198092Srdivacky if (Tok.is(tok::kw_template)) 458193326Sed return ParseTemplateTemplateParameter(Depth, Position); 459193326Sed 460193326Sed // If it's none of the above, then it must be a parameter declaration. 461193326Sed // NOTE: This will pick up errors in the closure of the template parameter 462193326Sed // list (e.g., template < ; Check here to implement >> style closures. 463193326Sed return ParseNonTypeTemplateParameter(Depth, Position); 464193326Sed} 465193326Sed 466193326Sed/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). 467193326Sed/// Other kinds of template parameters are parsed in 468193326Sed/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. 469193326Sed/// 470193326Sed/// type-parameter: [C++ temp.param] 471194179Sed/// 'class' ...[opt][C++0x] identifier[opt] 472193326Sed/// 'class' identifier[opt] '=' type-id 473194179Sed/// 'typename' ...[opt][C++0x] identifier[opt] 474193326Sed/// 'typename' identifier[opt] '=' type-id 475212904SdimDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { 476193326Sed assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && 477198092Srdivacky "A type-parameter starts with 'class' or 'typename'"); 478193326Sed 479193326Sed // Consume the 'class' or 'typename' keyword. 480193326Sed bool TypenameKeyword = Tok.is(tok::kw_typename); 481193326Sed SourceLocation KeyLoc = ConsumeToken(); 482193326Sed 483194179Sed // Grab the ellipsis (if given). 484194179Sed bool Ellipsis = false; 485194179Sed SourceLocation EllipsisLoc; 486194179Sed if (Tok.is(tok::ellipsis)) { 487194179Sed Ellipsis = true; 488194179Sed EllipsisLoc = ConsumeToken(); 489198092Srdivacky 490226890Sdim Diag(EllipsisLoc, 491252723Sdim getLangOpts().CPlusPlus11 492226890Sdim ? diag::warn_cxx98_compat_variadic_templates 493226890Sdim : diag::ext_variadic_templates); 494194179Sed } 495198092Srdivacky 496193326Sed // Grab the template parameter name (if given) 497193326Sed SourceLocation NameLoc; 498193326Sed IdentifierInfo* ParamName = 0; 499198092Srdivacky if (Tok.is(tok::identifier)) { 500193326Sed ParamName = Tok.getIdentifierInfo(); 501193326Sed NameLoc = ConsumeToken(); 502198092Srdivacky } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || 503235633Sdim Tok.is(tok::greater) || Tok.is(tok::greatergreater)) { 504193326Sed // Unnamed template parameter. Don't have to do anything here, just 505193326Sed // don't consume this token. 506193326Sed } else { 507193326Sed Diag(Tok.getLocation(), diag::err_expected_ident); 508212904Sdim return 0; 509193326Sed } 510198092Srdivacky 511210299Sed // Grab a default argument (if available). 512210299Sed // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 513210299Sed // we introduce the type parameter into the local scope. 514210299Sed SourceLocation EqualLoc; 515212904Sdim ParsedType DefaultArg; 516198092Srdivacky if (Tok.is(tok::equal)) { 517210299Sed EqualLoc = ConsumeToken(); 518235633Sdim DefaultArg = ParseTypeName(/*Range=*/0, 519235633Sdim Declarator::TemplateTypeArgContext).get(); 520193326Sed } 521235633Sdim 522210299Sed return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, Ellipsis, 523210299Sed EllipsisLoc, KeyLoc, ParamName, NameLoc, 524210299Sed Depth, Position, EqualLoc, DefaultArg); 525193326Sed} 526193326Sed 527193326Sed/// ParseTemplateTemplateParameter - Handle the parsing of template 528198092Srdivacky/// template parameters. 529193326Sed/// 530193326Sed/// type-parameter: [C++ temp.param] 531218893Sdim/// 'template' '<' template-parameter-list '>' 'class' 532218893Sdim/// ...[opt] identifier[opt] 533218893Sdim/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] 534218893Sdim/// = id-expression 535212904SdimDecl * 536193326SedParser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { 537193326Sed assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); 538193326Sed 539193326Sed // Handle the template <...> part. 540193326Sed SourceLocation TemplateLoc = ConsumeToken(); 541226890Sdim SmallVector<Decl*,8> TemplateParams; 542193326Sed SourceLocation LAngleLoc, RAngleLoc; 543193326Sed { 544193326Sed ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 545198092Srdivacky if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, 546198092Srdivacky RAngleLoc)) { 547212904Sdim return 0; 548193326Sed } 549193326Sed } 550193326Sed 551193326Sed // Generate a meaningful error if the user forgot to put class before the 552235633Sdim // identifier, comma, or greater. Provide a fixit if the identifier, comma, 553235633Sdim // or greater appear immediately or after 'typename' or 'struct'. In the 554235633Sdim // latter case, replace the keyword with 'class'. 555198092Srdivacky if (!Tok.is(tok::kw_class)) { 556235633Sdim bool Replace = Tok.is(tok::kw_typename) || Tok.is(tok::kw_struct); 557235633Sdim const Token& Next = Replace ? NextToken() : Tok; 558235633Sdim if (Next.is(tok::identifier) || Next.is(tok::comma) || 559235633Sdim Next.is(tok::greater) || Next.is(tok::greatergreater) || 560235633Sdim Next.is(tok::ellipsis)) 561235633Sdim Diag(Tok.getLocation(), diag::err_class_on_template_template_param) 562235633Sdim << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class") 563235633Sdim : FixItHint::CreateInsertion(Tok.getLocation(), "class ")); 564235633Sdim else 565235633Sdim Diag(Tok.getLocation(), diag::err_class_on_template_template_param); 566193326Sed 567235633Sdim if (Replace) 568235633Sdim ConsumeToken(); 569235633Sdim } else 570235633Sdim ConsumeToken(); 571235633Sdim 572218893Sdim // Parse the ellipsis, if given. 573218893Sdim SourceLocation EllipsisLoc; 574218893Sdim if (Tok.is(tok::ellipsis)) { 575218893Sdim EllipsisLoc = ConsumeToken(); 576218893Sdim 577226890Sdim Diag(EllipsisLoc, 578252723Sdim getLangOpts().CPlusPlus11 579226890Sdim ? diag::warn_cxx98_compat_variadic_templates 580226890Sdim : diag::ext_variadic_templates); 581218893Sdim } 582218893Sdim 583193326Sed // Get the identifier, if given. 584193326Sed SourceLocation NameLoc; 585193326Sed IdentifierInfo* ParamName = 0; 586198092Srdivacky if (Tok.is(tok::identifier)) { 587193326Sed ParamName = Tok.getIdentifierInfo(); 588193326Sed NameLoc = ConsumeToken(); 589235633Sdim } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || 590235633Sdim Tok.is(tok::greater) || Tok.is(tok::greatergreater)) { 591193326Sed // Unnamed template parameter. Don't have to do anything here, just 592193326Sed // don't consume this token. 593193326Sed } else { 594193326Sed Diag(Tok.getLocation(), diag::err_expected_ident); 595212904Sdim return 0; 596193326Sed } 597193326Sed 598226890Sdim TemplateParameterList *ParamList = 599193326Sed Actions.ActOnTemplateParameterList(Depth, SourceLocation(), 600193326Sed TemplateLoc, LAngleLoc, 601218893Sdim TemplateParams.data(), 602193326Sed TemplateParams.size(), 603193326Sed RAngleLoc); 604193326Sed 605210299Sed // Grab a default argument (if available). 606210299Sed // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 607210299Sed // we introduce the template parameter into the local scope. 608210299Sed SourceLocation EqualLoc; 609210299Sed ParsedTemplateArgument DefaultArg; 610193326Sed if (Tok.is(tok::equal)) { 611210299Sed EqualLoc = ConsumeToken(); 612210299Sed DefaultArg = ParseTemplateTemplateArgument(); 613210299Sed if (DefaultArg.isInvalid()) { 614199482Srdivacky Diag(Tok.getLocation(), 615199482Srdivacky diag::err_default_template_template_parameter_not_template); 616263509Sdim SkipUntil(tok::comma, tok::greater, tok::greatergreater, 617263509Sdim StopAtSemi | StopBeforeMatch); 618210299Sed } 619193326Sed } 620210299Sed 621210299Sed return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc, 622218893Sdim ParamList, EllipsisLoc, 623218893Sdim ParamName, NameLoc, Depth, 624218893Sdim Position, EqualLoc, DefaultArg); 625193326Sed} 626193326Sed 627193326Sed/// ParseNonTypeTemplateParameter - Handle the parsing of non-type 628198092Srdivacky/// template parameters (e.g., in "template<int Size> class array;"). 629193326Sed/// 630193326Sed/// template-parameter: 631193326Sed/// ... 632193326Sed/// parameter-declaration 633212904SdimDecl * 634193326SedParser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { 635193326Sed // Parse the declaration-specifiers (i.e., the type). 636193326Sed // FIXME: The type should probably be restricted in some way... Not all 637193326Sed // declarators (parts of declarators?) are accepted for parameters. 638221345Sdim DeclSpec DS(AttrFactory); 639193326Sed ParseDeclarationSpecifiers(DS); 640193326Sed 641193326Sed // Parse this as a typename. 642193326Sed Declarator ParamDecl(DS, Declarator::TemplateParamContext); 643193326Sed ParseDeclarator(ParamDecl); 644212904Sdim if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { 645235633Sdim Diag(Tok.getLocation(), diag::err_expected_template_parameter); 646212904Sdim return 0; 647193326Sed } 648193326Sed 649193326Sed // If there is a default value, parse it. 650210299Sed // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 651210299Sed // we introduce the template parameter into the local scope. 652210299Sed SourceLocation EqualLoc; 653212904Sdim ExprResult DefaultArg; 654193326Sed if (Tok.is(tok::equal)) { 655210299Sed EqualLoc = ConsumeToken(); 656193326Sed 657193326Sed // C++ [temp.param]p15: 658193326Sed // When parsing a default template-argument for a non-type 659193326Sed // template-parameter, the first non-nested > is taken as the 660193326Sed // end of the template-parameter-list rather than a greater-than 661193326Sed // operator. 662198092Srdivacky GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 663235633Sdim EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); 664193326Sed 665210299Sed DefaultArg = ParseAssignmentExpression(); 666193326Sed if (DefaultArg.isInvalid()) 667263509Sdim SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch); 668193326Sed } 669198092Srdivacky 670210299Sed // Create the parameter. 671210299Sed return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl, 672210299Sed Depth, Position, EqualLoc, 673212904Sdim DefaultArg.take()); 674193326Sed} 675193326Sed 676252723Sdim/// \brief Parses a '>' at the end of a template list. 677193326Sed/// 678252723Sdim/// If this function encounters '>>', '>>>', '>=', or '>>=', it tries 679252723Sdim/// to determine if these tokens were supposed to be a '>' followed by 680252723Sdim/// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary. 681193326Sed/// 682252723Sdim/// \param RAngleLoc the location of the consumed '>'. 683193326Sed/// 684252723Sdim/// \param ConsumeLastToken if true, the '>' is not consumed. 685263509Sdim/// 686263509Sdim/// \returns true, if current token does not start with '>', false otherwise. 687252723Sdimbool Parser::ParseGreaterThanInTemplateList(SourceLocation &RAngleLoc, 688252723Sdim bool ConsumeLastToken) { 689245431Sdim // What will be left once we've consumed the '>'. 690245431Sdim tok::TokenKind RemainingToken; 691245431Sdim const char *ReplacementStr = "> >"; 692245431Sdim 693245431Sdim switch (Tok.getKind()) { 694245431Sdim default: 695201361Srdivacky Diag(Tok.getLocation(), diag::err_expected_greater); 696193326Sed return true; 697245431Sdim 698245431Sdim case tok::greater: 699245431Sdim // Determine the location of the '>' token. Only consume this token 700245431Sdim // if the caller asked us to. 701245431Sdim RAngleLoc = Tok.getLocation(); 702245431Sdim if (ConsumeLastToken) 703245431Sdim ConsumeToken(); 704245431Sdim return false; 705245431Sdim 706245431Sdim case tok::greatergreater: 707245431Sdim RemainingToken = tok::greater; 708245431Sdim break; 709245431Sdim 710245431Sdim case tok::greatergreatergreater: 711245431Sdim RemainingToken = tok::greatergreater; 712245431Sdim break; 713245431Sdim 714245431Sdim case tok::greaterequal: 715245431Sdim RemainingToken = tok::equal; 716245431Sdim ReplacementStr = "> ="; 717245431Sdim break; 718245431Sdim 719245431Sdim case tok::greatergreaterequal: 720245431Sdim RemainingToken = tok::greaterequal; 721245431Sdim break; 722201361Srdivacky } 723193326Sed 724245431Sdim // This template-id is terminated by a token which starts with a '>'. Outside 725245431Sdim // C++11, this is now error recovery, and in C++11, this is error recovery if 726245431Sdim // the token isn't '>>'. 727245431Sdim 728193326Sed RAngleLoc = Tok.getLocation(); 729193326Sed 730245431Sdim // The source range of the '>>' or '>=' at the start of the token. 731245431Sdim CharSourceRange ReplacementRange = 732245431Sdim CharSourceRange::getCharRange(RAngleLoc, 733245431Sdim Lexer::AdvanceToTokenCharacter(RAngleLoc, 2, PP.getSourceManager(), 734245431Sdim getLangOpts())); 735193326Sed 736245431Sdim // A hint to put a space between the '>>'s. In order to make the hint as 737245431Sdim // clear as possible, we include the characters either side of the space in 738245431Sdim // the replacement, rather than just inserting a space at SecondCharLoc. 739245431Sdim FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange, 740245431Sdim ReplacementStr); 741193326Sed 742245431Sdim // A hint to put another space after the token, if it would otherwise be 743245431Sdim // lexed differently. 744245431Sdim FixItHint Hint2; 745245431Sdim Token Next = NextToken(); 746245431Sdim if ((RemainingToken == tok::greater || 747245431Sdim RemainingToken == tok::greatergreater) && 748245431Sdim (Next.is(tok::greater) || Next.is(tok::greatergreater) || 749245431Sdim Next.is(tok::greatergreatergreater) || Next.is(tok::equal) || 750245431Sdim Next.is(tok::greaterequal) || Next.is(tok::greatergreaterequal) || 751245431Sdim Next.is(tok::equalequal)) && 752245431Sdim areTokensAdjacent(Tok, Next)) 753245431Sdim Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " "); 754245431Sdim 755245431Sdim unsigned DiagId = diag::err_two_right_angle_brackets_need_space; 756252723Sdim if (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)) 757245431Sdim DiagId = diag::warn_cxx98_compat_two_right_angle_brackets; 758245431Sdim else if (Tok.is(tok::greaterequal)) 759245431Sdim DiagId = diag::err_right_angle_bracket_equal_needs_space; 760245431Sdim Diag(Tok.getLocation(), DiagId) << Hint1 << Hint2; 761245431Sdim 762245431Sdim // Strip the initial '>' from the token. 763245431Sdim if (RemainingToken == tok::equal && Next.is(tok::equal) && 764245431Sdim areTokensAdjacent(Tok, Next)) { 765245431Sdim // Join two adjacent '=' tokens into one, for cases like: 766245431Sdim // void (*p)() = f<int>; 767245431Sdim // return f<int>==p; 768193326Sed ConsumeToken(); 769245431Sdim Tok.setKind(tok::equalequal); 770245431Sdim Tok.setLength(Tok.getLength() + 1); 771245431Sdim } else { 772245431Sdim Tok.setKind(RemainingToken); 773245431Sdim Tok.setLength(Tok.getLength() - 1); 774245431Sdim } 775245431Sdim Tok.setLocation(Lexer::AdvanceToTokenCharacter(RAngleLoc, 1, 776245431Sdim PP.getSourceManager(), 777245431Sdim getLangOpts())); 778193326Sed 779245431Sdim if (!ConsumeLastToken) { 780245431Sdim // Since we're not supposed to consume the '>' token, we need to push 781245431Sdim // this token and revert the current token back to the '>'. 782245431Sdim PP.EnterToken(Tok); 783245431Sdim Tok.setKind(tok::greater); 784245431Sdim Tok.setLength(1); 785245431Sdim Tok.setLocation(RAngleLoc); 786245431Sdim } 787193326Sed return false; 788193326Sed} 789198092Srdivacky 790252723Sdim 791252723Sdim/// \brief Parses a template-id that after the template name has 792252723Sdim/// already been parsed. 793252723Sdim/// 794252723Sdim/// This routine takes care of parsing the enclosed template argument 795252723Sdim/// list ('<' template-parameter-list [opt] '>') and placing the 796252723Sdim/// results into a form that can be transferred to semantic analysis. 797252723Sdim/// 798252723Sdim/// \param Template the template declaration produced by isTemplateName 799252723Sdim/// 800252723Sdim/// \param TemplateNameLoc the source location of the template name 801252723Sdim/// 802252723Sdim/// \param SS if non-NULL, the nested-name-specifier preceding the 803252723Sdim/// template name. 804252723Sdim/// 805252723Sdim/// \param ConsumeLastToken if true, then we will consume the last 806252723Sdim/// token that forms the template-id. Otherwise, we will leave the 807252723Sdim/// last token in the stream (e.g., so that it can be replaced with an 808252723Sdim/// annotation token). 809252723Sdimbool 810252723SdimParser::ParseTemplateIdAfterTemplateName(TemplateTy Template, 811252723Sdim SourceLocation TemplateNameLoc, 812252723Sdim const CXXScopeSpec &SS, 813252723Sdim bool ConsumeLastToken, 814252723Sdim SourceLocation &LAngleLoc, 815252723Sdim TemplateArgList &TemplateArgs, 816252723Sdim SourceLocation &RAngleLoc) { 817252723Sdim assert(Tok.is(tok::less) && "Must have already parsed the template-name"); 818252723Sdim 819252723Sdim // Consume the '<'. 820252723Sdim LAngleLoc = ConsumeToken(); 821252723Sdim 822252723Sdim // Parse the optional template-argument-list. 823252723Sdim bool Invalid = false; 824252723Sdim { 825252723Sdim GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 826252723Sdim if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) 827252723Sdim Invalid = ParseTemplateArgumentList(TemplateArgs); 828252723Sdim 829252723Sdim if (Invalid) { 830252723Sdim // Try to find the closing '>'. 831263509Sdim if (ConsumeLastToken) 832263509Sdim SkipUntil(tok::greater, StopAtSemi); 833263509Sdim else 834263509Sdim SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch); 835252723Sdim return true; 836252723Sdim } 837252723Sdim } 838252723Sdim 839252723Sdim return ParseGreaterThanInTemplateList(RAngleLoc, ConsumeLastToken); 840252723Sdim} 841252723Sdim 842193326Sed/// \brief Replace the tokens that form a simple-template-id with an 843193326Sed/// annotation token containing the complete template-id. 844193326Sed/// 845193326Sed/// The first token in the stream must be the name of a template that 846193326Sed/// is followed by a '<'. This routine will parse the complete 847193326Sed/// simple-template-id and replace the tokens with a single annotation 848193326Sed/// token with one of two different kinds: if the template-id names a 849193326Sed/// type (and \p AllowTypeAnnotation is true), the annotation token is 850193326Sed/// a type annotation that includes the optional nested-name-specifier 851193326Sed/// (\p SS). Otherwise, the annotation token is a template-id 852193326Sed/// annotation that does not include the optional 853193326Sed/// nested-name-specifier. 854193326Sed/// 855193326Sed/// \param Template the declaration of the template named by the first 856193326Sed/// token (an identifier), as returned from \c Action::isTemplateName(). 857193326Sed/// 858252723Sdim/// \param TNK the kind of template that \p Template 859193326Sed/// refers to, as returned from \c Action::isTemplateName(). 860193326Sed/// 861193326Sed/// \param SS if non-NULL, the nested-name-specifier that precedes 862193326Sed/// this template name. 863193326Sed/// 864193326Sed/// \param TemplateKWLoc if valid, specifies that this template-id 865193326Sed/// annotation was preceded by the 'template' keyword and gives the 866193326Sed/// location of that keyword. If invalid (the default), then this 867193326Sed/// template-id was not preceded by a 'template' keyword. 868193326Sed/// 869193326Sed/// \param AllowTypeAnnotation if true (the default), then a 870193326Sed/// simple-template-id that refers to a class template, template 871193326Sed/// template parameter, or other template that produces a type will be 872193326Sed/// replaced with a type annotation token. Otherwise, the 873193326Sed/// simple-template-id is always replaced with a template-id 874193326Sed/// annotation token. 875195099Sed/// 876195099Sed/// If an unrecoverable parse error occurs and no annotation token can be 877195099Sed/// formed, this function returns true. 878195099Sed/// 879195099Sedbool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, 880221345Sdim CXXScopeSpec &SS, 881235633Sdim SourceLocation TemplateKWLoc, 882198893Srdivacky UnqualifiedId &TemplateName, 883193326Sed bool AllowTypeAnnotation) { 884235633Sdim assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++"); 885198893Srdivacky assert(Template && Tok.is(tok::less) && 886193326Sed "Parser isn't at the beginning of a template-id"); 887193326Sed 888193326Sed // Consume the template-name. 889198893Srdivacky SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); 890193326Sed 891193326Sed // Parse the enclosed template argument list. 892193326Sed SourceLocation LAngleLoc, RAngleLoc; 893193326Sed TemplateArgList TemplateArgs; 894198893Srdivacky bool Invalid = ParseTemplateIdAfterTemplateName(Template, 895198893Srdivacky TemplateNameLoc, 896198092Srdivacky SS, false, LAngleLoc, 897198092Srdivacky TemplateArgs, 898193326Sed RAngleLoc); 899198092Srdivacky 900195099Sed if (Invalid) { 901195099Sed // If we failed to parse the template ID but skipped ahead to a >, we're not 902195099Sed // going to be able to form a token annotation. Eat the '>' if present. 903195099Sed if (Tok.is(tok::greater)) 904195099Sed ConsumeToken(); 905195099Sed return true; 906195099Sed } 907193326Sed 908245431Sdim ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs); 909193326Sed 910193326Sed // Build the annotation token. 911193326Sed if (TNK == TNK_Type_template && AllowTypeAnnotation) { 912212904Sdim TypeResult Type 913235633Sdim = Actions.ActOnTemplateIdType(SS, TemplateKWLoc, 914221345Sdim Template, TemplateNameLoc, 915235633Sdim LAngleLoc, TemplateArgsPtr, RAngleLoc); 916195099Sed if (Type.isInvalid()) { 917195099Sed // If we failed to parse the template ID but skipped ahead to a >, we're not 918195099Sed // going to be able to form a token annotation. Eat the '>' if present. 919195099Sed if (Tok.is(tok::greater)) 920195099Sed ConsumeToken(); 921195099Sed return true; 922195099Sed } 923193326Sed 924193326Sed Tok.setKind(tok::annot_typename); 925212904Sdim setTypeAnnotation(Tok, Type.get()); 926221345Sdim if (SS.isNotEmpty()) 927221345Sdim Tok.setLocation(SS.getBeginLoc()); 928193326Sed else if (TemplateKWLoc.isValid()) 929193326Sed Tok.setLocation(TemplateKWLoc); 930198092Srdivacky else 931193326Sed Tok.setLocation(TemplateNameLoc); 932193326Sed } else { 933193326Sed // Build a template-id annotation token that can be processed 934193326Sed // later. 935193326Sed Tok.setKind(tok::annot_template_id); 936198092Srdivacky TemplateIdAnnotation *TemplateId 937235633Sdim = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds); 938193326Sed TemplateId->TemplateNameLoc = TemplateNameLoc; 939198893Srdivacky if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) { 940198893Srdivacky TemplateId->Name = TemplateName.Identifier; 941198893Srdivacky TemplateId->Operator = OO_None; 942198893Srdivacky } else { 943198893Srdivacky TemplateId->Name = 0; 944198893Srdivacky TemplateId->Operator = TemplateName.OperatorFunctionId.Operator; 945198893Srdivacky } 946221345Sdim TemplateId->SS = SS; 947235633Sdim TemplateId->TemplateKWLoc = TemplateKWLoc; 948212904Sdim TemplateId->Template = Template; 949193326Sed TemplateId->Kind = TNK; 950193326Sed TemplateId->LAngleLoc = LAngleLoc; 951193326Sed TemplateId->RAngleLoc = RAngleLoc; 952199482Srdivacky ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); 953199482Srdivacky for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) 954219077Sdim Args[Arg] = ParsedTemplateArgument(TemplateArgs[Arg]); 955193326Sed Tok.setAnnotationValue(TemplateId); 956193326Sed if (TemplateKWLoc.isValid()) 957193326Sed Tok.setLocation(TemplateKWLoc); 958193326Sed else 959193326Sed Tok.setLocation(TemplateNameLoc); 960193326Sed } 961193326Sed 962193326Sed // Common fields for the annotation token 963193326Sed Tok.setAnnotationEndLoc(RAngleLoc); 964193326Sed 965193326Sed // In case the tokens were cached, have Preprocessor replace them with the 966193326Sed // annotation token. 967193326Sed PP.AnnotateCachedTokens(Tok); 968195099Sed return false; 969193326Sed} 970193326Sed 971193326Sed/// \brief Replaces a template-id annotation token with a type 972193326Sed/// annotation token. 973193326Sed/// 974193326Sed/// If there was a failure when forming the type from the template-id, 975193326Sed/// a type annotation token will still be created, but will have a 976193326Sed/// NULL type pointer to signify an error. 977221345Sdimvoid Parser::AnnotateTemplateIdTokenAsType() { 978193326Sed assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); 979193326Sed 980224145Sdim TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 981193326Sed assert((TemplateId->Kind == TNK_Type_template || 982193326Sed TemplateId->Kind == TNK_Dependent_template_name) && 983193326Sed "Only works for type and dependent templates"); 984198092Srdivacky 985245431Sdim ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), 986193326Sed TemplateId->NumArgs); 987193326Sed 988212904Sdim TypeResult Type 989221345Sdim = Actions.ActOnTemplateIdType(TemplateId->SS, 990235633Sdim TemplateId->TemplateKWLoc, 991221345Sdim TemplateId->Template, 992193326Sed TemplateId->TemplateNameLoc, 993198092Srdivacky TemplateId->LAngleLoc, 994193326Sed TemplateArgsPtr, 995193326Sed TemplateId->RAngleLoc); 996193326Sed // Create the new "type" annotation token. 997193326Sed Tok.setKind(tok::annot_typename); 998212904Sdim setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get()); 999221345Sdim if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name. 1000221345Sdim Tok.setLocation(TemplateId->SS.getBeginLoc()); 1001203955Srdivacky // End location stays the same 1002193326Sed 1003198954Srdivacky // Replace the template-id annotation token, and possible the scope-specifier 1004198954Srdivacky // that precedes it, with the typename annotation token. 1005198954Srdivacky PP.AnnotateCachedTokens(Tok); 1006193326Sed} 1007193326Sed 1008199482Srdivacky/// \brief Determine whether the given token can end a template argument. 1009199482Srdivackystatic bool isEndOfTemplateArgument(Token Tok) { 1010199482Srdivacky return Tok.is(tok::comma) || Tok.is(tok::greater) || 1011199482Srdivacky Tok.is(tok::greatergreater); 1012199482Srdivacky} 1013199482Srdivacky 1014199482Srdivacky/// \brief Parse a C++ template template argument. 1015199482SrdivackyParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { 1016199482Srdivacky if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && 1017199482Srdivacky !Tok.is(tok::annot_cxxscope)) 1018199482Srdivacky return ParsedTemplateArgument(); 1019199482Srdivacky 1020199482Srdivacky // C++0x [temp.arg.template]p1: 1021199482Srdivacky // A template-argument for a template template-parameter shall be the name 1022223017Sdim // of a class template or an alias template, expressed as id-expression. 1023199482Srdivacky // 1024223017Sdim // We parse an id-expression that refers to a class template or alias 1025223017Sdim // template. The grammar we parse is: 1026199482Srdivacky // 1027218893Sdim // nested-name-specifier[opt] template[opt] identifier ...[opt] 1028199482Srdivacky // 1029199482Srdivacky // followed by a token that terminates a template argument, such as ',', 1030199482Srdivacky // '>', or (in some cases) '>>'. 1031199482Srdivacky CXXScopeSpec SS; // nested-name-specifier, if present 1032212904Sdim ParseOptionalCXXScopeSpecifier(SS, ParsedType(), 1033199482Srdivacky /*EnteringContext=*/false); 1034199482Srdivacky 1035218893Sdim ParsedTemplateArgument Result; 1036218893Sdim SourceLocation EllipsisLoc; 1037199482Srdivacky if (SS.isSet() && Tok.is(tok::kw_template)) { 1038199482Srdivacky // Parse the optional 'template' keyword following the 1039199482Srdivacky // nested-name-specifier. 1040235633Sdim SourceLocation TemplateKWLoc = ConsumeToken(); 1041199482Srdivacky 1042199482Srdivacky if (Tok.is(tok::identifier)) { 1043199482Srdivacky // We appear to have a dependent template name. 1044199482Srdivacky UnqualifiedId Name; 1045199482Srdivacky Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 1046199482Srdivacky ConsumeToken(); // the identifier 1047199482Srdivacky 1048218893Sdim // Parse the ellipsis. 1049218893Sdim if (Tok.is(tok::ellipsis)) 1050218893Sdim EllipsisLoc = ConsumeToken(); 1051218893Sdim 1052199482Srdivacky // If the next token signals the end of a template argument, 1053199482Srdivacky // then we have a dependent template name that could be a template 1054199482Srdivacky // template argument. 1055210299Sed TemplateTy Template; 1056210299Sed if (isEndOfTemplateArgument(Tok) && 1057235633Sdim Actions.ActOnDependentTemplateName(getCurScope(), 1058235633Sdim SS, TemplateKWLoc, Name, 1059212904Sdim /*ObjectType=*/ ParsedType(), 1060210299Sed /*EnteringContext=*/false, 1061210299Sed Template)) 1062218893Sdim Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); 1063210299Sed } 1064199482Srdivacky } else if (Tok.is(tok::identifier)) { 1065199482Srdivacky // We may have a (non-dependent) template name. 1066199482Srdivacky TemplateTy Template; 1067199482Srdivacky UnqualifiedId Name; 1068199482Srdivacky Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 1069199482Srdivacky ConsumeToken(); // the identifier 1070199482Srdivacky 1071218893Sdim // Parse the ellipsis. 1072218893Sdim if (Tok.is(tok::ellipsis)) 1073218893Sdim EllipsisLoc = ConsumeToken(); 1074218893Sdim 1075199482Srdivacky if (isEndOfTemplateArgument(Tok)) { 1076208600Srdivacky bool MemberOfUnknownSpecialization; 1077212904Sdim TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS, 1078212904Sdim /*hasTemplateKeyword=*/false, 1079212904Sdim Name, 1080212904Sdim /*ObjectType=*/ ParsedType(), 1081199482Srdivacky /*EnteringContext=*/false, 1082208600Srdivacky Template, 1083208600Srdivacky MemberOfUnknownSpecialization); 1084199482Srdivacky if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { 1085199482Srdivacky // We have an id-expression that refers to a class template or 1086223017Sdim // (C++0x) alias template. 1087218893Sdim Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); 1088199482Srdivacky } 1089199482Srdivacky } 1090199482Srdivacky } 1091199482Srdivacky 1092218893Sdim // If this is a pack expansion, build it as such. 1093218893Sdim if (EllipsisLoc.isValid() && !Result.isInvalid()) 1094218893Sdim Result = Actions.ActOnPackExpansion(Result, EllipsisLoc); 1095218893Sdim 1096218893Sdim return Result; 1097199482Srdivacky} 1098199482Srdivacky 1099193326Sed/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). 1100193326Sed/// 1101193326Sed/// template-argument: [C++ 14.2] 1102194711Sed/// constant-expression 1103193326Sed/// type-id 1104193326Sed/// id-expression 1105199482SrdivackyParsedTemplateArgument Parser::ParseTemplateArgument() { 1106193326Sed // C++ [temp.arg]p2: 1107193326Sed // In a template-argument, an ambiguity between a type-id and an 1108193326Sed // expression is resolved to a type-id, regardless of the form of 1109193326Sed // the corresponding template-parameter. 1110193326Sed // 1111199482Srdivacky // Therefore, we initially try to parse a type-id. 1112193326Sed if (isCXXTypeId(TypeIdAsTemplateArgument)) { 1113199482Srdivacky SourceLocation Loc = Tok.getLocation(); 1114218893Sdim TypeResult TypeArg = ParseTypeName(/*Range=*/0, 1115218893Sdim Declarator::TemplateTypeArgContext); 1116193326Sed if (TypeArg.isInvalid()) 1117199482Srdivacky return ParsedTemplateArgument(); 1118199482Srdivacky 1119212904Sdim return ParsedTemplateArgument(ParsedTemplateArgument::Type, 1120212904Sdim TypeArg.get().getAsOpaquePtr(), 1121199482Srdivacky Loc); 1122193326Sed } 1123199482Srdivacky 1124199482Srdivacky // Try to parse a template template argument. 1125199482Srdivacky { 1126199482Srdivacky TentativeParsingAction TPA(*this); 1127193326Sed 1128199482Srdivacky ParsedTemplateArgument TemplateTemplateArgument 1129199482Srdivacky = ParseTemplateTemplateArgument(); 1130199482Srdivacky if (!TemplateTemplateArgument.isInvalid()) { 1131199482Srdivacky TPA.Commit(); 1132199482Srdivacky return TemplateTemplateArgument; 1133199482Srdivacky } 1134199482Srdivacky 1135199482Srdivacky // Revert this tentative parse to parse a non-type template argument. 1136199482Srdivacky TPA.Revert(); 1137199482Srdivacky } 1138199482Srdivacky 1139199482Srdivacky // Parse a non-type template argument. 1140199482Srdivacky SourceLocation Loc = Tok.getLocation(); 1141235633Sdim ExprResult ExprArg = ParseConstantExpression(MaybeTypeCast); 1142193326Sed if (ExprArg.isInvalid() || !ExprArg.get()) 1143199482Srdivacky return ParsedTemplateArgument(); 1144193326Sed 1145199482Srdivacky return ParsedTemplateArgument(ParsedTemplateArgument::NonType, 1146199482Srdivacky ExprArg.release(), Loc); 1147193326Sed} 1148193326Sed 1149208600Srdivacky/// \brief Determine whether the current tokens can only be parsed as a 1150208600Srdivacky/// template argument list (starting with the '<') and never as a '<' 1151208600Srdivacky/// expression. 1152208600Srdivackybool Parser::IsTemplateArgumentList(unsigned Skip) { 1153208600Srdivacky struct AlwaysRevertAction : TentativeParsingAction { 1154208600Srdivacky AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { } 1155208600Srdivacky ~AlwaysRevertAction() { Revert(); } 1156208600Srdivacky } Tentative(*this); 1157208600Srdivacky 1158208600Srdivacky while (Skip) { 1159208600Srdivacky ConsumeToken(); 1160208600Srdivacky --Skip; 1161208600Srdivacky } 1162208600Srdivacky 1163208600Srdivacky // '<' 1164208600Srdivacky if (!Tok.is(tok::less)) 1165208600Srdivacky return false; 1166208600Srdivacky ConsumeToken(); 1167208600Srdivacky 1168208600Srdivacky // An empty template argument list. 1169208600Srdivacky if (Tok.is(tok::greater)) 1170208600Srdivacky return true; 1171208600Srdivacky 1172208600Srdivacky // See whether we have declaration specifiers, which indicate a type. 1173208600Srdivacky while (isCXXDeclarationSpecifier() == TPResult::True()) 1174208600Srdivacky ConsumeToken(); 1175208600Srdivacky 1176208600Srdivacky // If we have a '>' or a ',' then this is a template argument list. 1177208600Srdivacky return Tok.is(tok::greater) || Tok.is(tok::comma); 1178208600Srdivacky} 1179208600Srdivacky 1180193326Sed/// ParseTemplateArgumentList - Parse a C++ template-argument-list 1181193326Sed/// (C++ [temp.names]). Returns true if there was an error. 1182193326Sed/// 1183193326Sed/// template-argument-list: [C++ 14.2] 1184193326Sed/// template-argument 1185193326Sed/// template-argument-list ',' template-argument 1186198092Srdivackybool 1187199482SrdivackyParser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) { 1188252723Sdim // Template argument lists are constant-evaluation contexts. 1189252723Sdim EnterExpressionEvaluationContext EvalContext(Actions,Sema::ConstantEvaluated); 1190252723Sdim 1191193326Sed while (true) { 1192199482Srdivacky ParsedTemplateArgument Arg = ParseTemplateArgument(); 1193218893Sdim if (Tok.is(tok::ellipsis)) { 1194218893Sdim SourceLocation EllipsisLoc = ConsumeToken(); 1195218893Sdim Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc); 1196218893Sdim } 1197218893Sdim 1198199482Srdivacky if (Arg.isInvalid()) { 1199263509Sdim SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch); 1200193326Sed return true; 1201193326Sed } 1202193326Sed 1203199482Srdivacky // Save this template argument. 1204199482Srdivacky TemplateArgs.push_back(Arg); 1205199482Srdivacky 1206193326Sed // If the next token is a comma, consume it and keep reading 1207193326Sed // arguments. 1208193326Sed if (Tok.isNot(tok::comma)) break; 1209193326Sed 1210193326Sed // Consume the comma. 1211193326Sed ConsumeToken(); 1212193326Sed } 1213193326Sed 1214201361Srdivacky return false; 1215193326Sed} 1216193326Sed 1217198092Srdivacky/// \brief Parse a C++ explicit template instantiation 1218193326Sed/// (C++ [temp.explicit]). 1219193326Sed/// 1220193326Sed/// explicit-instantiation: 1221198092Srdivacky/// 'extern' [opt] 'template' declaration 1222198092Srdivacky/// 1223252723Sdim/// Note that the 'extern' is a GNU extension and C++11 feature. 1224235633SdimDecl *Parser::ParseExplicitInstantiation(unsigned Context, 1225235633Sdim SourceLocation ExternLoc, 1226212904Sdim SourceLocation TemplateLoc, 1227235633Sdim SourceLocation &DeclEnd, 1228235633Sdim AccessSpecifier AS) { 1229212904Sdim // This isn't really required here. 1230245431Sdim ParsingDeclRAIIObject 1231245431Sdim ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); 1232212904Sdim 1233235633Sdim return ParseSingleDeclarationAfterTemplate(Context, 1234198092Srdivacky ParsedTemplateInfo(ExternLoc, 1235198092Srdivacky TemplateLoc), 1236212904Sdim ParsingTemplateParams, 1237235633Sdim DeclEnd, AS); 1238193326Sed} 1239218893Sdim 1240218893SdimSourceRange Parser::ParsedTemplateInfo::getSourceRange() const { 1241218893Sdim if (TemplateParams) 1242218893Sdim return getTemplateParamsRange(TemplateParams->data(), 1243218893Sdim TemplateParams->size()); 1244218893Sdim 1245218893Sdim SourceRange R(TemplateLoc); 1246218893Sdim if (ExternLoc.isValid()) 1247218893Sdim R.setBegin(ExternLoc); 1248218893Sdim return R; 1249218893Sdim} 1250221345Sdim 1251263509Sdimvoid Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) { 1252263509Sdim ((Parser *)P)->ParseLateTemplatedFuncDef(LPT); 1253221345Sdim} 1254221345Sdim 1255221345Sdim/// \brief Late parse a C++ function template in Microsoft mode. 1256263509Sdimvoid Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) { 1257263509Sdim if (!LPT.D) 1258221345Sdim return; 1259221345Sdim 1260221345Sdim // Get the FunctionDecl. 1261263509Sdim FunctionTemplateDecl *FunTmplD = dyn_cast<FunctionTemplateDecl>(LPT.D); 1262252723Sdim FunctionDecl *FunD = 1263263509Sdim FunTmplD ? FunTmplD->getTemplatedDecl() : cast<FunctionDecl>(LPT.D); 1264252723Sdim // Track template parameter depth. 1265252723Sdim TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 1266235633Sdim 1267235633Sdim // To restore the context after late parsing. 1268235633Sdim Sema::ContextRAII GlobalSavedContext(Actions, Actions.CurContext); 1269235633Sdim 1270226890Sdim SmallVector<ParseScope*, 4> TemplateParamScopeStack; 1271226890Sdim 1272252723Sdim // Get the list of DeclContexts to reenter. 1273252723Sdim SmallVector<DeclContext*, 4> DeclContextsToReenter; 1274252723Sdim DeclContext *DD = FunD->getLexicalParent(); 1275252723Sdim while (DD && !DD->isTranslationUnit()) { 1276252723Sdim DeclContextsToReenter.push_back(DD); 1277252723Sdim DD = DD->getLexicalParent(); 1278252723Sdim } 1279252723Sdim 1280252723Sdim // Reenter template scopes from outermost to innermost. 1281263509Sdim SmallVectorImpl<DeclContext *>::reverse_iterator II = 1282252723Sdim DeclContextsToReenter.rbegin(); 1283252723Sdim for (; II != DeclContextsToReenter.rend(); ++II) { 1284252723Sdim if (ClassTemplatePartialSpecializationDecl *MD = 1285252723Sdim dyn_cast_or_null<ClassTemplatePartialSpecializationDecl>(*II)) { 1286252723Sdim TemplateParamScopeStack.push_back( 1287252723Sdim new ParseScope(this, Scope::TemplateParamScope)); 1288252723Sdim Actions.ActOnReenterTemplateScope(getCurScope(), MD); 1289252723Sdim ++CurTemplateDepthTracker; 1290252723Sdim } else if (CXXRecordDecl *MD = dyn_cast_or_null<CXXRecordDecl>(*II)) { 1291263509Sdim bool IsClassTemplate = MD->getDescribedClassTemplate() != 0; 1292252723Sdim TemplateParamScopeStack.push_back( 1293263509Sdim new ParseScope(this, Scope::TemplateParamScope, 1294263509Sdim /*ManageScope*/IsClassTemplate)); 1295252723Sdim Actions.ActOnReenterTemplateScope(getCurScope(), 1296252723Sdim MD->getDescribedClassTemplate()); 1297263509Sdim if (IsClassTemplate) 1298263509Sdim ++CurTemplateDepthTracker; 1299226890Sdim } 1300252723Sdim TemplateParamScopeStack.push_back(new ParseScope(this, Scope::DeclScope)); 1301252723Sdim Actions.PushDeclContext(Actions.getCurScope(), *II); 1302221345Sdim } 1303252723Sdim TemplateParamScopeStack.push_back( 1304252723Sdim new ParseScope(this, Scope::TemplateParamScope)); 1305235633Sdim 1306252723Sdim DeclaratorDecl *Declarator = dyn_cast<DeclaratorDecl>(FunD); 1307252723Sdim if (Declarator && Declarator->getNumTemplateParameterLists() != 0) { 1308252723Sdim Actions.ActOnReenterDeclaratorTemplateScope(getCurScope(), Declarator); 1309252723Sdim ++CurTemplateDepthTracker; 1310252723Sdim } 1311263509Sdim Actions.ActOnReenterTemplateScope(getCurScope(), LPT.D); 1312252723Sdim ++CurTemplateDepthTracker; 1313252723Sdim 1314263509Sdim assert(!LPT.Toks.empty() && "Empty body!"); 1315221345Sdim 1316221345Sdim // Append the current token at the end of the new token stream so that it 1317221345Sdim // doesn't get lost. 1318263509Sdim LPT.Toks.push_back(Tok); 1319263509Sdim PP.EnterTokenStream(LPT.Toks.data(), LPT.Toks.size(), true, false); 1320221345Sdim 1321221345Sdim // Consume the previously pushed token. 1322252723Sdim ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 1323221345Sdim assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) 1324221345Sdim && "Inline method not starting with '{', ':' or 'try'"); 1325221345Sdim 1326221345Sdim // Parse the method body. Function body parsing code is similar enough 1327221345Sdim // to be re-used for method bodies as well. 1328221345Sdim ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); 1329221345Sdim 1330235633Sdim // Recreate the containing function DeclContext. 1331252723Sdim Sema::ContextRAII FunctionSavedContext(Actions, Actions.getContainingDC(FunD)); 1332221345Sdim 1333252723Sdim Actions.ActOnStartOfFunctionDef(getCurScope(), FunD); 1334221345Sdim 1335221345Sdim if (Tok.is(tok::kw_try)) { 1336263509Sdim ParseFunctionTryBlock(LPT.D, FnScope); 1337226890Sdim } else { 1338226890Sdim if (Tok.is(tok::colon)) 1339263509Sdim ParseConstructorInitializer(LPT.D); 1340226890Sdim else 1341263509Sdim Actions.ActOnDefaultCtorInitializers(LPT.D); 1342221345Sdim 1343226890Sdim if (Tok.is(tok::l_brace)) { 1344252723Sdim assert((!FunTmplD || FunTmplD->getTemplateParameters()->getDepth() < 1345252723Sdim TemplateParameterDepth) && 1346252723Sdim "TemplateParameterDepth should be greater than the depth of " 1347252723Sdim "current template being instantiated!"); 1348263509Sdim ParseFunctionStatementBody(LPT.D, FnScope); 1349263509Sdim Actions.UnmarkAsLateParsedTemplate(FunD); 1350226890Sdim } else 1351263509Sdim Actions.ActOnFinishFunctionBody(LPT.D, 0); 1352226890Sdim } 1353221345Sdim 1354226890Sdim // Exit scopes. 1355226890Sdim FnScope.Exit(); 1356263509Sdim SmallVectorImpl<ParseScope *>::reverse_iterator I = 1357226890Sdim TemplateParamScopeStack.rbegin(); 1358226890Sdim for (; I != TemplateParamScopeStack.rend(); ++I) 1359226890Sdim delete *I; 1360221345Sdim} 1361221345Sdim 1362221345Sdim/// \brief Lex a delayed template function for late parsing. 1363221345Sdimvoid Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) { 1364221345Sdim tok::TokenKind kind = Tok.getKind(); 1365226890Sdim if (!ConsumeAndStoreFunctionPrologue(Toks)) { 1366226890Sdim // Consume everything up to (and including) the matching right brace. 1367226890Sdim ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1368221345Sdim } 1369221345Sdim 1370221345Sdim // If we're in a function-try-block, we need to store all the catch blocks. 1371221345Sdim if (kind == tok::kw_try) { 1372221345Sdim while (Tok.is(tok::kw_catch)) { 1373221345Sdim ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); 1374221345Sdim ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1375221345Sdim } 1376221345Sdim } 1377221345Sdim} 1378