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