ParseTemplate.cpp revision 6df6548e44a61c444bd85dccd0398cba047c79b1
1//===--- ParseTemplate.cpp - Template Parsing -----------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements parsing of C++ templates. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Parse/Parser.h" 15#include "clang/Parse/ParseDiagnostic.h" 16#include "clang/Sema/DeclSpec.h" 17#include "clang/Sema/ParsedTemplate.h" 18#include "clang/Sema/Scope.h" 19#include "RAIIObjectsForParser.h" 20#include "clang/AST/DeclTemplate.h" 21#include "clang/AST/ASTConsumer.h" 22using namespace clang; 23 24/// \brief Parse a template declaration, explicit instantiation, or 25/// explicit specialization. 26Decl * 27Parser::ParseDeclarationStartingWithTemplate(unsigned Context, 28 SourceLocation &DeclEnd, 29 AccessSpecifier AS) { 30 ObjCDeclContextSwitch ObjCDC(*this); 31 32 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) { 33 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(), 34 DeclEnd); 35 } 36 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS); 37} 38 39/// \brief RAII class that manages the template parameter depth. 40namespace { 41 class TemplateParameterDepthCounter { 42 unsigned &Depth; 43 unsigned AddedLevels; 44 45 public: 46 explicit TemplateParameterDepthCounter(unsigned &Depth) 47 : Depth(Depth), AddedLevels(0) { } 48 49 ~TemplateParameterDepthCounter() { 50 Depth -= AddedLevels; 51 } 52 53 void operator++() { 54 ++Depth; 55 ++AddedLevels; 56 } 57 58 operator unsigned() const { return Depth; } 59 }; 60} 61 62/// \brief Parse a template declaration or an explicit specialization. 63/// 64/// Template declarations include one or more template parameter lists 65/// and either the function or class template declaration. Explicit 66/// specializations contain one or more 'template < >' prefixes 67/// followed by a (possibly templated) declaration. Since the 68/// syntactic form of both features is nearly identical, we parse all 69/// of the template headers together and let semantic analysis sort 70/// the declarations from the explicit specializations. 71/// 72/// template-declaration: [C++ temp] 73/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration 74/// 75/// explicit-specialization: [ C++ temp.expl.spec] 76/// 'template' '<' '>' declaration 77Decl * 78Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context, 79 SourceLocation &DeclEnd, 80 AccessSpecifier AS) { 81 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && 82 "Token does not start a template declaration."); 83 84 // Enter template-parameter scope. 85 ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 86 87 // Tell the action that names should be checked in the context of 88 // the declaration to come. 89 ParsingDeclRAIIObject ParsingTemplateParams(*this); 90 91 // Parse multiple levels of template headers within this template 92 // parameter scope, e.g., 93 // 94 // template<typename T> 95 // template<typename U> 96 // class A<T>::B { ... }; 97 // 98 // We parse multiple levels non-recursively so that we can build a 99 // single data structure containing all of the template parameter 100 // lists to easily differentiate between the case above and: 101 // 102 // template<typename T> 103 // class A { 104 // template<typename U> class B; 105 // }; 106 // 107 // In the first case, the action for declaring A<T>::B receives 108 // both template parameter lists. In the second case, the action for 109 // defining A<T>::B receives just the inner template parameter list 110 // (and retrieves the outer template parameter list from its 111 // context). 112 bool isSpecialization = true; 113 bool LastParamListWasEmpty = false; 114 TemplateParameterLists ParamLists; 115 TemplateParameterDepthCounter Depth(TemplateParameterDepth); 116 do { 117 // Consume the 'export', if any. 118 SourceLocation ExportLoc; 119 if (Tok.is(tok::kw_export)) { 120 ExportLoc = ConsumeToken(); 121 } 122 123 // Consume the 'template', which should be here. 124 SourceLocation TemplateLoc; 125 if (Tok.is(tok::kw_template)) { 126 TemplateLoc = ConsumeToken(); 127 } else { 128 Diag(Tok.getLocation(), diag::err_expected_template); 129 return 0; 130 } 131 132 // Parse the '<' template-parameter-list '>' 133 SourceLocation LAngleLoc, RAngleLoc; 134 SmallVector<Decl*, 4> TemplateParams; 135 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc, 136 RAngleLoc)) { 137 // Skip until the semi-colon or a }. 138 SkipUntil(tok::r_brace, true, true); 139 if (Tok.is(tok::semi)) 140 ConsumeToken(); 141 return 0; 142 } 143 144 ParamLists.push_back( 145 Actions.ActOnTemplateParameterList(Depth, ExportLoc, 146 TemplateLoc, LAngleLoc, 147 TemplateParams.data(), 148 TemplateParams.size(), RAngleLoc)); 149 150 if (!TemplateParams.empty()) { 151 isSpecialization = false; 152 ++Depth; 153 } else { 154 LastParamListWasEmpty = true; 155 } 156 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); 157 158 // Parse the actual template declaration. 159 return ParseSingleDeclarationAfterTemplate(Context, 160 ParsedTemplateInfo(&ParamLists, 161 isSpecialization, 162 LastParamListWasEmpty), 163 ParsingTemplateParams, 164 DeclEnd, AS); 165} 166 167/// \brief Parse a single declaration that declares a template, 168/// template specialization, or explicit instantiation of a template. 169/// 170/// \param TemplateParams if non-NULL, the template parameter lists 171/// that preceded this declaration. In this case, the declaration is a 172/// template declaration, out-of-line definition of a template, or an 173/// explicit template specialization. When NULL, the declaration is an 174/// explicit template instantiation. 175/// 176/// \param TemplateLoc when TemplateParams is NULL, the location of 177/// the 'template' keyword that indicates that we have an explicit 178/// template instantiation. 179/// 180/// \param DeclEnd will receive the source location of the last token 181/// within this declaration. 182/// 183/// \param AS the access specifier associated with this 184/// declaration. Will be AS_none for namespace-scope declarations. 185/// 186/// \returns the new declaration. 187Decl * 188Parser::ParseSingleDeclarationAfterTemplate( 189 unsigned Context, 190 const ParsedTemplateInfo &TemplateInfo, 191 ParsingDeclRAIIObject &DiagsFromTParams, 192 SourceLocation &DeclEnd, 193 AccessSpecifier AS) { 194 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 195 "Template information required"); 196 197 if (Context == Declarator::MemberContext) { 198 // We are parsing a member template. 199 ParseCXXClassMemberDeclaration(AS, TemplateInfo, &DiagsFromTParams); 200 return 0; 201 } 202 203 ParsedAttributesWithRange prefixAttrs(AttrFactory); 204 MaybeParseCXX0XAttributes(prefixAttrs); 205 206 if (Tok.is(tok::kw_using)) 207 return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd, 208 prefixAttrs); 209 210 // Parse the declaration specifiers, stealing the accumulated 211 // diagnostics from the template parameters. 212 ParsingDeclSpec DS(*this, &DiagsFromTParams); 213 214 DS.takeAttributesFrom(prefixAttrs); 215 216 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, 217 getDeclSpecContextFromDeclaratorContext(Context)); 218 219 if (Tok.is(tok::semi)) { 220 DeclEnd = ConsumeToken(); 221 Decl *Decl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); 222 DS.complete(Decl); 223 return Decl; 224 } 225 226 // Parse the declarator. 227 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context); 228 ParseDeclarator(DeclaratorInfo); 229 // Error parsing the declarator? 230 if (!DeclaratorInfo.hasName()) { 231 // If so, skip until the semi-colon or a }. 232 SkipUntil(tok::r_brace, true, true); 233 if (Tok.is(tok::semi)) 234 ConsumeToken(); 235 return 0; 236 } 237 238 // If we have a declaration or declarator list, handle it. 239 if (isDeclarationAfterDeclarator()) { 240 // Parse this declaration. 241 Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, 242 TemplateInfo); 243 244 if (Tok.is(tok::comma)) { 245 Diag(Tok, diag::err_multiple_template_declarators) 246 << (int)TemplateInfo.Kind; 247 SkipUntil(tok::semi, true, false); 248 return ThisDecl; 249 } 250 251 // Eat the semi colon after the declaration. 252 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration); 253 DeclaratorInfo.complete(ThisDecl); 254 return ThisDecl; 255 } 256 257 if (DeclaratorInfo.isFunctionDeclarator() && 258 isStartOfFunctionDefinition(DeclaratorInfo)) { 259 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 260 Diag(Tok, diag::err_function_declared_typedef); 261 262 if (Tok.is(tok::l_brace)) { 263 // This recovery skips the entire function body. It would be nice 264 // to simply call ParseFunctionDefinition() below, however Sema 265 // assumes the declarator represents a function, not a typedef. 266 ConsumeBrace(); 267 SkipUntil(tok::r_brace, true); 268 } else { 269 SkipUntil(tok::semi); 270 } 271 return 0; 272 } 273 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo); 274 } 275 276 if (DeclaratorInfo.isFunctionDeclarator()) 277 Diag(Tok, diag::err_expected_fn_body); 278 else 279 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator); 280 SkipUntil(tok::semi); 281 return 0; 282} 283 284/// ParseTemplateParameters - Parses a template-parameter-list enclosed in 285/// angle brackets. Depth is the depth of this template-parameter-list, which 286/// is the number of template headers directly enclosing this template header. 287/// TemplateParams is the current list of template parameters we're building. 288/// The template parameter we parse will be added to this list. LAngleLoc and 289/// RAngleLoc will receive the positions of the '<' and '>', respectively, 290/// that enclose this template parameter list. 291/// 292/// \returns true if an error occurred, false otherwise. 293bool Parser::ParseTemplateParameters(unsigned Depth, 294 SmallVectorImpl<Decl*> &TemplateParams, 295 SourceLocation &LAngleLoc, 296 SourceLocation &RAngleLoc) { 297 // Get the template parameter list. 298 if (!Tok.is(tok::less)) { 299 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; 300 return true; 301 } 302 LAngleLoc = ConsumeToken(); 303 304 // Try to parse the template parameter list. 305 if (Tok.is(tok::greater)) 306 RAngleLoc = ConsumeToken(); 307 else if (ParseTemplateParameterList(Depth, TemplateParams)) { 308 if (!Tok.is(tok::greater)) { 309 Diag(Tok.getLocation(), diag::err_expected_greater); 310 return true; 311 } 312 RAngleLoc = ConsumeToken(); 313 } 314 return false; 315} 316 317/// ParseTemplateParameterList - Parse a template parameter list. If 318/// the parsing fails badly (i.e., closing bracket was left out), this 319/// will try to put the token stream in a reasonable position (closing 320/// a statement, etc.) and return false. 321/// 322/// template-parameter-list: [C++ temp] 323/// template-parameter 324/// template-parameter-list ',' template-parameter 325bool 326Parser::ParseTemplateParameterList(unsigned Depth, 327 SmallVectorImpl<Decl*> &TemplateParams) { 328 while (1) { 329 if (Decl *TmpParam 330 = ParseTemplateParameter(Depth, TemplateParams.size())) { 331 TemplateParams.push_back(TmpParam); 332 } else { 333 // If we failed to parse a template parameter, skip until we find 334 // a comma or closing brace. 335 SkipUntil(tok::comma, tok::greater, true, true); 336 } 337 338 // Did we find a comma or the end of the template parmeter list? 339 if (Tok.is(tok::comma)) { 340 ConsumeToken(); 341 } else if (Tok.is(tok::greater)) { 342 // Don't consume this... that's done by template parser. 343 break; 344 } else { 345 // Somebody probably forgot to close the template. Skip ahead and 346 // try to get out of the expression. This error is currently 347 // subsumed by whatever goes on in ParseTemplateParameter. 348 // TODO: This could match >>, and it would be nice to avoid those 349 // silly errors with template <vec<T>>. 350 Diag(Tok.getLocation(), diag::err_expected_comma_greater); 351 SkipUntil(tok::greater, true, true); 352 return false; 353 } 354 } 355 return true; 356} 357 358/// \brief Determine whether the parser is at the start of a template 359/// type parameter. 360bool Parser::isStartOfTemplateTypeParameter() { 361 if (Tok.is(tok::kw_class)) { 362 // "class" may be the start of an elaborated-type-specifier or a 363 // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter. 364 switch (NextToken().getKind()) { 365 case tok::equal: 366 case tok::comma: 367 case tok::greater: 368 case tok::greatergreater: 369 case tok::ellipsis: 370 return true; 371 372 case tok::identifier: 373 // This may be either a type-parameter or an elaborated-type-specifier. 374 // We have to look further. 375 break; 376 377 default: 378 return false; 379 } 380 381 switch (GetLookAheadToken(2).getKind()) { 382 case tok::equal: 383 case tok::comma: 384 case tok::greater: 385 case tok::greatergreater: 386 return true; 387 388 default: 389 return false; 390 } 391 } 392 393 if (Tok.isNot(tok::kw_typename)) 394 return false; 395 396 // C++ [temp.param]p2: 397 // There is no semantic difference between class and typename in a 398 // template-parameter. typename followed by an unqualified-id 399 // names a template type parameter. typename followed by a 400 // qualified-id denotes the type in a non-type 401 // parameter-declaration. 402 Token Next = NextToken(); 403 404 // If we have an identifier, skip over it. 405 if (Next.getKind() == tok::identifier) 406 Next = GetLookAheadToken(2); 407 408 switch (Next.getKind()) { 409 case tok::equal: 410 case tok::comma: 411 case tok::greater: 412 case tok::greatergreater: 413 case tok::ellipsis: 414 return true; 415 416 default: 417 return false; 418 } 419} 420 421/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). 422/// 423/// template-parameter: [C++ temp.param] 424/// type-parameter 425/// parameter-declaration 426/// 427/// type-parameter: (see below) 428/// 'class' ...[opt] identifier[opt] 429/// 'class' identifier[opt] '=' type-id 430/// 'typename' ...[opt] identifier[opt] 431/// 'typename' identifier[opt] '=' type-id 432/// 'template' '<' template-parameter-list '>' 433/// 'class' ...[opt] identifier[opt] 434/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] 435/// = id-expression 436Decl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { 437 if (isStartOfTemplateTypeParameter()) 438 return ParseTypeParameter(Depth, Position); 439 440 if (Tok.is(tok::kw_template)) 441 return ParseTemplateTemplateParameter(Depth, Position); 442 443 // If it's none of the above, then it must be a parameter declaration. 444 // NOTE: This will pick up errors in the closure of the template parameter 445 // list (e.g., template < ; Check here to implement >> style closures. 446 return ParseNonTypeTemplateParameter(Depth, Position); 447} 448 449/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). 450/// Other kinds of template parameters are parsed in 451/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. 452/// 453/// type-parameter: [C++ temp.param] 454/// 'class' ...[opt][C++0x] identifier[opt] 455/// 'class' identifier[opt] '=' type-id 456/// 'typename' ...[opt][C++0x] identifier[opt] 457/// 'typename' identifier[opt] '=' type-id 458Decl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { 459 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && 460 "A type-parameter starts with 'class' or 'typename'"); 461 462 // Consume the 'class' or 'typename' keyword. 463 bool TypenameKeyword = Tok.is(tok::kw_typename); 464 SourceLocation KeyLoc = ConsumeToken(); 465 466 // Grab the ellipsis (if given). 467 bool Ellipsis = false; 468 SourceLocation EllipsisLoc; 469 if (Tok.is(tok::ellipsis)) { 470 Ellipsis = true; 471 EllipsisLoc = ConsumeToken(); 472 473 if (!getLang().CPlusPlus0x) 474 Diag(EllipsisLoc, diag::ext_variadic_templates); 475 } 476 477 // Grab the template parameter name (if given) 478 SourceLocation NameLoc; 479 IdentifierInfo* ParamName = 0; 480 if (Tok.is(tok::identifier)) { 481 ParamName = Tok.getIdentifierInfo(); 482 NameLoc = ConsumeToken(); 483 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || 484 Tok.is(tok::greater)) { 485 // Unnamed template parameter. Don't have to do anything here, just 486 // don't consume this token. 487 } else { 488 Diag(Tok.getLocation(), diag::err_expected_ident); 489 return 0; 490 } 491 492 // Grab a default argument (if available). 493 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 494 // we introduce the type parameter into the local scope. 495 SourceLocation EqualLoc; 496 ParsedType DefaultArg; 497 if (Tok.is(tok::equal)) { 498 EqualLoc = ConsumeToken(); 499 DefaultArg = ParseTypeName().get(); 500 } 501 502 return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, Ellipsis, 503 EllipsisLoc, KeyLoc, ParamName, NameLoc, 504 Depth, Position, EqualLoc, DefaultArg); 505} 506 507/// ParseTemplateTemplateParameter - Handle the parsing of template 508/// template parameters. 509/// 510/// type-parameter: [C++ temp.param] 511/// 'template' '<' template-parameter-list '>' 'class' 512/// ...[opt] identifier[opt] 513/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] 514/// = id-expression 515Decl * 516Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { 517 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); 518 519 // Handle the template <...> part. 520 SourceLocation TemplateLoc = ConsumeToken(); 521 SmallVector<Decl*,8> TemplateParams; 522 SourceLocation LAngleLoc, RAngleLoc; 523 { 524 ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 525 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, 526 RAngleLoc)) { 527 return 0; 528 } 529 } 530 531 // Generate a meaningful error if the user forgot to put class before the 532 // identifier, comma, or greater. 533 if (!Tok.is(tok::kw_class)) { 534 Diag(Tok.getLocation(), diag::err_expected_class_before) 535 << PP.getSpelling(Tok); 536 return 0; 537 } 538 ConsumeToken(); 539 540 // Parse the ellipsis, if given. 541 SourceLocation EllipsisLoc; 542 if (Tok.is(tok::ellipsis)) { 543 EllipsisLoc = ConsumeToken(); 544 545 if (!getLang().CPlusPlus0x) 546 Diag(EllipsisLoc, diag::ext_variadic_templates); 547 } 548 549 // Get the identifier, if given. 550 SourceLocation NameLoc; 551 IdentifierInfo* ParamName = 0; 552 if (Tok.is(tok::identifier)) { 553 ParamName = Tok.getIdentifierInfo(); 554 NameLoc = ConsumeToken(); 555 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) { 556 // Unnamed template parameter. Don't have to do anything here, just 557 // don't consume this token. 558 } else { 559 Diag(Tok.getLocation(), diag::err_expected_ident); 560 return 0; 561 } 562 563 TemplateParameterList *ParamList = 564 Actions.ActOnTemplateParameterList(Depth, SourceLocation(), 565 TemplateLoc, LAngleLoc, 566 TemplateParams.data(), 567 TemplateParams.size(), 568 RAngleLoc); 569 570 // Grab a default argument (if available). 571 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 572 // we introduce the template parameter into the local scope. 573 SourceLocation EqualLoc; 574 ParsedTemplateArgument DefaultArg; 575 if (Tok.is(tok::equal)) { 576 EqualLoc = ConsumeToken(); 577 DefaultArg = ParseTemplateTemplateArgument(); 578 if (DefaultArg.isInvalid()) { 579 Diag(Tok.getLocation(), 580 diag::err_default_template_template_parameter_not_template); 581 static const tok::TokenKind EndToks[] = { 582 tok::comma, tok::greater, tok::greatergreater 583 }; 584 SkipUntil(EndToks, 3, true, true); 585 } 586 } 587 588 return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc, 589 ParamList, EllipsisLoc, 590 ParamName, NameLoc, Depth, 591 Position, EqualLoc, DefaultArg); 592} 593 594/// ParseNonTypeTemplateParameter - Handle the parsing of non-type 595/// template parameters (e.g., in "template<int Size> class array;"). 596/// 597/// template-parameter: 598/// ... 599/// parameter-declaration 600Decl * 601Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { 602 // Parse the declaration-specifiers (i.e., the type). 603 // FIXME: The type should probably be restricted in some way... Not all 604 // declarators (parts of declarators?) are accepted for parameters. 605 DeclSpec DS(AttrFactory); 606 ParseDeclarationSpecifiers(DS); 607 608 // Parse this as a typename. 609 Declarator ParamDecl(DS, Declarator::TemplateParamContext); 610 ParseDeclarator(ParamDecl); 611 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { 612 // This probably shouldn't happen - and it's more of a Sema thing, but 613 // basically we didn't parse the type name because we couldn't associate 614 // it with an AST node. we should just skip to the comma or greater. 615 // TODO: This is currently a placeholder for some kind of Sema Error. 616 Diag(Tok.getLocation(), diag::err_parse_error); 617 SkipUntil(tok::comma, tok::greater, true, true); 618 return 0; 619 } 620 621 // If there is a default value, parse it. 622 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 623 // we introduce the template parameter into the local scope. 624 SourceLocation EqualLoc; 625 ExprResult DefaultArg; 626 if (Tok.is(tok::equal)) { 627 EqualLoc = ConsumeToken(); 628 629 // C++ [temp.param]p15: 630 // When parsing a default template-argument for a non-type 631 // template-parameter, the first non-nested > is taken as the 632 // end of the template-parameter-list rather than a greater-than 633 // operator. 634 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 635 636 DefaultArg = ParseAssignmentExpression(); 637 if (DefaultArg.isInvalid()) 638 SkipUntil(tok::comma, tok::greater, true, true); 639 } 640 641 // Create the parameter. 642 return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl, 643 Depth, Position, EqualLoc, 644 DefaultArg.take()); 645} 646 647/// \brief Parses a template-id that after the template name has 648/// already been parsed. 649/// 650/// This routine takes care of parsing the enclosed template argument 651/// list ('<' template-parameter-list [opt] '>') and placing the 652/// results into a form that can be transferred to semantic analysis. 653/// 654/// \param Template the template declaration produced by isTemplateName 655/// 656/// \param TemplateNameLoc the source location of the template name 657/// 658/// \param SS if non-NULL, the nested-name-specifier preceding the 659/// template name. 660/// 661/// \param ConsumeLastToken if true, then we will consume the last 662/// token that forms the template-id. Otherwise, we will leave the 663/// last token in the stream (e.g., so that it can be replaced with an 664/// annotation token). 665bool 666Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template, 667 SourceLocation TemplateNameLoc, 668 const CXXScopeSpec &SS, 669 bool ConsumeLastToken, 670 SourceLocation &LAngleLoc, 671 TemplateArgList &TemplateArgs, 672 SourceLocation &RAngleLoc) { 673 assert(Tok.is(tok::less) && "Must have already parsed the template-name"); 674 675 // Consume the '<'. 676 LAngleLoc = ConsumeToken(); 677 678 // Parse the optional template-argument-list. 679 bool Invalid = false; 680 { 681 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 682 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) 683 Invalid = ParseTemplateArgumentList(TemplateArgs); 684 685 if (Invalid) { 686 // Try to find the closing '>'. 687 SkipUntil(tok::greater, true, !ConsumeLastToken); 688 689 return true; 690 } 691 } 692 693 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) { 694 Diag(Tok.getLocation(), diag::err_expected_greater); 695 return true; 696 } 697 698 // Determine the location of the '>' or '>>'. Only consume this 699 // token if the caller asked us to. 700 RAngleLoc = Tok.getLocation(); 701 702 if (Tok.is(tok::greatergreater)) { 703 if (!getLang().CPlusPlus0x) { 704 const char *ReplaceStr = "> >"; 705 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater)) 706 ReplaceStr = "> > "; 707 708 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space) 709 << FixItHint::CreateReplacement( 710 SourceRange(Tok.getLocation()), ReplaceStr); 711 } 712 713 Tok.setKind(tok::greater); 714 if (!ConsumeLastToken) { 715 // Since we're not supposed to consume the '>>' token, we need 716 // to insert a second '>' token after the first. 717 PP.EnterToken(Tok); 718 } 719 } else if (ConsumeLastToken) 720 ConsumeToken(); 721 722 return false; 723} 724 725/// \brief Replace the tokens that form a simple-template-id with an 726/// annotation token containing the complete template-id. 727/// 728/// The first token in the stream must be the name of a template that 729/// is followed by a '<'. This routine will parse the complete 730/// simple-template-id and replace the tokens with a single annotation 731/// token with one of two different kinds: if the template-id names a 732/// type (and \p AllowTypeAnnotation is true), the annotation token is 733/// a type annotation that includes the optional nested-name-specifier 734/// (\p SS). Otherwise, the annotation token is a template-id 735/// annotation that does not include the optional 736/// nested-name-specifier. 737/// 738/// \param Template the declaration of the template named by the first 739/// token (an identifier), as returned from \c Action::isTemplateName(). 740/// 741/// \param TemplateNameKind the kind of template that \p Template 742/// refers to, as returned from \c Action::isTemplateName(). 743/// 744/// \param SS if non-NULL, the nested-name-specifier that precedes 745/// this template name. 746/// 747/// \param TemplateKWLoc if valid, specifies that this template-id 748/// annotation was preceded by the 'template' keyword and gives the 749/// location of that keyword. If invalid (the default), then this 750/// template-id was not preceded by a 'template' keyword. 751/// 752/// \param AllowTypeAnnotation if true (the default), then a 753/// simple-template-id that refers to a class template, template 754/// template parameter, or other template that produces a type will be 755/// replaced with a type annotation token. Otherwise, the 756/// simple-template-id is always replaced with a template-id 757/// annotation token. 758/// 759/// If an unrecoverable parse error occurs and no annotation token can be 760/// formed, this function returns true. 761/// 762bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, 763 CXXScopeSpec &SS, 764 UnqualifiedId &TemplateName, 765 SourceLocation TemplateKWLoc, 766 bool AllowTypeAnnotation) { 767 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++"); 768 assert(Template && Tok.is(tok::less) && 769 "Parser isn't at the beginning of a template-id"); 770 771 // Consume the template-name. 772 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); 773 774 // Parse the enclosed template argument list. 775 SourceLocation LAngleLoc, RAngleLoc; 776 TemplateArgList TemplateArgs; 777 bool Invalid = ParseTemplateIdAfterTemplateName(Template, 778 TemplateNameLoc, 779 SS, false, LAngleLoc, 780 TemplateArgs, 781 RAngleLoc); 782 783 if (Invalid) { 784 // If we failed to parse the template ID but skipped ahead to a >, we're not 785 // going to be able to form a token annotation. Eat the '>' if present. 786 if (Tok.is(tok::greater)) 787 ConsumeToken(); 788 return true; 789 } 790 791 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), 792 TemplateArgs.size()); 793 794 // Build the annotation token. 795 if (TNK == TNK_Type_template && AllowTypeAnnotation) { 796 TypeResult Type 797 = Actions.ActOnTemplateIdType(SS, 798 Template, TemplateNameLoc, 799 LAngleLoc, TemplateArgsPtr, 800 RAngleLoc); 801 if (Type.isInvalid()) { 802 // If we failed to parse the template ID but skipped ahead to a >, we're not 803 // going to be able to form a token annotation. Eat the '>' if present. 804 if (Tok.is(tok::greater)) 805 ConsumeToken(); 806 return true; 807 } 808 809 Tok.setKind(tok::annot_typename); 810 setTypeAnnotation(Tok, Type.get()); 811 if (SS.isNotEmpty()) 812 Tok.setLocation(SS.getBeginLoc()); 813 else if (TemplateKWLoc.isValid()) 814 Tok.setLocation(TemplateKWLoc); 815 else 816 Tok.setLocation(TemplateNameLoc); 817 } else { 818 // Build a template-id annotation token that can be processed 819 // later. 820 Tok.setKind(tok::annot_template_id); 821 TemplateIdAnnotation *TemplateId 822 = TemplateIdAnnotation::Allocate(TemplateArgs.size()); 823 TemplateId->TemplateNameLoc = TemplateNameLoc; 824 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) { 825 TemplateId->Name = TemplateName.Identifier; 826 TemplateId->Operator = OO_None; 827 } else { 828 TemplateId->Name = 0; 829 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator; 830 } 831 TemplateId->SS = SS; 832 TemplateId->Template = Template; 833 TemplateId->Kind = TNK; 834 TemplateId->LAngleLoc = LAngleLoc; 835 TemplateId->RAngleLoc = RAngleLoc; 836 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); 837 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) 838 Args[Arg] = ParsedTemplateArgument(TemplateArgs[Arg]); 839 Tok.setAnnotationValue(TemplateId); 840 if (TemplateKWLoc.isValid()) 841 Tok.setLocation(TemplateKWLoc); 842 else 843 Tok.setLocation(TemplateNameLoc); 844 845 TemplateArgsPtr.release(); 846 } 847 848 // Common fields for the annotation token 849 Tok.setAnnotationEndLoc(RAngleLoc); 850 851 // In case the tokens were cached, have Preprocessor replace them with the 852 // annotation token. 853 PP.AnnotateCachedTokens(Tok); 854 return false; 855} 856 857/// \brief Replaces a template-id annotation token with a type 858/// annotation token. 859/// 860/// If there was a failure when forming the type from the template-id, 861/// a type annotation token will still be created, but will have a 862/// NULL type pointer to signify an error. 863void Parser::AnnotateTemplateIdTokenAsType() { 864 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); 865 866 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 867 assert((TemplateId->Kind == TNK_Type_template || 868 TemplateId->Kind == TNK_Dependent_template_name) && 869 "Only works for type and dependent templates"); 870 871 ASTTemplateArgsPtr TemplateArgsPtr(Actions, 872 TemplateId->getTemplateArgs(), 873 TemplateId->NumArgs); 874 875 TypeResult Type 876 = Actions.ActOnTemplateIdType(TemplateId->SS, 877 TemplateId->Template, 878 TemplateId->TemplateNameLoc, 879 TemplateId->LAngleLoc, 880 TemplateArgsPtr, 881 TemplateId->RAngleLoc); 882 // Create the new "type" annotation token. 883 Tok.setKind(tok::annot_typename); 884 setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get()); 885 if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name. 886 Tok.setLocation(TemplateId->SS.getBeginLoc()); 887 // End location stays the same 888 889 // Replace the template-id annotation token, and possible the scope-specifier 890 // that precedes it, with the typename annotation token. 891 PP.AnnotateCachedTokens(Tok); 892} 893 894/// \brief Determine whether the given token can end a template argument. 895static bool isEndOfTemplateArgument(Token Tok) { 896 return Tok.is(tok::comma) || Tok.is(tok::greater) || 897 Tok.is(tok::greatergreater); 898} 899 900/// \brief Parse a C++ template template argument. 901ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { 902 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && 903 !Tok.is(tok::annot_cxxscope)) 904 return ParsedTemplateArgument(); 905 906 // C++0x [temp.arg.template]p1: 907 // A template-argument for a template template-parameter shall be the name 908 // of a class template or an alias template, expressed as id-expression. 909 // 910 // We parse an id-expression that refers to a class template or alias 911 // template. The grammar we parse is: 912 // 913 // nested-name-specifier[opt] template[opt] identifier ...[opt] 914 // 915 // followed by a token that terminates a template argument, such as ',', 916 // '>', or (in some cases) '>>'. 917 CXXScopeSpec SS; // nested-name-specifier, if present 918 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), 919 /*EnteringContext=*/false); 920 921 ParsedTemplateArgument Result; 922 SourceLocation EllipsisLoc; 923 if (SS.isSet() && Tok.is(tok::kw_template)) { 924 // Parse the optional 'template' keyword following the 925 // nested-name-specifier. 926 SourceLocation TemplateLoc = ConsumeToken(); 927 928 if (Tok.is(tok::identifier)) { 929 // We appear to have a dependent template name. 930 UnqualifiedId Name; 931 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 932 ConsumeToken(); // the identifier 933 934 // Parse the ellipsis. 935 if (Tok.is(tok::ellipsis)) 936 EllipsisLoc = ConsumeToken(); 937 938 // If the next token signals the end of a template argument, 939 // then we have a dependent template name that could be a template 940 // template argument. 941 TemplateTy Template; 942 if (isEndOfTemplateArgument(Tok) && 943 Actions.ActOnDependentTemplateName(getCurScope(), TemplateLoc, 944 SS, Name, 945 /*ObjectType=*/ ParsedType(), 946 /*EnteringContext=*/false, 947 Template)) 948 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); 949 } 950 } else if (Tok.is(tok::identifier)) { 951 // We may have a (non-dependent) template name. 952 TemplateTy Template; 953 UnqualifiedId Name; 954 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 955 ConsumeToken(); // the identifier 956 957 // Parse the ellipsis. 958 if (Tok.is(tok::ellipsis)) 959 EllipsisLoc = ConsumeToken(); 960 961 if (isEndOfTemplateArgument(Tok)) { 962 bool MemberOfUnknownSpecialization; 963 TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS, 964 /*hasTemplateKeyword=*/false, 965 Name, 966 /*ObjectType=*/ ParsedType(), 967 /*EnteringContext=*/false, 968 Template, 969 MemberOfUnknownSpecialization); 970 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { 971 // We have an id-expression that refers to a class template or 972 // (C++0x) alias template. 973 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); 974 } 975 } 976 } 977 978 // If this is a pack expansion, build it as such. 979 if (EllipsisLoc.isValid() && !Result.isInvalid()) 980 Result = Actions.ActOnPackExpansion(Result, EllipsisLoc); 981 982 return Result; 983} 984 985/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). 986/// 987/// template-argument: [C++ 14.2] 988/// constant-expression 989/// type-id 990/// id-expression 991ParsedTemplateArgument Parser::ParseTemplateArgument() { 992 // C++ [temp.arg]p2: 993 // In a template-argument, an ambiguity between a type-id and an 994 // expression is resolved to a type-id, regardless of the form of 995 // the corresponding template-parameter. 996 // 997 // Therefore, we initially try to parse a type-id. 998 if (isCXXTypeId(TypeIdAsTemplateArgument)) { 999 SourceLocation Loc = Tok.getLocation(); 1000 TypeResult TypeArg = ParseTypeName(/*Range=*/0, 1001 Declarator::TemplateTypeArgContext); 1002 if (TypeArg.isInvalid()) 1003 return ParsedTemplateArgument(); 1004 1005 return ParsedTemplateArgument(ParsedTemplateArgument::Type, 1006 TypeArg.get().getAsOpaquePtr(), 1007 Loc); 1008 } 1009 1010 // Try to parse a template template argument. 1011 { 1012 TentativeParsingAction TPA(*this); 1013 1014 ParsedTemplateArgument TemplateTemplateArgument 1015 = ParseTemplateTemplateArgument(); 1016 if (!TemplateTemplateArgument.isInvalid()) { 1017 TPA.Commit(); 1018 return TemplateTemplateArgument; 1019 } 1020 1021 // Revert this tentative parse to parse a non-type template argument. 1022 TPA.Revert(); 1023 } 1024 1025 // Parse a non-type template argument. 1026 SourceLocation Loc = Tok.getLocation(); 1027 ExprResult ExprArg = ParseConstantExpression(); 1028 if (ExprArg.isInvalid() || !ExprArg.get()) 1029 return ParsedTemplateArgument(); 1030 1031 return ParsedTemplateArgument(ParsedTemplateArgument::NonType, 1032 ExprArg.release(), Loc); 1033} 1034 1035/// \brief Determine whether the current tokens can only be parsed as a 1036/// template argument list (starting with the '<') and never as a '<' 1037/// expression. 1038bool Parser::IsTemplateArgumentList(unsigned Skip) { 1039 struct AlwaysRevertAction : TentativeParsingAction { 1040 AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { } 1041 ~AlwaysRevertAction() { Revert(); } 1042 } Tentative(*this); 1043 1044 while (Skip) { 1045 ConsumeToken(); 1046 --Skip; 1047 } 1048 1049 // '<' 1050 if (!Tok.is(tok::less)) 1051 return false; 1052 ConsumeToken(); 1053 1054 // An empty template argument list. 1055 if (Tok.is(tok::greater)) 1056 return true; 1057 1058 // See whether we have declaration specifiers, which indicate a type. 1059 while (isCXXDeclarationSpecifier() == TPResult::True()) 1060 ConsumeToken(); 1061 1062 // If we have a '>' or a ',' then this is a template argument list. 1063 return Tok.is(tok::greater) || Tok.is(tok::comma); 1064} 1065 1066/// ParseTemplateArgumentList - Parse a C++ template-argument-list 1067/// (C++ [temp.names]). Returns true if there was an error. 1068/// 1069/// template-argument-list: [C++ 14.2] 1070/// template-argument 1071/// template-argument-list ',' template-argument 1072bool 1073Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) { 1074 while (true) { 1075 ParsedTemplateArgument Arg = ParseTemplateArgument(); 1076 if (Tok.is(tok::ellipsis)) { 1077 SourceLocation EllipsisLoc = ConsumeToken(); 1078 Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc); 1079 } 1080 1081 if (Arg.isInvalid()) { 1082 SkipUntil(tok::comma, tok::greater, true, true); 1083 return true; 1084 } 1085 1086 // Save this template argument. 1087 TemplateArgs.push_back(Arg); 1088 1089 // If the next token is a comma, consume it and keep reading 1090 // arguments. 1091 if (Tok.isNot(tok::comma)) break; 1092 1093 // Consume the comma. 1094 ConsumeToken(); 1095 } 1096 1097 return false; 1098} 1099 1100/// \brief Parse a C++ explicit template instantiation 1101/// (C++ [temp.explicit]). 1102/// 1103/// explicit-instantiation: 1104/// 'extern' [opt] 'template' declaration 1105/// 1106/// Note that the 'extern' is a GNU extension and C++0x feature. 1107Decl *Parser::ParseExplicitInstantiation(SourceLocation ExternLoc, 1108 SourceLocation TemplateLoc, 1109 SourceLocation &DeclEnd) { 1110 // This isn't really required here. 1111 ParsingDeclRAIIObject ParsingTemplateParams(*this); 1112 1113 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext, 1114 ParsedTemplateInfo(ExternLoc, 1115 TemplateLoc), 1116 ParsingTemplateParams, 1117 DeclEnd, AS_none); 1118} 1119 1120SourceRange Parser::ParsedTemplateInfo::getSourceRange() const { 1121 if (TemplateParams) 1122 return getTemplateParamsRange(TemplateParams->data(), 1123 TemplateParams->size()); 1124 1125 SourceRange R(TemplateLoc); 1126 if (ExternLoc.isValid()) 1127 R.setBegin(ExternLoc); 1128 return R; 1129} 1130 1131void Parser::LateTemplateParserCallback(void *P, const FunctionDecl *FD) { 1132 ((Parser*)P)->LateTemplateParser(FD); 1133} 1134 1135 1136void Parser::LateTemplateParser(const FunctionDecl *FD) { 1137 LateParsedTemplatedFunction *LPT = LateParsedTemplateMap[FD]; 1138 if (LPT) { 1139 ParseLateTemplatedFuncDef(*LPT); 1140 return; 1141 } 1142 1143 llvm_unreachable("Late templated function without associated lexed tokens"); 1144} 1145 1146/// \brief Late parse a C++ function template in Microsoft mode. 1147void Parser::ParseLateTemplatedFuncDef(LateParsedTemplatedFunction &LMT) { 1148 if(!LMT.D) 1149 return; 1150 1151 // If this is a member template, introduce the template parameter scope. 1152 ParseScope TemplateScope(this, Scope::TemplateParamScope); 1153 1154 // Get the FunctionDecl. 1155 FunctionDecl *FD = 0; 1156 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(LMT.D)) 1157 FD = FunTmpl->getTemplatedDecl(); 1158 else 1159 FD = cast<FunctionDecl>(LMT.D); 1160 1161 // Reinject the template parameters. 1162 SmallVector<ParseScope*, 4> TemplateParamScopeStack; 1163 DeclaratorDecl* Declarator = dyn_cast<DeclaratorDecl>(FD); 1164 if (Declarator && Declarator->getNumTemplateParameterLists() != 0) { 1165 Actions.ActOnReenterDeclaratorTemplateScope(getCurScope(), Declarator); 1166 Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D); 1167 } else { 1168 Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D); 1169 1170 // Get the list of DeclContext to reenter. 1171 SmallVector<DeclContext*, 4> DeclContextToReenter; 1172 DeclContext *DD = FD->getLexicalParent(); 1173 while (DD && DD->isRecord()) { 1174 DeclContextToReenter.push_back(DD); 1175 DD = DD->getLexicalParent(); 1176 } 1177 1178 // Reenter template scopes from outmost to innermost. 1179 SmallVector<DeclContext*, 4>::reverse_iterator II = 1180 DeclContextToReenter.rbegin(); 1181 for (; II != DeclContextToReenter.rend(); ++II) { 1182 if (ClassTemplatePartialSpecializationDecl* MD = 1183 dyn_cast_or_null<ClassTemplatePartialSpecializationDecl>(*II)) { 1184 TemplateParamScopeStack.push_back(new ParseScope(this, 1185 Scope::TemplateParamScope)); 1186 Actions.ActOnReenterTemplateScope(getCurScope(), MD); 1187 } else if (CXXRecordDecl* MD = dyn_cast_or_null<CXXRecordDecl>(*II)) { 1188 TemplateParamScopeStack.push_back(new ParseScope(this, 1189 Scope::TemplateParamScope, 1190 MD->getDescribedClassTemplate() != 0 )); 1191 Actions.ActOnReenterTemplateScope(getCurScope(), 1192 MD->getDescribedClassTemplate()); 1193 } 1194 } 1195 } 1196 assert(!LMT.Toks.empty() && "Empty body!"); 1197 1198 // Append the current token at the end of the new token stream so that it 1199 // doesn't get lost. 1200 LMT.Toks.push_back(Tok); 1201 PP.EnterTokenStream(LMT.Toks.data(), LMT.Toks.size(), true, false); 1202 1203 // Consume the previously pushed token. 1204 ConsumeAnyToken(); 1205 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) 1206 && "Inline method not starting with '{', ':' or 'try'"); 1207 1208 // Parse the method body. Function body parsing code is similar enough 1209 // to be re-used for method bodies as well. 1210 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); 1211 1212 // Recreate the DeclContext. 1213 Sema::ContextRAII SavedContext(Actions, Actions.getContainingDC(FD)); 1214 1215 if (FunctionTemplateDecl *FunctionTemplate 1216 = dyn_cast_or_null<FunctionTemplateDecl>(LMT.D)) 1217 Actions.ActOnStartOfFunctionDef(getCurScope(), 1218 FunctionTemplate->getTemplatedDecl()); 1219 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(LMT.D)) 1220 Actions.ActOnStartOfFunctionDef(getCurScope(), Function); 1221 1222 1223 if (Tok.is(tok::kw_try)) { 1224 ParseFunctionTryBlock(LMT.D, FnScope); 1225 } else { 1226 if (Tok.is(tok::colon)) 1227 ParseConstructorInitializer(LMT.D); 1228 else 1229 Actions.ActOnDefaultCtorInitializers(LMT.D); 1230 1231 if (Tok.is(tok::l_brace)) { 1232 ParseFunctionStatementBody(LMT.D, FnScope); 1233 Actions.MarkAsLateParsedTemplate(FD, false); 1234 } else 1235 Actions.ActOnFinishFunctionBody(LMT.D, 0); 1236 } 1237 1238 // Exit scopes. 1239 FnScope.Exit(); 1240 SmallVector<ParseScope*, 4>::reverse_iterator I = 1241 TemplateParamScopeStack.rbegin(); 1242 for (; I != TemplateParamScopeStack.rend(); ++I) 1243 delete *I; 1244 1245 DeclGroupPtrTy grp = Actions.ConvertDeclToDeclGroup(LMT.D); 1246 if (grp) 1247 Actions.getASTConsumer().HandleTopLevelDecl(grp.get()); 1248} 1249 1250/// \brief Lex a delayed template function for late parsing. 1251void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) { 1252 tok::TokenKind kind = Tok.getKind(); 1253 // We may have a constructor initializer or function-try-block here. 1254 if (kind == tok::colon || kind == tok::kw_try) 1255 ConsumeAndStoreTryAndInitializers(Toks); 1256 else { 1257 Toks.push_back(Tok); 1258 ConsumeBrace(); 1259 } 1260 // Consume everything up to (and including) the matching right brace. 1261 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1262 1263 // If we're in a function-try-block, we need to store all the catch blocks. 1264 if (kind == tok::kw_try) { 1265 while (Tok.is(tok::kw_catch)) { 1266 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); 1267 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1268 } 1269 } 1270} 1271