1 //===--- ParseCXXInlineMethods.cpp - C++ class inline methods 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 for C++ class inline methods. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Parse/Parser.h" 15 #include "clang/AST/DeclTemplate.h" 16 #include "clang/Parse/ParseDiagnostic.h" 17 #include "clang/Parse/RAIIObjectsForParser.h" 18 #include "clang/Sema/DeclSpec.h" 19 #include "clang/Sema/Scope.h" 20 using namespace clang; 21 22 /// ParseCXXInlineMethodDef - We parsed and verified that the specified 23 /// Declarator is a well formed C++ inline method definition. Now lex its body 24 /// and store its tokens for parsing after the C++ class is complete. 25 NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, 26 AttributeList *AccessAttrs, 27 ParsingDeclarator &D, 28 const ParsedTemplateInfo &TemplateInfo, 29 const VirtSpecifiers& VS, 30 SourceLocation PureSpecLoc) { 31 assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); 32 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) && 33 "Current token not a '{', ':', '=', or 'try'!"); 34 35 MultiTemplateParamsArg TemplateParams( 36 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() 37 : nullptr, 38 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); 39 40 NamedDecl *FnD; 41 if (D.getDeclSpec().isFriendSpecified()) 42 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, 43 TemplateParams); 44 else { 45 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, 46 TemplateParams, nullptr, 47 VS, ICIS_NoInit); 48 if (FnD) { 49 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs); 50 if (PureSpecLoc.isValid()) 51 Actions.ActOnPureSpecifier(FnD, PureSpecLoc); 52 } 53 } 54 55 if (FnD) 56 HandleMemberFunctionDeclDelays(D, FnD); 57 58 D.complete(FnD); 59 60 if (TryConsumeToken(tok::equal)) { 61 if (!FnD) { 62 SkipUntil(tok::semi); 63 return nullptr; 64 } 65 66 bool Delete = false; 67 SourceLocation KWLoc; 68 SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1); 69 if (TryConsumeToken(tok::kw_delete, KWLoc)) { 70 Diag(KWLoc, getLangOpts().CPlusPlus11 71 ? diag::warn_cxx98_compat_defaulted_deleted_function 72 : diag::ext_defaulted_deleted_function) 73 << 1 /* deleted */; 74 Actions.SetDeclDeleted(FnD, KWLoc); 75 Delete = true; 76 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) { 77 DeclAsFunction->setRangeEnd(KWEndLoc); 78 } 79 } else if (TryConsumeToken(tok::kw_default, KWLoc)) { 80 Diag(KWLoc, getLangOpts().CPlusPlus11 81 ? diag::warn_cxx98_compat_defaulted_deleted_function 82 : diag::ext_defaulted_deleted_function) 83 << 0 /* defaulted */; 84 Actions.SetDeclDefaulted(FnD, KWLoc); 85 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) { 86 DeclAsFunction->setRangeEnd(KWEndLoc); 87 } 88 } else { 89 llvm_unreachable("function definition after = not 'delete' or 'default'"); 90 } 91 92 if (Tok.is(tok::comma)) { 93 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 94 << Delete; 95 SkipUntil(tok::semi); 96 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after, 97 Delete ? "delete" : "default")) { 98 SkipUntil(tok::semi); 99 } 100 101 return FnD; 102 } 103 104 if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) && 105 trySkippingFunctionBody()) { 106 Actions.ActOnSkippedFunctionBody(FnD); 107 return FnD; 108 } 109 110 // In delayed template parsing mode, if we are within a class template 111 // or if we are about to parse function member template then consume 112 // the tokens and store them for parsing at the end of the translation unit. 113 if (getLangOpts().DelayedTemplateParsing && 114 D.getFunctionDefinitionKind() == FDK_Definition && 115 !D.getDeclSpec().isConstexprSpecified() && 116 !(FnD && FnD->getAsFunction() && 117 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) && 118 ((Actions.CurContext->isDependentContext() || 119 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 120 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) && 121 !Actions.IsInsideALocalClassWithinATemplateFunction())) { 122 123 CachedTokens Toks; 124 LexTemplateFunctionForLateParsing(Toks); 125 126 if (FnD) { 127 FunctionDecl *FD = FnD->getAsFunction(); 128 Actions.CheckForFunctionRedefinition(FD); 129 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks); 130 } 131 132 return FnD; 133 } 134 135 // Consume the tokens and store them for later parsing. 136 137 LexedMethod* LM = new LexedMethod(this, FnD); 138 getCurrentClass().LateParsedDeclarations.push_back(LM); 139 LM->TemplateScope = getCurScope()->isTemplateParamScope(); 140 CachedTokens &Toks = LM->Toks; 141 142 tok::TokenKind kind = Tok.getKind(); 143 // Consume everything up to (and including) the left brace of the 144 // function body. 145 if (ConsumeAndStoreFunctionPrologue(Toks)) { 146 // We didn't find the left-brace we expected after the 147 // constructor initializer; we already printed an error, and it's likely 148 // impossible to recover, so don't try to parse this method later. 149 // Skip over the rest of the decl and back to somewhere that looks 150 // reasonable. 151 SkipMalformedDecl(); 152 delete getCurrentClass().LateParsedDeclarations.back(); 153 getCurrentClass().LateParsedDeclarations.pop_back(); 154 return FnD; 155 } else { 156 // Consume everything up to (and including) the matching right brace. 157 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 158 } 159 160 // If we're in a function-try-block, we need to store all the catch blocks. 161 if (kind == tok::kw_try) { 162 while (Tok.is(tok::kw_catch)) { 163 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); 164 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 165 } 166 } 167 168 if (FnD) { 169 // If this is a friend function, mark that it's late-parsed so that 170 // it's still known to be a definition even before we attach the 171 // parsed body. Sema needs to treat friend function definitions 172 // differently during template instantiation, and it's possible for 173 // the containing class to be instantiated before all its member 174 // function definitions are parsed. 175 // 176 // If you remove this, you can remove the code that clears the flag 177 // after parsing the member. 178 if (D.getDeclSpec().isFriendSpecified()) { 179 FunctionDecl *FD = FnD->getAsFunction(); 180 Actions.CheckForFunctionRedefinition(FD); 181 FD->setLateTemplateParsed(true); 182 } 183 } else { 184 // If semantic analysis could not build a function declaration, 185 // just throw away the late-parsed declaration. 186 delete getCurrentClass().LateParsedDeclarations.back(); 187 getCurrentClass().LateParsedDeclarations.pop_back(); 188 } 189 190 return FnD; 191 } 192 193 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the 194 /// specified Declarator is a well formed C++ non-static data member 195 /// declaration. Now lex its initializer and store its tokens for parsing 196 /// after the class is complete. 197 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) { 198 assert(Tok.isOneOf(tok::l_brace, tok::equal) && 199 "Current token not a '{' or '='!"); 200 201 LateParsedMemberInitializer *MI = 202 new LateParsedMemberInitializer(this, VarD); 203 getCurrentClass().LateParsedDeclarations.push_back(MI); 204 CachedTokens &Toks = MI->Toks; 205 206 tok::TokenKind kind = Tok.getKind(); 207 if (kind == tok::equal) { 208 Toks.push_back(Tok); 209 ConsumeToken(); 210 } 211 212 if (kind == tok::l_brace) { 213 // Begin by storing the '{' token. 214 Toks.push_back(Tok); 215 ConsumeBrace(); 216 217 // Consume everything up to (and including) the matching right brace. 218 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true); 219 } else { 220 // Consume everything up to (but excluding) the comma or semicolon. 221 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer); 222 } 223 224 // Store an artificial EOF token to ensure that we don't run off the end of 225 // the initializer when we come to parse it. 226 Token Eof; 227 Eof.startToken(); 228 Eof.setKind(tok::eof); 229 Eof.setLocation(Tok.getLocation()); 230 Eof.setEofData(VarD); 231 Toks.push_back(Eof); 232 } 233 234 Parser::LateParsedDeclaration::~LateParsedDeclaration() {} 235 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} 236 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {} 237 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} 238 239 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) 240 : Self(P), Class(C) {} 241 242 Parser::LateParsedClass::~LateParsedClass() { 243 Self->DeallocateParsedClasses(Class); 244 } 245 246 void Parser::LateParsedClass::ParseLexedMethodDeclarations() { 247 Self->ParseLexedMethodDeclarations(*Class); 248 } 249 250 void Parser::LateParsedClass::ParseLexedMemberInitializers() { 251 Self->ParseLexedMemberInitializers(*Class); 252 } 253 254 void Parser::LateParsedClass::ParseLexedMethodDefs() { 255 Self->ParseLexedMethodDefs(*Class); 256 } 257 258 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { 259 Self->ParseLexedMethodDeclaration(*this); 260 } 261 262 void Parser::LexedMethod::ParseLexedMethodDefs() { 263 Self->ParseLexedMethodDef(*this); 264 } 265 266 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() { 267 Self->ParseLexedMemberInitializer(*this); 268 } 269 270 /// ParseLexedMethodDeclarations - We finished parsing the member 271 /// specification of a top (non-nested) C++ class. Now go over the 272 /// stack of method declarations with some parts for which parsing was 273 /// delayed (such as default arguments) and parse them. 274 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { 275 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 276 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, 277 HasTemplateScope); 278 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 279 if (HasTemplateScope) { 280 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 281 ++CurTemplateDepthTracker; 282 } 283 284 // The current scope is still active if we're the top-level class. 285 // Otherwise we'll need to push and enter a new scope. 286 bool HasClassScope = !Class.TopLevelClass; 287 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, 288 HasClassScope); 289 if (HasClassScope) 290 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), 291 Class.TagOrTemplate); 292 293 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 294 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations(); 295 } 296 297 if (HasClassScope) 298 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), 299 Class.TagOrTemplate); 300 } 301 302 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { 303 // If this is a member template, introduce the template parameter scope. 304 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); 305 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 306 if (LM.TemplateScope) { 307 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method); 308 ++CurTemplateDepthTracker; 309 } 310 // Start the delayed C++ method declaration 311 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 312 313 // Introduce the parameters into scope and parse their default 314 // arguments. 315 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | 316 Scope::FunctionDeclarationScope | Scope::DeclScope); 317 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { 318 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param); 319 // Introduce the parameter into scope. 320 bool HasUnparsed = Param->hasUnparsedDefaultArg(); 321 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param); 322 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks); 323 if (Toks) { 324 // Mark the end of the default argument so that we know when to stop when 325 // we parse it later on. 326 Token LastDefaultArgToken = Toks->back(); 327 Token DefArgEnd; 328 DefArgEnd.startToken(); 329 DefArgEnd.setKind(tok::eof); 330 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc()); 331 DefArgEnd.setEofData(Param); 332 Toks->push_back(DefArgEnd); 333 334 // Parse the default argument from its saved token stream. 335 Toks->push_back(Tok); // So that the current token doesn't get lost 336 PP.EnterTokenStream(*Toks, true); 337 338 // Consume the previously-pushed token. 339 ConsumeAnyToken(); 340 341 // Consume the '='. 342 assert(Tok.is(tok::equal) && "Default argument not starting with '='"); 343 SourceLocation EqualLoc = ConsumeToken(); 344 345 // The argument isn't actually potentially evaluated unless it is 346 // used. 347 EnterExpressionEvaluationContext Eval( 348 Actions, 349 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param); 350 351 ExprResult DefArgResult; 352 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 353 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 354 DefArgResult = ParseBraceInitializer(); 355 } else 356 DefArgResult = ParseAssignmentExpression(); 357 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult); 358 if (DefArgResult.isInvalid()) { 359 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); 360 } else { 361 if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) { 362 // The last two tokens are the terminator and the saved value of 363 // Tok; the last token in the default argument is the one before 364 // those. 365 assert(Toks->size() >= 3 && "expected a token in default arg"); 366 Diag(Tok.getLocation(), diag::err_default_arg_unparsed) 367 << SourceRange(Tok.getLocation(), 368 (*Toks)[Toks->size() - 3].getLocation()); 369 } 370 Actions.ActOnParamDefaultArgument(Param, EqualLoc, 371 DefArgResult.get()); 372 } 373 374 // There could be leftover tokens (e.g. because of an error). 375 // Skip through until we reach the 'end of default argument' token. 376 while (Tok.isNot(tok::eof)) 377 ConsumeAnyToken(); 378 379 if (Tok.is(tok::eof) && Tok.getEofData() == Param) 380 ConsumeAnyToken(); 381 } else if (HasUnparsed) { 382 assert(Param->hasInheritedDefaultArg()); 383 FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl(); 384 ParmVarDecl *OldParam = Old->getParamDecl(I); 385 assert (!OldParam->hasUnparsedDefaultArg()); 386 if (OldParam->hasUninstantiatedDefaultArg()) 387 Param->setUninstantiatedDefaultArg( 388 OldParam->getUninstantiatedDefaultArg()); 389 else 390 Param->setDefaultArg(OldParam->getInit()); 391 } 392 } 393 394 // Parse a delayed exception-specification, if there is one. 395 if (CachedTokens *Toks = LM.ExceptionSpecTokens) { 396 // Add the 'stop' token. 397 Token LastExceptionSpecToken = Toks->back(); 398 Token ExceptionSpecEnd; 399 ExceptionSpecEnd.startToken(); 400 ExceptionSpecEnd.setKind(tok::eof); 401 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc()); 402 ExceptionSpecEnd.setEofData(LM.Method); 403 Toks->push_back(ExceptionSpecEnd); 404 405 // Parse the default argument from its saved token stream. 406 Toks->push_back(Tok); // So that the current token doesn't get lost 407 PP.EnterTokenStream(*Toks, true); 408 409 // Consume the previously-pushed token. 410 ConsumeAnyToken(); 411 412 // C++11 [expr.prim.general]p3: 413 // If a declaration declares a member function or member function 414 // template of a class X, the expression this is a prvalue of type 415 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq 416 // and the end of the function-definition, member-declarator, or 417 // declarator. 418 CXXMethodDecl *Method; 419 if (FunctionTemplateDecl *FunTmpl 420 = dyn_cast<FunctionTemplateDecl>(LM.Method)) 421 Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); 422 else 423 Method = cast<CXXMethodDecl>(LM.Method); 424 425 Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(), 426 Method->getTypeQualifiers(), 427 getLangOpts().CPlusPlus11); 428 429 // Parse the exception-specification. 430 SourceRange SpecificationRange; 431 SmallVector<ParsedType, 4> DynamicExceptions; 432 SmallVector<SourceRange, 4> DynamicExceptionRanges; 433 ExprResult NoexceptExpr; 434 CachedTokens *ExceptionSpecTokens; 435 436 ExceptionSpecificationType EST 437 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange, 438 DynamicExceptions, 439 DynamicExceptionRanges, NoexceptExpr, 440 ExceptionSpecTokens); 441 442 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method) 443 Diag(Tok.getLocation(), diag::err_except_spec_unparsed); 444 445 // Attach the exception-specification to the method. 446 Actions.actOnDelayedExceptionSpecification(LM.Method, EST, 447 SpecificationRange, 448 DynamicExceptions, 449 DynamicExceptionRanges, 450 NoexceptExpr.isUsable()? 451 NoexceptExpr.get() : nullptr); 452 453 // There could be leftover tokens (e.g. because of an error). 454 // Skip through until we reach the original token position. 455 while (Tok.isNot(tok::eof)) 456 ConsumeAnyToken(); 457 458 // Clean up the remaining EOF token. 459 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method) 460 ConsumeAnyToken(); 461 462 delete Toks; 463 LM.ExceptionSpecTokens = nullptr; 464 } 465 466 PrototypeScope.Exit(); 467 468 // Finish the delayed C++ method declaration. 469 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 470 } 471 472 /// ParseLexedMethodDefs - We finished parsing the member specification of a top 473 /// (non-nested) C++ class. Now go over the stack of lexed methods that were 474 /// collected during its parsing and parse them all. 475 void Parser::ParseLexedMethodDefs(ParsingClass &Class) { 476 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 477 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); 478 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 479 if (HasTemplateScope) { 480 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 481 ++CurTemplateDepthTracker; 482 } 483 bool HasClassScope = !Class.TopLevelClass; 484 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, 485 HasClassScope); 486 487 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 488 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs(); 489 } 490 } 491 492 void Parser::ParseLexedMethodDef(LexedMethod &LM) { 493 // If this is a member template, introduce the template parameter scope. 494 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); 495 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 496 if (LM.TemplateScope) { 497 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D); 498 ++CurTemplateDepthTracker; 499 } 500 501 assert(!LM.Toks.empty() && "Empty body!"); 502 Token LastBodyToken = LM.Toks.back(); 503 Token BodyEnd; 504 BodyEnd.startToken(); 505 BodyEnd.setKind(tok::eof); 506 BodyEnd.setLocation(LastBodyToken.getEndLoc()); 507 BodyEnd.setEofData(LM.D); 508 LM.Toks.push_back(BodyEnd); 509 // Append the current token at the end of the new token stream so that it 510 // doesn't get lost. 511 LM.Toks.push_back(Tok); 512 PP.EnterTokenStream(LM.Toks, true); 513 514 // Consume the previously pushed token. 515 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 516 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) 517 && "Inline method not starting with '{', ':' or 'try'"); 518 519 // Parse the method body. Function body parsing code is similar enough 520 // to be re-used for method bodies as well. 521 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope | 522 Scope::CompoundStmtScope); 523 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); 524 525 if (Tok.is(tok::kw_try)) { 526 ParseFunctionTryBlock(LM.D, FnScope); 527 528 while (Tok.isNot(tok::eof)) 529 ConsumeAnyToken(); 530 531 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D) 532 ConsumeAnyToken(); 533 return; 534 } 535 if (Tok.is(tok::colon)) { 536 ParseConstructorInitializer(LM.D); 537 538 // Error recovery. 539 if (!Tok.is(tok::l_brace)) { 540 FnScope.Exit(); 541 Actions.ActOnFinishFunctionBody(LM.D, nullptr); 542 543 while (Tok.isNot(tok::eof)) 544 ConsumeAnyToken(); 545 546 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D) 547 ConsumeAnyToken(); 548 return; 549 } 550 } else 551 Actions.ActOnDefaultCtorInitializers(LM.D); 552 553 assert((Actions.getDiagnostics().hasErrorOccurred() || 554 !isa<FunctionTemplateDecl>(LM.D) || 555 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth() 556 < TemplateParameterDepth) && 557 "TemplateParameterDepth should be greater than the depth of " 558 "current template being instantiated!"); 559 560 ParseFunctionStatementBody(LM.D, FnScope); 561 562 // Clear the late-template-parsed bit if we set it before. 563 if (LM.D) 564 LM.D->getAsFunction()->setLateTemplateParsed(false); 565 566 while (Tok.isNot(tok::eof)) 567 ConsumeAnyToken(); 568 569 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D) 570 ConsumeAnyToken(); 571 572 if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D)) 573 if (isa<CXXMethodDecl>(FD) || 574 FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend)) 575 Actions.ActOnFinishInlineFunctionDef(FD); 576 } 577 578 /// ParseLexedMemberInitializers - We finished parsing the member specification 579 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member 580 /// initializers that were collected during its parsing and parse them all. 581 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) { 582 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 583 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, 584 HasTemplateScope); 585 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 586 if (HasTemplateScope) { 587 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 588 ++CurTemplateDepthTracker; 589 } 590 // Set or update the scope flags. 591 bool AlreadyHasClassScope = Class.TopLevelClass; 592 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; 593 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); 594 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); 595 596 if (!AlreadyHasClassScope) 597 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), 598 Class.TagOrTemplate); 599 600 if (!Class.LateParsedDeclarations.empty()) { 601 // C++11 [expr.prim.general]p4: 602 // Otherwise, if a member-declarator declares a non-static data member 603 // (9.2) of a class X, the expression this is a prvalue of type "pointer 604 // to X" within the optional brace-or-equal-initializer. It shall not 605 // appear elsewhere in the member-declarator. 606 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, 607 /*TypeQuals=*/(unsigned)0); 608 609 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 610 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers(); 611 } 612 } 613 614 if (!AlreadyHasClassScope) 615 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), 616 Class.TagOrTemplate); 617 618 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate); 619 } 620 621 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { 622 if (!MI.Field || MI.Field->isInvalidDecl()) 623 return; 624 625 // Append the current token at the end of the new token stream so that it 626 // doesn't get lost. 627 MI.Toks.push_back(Tok); 628 PP.EnterTokenStream(MI.Toks, true); 629 630 // Consume the previously pushed token. 631 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 632 633 SourceLocation EqualLoc; 634 635 Actions.ActOnStartCXXInClassMemberInitializer(); 636 637 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false, 638 EqualLoc); 639 640 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc, 641 Init.get()); 642 643 // The next token should be our artificial terminating EOF token. 644 if (Tok.isNot(tok::eof)) { 645 if (!Init.isInvalid()) { 646 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 647 if (!EndLoc.isValid()) 648 EndLoc = Tok.getLocation(); 649 // No fixit; we can't recover as if there were a semicolon here. 650 Diag(EndLoc, diag::err_expected_semi_decl_list); 651 } 652 653 // Consume tokens until we hit the artificial EOF. 654 while (Tok.isNot(tok::eof)) 655 ConsumeAnyToken(); 656 } 657 // Make sure this is *our* artificial EOF token. 658 if (Tok.getEofData() == MI.Field) 659 ConsumeAnyToken(); 660 } 661 662 /// ConsumeAndStoreUntil - Consume and store the token at the passed token 663 /// container until the token 'T' is reached (which gets 664 /// consumed/stored too, if ConsumeFinalToken). 665 /// If StopAtSemi is true, then we will stop early at a ';' character. 666 /// Returns true if token 'T1' or 'T2' was found. 667 /// NOTE: This is a specialized version of Parser::SkipUntil. 668 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, 669 CachedTokens &Toks, 670 bool StopAtSemi, bool ConsumeFinalToken) { 671 // We always want this function to consume at least one token if the first 672 // token isn't T and if not at EOF. 673 bool isFirstTokenConsumed = true; 674 while (1) { 675 // If we found one of the tokens, stop and return true. 676 if (Tok.is(T1) || Tok.is(T2)) { 677 if (ConsumeFinalToken) { 678 Toks.push_back(Tok); 679 ConsumeAnyToken(); 680 } 681 return true; 682 } 683 684 switch (Tok.getKind()) { 685 case tok::eof: 686 case tok::annot_module_begin: 687 case tok::annot_module_end: 688 case tok::annot_module_include: 689 // Ran out of tokens. 690 return false; 691 692 case tok::l_paren: 693 // Recursively consume properly-nested parens. 694 Toks.push_back(Tok); 695 ConsumeParen(); 696 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); 697 break; 698 case tok::l_square: 699 // Recursively consume properly-nested square brackets. 700 Toks.push_back(Tok); 701 ConsumeBracket(); 702 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); 703 break; 704 case tok::l_brace: 705 // Recursively consume properly-nested braces. 706 Toks.push_back(Tok); 707 ConsumeBrace(); 708 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 709 break; 710 711 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 712 // Since the user wasn't looking for this token (if they were, it would 713 // already be handled), this isn't balanced. If there is a LHS token at a 714 // higher level, we will assume that this matches the unbalanced token 715 // and return it. Otherwise, this is a spurious RHS token, which we skip. 716 case tok::r_paren: 717 if (ParenCount && !isFirstTokenConsumed) 718 return false; // Matches something. 719 Toks.push_back(Tok); 720 ConsumeParen(); 721 break; 722 case tok::r_square: 723 if (BracketCount && !isFirstTokenConsumed) 724 return false; // Matches something. 725 Toks.push_back(Tok); 726 ConsumeBracket(); 727 break; 728 case tok::r_brace: 729 if (BraceCount && !isFirstTokenConsumed) 730 return false; // Matches something. 731 Toks.push_back(Tok); 732 ConsumeBrace(); 733 break; 734 735 case tok::semi: 736 if (StopAtSemi) 737 return false; 738 // FALL THROUGH. 739 default: 740 // consume this token. 741 Toks.push_back(Tok); 742 ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true); 743 break; 744 } 745 isFirstTokenConsumed = false; 746 } 747 } 748 749 /// \brief Consume tokens and store them in the passed token container until 750 /// we've passed the try keyword and constructor initializers and have consumed 751 /// the opening brace of the function body. The opening brace will be consumed 752 /// if and only if there was no error. 753 /// 754 /// \return True on error. 755 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) { 756 if (Tok.is(tok::kw_try)) { 757 Toks.push_back(Tok); 758 ConsumeToken(); 759 } 760 761 if (Tok.isNot(tok::colon)) { 762 // Easy case, just a function body. 763 764 // Grab any remaining garbage to be diagnosed later. We stop when we reach a 765 // brace: an opening one is the function body, while a closing one probably 766 // means we've reached the end of the class. 767 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks, 768 /*StopAtSemi=*/true, 769 /*ConsumeFinalToken=*/false); 770 if (Tok.isNot(tok::l_brace)) 771 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace; 772 773 Toks.push_back(Tok); 774 ConsumeBrace(); 775 return false; 776 } 777 778 Toks.push_back(Tok); 779 ConsumeToken(); 780 781 // We can't reliably skip over a mem-initializer-id, because it could be 782 // a template-id involving not-yet-declared names. Given: 783 // 784 // S ( ) : a < b < c > ( e ) 785 // 786 // 'e' might be an initializer or part of a template argument, depending 787 // on whether 'b' is a template. 788 789 // Track whether we might be inside a template argument. We can give 790 // significantly better diagnostics if we know that we're not. 791 bool MightBeTemplateArgument = false; 792 793 while (true) { 794 // Skip over the mem-initializer-id, if possible. 795 if (Tok.is(tok::kw_decltype)) { 796 Toks.push_back(Tok); 797 SourceLocation OpenLoc = ConsumeToken(); 798 if (Tok.isNot(tok::l_paren)) 799 return Diag(Tok.getLocation(), diag::err_expected_lparen_after) 800 << "decltype"; 801 Toks.push_back(Tok); 802 ConsumeParen(); 803 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) { 804 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; 805 Diag(OpenLoc, diag::note_matching) << tok::l_paren; 806 return true; 807 } 808 } 809 do { 810 // Walk over a component of a nested-name-specifier. 811 if (Tok.is(tok::coloncolon)) { 812 Toks.push_back(Tok); 813 ConsumeToken(); 814 815 if (Tok.is(tok::kw_template)) { 816 Toks.push_back(Tok); 817 ConsumeToken(); 818 } 819 } 820 821 if (Tok.is(tok::identifier)) { 822 Toks.push_back(Tok); 823 ConsumeToken(); 824 } else { 825 break; 826 } 827 } while (Tok.is(tok::coloncolon)); 828 829 if (Tok.is(tok::code_completion)) { 830 Toks.push_back(Tok); 831 ConsumeCodeCompletionToken(); 832 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) { 833 // Could be the start of another member initializer (the ',' has not 834 // been written yet) 835 continue; 836 } 837 } 838 839 if (Tok.is(tok::comma)) { 840 // The initialization is missing, we'll diagnose it later. 841 Toks.push_back(Tok); 842 ConsumeToken(); 843 continue; 844 } 845 if (Tok.is(tok::less)) 846 MightBeTemplateArgument = true; 847 848 if (MightBeTemplateArgument) { 849 // We may be inside a template argument list. Grab up to the start of the 850 // next parenthesized initializer or braced-init-list. This *might* be the 851 // initializer, or it might be a subexpression in the template argument 852 // list. 853 // FIXME: Count angle brackets, and clear MightBeTemplateArgument 854 // if all angles are closed. 855 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks, 856 /*StopAtSemi=*/true, 857 /*ConsumeFinalToken=*/false)) { 858 // We're not just missing the initializer, we're also missing the 859 // function body! 860 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace; 861 } 862 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) { 863 // We found something weird in a mem-initializer-id. 864 if (getLangOpts().CPlusPlus11) 865 return Diag(Tok.getLocation(), diag::err_expected_either) 866 << tok::l_paren << tok::l_brace; 867 else 868 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren; 869 } 870 871 tok::TokenKind kind = Tok.getKind(); 872 Toks.push_back(Tok); 873 bool IsLParen = (kind == tok::l_paren); 874 SourceLocation OpenLoc = Tok.getLocation(); 875 876 if (IsLParen) { 877 ConsumeParen(); 878 } else { 879 assert(kind == tok::l_brace && "Must be left paren or brace here."); 880 ConsumeBrace(); 881 // In C++03, this has to be the start of the function body, which 882 // means the initializer is malformed; we'll diagnose it later. 883 if (!getLangOpts().CPlusPlus11) 884 return false; 885 886 const Token &PreviousToken = Toks[Toks.size() - 2]; 887 if (!MightBeTemplateArgument && 888 !PreviousToken.isOneOf(tok::identifier, tok::greater, 889 tok::greatergreater)) { 890 // If the opening brace is not preceded by one of these tokens, we are 891 // missing the mem-initializer-id. In order to recover better, we need 892 // to use heuristics to determine if this '{' is most likely the 893 // beginning of a brace-init-list or the function body. 894 // Check the token after the corresponding '}'. 895 TentativeParsingAction PA(*this); 896 if (SkipUntil(tok::r_brace) && 897 !Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) { 898 // Consider there was a malformed initializer and this is the start 899 // of the function body. We'll diagnose it later. 900 PA.Revert(); 901 return false; 902 } 903 PA.Revert(); 904 } 905 } 906 907 // Grab the initializer (or the subexpression of the template argument). 908 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false 909 // if we might be inside the braces of a lambda-expression. 910 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace; 911 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) { 912 Diag(Tok, diag::err_expected) << CloseKind; 913 Diag(OpenLoc, diag::note_matching) << kind; 914 return true; 915 } 916 917 // Grab pack ellipsis, if present. 918 if (Tok.is(tok::ellipsis)) { 919 Toks.push_back(Tok); 920 ConsumeToken(); 921 } 922 923 // If we know we just consumed a mem-initializer, we must have ',' or '{' 924 // next. 925 if (Tok.is(tok::comma)) { 926 Toks.push_back(Tok); 927 ConsumeToken(); 928 } else if (Tok.is(tok::l_brace)) { 929 // This is the function body if the ')' or '}' is immediately followed by 930 // a '{'. That cannot happen within a template argument, apart from the 931 // case where a template argument contains a compound literal: 932 // 933 // S ( ) : a < b < c > ( d ) { } 934 // // End of declaration, or still inside the template argument? 935 // 936 // ... and the case where the template argument contains a lambda: 937 // 938 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; } 939 // ( ) > ( ) { } 940 // 941 // FIXME: Disambiguate these cases. Note that the latter case is probably 942 // going to be made ill-formed by core issue 1607. 943 Toks.push_back(Tok); 944 ConsumeBrace(); 945 return false; 946 } else if (!MightBeTemplateArgument) { 947 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace 948 << tok::comma; 949 } 950 } 951 } 952 953 /// \brief Consume and store tokens from the '?' to the ':' in a conditional 954 /// expression. 955 bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) { 956 // Consume '?'. 957 assert(Tok.is(tok::question)); 958 Toks.push_back(Tok); 959 ConsumeToken(); 960 961 while (Tok.isNot(tok::colon)) { 962 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks, 963 /*StopAtSemi=*/true, 964 /*ConsumeFinalToken=*/false)) 965 return false; 966 967 // If we found a nested conditional, consume it. 968 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks)) 969 return false; 970 } 971 972 // Consume ':'. 973 Toks.push_back(Tok); 974 ConsumeToken(); 975 return true; 976 } 977 978 /// \brief A tentative parsing action that can also revert token annotations. 979 class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction { 980 public: 981 explicit UnannotatedTentativeParsingAction(Parser &Self, 982 tok::TokenKind EndKind) 983 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) { 984 // Stash away the old token stream, so we can restore it once the 985 // tentative parse is complete. 986 TentativeParsingAction Inner(Self); 987 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false); 988 Inner.Revert(); 989 } 990 991 void RevertAnnotations() { 992 Revert(); 993 994 // Put back the original tokens. 995 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch); 996 if (Toks.size()) { 997 auto Buffer = llvm::make_unique<Token[]>(Toks.size()); 998 std::copy(Toks.begin() + 1, Toks.end(), Buffer.get()); 999 Buffer[Toks.size() - 1] = Self.Tok; 1000 Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true); 1001 1002 Self.Tok = Toks.front(); 1003 } 1004 } 1005 1006 private: 1007 Parser &Self; 1008 CachedTokens Toks; 1009 tok::TokenKind EndKind; 1010 }; 1011 1012 /// ConsumeAndStoreInitializer - Consume and store the token at the passed token 1013 /// container until the end of the current initializer expression (either a 1014 /// default argument or an in-class initializer for a non-static data member). 1015 /// 1016 /// Returns \c true if we reached the end of something initializer-shaped, 1017 /// \c false if we bailed out. 1018 bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks, 1019 CachedInitKind CIK) { 1020 // We always want this function to consume at least one token if not at EOF. 1021 bool IsFirstToken = true; 1022 1023 // Number of possible unclosed <s we've seen so far. These might be templates, 1024 // and might not, but if there were none of them (or we know for sure that 1025 // we're within a template), we can avoid a tentative parse. 1026 unsigned AngleCount = 0; 1027 unsigned KnownTemplateCount = 0; 1028 1029 while (1) { 1030 switch (Tok.getKind()) { 1031 case tok::comma: 1032 // If we might be in a template, perform a tentative parse to check. 1033 if (!AngleCount) 1034 // Not a template argument: this is the end of the initializer. 1035 return true; 1036 if (KnownTemplateCount) 1037 goto consume_token; 1038 1039 // We hit a comma inside angle brackets. This is the hard case. The 1040 // rule we follow is: 1041 // * For a default argument, if the tokens after the comma form a 1042 // syntactically-valid parameter-declaration-clause, in which each 1043 // parameter has an initializer, then this comma ends the default 1044 // argument. 1045 // * For a default initializer, if the tokens after the comma form a 1046 // syntactically-valid init-declarator-list, then this comma ends 1047 // the default initializer. 1048 { 1049 UnannotatedTentativeParsingAction PA(*this, 1050 CIK == CIK_DefaultInitializer 1051 ? tok::semi : tok::r_paren); 1052 Sema::TentativeAnalysisScope Scope(Actions); 1053 1054 TPResult Result = TPResult::Error; 1055 ConsumeToken(); 1056 switch (CIK) { 1057 case CIK_DefaultInitializer: 1058 Result = TryParseInitDeclaratorList(); 1059 // If we parsed a complete, ambiguous init-declarator-list, this 1060 // is only syntactically-valid if it's followed by a semicolon. 1061 if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi)) 1062 Result = TPResult::False; 1063 break; 1064 1065 case CIK_DefaultArgument: 1066 bool InvalidAsDeclaration = false; 1067 Result = TryParseParameterDeclarationClause( 1068 &InvalidAsDeclaration, /*VersusTemplateArgument=*/true); 1069 // If this is an expression or a declaration with a missing 1070 // 'typename', assume it's not a declaration. 1071 if (Result == TPResult::Ambiguous && InvalidAsDeclaration) 1072 Result = TPResult::False; 1073 break; 1074 } 1075 1076 // If what follows could be a declaration, it is a declaration. 1077 if (Result != TPResult::False && Result != TPResult::Error) { 1078 PA.Revert(); 1079 return true; 1080 } 1081 1082 // In the uncommon case that we decide the following tokens are part 1083 // of a template argument, revert any annotations we've performed in 1084 // those tokens. We're not going to look them up until we've parsed 1085 // the rest of the class, and that might add more declarations. 1086 PA.RevertAnnotations(); 1087 } 1088 1089 // Keep going. We know we're inside a template argument list now. 1090 ++KnownTemplateCount; 1091 goto consume_token; 1092 1093 case tok::eof: 1094 case tok::annot_module_begin: 1095 case tok::annot_module_end: 1096 case tok::annot_module_include: 1097 // Ran out of tokens. 1098 return false; 1099 1100 case tok::less: 1101 // FIXME: A '<' can only start a template-id if it's preceded by an 1102 // identifier, an operator-function-id, or a literal-operator-id. 1103 ++AngleCount; 1104 goto consume_token; 1105 1106 case tok::question: 1107 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does, 1108 // that is *never* the end of the initializer. Skip to the ':'. 1109 if (!ConsumeAndStoreConditional(Toks)) 1110 return false; 1111 break; 1112 1113 case tok::greatergreatergreater: 1114 if (!getLangOpts().CPlusPlus11) 1115 goto consume_token; 1116 if (AngleCount) --AngleCount; 1117 if (KnownTemplateCount) --KnownTemplateCount; 1118 // Fall through. 1119 case tok::greatergreater: 1120 if (!getLangOpts().CPlusPlus11) 1121 goto consume_token; 1122 if (AngleCount) --AngleCount; 1123 if (KnownTemplateCount) --KnownTemplateCount; 1124 // Fall through. 1125 case tok::greater: 1126 if (AngleCount) --AngleCount; 1127 if (KnownTemplateCount) --KnownTemplateCount; 1128 goto consume_token; 1129 1130 case tok::kw_template: 1131 // 'template' identifier '<' is known to start a template argument list, 1132 // and can be used to disambiguate the parse. 1133 // FIXME: Support all forms of 'template' unqualified-id '<'. 1134 Toks.push_back(Tok); 1135 ConsumeToken(); 1136 if (Tok.is(tok::identifier)) { 1137 Toks.push_back(Tok); 1138 ConsumeToken(); 1139 if (Tok.is(tok::less)) { 1140 ++AngleCount; 1141 ++KnownTemplateCount; 1142 Toks.push_back(Tok); 1143 ConsumeToken(); 1144 } 1145 } 1146 break; 1147 1148 case tok::kw_operator: 1149 // If 'operator' precedes other punctuation, that punctuation loses 1150 // its special behavior. 1151 Toks.push_back(Tok); 1152 ConsumeToken(); 1153 switch (Tok.getKind()) { 1154 case tok::comma: 1155 case tok::greatergreatergreater: 1156 case tok::greatergreater: 1157 case tok::greater: 1158 case tok::less: 1159 Toks.push_back(Tok); 1160 ConsumeToken(); 1161 break; 1162 default: 1163 break; 1164 } 1165 break; 1166 1167 case tok::l_paren: 1168 // Recursively consume properly-nested parens. 1169 Toks.push_back(Tok); 1170 ConsumeParen(); 1171 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); 1172 break; 1173 case tok::l_square: 1174 // Recursively consume properly-nested square brackets. 1175 Toks.push_back(Tok); 1176 ConsumeBracket(); 1177 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); 1178 break; 1179 case tok::l_brace: 1180 // Recursively consume properly-nested braces. 1181 Toks.push_back(Tok); 1182 ConsumeBrace(); 1183 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1184 break; 1185 1186 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 1187 // Since the user wasn't looking for this token (if they were, it would 1188 // already be handled), this isn't balanced. If there is a LHS token at a 1189 // higher level, we will assume that this matches the unbalanced token 1190 // and return it. Otherwise, this is a spurious RHS token, which we 1191 // consume and pass on to downstream code to diagnose. 1192 case tok::r_paren: 1193 if (CIK == CIK_DefaultArgument) 1194 return true; // End of the default argument. 1195 if (ParenCount && !IsFirstToken) 1196 return false; 1197 Toks.push_back(Tok); 1198 ConsumeParen(); 1199 continue; 1200 case tok::r_square: 1201 if (BracketCount && !IsFirstToken) 1202 return false; 1203 Toks.push_back(Tok); 1204 ConsumeBracket(); 1205 continue; 1206 case tok::r_brace: 1207 if (BraceCount && !IsFirstToken) 1208 return false; 1209 Toks.push_back(Tok); 1210 ConsumeBrace(); 1211 continue; 1212 1213 case tok::code_completion: 1214 Toks.push_back(Tok); 1215 ConsumeCodeCompletionToken(); 1216 break; 1217 1218 case tok::string_literal: 1219 case tok::wide_string_literal: 1220 case tok::utf8_string_literal: 1221 case tok::utf16_string_literal: 1222 case tok::utf32_string_literal: 1223 Toks.push_back(Tok); 1224 ConsumeStringToken(); 1225 break; 1226 case tok::semi: 1227 if (CIK == CIK_DefaultInitializer) 1228 return true; // End of the default initializer. 1229 // FALL THROUGH. 1230 default: 1231 consume_token: 1232 Toks.push_back(Tok); 1233 ConsumeToken(); 1234 break; 1235 } 1236 IsFirstToken = false; 1237 } 1238 } 1239