ParseTemplate.cpp revision 200583
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/Parse/DeclSpec.h" 17#include "clang/Parse/Scope.h" 18#include "clang/Parse/Template.h" 19#include "RAIIObjectsForParser.h" 20using namespace clang; 21 22/// \brief Parse a template declaration, explicit instantiation, or 23/// explicit specialization. 24Parser::DeclPtrTy 25Parser::ParseDeclarationStartingWithTemplate(unsigned Context, 26 SourceLocation &DeclEnd, 27 AccessSpecifier AS) { 28 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) 29 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(), 30 DeclEnd); 31 32 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS); 33} 34 35/// \brief RAII class that manages the template parameter depth. 36namespace { 37 class TemplateParameterDepthCounter { 38 unsigned &Depth; 39 unsigned AddedLevels; 40 41 public: 42 explicit TemplateParameterDepthCounter(unsigned &Depth) 43 : Depth(Depth), AddedLevels(0) { } 44 45 ~TemplateParameterDepthCounter() { 46 Depth -= AddedLevels; 47 } 48 49 void operator++() { 50 ++Depth; 51 ++AddedLevels; 52 } 53 54 operator unsigned() const { return Depth; } 55 }; 56} 57 58/// \brief Parse a template declaration or an explicit specialization. 59/// 60/// Template declarations include one or more template parameter lists 61/// and either the function or class template declaration. Explicit 62/// specializations contain one or more 'template < >' prefixes 63/// followed by a (possibly templated) declaration. Since the 64/// syntactic form of both features is nearly identical, we parse all 65/// of the template headers together and let semantic analysis sort 66/// the declarations from the explicit specializations. 67/// 68/// template-declaration: [C++ temp] 69/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration 70/// 71/// explicit-specialization: [ C++ temp.expl.spec] 72/// 'template' '<' '>' declaration 73Parser::DeclPtrTy 74Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context, 75 SourceLocation &DeclEnd, 76 AccessSpecifier AS) { 77 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && 78 "Token does not start a template declaration."); 79 80 // Enter template-parameter scope. 81 ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 82 83 // Parse multiple levels of template headers within this template 84 // parameter scope, e.g., 85 // 86 // template<typename T> 87 // template<typename U> 88 // class A<T>::B { ... }; 89 // 90 // We parse multiple levels non-recursively so that we can build a 91 // single data structure containing all of the template parameter 92 // lists to easily differentiate between the case above and: 93 // 94 // template<typename T> 95 // class A { 96 // template<typename U> class B; 97 // }; 98 // 99 // In the first case, the action for declaring A<T>::B receives 100 // both template parameter lists. In the second case, the action for 101 // defining A<T>::B receives just the inner template parameter list 102 // (and retrieves the outer template parameter list from its 103 // context). 104 bool isSpecialization = true; 105 bool LastParamListWasEmpty = false; 106 TemplateParameterLists ParamLists; 107 TemplateParameterDepthCounter Depth(TemplateParameterDepth); 108 do { 109 // Consume the 'export', if any. 110 SourceLocation ExportLoc; 111 if (Tok.is(tok::kw_export)) { 112 ExportLoc = ConsumeToken(); 113 } 114 115 // Consume the 'template', which should be here. 116 SourceLocation TemplateLoc; 117 if (Tok.is(tok::kw_template)) { 118 TemplateLoc = ConsumeToken(); 119 } else { 120 Diag(Tok.getLocation(), diag::err_expected_template); 121 return DeclPtrTy(); 122 } 123 124 // Parse the '<' template-parameter-list '>' 125 SourceLocation LAngleLoc, RAngleLoc; 126 TemplateParameterList TemplateParams; 127 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc, 128 RAngleLoc)) { 129 // Skip until the semi-colon or a }. 130 SkipUntil(tok::r_brace, true, true); 131 if (Tok.is(tok::semi)) 132 ConsumeToken(); 133 return DeclPtrTy(); 134 } 135 136 ParamLists.push_back( 137 Actions.ActOnTemplateParameterList(Depth, ExportLoc, 138 TemplateLoc, LAngleLoc, 139 TemplateParams.data(), 140 TemplateParams.size(), RAngleLoc)); 141 142 if (!TemplateParams.empty()) { 143 isSpecialization = false; 144 ++Depth; 145 } else { 146 LastParamListWasEmpty = true; 147 } 148 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); 149 150 // Parse the actual template declaration. 151 return ParseSingleDeclarationAfterTemplate(Context, 152 ParsedTemplateInfo(&ParamLists, 153 isSpecialization, 154 LastParamListWasEmpty), 155 DeclEnd, AS); 156} 157 158/// \brief Parse a single declaration that declares a template, 159/// template specialization, or explicit instantiation of a template. 160/// 161/// \param TemplateParams if non-NULL, the template parameter lists 162/// that preceded this declaration. In this case, the declaration is a 163/// template declaration, out-of-line definition of a template, or an 164/// explicit template specialization. When NULL, the declaration is an 165/// explicit template instantiation. 166/// 167/// \param TemplateLoc when TemplateParams is NULL, the location of 168/// the 'template' keyword that indicates that we have an explicit 169/// template instantiation. 170/// 171/// \param DeclEnd will receive the source location of the last token 172/// within this declaration. 173/// 174/// \param AS the access specifier associated with this 175/// declaration. Will be AS_none for namespace-scope declarations. 176/// 177/// \returns the new declaration. 178Parser::DeclPtrTy 179Parser::ParseSingleDeclarationAfterTemplate( 180 unsigned Context, 181 const ParsedTemplateInfo &TemplateInfo, 182 SourceLocation &DeclEnd, 183 AccessSpecifier AS) { 184 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 185 "Template information required"); 186 187 if (Context == Declarator::MemberContext) { 188 // We are parsing a member template. 189 ParseCXXClassMemberDeclaration(AS, TemplateInfo); 190 return DeclPtrTy::make((void*)0); 191 } 192 193 // Parse the declaration specifiers. 194 ParsingDeclSpec DS(*this); 195 196 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 197 DS.AddAttributes(ParseCXX0XAttributes().AttrList); 198 199 ParseDeclarationSpecifiers(DS, TemplateInfo, AS); 200 201 if (Tok.is(tok::semi)) { 202 DeclEnd = ConsumeToken(); 203 DeclPtrTy Decl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS); 204 DS.complete(Decl); 205 return Decl; 206 } 207 208 // Parse the declarator. 209 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context); 210 ParseDeclarator(DeclaratorInfo); 211 // Error parsing the declarator? 212 if (!DeclaratorInfo.hasName()) { 213 // If so, skip until the semi-colon or a }. 214 SkipUntil(tok::r_brace, true, true); 215 if (Tok.is(tok::semi)) 216 ConsumeToken(); 217 return DeclPtrTy(); 218 } 219 220 // If we have a declaration or declarator list, handle it. 221 if (isDeclarationAfterDeclarator()) { 222 // Parse this declaration. 223 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, 224 TemplateInfo); 225 226 if (Tok.is(tok::comma)) { 227 Diag(Tok, diag::err_multiple_template_declarators) 228 << (int)TemplateInfo.Kind; 229 SkipUntil(tok::semi, true, false); 230 return ThisDecl; 231 } 232 233 // Eat the semi colon after the declaration. 234 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration); 235 DS.complete(ThisDecl); 236 return ThisDecl; 237 } 238 239 if (DeclaratorInfo.isFunctionDeclarator() && 240 isStartOfFunctionDefinition()) { 241 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 242 Diag(Tok, diag::err_function_declared_typedef); 243 244 if (Tok.is(tok::l_brace)) { 245 // This recovery skips the entire function body. It would be nice 246 // to simply call ParseFunctionDefinition() below, however Sema 247 // assumes the declarator represents a function, not a typedef. 248 ConsumeBrace(); 249 SkipUntil(tok::r_brace, true); 250 } else { 251 SkipUntil(tok::semi); 252 } 253 return DeclPtrTy(); 254 } 255 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo); 256 } 257 258 if (DeclaratorInfo.isFunctionDeclarator()) 259 Diag(Tok, diag::err_expected_fn_body); 260 else 261 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator); 262 SkipUntil(tok::semi); 263 return DeclPtrTy(); 264} 265 266/// ParseTemplateParameters - Parses a template-parameter-list enclosed in 267/// angle brackets. Depth is the depth of this template-parameter-list, which 268/// is the number of template headers directly enclosing this template header. 269/// TemplateParams is the current list of template parameters we're building. 270/// The template parameter we parse will be added to this list. LAngleLoc and 271/// RAngleLoc will receive the positions of the '<' and '>', respectively, 272/// that enclose this template parameter list. 273/// 274/// \returns true if an error occurred, false otherwise. 275bool Parser::ParseTemplateParameters(unsigned Depth, 276 TemplateParameterList &TemplateParams, 277 SourceLocation &LAngleLoc, 278 SourceLocation &RAngleLoc) { 279 // Get the template parameter list. 280 if (!Tok.is(tok::less)) { 281 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; 282 return true; 283 } 284 LAngleLoc = ConsumeToken(); 285 286 // Try to parse the template parameter list. 287 if (Tok.is(tok::greater)) 288 RAngleLoc = ConsumeToken(); 289 else if (ParseTemplateParameterList(Depth, TemplateParams)) { 290 if (!Tok.is(tok::greater)) { 291 Diag(Tok.getLocation(), diag::err_expected_greater); 292 return true; 293 } 294 RAngleLoc = ConsumeToken(); 295 } 296 return false; 297} 298 299/// ParseTemplateParameterList - Parse a template parameter list. If 300/// the parsing fails badly (i.e., closing bracket was left out), this 301/// will try to put the token stream in a reasonable position (closing 302/// a statement, etc.) and return false. 303/// 304/// template-parameter-list: [C++ temp] 305/// template-parameter 306/// template-parameter-list ',' template-parameter 307bool 308Parser::ParseTemplateParameterList(unsigned Depth, 309 TemplateParameterList &TemplateParams) { 310 while (1) { 311 if (DeclPtrTy TmpParam 312 = ParseTemplateParameter(Depth, TemplateParams.size())) { 313 TemplateParams.push_back(TmpParam); 314 } else { 315 // If we failed to parse a template parameter, skip until we find 316 // a comma or closing brace. 317 SkipUntil(tok::comma, tok::greater, true, true); 318 } 319 320 // Did we find a comma or the end of the template parmeter list? 321 if (Tok.is(tok::comma)) { 322 ConsumeToken(); 323 } else if (Tok.is(tok::greater)) { 324 // Don't consume this... that's done by template parser. 325 break; 326 } else { 327 // Somebody probably forgot to close the template. Skip ahead and 328 // try to get out of the expression. This error is currently 329 // subsumed by whatever goes on in ParseTemplateParameter. 330 // TODO: This could match >>, and it would be nice to avoid those 331 // silly errors with template <vec<T>>. 332 // Diag(Tok.getLocation(), diag::err_expected_comma_greater); 333 SkipUntil(tok::greater, true, true); 334 return false; 335 } 336 } 337 return true; 338} 339 340/// \brief Determine whether the parser is at the start of a template 341/// type parameter. 342bool Parser::isStartOfTemplateTypeParameter() { 343 if (Tok.is(tok::kw_class)) 344 return true; 345 346 if (Tok.isNot(tok::kw_typename)) 347 return false; 348 349 // C++ [temp.param]p2: 350 // There is no semantic difference between class and typename in a 351 // template-parameter. typename followed by an unqualified-id 352 // names a template type parameter. typename followed by a 353 // qualified-id denotes the type in a non-type 354 // parameter-declaration. 355 Token Next = NextToken(); 356 357 // If we have an identifier, skip over it. 358 if (Next.getKind() == tok::identifier) 359 Next = GetLookAheadToken(2); 360 361 switch (Next.getKind()) { 362 case tok::equal: 363 case tok::comma: 364 case tok::greater: 365 case tok::greatergreater: 366 case tok::ellipsis: 367 return true; 368 369 default: 370 return false; 371 } 372} 373 374/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). 375/// 376/// template-parameter: [C++ temp.param] 377/// type-parameter 378/// parameter-declaration 379/// 380/// type-parameter: (see below) 381/// 'class' ...[opt][C++0x] identifier[opt] 382/// 'class' identifier[opt] '=' type-id 383/// 'typename' ...[opt][C++0x] identifier[opt] 384/// 'typename' identifier[opt] '=' type-id 385/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt] 386/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression 387Parser::DeclPtrTy 388Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { 389 if (isStartOfTemplateTypeParameter()) 390 return ParseTypeParameter(Depth, Position); 391 392 if (Tok.is(tok::kw_template)) 393 return ParseTemplateTemplateParameter(Depth, Position); 394 395 // If it's none of the above, then it must be a parameter declaration. 396 // NOTE: This will pick up errors in the closure of the template parameter 397 // list (e.g., template < ; Check here to implement >> style closures. 398 return ParseNonTypeTemplateParameter(Depth, Position); 399} 400 401/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). 402/// Other kinds of template parameters are parsed in 403/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. 404/// 405/// type-parameter: [C++ temp.param] 406/// 'class' ...[opt][C++0x] identifier[opt] 407/// 'class' identifier[opt] '=' type-id 408/// 'typename' ...[opt][C++0x] identifier[opt] 409/// 'typename' identifier[opt] '=' type-id 410Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){ 411 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && 412 "A type-parameter starts with 'class' or 'typename'"); 413 414 // Consume the 'class' or 'typename' keyword. 415 bool TypenameKeyword = Tok.is(tok::kw_typename); 416 SourceLocation KeyLoc = ConsumeToken(); 417 418 // Grab the ellipsis (if given). 419 bool Ellipsis = false; 420 SourceLocation EllipsisLoc; 421 if (Tok.is(tok::ellipsis)) { 422 Ellipsis = true; 423 EllipsisLoc = ConsumeToken(); 424 425 if (!getLang().CPlusPlus0x) 426 Diag(EllipsisLoc, diag::err_variadic_templates); 427 } 428 429 // Grab the template parameter name (if given) 430 SourceLocation NameLoc; 431 IdentifierInfo* ParamName = 0; 432 if (Tok.is(tok::identifier)) { 433 ParamName = Tok.getIdentifierInfo(); 434 NameLoc = ConsumeToken(); 435 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || 436 Tok.is(tok::greater)) { 437 // Unnamed template parameter. Don't have to do anything here, just 438 // don't consume this token. 439 } else { 440 Diag(Tok.getLocation(), diag::err_expected_ident); 441 return DeclPtrTy(); 442 } 443 444 DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword, 445 Ellipsis, EllipsisLoc, 446 KeyLoc, ParamName, NameLoc, 447 Depth, Position); 448 449 // Grab a default type id (if given). 450 if (Tok.is(tok::equal)) { 451 SourceLocation EqualLoc = ConsumeToken(); 452 SourceLocation DefaultLoc = Tok.getLocation(); 453 TypeResult DefaultType = ParseTypeName(); 454 if (!DefaultType.isInvalid()) 455 Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc, 456 DefaultType.get()); 457 } 458 459 return TypeParam; 460} 461 462/// ParseTemplateTemplateParameter - Handle the parsing of template 463/// template parameters. 464/// 465/// type-parameter: [C++ temp.param] 466/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] 467/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression 468Parser::DeclPtrTy 469Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { 470 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); 471 472 // Handle the template <...> part. 473 SourceLocation TemplateLoc = ConsumeToken(); 474 TemplateParameterList TemplateParams; 475 SourceLocation LAngleLoc, RAngleLoc; 476 { 477 ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 478 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, 479 RAngleLoc)) { 480 return DeclPtrTy(); 481 } 482 } 483 484 // Generate a meaningful error if the user forgot to put class before the 485 // identifier, comma, or greater. 486 if (!Tok.is(tok::kw_class)) { 487 Diag(Tok.getLocation(), diag::err_expected_class_before) 488 << PP.getSpelling(Tok); 489 return DeclPtrTy(); 490 } 491 SourceLocation ClassLoc = ConsumeToken(); 492 493 // Get the identifier, if given. 494 SourceLocation NameLoc; 495 IdentifierInfo* ParamName = 0; 496 if (Tok.is(tok::identifier)) { 497 ParamName = Tok.getIdentifierInfo(); 498 NameLoc = ConsumeToken(); 499 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) { 500 // Unnamed template parameter. Don't have to do anything here, just 501 // don't consume this token. 502 } else { 503 Diag(Tok.getLocation(), diag::err_expected_ident); 504 return DeclPtrTy(); 505 } 506 507 TemplateParamsTy *ParamList = 508 Actions.ActOnTemplateParameterList(Depth, SourceLocation(), 509 TemplateLoc, LAngleLoc, 510 &TemplateParams[0], 511 TemplateParams.size(), 512 RAngleLoc); 513 514 Parser::DeclPtrTy Param 515 = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc, 516 ParamList, ParamName, 517 NameLoc, Depth, Position); 518 519 // Get the a default value, if given. 520 if (Tok.is(tok::equal)) { 521 SourceLocation EqualLoc = ConsumeToken(); 522 ParsedTemplateArgument Default = ParseTemplateTemplateArgument(); 523 if (Default.isInvalid()) { 524 Diag(Tok.getLocation(), 525 diag::err_default_template_template_parameter_not_template); 526 static const tok::TokenKind EndToks[] = { 527 tok::comma, tok::greater, tok::greatergreater 528 }; 529 SkipUntil(EndToks, 3, true, true); 530 return Param; 531 } else if (Param) 532 Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc, Default); 533 } 534 535 return Param; 536} 537 538/// ParseNonTypeTemplateParameter - Handle the parsing of non-type 539/// template parameters (e.g., in "template<int Size> class array;"). 540/// 541/// template-parameter: 542/// ... 543/// parameter-declaration 544/// 545/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(), 546/// but that didn't work out to well. Instead, this tries to recrate the basic 547/// parsing of parameter declarations, but tries to constrain it for template 548/// parameters. 549/// FIXME: We need to make a ParseParameterDeclaration that works for 550/// non-type template parameters and normal function parameters. 551Parser::DeclPtrTy 552Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { 553 SourceLocation StartLoc = Tok.getLocation(); 554 555 // Parse the declaration-specifiers (i.e., the type). 556 // FIXME: The type should probably be restricted in some way... Not all 557 // declarators (parts of declarators?) are accepted for parameters. 558 DeclSpec DS; 559 ParseDeclarationSpecifiers(DS); 560 561 // Parse this as a typename. 562 Declarator ParamDecl(DS, Declarator::TemplateParamContext); 563 ParseDeclarator(ParamDecl); 564 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) { 565 // This probably shouldn't happen - and it's more of a Sema thing, but 566 // basically we didn't parse the type name because we couldn't associate 567 // it with an AST node. we should just skip to the comma or greater. 568 // TODO: This is currently a placeholder for some kind of Sema Error. 569 Diag(Tok.getLocation(), diag::err_parse_error); 570 SkipUntil(tok::comma, tok::greater, true, true); 571 return DeclPtrTy(); 572 } 573 574 // Create the parameter. 575 DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl, 576 Depth, Position); 577 578 // If there is a default value, parse it. 579 if (Tok.is(tok::equal)) { 580 SourceLocation EqualLoc = ConsumeToken(); 581 582 // C++ [temp.param]p15: 583 // When parsing a default template-argument for a non-type 584 // template-parameter, the first non-nested > is taken as the 585 // end of the template-parameter-list rather than a greater-than 586 // operator. 587 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 588 589 OwningExprResult DefaultArg = ParseAssignmentExpression(); 590 if (DefaultArg.isInvalid()) 591 SkipUntil(tok::comma, tok::greater, true, true); 592 else if (Param) 593 Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc, 594 move(DefaultArg)); 595 } 596 597 return Param; 598} 599 600/// \brief Parses a template-id that after the template name has 601/// already been parsed. 602/// 603/// This routine takes care of parsing the enclosed template argument 604/// list ('<' template-parameter-list [opt] '>') and placing the 605/// results into a form that can be transferred to semantic analysis. 606/// 607/// \param Template the template declaration produced by isTemplateName 608/// 609/// \param TemplateNameLoc the source location of the template name 610/// 611/// \param SS if non-NULL, the nested-name-specifier preceding the 612/// template name. 613/// 614/// \param ConsumeLastToken if true, then we will consume the last 615/// token that forms the template-id. Otherwise, we will leave the 616/// last token in the stream (e.g., so that it can be replaced with an 617/// annotation token). 618bool 619Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template, 620 SourceLocation TemplateNameLoc, 621 const CXXScopeSpec *SS, 622 bool ConsumeLastToken, 623 SourceLocation &LAngleLoc, 624 TemplateArgList &TemplateArgs, 625 SourceLocation &RAngleLoc) { 626 assert(Tok.is(tok::less) && "Must have already parsed the template-name"); 627 628 // Consume the '<'. 629 LAngleLoc = ConsumeToken(); 630 631 // Parse the optional template-argument-list. 632 bool Invalid = false; 633 { 634 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 635 if (Tok.isNot(tok::greater)) 636 Invalid = ParseTemplateArgumentList(TemplateArgs); 637 638 if (Invalid) { 639 // Try to find the closing '>'. 640 SkipUntil(tok::greater, true, !ConsumeLastToken); 641 642 return true; 643 } 644 } 645 646 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) 647 return true; 648 649 // Determine the location of the '>' or '>>'. Only consume this 650 // token if the caller asked us to. 651 RAngleLoc = Tok.getLocation(); 652 653 if (Tok.is(tok::greatergreater)) { 654 if (!getLang().CPlusPlus0x) { 655 const char *ReplaceStr = "> >"; 656 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater)) 657 ReplaceStr = "> > "; 658 659 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space) 660 << CodeModificationHint::CreateReplacement( 661 SourceRange(Tok.getLocation()), ReplaceStr); 662 } 663 664 Tok.setKind(tok::greater); 665 if (!ConsumeLastToken) { 666 // Since we're not supposed to consume the '>>' token, we need 667 // to insert a second '>' token after the first. 668 PP.EnterToken(Tok); 669 } 670 } else if (ConsumeLastToken) 671 ConsumeToken(); 672 673 return false; 674} 675 676/// \brief Replace the tokens that form a simple-template-id with an 677/// annotation token containing the complete template-id. 678/// 679/// The first token in the stream must be the name of a template that 680/// is followed by a '<'. This routine will parse the complete 681/// simple-template-id and replace the tokens with a single annotation 682/// token with one of two different kinds: if the template-id names a 683/// type (and \p AllowTypeAnnotation is true), the annotation token is 684/// a type annotation that includes the optional nested-name-specifier 685/// (\p SS). Otherwise, the annotation token is a template-id 686/// annotation that does not include the optional 687/// nested-name-specifier. 688/// 689/// \param Template the declaration of the template named by the first 690/// token (an identifier), as returned from \c Action::isTemplateName(). 691/// 692/// \param TemplateNameKind the kind of template that \p Template 693/// refers to, as returned from \c Action::isTemplateName(). 694/// 695/// \param SS if non-NULL, the nested-name-specifier that precedes 696/// this template name. 697/// 698/// \param TemplateKWLoc if valid, specifies that this template-id 699/// annotation was preceded by the 'template' keyword and gives the 700/// location of that keyword. If invalid (the default), then this 701/// template-id was not preceded by a 'template' keyword. 702/// 703/// \param AllowTypeAnnotation if true (the default), then a 704/// simple-template-id that refers to a class template, template 705/// template parameter, or other template that produces a type will be 706/// replaced with a type annotation token. Otherwise, the 707/// simple-template-id is always replaced with a template-id 708/// annotation token. 709/// 710/// If an unrecoverable parse error occurs and no annotation token can be 711/// formed, this function returns true. 712/// 713bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, 714 const CXXScopeSpec *SS, 715 UnqualifiedId &TemplateName, 716 SourceLocation TemplateKWLoc, 717 bool AllowTypeAnnotation) { 718 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++"); 719 assert(Template && Tok.is(tok::less) && 720 "Parser isn't at the beginning of a template-id"); 721 722 // Consume the template-name. 723 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); 724 725 // Parse the enclosed template argument list. 726 SourceLocation LAngleLoc, RAngleLoc; 727 TemplateArgList TemplateArgs; 728 bool Invalid = ParseTemplateIdAfterTemplateName(Template, 729 TemplateNameLoc, 730 SS, false, LAngleLoc, 731 TemplateArgs, 732 RAngleLoc); 733 734 if (Invalid) { 735 // If we failed to parse the template ID but skipped ahead to a >, we're not 736 // going to be able to form a token annotation. Eat the '>' if present. 737 if (Tok.is(tok::greater)) 738 ConsumeToken(); 739 return true; 740 } 741 742 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), 743 TemplateArgs.size()); 744 745 // Build the annotation token. 746 if (TNK == TNK_Type_template && AllowTypeAnnotation) { 747 Action::TypeResult Type 748 = Actions.ActOnTemplateIdType(Template, TemplateNameLoc, 749 LAngleLoc, TemplateArgsPtr, 750 RAngleLoc); 751 if (Type.isInvalid()) { 752 // If we failed to parse the template ID but skipped ahead to a >, we're not 753 // going to be able to form a token annotation. Eat the '>' if present. 754 if (Tok.is(tok::greater)) 755 ConsumeToken(); 756 return true; 757 } 758 759 Tok.setKind(tok::annot_typename); 760 Tok.setAnnotationValue(Type.get()); 761 if (SS && SS->isNotEmpty()) 762 Tok.setLocation(SS->getBeginLoc()); 763 else if (TemplateKWLoc.isValid()) 764 Tok.setLocation(TemplateKWLoc); 765 else 766 Tok.setLocation(TemplateNameLoc); 767 } else { 768 // Build a template-id annotation token that can be processed 769 // later. 770 Tok.setKind(tok::annot_template_id); 771 TemplateIdAnnotation *TemplateId 772 = TemplateIdAnnotation::Allocate(TemplateArgs.size()); 773 TemplateId->TemplateNameLoc = TemplateNameLoc; 774 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) { 775 TemplateId->Name = TemplateName.Identifier; 776 TemplateId->Operator = OO_None; 777 } else { 778 TemplateId->Name = 0; 779 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator; 780 } 781 TemplateId->Template = Template.getAs<void*>(); 782 TemplateId->Kind = TNK; 783 TemplateId->LAngleLoc = LAngleLoc; 784 TemplateId->RAngleLoc = RAngleLoc; 785 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); 786 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) 787 Args[Arg] = TemplateArgs[Arg]; 788 Tok.setAnnotationValue(TemplateId); 789 if (TemplateKWLoc.isValid()) 790 Tok.setLocation(TemplateKWLoc); 791 else 792 Tok.setLocation(TemplateNameLoc); 793 794 TemplateArgsPtr.release(); 795 } 796 797 // Common fields for the annotation token 798 Tok.setAnnotationEndLoc(RAngleLoc); 799 800 // In case the tokens were cached, have Preprocessor replace them with the 801 // annotation token. 802 PP.AnnotateCachedTokens(Tok); 803 return false; 804} 805 806/// \brief Replaces a template-id annotation token with a type 807/// annotation token. 808/// 809/// If there was a failure when forming the type from the template-id, 810/// a type annotation token will still be created, but will have a 811/// NULL type pointer to signify an error. 812void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) { 813 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); 814 815 TemplateIdAnnotation *TemplateId 816 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 817 assert((TemplateId->Kind == TNK_Type_template || 818 TemplateId->Kind == TNK_Dependent_template_name) && 819 "Only works for type and dependent templates"); 820 821 ASTTemplateArgsPtr TemplateArgsPtr(Actions, 822 TemplateId->getTemplateArgs(), 823 TemplateId->NumArgs); 824 825 Action::TypeResult Type 826 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template), 827 TemplateId->TemplateNameLoc, 828 TemplateId->LAngleLoc, 829 TemplateArgsPtr, 830 TemplateId->RAngleLoc); 831 // Create the new "type" annotation token. 832 Tok.setKind(tok::annot_typename); 833 Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get()); 834 if (SS && SS->isNotEmpty()) // it was a C++ qualified type name. 835 Tok.setLocation(SS->getBeginLoc()); 836 Tok.setAnnotationEndLoc(TemplateId->TemplateNameLoc); 837 838 // Replace the template-id annotation token, and possible the scope-specifier 839 // that precedes it, with the typename annotation token. 840 PP.AnnotateCachedTokens(Tok); 841 TemplateId->Destroy(); 842} 843 844/// \brief Determine whether the given token can end a template argument. 845static bool isEndOfTemplateArgument(Token Tok) { 846 return Tok.is(tok::comma) || Tok.is(tok::greater) || 847 Tok.is(tok::greatergreater); 848} 849 850/// \brief Parse a C++ template template argument. 851ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { 852 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && 853 !Tok.is(tok::annot_cxxscope)) 854 return ParsedTemplateArgument(); 855 856 // C++0x [temp.arg.template]p1: 857 // A template-argument for a template template-parameter shall be the name 858 // of a class template or a template alias, expressed as id-expression. 859 // 860 // We parse an id-expression that refers to a class template or template 861 // alias. The grammar we parse is: 862 // 863 // nested-name-specifier[opt] template[opt] identifier 864 // 865 // followed by a token that terminates a template argument, such as ',', 866 // '>', or (in some cases) '>>'. 867 CXXScopeSpec SS; // nested-name-specifier, if present 868 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, 869 /*EnteringContext=*/false); 870 871 if (SS.isSet() && Tok.is(tok::kw_template)) { 872 // Parse the optional 'template' keyword following the 873 // nested-name-specifier. 874 SourceLocation TemplateLoc = ConsumeToken(); 875 876 if (Tok.is(tok::identifier)) { 877 // We appear to have a dependent template name. 878 UnqualifiedId Name; 879 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 880 ConsumeToken(); // the identifier 881 882 // If the next token signals the end of a template argument, 883 // then we have a dependent template name that could be a template 884 // template argument. 885 if (isEndOfTemplateArgument(Tok)) { 886 TemplateTy Template 887 = Actions.ActOnDependentTemplateName(TemplateLoc, SS, Name, 888 /*ObjectType=*/0, 889 /*EnteringContext=*/false); 890 if (Template.get()) 891 return ParsedTemplateArgument(SS, Template, Name.StartLocation); 892 } 893 } 894 } else if (Tok.is(tok::identifier)) { 895 // We may have a (non-dependent) template name. 896 TemplateTy Template; 897 UnqualifiedId Name; 898 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 899 ConsumeToken(); // the identifier 900 901 if (isEndOfTemplateArgument(Tok)) { 902 TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS, Name, 903 /*ObjectType=*/0, 904 /*EnteringContext=*/false, 905 Template); 906 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { 907 // We have an id-expression that refers to a class template or 908 // (C++0x) template alias. 909 return ParsedTemplateArgument(SS, Template, Name.StartLocation); 910 } 911 } 912 } 913 914 // We don't have a template template argument. 915 return ParsedTemplateArgument(); 916} 917 918/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). 919/// 920/// template-argument: [C++ 14.2] 921/// constant-expression 922/// type-id 923/// id-expression 924ParsedTemplateArgument Parser::ParseTemplateArgument() { 925 // C++ [temp.arg]p2: 926 // In a template-argument, an ambiguity between a type-id and an 927 // expression is resolved to a type-id, regardless of the form of 928 // the corresponding template-parameter. 929 // 930 // Therefore, we initially try to parse a type-id. 931 if (isCXXTypeId(TypeIdAsTemplateArgument)) { 932 SourceLocation Loc = Tok.getLocation(); 933 TypeResult TypeArg = ParseTypeName(); 934 if (TypeArg.isInvalid()) 935 return ParsedTemplateArgument(); 936 937 return ParsedTemplateArgument(ParsedTemplateArgument::Type, TypeArg.get(), 938 Loc); 939 } 940 941 // Try to parse a template template argument. 942 { 943 TentativeParsingAction TPA(*this); 944 945 ParsedTemplateArgument TemplateTemplateArgument 946 = ParseTemplateTemplateArgument(); 947 if (!TemplateTemplateArgument.isInvalid()) { 948 TPA.Commit(); 949 return TemplateTemplateArgument; 950 } 951 952 // Revert this tentative parse to parse a non-type template argument. 953 TPA.Revert(); 954 } 955 956 // Parse a non-type template argument. 957 SourceLocation Loc = Tok.getLocation(); 958 OwningExprResult ExprArg = ParseConstantExpression(); 959 if (ExprArg.isInvalid() || !ExprArg.get()) 960 return ParsedTemplateArgument(); 961 962 return ParsedTemplateArgument(ParsedTemplateArgument::NonType, 963 ExprArg.release(), Loc); 964} 965 966/// ParseTemplateArgumentList - Parse a C++ template-argument-list 967/// (C++ [temp.names]). Returns true if there was an error. 968/// 969/// template-argument-list: [C++ 14.2] 970/// template-argument 971/// template-argument-list ',' template-argument 972bool 973Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) { 974 while (true) { 975 ParsedTemplateArgument Arg = ParseTemplateArgument(); 976 if (Arg.isInvalid()) { 977 SkipUntil(tok::comma, tok::greater, true, true); 978 return true; 979 } 980 981 // Save this template argument. 982 TemplateArgs.push_back(Arg); 983 984 // If the next token is a comma, consume it and keep reading 985 // arguments. 986 if (Tok.isNot(tok::comma)) break; 987 988 // Consume the comma. 989 ConsumeToken(); 990 } 991 992 return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater); 993} 994 995/// \brief Parse a C++ explicit template instantiation 996/// (C++ [temp.explicit]). 997/// 998/// explicit-instantiation: 999/// 'extern' [opt] 'template' declaration 1000/// 1001/// Note that the 'extern' is a GNU extension and C++0x feature. 1002Parser::DeclPtrTy 1003Parser::ParseExplicitInstantiation(SourceLocation ExternLoc, 1004 SourceLocation TemplateLoc, 1005 SourceLocation &DeclEnd) { 1006 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext, 1007 ParsedTemplateInfo(ExternLoc, 1008 TemplateLoc), 1009 DeclEnd, AS_none); 1010} 1011