1 //===--- ParseTemplate.cpp - Template Parsing -----------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements parsing of C++ templates. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/AST/ASTContext.h" 14 #include "clang/AST/DeclTemplate.h" 15 #include "clang/AST/ExprCXX.h" 16 #include "clang/Parse/ParseDiagnostic.h" 17 #include "clang/Parse/Parser.h" 18 #include "clang/Parse/RAIIObjectsForParser.h" 19 #include "clang/Sema/DeclSpec.h" 20 #include "clang/Sema/ParsedTemplate.h" 21 #include "clang/Sema/Scope.h" 22 #include "llvm/Support/TimeProfiler.h" 23 using namespace clang; 24 25 /// Re-enter a possible template scope, creating as many template parameter 26 /// scopes as necessary. 27 /// \return The number of template parameter scopes entered. 28 unsigned Parser::ReenterTemplateScopes(MultiParseScope &S, Decl *D) { 29 return Actions.ActOnReenterTemplateScope(D, [&] { 30 S.Enter(Scope::TemplateParamScope); 31 return Actions.getCurScope(); 32 }); 33 } 34 35 /// Parse a template declaration, explicit instantiation, or 36 /// explicit specialization. 37 Decl *Parser::ParseDeclarationStartingWithTemplate( 38 DeclaratorContext Context, SourceLocation &DeclEnd, 39 ParsedAttributes &AccessAttrs, AccessSpecifier AS) { 40 ObjCDeclContextSwitch ObjCDC(*this); 41 42 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) { 43 return ParseExplicitInstantiation(Context, SourceLocation(), ConsumeToken(), 44 DeclEnd, AccessAttrs, AS); 45 } 46 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AccessAttrs, 47 AS); 48 } 49 50 /// Parse a template declaration or an explicit specialization. 51 /// 52 /// Template declarations include one or more template parameter lists 53 /// and either the function or class template declaration. Explicit 54 /// specializations contain one or more 'template < >' prefixes 55 /// followed by a (possibly templated) declaration. Since the 56 /// syntactic form of both features is nearly identical, we parse all 57 /// of the template headers together and let semantic analysis sort 58 /// the declarations from the explicit specializations. 59 /// 60 /// template-declaration: [C++ temp] 61 /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration 62 /// 63 /// template-declaration: [C++2a] 64 /// template-head declaration 65 /// template-head concept-definition 66 /// 67 /// TODO: requires-clause 68 /// template-head: [C++2a] 69 /// 'template' '<' template-parameter-list '>' 70 /// requires-clause[opt] 71 /// 72 /// explicit-specialization: [ C++ temp.expl.spec] 73 /// 'template' '<' '>' declaration 74 Decl *Parser::ParseTemplateDeclarationOrSpecialization( 75 DeclaratorContext Context, SourceLocation &DeclEnd, 76 ParsedAttributes &AccessAttrs, AccessSpecifier AS) { 77 assert(Tok.isOneOf(tok::kw_export, tok::kw_template) && 78 "Token does not start a template declaration."); 79 80 MultiParseScope TemplateParamScopes(*this); 81 82 // Tell the action that names should be checked in the context of 83 // the declaration to come. 84 ParsingDeclRAIIObject 85 ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); 86 87 // Parse multiple levels of template headers within this template 88 // parameter scope, e.g., 89 // 90 // template<typename T> 91 // template<typename U> 92 // class A<T>::B { ... }; 93 // 94 // We parse multiple levels non-recursively so that we can build a 95 // single data structure containing all of the template parameter 96 // lists to easily differentiate between the case above and: 97 // 98 // template<typename T> 99 // class A { 100 // template<typename U> class B; 101 // }; 102 // 103 // In the first case, the action for declaring A<T>::B receives 104 // both template parameter lists. In the second case, the action for 105 // defining A<T>::B receives just the inner template parameter list 106 // (and retrieves the outer template parameter list from its 107 // context). 108 bool isSpecialization = true; 109 bool LastParamListWasEmpty = false; 110 TemplateParameterLists ParamLists; 111 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 112 113 do { 114 // Consume the 'export', if any. 115 SourceLocation ExportLoc; 116 TryConsumeToken(tok::kw_export, ExportLoc); 117 118 // Consume the 'template', which should be here. 119 SourceLocation TemplateLoc; 120 if (!TryConsumeToken(tok::kw_template, TemplateLoc)) { 121 Diag(Tok.getLocation(), diag::err_expected_template); 122 return nullptr; 123 } 124 125 // Parse the '<' template-parameter-list '>' 126 SourceLocation LAngleLoc, RAngleLoc; 127 SmallVector<NamedDecl*, 4> TemplateParams; 128 if (ParseTemplateParameters(TemplateParamScopes, 129 CurTemplateDepthTracker.getDepth(), 130 TemplateParams, LAngleLoc, RAngleLoc)) { 131 // Skip until the semi-colon or a '}'. 132 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); 133 TryConsumeToken(tok::semi); 134 return nullptr; 135 } 136 137 ExprResult OptionalRequiresClauseConstraintER; 138 if (!TemplateParams.empty()) { 139 isSpecialization = false; 140 ++CurTemplateDepthTracker; 141 142 if (TryConsumeToken(tok::kw_requires)) { 143 OptionalRequiresClauseConstraintER = 144 Actions.ActOnRequiresClause(ParseConstraintLogicalOrExpression( 145 /*IsTrailingRequiresClause=*/false)); 146 if (!OptionalRequiresClauseConstraintER.isUsable()) { 147 // Skip until the semi-colon or a '}'. 148 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); 149 TryConsumeToken(tok::semi); 150 return nullptr; 151 } 152 } 153 } else { 154 LastParamListWasEmpty = true; 155 } 156 157 ParamLists.push_back(Actions.ActOnTemplateParameterList( 158 CurTemplateDepthTracker.getDepth(), ExportLoc, TemplateLoc, LAngleLoc, 159 TemplateParams, RAngleLoc, OptionalRequiresClauseConstraintER.get())); 160 } while (Tok.isOneOf(tok::kw_export, tok::kw_template)); 161 162 // Parse the actual template declaration. 163 if (Tok.is(tok::kw_concept)) 164 return ParseConceptDefinition( 165 ParsedTemplateInfo(&ParamLists, isSpecialization, 166 LastParamListWasEmpty), 167 DeclEnd); 168 169 return ParseSingleDeclarationAfterTemplate( 170 Context, 171 ParsedTemplateInfo(&ParamLists, isSpecialization, LastParamListWasEmpty), 172 ParsingTemplateParams, DeclEnd, AccessAttrs, AS); 173 } 174 175 /// Parse a single declaration that declares a template, 176 /// template specialization, or explicit instantiation of a template. 177 /// 178 /// \param DeclEnd will receive the source location of the last token 179 /// within this declaration. 180 /// 181 /// \param AS the access specifier associated with this 182 /// declaration. Will be AS_none for namespace-scope declarations. 183 /// 184 /// \returns the new declaration. 185 Decl *Parser::ParseSingleDeclarationAfterTemplate( 186 DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo, 187 ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd, 188 ParsedAttributes &AccessAttrs, AccessSpecifier AS) { 189 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 190 "Template information required"); 191 192 if (Tok.is(tok::kw_static_assert)) { 193 // A static_assert declaration may not be templated. 194 Diag(Tok.getLocation(), diag::err_templated_invalid_declaration) 195 << TemplateInfo.getSourceRange(); 196 // Parse the static_assert declaration to improve error recovery. 197 return ParseStaticAssertDeclaration(DeclEnd); 198 } 199 200 if (Context == DeclaratorContext::Member) { 201 // We are parsing a member template. 202 ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo, 203 &DiagsFromTParams); 204 return nullptr; 205 } 206 207 ParsedAttributesWithRange prefixAttrs(AttrFactory); 208 MaybeParseCXX11Attributes(prefixAttrs); 209 210 if (Tok.is(tok::kw_using)) { 211 auto usingDeclPtr = ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd, 212 prefixAttrs); 213 if (!usingDeclPtr || !usingDeclPtr.get().isSingleDecl()) 214 return nullptr; 215 return usingDeclPtr.get().getSingleDecl(); 216 } 217 218 // Parse the declaration specifiers, stealing any diagnostics from 219 // the template parameters. 220 ParsingDeclSpec DS(*this, &DiagsFromTParams); 221 222 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, 223 getDeclSpecContextFromDeclaratorContext(Context)); 224 225 if (Tok.is(tok::semi)) { 226 ProhibitAttributes(prefixAttrs); 227 DeclEnd = ConsumeToken(); 228 RecordDecl *AnonRecord = nullptr; 229 Decl *Decl = Actions.ParsedFreeStandingDeclSpec( 230 getCurScope(), AS, DS, 231 TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams 232 : MultiTemplateParamsArg(), 233 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation, 234 AnonRecord); 235 assert(!AnonRecord && 236 "Anonymous unions/structs should not be valid with template"); 237 DS.complete(Decl); 238 return Decl; 239 } 240 241 // Move the attributes from the prefix into the DS. 242 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) 243 ProhibitAttributes(prefixAttrs); 244 else 245 DS.takeAttributesFrom(prefixAttrs); 246 247 // Parse the declarator. 248 ParsingDeclarator DeclaratorInfo(*this, DS, (DeclaratorContext)Context); 249 if (TemplateInfo.TemplateParams) 250 DeclaratorInfo.setTemplateParameterLists(*TemplateInfo.TemplateParams); 251 252 // Turn off usual access checking for template specializations and 253 // instantiations. 254 // C++20 [temp.spec] 13.9/6. 255 // This disables the access checking rules for function template explicit 256 // instantiation and explicit specialization: 257 // - parameter-list; 258 // - template-argument-list; 259 // - noexcept-specifier; 260 // - dynamic-exception-specifications (deprecated in C++11, removed since 261 // C++17). 262 bool IsTemplateSpecOrInst = 263 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || 264 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); 265 SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst); 266 267 ParseDeclarator(DeclaratorInfo); 268 269 if (IsTemplateSpecOrInst) 270 SAC.done(); 271 272 // Error parsing the declarator? 273 if (!DeclaratorInfo.hasName()) { 274 // If so, skip until the semi-colon or a }. 275 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); 276 if (Tok.is(tok::semi)) 277 ConsumeToken(); 278 return nullptr; 279 } 280 281 llvm::TimeTraceScope TimeScope("ParseTemplate", [&]() { 282 return std::string(DeclaratorInfo.getIdentifier() != nullptr 283 ? DeclaratorInfo.getIdentifier()->getName() 284 : "<unknown>"); 285 }); 286 287 LateParsedAttrList LateParsedAttrs(true); 288 if (DeclaratorInfo.isFunctionDeclarator()) { 289 if (Tok.is(tok::kw_requires)) 290 ParseTrailingRequiresClause(DeclaratorInfo); 291 292 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); 293 } 294 295 if (DeclaratorInfo.isFunctionDeclarator() && 296 isStartOfFunctionDefinition(DeclaratorInfo)) { 297 298 // Function definitions are only allowed at file scope and in C++ classes. 299 // The C++ inline method definition case is handled elsewhere, so we only 300 // need to handle the file scope definition case. 301 if (Context != DeclaratorContext::File) { 302 Diag(Tok, diag::err_function_definition_not_allowed); 303 SkipMalformedDecl(); 304 return nullptr; 305 } 306 307 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 308 // Recover by ignoring the 'typedef'. This was probably supposed to be 309 // the 'typename' keyword, which we should have already suggested adding 310 // if it's appropriate. 311 Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef) 312 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); 313 DS.ClearStorageClassSpecs(); 314 } 315 316 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { 317 if (DeclaratorInfo.getName().getKind() != 318 UnqualifiedIdKind::IK_TemplateId) { 319 // If the declarator-id is not a template-id, issue a diagnostic and 320 // recover by ignoring the 'template' keyword. 321 Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0; 322 return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(), 323 &LateParsedAttrs); 324 } else { 325 SourceLocation LAngleLoc 326 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); 327 Diag(DeclaratorInfo.getIdentifierLoc(), 328 diag::err_explicit_instantiation_with_definition) 329 << SourceRange(TemplateInfo.TemplateLoc) 330 << FixItHint::CreateInsertion(LAngleLoc, "<>"); 331 332 // Recover as if it were an explicit specialization. 333 TemplateParameterLists FakedParamLists; 334 FakedParamLists.push_back(Actions.ActOnTemplateParameterList( 335 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, 336 LAngleLoc, nullptr)); 337 338 return ParseFunctionDefinition( 339 DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists, 340 /*isSpecialization=*/true, 341 /*lastParameterListWasEmpty=*/true), 342 &LateParsedAttrs); 343 } 344 } 345 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo, 346 &LateParsedAttrs); 347 } 348 349 // Parse this declaration. 350 Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, 351 TemplateInfo); 352 353 if (Tok.is(tok::comma)) { 354 Diag(Tok, diag::err_multiple_template_declarators) 355 << (int)TemplateInfo.Kind; 356 SkipUntil(tok::semi); 357 return ThisDecl; 358 } 359 360 // Eat the semi colon after the declaration. 361 ExpectAndConsumeSemi(diag::err_expected_semi_declaration); 362 if (LateParsedAttrs.size() > 0) 363 ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false); 364 DeclaratorInfo.complete(ThisDecl); 365 return ThisDecl; 366 } 367 368 /// \brief Parse a single declaration that declares a concept. 369 /// 370 /// \param DeclEnd will receive the source location of the last token 371 /// within this declaration. 372 /// 373 /// \returns the new declaration. 374 Decl * 375 Parser::ParseConceptDefinition(const ParsedTemplateInfo &TemplateInfo, 376 SourceLocation &DeclEnd) { 377 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 378 "Template information required"); 379 assert(Tok.is(tok::kw_concept) && 380 "ParseConceptDefinition must be called when at a 'concept' keyword"); 381 382 ConsumeToken(); // Consume 'concept' 383 384 SourceLocation BoolKWLoc; 385 if (TryConsumeToken(tok::kw_bool, BoolKWLoc)) 386 Diag(Tok.getLocation(), diag::ext_concept_legacy_bool_keyword) << 387 FixItHint::CreateRemoval(SourceLocation(BoolKWLoc)); 388 389 DiagnoseAndSkipCXX11Attributes(); 390 391 CXXScopeSpec SS; 392 if (ParseOptionalCXXScopeSpecifier( 393 SS, /*ObjectType=*/nullptr, 394 /*ObjectHasErrors=*/false, /*EnteringContext=*/false, 395 /*MayBePseudoDestructor=*/nullptr, 396 /*IsTypename=*/false, /*LastII=*/nullptr, /*OnlyNamespace=*/true) || 397 SS.isInvalid()) { 398 SkipUntil(tok::semi); 399 return nullptr; 400 } 401 402 if (SS.isNotEmpty()) 403 Diag(SS.getBeginLoc(), 404 diag::err_concept_definition_not_identifier); 405 406 UnqualifiedId Result; 407 if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr, 408 /*ObjectHadErrors=*/false, /*EnteringContext=*/false, 409 /*AllowDestructorName=*/false, 410 /*AllowConstructorName=*/false, 411 /*AllowDeductionGuide=*/false, 412 /*TemplateKWLoc=*/nullptr, Result)) { 413 SkipUntil(tok::semi); 414 return nullptr; 415 } 416 417 if (Result.getKind() != UnqualifiedIdKind::IK_Identifier) { 418 Diag(Result.getBeginLoc(), diag::err_concept_definition_not_identifier); 419 SkipUntil(tok::semi); 420 return nullptr; 421 } 422 423 IdentifierInfo *Id = Result.Identifier; 424 SourceLocation IdLoc = Result.getBeginLoc(); 425 426 DiagnoseAndSkipCXX11Attributes(); 427 428 if (!TryConsumeToken(tok::equal)) { 429 Diag(Tok.getLocation(), diag::err_expected) << tok::equal; 430 SkipUntil(tok::semi); 431 return nullptr; 432 } 433 434 ExprResult ConstraintExprResult = 435 Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression()); 436 if (ConstraintExprResult.isInvalid()) { 437 SkipUntil(tok::semi); 438 return nullptr; 439 } 440 441 DeclEnd = Tok.getLocation(); 442 ExpectAndConsumeSemi(diag::err_expected_semi_declaration); 443 Expr *ConstraintExpr = ConstraintExprResult.get(); 444 return Actions.ActOnConceptDefinition(getCurScope(), 445 *TemplateInfo.TemplateParams, 446 Id, IdLoc, ConstraintExpr); 447 } 448 449 /// ParseTemplateParameters - Parses a template-parameter-list enclosed in 450 /// angle brackets. Depth is the depth of this template-parameter-list, which 451 /// is the number of template headers directly enclosing this template header. 452 /// TemplateParams is the current list of template parameters we're building. 453 /// The template parameter we parse will be added to this list. LAngleLoc and 454 /// RAngleLoc will receive the positions of the '<' and '>', respectively, 455 /// that enclose this template parameter list. 456 /// 457 /// \returns true if an error occurred, false otherwise. 458 bool Parser::ParseTemplateParameters( 459 MultiParseScope &TemplateScopes, unsigned Depth, 460 SmallVectorImpl<NamedDecl *> &TemplateParams, SourceLocation &LAngleLoc, 461 SourceLocation &RAngleLoc) { 462 // Get the template parameter list. 463 if (!TryConsumeToken(tok::less, LAngleLoc)) { 464 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; 465 return true; 466 } 467 468 // Try to parse the template parameter list. 469 bool Failed = false; 470 // FIXME: Missing greatergreatergreater support. 471 if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater)) { 472 TemplateScopes.Enter(Scope::TemplateParamScope); 473 Failed = ParseTemplateParameterList(Depth, TemplateParams); 474 } 475 476 if (Tok.is(tok::greatergreater)) { 477 // No diagnostic required here: a template-parameter-list can only be 478 // followed by a declaration or, for a template template parameter, the 479 // 'class' keyword. Therefore, the second '>' will be diagnosed later. 480 // This matters for elegant diagnosis of: 481 // template<template<typename>> struct S; 482 Tok.setKind(tok::greater); 483 RAngleLoc = Tok.getLocation(); 484 Tok.setLocation(Tok.getLocation().getLocWithOffset(1)); 485 } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) { 486 Diag(Tok.getLocation(), diag::err_expected) << tok::greater; 487 return true; 488 } 489 return false; 490 } 491 492 /// ParseTemplateParameterList - Parse a template parameter list. If 493 /// the parsing fails badly (i.e., closing bracket was left out), this 494 /// will try to put the token stream in a reasonable position (closing 495 /// a statement, etc.) and return false. 496 /// 497 /// template-parameter-list: [C++ temp] 498 /// template-parameter 499 /// template-parameter-list ',' template-parameter 500 bool 501 Parser::ParseTemplateParameterList(const unsigned Depth, 502 SmallVectorImpl<NamedDecl*> &TemplateParams) { 503 while (true) { 504 505 if (NamedDecl *TmpParam 506 = ParseTemplateParameter(Depth, TemplateParams.size())) { 507 TemplateParams.push_back(TmpParam); 508 } else { 509 // If we failed to parse a template parameter, skip until we find 510 // a comma or closing brace. 511 SkipUntil(tok::comma, tok::greater, tok::greatergreater, 512 StopAtSemi | StopBeforeMatch); 513 } 514 515 // Did we find a comma or the end of the template parameter list? 516 if (Tok.is(tok::comma)) { 517 ConsumeToken(); 518 } else if (Tok.isOneOf(tok::greater, tok::greatergreater)) { 519 // Don't consume this... that's done by template parser. 520 break; 521 } else { 522 // Somebody probably forgot to close the template. Skip ahead and 523 // try to get out of the expression. This error is currently 524 // subsumed by whatever goes on in ParseTemplateParameter. 525 Diag(Tok.getLocation(), diag::err_expected_comma_greater); 526 SkipUntil(tok::comma, tok::greater, tok::greatergreater, 527 StopAtSemi | StopBeforeMatch); 528 return false; 529 } 530 } 531 return true; 532 } 533 534 /// Determine whether the parser is at the start of a template 535 /// type parameter. 536 Parser::TPResult Parser::isStartOfTemplateTypeParameter() { 537 if (Tok.is(tok::kw_class)) { 538 // "class" may be the start of an elaborated-type-specifier or a 539 // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter. 540 switch (NextToken().getKind()) { 541 case tok::equal: 542 case tok::comma: 543 case tok::greater: 544 case tok::greatergreater: 545 case tok::ellipsis: 546 return TPResult::True; 547 548 case tok::identifier: 549 // This may be either a type-parameter or an elaborated-type-specifier. 550 // We have to look further. 551 break; 552 553 default: 554 return TPResult::False; 555 } 556 557 switch (GetLookAheadToken(2).getKind()) { 558 case tok::equal: 559 case tok::comma: 560 case tok::greater: 561 case tok::greatergreater: 562 return TPResult::True; 563 564 default: 565 return TPResult::False; 566 } 567 } 568 569 if (TryAnnotateTypeConstraint()) 570 return TPResult::Error; 571 572 if (isTypeConstraintAnnotation() && 573 // Next token might be 'auto' or 'decltype', indicating that this 574 // type-constraint is in fact part of a placeholder-type-specifier of a 575 // non-type template parameter. 576 !GetLookAheadToken(Tok.is(tok::annot_cxxscope) ? 2 : 1) 577 .isOneOf(tok::kw_auto, tok::kw_decltype)) 578 return TPResult::True; 579 580 // 'typedef' is a reasonably-common typo/thinko for 'typename', and is 581 // ill-formed otherwise. 582 if (Tok.isNot(tok::kw_typename) && Tok.isNot(tok::kw_typedef)) 583 return TPResult::False; 584 585 // C++ [temp.param]p2: 586 // There is no semantic difference between class and typename in a 587 // template-parameter. typename followed by an unqualified-id 588 // names a template type parameter. typename followed by a 589 // qualified-id denotes the type in a non-type 590 // parameter-declaration. 591 Token Next = NextToken(); 592 593 // If we have an identifier, skip over it. 594 if (Next.getKind() == tok::identifier) 595 Next = GetLookAheadToken(2); 596 597 switch (Next.getKind()) { 598 case tok::equal: 599 case tok::comma: 600 case tok::greater: 601 case tok::greatergreater: 602 case tok::ellipsis: 603 return TPResult::True; 604 605 case tok::kw_typename: 606 case tok::kw_typedef: 607 case tok::kw_class: 608 // These indicate that a comma was missed after a type parameter, not that 609 // we have found a non-type parameter. 610 return TPResult::True; 611 612 default: 613 return TPResult::False; 614 } 615 } 616 617 /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). 618 /// 619 /// template-parameter: [C++ temp.param] 620 /// type-parameter 621 /// parameter-declaration 622 /// 623 /// type-parameter: (See below) 624 /// type-parameter-key ...[opt] identifier[opt] 625 /// type-parameter-key identifier[opt] = type-id 626 /// (C++2a) type-constraint ...[opt] identifier[opt] 627 /// (C++2a) type-constraint identifier[opt] = type-id 628 /// 'template' '<' template-parameter-list '>' type-parameter-key 629 /// ...[opt] identifier[opt] 630 /// 'template' '<' template-parameter-list '>' type-parameter-key 631 /// identifier[opt] '=' id-expression 632 /// 633 /// type-parameter-key: 634 /// class 635 /// typename 636 /// 637 NamedDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { 638 639 switch (isStartOfTemplateTypeParameter()) { 640 case TPResult::True: 641 // Is there just a typo in the input code? ('typedef' instead of 642 // 'typename') 643 if (Tok.is(tok::kw_typedef)) { 644 Diag(Tok.getLocation(), diag::err_expected_template_parameter); 645 646 Diag(Tok.getLocation(), diag::note_meant_to_use_typename) 647 << FixItHint::CreateReplacement(CharSourceRange::getCharRange( 648 Tok.getLocation(), 649 Tok.getEndLoc()), 650 "typename"); 651 652 Tok.setKind(tok::kw_typename); 653 } 654 655 return ParseTypeParameter(Depth, Position); 656 case TPResult::False: 657 break; 658 659 case TPResult::Error: { 660 // We return an invalid parameter as opposed to null to avoid having bogus 661 // diagnostics about an empty template parameter list. 662 // FIXME: Fix ParseTemplateParameterList to better handle nullptr results 663 // from here. 664 // Return a NTTP as if there was an error in a scope specifier, the user 665 // probably meant to write the type of a NTTP. 666 DeclSpec DS(getAttrFactory()); 667 DS.SetTypeSpecError(); 668 Declarator D(DS, DeclaratorContext::TemplateParam); 669 D.SetIdentifier(nullptr, Tok.getLocation()); 670 D.setInvalidType(true); 671 NamedDecl *ErrorParam = Actions.ActOnNonTypeTemplateParameter( 672 getCurScope(), D, Depth, Position, /*EqualLoc=*/SourceLocation(), 673 /*DefaultArg=*/nullptr); 674 ErrorParam->setInvalidDecl(true); 675 SkipUntil(tok::comma, tok::greater, tok::greatergreater, 676 StopAtSemi | StopBeforeMatch); 677 return ErrorParam; 678 } 679 680 case TPResult::Ambiguous: 681 llvm_unreachable("template param classification can't be ambiguous"); 682 } 683 684 if (Tok.is(tok::kw_template)) 685 return ParseTemplateTemplateParameter(Depth, Position); 686 687 // If it's none of the above, then it must be a parameter declaration. 688 // NOTE: This will pick up errors in the closure of the template parameter 689 // list (e.g., template < ; Check here to implement >> style closures. 690 return ParseNonTypeTemplateParameter(Depth, Position); 691 } 692 693 /// Check whether the current token is a template-id annotation denoting a 694 /// type-constraint. 695 bool Parser::isTypeConstraintAnnotation() { 696 const Token &T = Tok.is(tok::annot_cxxscope) ? NextToken() : Tok; 697 if (T.isNot(tok::annot_template_id)) 698 return false; 699 const auto *ExistingAnnot = 700 static_cast<TemplateIdAnnotation *>(T.getAnnotationValue()); 701 return ExistingAnnot->Kind == TNK_Concept_template; 702 } 703 704 /// Try parsing a type-constraint at the current location. 705 /// 706 /// type-constraint: 707 /// nested-name-specifier[opt] concept-name 708 /// nested-name-specifier[opt] concept-name 709 /// '<' template-argument-list[opt] '>'[opt] 710 /// 711 /// \returns true if an error occurred, and false otherwise. 712 bool Parser::TryAnnotateTypeConstraint() { 713 if (!getLangOpts().CPlusPlus20) 714 return false; 715 CXXScopeSpec SS; 716 bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); 717 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 718 /*ObjectHasErrors=*/false, 719 /*EnteringContext=*/false, 720 /*MayBePseudoDestructor=*/nullptr, 721 // If this is not a type-constraint, then 722 // this scope-spec is part of the typename 723 // of a non-type template parameter 724 /*IsTypename=*/true, /*LastII=*/nullptr, 725 // We won't find concepts in 726 // non-namespaces anyway, so might as well 727 // parse this correctly for possible type 728 // names. 729 /*OnlyNamespace=*/false)) 730 return true; 731 732 if (Tok.is(tok::identifier)) { 733 UnqualifiedId PossibleConceptName; 734 PossibleConceptName.setIdentifier(Tok.getIdentifierInfo(), 735 Tok.getLocation()); 736 737 TemplateTy PossibleConcept; 738 bool MemberOfUnknownSpecialization = false; 739 auto TNK = Actions.isTemplateName(getCurScope(), SS, 740 /*hasTemplateKeyword=*/false, 741 PossibleConceptName, 742 /*ObjectType=*/ParsedType(), 743 /*EnteringContext=*/false, 744 PossibleConcept, 745 MemberOfUnknownSpecialization, 746 /*Disambiguation=*/true); 747 if (MemberOfUnknownSpecialization || !PossibleConcept || 748 TNK != TNK_Concept_template) { 749 if (SS.isNotEmpty()) 750 AnnotateScopeToken(SS, !WasScopeAnnotation); 751 return false; 752 } 753 754 // At this point we're sure we're dealing with a constrained parameter. It 755 // may or may not have a template parameter list following the concept 756 // name. 757 if (AnnotateTemplateIdToken(PossibleConcept, TNK, SS, 758 /*TemplateKWLoc=*/SourceLocation(), 759 PossibleConceptName, 760 /*AllowTypeAnnotation=*/false, 761 /*TypeConstraint=*/true)) 762 return true; 763 } 764 765 if (SS.isNotEmpty()) 766 AnnotateScopeToken(SS, !WasScopeAnnotation); 767 return false; 768 } 769 770 /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). 771 /// Other kinds of template parameters are parsed in 772 /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. 773 /// 774 /// type-parameter: [C++ temp.param] 775 /// 'class' ...[opt][C++0x] identifier[opt] 776 /// 'class' identifier[opt] '=' type-id 777 /// 'typename' ...[opt][C++0x] identifier[opt] 778 /// 'typename' identifier[opt] '=' type-id 779 NamedDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { 780 assert((Tok.isOneOf(tok::kw_class, tok::kw_typename) || 781 isTypeConstraintAnnotation()) && 782 "A type-parameter starts with 'class', 'typename' or a " 783 "type-constraint"); 784 785 CXXScopeSpec TypeConstraintSS; 786 TemplateIdAnnotation *TypeConstraint = nullptr; 787 bool TypenameKeyword = false; 788 SourceLocation KeyLoc; 789 ParseOptionalCXXScopeSpecifier(TypeConstraintSS, /*ObjectType=*/nullptr, 790 /*ObjectHasErrors=*/false, 791 /*EnteringContext*/ false); 792 if (Tok.is(tok::annot_template_id)) { 793 // Consume the 'type-constraint'. 794 TypeConstraint = 795 static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 796 assert(TypeConstraint->Kind == TNK_Concept_template && 797 "stray non-concept template-id annotation"); 798 KeyLoc = ConsumeAnnotationToken(); 799 } else { 800 assert(TypeConstraintSS.isEmpty() && 801 "expected type constraint after scope specifier"); 802 803 // Consume the 'class' or 'typename' keyword. 804 TypenameKeyword = Tok.is(tok::kw_typename); 805 KeyLoc = ConsumeToken(); 806 } 807 808 // Grab the ellipsis (if given). 809 SourceLocation EllipsisLoc; 810 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) { 811 Diag(EllipsisLoc, 812 getLangOpts().CPlusPlus11 813 ? diag::warn_cxx98_compat_variadic_templates 814 : diag::ext_variadic_templates); 815 } 816 817 // Grab the template parameter name (if given) 818 SourceLocation NameLoc = Tok.getLocation(); 819 IdentifierInfo *ParamName = nullptr; 820 if (Tok.is(tok::identifier)) { 821 ParamName = Tok.getIdentifierInfo(); 822 ConsumeToken(); 823 } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater, 824 tok::greatergreater)) { 825 // Unnamed template parameter. Don't have to do anything here, just 826 // don't consume this token. 827 } else { 828 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; 829 return nullptr; 830 } 831 832 // Recover from misplaced ellipsis. 833 bool AlreadyHasEllipsis = EllipsisLoc.isValid(); 834 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) 835 DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true); 836 837 // Grab a default argument (if available). 838 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 839 // we introduce the type parameter into the local scope. 840 SourceLocation EqualLoc; 841 ParsedType DefaultArg; 842 if (TryConsumeToken(tok::equal, EqualLoc)) 843 DefaultArg = 844 ParseTypeName(/*Range=*/nullptr, DeclaratorContext::TemplateTypeArg) 845 .get(); 846 847 NamedDecl *NewDecl = Actions.ActOnTypeParameter(getCurScope(), 848 TypenameKeyword, EllipsisLoc, 849 KeyLoc, ParamName, NameLoc, 850 Depth, Position, EqualLoc, 851 DefaultArg, 852 TypeConstraint != nullptr); 853 854 if (TypeConstraint) { 855 Actions.ActOnTypeConstraint(TypeConstraintSS, TypeConstraint, 856 cast<TemplateTypeParmDecl>(NewDecl), 857 EllipsisLoc); 858 } 859 860 return NewDecl; 861 } 862 863 /// ParseTemplateTemplateParameter - Handle the parsing of template 864 /// template parameters. 865 /// 866 /// type-parameter: [C++ temp.param] 867 /// 'template' '<' template-parameter-list '>' type-parameter-key 868 /// ...[opt] identifier[opt] 869 /// 'template' '<' template-parameter-list '>' type-parameter-key 870 /// identifier[opt] = id-expression 871 /// type-parameter-key: 872 /// 'class' 873 /// 'typename' [C++1z] 874 NamedDecl * 875 Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { 876 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); 877 878 // Handle the template <...> part. 879 SourceLocation TemplateLoc = ConsumeToken(); 880 SmallVector<NamedDecl*,8> TemplateParams; 881 SourceLocation LAngleLoc, RAngleLoc; 882 { 883 MultiParseScope TemplateParmScope(*this); 884 if (ParseTemplateParameters(TemplateParmScope, Depth + 1, TemplateParams, 885 LAngleLoc, RAngleLoc)) { 886 return nullptr; 887 } 888 } 889 890 // Provide an ExtWarn if the C++1z feature of using 'typename' here is used. 891 // Generate a meaningful error if the user forgot to put class before the 892 // identifier, comma, or greater. Provide a fixit if the identifier, comma, 893 // or greater appear immediately or after 'struct'. In the latter case, 894 // replace the keyword with 'class'. 895 if (!TryConsumeToken(tok::kw_class)) { 896 bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct); 897 const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok; 898 if (Tok.is(tok::kw_typename)) { 899 Diag(Tok.getLocation(), 900 getLangOpts().CPlusPlus17 901 ? diag::warn_cxx14_compat_template_template_param_typename 902 : diag::ext_template_template_param_typename) 903 << (!getLangOpts().CPlusPlus17 904 ? FixItHint::CreateReplacement(Tok.getLocation(), "class") 905 : FixItHint()); 906 } else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater, 907 tok::greatergreater, tok::ellipsis)) { 908 Diag(Tok.getLocation(), diag::err_class_on_template_template_param) 909 << getLangOpts().CPlusPlus17 910 << (Replace 911 ? FixItHint::CreateReplacement(Tok.getLocation(), "class") 912 : FixItHint::CreateInsertion(Tok.getLocation(), "class ")); 913 } else 914 Diag(Tok.getLocation(), diag::err_class_on_template_template_param) 915 << getLangOpts().CPlusPlus17; 916 917 if (Replace) 918 ConsumeToken(); 919 } 920 921 // Parse the ellipsis, if given. 922 SourceLocation EllipsisLoc; 923 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) 924 Diag(EllipsisLoc, 925 getLangOpts().CPlusPlus11 926 ? diag::warn_cxx98_compat_variadic_templates 927 : diag::ext_variadic_templates); 928 929 // Get the identifier, if given. 930 SourceLocation NameLoc = Tok.getLocation(); 931 IdentifierInfo *ParamName = nullptr; 932 if (Tok.is(tok::identifier)) { 933 ParamName = Tok.getIdentifierInfo(); 934 ConsumeToken(); 935 } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater, 936 tok::greatergreater)) { 937 // Unnamed template parameter. Don't have to do anything here, just 938 // don't consume this token. 939 } else { 940 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; 941 return nullptr; 942 } 943 944 // Recover from misplaced ellipsis. 945 bool AlreadyHasEllipsis = EllipsisLoc.isValid(); 946 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) 947 DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true); 948 949 TemplateParameterList *ParamList = 950 Actions.ActOnTemplateParameterList(Depth, SourceLocation(), 951 TemplateLoc, LAngleLoc, 952 TemplateParams, 953 RAngleLoc, nullptr); 954 955 // Grab a default argument (if available). 956 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 957 // we introduce the template parameter into the local scope. 958 SourceLocation EqualLoc; 959 ParsedTemplateArgument DefaultArg; 960 if (TryConsumeToken(tok::equal, EqualLoc)) { 961 DefaultArg = ParseTemplateTemplateArgument(); 962 if (DefaultArg.isInvalid()) { 963 Diag(Tok.getLocation(), 964 diag::err_default_template_template_parameter_not_template); 965 SkipUntil(tok::comma, tok::greater, tok::greatergreater, 966 StopAtSemi | StopBeforeMatch); 967 } 968 } 969 970 return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc, 971 ParamList, EllipsisLoc, 972 ParamName, NameLoc, Depth, 973 Position, EqualLoc, DefaultArg); 974 } 975 976 /// ParseNonTypeTemplateParameter - Handle the parsing of non-type 977 /// template parameters (e.g., in "template<int Size> class array;"). 978 /// 979 /// template-parameter: 980 /// ... 981 /// parameter-declaration 982 NamedDecl * 983 Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { 984 // Parse the declaration-specifiers (i.e., the type). 985 // FIXME: The type should probably be restricted in some way... Not all 986 // declarators (parts of declarators?) are accepted for parameters. 987 DeclSpec DS(AttrFactory); 988 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, 989 DeclSpecContext::DSC_template_param); 990 991 // Parse this as a typename. 992 Declarator ParamDecl(DS, DeclaratorContext::TemplateParam); 993 ParseDeclarator(ParamDecl); 994 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { 995 Diag(Tok.getLocation(), diag::err_expected_template_parameter); 996 return nullptr; 997 } 998 999 // Recover from misplaced ellipsis. 1000 SourceLocation EllipsisLoc; 1001 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) 1002 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl); 1003 1004 // If there is a default value, parse it. 1005 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 1006 // we introduce the template parameter into the local scope. 1007 SourceLocation EqualLoc; 1008 ExprResult DefaultArg; 1009 if (TryConsumeToken(tok::equal, EqualLoc)) { 1010 // C++ [temp.param]p15: 1011 // When parsing a default template-argument for a non-type 1012 // template-parameter, the first non-nested > is taken as the 1013 // end of the template-parameter-list rather than a greater-than 1014 // operator. 1015 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 1016 EnterExpressionEvaluationContext ConstantEvaluated( 1017 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); 1018 1019 DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); 1020 if (DefaultArg.isInvalid()) 1021 SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch); 1022 } 1023 1024 // Create the parameter. 1025 return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl, 1026 Depth, Position, EqualLoc, 1027 DefaultArg.get()); 1028 } 1029 1030 void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc, 1031 SourceLocation CorrectLoc, 1032 bool AlreadyHasEllipsis, 1033 bool IdentifierHasName) { 1034 FixItHint Insertion; 1035 if (!AlreadyHasEllipsis) 1036 Insertion = FixItHint::CreateInsertion(CorrectLoc, "..."); 1037 Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) 1038 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion 1039 << !IdentifierHasName; 1040 } 1041 1042 void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc, 1043 Declarator &D) { 1044 assert(EllipsisLoc.isValid()); 1045 bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid(); 1046 if (!AlreadyHasEllipsis) 1047 D.setEllipsisLoc(EllipsisLoc); 1048 DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(), 1049 AlreadyHasEllipsis, D.hasName()); 1050 } 1051 1052 /// Parses a '>' at the end of a template list. 1053 /// 1054 /// If this function encounters '>>', '>>>', '>=', or '>>=', it tries 1055 /// to determine if these tokens were supposed to be a '>' followed by 1056 /// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary. 1057 /// 1058 /// \param RAngleLoc the location of the consumed '>'. 1059 /// 1060 /// \param ConsumeLastToken if true, the '>' is consumed. 1061 /// 1062 /// \param ObjCGenericList if true, this is the '>' closing an Objective-C 1063 /// type parameter or type argument list, rather than a C++ template parameter 1064 /// or argument list. 1065 /// 1066 /// \returns true, if current token does not start with '>', false otherwise. 1067 bool Parser::ParseGreaterThanInTemplateList(SourceLocation LAngleLoc, 1068 SourceLocation &RAngleLoc, 1069 bool ConsumeLastToken, 1070 bool ObjCGenericList) { 1071 // What will be left once we've consumed the '>'. 1072 tok::TokenKind RemainingToken; 1073 const char *ReplacementStr = "> >"; 1074 bool MergeWithNextToken = false; 1075 1076 switch (Tok.getKind()) { 1077 default: 1078 Diag(getEndOfPreviousToken(), diag::err_expected) << tok::greater; 1079 Diag(LAngleLoc, diag::note_matching) << tok::less; 1080 return true; 1081 1082 case tok::greater: 1083 // Determine the location of the '>' token. Only consume this token 1084 // if the caller asked us to. 1085 RAngleLoc = Tok.getLocation(); 1086 if (ConsumeLastToken) 1087 ConsumeToken(); 1088 return false; 1089 1090 case tok::greatergreater: 1091 RemainingToken = tok::greater; 1092 break; 1093 1094 case tok::greatergreatergreater: 1095 RemainingToken = tok::greatergreater; 1096 break; 1097 1098 case tok::greaterequal: 1099 RemainingToken = tok::equal; 1100 ReplacementStr = "> ="; 1101 1102 // Join two adjacent '=' tokens into one, for cases like: 1103 // void (*p)() = f<int>; 1104 // return f<int>==p; 1105 if (NextToken().is(tok::equal) && 1106 areTokensAdjacent(Tok, NextToken())) { 1107 RemainingToken = tok::equalequal; 1108 MergeWithNextToken = true; 1109 } 1110 break; 1111 1112 case tok::greatergreaterequal: 1113 RemainingToken = tok::greaterequal; 1114 break; 1115 } 1116 1117 // This template-id is terminated by a token that starts with a '>'. 1118 // Outside C++11 and Objective-C, this is now error recovery. 1119 // 1120 // C++11 allows this when the token is '>>', and in CUDA + C++11 mode, we 1121 // extend that treatment to also apply to the '>>>' token. 1122 // 1123 // Objective-C allows this in its type parameter / argument lists. 1124 1125 SourceLocation TokBeforeGreaterLoc = PrevTokLocation; 1126 SourceLocation TokLoc = Tok.getLocation(); 1127 Token Next = NextToken(); 1128 1129 // Whether splitting the current token after the '>' would undesirably result 1130 // in the remaining token pasting with the token after it. This excludes the 1131 // MergeWithNextToken cases, which we've already handled. 1132 bool PreventMergeWithNextToken = 1133 (RemainingToken == tok::greater || 1134 RemainingToken == tok::greatergreater) && 1135 (Next.isOneOf(tok::greater, tok::greatergreater, 1136 tok::greatergreatergreater, tok::equal, tok::greaterequal, 1137 tok::greatergreaterequal, tok::equalequal)) && 1138 areTokensAdjacent(Tok, Next); 1139 1140 // Diagnose this situation as appropriate. 1141 if (!ObjCGenericList) { 1142 // The source range of the replaced token(s). 1143 CharSourceRange ReplacementRange = CharSourceRange::getCharRange( 1144 TokLoc, Lexer::AdvanceToTokenCharacter(TokLoc, 2, PP.getSourceManager(), 1145 getLangOpts())); 1146 1147 // A hint to put a space between the '>>'s. In order to make the hint as 1148 // clear as possible, we include the characters either side of the space in 1149 // the replacement, rather than just inserting a space at SecondCharLoc. 1150 FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange, 1151 ReplacementStr); 1152 1153 // A hint to put another space after the token, if it would otherwise be 1154 // lexed differently. 1155 FixItHint Hint2; 1156 if (PreventMergeWithNextToken) 1157 Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " "); 1158 1159 unsigned DiagId = diag::err_two_right_angle_brackets_need_space; 1160 if (getLangOpts().CPlusPlus11 && 1161 (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater))) 1162 DiagId = diag::warn_cxx98_compat_two_right_angle_brackets; 1163 else if (Tok.is(tok::greaterequal)) 1164 DiagId = diag::err_right_angle_bracket_equal_needs_space; 1165 Diag(TokLoc, DiagId) << Hint1 << Hint2; 1166 } 1167 1168 // Find the "length" of the resulting '>' token. This is not always 1, as it 1169 // can contain escaped newlines. 1170 unsigned GreaterLength = Lexer::getTokenPrefixLength( 1171 TokLoc, 1, PP.getSourceManager(), getLangOpts()); 1172 1173 // Annotate the source buffer to indicate that we split the token after the 1174 // '>'. This allows us to properly find the end of, and extract the spelling 1175 // of, the '>' token later. 1176 RAngleLoc = PP.SplitToken(TokLoc, GreaterLength); 1177 1178 // Strip the initial '>' from the token. 1179 bool CachingTokens = PP.IsPreviousCachedToken(Tok); 1180 1181 Token Greater = Tok; 1182 Greater.setLocation(RAngleLoc); 1183 Greater.setKind(tok::greater); 1184 Greater.setLength(GreaterLength); 1185 1186 unsigned OldLength = Tok.getLength(); 1187 if (MergeWithNextToken) { 1188 ConsumeToken(); 1189 OldLength += Tok.getLength(); 1190 } 1191 1192 Tok.setKind(RemainingToken); 1193 Tok.setLength(OldLength - GreaterLength); 1194 1195 // Split the second token if lexing it normally would lex a different token 1196 // (eg, the fifth token in 'A<B>>>' should re-lex as '>', not '>>'). 1197 SourceLocation AfterGreaterLoc = TokLoc.getLocWithOffset(GreaterLength); 1198 if (PreventMergeWithNextToken) 1199 AfterGreaterLoc = PP.SplitToken(AfterGreaterLoc, Tok.getLength()); 1200 Tok.setLocation(AfterGreaterLoc); 1201 1202 // Update the token cache to match what we just did if necessary. 1203 if (CachingTokens) { 1204 // If the previous cached token is being merged, delete it. 1205 if (MergeWithNextToken) 1206 PP.ReplacePreviousCachedToken({}); 1207 1208 if (ConsumeLastToken) 1209 PP.ReplacePreviousCachedToken({Greater, Tok}); 1210 else 1211 PP.ReplacePreviousCachedToken({Greater}); 1212 } 1213 1214 if (ConsumeLastToken) { 1215 PrevTokLocation = RAngleLoc; 1216 } else { 1217 PrevTokLocation = TokBeforeGreaterLoc; 1218 PP.EnterToken(Tok, /*IsReinject=*/true); 1219 Tok = Greater; 1220 } 1221 1222 return false; 1223 } 1224 1225 /// Parses a template-id that after the template name has 1226 /// already been parsed. 1227 /// 1228 /// This routine takes care of parsing the enclosed template argument 1229 /// list ('<' template-parameter-list [opt] '>') and placing the 1230 /// results into a form that can be transferred to semantic analysis. 1231 /// 1232 /// \param ConsumeLastToken if true, then we will consume the last 1233 /// token that forms the template-id. Otherwise, we will leave the 1234 /// last token in the stream (e.g., so that it can be replaced with an 1235 /// annotation token). 1236 /// 1237 /// \param NameHint is not required, and merely affects code completion. 1238 bool Parser::ParseTemplateIdAfterTemplateName(bool ConsumeLastToken, 1239 SourceLocation &LAngleLoc, 1240 TemplateArgList &TemplateArgs, 1241 SourceLocation &RAngleLoc, 1242 TemplateTy Template) { 1243 assert(Tok.is(tok::less) && "Must have already parsed the template-name"); 1244 1245 // Consume the '<'. 1246 LAngleLoc = ConsumeToken(); 1247 1248 // Parse the optional template-argument-list. 1249 bool Invalid = false; 1250 { 1251 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 1252 if (!Tok.isOneOf(tok::greater, tok::greatergreater, 1253 tok::greatergreatergreater, tok::greaterequal, 1254 tok::greatergreaterequal)) 1255 Invalid = ParseTemplateArgumentList(TemplateArgs, Template, LAngleLoc); 1256 1257 if (Invalid) { 1258 // Try to find the closing '>'. 1259 if (getLangOpts().CPlusPlus11) 1260 SkipUntil(tok::greater, tok::greatergreater, 1261 tok::greatergreatergreater, StopAtSemi | StopBeforeMatch); 1262 else 1263 SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch); 1264 } 1265 } 1266 1267 return ParseGreaterThanInTemplateList(LAngleLoc, RAngleLoc, ConsumeLastToken, 1268 /*ObjCGenericList=*/false) || 1269 Invalid; 1270 } 1271 1272 /// Replace the tokens that form a simple-template-id with an 1273 /// annotation token containing the complete template-id. 1274 /// 1275 /// The first token in the stream must be the name of a template that 1276 /// is followed by a '<'. This routine will parse the complete 1277 /// simple-template-id and replace the tokens with a single annotation 1278 /// token with one of two different kinds: if the template-id names a 1279 /// type (and \p AllowTypeAnnotation is true), the annotation token is 1280 /// a type annotation that includes the optional nested-name-specifier 1281 /// (\p SS). Otherwise, the annotation token is a template-id 1282 /// annotation that does not include the optional 1283 /// nested-name-specifier. 1284 /// 1285 /// \param Template the declaration of the template named by the first 1286 /// token (an identifier), as returned from \c Action::isTemplateName(). 1287 /// 1288 /// \param TNK the kind of template that \p Template 1289 /// refers to, as returned from \c Action::isTemplateName(). 1290 /// 1291 /// \param SS if non-NULL, the nested-name-specifier that precedes 1292 /// this template name. 1293 /// 1294 /// \param TemplateKWLoc if valid, specifies that this template-id 1295 /// annotation was preceded by the 'template' keyword and gives the 1296 /// location of that keyword. If invalid (the default), then this 1297 /// template-id was not preceded by a 'template' keyword. 1298 /// 1299 /// \param AllowTypeAnnotation if true (the default), then a 1300 /// simple-template-id that refers to a class template, template 1301 /// template parameter, or other template that produces a type will be 1302 /// replaced with a type annotation token. Otherwise, the 1303 /// simple-template-id is always replaced with a template-id 1304 /// annotation token. 1305 /// 1306 /// \param TypeConstraint if true, then this is actually a type-constraint, 1307 /// meaning that the template argument list can be omitted (and the template in 1308 /// question must be a concept). 1309 /// 1310 /// If an unrecoverable parse error occurs and no annotation token can be 1311 /// formed, this function returns true. 1312 /// 1313 bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, 1314 CXXScopeSpec &SS, 1315 SourceLocation TemplateKWLoc, 1316 UnqualifiedId &TemplateName, 1317 bool AllowTypeAnnotation, 1318 bool TypeConstraint) { 1319 assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++"); 1320 assert((Tok.is(tok::less) || TypeConstraint) && 1321 "Parser isn't at the beginning of a template-id"); 1322 assert(!(TypeConstraint && AllowTypeAnnotation) && "type-constraint can't be " 1323 "a type annotation"); 1324 assert((!TypeConstraint || TNK == TNK_Concept_template) && "type-constraint " 1325 "must accompany a concept name"); 1326 assert((Template || TNK == TNK_Non_template) && "missing template name"); 1327 1328 // Consume the template-name. 1329 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); 1330 1331 // Parse the enclosed template argument list. 1332 SourceLocation LAngleLoc, RAngleLoc; 1333 TemplateArgList TemplateArgs; 1334 bool ArgsInvalid = false; 1335 if (!TypeConstraint || Tok.is(tok::less)) { 1336 ArgsInvalid = ParseTemplateIdAfterTemplateName( 1337 false, LAngleLoc, TemplateArgs, RAngleLoc, Template); 1338 // If we couldn't recover from invalid arguments, don't form an annotation 1339 // token -- we don't know how much to annotate. 1340 // FIXME: This can lead to duplicate diagnostics if we retry parsing this 1341 // template-id in another context. Try to annotate anyway? 1342 if (RAngleLoc.isInvalid()) 1343 return true; 1344 } 1345 1346 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs); 1347 1348 // Build the annotation token. 1349 if (TNK == TNK_Type_template && AllowTypeAnnotation) { 1350 TypeResult Type = ArgsInvalid 1351 ? TypeError() 1352 : Actions.ActOnTemplateIdType( 1353 getCurScope(), SS, TemplateKWLoc, Template, 1354 TemplateName.Identifier, TemplateNameLoc, 1355 LAngleLoc, TemplateArgsPtr, RAngleLoc); 1356 1357 Tok.setKind(tok::annot_typename); 1358 setTypeAnnotation(Tok, Type); 1359 if (SS.isNotEmpty()) 1360 Tok.setLocation(SS.getBeginLoc()); 1361 else if (TemplateKWLoc.isValid()) 1362 Tok.setLocation(TemplateKWLoc); 1363 else 1364 Tok.setLocation(TemplateNameLoc); 1365 } else { 1366 // Build a template-id annotation token that can be processed 1367 // later. 1368 Tok.setKind(tok::annot_template_id); 1369 1370 IdentifierInfo *TemplateII = 1371 TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier 1372 ? TemplateName.Identifier 1373 : nullptr; 1374 1375 OverloadedOperatorKind OpKind = 1376 TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier 1377 ? OO_None 1378 : TemplateName.OperatorFunctionId.Operator; 1379 1380 TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create( 1381 TemplateKWLoc, TemplateNameLoc, TemplateII, OpKind, Template, TNK, 1382 LAngleLoc, RAngleLoc, TemplateArgs, ArgsInvalid, TemplateIds); 1383 1384 Tok.setAnnotationValue(TemplateId); 1385 if (TemplateKWLoc.isValid()) 1386 Tok.setLocation(TemplateKWLoc); 1387 else 1388 Tok.setLocation(TemplateNameLoc); 1389 } 1390 1391 // Common fields for the annotation token 1392 Tok.setAnnotationEndLoc(RAngleLoc); 1393 1394 // In case the tokens were cached, have Preprocessor replace them with the 1395 // annotation token. 1396 PP.AnnotateCachedTokens(Tok); 1397 return false; 1398 } 1399 1400 /// Replaces a template-id annotation token with a type 1401 /// annotation token. 1402 /// 1403 /// If there was a failure when forming the type from the template-id, 1404 /// a type annotation token will still be created, but will have a 1405 /// NULL type pointer to signify an error. 1406 /// 1407 /// \param SS The scope specifier appearing before the template-id, if any. 1408 /// 1409 /// \param IsClassName Is this template-id appearing in a context where we 1410 /// know it names a class, such as in an elaborated-type-specifier or 1411 /// base-specifier? ('typename' and 'template' are unneeded and disallowed 1412 /// in those contexts.) 1413 void Parser::AnnotateTemplateIdTokenAsType(CXXScopeSpec &SS, 1414 bool IsClassName) { 1415 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); 1416 1417 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1418 assert(TemplateId->mightBeType() && 1419 "Only works for type and dependent templates"); 1420 1421 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), 1422 TemplateId->NumArgs); 1423 1424 TypeResult Type = 1425 TemplateId->isInvalid() 1426 ? TypeError() 1427 : Actions.ActOnTemplateIdType( 1428 getCurScope(), SS, TemplateId->TemplateKWLoc, 1429 TemplateId->Template, TemplateId->Name, 1430 TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, 1431 TemplateArgsPtr, TemplateId->RAngleLoc, 1432 /*IsCtorOrDtorName*/ false, IsClassName); 1433 // Create the new "type" annotation token. 1434 Tok.setKind(tok::annot_typename); 1435 setTypeAnnotation(Tok, Type); 1436 if (SS.isNotEmpty()) // it was a C++ qualified type name. 1437 Tok.setLocation(SS.getBeginLoc()); 1438 // End location stays the same 1439 1440 // Replace the template-id annotation token, and possible the scope-specifier 1441 // that precedes it, with the typename annotation token. 1442 PP.AnnotateCachedTokens(Tok); 1443 } 1444 1445 /// Determine whether the given token can end a template argument. 1446 static bool isEndOfTemplateArgument(Token Tok) { 1447 // FIXME: Handle '>>>'. 1448 return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater, 1449 tok::greatergreatergreater); 1450 } 1451 1452 /// Parse a C++ template template argument. 1453 ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { 1454 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && 1455 !Tok.is(tok::annot_cxxscope)) 1456 return ParsedTemplateArgument(); 1457 1458 // C++0x [temp.arg.template]p1: 1459 // A template-argument for a template template-parameter shall be the name 1460 // of a class template or an alias template, expressed as id-expression. 1461 // 1462 // We parse an id-expression that refers to a class template or alias 1463 // template. The grammar we parse is: 1464 // 1465 // nested-name-specifier[opt] template[opt] identifier ...[opt] 1466 // 1467 // followed by a token that terminates a template argument, such as ',', 1468 // '>', or (in some cases) '>>'. 1469 CXXScopeSpec SS; // nested-name-specifier, if present 1470 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 1471 /*ObjectHasErrors=*/false, 1472 /*EnteringContext=*/false); 1473 1474 ParsedTemplateArgument Result; 1475 SourceLocation EllipsisLoc; 1476 if (SS.isSet() && Tok.is(tok::kw_template)) { 1477 // Parse the optional 'template' keyword following the 1478 // nested-name-specifier. 1479 SourceLocation TemplateKWLoc = ConsumeToken(); 1480 1481 if (Tok.is(tok::identifier)) { 1482 // We appear to have a dependent template name. 1483 UnqualifiedId Name; 1484 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 1485 ConsumeToken(); // the identifier 1486 1487 TryConsumeToken(tok::ellipsis, EllipsisLoc); 1488 1489 // If the next token signals the end of a template argument, then we have 1490 // a (possibly-dependent) template name that could be a template template 1491 // argument. 1492 TemplateTy Template; 1493 if (isEndOfTemplateArgument(Tok) && 1494 Actions.ActOnTemplateName(getCurScope(), SS, TemplateKWLoc, Name, 1495 /*ObjectType=*/nullptr, 1496 /*EnteringContext=*/false, Template)) 1497 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); 1498 } 1499 } else if (Tok.is(tok::identifier)) { 1500 // We may have a (non-dependent) template name. 1501 TemplateTy Template; 1502 UnqualifiedId Name; 1503 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 1504 ConsumeToken(); // the identifier 1505 1506 TryConsumeToken(tok::ellipsis, EllipsisLoc); 1507 1508 if (isEndOfTemplateArgument(Tok)) { 1509 bool MemberOfUnknownSpecialization; 1510 TemplateNameKind TNK = Actions.isTemplateName( 1511 getCurScope(), SS, 1512 /*hasTemplateKeyword=*/false, Name, 1513 /*ObjectType=*/nullptr, 1514 /*EnteringContext=*/false, Template, MemberOfUnknownSpecialization); 1515 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { 1516 // We have an id-expression that refers to a class template or 1517 // (C++0x) alias template. 1518 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); 1519 } 1520 } 1521 } 1522 1523 // If this is a pack expansion, build it as such. 1524 if (EllipsisLoc.isValid() && !Result.isInvalid()) 1525 Result = Actions.ActOnPackExpansion(Result, EllipsisLoc); 1526 1527 return Result; 1528 } 1529 1530 /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). 1531 /// 1532 /// template-argument: [C++ 14.2] 1533 /// constant-expression 1534 /// type-id 1535 /// id-expression 1536 ParsedTemplateArgument Parser::ParseTemplateArgument() { 1537 // C++ [temp.arg]p2: 1538 // In a template-argument, an ambiguity between a type-id and an 1539 // expression is resolved to a type-id, regardless of the form of 1540 // the corresponding template-parameter. 1541 // 1542 // Therefore, we initially try to parse a type-id - and isCXXTypeId might look 1543 // up and annotate an identifier as an id-expression during disambiguation, 1544 // so enter the appropriate context for a constant expression template 1545 // argument before trying to disambiguate. 1546 1547 EnterExpressionEvaluationContext EnterConstantEvaluated( 1548 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated, 1549 /*LambdaContextDecl=*/nullptr, 1550 /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_TemplateArgument); 1551 if (isCXXTypeId(TypeIdAsTemplateArgument)) { 1552 TypeResult TypeArg = ParseTypeName( 1553 /*Range=*/nullptr, DeclaratorContext::TemplateArg); 1554 return Actions.ActOnTemplateTypeArgument(TypeArg); 1555 } 1556 1557 // Try to parse a template template argument. 1558 { 1559 TentativeParsingAction TPA(*this); 1560 1561 ParsedTemplateArgument TemplateTemplateArgument 1562 = ParseTemplateTemplateArgument(); 1563 if (!TemplateTemplateArgument.isInvalid()) { 1564 TPA.Commit(); 1565 return TemplateTemplateArgument; 1566 } 1567 1568 // Revert this tentative parse to parse a non-type template argument. 1569 TPA.Revert(); 1570 } 1571 1572 // Parse a non-type template argument. 1573 SourceLocation Loc = Tok.getLocation(); 1574 ExprResult ExprArg = ParseConstantExpressionInExprEvalContext(MaybeTypeCast); 1575 if (ExprArg.isInvalid() || !ExprArg.get()) { 1576 return ParsedTemplateArgument(); 1577 } 1578 1579 return ParsedTemplateArgument(ParsedTemplateArgument::NonType, 1580 ExprArg.get(), Loc); 1581 } 1582 1583 /// ParseTemplateArgumentList - Parse a C++ template-argument-list 1584 /// (C++ [temp.names]). Returns true if there was an error. 1585 /// 1586 /// template-argument-list: [C++ 14.2] 1587 /// template-argument 1588 /// template-argument-list ',' template-argument 1589 /// 1590 /// \param Template is only used for code completion, and may be null. 1591 bool Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs, 1592 TemplateTy Template, 1593 SourceLocation OpenLoc) { 1594 1595 ColonProtectionRAIIObject ColonProtection(*this, false); 1596 1597 auto RunSignatureHelp = [&] { 1598 if (!Template) 1599 return QualType(); 1600 CalledSignatureHelp = true; 1601 return Actions.ProduceTemplateArgumentSignatureHelp(Template, TemplateArgs, 1602 OpenLoc); 1603 }; 1604 1605 do { 1606 PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp); 1607 ParsedTemplateArgument Arg = ParseTemplateArgument(); 1608 SourceLocation EllipsisLoc; 1609 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) 1610 Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc); 1611 1612 if (Arg.isInvalid()) { 1613 if (PP.isCodeCompletionReached() && !CalledSignatureHelp) 1614 RunSignatureHelp(); 1615 return true; 1616 } 1617 1618 // Save this template argument. 1619 TemplateArgs.push_back(Arg); 1620 1621 // If the next token is a comma, consume it and keep reading 1622 // arguments. 1623 } while (TryConsumeToken(tok::comma)); 1624 1625 return false; 1626 } 1627 1628 /// Parse a C++ explicit template instantiation 1629 /// (C++ [temp.explicit]). 1630 /// 1631 /// explicit-instantiation: 1632 /// 'extern' [opt] 'template' declaration 1633 /// 1634 /// Note that the 'extern' is a GNU extension and C++11 feature. 1635 Decl *Parser::ParseExplicitInstantiation(DeclaratorContext Context, 1636 SourceLocation ExternLoc, 1637 SourceLocation TemplateLoc, 1638 SourceLocation &DeclEnd, 1639 ParsedAttributes &AccessAttrs, 1640 AccessSpecifier AS) { 1641 // This isn't really required here. 1642 ParsingDeclRAIIObject 1643 ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); 1644 1645 return ParseSingleDeclarationAfterTemplate( 1646 Context, ParsedTemplateInfo(ExternLoc, TemplateLoc), 1647 ParsingTemplateParams, DeclEnd, AccessAttrs, AS); 1648 } 1649 1650 SourceRange Parser::ParsedTemplateInfo::getSourceRange() const { 1651 if (TemplateParams) 1652 return getTemplateParamsRange(TemplateParams->data(), 1653 TemplateParams->size()); 1654 1655 SourceRange R(TemplateLoc); 1656 if (ExternLoc.isValid()) 1657 R.setBegin(ExternLoc); 1658 return R; 1659 } 1660 1661 void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) { 1662 ((Parser *)P)->ParseLateTemplatedFuncDef(LPT); 1663 } 1664 1665 /// Late parse a C++ function template in Microsoft mode. 1666 void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) { 1667 if (!LPT.D) 1668 return; 1669 1670 // Destroy TemplateIdAnnotations when we're done, if possible. 1671 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this); 1672 1673 // Get the FunctionDecl. 1674 FunctionDecl *FunD = LPT.D->getAsFunction(); 1675 // Track template parameter depth. 1676 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 1677 1678 // To restore the context after late parsing. 1679 Sema::ContextRAII GlobalSavedContext( 1680 Actions, Actions.Context.getTranslationUnitDecl()); 1681 1682 MultiParseScope Scopes(*this); 1683 1684 // Get the list of DeclContexts to reenter. 1685 SmallVector<DeclContext*, 4> DeclContextsToReenter; 1686 for (DeclContext *DC = FunD; DC && !DC->isTranslationUnit(); 1687 DC = DC->getLexicalParent()) 1688 DeclContextsToReenter.push_back(DC); 1689 1690 // Reenter scopes from outermost to innermost. 1691 for (DeclContext *DC : reverse(DeclContextsToReenter)) { 1692 CurTemplateDepthTracker.addDepth( 1693 ReenterTemplateScopes(Scopes, cast<Decl>(DC))); 1694 Scopes.Enter(Scope::DeclScope); 1695 // We'll reenter the function context itself below. 1696 if (DC != FunD) 1697 Actions.PushDeclContext(Actions.getCurScope(), DC); 1698 } 1699 1700 assert(!LPT.Toks.empty() && "Empty body!"); 1701 1702 // Append the current token at the end of the new token stream so that it 1703 // doesn't get lost. 1704 LPT.Toks.push_back(Tok); 1705 PP.EnterTokenStream(LPT.Toks, true, /*IsReinject*/true); 1706 1707 // Consume the previously pushed token. 1708 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 1709 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) && 1710 "Inline method not starting with '{', ':' or 'try'"); 1711 1712 // Parse the method body. Function body parsing code is similar enough 1713 // to be re-used for method bodies as well. 1714 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope | 1715 Scope::CompoundStmtScope); 1716 1717 // Recreate the containing function DeclContext. 1718 Sema::ContextRAII FunctionSavedContext(Actions, FunD->getLexicalParent()); 1719 1720 Actions.ActOnStartOfFunctionDef(getCurScope(), FunD); 1721 1722 if (Tok.is(tok::kw_try)) { 1723 ParseFunctionTryBlock(LPT.D, FnScope); 1724 } else { 1725 if (Tok.is(tok::colon)) 1726 ParseConstructorInitializer(LPT.D); 1727 else 1728 Actions.ActOnDefaultCtorInitializers(LPT.D); 1729 1730 if (Tok.is(tok::l_brace)) { 1731 assert((!isa<FunctionTemplateDecl>(LPT.D) || 1732 cast<FunctionTemplateDecl>(LPT.D) 1733 ->getTemplateParameters() 1734 ->getDepth() == TemplateParameterDepth - 1) && 1735 "TemplateParameterDepth should be greater than the depth of " 1736 "current template being instantiated!"); 1737 ParseFunctionStatementBody(LPT.D, FnScope); 1738 Actions.UnmarkAsLateParsedTemplate(FunD); 1739 } else 1740 Actions.ActOnFinishFunctionBody(LPT.D, nullptr); 1741 } 1742 } 1743 1744 /// Lex a delayed template function for late parsing. 1745 void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) { 1746 tok::TokenKind kind = Tok.getKind(); 1747 if (!ConsumeAndStoreFunctionPrologue(Toks)) { 1748 // Consume everything up to (and including) the matching right brace. 1749 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1750 } 1751 1752 // If we're in a function-try-block, we need to store all the catch blocks. 1753 if (kind == tok::kw_try) { 1754 while (Tok.is(tok::kw_catch)) { 1755 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); 1756 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1757 } 1758 } 1759 } 1760 1761 /// We've parsed something that could plausibly be intended to be a template 1762 /// name (\p LHS) followed by a '<' token, and the following code can't possibly 1763 /// be an expression. Determine if this is likely to be a template-id and if so, 1764 /// diagnose it. 1765 bool Parser::diagnoseUnknownTemplateId(ExprResult LHS, SourceLocation Less) { 1766 TentativeParsingAction TPA(*this); 1767 // FIXME: We could look at the token sequence in a lot more detail here. 1768 if (SkipUntil(tok::greater, tok::greatergreater, tok::greatergreatergreater, 1769 StopAtSemi | StopBeforeMatch)) { 1770 TPA.Commit(); 1771 1772 SourceLocation Greater; 1773 ParseGreaterThanInTemplateList(Less, Greater, true, false); 1774 Actions.diagnoseExprIntendedAsTemplateName(getCurScope(), LHS, 1775 Less, Greater); 1776 return true; 1777 } 1778 1779 // There's no matching '>' token, this probably isn't supposed to be 1780 // interpreted as a template-id. Parse it as an (ill-formed) comparison. 1781 TPA.Revert(); 1782 return false; 1783 } 1784 1785 void Parser::checkPotentialAngleBracket(ExprResult &PotentialTemplateName) { 1786 assert(Tok.is(tok::less) && "not at a potential angle bracket"); 1787 1788 bool DependentTemplateName = false; 1789 if (!Actions.mightBeIntendedToBeTemplateName(PotentialTemplateName, 1790 DependentTemplateName)) 1791 return; 1792 1793 // OK, this might be a name that the user intended to be parsed as a 1794 // template-name, followed by a '<' token. Check for some easy cases. 1795 1796 // If we have potential_template<>, then it's supposed to be a template-name. 1797 if (NextToken().is(tok::greater) || 1798 (getLangOpts().CPlusPlus11 && 1799 NextToken().isOneOf(tok::greatergreater, tok::greatergreatergreater))) { 1800 SourceLocation Less = ConsumeToken(); 1801 SourceLocation Greater; 1802 ParseGreaterThanInTemplateList(Less, Greater, true, false); 1803 Actions.diagnoseExprIntendedAsTemplateName( 1804 getCurScope(), PotentialTemplateName, Less, Greater); 1805 // FIXME: Perform error recovery. 1806 PotentialTemplateName = ExprError(); 1807 return; 1808 } 1809 1810 // If we have 'potential_template<type-id', assume it's supposed to be a 1811 // template-name if there's a matching '>' later on. 1812 { 1813 // FIXME: Avoid the tentative parse when NextToken() can't begin a type. 1814 TentativeParsingAction TPA(*this); 1815 SourceLocation Less = ConsumeToken(); 1816 if (isTypeIdUnambiguously() && 1817 diagnoseUnknownTemplateId(PotentialTemplateName, Less)) { 1818 TPA.Commit(); 1819 // FIXME: Perform error recovery. 1820 PotentialTemplateName = ExprError(); 1821 return; 1822 } 1823 TPA.Revert(); 1824 } 1825 1826 // Otherwise, remember that we saw this in case we see a potentially-matching 1827 // '>' token later on. 1828 AngleBracketTracker::Priority Priority = 1829 (DependentTemplateName ? AngleBracketTracker::DependentName 1830 : AngleBracketTracker::PotentialTypo) | 1831 (Tok.hasLeadingSpace() ? AngleBracketTracker::SpaceBeforeLess 1832 : AngleBracketTracker::NoSpaceBeforeLess); 1833 AngleBrackets.add(*this, PotentialTemplateName.get(), Tok.getLocation(), 1834 Priority); 1835 } 1836 1837 bool Parser::checkPotentialAngleBracketDelimiter( 1838 const AngleBracketTracker::Loc &LAngle, const Token &OpToken) { 1839 // If a comma in an expression context is followed by a type that can be a 1840 // template argument and cannot be an expression, then this is ill-formed, 1841 // but might be intended to be part of a template-id. 1842 if (OpToken.is(tok::comma) && isTypeIdUnambiguously() && 1843 diagnoseUnknownTemplateId(LAngle.TemplateName, LAngle.LessLoc)) { 1844 AngleBrackets.clear(*this); 1845 return true; 1846 } 1847 1848 // If a context that looks like a template-id is followed by '()', then 1849 // this is ill-formed, but might be intended to be a template-id 1850 // followed by '()'. 1851 if (OpToken.is(tok::greater) && Tok.is(tok::l_paren) && 1852 NextToken().is(tok::r_paren)) { 1853 Actions.diagnoseExprIntendedAsTemplateName( 1854 getCurScope(), LAngle.TemplateName, LAngle.LessLoc, 1855 OpToken.getLocation()); 1856 AngleBrackets.clear(*this); 1857 return true; 1858 } 1859 1860 // After a '>' (etc), we're no longer potentially in a construct that's 1861 // intended to be treated as a template-id. 1862 if (OpToken.is(tok::greater) || 1863 (getLangOpts().CPlusPlus11 && 1864 OpToken.isOneOf(tok::greatergreater, tok::greatergreatergreater))) 1865 AngleBrackets.clear(*this); 1866 return false; 1867 } 1868