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