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