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" 15249423Sdim#include "RAIIObjectsForParser.h" 16249423Sdim#include "clang/AST/ASTConsumer.h" 17249423Sdim#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, 29226633Sdim AccessSpecifier AS, 30226633Sdim AttributeList *AccessAttrs) { 31226633Sdim ObjCDeclContextSwitch ObjCDC(*this); 32226633Sdim 33226633Sdim if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) { 34234353Sdim return ParseExplicitInstantiation(Context, 35234353Sdim SourceLocation(), ConsumeToken(), 36234353Sdim DeclEnd, AS); 37226633Sdim } 38226633Sdim return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS, 39226633Sdim 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, 62226633Sdim AccessSpecifier AS, 63226633Sdim 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. 72239462Sdim ParsingDeclRAIIObject 73239462Sdim 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; 99251662Sdim TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 100251662Sdim 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; 119226633Sdim SmallVector<Decl*, 4> TemplateParams; 120251662Sdim if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(), 121251662Sdim TemplateParams, LAngleLoc, RAngleLoc)) { 122198092Srdivacky // Skip until the semi-colon or a }. 123263508Sdim SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); 124198092Srdivacky if (Tok.is(tok::semi)) 125198092Srdivacky ConsumeToken(); 126212904Sdim return 0; 127198092Srdivacky } 128193326Sed 129193326Sed ParamLists.push_back( 130251662Sdim Actions.ActOnTemplateParameterList(CurTemplateDepthTracker.getDepth(), 131251662Sdim ExportLoc, 132198092Srdivacky TemplateLoc, LAngleLoc, 133193326Sed TemplateParams.data(), 134193326Sed TemplateParams.size(), RAngleLoc)); 135198092Srdivacky 136198092Srdivacky if (!TemplateParams.empty()) { 137198092Srdivacky isSpecialization = false; 138251662Sdim ++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, 150226633Sdim 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, 169226633Sdim AccessSpecifier AS, 170226633Sdim 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. 176226633Sdim ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo, 177226633Sdim &DiagsFromTParams); 178212904Sdim return 0; 179198092Srdivacky } 180198092Srdivacky 181221345Sdim ParsedAttributesWithRange prefixAttrs(AttrFactory); 182249423Sdim MaybeParseCXX11Attributes(prefixAttrs); 183218893Sdim 184218893Sdim if (Tok.is(tok::kw_using)) 185218893Sdim return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd, 186218893Sdim prefixAttrs); 187218893Sdim 188239462Sdim // Parse the declaration specifiers, stealing any diagnostics from 189239462Sdim // the template parameters. 190221345Sdim ParsingDeclSpec DS(*this, &DiagsFromTParams); 191199990Srdivacky 192202379Srdivacky ParseDeclarationSpecifiers(DS, TemplateInfo, AS, 193202379Srdivacky getDeclSpecContextFromDeclaratorContext(Context)); 194193326Sed 195193326Sed if (Tok.is(tok::semi)) { 196249423Sdim ProhibitAttributes(prefixAttrs); 197193326Sed DeclEnd = ConsumeToken(); 198249423Sdim Decl *Decl = Actions.ParsedFreeStandingDeclSpec( 199249423Sdim getCurScope(), AS, DS, 200249423Sdim TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams 201249423Sdim : MultiTemplateParamsArg(), 202249423Sdim TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation); 203198893Srdivacky DS.complete(Decl); 204198893Srdivacky return Decl; 205193326Sed } 206193326Sed 207249423Sdim // Move the attributes from the prefix into the DS. 208249423Sdim if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) 209249423Sdim ProhibitAttributes(prefixAttrs); 210249423Sdim else 211249423Sdim DS.takeAttributesFrom(prefixAttrs); 212249423Sdim 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 }. 219263508Sdim SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); 220193326Sed if (Tok.is(tok::semi)) 221193326Sed ConsumeToken(); 222212904Sdim return 0; 223193326Sed } 224198092Srdivacky 225243830Sdim LateParsedAttrList LateParsedAttrs(true); 226234353Sdim if (DeclaratorInfo.isFunctionDeclarator()) 227234353Sdim MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); 228234353Sdim 229193326Sed if (DeclaratorInfo.isFunctionDeclarator() && 230210299Sed isStartOfFunctionDefinition(DeclaratorInfo)) { 231193326Sed if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 232234353Sdim // Recover by ignoring the 'typedef'. This was probably supposed to be 233234353Sdim // the 'typename' keyword, which we should have already suggested adding 234234353Sdim // if it's appropriate. 235234353Sdim Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef) 236234353Sdim << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); 237234353Sdim DS.ClearStorageClassSpecs(); 238193326Sed } 239263508Sdim 240263508Sdim if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { 241263508Sdim if (DeclaratorInfo.getName().getKind() != UnqualifiedId::IK_TemplateId) { 242263508Sdim // If the declarator-id is not a template-id, issue a diagnostic and 243263508Sdim // recover by ignoring the 'template' keyword. 244263508Sdim Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0; 245263508Sdim return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(), 246263508Sdim &LateParsedAttrs); 247263508Sdim } else { 248263508Sdim SourceLocation LAngleLoc 249263508Sdim = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); 250263508Sdim Diag(DeclaratorInfo.getIdentifierLoc(), 251263508Sdim diag::err_explicit_instantiation_with_definition) 252263508Sdim << SourceRange(TemplateInfo.TemplateLoc) 253263508Sdim << FixItHint::CreateInsertion(LAngleLoc, "<>"); 254263508Sdim 255263508Sdim // Recover as if it were an explicit specialization. 256263508Sdim TemplateParameterLists FakedParamLists; 257263508Sdim FakedParamLists.push_back(Actions.ActOnTemplateParameterList( 258263508Sdim 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, 259263508Sdim LAngleLoc)); 260263508Sdim 261263508Sdim return ParseFunctionDefinition( 262263508Sdim DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists, 263263508Sdim /*isSpecialization=*/true, 264263508Sdim /*LastParamListWasEmpty=*/true), 265263508Sdim &LateParsedAttrs); 266263508Sdim } 267263508Sdim } 268234353Sdim return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo, 269234353Sdim &LateParsedAttrs); 270193326Sed } 271193326Sed 272249423Sdim // Parse this declaration. 273249423Sdim Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, 274249423Sdim TemplateInfo); 275249423Sdim 276249423Sdim if (Tok.is(tok::comma)) { 277249423Sdim Diag(Tok, diag::err_multiple_template_declarators) 278249423Sdim << (int)TemplateInfo.Kind; 279263508Sdim SkipUntil(tok::semi); 280249423Sdim return ThisDecl; 281249423Sdim } 282249423Sdim 283249423Sdim // Eat the semi colon after the declaration. 284249423Sdim ExpectAndConsumeSemi(diag::err_expected_semi_declaration); 285249423Sdim if (LateParsedAttrs.size() > 0) 286249423Sdim ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false); 287249423Sdim DeclaratorInfo.complete(ThisDecl); 288249423Sdim 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, 301226633Sdim 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. 312234353Sdim bool Failed = false; 313234353Sdim if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater)) 314234353Sdim Failed = ParseTemplateParameterList(Depth, TemplateParams); 315234353Sdim 316234353Sdim if (Tok.is(tok::greatergreater)) { 317239462Sdim // No diagnostic required here: a template-parameter-list can only be 318239462Sdim // followed by a declaration or, for a template template parameter, the 319239462Sdim // 'class' keyword. Therefore, the second '>' will be diagnosed later. 320239462Sdim // This matters for elegant diagnosis of: 321239462Sdim // template<template<typename>> struct S; 322234353Sdim Tok.setKind(tok::greater); 323234353Sdim RAngleLoc = Tok.getLocation(); 324234353Sdim Tok.setLocation(Tok.getLocation().getLocWithOffset(1)); 325234353Sdim } else if (Tok.is(tok::greater)) 326193326Sed RAngleLoc = ConsumeToken(); 327234353Sdim else if (Failed) { 328234353Sdim Diag(Tok.getLocation(), diag::err_expected_greater); 329234353Sdim 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, 344226633Sdim 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. 352263508Sdim SkipUntil(tok::comma, tok::greater, tok::greatergreater, 353263508Sdim StopAtSemi | StopBeforeMatch); 354193326Sed } 355198092Srdivacky 356249423Sdim // Did we find a comma or the end of the template parameter list? 357198092Srdivacky if (Tok.is(tok::comma)) { 358193326Sed ConsumeToken(); 359234353Sdim } 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); 367263508Sdim SkipUntil(tok::comma, tok::greater, tok::greatergreater, 368263508Sdim 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 490226633Sdim Diag(EllipsisLoc, 491249423Sdim getLangOpts().CPlusPlus11 492226633Sdim ? diag::warn_cxx98_compat_variadic_templates 493226633Sdim : 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) || 503234353Sdim 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(); 518234353Sdim DefaultArg = ParseTypeName(/*Range=*/0, 519234353Sdim Declarator::TemplateTypeArgContext).get(); 520193326Sed } 521234353Sdim 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(); 541226633Sdim 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 552234353Sdim // identifier, comma, or greater. Provide a fixit if the identifier, comma, 553234353Sdim // or greater appear immediately or after 'typename' or 'struct'. In the 554234353Sdim // latter case, replace the keyword with 'class'. 555198092Srdivacky if (!Tok.is(tok::kw_class)) { 556234353Sdim bool Replace = Tok.is(tok::kw_typename) || Tok.is(tok::kw_struct); 557234353Sdim const Token& Next = Replace ? NextToken() : Tok; 558234353Sdim if (Next.is(tok::identifier) || Next.is(tok::comma) || 559234353Sdim Next.is(tok::greater) || Next.is(tok::greatergreater) || 560234353Sdim Next.is(tok::ellipsis)) 561234353Sdim Diag(Tok.getLocation(), diag::err_class_on_template_template_param) 562234353Sdim << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class") 563234353Sdim : FixItHint::CreateInsertion(Tok.getLocation(), "class ")); 564234353Sdim else 565234353Sdim Diag(Tok.getLocation(), diag::err_class_on_template_template_param); 566193326Sed 567234353Sdim if (Replace) 568234353Sdim ConsumeToken(); 569234353Sdim } else 570234353Sdim ConsumeToken(); 571234353Sdim 572218893Sdim // Parse the ellipsis, if given. 573218893Sdim SourceLocation EllipsisLoc; 574218893Sdim if (Tok.is(tok::ellipsis)) { 575218893Sdim EllipsisLoc = ConsumeToken(); 576218893Sdim 577226633Sdim Diag(EllipsisLoc, 578249423Sdim getLangOpts().CPlusPlus11 579226633Sdim ? diag::warn_cxx98_compat_variadic_templates 580226633Sdim : 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(); 589234353Sdim } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || 590234353Sdim 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 598226633Sdim 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); 616263508Sdim SkipUntil(tok::comma, tok::greater, tok::greatergreater, 617263508Sdim 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) { 645234353Sdim 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); 663234982Sdim EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); 664193326Sed 665210299Sed DefaultArg = ParseAssignmentExpression(); 666193326Sed if (DefaultArg.isInvalid()) 667263508Sdim 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 676249423Sdim/// \brief Parses a '>' at the end of a template list. 677193326Sed/// 678249423Sdim/// If this function encounters '>>', '>>>', '>=', or '>>=', it tries 679249423Sdim/// to determine if these tokens were supposed to be a '>' followed by 680249423Sdim/// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary. 681193326Sed/// 682249423Sdim/// \param RAngleLoc the location of the consumed '>'. 683193326Sed/// 684249423Sdim/// \param ConsumeLastToken if true, the '>' is not consumed. 685263508Sdim/// 686263508Sdim/// \returns true, if current token does not start with '>', false otherwise. 687249423Sdimbool Parser::ParseGreaterThanInTemplateList(SourceLocation &RAngleLoc, 688249423Sdim bool ConsumeLastToken) { 689239462Sdim // What will be left once we've consumed the '>'. 690239462Sdim tok::TokenKind RemainingToken; 691239462Sdim const char *ReplacementStr = "> >"; 692239462Sdim 693239462Sdim switch (Tok.getKind()) { 694239462Sdim default: 695201361Srdivacky Diag(Tok.getLocation(), diag::err_expected_greater); 696193326Sed return true; 697239462Sdim 698239462Sdim case tok::greater: 699239462Sdim // Determine the location of the '>' token. Only consume this token 700239462Sdim // if the caller asked us to. 701239462Sdim RAngleLoc = Tok.getLocation(); 702239462Sdim if (ConsumeLastToken) 703239462Sdim ConsumeToken(); 704239462Sdim return false; 705239462Sdim 706239462Sdim case tok::greatergreater: 707239462Sdim RemainingToken = tok::greater; 708239462Sdim break; 709239462Sdim 710239462Sdim case tok::greatergreatergreater: 711239462Sdim RemainingToken = tok::greatergreater; 712239462Sdim break; 713239462Sdim 714239462Sdim case tok::greaterequal: 715239462Sdim RemainingToken = tok::equal; 716239462Sdim ReplacementStr = "> ="; 717239462Sdim break; 718239462Sdim 719239462Sdim case tok::greatergreaterequal: 720239462Sdim RemainingToken = tok::greaterequal; 721239462Sdim break; 722201361Srdivacky } 723193326Sed 724239462Sdim // This template-id is terminated by a token which starts with a '>'. Outside 725239462Sdim // C++11, this is now error recovery, and in C++11, this is error recovery if 726239462Sdim // the token isn't '>>'. 727239462Sdim 728193326Sed RAngleLoc = Tok.getLocation(); 729193326Sed 730239462Sdim // The source range of the '>>' or '>=' at the start of the token. 731239462Sdim CharSourceRange ReplacementRange = 732239462Sdim CharSourceRange::getCharRange(RAngleLoc, 733239462Sdim Lexer::AdvanceToTokenCharacter(RAngleLoc, 2, PP.getSourceManager(), 734239462Sdim getLangOpts())); 735193326Sed 736239462Sdim // A hint to put a space between the '>>'s. In order to make the hint as 737239462Sdim // clear as possible, we include the characters either side of the space in 738239462Sdim // the replacement, rather than just inserting a space at SecondCharLoc. 739239462Sdim FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange, 740239462Sdim ReplacementStr); 741193326Sed 742239462Sdim // A hint to put another space after the token, if it would otherwise be 743239462Sdim // lexed differently. 744239462Sdim FixItHint Hint2; 745239462Sdim Token Next = NextToken(); 746239462Sdim if ((RemainingToken == tok::greater || 747239462Sdim RemainingToken == tok::greatergreater) && 748239462Sdim (Next.is(tok::greater) || Next.is(tok::greatergreater) || 749239462Sdim Next.is(tok::greatergreatergreater) || Next.is(tok::equal) || 750239462Sdim Next.is(tok::greaterequal) || Next.is(tok::greatergreaterequal) || 751239462Sdim Next.is(tok::equalequal)) && 752239462Sdim areTokensAdjacent(Tok, Next)) 753239462Sdim Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " "); 754239462Sdim 755239462Sdim unsigned DiagId = diag::err_two_right_angle_brackets_need_space; 756249423Sdim if (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)) 757239462Sdim DiagId = diag::warn_cxx98_compat_two_right_angle_brackets; 758239462Sdim else if (Tok.is(tok::greaterequal)) 759239462Sdim DiagId = diag::err_right_angle_bracket_equal_needs_space; 760239462Sdim Diag(Tok.getLocation(), DiagId) << Hint1 << Hint2; 761239462Sdim 762239462Sdim // Strip the initial '>' from the token. 763239462Sdim if (RemainingToken == tok::equal && Next.is(tok::equal) && 764239462Sdim areTokensAdjacent(Tok, Next)) { 765239462Sdim // Join two adjacent '=' tokens into one, for cases like: 766239462Sdim // void (*p)() = f<int>; 767239462Sdim // return f<int>==p; 768193326Sed ConsumeToken(); 769239462Sdim Tok.setKind(tok::equalequal); 770239462Sdim Tok.setLength(Tok.getLength() + 1); 771239462Sdim } else { 772239462Sdim Tok.setKind(RemainingToken); 773239462Sdim Tok.setLength(Tok.getLength() - 1); 774239462Sdim } 775239462Sdim Tok.setLocation(Lexer::AdvanceToTokenCharacter(RAngleLoc, 1, 776239462Sdim PP.getSourceManager(), 777239462Sdim getLangOpts())); 778193326Sed 779239462Sdim if (!ConsumeLastToken) { 780239462Sdim // Since we're not supposed to consume the '>' token, we need to push 781239462Sdim // this token and revert the current token back to the '>'. 782239462Sdim PP.EnterToken(Tok); 783239462Sdim Tok.setKind(tok::greater); 784239462Sdim Tok.setLength(1); 785239462Sdim Tok.setLocation(RAngleLoc); 786239462Sdim } 787193326Sed return false; 788193326Sed} 789198092Srdivacky 790249423Sdim 791249423Sdim/// \brief Parses a template-id that after the template name has 792249423Sdim/// already been parsed. 793249423Sdim/// 794249423Sdim/// This routine takes care of parsing the enclosed template argument 795249423Sdim/// list ('<' template-parameter-list [opt] '>') and placing the 796249423Sdim/// results into a form that can be transferred to semantic analysis. 797249423Sdim/// 798249423Sdim/// \param Template the template declaration produced by isTemplateName 799249423Sdim/// 800249423Sdim/// \param TemplateNameLoc the source location of the template name 801249423Sdim/// 802249423Sdim/// \param SS if non-NULL, the nested-name-specifier preceding the 803249423Sdim/// template name. 804249423Sdim/// 805249423Sdim/// \param ConsumeLastToken if true, then we will consume the last 806249423Sdim/// token that forms the template-id. Otherwise, we will leave the 807249423Sdim/// last token in the stream (e.g., so that it can be replaced with an 808249423Sdim/// annotation token). 809249423Sdimbool 810249423SdimParser::ParseTemplateIdAfterTemplateName(TemplateTy Template, 811249423Sdim SourceLocation TemplateNameLoc, 812249423Sdim const CXXScopeSpec &SS, 813249423Sdim bool ConsumeLastToken, 814249423Sdim SourceLocation &LAngleLoc, 815249423Sdim TemplateArgList &TemplateArgs, 816249423Sdim SourceLocation &RAngleLoc) { 817249423Sdim assert(Tok.is(tok::less) && "Must have already parsed the template-name"); 818249423Sdim 819249423Sdim // Consume the '<'. 820249423Sdim LAngleLoc = ConsumeToken(); 821249423Sdim 822249423Sdim // Parse the optional template-argument-list. 823249423Sdim bool Invalid = false; 824249423Sdim { 825249423Sdim GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 826249423Sdim if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) 827249423Sdim Invalid = ParseTemplateArgumentList(TemplateArgs); 828249423Sdim 829249423Sdim if (Invalid) { 830249423Sdim // Try to find the closing '>'. 831263508Sdim if (ConsumeLastToken) 832263508Sdim SkipUntil(tok::greater, StopAtSemi); 833263508Sdim else 834263508Sdim SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch); 835249423Sdim return true; 836249423Sdim } 837249423Sdim } 838249423Sdim 839249423Sdim return ParseGreaterThanInTemplateList(RAngleLoc, ConsumeLastToken); 840249423Sdim} 841249423Sdim 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/// 858249423Sdim/// \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, 881234353Sdim SourceLocation TemplateKWLoc, 882198893Srdivacky UnqualifiedId &TemplateName, 883193326Sed bool AllowTypeAnnotation) { 884234353Sdim 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 908243830Sdim ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs); 909193326Sed 910193326Sed // Build the annotation token. 911193326Sed if (TNK == TNK_Type_template && AllowTypeAnnotation) { 912212904Sdim TypeResult Type 913234353Sdim = Actions.ActOnTemplateIdType(SS, TemplateKWLoc, 914221345Sdim Template, TemplateNameLoc, 915234353Sdim 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 937234982Sdim = 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; 947234353Sdim 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 985243830Sdim ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), 986193326Sed TemplateId->NumArgs); 987193326Sed 988212904Sdim TypeResult Type 989221345Sdim = Actions.ActOnTemplateIdType(TemplateId->SS, 990234353Sdim 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. 1040234353Sdim 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) && 1057234353Sdim Actions.ActOnDependentTemplateName(getCurScope(), 1058234353Sdim 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(); 1141234353Sdim 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) { 1188251662Sdim // Template argument lists are constant-evaluation contexts. 1189251662Sdim EnterExpressionEvaluationContext EvalContext(Actions,Sema::ConstantEvaluated); 1190251662Sdim 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()) { 1199263508Sdim 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/// 1223249423Sdim/// Note that the 'extern' is a GNU extension and C++11 feature. 1224234353SdimDecl *Parser::ParseExplicitInstantiation(unsigned Context, 1225234353Sdim SourceLocation ExternLoc, 1226212904Sdim SourceLocation TemplateLoc, 1227234353Sdim SourceLocation &DeclEnd, 1228234353Sdim AccessSpecifier AS) { 1229212904Sdim // This isn't really required here. 1230239462Sdim ParsingDeclRAIIObject 1231239462Sdim ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); 1232212904Sdim 1233234353Sdim return ParseSingleDeclarationAfterTemplate(Context, 1234198092Srdivacky ParsedTemplateInfo(ExternLoc, 1235198092Srdivacky TemplateLoc), 1236212904Sdim ParsingTemplateParams, 1237234353Sdim 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 1251263508Sdimvoid Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) { 1252263508Sdim ((Parser *)P)->ParseLateTemplatedFuncDef(LPT); 1253221345Sdim} 1254221345Sdim 1255221345Sdim/// \brief Late parse a C++ function template in Microsoft mode. 1256263508Sdimvoid Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) { 1257263508Sdim if (!LPT.D) 1258221345Sdim return; 1259221345Sdim 1260221345Sdim // Get the FunctionDecl. 1261263508Sdim FunctionTemplateDecl *FunTmplD = dyn_cast<FunctionTemplateDecl>(LPT.D); 1262251662Sdim FunctionDecl *FunD = 1263263508Sdim FunTmplD ? FunTmplD->getTemplatedDecl() : cast<FunctionDecl>(LPT.D); 1264251662Sdim // Track template parameter depth. 1265251662Sdim TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 1266234353Sdim 1267234353Sdim // To restore the context after late parsing. 1268234353Sdim Sema::ContextRAII GlobalSavedContext(Actions, Actions.CurContext); 1269234353Sdim 1270226633Sdim SmallVector<ParseScope*, 4> TemplateParamScopeStack; 1271226633Sdim 1272251662Sdim // Get the list of DeclContexts to reenter. 1273251662Sdim SmallVector<DeclContext*, 4> DeclContextsToReenter; 1274251662Sdim DeclContext *DD = FunD->getLexicalParent(); 1275251662Sdim while (DD && !DD->isTranslationUnit()) { 1276251662Sdim DeclContextsToReenter.push_back(DD); 1277251662Sdim DD = DD->getLexicalParent(); 1278251662Sdim } 1279251662Sdim 1280251662Sdim // Reenter template scopes from outermost to innermost. 1281263508Sdim SmallVectorImpl<DeclContext *>::reverse_iterator II = 1282251662Sdim DeclContextsToReenter.rbegin(); 1283251662Sdim for (; II != DeclContextsToReenter.rend(); ++II) { 1284251662Sdim if (ClassTemplatePartialSpecializationDecl *MD = 1285251662Sdim dyn_cast_or_null<ClassTemplatePartialSpecializationDecl>(*II)) { 1286251662Sdim TemplateParamScopeStack.push_back( 1287251662Sdim new ParseScope(this, Scope::TemplateParamScope)); 1288251662Sdim Actions.ActOnReenterTemplateScope(getCurScope(), MD); 1289251662Sdim ++CurTemplateDepthTracker; 1290251662Sdim } else if (CXXRecordDecl *MD = dyn_cast_or_null<CXXRecordDecl>(*II)) { 1291263508Sdim bool IsClassTemplate = MD->getDescribedClassTemplate() != 0; 1292251662Sdim TemplateParamScopeStack.push_back( 1293263508Sdim new ParseScope(this, Scope::TemplateParamScope, 1294263508Sdim /*ManageScope*/IsClassTemplate)); 1295251662Sdim Actions.ActOnReenterTemplateScope(getCurScope(), 1296251662Sdim MD->getDescribedClassTemplate()); 1297263508Sdim if (IsClassTemplate) 1298263508Sdim ++CurTemplateDepthTracker; 1299226633Sdim } 1300251662Sdim TemplateParamScopeStack.push_back(new ParseScope(this, Scope::DeclScope)); 1301251662Sdim Actions.PushDeclContext(Actions.getCurScope(), *II); 1302221345Sdim } 1303251662Sdim TemplateParamScopeStack.push_back( 1304251662Sdim new ParseScope(this, Scope::TemplateParamScope)); 1305234353Sdim 1306251662Sdim DeclaratorDecl *Declarator = dyn_cast<DeclaratorDecl>(FunD); 1307251662Sdim if (Declarator && Declarator->getNumTemplateParameterLists() != 0) { 1308251662Sdim Actions.ActOnReenterDeclaratorTemplateScope(getCurScope(), Declarator); 1309251662Sdim ++CurTemplateDepthTracker; 1310251662Sdim } 1311263508Sdim Actions.ActOnReenterTemplateScope(getCurScope(), LPT.D); 1312251662Sdim ++CurTemplateDepthTracker; 1313251662Sdim 1314263508Sdim 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. 1318263508Sdim LPT.Toks.push_back(Tok); 1319263508Sdim PP.EnterTokenStream(LPT.Toks.data(), LPT.Toks.size(), true, false); 1320221345Sdim 1321221345Sdim // Consume the previously pushed token. 1322249423Sdim 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 1330234353Sdim // Recreate the containing function DeclContext. 1331251662Sdim Sema::ContextRAII FunctionSavedContext(Actions, Actions.getContainingDC(FunD)); 1332221345Sdim 1333251662Sdim Actions.ActOnStartOfFunctionDef(getCurScope(), FunD); 1334221345Sdim 1335221345Sdim if (Tok.is(tok::kw_try)) { 1336263508Sdim ParseFunctionTryBlock(LPT.D, FnScope); 1337226633Sdim } else { 1338226633Sdim if (Tok.is(tok::colon)) 1339263508Sdim ParseConstructorInitializer(LPT.D); 1340226633Sdim else 1341263508Sdim Actions.ActOnDefaultCtorInitializers(LPT.D); 1342221345Sdim 1343226633Sdim if (Tok.is(tok::l_brace)) { 1344251662Sdim assert((!FunTmplD || FunTmplD->getTemplateParameters()->getDepth() < 1345251662Sdim TemplateParameterDepth) && 1346251662Sdim "TemplateParameterDepth should be greater than the depth of " 1347251662Sdim "current template being instantiated!"); 1348263508Sdim ParseFunctionStatementBody(LPT.D, FnScope); 1349263508Sdim Actions.UnmarkAsLateParsedTemplate(FunD); 1350226633Sdim } else 1351263508Sdim Actions.ActOnFinishFunctionBody(LPT.D, 0); 1352226633Sdim } 1353221345Sdim 1354226633Sdim // Exit scopes. 1355226633Sdim FnScope.Exit(); 1356263508Sdim SmallVectorImpl<ParseScope *>::reverse_iterator I = 1357226633Sdim TemplateParamScopeStack.rbegin(); 1358226633Sdim for (; I != TemplateParamScopeStack.rend(); ++I) 1359226633Sdim 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(); 1365226633Sdim if (!ConsumeAndStoreFunctionPrologue(Toks)) { 1366226633Sdim // Consume everything up to (and including) the matching right brace. 1367226633Sdim 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