ParseCXXInlineMethods.cpp revision ccc1b5eebc6ca8a904c58c0468b9a71483b7c7cf
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/ParseDiagnostic.h" 15#include "clang/Parse/Parser.h" 16#include "clang/Sema/DeclSpec.h" 17#include "clang/Sema/Scope.h" 18#include "clang/AST/DeclTemplate.h" 19using namespace clang; 20 21/// ParseCXXInlineMethodDef - We parsed and verified that the specified 22/// Declarator is a well formed C++ inline method definition. Now lex its body 23/// and store its tokens for parsing after the C++ class is complete. 24Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, 25 AttributeList *AccessAttrs, 26 ParsingDeclarator &D, 27 const ParsedTemplateInfo &TemplateInfo, 28 const VirtSpecifiers& VS, 29 FunctionDefinitionKind DefinitionKind, 30 ExprResult& Init) { 31 assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); 32 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) || 33 Tok.is(tok::equal)) && 34 "Current token not a '{', ':', '=', or 'try'!"); 35 36 MultiTemplateParamsArg TemplateParams(Actions, 37 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0, 38 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); 39 40 Decl *FnD; 41 D.setFunctionDefinitionKind(DefinitionKind); 42 if (D.getDeclSpec().isFriendSpecified()) 43 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, 44 move(TemplateParams)); 45 else { 46 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, 47 move(TemplateParams), 0, 48 VS, /*HasDeferredInit=*/false); 49 if (FnD) { 50 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs, 51 false, true); 52 bool TypeSpecContainsAuto 53 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; 54 if (Init.isUsable()) 55 Actions.AddInitializerToDecl(FnD, Init.get(), false, 56 TypeSpecContainsAuto); 57 else 58 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto); 59 } 60 } 61 62 HandleMemberFunctionDefaultArgs(D, FnD); 63 64 D.complete(FnD); 65 66 if (Tok.is(tok::equal)) { 67 ConsumeToken(); 68 69 if (!FnD) { 70 SkipUntil(tok::semi); 71 return 0; 72 } 73 74 bool Delete = false; 75 SourceLocation KWLoc; 76 if (Tok.is(tok::kw_delete)) { 77 Diag(Tok, getLang().CPlusPlus0x ? 78 diag::warn_cxx98_compat_deleted_function : 79 diag::ext_deleted_function); 80 81 KWLoc = ConsumeToken(); 82 Actions.SetDeclDeleted(FnD, KWLoc); 83 Delete = true; 84 } else if (Tok.is(tok::kw_default)) { 85 Diag(Tok, getLang().CPlusPlus0x ? 86 diag::warn_cxx98_compat_defaulted_function : 87 diag::ext_defaulted_function); 88 89 KWLoc = ConsumeToken(); 90 Actions.SetDeclDefaulted(FnD, KWLoc); 91 } else { 92 llvm_unreachable("function definition after = not 'delete' or 'default'"); 93 } 94 95 if (Tok.is(tok::comma)) { 96 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 97 << Delete; 98 SkipUntil(tok::semi); 99 } else { 100 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 101 Delete ? "delete" : "default", tok::semi); 102 } 103 104 return FnD; 105 } 106 107 // In delayed template parsing mode, if we are within a class template 108 // or if we are about to parse function member template then consume 109 // the tokens and store them for parsing at the end of the translation unit. 110 if (getLang().DelayedTemplateParsing && 111 ((Actions.CurContext->isDependentContext() || 112 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) && 113 !Actions.IsInsideALocalClassWithinATemplateFunction())) { 114 115 if (FnD) { 116 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD); 117 118 FunctionDecl *FD = 0; 119 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD)) 120 FD = FunTmpl->getTemplatedDecl(); 121 else 122 FD = cast<FunctionDecl>(FnD); 123 Actions.CheckForFunctionRedefinition(FD); 124 125 LateParsedTemplateMap[FD] = LPT; 126 Actions.MarkAsLateParsedTemplate(FD); 127 LexTemplateFunctionForLateParsing(LPT->Toks); 128 } else { 129 CachedTokens Toks; 130 LexTemplateFunctionForLateParsing(Toks); 131 } 132 133 return FnD; 134 } 135 136 // Consume the tokens and store them for later parsing. 137 138 LexedMethod* LM = new LexedMethod(this, FnD); 139 getCurrentClass().LateParsedDeclarations.push_back(LM); 140 LM->TemplateScope = getCurScope()->isTemplateParamScope(); 141 CachedTokens &Toks = LM->Toks; 142 143 tok::TokenKind kind = Tok.getKind(); 144 // Consume everything up to (and including) the left brace of the 145 // function body. 146 if (ConsumeAndStoreFunctionPrologue(Toks)) { 147 // We didn't find the left-brace we expected after the 148 // constructor initializer. 149 if (Tok.is(tok::semi)) { 150 // We found a semicolon; complain, consume the semicolon, and 151 // don't try to parse this method later. 152 Diag(Tok.getLocation(), diag::err_expected_lbrace); 153 ConsumeAnyToken(); 154 delete getCurrentClass().LateParsedDeclarations.back(); 155 getCurrentClass().LateParsedDeclarations.pop_back(); 156 return FnD; 157 } 158 } else { 159 // Consume everything up to (and including) the matching right brace. 160 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 161 } 162 163 // If we're in a function-try-block, we need to store all the catch blocks. 164 if (kind == tok::kw_try) { 165 while (Tok.is(tok::kw_catch)) { 166 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); 167 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 168 } 169 } 170 171 172 if (!FnD) { 173 // If semantic analysis could not build a function declaration, 174 // just throw away the late-parsed declaration. 175 delete getCurrentClass().LateParsedDeclarations.back(); 176 getCurrentClass().LateParsedDeclarations.pop_back(); 177 } 178 179 return FnD; 180} 181 182/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the 183/// specified Declarator is a well formed C++ non-static data member 184/// declaration. Now lex its initializer and store its tokens for parsing 185/// after the class is complete. 186void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) { 187 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) && 188 "Current token not a '{' or '='!"); 189 190 LateParsedMemberInitializer *MI = 191 new LateParsedMemberInitializer(this, VarD); 192 getCurrentClass().LateParsedDeclarations.push_back(MI); 193 CachedTokens &Toks = MI->Toks; 194 195 tok::TokenKind kind = Tok.getKind(); 196 if (kind == tok::equal) { 197 Toks.push_back(Tok); 198 ConsumeAnyToken(); 199 } 200 201 if (kind == tok::l_brace) { 202 // Begin by storing the '{' token. 203 Toks.push_back(Tok); 204 ConsumeBrace(); 205 206 // Consume everything up to (and including) the matching right brace. 207 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true); 208 } else { 209 // Consume everything up to (but excluding) the comma or semicolon. 210 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true, 211 /*ConsumeFinalToken=*/false); 212 } 213 214 // Store an artificial EOF token to ensure that we don't run off the end of 215 // the initializer when we come to parse it. 216 Token Eof; 217 Eof.startToken(); 218 Eof.setKind(tok::eof); 219 Eof.setLocation(Tok.getLocation()); 220 Toks.push_back(Eof); 221} 222 223Parser::LateParsedDeclaration::~LateParsedDeclaration() {} 224void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} 225void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {} 226void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} 227 228Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) 229 : Self(P), Class(C) {} 230 231Parser::LateParsedClass::~LateParsedClass() { 232 Self->DeallocateParsedClasses(Class); 233} 234 235void Parser::LateParsedClass::ParseLexedMethodDeclarations() { 236 Self->ParseLexedMethodDeclarations(*Class); 237} 238 239void Parser::LateParsedClass::ParseLexedMemberInitializers() { 240 Self->ParseLexedMemberInitializers(*Class); 241} 242 243void Parser::LateParsedClass::ParseLexedMethodDefs() { 244 Self->ParseLexedMethodDefs(*Class); 245} 246 247void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { 248 Self->ParseLexedMethodDeclaration(*this); 249} 250 251void Parser::LexedMethod::ParseLexedMethodDefs() { 252 Self->ParseLexedMethodDef(*this); 253} 254 255void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() { 256 Self->ParseLexedMemberInitializer(*this); 257} 258 259/// ParseLexedMethodDeclarations - We finished parsing the member 260/// specification of a top (non-nested) C++ class. Now go over the 261/// stack of method declarations with some parts for which parsing was 262/// delayed (such as default arguments) and parse them. 263void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { 264 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 265 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); 266 if (HasTemplateScope) 267 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 268 269 // The current scope is still active if we're the top-level class. 270 // Otherwise we'll need to push and enter a new scope. 271 bool HasClassScope = !Class.TopLevelClass; 272 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, 273 HasClassScope); 274 if (HasClassScope) 275 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); 276 277 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 278 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations(); 279 } 280 281 if (HasClassScope) 282 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); 283} 284 285void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { 286 // If this is a member template, introduce the template parameter scope. 287 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); 288 if (LM.TemplateScope) 289 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method); 290 291 // Start the delayed C++ method declaration 292 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 293 294 // Introduce the parameters into scope and parse their default 295 // arguments. 296 ParseScope PrototypeScope(this, 297 Scope::FunctionPrototypeScope|Scope::DeclScope); 298 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { 299 // Introduce the parameter into scope. 300 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), 301 LM.DefaultArgs[I].Param); 302 303 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) { 304 // Save the current token position. 305 SourceLocation origLoc = Tok.getLocation(); 306 307 // Parse the default argument from its saved token stream. 308 Toks->push_back(Tok); // So that the current token doesn't get lost 309 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); 310 311 // Consume the previously-pushed token. 312 ConsumeAnyToken(); 313 314 // Consume the '='. 315 assert(Tok.is(tok::equal) && "Default argument not starting with '='"); 316 SourceLocation EqualLoc = ConsumeToken(); 317 318 // The argument isn't actually potentially evaluated unless it is 319 // used. 320 EnterExpressionEvaluationContext Eval(Actions, 321 Sema::PotentiallyEvaluatedIfUsed, 322 LM.DefaultArgs[I].Param); 323 324 ExprResult DefArgResult(ParseAssignmentExpression()); 325 if (DefArgResult.isInvalid()) 326 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param); 327 else { 328 if (Tok.is(tok::cxx_defaultarg_end)) 329 ConsumeToken(); 330 else 331 Diag(Tok.getLocation(), diag::err_default_arg_unparsed); 332 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc, 333 DefArgResult.take()); 334 } 335 336 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, 337 Tok.getLocation()) && 338 "ParseAssignmentExpression went over the default arg tokens!"); 339 // There could be leftover tokens (e.g. because of an error). 340 // Skip through until we reach the original token position. 341 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 342 ConsumeAnyToken(); 343 344 delete Toks; 345 LM.DefaultArgs[I].Toks = 0; 346 } 347 } 348 PrototypeScope.Exit(); 349 350 // Finish the delayed C++ method declaration. 351 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 352} 353 354/// ParseLexedMethodDefs - We finished parsing the member specification of a top 355/// (non-nested) C++ class. Now go over the stack of lexed methods that were 356/// collected during its parsing and parse them all. 357void Parser::ParseLexedMethodDefs(ParsingClass &Class) { 358 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 359 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); 360 if (HasTemplateScope) 361 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 362 363 bool HasClassScope = !Class.TopLevelClass; 364 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, 365 HasClassScope); 366 367 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 368 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs(); 369 } 370} 371 372void Parser::ParseLexedMethodDef(LexedMethod &LM) { 373 // If this is a member template, introduce the template parameter scope. 374 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); 375 if (LM.TemplateScope) 376 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D); 377 378 // Save the current token position. 379 SourceLocation origLoc = Tok.getLocation(); 380 381 assert(!LM.Toks.empty() && "Empty body!"); 382 // Append the current token at the end of the new token stream so that it 383 // doesn't get lost. 384 LM.Toks.push_back(Tok); 385 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); 386 387 // Consume the previously pushed token. 388 ConsumeAnyToken(); 389 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) 390 && "Inline method not starting with '{', ':' or 'try'"); 391 392 // Parse the method body. Function body parsing code is similar enough 393 // to be re-used for method bodies as well. 394 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); 395 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); 396 397 if (Tok.is(tok::kw_try)) { 398 ParseFunctionTryBlock(LM.D, FnScope); 399 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, 400 Tok.getLocation()) && 401 "ParseFunctionTryBlock went over the cached tokens!"); 402 // There could be leftover tokens (e.g. because of an error). 403 // Skip through until we reach the original token position. 404 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 405 ConsumeAnyToken(); 406 return; 407 } 408 if (Tok.is(tok::colon)) { 409 ParseConstructorInitializer(LM.D); 410 411 // Error recovery. 412 if (!Tok.is(tok::l_brace)) { 413 FnScope.Exit(); 414 Actions.ActOnFinishFunctionBody(LM.D, 0); 415 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 416 ConsumeAnyToken(); 417 return; 418 } 419 } else 420 Actions.ActOnDefaultCtorInitializers(LM.D); 421 422 ParseFunctionStatementBody(LM.D, FnScope); 423 424 if (Tok.getLocation() != origLoc) { 425 // Due to parsing error, we either went over the cached tokens or 426 // there are still cached tokens left. If it's the latter case skip the 427 // leftover tokens. 428 // Since this is an uncommon situation that should be avoided, use the 429 // expensive isBeforeInTranslationUnit call. 430 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), 431 origLoc)) 432 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 433 ConsumeAnyToken(); 434 } 435} 436 437/// ParseLexedMemberInitializers - We finished parsing the member specification 438/// of a top (non-nested) C++ class. Now go over the stack of lexed data member 439/// initializers that were collected during its parsing and parse them all. 440void Parser::ParseLexedMemberInitializers(ParsingClass &Class) { 441 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 442 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, 443 HasTemplateScope); 444 if (HasTemplateScope) 445 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 446 447 // Set or update the scope flags to include Scope::ThisScope. 448 bool AlreadyHasClassScope = Class.TopLevelClass; 449 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope; 450 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); 451 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); 452 453 if (!AlreadyHasClassScope) 454 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), 455 Class.TagOrTemplate); 456 457 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 458 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers(); 459 } 460 461 if (!AlreadyHasClassScope) 462 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), 463 Class.TagOrTemplate); 464 465 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate); 466} 467 468void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { 469 if (!MI.Field || MI.Field->isInvalidDecl()) 470 return; 471 472 // Append the current token at the end of the new token stream so that it 473 // doesn't get lost. 474 MI.Toks.push_back(Tok); 475 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false); 476 477 // Consume the previously pushed token. 478 ConsumeAnyToken(); 479 480 SourceLocation EqualLoc; 481 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc); 482 483 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release()); 484 485 // The next token should be our artificial terminating EOF token. 486 if (Tok.isNot(tok::eof)) { 487 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 488 if (!EndLoc.isValid()) 489 EndLoc = Tok.getLocation(); 490 // No fixit; we can't recover as if there were a semicolon here. 491 Diag(EndLoc, diag::err_expected_semi_decl_list); 492 493 // Consume tokens until we hit the artificial EOF. 494 while (Tok.isNot(tok::eof)) 495 ConsumeAnyToken(); 496 } 497 ConsumeAnyToken(); 498} 499 500/// ConsumeAndStoreUntil - Consume and store the token at the passed token 501/// container until the token 'T' is reached (which gets 502/// consumed/stored too, if ConsumeFinalToken). 503/// If StopAtSemi is true, then we will stop early at a ';' character. 504/// Returns true if token 'T1' or 'T2' was found. 505/// NOTE: This is a specialized version of Parser::SkipUntil. 506bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, 507 CachedTokens &Toks, 508 bool StopAtSemi, bool ConsumeFinalToken) { 509 // We always want this function to consume at least one token if the first 510 // token isn't T and if not at EOF. 511 bool isFirstTokenConsumed = true; 512 while (1) { 513 // If we found one of the tokens, stop and return true. 514 if (Tok.is(T1) || Tok.is(T2)) { 515 if (ConsumeFinalToken) { 516 Toks.push_back(Tok); 517 ConsumeAnyToken(); 518 } 519 return true; 520 } 521 522 switch (Tok.getKind()) { 523 case tok::eof: 524 // Ran out of tokens. 525 return false; 526 527 case tok::l_paren: 528 // Recursively consume properly-nested parens. 529 Toks.push_back(Tok); 530 ConsumeParen(); 531 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); 532 break; 533 case tok::l_square: 534 // Recursively consume properly-nested square brackets. 535 Toks.push_back(Tok); 536 ConsumeBracket(); 537 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); 538 break; 539 case tok::l_brace: 540 // Recursively consume properly-nested braces. 541 Toks.push_back(Tok); 542 ConsumeBrace(); 543 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 544 break; 545 546 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 547 // Since the user wasn't looking for this token (if they were, it would 548 // already be handled), this isn't balanced. If there is a LHS token at a 549 // higher level, we will assume that this matches the unbalanced token 550 // and return it. Otherwise, this is a spurious RHS token, which we skip. 551 case tok::r_paren: 552 if (ParenCount && !isFirstTokenConsumed) 553 return false; // Matches something. 554 Toks.push_back(Tok); 555 ConsumeParen(); 556 break; 557 case tok::r_square: 558 if (BracketCount && !isFirstTokenConsumed) 559 return false; // Matches something. 560 Toks.push_back(Tok); 561 ConsumeBracket(); 562 break; 563 case tok::r_brace: 564 if (BraceCount && !isFirstTokenConsumed) 565 return false; // Matches something. 566 Toks.push_back(Tok); 567 ConsumeBrace(); 568 break; 569 570 case tok::code_completion: 571 Toks.push_back(Tok); 572 ConsumeCodeCompletionToken(); 573 break; 574 575 case tok::string_literal: 576 case tok::wide_string_literal: 577 case tok::utf8_string_literal: 578 case tok::utf16_string_literal: 579 case tok::utf32_string_literal: 580 Toks.push_back(Tok); 581 ConsumeStringToken(); 582 break; 583 case tok::semi: 584 if (StopAtSemi) 585 return false; 586 // FALL THROUGH. 587 default: 588 // consume this token. 589 Toks.push_back(Tok); 590 ConsumeToken(); 591 break; 592 } 593 isFirstTokenConsumed = false; 594 } 595} 596 597/// \brief Consume tokens and store them in the passed token container until 598/// we've passed the try keyword and constructor initializers and have consumed 599/// the opening brace of the function body. The opening brace will be consumed 600/// if and only if there was no error. 601/// 602/// \return True on error. 603bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) { 604 if (Tok.is(tok::kw_try)) { 605 Toks.push_back(Tok); 606 ConsumeToken(); 607 } 608 if (Tok.is(tok::colon)) { 609 // Initializers can contain braces too. 610 Toks.push_back(Tok); 611 ConsumeToken(); 612 613 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { 614 if (Tok.is(tok::eof) || Tok.is(tok::semi)) 615 return true; 616 617 // Grab the identifier. 618 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks, 619 /*StopAtSemi=*/true, 620 /*ConsumeFinalToken=*/false)) 621 return true; 622 623 tok::TokenKind kind = Tok.getKind(); 624 Toks.push_back(Tok); 625 if (kind == tok::l_paren) 626 ConsumeParen(); 627 else { 628 assert(kind == tok::l_brace && "Must be left paren or brace here."); 629 ConsumeBrace(); 630 // In C++03, this has to be the start of the function body, which 631 // means the initializer is malformed. 632 if (!getLang().CPlusPlus0x) 633 return false; 634 } 635 636 // Grab the initializer 637 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren : 638 tok::r_brace, 639 Toks, /*StopAtSemi=*/true)) 640 return true; 641 642 // Grab the separating comma, if any. 643 if (Tok.is(tok::comma)) { 644 Toks.push_back(Tok); 645 ConsumeToken(); 646 } 647 } 648 } 649 650 // Grab any remaining garbage to be diagnosed later. We stop when we reach a 651 // brace: an opening one is the function body, while a closing one probably 652 // means we've reached the end of the class. 653 if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks, 654 /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false)) 655 return true; 656 if(Tok.isNot(tok::l_brace)) 657 return true; 658 659 Toks.push_back(Tok); 660 ConsumeBrace(); 661 return false; 662} 663