ParseCXXInlineMethods.cpp revision 249423
1193880Syongari//===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===// 2193880Syongari// 3193880Syongari// The LLVM Compiler Infrastructure 4193880Syongari// 5193880Syongari// This file is distributed under the University of Illinois Open Source 6193880Syongari// License. See LICENSE.TXT for details. 7193880Syongari// 8193880Syongari//===----------------------------------------------------------------------===// 9193880Syongari// 10193880Syongari// This file implements parsing for C++ class inline methods. 11193880Syongari// 12193880Syongari//===----------------------------------------------------------------------===// 13193880Syongari 14193880Syongari#include "clang/Parse/Parser.h" 15193880Syongari#include "RAIIObjectsForParser.h" 16193880Syongari#include "clang/AST/DeclTemplate.h" 17193880Syongari#include "clang/Parse/ParseDiagnostic.h" 18193880Syongari#include "clang/Sema/DeclSpec.h" 19193880Syongari#include "clang/Sema/Scope.h" 20193880Syongariusing namespace clang; 21193880Syongari 22193880Syongari/// Get the FunctionDecl for a function or function template decl. 23193880Syongaristatic FunctionDecl *getFunctionDecl(Decl *D) { 24193880Syongari if (FunctionDecl *fn = dyn_cast<FunctionDecl>(D)) 25193880Syongari return fn; 26193880Syongari return cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); 27193880Syongari} 28193880Syongari 29193880Syongari/// ParseCXXInlineMethodDef - We parsed and verified that the specified 30193880Syongari/// Declarator is a well formed C++ inline method definition. Now lex its body 31193880Syongari/// and store its tokens for parsing after the C++ class is complete. 32193880SyongariNamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, 33193880Syongari AttributeList *AccessAttrs, 34193880Syongari ParsingDeclarator &D, 35193880Syongari const ParsedTemplateInfo &TemplateInfo, 36193880Syongari const VirtSpecifiers& VS, 37193880Syongari FunctionDefinitionKind DefinitionKind, 38193880Syongari ExprResult& Init) { 39193880Syongari assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); 40193880Syongari assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) || 41193880Syongari Tok.is(tok::equal)) && 42193880Syongari "Current token not a '{', ':', '=', or 'try'!"); 43193880Syongari 44193880Syongari MultiTemplateParamsArg TemplateParams( 45264442Syongari TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0, 46193880Syongari TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); 47193880Syongari 48193880Syongari NamedDecl *FnD; 49193880Syongari D.setFunctionDefinitionKind(DefinitionKind); 50193880Syongari if (D.getDeclSpec().isFriendSpecified()) 51193880Syongari FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, 52193880Syongari TemplateParams); 53193880Syongari else { 54193880Syongari FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, 55273366Syongari TemplateParams, 0, 56273366Syongari VS, ICIS_NoInit); 57273366Syongari if (FnD) { 58273366Syongari Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs, 59193880Syongari false, true); 60193880Syongari bool TypeSpecContainsAuto 61193880Syongari = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; 62193880Syongari if (Init.isUsable()) 63193880Syongari Actions.AddInitializerToDecl(FnD, Init.get(), false, 64193880Syongari TypeSpecContainsAuto); 65193880Syongari else 66193880Syongari Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto); 67193880Syongari } 68193880Syongari } 69193880Syongari 70193880Syongari HandleMemberFunctionDeclDelays(D, FnD); 71193880Syongari 72193880Syongari D.complete(FnD); 73193880Syongari 74193880Syongari if (Tok.is(tok::equal)) { 75193880Syongari ConsumeToken(); 76211105Syongari 77193880Syongari if (!FnD) { 78193880Syongari SkipUntil(tok::semi); 79193880Syongari return 0; 80193880Syongari } 81193880Syongari 82193880Syongari bool Delete = false; 83193880Syongari SourceLocation KWLoc; 84193880Syongari if (Tok.is(tok::kw_delete)) { 85193880Syongari Diag(Tok, getLangOpts().CPlusPlus11 ? 86193880Syongari diag::warn_cxx98_compat_deleted_function : 87193880Syongari diag::ext_deleted_function); 88193880Syongari 89193880Syongari KWLoc = ConsumeToken(); 90193880Syongari Actions.SetDeclDeleted(FnD, KWLoc); 91193880Syongari Delete = true; 92193880Syongari } else if (Tok.is(tok::kw_default)) { 93193880Syongari Diag(Tok, getLangOpts().CPlusPlus11 ? 94193880Syongari diag::warn_cxx98_compat_defaulted_function : 95193880Syongari diag::ext_defaulted_function); 96193880Syongari 97193880Syongari KWLoc = ConsumeToken(); 98193880Syongari Actions.SetDeclDefaulted(FnD, KWLoc); 99193880Syongari } else { 100193880Syongari llvm_unreachable("function definition after = not 'delete' or 'default'"); 101193880Syongari } 102193880Syongari 103193880Syongari if (Tok.is(tok::comma)) { 104193880Syongari Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 105193880Syongari << Delete; 106193880Syongari SkipUntil(tok::semi); 107193880Syongari } else { 108193880Syongari ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 109193880Syongari Delete ? "delete" : "default", tok::semi); 110193880Syongari } 111193880Syongari 112193880Syongari return FnD; 113193880Syongari } 114193880Syongari 115193880Syongari // In delayed template parsing mode, if we are within a class template 116193880Syongari // or if we are about to parse function member template then consume 117193880Syongari // the tokens and store them for parsing at the end of the translation unit. 118193880Syongari if (getLangOpts().DelayedTemplateParsing && 119193880Syongari DefinitionKind == FDK_Definition && 120193880Syongari ((Actions.CurContext->isDependentContext() || 121193880Syongari TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) && 122193880Syongari !Actions.IsInsideALocalClassWithinATemplateFunction())) { 123193880Syongari 124193880Syongari if (FnD) { 125193880Syongari LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD); 126193880Syongari 127193880Syongari FunctionDecl *FD = getFunctionDecl(FnD); 128193880Syongari Actions.CheckForFunctionRedefinition(FD); 129193880Syongari 130193880Syongari LateParsedTemplateMap[FD] = LPT; 131193880Syongari Actions.MarkAsLateParsedTemplate(FD); 132193880Syongari LexTemplateFunctionForLateParsing(LPT->Toks); 133193880Syongari } else { 134193880Syongari CachedTokens Toks; 135193880Syongari LexTemplateFunctionForLateParsing(Toks); 136193880Syongari } 137193880Syongari 138193880Syongari return FnD; 139193880Syongari } 140193880Syongari 141193880Syongari // Consume the tokens and store them for later parsing. 142193880Syongari 143193880Syongari LexedMethod* LM = new LexedMethod(this, FnD); 144193880Syongari getCurrentClass().LateParsedDeclarations.push_back(LM); 145193880Syongari LM->TemplateScope = getCurScope()->isTemplateParamScope(); 146193880Syongari CachedTokens &Toks = LM->Toks; 147193880Syongari 148193880Syongari tok::TokenKind kind = Tok.getKind(); 149193880Syongari // Consume everything up to (and including) the left brace of the 150193880Syongari // function body. 151193880Syongari if (ConsumeAndStoreFunctionPrologue(Toks)) { 152193880Syongari // We didn't find the left-brace we expected after the 153193880Syongari // constructor initializer; we already printed an error, and it's likely 154193880Syongari // impossible to recover, so don't try to parse this method later. 155193880Syongari // If we stopped at a semicolon, consume it to avoid an extra warning. 156193880Syongari if (Tok.is(tok::semi)) 157193880Syongari ConsumeToken(); 158193880Syongari delete getCurrentClass().LateParsedDeclarations.back(); 159193880Syongari getCurrentClass().LateParsedDeclarations.pop_back(); 160193880Syongari return FnD; 161193880Syongari } else { 162193880Syongari // Consume everything up to (and including) the matching right brace. 163193880Syongari ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 164193880Syongari } 165193880Syongari 166193880Syongari // If we're in a function-try-block, we need to store all the catch blocks. 167193880Syongari if (kind == tok::kw_try) { 168193880Syongari while (Tok.is(tok::kw_catch)) { 169193880Syongari ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); 170193880Syongari ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 171193880Syongari } 172193880Syongari } 173193880Syongari 174193880Syongari 175193880Syongari if (!FnD) { 176193880Syongari // If semantic analysis could not build a function declaration, 177193880Syongari // just throw away the late-parsed declaration. 178193880Syongari delete getCurrentClass().LateParsedDeclarations.back(); 179193880Syongari getCurrentClass().LateParsedDeclarations.pop_back(); 180193880Syongari } 181193880Syongari 182193880Syongari // If this is a friend function, mark that it's late-parsed so that 183193880Syongari // it's still known to be a definition even before we attach the 184193880Syongari // parsed body. Sema needs to treat friend function definitions 185193880Syongari // differently during template instantiation, and it's possible for 186193880Syongari // the containing class to be instantiated before all its member 187193880Syongari // function definitions are parsed. 188193880Syongari // 189193880Syongari // If you remove this, you can remove the code that clears the flag 190193880Syongari // after parsing the member. 191193880Syongari if (D.getDeclSpec().isFriendSpecified()) { 192193880Syongari getFunctionDecl(FnD)->setLateTemplateParsed(true); 193193880Syongari } 194211105Syongari 195211105Syongari return FnD; 196211105Syongari} 197211105Syongari 198211105Syongari/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the 199211105Syongari/// specified Declarator is a well formed C++ non-static data member 200211105Syongari/// declaration. Now lex its initializer and store its tokens for parsing 201193880Syongari/// after the class is complete. 202193880Syongarivoid Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) { 203193880Syongari assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) && 204193880Syongari "Current token not a '{' or '='!"); 205193880Syongari 206193880Syongari LateParsedMemberInitializer *MI = 207193880Syongari new LateParsedMemberInitializer(this, VarD); 208193880Syongari getCurrentClass().LateParsedDeclarations.push_back(MI); 209193880Syongari CachedTokens &Toks = MI->Toks; 210193880Syongari 211193880Syongari tok::TokenKind kind = Tok.getKind(); 212193880Syongari if (kind == tok::equal) { 213211105Syongari Toks.push_back(Tok); 214193880Syongari ConsumeToken(); 215193880Syongari } 216193880Syongari 217193880Syongari if (kind == tok::l_brace) { 218193880Syongari // Begin by storing the '{' token. 219193880Syongari Toks.push_back(Tok); 220193880Syongari ConsumeBrace(); 221211053Syongari 222211053Syongari // Consume everything up to (and including) the matching right brace. 223193880Syongari ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true); 224193880Syongari } else { 225193880Syongari // Consume everything up to (but excluding) the comma or semicolon. 226193880Syongari ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true, 227193880Syongari /*ConsumeFinalToken=*/false); 228211053Syongari } 229193880Syongari 230193880Syongari // Store an artificial EOF token to ensure that we don't run off the end of 231193880Syongari // the initializer when we come to parse it. 232193880Syongari Token Eof; 233211105Syongari Eof.startToken(); 234211105Syongari Eof.setKind(tok::eof); 235211105Syongari Eof.setLocation(Tok.getLocation()); 236273366Syongari Toks.push_back(Eof); 237273366Syongari} 238193880Syongari 239193880SyongariParser::LateParsedDeclaration::~LateParsedDeclaration() {} 240193880Syongarivoid Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} 241193880Syongarivoid Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {} 242193880Syongarivoid Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} 243193880Syongari 244193880SyongariParser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) 245193880Syongari : Self(P), Class(C) {} 246193880Syongari 247193880SyongariParser::LateParsedClass::~LateParsedClass() { 248193880Syongari Self->DeallocateParsedClasses(Class); 249193880Syongari} 250193880Syongari 251193880Syongarivoid Parser::LateParsedClass::ParseLexedMethodDeclarations() { 252193880Syongari Self->ParseLexedMethodDeclarations(*Class); 253193880Syongari} 254193880Syongari 255193880Syongarivoid Parser::LateParsedClass::ParseLexedMemberInitializers() { 256193880Syongari Self->ParseLexedMemberInitializers(*Class); 257193880Syongari} 258193880Syongari 259193880Syongarivoid Parser::LateParsedClass::ParseLexedMethodDefs() { 260193880Syongari Self->ParseLexedMethodDefs(*Class); 261193880Syongari} 262193880Syongari 263193880Syongarivoid Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { 264193880Syongari Self->ParseLexedMethodDeclaration(*this); 265193880Syongari} 266193880Syongari 267193880Syongarivoid Parser::LexedMethod::ParseLexedMethodDefs() { 268193880Syongari Self->ParseLexedMethodDef(*this); 269193880Syongari} 270193880Syongari 271193880Syongarivoid Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() { 272193880Syongari Self->ParseLexedMemberInitializer(*this); 273193880Syongari} 274193880Syongari 275193880Syongari/// ParseLexedMethodDeclarations - We finished parsing the member 276193880Syongari/// specification of a top (non-nested) C++ class. Now go over the 277193880Syongari/// stack of method declarations with some parts for which parsing was 278193880Syongari/// delayed (such as default arguments) and parse them. 279193880Syongarivoid Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { 280193880Syongari bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 281193880Syongari ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); 282193880Syongari if (HasTemplateScope) 283193880Syongari Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 284193880Syongari 285193880Syongari // The current scope is still active if we're the top-level class. 286193880Syongari // Otherwise we'll need to push and enter a new scope. 287 bool HasClassScope = !Class.TopLevelClass; 288 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, 289 HasClassScope); 290 if (HasClassScope) 291 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), 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(), Class.TagOrTemplate); 299} 300 301void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { 302 // If this is a member template, introduce the template parameter scope. 303 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); 304 if (LM.TemplateScope) 305 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method); 306 307 // Start the delayed C++ method declaration 308 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 309 310 // Introduce the parameters into scope and parse their default 311 // arguments. 312 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | 313 Scope::FunctionDeclarationScope | Scope::DeclScope); 314 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { 315 // Introduce the parameter into scope. 316 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), 317 LM.DefaultArgs[I].Param); 318 319 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) { 320 // Save the current token position. 321 SourceLocation origLoc = Tok.getLocation(); 322 323 // Parse the default argument from its saved token stream. 324 Toks->push_back(Tok); // So that the current token doesn't get lost 325 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); 326 327 // Consume the previously-pushed token. 328 ConsumeAnyToken(); 329 330 // Consume the '='. 331 assert(Tok.is(tok::equal) && "Default argument not starting with '='"); 332 SourceLocation EqualLoc = ConsumeToken(); 333 334 // The argument isn't actually potentially evaluated unless it is 335 // used. 336 EnterExpressionEvaluationContext Eval(Actions, 337 Sema::PotentiallyEvaluatedIfUsed, 338 LM.DefaultArgs[I].Param); 339 340 ExprResult DefArgResult; 341 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 342 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 343 DefArgResult = ParseBraceInitializer(); 344 } else 345 DefArgResult = ParseAssignmentExpression(); 346 if (DefArgResult.isInvalid()) 347 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param); 348 else { 349 if (Tok.is(tok::cxx_defaultarg_end)) 350 ConsumeToken(); 351 else 352 Diag(Tok.getLocation(), diag::err_default_arg_unparsed); 353 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc, 354 DefArgResult.take()); 355 } 356 357 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, 358 Tok.getLocation()) && 359 "ParseAssignmentExpression went over the default arg tokens!"); 360 // There could be leftover tokens (e.g. because of an error). 361 // Skip through until we reach the original token position. 362 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 363 ConsumeAnyToken(); 364 365 delete Toks; 366 LM.DefaultArgs[I].Toks = 0; 367 } 368 } 369 370 PrototypeScope.Exit(); 371 372 // Finish the delayed C++ method declaration. 373 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 374} 375 376/// ParseLexedMethodDefs - We finished parsing the member specification of a top 377/// (non-nested) C++ class. Now go over the stack of lexed methods that were 378/// collected during its parsing and parse them all. 379void Parser::ParseLexedMethodDefs(ParsingClass &Class) { 380 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 381 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); 382 if (HasTemplateScope) 383 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 384 385 bool HasClassScope = !Class.TopLevelClass; 386 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, 387 HasClassScope); 388 389 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 390 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs(); 391 } 392} 393 394void Parser::ParseLexedMethodDef(LexedMethod &LM) { 395 // If this is a member template, introduce the template parameter scope. 396 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); 397 if (LM.TemplateScope) 398 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D); 399 400 // Save the current token position. 401 SourceLocation origLoc = Tok.getLocation(); 402 403 assert(!LM.Toks.empty() && "Empty body!"); 404 // Append the current token at the end of the new token stream so that it 405 // doesn't get lost. 406 LM.Toks.push_back(Tok); 407 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); 408 409 // Consume the previously pushed token. 410 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 411 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) 412 && "Inline method not starting with '{', ':' or 'try'"); 413 414 // Parse the method body. Function body parsing code is similar enough 415 // to be re-used for method bodies as well. 416 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); 417 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); 418 419 if (Tok.is(tok::kw_try)) { 420 ParseFunctionTryBlock(LM.D, FnScope); 421 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, 422 Tok.getLocation()) && 423 "ParseFunctionTryBlock went over the cached tokens!"); 424 // There could be leftover tokens (e.g. because of an error). 425 // Skip through until we reach the original token position. 426 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 427 ConsumeAnyToken(); 428 return; 429 } 430 if (Tok.is(tok::colon)) { 431 ParseConstructorInitializer(LM.D); 432 433 // Error recovery. 434 if (!Tok.is(tok::l_brace)) { 435 FnScope.Exit(); 436 Actions.ActOnFinishFunctionBody(LM.D, 0); 437 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 438 ConsumeAnyToken(); 439 return; 440 } 441 } else 442 Actions.ActOnDefaultCtorInitializers(LM.D); 443 444 ParseFunctionStatementBody(LM.D, FnScope); 445 446 // Clear the late-template-parsed bit if we set it before. 447 if (LM.D) getFunctionDecl(LM.D)->setLateTemplateParsed(false); 448 449 if (Tok.getLocation() != origLoc) { 450 // Due to parsing error, we either went over the cached tokens or 451 // there are still cached tokens left. If it's the latter case skip the 452 // leftover tokens. 453 // Since this is an uncommon situation that should be avoided, use the 454 // expensive isBeforeInTranslationUnit call. 455 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), 456 origLoc)) 457 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 458 ConsumeAnyToken(); 459 } 460} 461 462/// ParseLexedMemberInitializers - We finished parsing the member specification 463/// of a top (non-nested) C++ class. Now go over the stack of lexed data member 464/// initializers that were collected during its parsing and parse them all. 465void Parser::ParseLexedMemberInitializers(ParsingClass &Class) { 466 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 467 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, 468 HasTemplateScope); 469 if (HasTemplateScope) 470 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 471 472 // Set or update the scope flags. 473 bool AlreadyHasClassScope = Class.TopLevelClass; 474 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; 475 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); 476 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); 477 478 if (!AlreadyHasClassScope) 479 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), 480 Class.TagOrTemplate); 481 482 if (!Class.LateParsedDeclarations.empty()) { 483 // C++11 [expr.prim.general]p4: 484 // Otherwise, if a member-declarator declares a non-static data member 485 // (9.2) of a class X, the expression this is a prvalue of type "pointer 486 // to X" within the optional brace-or-equal-initializer. It shall not 487 // appear elsewhere in the member-declarator. 488 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, 489 /*TypeQuals=*/(unsigned)0); 490 491 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 492 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers(); 493 } 494 } 495 496 if (!AlreadyHasClassScope) 497 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), 498 Class.TagOrTemplate); 499 500 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate); 501} 502 503void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { 504 if (!MI.Field || MI.Field->isInvalidDecl()) 505 return; 506 507 // Append the current token at the end of the new token stream so that it 508 // doesn't get lost. 509 MI.Toks.push_back(Tok); 510 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false); 511 512 // Consume the previously pushed token. 513 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 514 515 SourceLocation EqualLoc; 516 517 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false, 518 EqualLoc); 519 520 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release()); 521 522 // The next token should be our artificial terminating EOF token. 523 if (Tok.isNot(tok::eof)) { 524 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 525 if (!EndLoc.isValid()) 526 EndLoc = Tok.getLocation(); 527 // No fixit; we can't recover as if there were a semicolon here. 528 Diag(EndLoc, diag::err_expected_semi_decl_list); 529 530 // Consume tokens until we hit the artificial EOF. 531 while (Tok.isNot(tok::eof)) 532 ConsumeAnyToken(); 533 } 534 ConsumeAnyToken(); 535} 536 537/// ConsumeAndStoreUntil - Consume and store the token at the passed token 538/// container until the token 'T' is reached (which gets 539/// consumed/stored too, if ConsumeFinalToken). 540/// If StopAtSemi is true, then we will stop early at a ';' character. 541/// Returns true if token 'T1' or 'T2' was found. 542/// NOTE: This is a specialized version of Parser::SkipUntil. 543bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, 544 CachedTokens &Toks, 545 bool StopAtSemi, bool ConsumeFinalToken) { 546 // We always want this function to consume at least one token if the first 547 // token isn't T and if not at EOF. 548 bool isFirstTokenConsumed = true; 549 while (1) { 550 // If we found one of the tokens, stop and return true. 551 if (Tok.is(T1) || Tok.is(T2)) { 552 if (ConsumeFinalToken) { 553 Toks.push_back(Tok); 554 ConsumeAnyToken(); 555 } 556 return true; 557 } 558 559 switch (Tok.getKind()) { 560 case tok::eof: 561 // Ran out of tokens. 562 return false; 563 564 case tok::l_paren: 565 // Recursively consume properly-nested parens. 566 Toks.push_back(Tok); 567 ConsumeParen(); 568 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); 569 break; 570 case tok::l_square: 571 // Recursively consume properly-nested square brackets. 572 Toks.push_back(Tok); 573 ConsumeBracket(); 574 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); 575 break; 576 case tok::l_brace: 577 // Recursively consume properly-nested braces. 578 Toks.push_back(Tok); 579 ConsumeBrace(); 580 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 581 break; 582 583 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 584 // Since the user wasn't looking for this token (if they were, it would 585 // already be handled), this isn't balanced. If there is a LHS token at a 586 // higher level, we will assume that this matches the unbalanced token 587 // and return it. Otherwise, this is a spurious RHS token, which we skip. 588 case tok::r_paren: 589 if (ParenCount && !isFirstTokenConsumed) 590 return false; // Matches something. 591 Toks.push_back(Tok); 592 ConsumeParen(); 593 break; 594 case tok::r_square: 595 if (BracketCount && !isFirstTokenConsumed) 596 return false; // Matches something. 597 Toks.push_back(Tok); 598 ConsumeBracket(); 599 break; 600 case tok::r_brace: 601 if (BraceCount && !isFirstTokenConsumed) 602 return false; // Matches something. 603 Toks.push_back(Tok); 604 ConsumeBrace(); 605 break; 606 607 case tok::code_completion: 608 Toks.push_back(Tok); 609 ConsumeCodeCompletionToken(); 610 break; 611 612 case tok::string_literal: 613 case tok::wide_string_literal: 614 case tok::utf8_string_literal: 615 case tok::utf16_string_literal: 616 case tok::utf32_string_literal: 617 Toks.push_back(Tok); 618 ConsumeStringToken(); 619 break; 620 case tok::semi: 621 if (StopAtSemi) 622 return false; 623 // FALL THROUGH. 624 default: 625 // consume this token. 626 Toks.push_back(Tok); 627 ConsumeToken(); 628 break; 629 } 630 isFirstTokenConsumed = false; 631 } 632} 633 634/// \brief Consume tokens and store them in the passed token container until 635/// we've passed the try keyword and constructor initializers and have consumed 636/// the opening brace of the function body. The opening brace will be consumed 637/// if and only if there was no error. 638/// 639/// \return True on error. 640bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) { 641 if (Tok.is(tok::kw_try)) { 642 Toks.push_back(Tok); 643 ConsumeToken(); 644 } 645 bool ReadInitializer = false; 646 if (Tok.is(tok::colon)) { 647 // Initializers can contain braces too. 648 Toks.push_back(Tok); 649 ConsumeToken(); 650 651 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { 652 if (Tok.is(tok::eof) || Tok.is(tok::semi)) 653 return Diag(Tok.getLocation(), diag::err_expected_lbrace); 654 655 // Grab the identifier. 656 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks, 657 /*StopAtSemi=*/true, 658 /*ConsumeFinalToken=*/false)) 659 return Diag(Tok.getLocation(), diag::err_expected_lparen); 660 661 tok::TokenKind kind = Tok.getKind(); 662 Toks.push_back(Tok); 663 bool IsLParen = (kind == tok::l_paren); 664 SourceLocation LOpen = Tok.getLocation(); 665 666 if (IsLParen) { 667 ConsumeParen(); 668 } else { 669 assert(kind == tok::l_brace && "Must be left paren or brace here."); 670 ConsumeBrace(); 671 // In C++03, this has to be the start of the function body, which 672 // means the initializer is malformed; we'll diagnose it later. 673 if (!getLangOpts().CPlusPlus11) 674 return false; 675 } 676 677 // Grab the initializer 678 if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace, 679 Toks, /*StopAtSemi=*/true)) { 680 Diag(Tok, IsLParen ? diag::err_expected_rparen : 681 diag::err_expected_rbrace); 682 Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{"); 683 return true; 684 } 685 686 // Grab pack ellipsis, if present 687 if (Tok.is(tok::ellipsis)) { 688 Toks.push_back(Tok); 689 ConsumeToken(); 690 } 691 692 // Grab the separating comma, if any. 693 if (Tok.is(tok::comma)) { 694 Toks.push_back(Tok); 695 ConsumeToken(); 696 } else if (Tok.isNot(tok::l_brace)) { 697 ReadInitializer = true; 698 break; 699 } 700 } 701 } 702 703 // Grab any remaining garbage to be diagnosed later. We stop when we reach a 704 // brace: an opening one is the function body, while a closing one probably 705 // means we've reached the end of the class. 706 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks, 707 /*StopAtSemi=*/true, 708 /*ConsumeFinalToken=*/false); 709 if (Tok.isNot(tok::l_brace)) { 710 if (ReadInitializer) 711 return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); 712 return Diag(Tok.getLocation(), diag::err_expected_lbrace); 713 } 714 715 Toks.push_back(Tok); 716 ConsumeBrace(); 717 return false; 718} 719