1 //===--- Parser.cpp - C Language Family Parser ----------------------------===// 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 the Parser interfaces. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Parse/Parser.h" 15 #include "ParsePragma.h" 16 #include "RAIIObjectsForParser.h" 17 #include "clang/AST/ASTConsumer.h" 18 #include "clang/AST/ASTContext.h" 19 #include "clang/AST/DeclTemplate.h" 20 #include "clang/Parse/ParseDiagnostic.h" 21 #include "clang/Sema/DeclSpec.h" 22 #include "clang/Sema/ParsedTemplate.h" 23 #include "clang/Sema/Scope.h" 24 #include "llvm/Support/raw_ostream.h" 25 using namespace clang; 26 27 28 namespace { 29 /// \brief A comment handler that passes comments found by the preprocessor 30 /// to the parser action. 31 class ActionCommentHandler : public CommentHandler { 32 Sema &S; 33 34 public: 35 explicit ActionCommentHandler(Sema &S) : S(S) { } 36 37 virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) { 38 S.ActOnComment(Comment); 39 return false; 40 } 41 }; 42 } // end anonymous namespace 43 44 IdentifierInfo *Parser::getSEHExceptKeyword() { 45 // __except is accepted as a (contextual) keyword 46 if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland)) 47 Ident__except = PP.getIdentifierInfo("__except"); 48 49 return Ident__except; 50 } 51 52 Parser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies) 53 : PP(pp), Actions(actions), Diags(PP.getDiagnostics()), 54 GreaterThanIsOperator(true), ColonIsSacred(false), 55 InMessageExpression(false), TemplateParameterDepth(0), 56 ParsingInObjCContainer(false) { 57 SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies; 58 Tok.startToken(); 59 Tok.setKind(tok::eof); 60 Actions.CurScope = 0; 61 NumCachedScopes = 0; 62 ParenCount = BracketCount = BraceCount = 0; 63 CurParsedObjCImpl = 0; 64 65 // Add #pragma handlers. These are removed and destroyed in the 66 // destructor. 67 AlignHandler.reset(new PragmaAlignHandler()); 68 PP.AddPragmaHandler(AlignHandler.get()); 69 70 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler()); 71 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get()); 72 73 OptionsHandler.reset(new PragmaOptionsHandler()); 74 PP.AddPragmaHandler(OptionsHandler.get()); 75 76 PackHandler.reset(new PragmaPackHandler()); 77 PP.AddPragmaHandler(PackHandler.get()); 78 79 MSStructHandler.reset(new PragmaMSStructHandler()); 80 PP.AddPragmaHandler(MSStructHandler.get()); 81 82 UnusedHandler.reset(new PragmaUnusedHandler()); 83 PP.AddPragmaHandler(UnusedHandler.get()); 84 85 WeakHandler.reset(new PragmaWeakHandler()); 86 PP.AddPragmaHandler(WeakHandler.get()); 87 88 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler()); 89 PP.AddPragmaHandler(RedefineExtnameHandler.get()); 90 91 FPContractHandler.reset(new PragmaFPContractHandler()); 92 PP.AddPragmaHandler("STDC", FPContractHandler.get()); 93 94 if (getLangOpts().OpenCL) { 95 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler()); 96 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get()); 97 98 PP.AddPragmaHandler("OPENCL", FPContractHandler.get()); 99 } 100 if (getLangOpts().OpenMP) 101 OpenMPHandler.reset(new PragmaOpenMPHandler()); 102 else 103 OpenMPHandler.reset(new PragmaNoOpenMPHandler()); 104 PP.AddPragmaHandler(OpenMPHandler.get()); 105 106 if (getLangOpts().MicrosoftExt) { 107 MSCommentHandler.reset(new PragmaCommentHandler(actions)); 108 PP.AddPragmaHandler(MSCommentHandler.get()); 109 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(actions)); 110 PP.AddPragmaHandler(MSDetectMismatchHandler.get()); 111 } 112 113 CommentSemaHandler.reset(new ActionCommentHandler(actions)); 114 PP.addCommentHandler(CommentSemaHandler.get()); 115 116 PP.setCodeCompletionHandler(*this); 117 } 118 119 DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) { 120 return Diags.Report(Loc, DiagID); 121 } 122 123 DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) { 124 return Diag(Tok.getLocation(), DiagID); 125 } 126 127 /// \brief Emits a diagnostic suggesting parentheses surrounding a 128 /// given range. 129 /// 130 /// \param Loc The location where we'll emit the diagnostic. 131 /// \param DK The kind of diagnostic to emit. 132 /// \param ParenRange Source range enclosing code that should be parenthesized. 133 void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK, 134 SourceRange ParenRange) { 135 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd()); 136 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { 137 // We can't display the parentheses, so just dig the 138 // warning/error and return. 139 Diag(Loc, DK); 140 return; 141 } 142 143 Diag(Loc, DK) 144 << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") 145 << FixItHint::CreateInsertion(EndLoc, ")"); 146 } 147 148 static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) { 149 switch (ExpectedTok) { 150 case tok::semi: 151 return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ; 152 default: return false; 153 } 154 } 155 156 bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID, 157 const char *Msg) { 158 if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) { 159 ConsumeAnyToken(); 160 return false; 161 } 162 163 // Detect common single-character typos and resume. 164 if (IsCommonTypo(ExpectedTok, Tok)) { 165 SourceLocation Loc = Tok.getLocation(); 166 DiagnosticBuilder DB = Diag(Loc, DiagID); 167 DB << FixItHint::CreateReplacement(SourceRange(Loc), 168 getPunctuatorSpelling(ExpectedTok)); 169 if (DiagID == diag::err_expected) 170 DB << ExpectedTok; 171 else if (DiagID == diag::err_expected_after) 172 DB << Msg << ExpectedTok; 173 else 174 DB << Msg; 175 ConsumeAnyToken(); 176 177 // Pretend there wasn't a problem. 178 return false; 179 } 180 181 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 182 const char *Spelling = 0; 183 if (EndLoc.isValid()) 184 Spelling = tok::getPunctuatorSpelling(ExpectedTok); 185 186 DiagnosticBuilder DB = 187 Spelling 188 ? Diag(EndLoc, DiagID) << FixItHint::CreateInsertion(EndLoc, Spelling) 189 : Diag(Tok, DiagID); 190 if (DiagID == diag::err_expected) 191 DB << ExpectedTok; 192 else if (DiagID == diag::err_expected_after) 193 DB << Msg << ExpectedTok; 194 else 195 DB << Msg; 196 197 return true; 198 } 199 200 bool Parser::ExpectAndConsumeSemi(unsigned DiagID) { 201 if (TryConsumeToken(tok::semi)) 202 return false; 203 204 if (Tok.is(tok::code_completion)) { 205 handleUnexpectedCodeCompletionToken(); 206 return false; 207 } 208 209 if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) && 210 NextToken().is(tok::semi)) { 211 Diag(Tok, diag::err_extraneous_token_before_semi) 212 << PP.getSpelling(Tok) 213 << FixItHint::CreateRemoval(Tok.getLocation()); 214 ConsumeAnyToken(); // The ')' or ']'. 215 ConsumeToken(); // The ';'. 216 return false; 217 } 218 219 return ExpectAndConsume(tok::semi, DiagID); 220 } 221 222 void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, unsigned TST) { 223 if (!Tok.is(tok::semi)) return; 224 225 bool HadMultipleSemis = false; 226 SourceLocation StartLoc = Tok.getLocation(); 227 SourceLocation EndLoc = Tok.getLocation(); 228 ConsumeToken(); 229 230 while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) { 231 HadMultipleSemis = true; 232 EndLoc = Tok.getLocation(); 233 ConsumeToken(); 234 } 235 236 // C++11 allows extra semicolons at namespace scope, but not in any of the 237 // other contexts. 238 if (Kind == OutsideFunction && getLangOpts().CPlusPlus) { 239 if (getLangOpts().CPlusPlus11) 240 Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi) 241 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 242 else 243 Diag(StartLoc, diag::ext_extra_semi_cxx11) 244 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 245 return; 246 } 247 248 if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis) 249 Diag(StartLoc, diag::ext_extra_semi) 250 << Kind << DeclSpec::getSpecifierName((DeclSpec::TST)TST, 251 Actions.getASTContext().getPrintingPolicy()) 252 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 253 else 254 // A single semicolon is valid after a member function definition. 255 Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def) 256 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); 257 } 258 259 //===----------------------------------------------------------------------===// 260 // Error recovery. 261 //===----------------------------------------------------------------------===// 262 263 static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) { 264 return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0; 265 } 266 267 /// SkipUntil - Read tokens until we get to the specified token, then consume 268 /// it (unless no flag StopBeforeMatch). Because we cannot guarantee that the 269 /// token will ever occur, this skips to the next token, or to some likely 270 /// good stopping point. If StopAtSemi is true, skipping will stop at a ';' 271 /// character. 272 /// 273 /// If SkipUntil finds the specified token, it returns true, otherwise it 274 /// returns false. 275 bool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, SkipUntilFlags Flags) { 276 // We always want this function to skip at least one token if the first token 277 // isn't T and if not at EOF. 278 bool isFirstTokenSkipped = true; 279 while (1) { 280 // If we found one of the tokens, stop and return true. 281 for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) { 282 if (Tok.is(Toks[i])) { 283 if (HasFlagsSet(Flags, StopBeforeMatch)) { 284 // Noop, don't consume the token. 285 } else { 286 ConsumeAnyToken(); 287 } 288 return true; 289 } 290 } 291 292 // Important special case: The caller has given up and just wants us to 293 // skip the rest of the file. Do this without recursing, since we can 294 // get here precisely because the caller detected too much recursion. 295 if (Toks.size() == 1 && Toks[0] == tok::eof && 296 !HasFlagsSet(Flags, StopAtSemi) && 297 !HasFlagsSet(Flags, StopAtCodeCompletion)) { 298 while (Tok.isNot(tok::eof)) 299 ConsumeAnyToken(); 300 return true; 301 } 302 303 switch (Tok.getKind()) { 304 case tok::eof: 305 // Ran out of tokens. 306 return false; 307 308 case tok::annot_pragma_openmp_end: 309 // Stop before an OpenMP pragma boundary. 310 case tok::annot_module_begin: 311 case tok::annot_module_end: 312 case tok::annot_module_include: 313 // Stop before we change submodules. They generally indicate a "good" 314 // place to pick up parsing again (except in the special case where 315 // we're trying to skip to EOF). 316 return false; 317 318 case tok::code_completion: 319 if (!HasFlagsSet(Flags, StopAtCodeCompletion)) 320 handleUnexpectedCodeCompletionToken(); 321 return false; 322 323 case tok::l_paren: 324 // Recursively skip properly-nested parens. 325 ConsumeParen(); 326 if (HasFlagsSet(Flags, StopAtCodeCompletion)) 327 SkipUntil(tok::r_paren, StopAtCodeCompletion); 328 else 329 SkipUntil(tok::r_paren); 330 break; 331 case tok::l_square: 332 // Recursively skip properly-nested square brackets. 333 ConsumeBracket(); 334 if (HasFlagsSet(Flags, StopAtCodeCompletion)) 335 SkipUntil(tok::r_square, StopAtCodeCompletion); 336 else 337 SkipUntil(tok::r_square); 338 break; 339 case tok::l_brace: 340 // Recursively skip properly-nested braces. 341 ConsumeBrace(); 342 if (HasFlagsSet(Flags, StopAtCodeCompletion)) 343 SkipUntil(tok::r_brace, StopAtCodeCompletion); 344 else 345 SkipUntil(tok::r_brace); 346 break; 347 348 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 349 // Since the user wasn't looking for this token (if they were, it would 350 // already be handled), this isn't balanced. If there is a LHS token at a 351 // higher level, we will assume that this matches the unbalanced token 352 // and return it. Otherwise, this is a spurious RHS token, which we skip. 353 case tok::r_paren: 354 if (ParenCount && !isFirstTokenSkipped) 355 return false; // Matches something. 356 ConsumeParen(); 357 break; 358 case tok::r_square: 359 if (BracketCount && !isFirstTokenSkipped) 360 return false; // Matches something. 361 ConsumeBracket(); 362 break; 363 case tok::r_brace: 364 if (BraceCount && !isFirstTokenSkipped) 365 return false; // Matches something. 366 ConsumeBrace(); 367 break; 368 369 case tok::string_literal: 370 case tok::wide_string_literal: 371 case tok::utf8_string_literal: 372 case tok::utf16_string_literal: 373 case tok::utf32_string_literal: 374 ConsumeStringToken(); 375 break; 376 377 case tok::semi: 378 if (HasFlagsSet(Flags, StopAtSemi)) 379 return false; 380 // FALL THROUGH. 381 default: 382 // Skip this token. 383 ConsumeToken(); 384 break; 385 } 386 isFirstTokenSkipped = false; 387 } 388 } 389 390 //===----------------------------------------------------------------------===// 391 // Scope manipulation 392 //===----------------------------------------------------------------------===// 393 394 /// EnterScope - Start a new scope. 395 void Parser::EnterScope(unsigned ScopeFlags) { 396 if (NumCachedScopes) { 397 Scope *N = ScopeCache[--NumCachedScopes]; 398 N->Init(getCurScope(), ScopeFlags); 399 Actions.CurScope = N; 400 } else { 401 Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags); 402 } 403 } 404 405 /// ExitScope - Pop a scope off the scope stack. 406 void Parser::ExitScope() { 407 assert(getCurScope() && "Scope imbalance!"); 408 409 // Inform the actions module that this scope is going away if there are any 410 // decls in it. 411 if (!getCurScope()->decl_empty()) 412 Actions.ActOnPopScope(Tok.getLocation(), getCurScope()); 413 414 Scope *OldScope = getCurScope(); 415 Actions.CurScope = OldScope->getParent(); 416 417 if (NumCachedScopes == ScopeCacheSize) 418 delete OldScope; 419 else 420 ScopeCache[NumCachedScopes++] = OldScope; 421 } 422 423 /// Set the flags for the current scope to ScopeFlags. If ManageFlags is false, 424 /// this object does nothing. 425 Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags, 426 bool ManageFlags) 427 : CurScope(ManageFlags ? Self->getCurScope() : 0) { 428 if (CurScope) { 429 OldFlags = CurScope->getFlags(); 430 CurScope->setFlags(ScopeFlags); 431 } 432 } 433 434 /// Restore the flags for the current scope to what they were before this 435 /// object overrode them. 436 Parser::ParseScopeFlags::~ParseScopeFlags() { 437 if (CurScope) 438 CurScope->setFlags(OldFlags); 439 } 440 441 442 //===----------------------------------------------------------------------===// 443 // C99 6.9: External Definitions. 444 //===----------------------------------------------------------------------===// 445 446 Parser::~Parser() { 447 // If we still have scopes active, delete the scope tree. 448 delete getCurScope(); 449 Actions.CurScope = 0; 450 451 // Free the scope cache. 452 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i) 453 delete ScopeCache[i]; 454 455 // Remove the pragma handlers we installed. 456 PP.RemovePragmaHandler(AlignHandler.get()); 457 AlignHandler.reset(); 458 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get()); 459 GCCVisibilityHandler.reset(); 460 PP.RemovePragmaHandler(OptionsHandler.get()); 461 OptionsHandler.reset(); 462 PP.RemovePragmaHandler(PackHandler.get()); 463 PackHandler.reset(); 464 PP.RemovePragmaHandler(MSStructHandler.get()); 465 MSStructHandler.reset(); 466 PP.RemovePragmaHandler(UnusedHandler.get()); 467 UnusedHandler.reset(); 468 PP.RemovePragmaHandler(WeakHandler.get()); 469 WeakHandler.reset(); 470 PP.RemovePragmaHandler(RedefineExtnameHandler.get()); 471 RedefineExtnameHandler.reset(); 472 473 if (getLangOpts().OpenCL) { 474 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get()); 475 OpenCLExtensionHandler.reset(); 476 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get()); 477 } 478 PP.RemovePragmaHandler(OpenMPHandler.get()); 479 OpenMPHandler.reset(); 480 481 if (getLangOpts().MicrosoftExt) { 482 PP.RemovePragmaHandler(MSCommentHandler.get()); 483 MSCommentHandler.reset(); 484 PP.RemovePragmaHandler(MSDetectMismatchHandler.get()); 485 MSDetectMismatchHandler.reset(); 486 } 487 488 PP.RemovePragmaHandler("STDC", FPContractHandler.get()); 489 FPContractHandler.reset(); 490 491 PP.removeCommentHandler(CommentSemaHandler.get()); 492 493 PP.clearCodeCompletionHandler(); 494 495 assert(TemplateIds.empty() && "Still alive TemplateIdAnnotations around?"); 496 } 497 498 /// Initialize - Warm up the parser. 499 /// 500 void Parser::Initialize() { 501 // Create the translation unit scope. Install it as the current scope. 502 assert(getCurScope() == 0 && "A scope is already active?"); 503 EnterScope(Scope::DeclScope); 504 Actions.ActOnTranslationUnitScope(getCurScope()); 505 506 // Initialization for Objective-C context sensitive keywords recognition. 507 // Referenced in Parser::ParseObjCTypeQualifierList. 508 if (getLangOpts().ObjC1) { 509 ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in"); 510 ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out"); 511 ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout"); 512 ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway"); 513 ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy"); 514 ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref"); 515 } 516 517 Ident_instancetype = 0; 518 Ident_final = 0; 519 Ident_sealed = 0; 520 Ident_override = 0; 521 522 Ident_super = &PP.getIdentifierTable().get("super"); 523 524 if (getLangOpts().AltiVec) { 525 Ident_vector = &PP.getIdentifierTable().get("vector"); 526 Ident_pixel = &PP.getIdentifierTable().get("pixel"); 527 Ident_bool = &PP.getIdentifierTable().get("bool"); 528 } 529 530 Ident_introduced = 0; 531 Ident_deprecated = 0; 532 Ident_obsoleted = 0; 533 Ident_unavailable = 0; 534 535 Ident__except = 0; 536 537 Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0; 538 Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0; 539 Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0; 540 541 if(getLangOpts().Borland) { 542 Ident__exception_info = PP.getIdentifierInfo("_exception_info"); 543 Ident___exception_info = PP.getIdentifierInfo("__exception_info"); 544 Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation"); 545 Ident__exception_code = PP.getIdentifierInfo("_exception_code"); 546 Ident___exception_code = PP.getIdentifierInfo("__exception_code"); 547 Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode"); 548 Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination"); 549 Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination"); 550 Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination"); 551 552 PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block); 553 PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block); 554 PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block); 555 PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter); 556 PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter); 557 PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter); 558 PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block); 559 PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block); 560 PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block); 561 } 562 563 Actions.Initialize(); 564 565 // Prime the lexer look-ahead. 566 ConsumeToken(); 567 } 568 569 namespace { 570 /// \brief RAIIObject to destroy the contents of a SmallVector of 571 /// TemplateIdAnnotation pointers and clear the vector. 572 class DestroyTemplateIdAnnotationsRAIIObj { 573 SmallVectorImpl<TemplateIdAnnotation *> &Container; 574 public: 575 DestroyTemplateIdAnnotationsRAIIObj(SmallVectorImpl<TemplateIdAnnotation *> 576 &Container) 577 : Container(Container) {} 578 579 ~DestroyTemplateIdAnnotationsRAIIObj() { 580 for (SmallVectorImpl<TemplateIdAnnotation *>::iterator I = 581 Container.begin(), E = Container.end(); 582 I != E; ++I) 583 (*I)->Destroy(); 584 Container.clear(); 585 } 586 }; 587 } 588 589 /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the 590 /// action tells us to. This returns true if the EOF was encountered. 591 bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) { 592 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds); 593 594 // Skip over the EOF token, flagging end of previous input for incremental 595 // processing 596 if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof)) 597 ConsumeToken(); 598 599 Result = DeclGroupPtrTy(); 600 switch (Tok.getKind()) { 601 case tok::annot_pragma_unused: 602 HandlePragmaUnused(); 603 return false; 604 605 case tok::annot_module_include: 606 Actions.ActOnModuleInclude(Tok.getLocation(), 607 reinterpret_cast<Module *>( 608 Tok.getAnnotationValue())); 609 ConsumeToken(); 610 return false; 611 612 case tok::annot_module_begin: 613 case tok::annot_module_end: 614 // FIXME: Update visibility based on the submodule we're in. 615 ConsumeToken(); 616 return false; 617 618 case tok::eof: 619 // Late template parsing can begin. 620 if (getLangOpts().DelayedTemplateParsing) 621 Actions.SetLateTemplateParser(LateTemplateParserCallback, this); 622 if (!PP.isIncrementalProcessingEnabled()) 623 Actions.ActOnEndOfTranslationUnit(); 624 //else don't tell Sema that we ended parsing: more input might come. 625 return true; 626 627 default: 628 break; 629 } 630 631 ParsedAttributesWithRange attrs(AttrFactory); 632 MaybeParseCXX11Attributes(attrs); 633 MaybeParseMicrosoftAttributes(attrs); 634 635 Result = ParseExternalDeclaration(attrs); 636 return false; 637 } 638 639 /// ParseExternalDeclaration: 640 /// 641 /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl] 642 /// function-definition 643 /// declaration 644 /// [GNU] asm-definition 645 /// [GNU] __extension__ external-declaration 646 /// [OBJC] objc-class-definition 647 /// [OBJC] objc-class-declaration 648 /// [OBJC] objc-alias-declaration 649 /// [OBJC] objc-protocol-definition 650 /// [OBJC] objc-method-definition 651 /// [OBJC] @end 652 /// [C++] linkage-specification 653 /// [GNU] asm-definition: 654 /// simple-asm-expr ';' 655 /// [C++11] empty-declaration 656 /// [C++11] attribute-declaration 657 /// 658 /// [C++11] empty-declaration: 659 /// ';' 660 /// 661 /// [C++0x/GNU] 'extern' 'template' declaration 662 Parser::DeclGroupPtrTy 663 Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs, 664 ParsingDeclSpec *DS) { 665 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds); 666 ParenBraceBracketBalancer BalancerRAIIObj(*this); 667 668 if (PP.isCodeCompletionReached()) { 669 cutOffParsing(); 670 return DeclGroupPtrTy(); 671 } 672 673 Decl *SingleDecl = 0; 674 switch (Tok.getKind()) { 675 case tok::annot_pragma_vis: 676 HandlePragmaVisibility(); 677 return DeclGroupPtrTy(); 678 case tok::annot_pragma_pack: 679 HandlePragmaPack(); 680 return DeclGroupPtrTy(); 681 case tok::annot_pragma_msstruct: 682 HandlePragmaMSStruct(); 683 return DeclGroupPtrTy(); 684 case tok::annot_pragma_align: 685 HandlePragmaAlign(); 686 return DeclGroupPtrTy(); 687 case tok::annot_pragma_weak: 688 HandlePragmaWeak(); 689 return DeclGroupPtrTy(); 690 case tok::annot_pragma_weakalias: 691 HandlePragmaWeakAlias(); 692 return DeclGroupPtrTy(); 693 case tok::annot_pragma_redefine_extname: 694 HandlePragmaRedefineExtname(); 695 return DeclGroupPtrTy(); 696 case tok::annot_pragma_fp_contract: 697 HandlePragmaFPContract(); 698 return DeclGroupPtrTy(); 699 case tok::annot_pragma_opencl_extension: 700 HandlePragmaOpenCLExtension(); 701 return DeclGroupPtrTy(); 702 case tok::annot_pragma_openmp: 703 ParseOpenMPDeclarativeDirective(); 704 return DeclGroupPtrTy(); 705 case tok::semi: 706 // Either a C++11 empty-declaration or attribute-declaration. 707 SingleDecl = Actions.ActOnEmptyDeclaration(getCurScope(), 708 attrs.getList(), 709 Tok.getLocation()); 710 ConsumeExtraSemi(OutsideFunction); 711 break; 712 case tok::r_brace: 713 Diag(Tok, diag::err_extraneous_closing_brace); 714 ConsumeBrace(); 715 return DeclGroupPtrTy(); 716 case tok::eof: 717 Diag(Tok, diag::err_expected_external_declaration); 718 return DeclGroupPtrTy(); 719 case tok::kw___extension__: { 720 // __extension__ silences extension warnings in the subexpression. 721 ExtensionRAIIObject O(Diags); // Use RAII to do this. 722 ConsumeToken(); 723 return ParseExternalDeclaration(attrs); 724 } 725 case tok::kw_asm: { 726 ProhibitAttributes(attrs); 727 728 SourceLocation StartLoc = Tok.getLocation(); 729 SourceLocation EndLoc; 730 ExprResult Result(ParseSimpleAsm(&EndLoc)); 731 732 ExpectAndConsume(tok::semi, diag::err_expected_after, 733 "top-level asm block"); 734 735 if (Result.isInvalid()) 736 return DeclGroupPtrTy(); 737 SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc); 738 break; 739 } 740 case tok::at: 741 return ParseObjCAtDirectives(); 742 case tok::minus: 743 case tok::plus: 744 if (!getLangOpts().ObjC1) { 745 Diag(Tok, diag::err_expected_external_declaration); 746 ConsumeToken(); 747 return DeclGroupPtrTy(); 748 } 749 SingleDecl = ParseObjCMethodDefinition(); 750 break; 751 case tok::code_completion: 752 Actions.CodeCompleteOrdinaryName(getCurScope(), 753 CurParsedObjCImpl? Sema::PCC_ObjCImplementation 754 : Sema::PCC_Namespace); 755 cutOffParsing(); 756 return DeclGroupPtrTy(); 757 case tok::kw_using: 758 case tok::kw_namespace: 759 case tok::kw_typedef: 760 case tok::kw_template: 761 case tok::kw_export: // As in 'export template' 762 case tok::kw_static_assert: 763 case tok::kw__Static_assert: 764 // A function definition cannot start with any of these keywords. 765 { 766 SourceLocation DeclEnd; 767 StmtVector Stmts; 768 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 769 } 770 771 case tok::kw_static: 772 // Parse (then ignore) 'static' prior to a template instantiation. This is 773 // a GCC extension that we intentionally do not support. 774 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) { 775 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 776 << 0; 777 SourceLocation DeclEnd; 778 StmtVector Stmts; 779 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 780 } 781 goto dont_know; 782 783 case tok::kw_inline: 784 if (getLangOpts().CPlusPlus) { 785 tok::TokenKind NextKind = NextToken().getKind(); 786 787 // Inline namespaces. Allowed as an extension even in C++03. 788 if (NextKind == tok::kw_namespace) { 789 SourceLocation DeclEnd; 790 StmtVector Stmts; 791 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 792 } 793 794 // Parse (then ignore) 'inline' prior to a template instantiation. This is 795 // a GCC extension that we intentionally do not support. 796 if (NextKind == tok::kw_template) { 797 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 798 << 1; 799 SourceLocation DeclEnd; 800 StmtVector Stmts; 801 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 802 } 803 } 804 goto dont_know; 805 806 case tok::kw_extern: 807 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) { 808 // Extern templates 809 SourceLocation ExternLoc = ConsumeToken(); 810 SourceLocation TemplateLoc = ConsumeToken(); 811 Diag(ExternLoc, getLangOpts().CPlusPlus11 ? 812 diag::warn_cxx98_compat_extern_template : 813 diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc); 814 SourceLocation DeclEnd; 815 return Actions.ConvertDeclToDeclGroup( 816 ParseExplicitInstantiation(Declarator::FileContext, 817 ExternLoc, TemplateLoc, DeclEnd)); 818 } 819 // FIXME: Detect C++ linkage specifications here? 820 goto dont_know; 821 822 case tok::kw___if_exists: 823 case tok::kw___if_not_exists: 824 ParseMicrosoftIfExistsExternalDeclaration(); 825 return DeclGroupPtrTy(); 826 827 default: 828 dont_know: 829 // We can't tell whether this is a function-definition or declaration yet. 830 return ParseDeclarationOrFunctionDefinition(attrs, DS); 831 } 832 833 // This routine returns a DeclGroup, if the thing we parsed only contains a 834 // single decl, convert it now. 835 return Actions.ConvertDeclToDeclGroup(SingleDecl); 836 } 837 838 /// \brief Determine whether the current token, if it occurs after a 839 /// declarator, continues a declaration or declaration list. 840 bool Parser::isDeclarationAfterDeclarator() { 841 // Check for '= delete' or '= default' 842 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) { 843 const Token &KW = NextToken(); 844 if (KW.is(tok::kw_default) || KW.is(tok::kw_delete)) 845 return false; 846 } 847 848 return Tok.is(tok::equal) || // int X()= -> not a function def 849 Tok.is(tok::comma) || // int X(), -> not a function def 850 Tok.is(tok::semi) || // int X(); -> not a function def 851 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def 852 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def 853 (getLangOpts().CPlusPlus && 854 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++] 855 } 856 857 /// \brief Determine whether the current token, if it occurs after a 858 /// declarator, indicates the start of a function definition. 859 bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) { 860 assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator"); 861 if (Tok.is(tok::l_brace)) // int X() {} 862 return true; 863 864 // Handle K&R C argument lists: int X(f) int f; {} 865 if (!getLangOpts().CPlusPlus && 866 Declarator.getFunctionTypeInfo().isKNRPrototype()) 867 return isDeclarationSpecifier(); 868 869 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) { 870 const Token &KW = NextToken(); 871 return KW.is(tok::kw_default) || KW.is(tok::kw_delete); 872 } 873 874 return Tok.is(tok::colon) || // X() : Base() {} (used for ctors) 875 Tok.is(tok::kw_try); // X() try { ... } 876 } 877 878 /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or 879 /// a declaration. We can't tell which we have until we read up to the 880 /// compound-statement in function-definition. TemplateParams, if 881 /// non-NULL, provides the template parameters when we're parsing a 882 /// C++ template-declaration. 883 /// 884 /// function-definition: [C99 6.9.1] 885 /// decl-specs declarator declaration-list[opt] compound-statement 886 /// [C90] function-definition: [C99 6.7.1] - implicit int result 887 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 888 /// 889 /// declaration: [C99 6.7] 890 /// declaration-specifiers init-declarator-list[opt] ';' 891 /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode] 892 /// [OMP] threadprivate-directive [TODO] 893 /// 894 Parser::DeclGroupPtrTy 895 Parser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs, 896 ParsingDeclSpec &DS, 897 AccessSpecifier AS) { 898 // Parse the common declaration-specifiers piece. 899 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level); 900 901 // If we had a free-standing type definition with a missing semicolon, we 902 // may get this far before the problem becomes obvious. 903 if (DS.hasTagDefinition() && 904 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_top_level)) 905 return DeclGroupPtrTy(); 906 907 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" 908 // declaration-specifiers init-declarator-list[opt] ';' 909 if (Tok.is(tok::semi)) { 910 ProhibitAttributes(attrs); 911 ConsumeToken(); 912 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); 913 DS.complete(TheDecl); 914 return Actions.ConvertDeclToDeclGroup(TheDecl); 915 } 916 917 DS.takeAttributesFrom(attrs); 918 919 // ObjC2 allows prefix attributes on class interfaces and protocols. 920 // FIXME: This still needs better diagnostics. We should only accept 921 // attributes here, no types, etc. 922 if (getLangOpts().ObjC2 && Tok.is(tok::at)) { 923 SourceLocation AtLoc = ConsumeToken(); // the "@" 924 if (!Tok.isObjCAtKeyword(tok::objc_interface) && 925 !Tok.isObjCAtKeyword(tok::objc_protocol)) { 926 Diag(Tok, diag::err_objc_unexpected_attr); 927 SkipUntil(tok::semi); // FIXME: better skip? 928 return DeclGroupPtrTy(); 929 } 930 931 DS.abort(); 932 933 const char *PrevSpec = 0; 934 unsigned DiagID; 935 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID, 936 Actions.getASTContext().getPrintingPolicy())) 937 Diag(AtLoc, DiagID) << PrevSpec; 938 939 if (Tok.isObjCAtKeyword(tok::objc_protocol)) 940 return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes()); 941 942 return Actions.ConvertDeclToDeclGroup( 943 ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes())); 944 } 945 946 // If the declspec consisted only of 'extern' and we have a string 947 // literal following it, this must be a C++ linkage specifier like 948 // 'extern "C"'. 949 if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus && 950 DS.getStorageClassSpec() == DeclSpec::SCS_extern && 951 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) { 952 Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext); 953 return Actions.ConvertDeclToDeclGroup(TheDecl); 954 } 955 956 return ParseDeclGroup(DS, Declarator::FileContext, true); 957 } 958 959 Parser::DeclGroupPtrTy 960 Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs, 961 ParsingDeclSpec *DS, 962 AccessSpecifier AS) { 963 if (DS) { 964 return ParseDeclOrFunctionDefInternal(attrs, *DS, AS); 965 } else { 966 ParsingDeclSpec PDS(*this); 967 // Must temporarily exit the objective-c container scope for 968 // parsing c constructs and re-enter objc container scope 969 // afterwards. 970 ObjCDeclContextSwitch ObjCDC(*this); 971 972 return ParseDeclOrFunctionDefInternal(attrs, PDS, AS); 973 } 974 } 975 976 977 static inline bool isFunctionDeclaratorRequiringReturnTypeDeduction( 978 const Declarator &D) { 979 if (!D.isFunctionDeclarator() || !D.getDeclSpec().containsPlaceholderType()) 980 return false; 981 for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) { 982 unsigned chunkIndex = E - I - 1; 983 const DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); 984 if (DeclType.Kind == DeclaratorChunk::Function) { 985 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; 986 if (!FTI.hasTrailingReturnType()) 987 return true; 988 QualType TrailingRetType = FTI.getTrailingReturnType().get(); 989 return TrailingRetType->getCanonicalTypeInternal() 990 ->getContainedAutoType(); 991 } 992 } 993 return false; 994 } 995 996 /// ParseFunctionDefinition - We parsed and verified that the specified 997 /// Declarator is well formed. If this is a K&R-style function, read the 998 /// parameters declaration-list, then start the compound-statement. 999 /// 1000 /// function-definition: [C99 6.9.1] 1001 /// decl-specs declarator declaration-list[opt] compound-statement 1002 /// [C90] function-definition: [C99 6.7.1] - implicit int result 1003 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 1004 /// [C++] function-definition: [C++ 8.4] 1005 /// decl-specifier-seq[opt] declarator ctor-initializer[opt] 1006 /// function-body 1007 /// [C++] function-definition: [C++ 8.4] 1008 /// decl-specifier-seq[opt] declarator function-try-block 1009 /// 1010 Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D, 1011 const ParsedTemplateInfo &TemplateInfo, 1012 LateParsedAttrList *LateParsedAttrs) { 1013 // Poison the SEH identifiers so they are flagged as illegal in function bodies 1014 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); 1015 const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 1016 1017 // If this is C90 and the declspecs were completely missing, fudge in an 1018 // implicit int. We do this here because this is the only place where 1019 // declaration-specifiers are completely optional in the grammar. 1020 if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) { 1021 const char *PrevSpec; 1022 unsigned DiagID; 1023 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); 1024 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int, 1025 D.getIdentifierLoc(), 1026 PrevSpec, DiagID, 1027 Policy); 1028 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin()); 1029 } 1030 1031 // If this declaration was formed with a K&R-style identifier list for the 1032 // arguments, parse declarations for all of the args next. 1033 // int foo(a,b) int a; float b; {} 1034 if (FTI.isKNRPrototype()) 1035 ParseKNRParamDeclarations(D); 1036 1037 // We should have either an opening brace or, in a C++ constructor, 1038 // we may have a colon. 1039 if (Tok.isNot(tok::l_brace) && 1040 (!getLangOpts().CPlusPlus || 1041 (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) && 1042 Tok.isNot(tok::equal)))) { 1043 Diag(Tok, diag::err_expected_fn_body); 1044 1045 // Skip over garbage, until we get to '{'. Don't eat the '{'. 1046 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch); 1047 1048 // If we didn't find the '{', bail out. 1049 if (Tok.isNot(tok::l_brace)) 1050 return 0; 1051 } 1052 1053 // Check to make sure that any normal attributes are allowed to be on 1054 // a definition. Late parsed attributes are checked at the end. 1055 if (Tok.isNot(tok::equal)) { 1056 AttributeList *DtorAttrs = D.getAttributes(); 1057 while (DtorAttrs) { 1058 if (DtorAttrs->isKnownToGCC() && 1059 !DtorAttrs->isCXX11Attribute()) { 1060 Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition) 1061 << DtorAttrs->getName(); 1062 } 1063 DtorAttrs = DtorAttrs->getNext(); 1064 } 1065 } 1066 1067 // In delayed template parsing mode, for function template we consume the 1068 // tokens and store them for late parsing at the end of the translation unit. 1069 if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) && 1070 TemplateInfo.Kind == ParsedTemplateInfo::Template && 1071 !D.getDeclSpec().isConstexprSpecified() && 1072 !isFunctionDeclaratorRequiringReturnTypeDeduction(D)) { 1073 MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams); 1074 1075 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); 1076 Scope *ParentScope = getCurScope()->getParent(); 1077 1078 D.setFunctionDefinitionKind(FDK_Definition); 1079 Decl *DP = Actions.HandleDeclarator(ParentScope, D, 1080 TemplateParameterLists); 1081 D.complete(DP); 1082 D.getMutableDeclSpec().abort(); 1083 1084 CachedTokens Toks; 1085 LexTemplateFunctionForLateParsing(Toks); 1086 1087 if (DP) { 1088 FunctionDecl *FnD = DP->getAsFunction(); 1089 Actions.CheckForFunctionRedefinition(FnD); 1090 Actions.MarkAsLateParsedTemplate(FnD, DP, Toks); 1091 } 1092 return DP; 1093 } 1094 else if (CurParsedObjCImpl && 1095 !TemplateInfo.TemplateParams && 1096 (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) || 1097 Tok.is(tok::colon)) && 1098 Actions.CurContext->isTranslationUnit()) { 1099 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); 1100 Scope *ParentScope = getCurScope()->getParent(); 1101 1102 D.setFunctionDefinitionKind(FDK_Definition); 1103 Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D, 1104 MultiTemplateParamsArg()); 1105 D.complete(FuncDecl); 1106 D.getMutableDeclSpec().abort(); 1107 if (FuncDecl) { 1108 // Consume the tokens and store them for later parsing. 1109 StashAwayMethodOrFunctionBodyTokens(FuncDecl); 1110 CurParsedObjCImpl->HasCFunction = true; 1111 return FuncDecl; 1112 } 1113 } 1114 1115 // Enter a scope for the function body. 1116 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); 1117 1118 // Tell the actions module that we have entered a function definition with the 1119 // specified Declarator for the function. 1120 Decl *Res = TemplateInfo.TemplateParams? 1121 Actions.ActOnStartOfFunctionTemplateDef(getCurScope(), 1122 *TemplateInfo.TemplateParams, D) 1123 : Actions.ActOnStartOfFunctionDef(getCurScope(), D); 1124 1125 // Break out of the ParsingDeclarator context before we parse the body. 1126 D.complete(Res); 1127 1128 // Break out of the ParsingDeclSpec context, too. This const_cast is 1129 // safe because we're always the sole owner. 1130 D.getMutableDeclSpec().abort(); 1131 1132 if (TryConsumeToken(tok::equal)) { 1133 assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='"); 1134 Actions.ActOnFinishFunctionBody(Res, 0, false); 1135 1136 bool Delete = false; 1137 SourceLocation KWLoc; 1138 if (TryConsumeToken(tok::kw_delete, KWLoc)) { 1139 Diag(KWLoc, getLangOpts().CPlusPlus11 1140 ? diag::warn_cxx98_compat_deleted_function 1141 : diag::ext_deleted_function); 1142 Actions.SetDeclDeleted(Res, KWLoc); 1143 Delete = true; 1144 } else if (TryConsumeToken(tok::kw_default, KWLoc)) { 1145 Diag(KWLoc, getLangOpts().CPlusPlus11 1146 ? diag::warn_cxx98_compat_defaulted_function 1147 : diag::ext_defaulted_function); 1148 Actions.SetDeclDefaulted(Res, KWLoc); 1149 } else { 1150 llvm_unreachable("function definition after = not 'delete' or 'default'"); 1151 } 1152 1153 if (Tok.is(tok::comma)) { 1154 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 1155 << Delete; 1156 SkipUntil(tok::semi); 1157 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after, 1158 Delete ? "delete" : "default")) { 1159 SkipUntil(tok::semi); 1160 } 1161 1162 return Res; 1163 } 1164 1165 if (Tok.is(tok::kw_try)) 1166 return ParseFunctionTryBlock(Res, BodyScope); 1167 1168 // If we have a colon, then we're probably parsing a C++ 1169 // ctor-initializer. 1170 if (Tok.is(tok::colon)) { 1171 ParseConstructorInitializer(Res); 1172 1173 // Recover from error. 1174 if (!Tok.is(tok::l_brace)) { 1175 BodyScope.Exit(); 1176 Actions.ActOnFinishFunctionBody(Res, 0); 1177 return Res; 1178 } 1179 } else 1180 Actions.ActOnDefaultCtorInitializers(Res); 1181 1182 // Late attributes are parsed in the same scope as the function body. 1183 if (LateParsedAttrs) 1184 ParseLexedAttributeList(*LateParsedAttrs, Res, false, true); 1185 1186 return ParseFunctionStatementBody(Res, BodyScope); 1187 } 1188 1189 /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides 1190 /// types for a function with a K&R-style identifier list for arguments. 1191 void Parser::ParseKNRParamDeclarations(Declarator &D) { 1192 // We know that the top-level of this declarator is a function. 1193 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 1194 1195 // Enter function-declaration scope, limiting any declarators to the 1196 // function prototype scope, including parameter declarators. 1197 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | 1198 Scope::FunctionDeclarationScope | Scope::DeclScope); 1199 1200 // Read all the argument declarations. 1201 while (isDeclarationSpecifier()) { 1202 SourceLocation DSStart = Tok.getLocation(); 1203 1204 // Parse the common declaration-specifiers piece. 1205 DeclSpec DS(AttrFactory); 1206 ParseDeclarationSpecifiers(DS); 1207 1208 // C99 6.9.1p6: 'each declaration in the declaration list shall have at 1209 // least one declarator'. 1210 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with 1211 // the declarations though. It's trivial to ignore them, really hard to do 1212 // anything else with them. 1213 if (TryConsumeToken(tok::semi)) { 1214 Diag(DSStart, diag::err_declaration_does_not_declare_param); 1215 continue; 1216 } 1217 1218 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other 1219 // than register. 1220 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && 1221 DS.getStorageClassSpec() != DeclSpec::SCS_register) { 1222 Diag(DS.getStorageClassSpecLoc(), 1223 diag::err_invalid_storage_class_in_func_decl); 1224 DS.ClearStorageClassSpecs(); 1225 } 1226 if (DS.getThreadStorageClassSpec() != DeclSpec::TSCS_unspecified) { 1227 Diag(DS.getThreadStorageClassSpecLoc(), 1228 diag::err_invalid_storage_class_in_func_decl); 1229 DS.ClearStorageClassSpecs(); 1230 } 1231 1232 // Parse the first declarator attached to this declspec. 1233 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext); 1234 ParseDeclarator(ParmDeclarator); 1235 1236 // Handle the full declarator list. 1237 while (1) { 1238 // If attributes are present, parse them. 1239 MaybeParseGNUAttributes(ParmDeclarator); 1240 1241 // Ask the actions module to compute the type for this declarator. 1242 Decl *Param = 1243 Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); 1244 1245 if (Param && 1246 // A missing identifier has already been diagnosed. 1247 ParmDeclarator.getIdentifier()) { 1248 1249 // Scan the argument list looking for the correct param to apply this 1250 // type. 1251 for (unsigned i = 0; ; ++i) { 1252 // C99 6.9.1p6: those declarators shall declare only identifiers from 1253 // the identifier list. 1254 if (i == FTI.NumArgs) { 1255 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param) 1256 << ParmDeclarator.getIdentifier(); 1257 break; 1258 } 1259 1260 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) { 1261 // Reject redefinitions of parameters. 1262 if (FTI.ArgInfo[i].Param) { 1263 Diag(ParmDeclarator.getIdentifierLoc(), 1264 diag::err_param_redefinition) 1265 << ParmDeclarator.getIdentifier(); 1266 } else { 1267 FTI.ArgInfo[i].Param = Param; 1268 } 1269 break; 1270 } 1271 } 1272 } 1273 1274 // If we don't have a comma, it is either the end of the list (a ';') or 1275 // an error, bail out. 1276 if (Tok.isNot(tok::comma)) 1277 break; 1278 1279 ParmDeclarator.clear(); 1280 1281 // Consume the comma. 1282 ParmDeclarator.setCommaLoc(ConsumeToken()); 1283 1284 // Parse the next declarator. 1285 ParseDeclarator(ParmDeclarator); 1286 } 1287 1288 // Consume ';' and continue parsing. 1289 if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration)) 1290 continue; 1291 1292 // Otherwise recover by skipping to next semi or mandatory function body. 1293 if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch)) 1294 break; 1295 TryConsumeToken(tok::semi); 1296 } 1297 1298 // The actions module must verify that all arguments were declared. 1299 Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation()); 1300 } 1301 1302 1303 /// ParseAsmStringLiteral - This is just a normal string-literal, but is not 1304 /// allowed to be a wide string, and is not subject to character translation. 1305 /// 1306 /// [GNU] asm-string-literal: 1307 /// string-literal 1308 /// 1309 Parser::ExprResult Parser::ParseAsmStringLiteral() { 1310 switch (Tok.getKind()) { 1311 case tok::string_literal: 1312 break; 1313 case tok::utf8_string_literal: 1314 case tok::utf16_string_literal: 1315 case tok::utf32_string_literal: 1316 case tok::wide_string_literal: { 1317 SourceLocation L = Tok.getLocation(); 1318 Diag(Tok, diag::err_asm_operand_wide_string_literal) 1319 << (Tok.getKind() == tok::wide_string_literal) 1320 << SourceRange(L, L); 1321 return ExprError(); 1322 } 1323 default: 1324 Diag(Tok, diag::err_expected_string_literal) 1325 << /*Source='in...'*/0 << "'asm'"; 1326 return ExprError(); 1327 } 1328 1329 return ParseStringLiteralExpression(); 1330 } 1331 1332 /// ParseSimpleAsm 1333 /// 1334 /// [GNU] simple-asm-expr: 1335 /// 'asm' '(' asm-string-literal ')' 1336 /// 1337 Parser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) { 1338 assert(Tok.is(tok::kw_asm) && "Not an asm!"); 1339 SourceLocation Loc = ConsumeToken(); 1340 1341 if (Tok.is(tok::kw_volatile)) { 1342 // Remove from the end of 'asm' to the end of 'volatile'. 1343 SourceRange RemovalRange(PP.getLocForEndOfToken(Loc), 1344 PP.getLocForEndOfToken(Tok.getLocation())); 1345 1346 Diag(Tok, diag::warn_file_asm_volatile) 1347 << FixItHint::CreateRemoval(RemovalRange); 1348 ConsumeToken(); 1349 } 1350 1351 BalancedDelimiterTracker T(*this, tok::l_paren); 1352 if (T.consumeOpen()) { 1353 Diag(Tok, diag::err_expected_lparen_after) << "asm"; 1354 return ExprError(); 1355 } 1356 1357 ExprResult Result(ParseAsmStringLiteral()); 1358 1359 if (!Result.isInvalid()) { 1360 // Close the paren and get the location of the end bracket 1361 T.consumeClose(); 1362 if (EndLoc) 1363 *EndLoc = T.getCloseLocation(); 1364 } else if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) { 1365 if (EndLoc) 1366 *EndLoc = Tok.getLocation(); 1367 ConsumeParen(); 1368 } 1369 1370 return Result; 1371 } 1372 1373 /// \brief Get the TemplateIdAnnotation from the token and put it in the 1374 /// cleanup pool so that it gets destroyed when parsing the current top level 1375 /// declaration is finished. 1376 TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) { 1377 assert(tok.is(tok::annot_template_id) && "Expected template-id token"); 1378 TemplateIdAnnotation * 1379 Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue()); 1380 return Id; 1381 } 1382 1383 void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) { 1384 // Push the current token back into the token stream (or revert it if it is 1385 // cached) and use an annotation scope token for current token. 1386 if (PP.isBacktrackEnabled()) 1387 PP.RevertCachedTokens(1); 1388 else 1389 PP.EnterToken(Tok); 1390 Tok.setKind(tok::annot_cxxscope); 1391 Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS)); 1392 Tok.setAnnotationRange(SS.getRange()); 1393 1394 // In case the tokens were cached, have Preprocessor replace them 1395 // with the annotation token. We don't need to do this if we've 1396 // just reverted back to a prior state. 1397 if (IsNewAnnotation) 1398 PP.AnnotateCachedTokens(Tok); 1399 } 1400 1401 /// \brief Attempt to classify the name at the current token position. This may 1402 /// form a type, scope or primary expression annotation, or replace the token 1403 /// with a typo-corrected keyword. This is only appropriate when the current 1404 /// name must refer to an entity which has already been declared. 1405 /// 1406 /// \param IsAddressOfOperand Must be \c true if the name is preceded by an '&' 1407 /// and might possibly have a dependent nested name specifier. 1408 /// \param CCC Indicates how to perform typo-correction for this name. If NULL, 1409 /// no typo correction will be performed. 1410 Parser::AnnotatedNameKind 1411 Parser::TryAnnotateName(bool IsAddressOfOperand, 1412 CorrectionCandidateCallback *CCC) { 1413 assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope)); 1414 1415 const bool EnteringContext = false; 1416 const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); 1417 1418 CXXScopeSpec SS; 1419 if (getLangOpts().CPlusPlus && 1420 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 1421 return ANK_Error; 1422 1423 if (Tok.isNot(tok::identifier) || SS.isInvalid()) { 1424 if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS, 1425 !WasScopeAnnotation)) 1426 return ANK_Error; 1427 return ANK_Unresolved; 1428 } 1429 1430 IdentifierInfo *Name = Tok.getIdentifierInfo(); 1431 SourceLocation NameLoc = Tok.getLocation(); 1432 1433 // FIXME: Move the tentative declaration logic into ClassifyName so we can 1434 // typo-correct to tentatively-declared identifiers. 1435 if (isTentativelyDeclared(Name)) { 1436 // Identifier has been tentatively declared, and thus cannot be resolved as 1437 // an expression. Fall back to annotating it as a type. 1438 if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS, 1439 !WasScopeAnnotation)) 1440 return ANK_Error; 1441 return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl; 1442 } 1443 1444 Token Next = NextToken(); 1445 1446 // Look up and classify the identifier. We don't perform any typo-correction 1447 // after a scope specifier, because in general we can't recover from typos 1448 // there (eg, after correcting 'A::tempalte B<X>::C' [sic], we would need to 1449 // jump back into scope specifier parsing). 1450 Sema::NameClassification Classification 1451 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next, 1452 IsAddressOfOperand, SS.isEmpty() ? CCC : 0); 1453 1454 switch (Classification.getKind()) { 1455 case Sema::NC_Error: 1456 return ANK_Error; 1457 1458 case Sema::NC_Keyword: 1459 // The identifier was typo-corrected to a keyword. 1460 Tok.setIdentifierInfo(Name); 1461 Tok.setKind(Name->getTokenID()); 1462 PP.TypoCorrectToken(Tok); 1463 if (SS.isNotEmpty()) 1464 AnnotateScopeToken(SS, !WasScopeAnnotation); 1465 // We've "annotated" this as a keyword. 1466 return ANK_Success; 1467 1468 case Sema::NC_Unknown: 1469 // It's not something we know about. Leave it unannotated. 1470 break; 1471 1472 case Sema::NC_Type: 1473 Tok.setKind(tok::annot_typename); 1474 setTypeAnnotation(Tok, Classification.getType()); 1475 Tok.setAnnotationEndLoc(NameLoc); 1476 if (SS.isNotEmpty()) 1477 Tok.setLocation(SS.getBeginLoc()); 1478 PP.AnnotateCachedTokens(Tok); 1479 return ANK_Success; 1480 1481 case Sema::NC_Expression: 1482 Tok.setKind(tok::annot_primary_expr); 1483 setExprAnnotation(Tok, Classification.getExpression()); 1484 Tok.setAnnotationEndLoc(NameLoc); 1485 if (SS.isNotEmpty()) 1486 Tok.setLocation(SS.getBeginLoc()); 1487 PP.AnnotateCachedTokens(Tok); 1488 return ANK_Success; 1489 1490 case Sema::NC_TypeTemplate: 1491 if (Next.isNot(tok::less)) { 1492 // This may be a type template being used as a template template argument. 1493 if (SS.isNotEmpty()) 1494 AnnotateScopeToken(SS, !WasScopeAnnotation); 1495 return ANK_TemplateName; 1496 } 1497 // Fall through. 1498 case Sema::NC_VarTemplate: 1499 case Sema::NC_FunctionTemplate: { 1500 // We have a type, variable or function template followed by '<'. 1501 ConsumeToken(); 1502 UnqualifiedId Id; 1503 Id.setIdentifier(Name, NameLoc); 1504 if (AnnotateTemplateIdToken( 1505 TemplateTy::make(Classification.getTemplateName()), 1506 Classification.getTemplateNameKind(), SS, SourceLocation(), Id)) 1507 return ANK_Error; 1508 return ANK_Success; 1509 } 1510 1511 case Sema::NC_NestedNameSpecifier: 1512 llvm_unreachable("already parsed nested name specifier"); 1513 } 1514 1515 // Unable to classify the name, but maybe we can annotate a scope specifier. 1516 if (SS.isNotEmpty()) 1517 AnnotateScopeToken(SS, !WasScopeAnnotation); 1518 return ANK_Unresolved; 1519 } 1520 1521 bool Parser::TryKeywordIdentFallback(bool DisableKeyword) { 1522 assert(!Tok.is(tok::identifier) && !Tok.isAnnotation()); 1523 Diag(Tok, diag::ext_keyword_as_ident) 1524 << PP.getSpelling(Tok) 1525 << DisableKeyword; 1526 if (DisableKeyword) { 1527 IdentifierInfo *II = Tok.getIdentifierInfo(); 1528 ContextualKeywords[II] = Tok.getKind(); 1529 II->RevertTokenIDToIdentifier(); 1530 } 1531 Tok.setKind(tok::identifier); 1532 return true; 1533 } 1534 1535 bool Parser::TryIdentKeywordUpgrade() { 1536 assert(Tok.is(tok::identifier)); 1537 const IdentifierInfo *II = Tok.getIdentifierInfo(); 1538 assert(II->hasRevertedTokenIDToIdentifier()); 1539 // If we find that this is in fact the name of a type trait, 1540 // update the token kind in place and parse again to treat it as 1541 // the appropriate kind of type trait. 1542 llvm::SmallDenseMap<const IdentifierInfo *, tok::TokenKind>::iterator Known = 1543 ContextualKeywords.find(II); 1544 if (Known == ContextualKeywords.end()) 1545 return false; 1546 Tok.setKind(Known->second); 1547 return true; 1548 } 1549 1550 /// TryAnnotateTypeOrScopeToken - If the current token position is on a 1551 /// typename (possibly qualified in C++) or a C++ scope specifier not followed 1552 /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens 1553 /// with a single annotation token representing the typename or C++ scope 1554 /// respectively. 1555 /// This simplifies handling of C++ scope specifiers and allows efficient 1556 /// backtracking without the need to re-parse and resolve nested-names and 1557 /// typenames. 1558 /// It will mainly be called when we expect to treat identifiers as typenames 1559 /// (if they are typenames). For example, in C we do not expect identifiers 1560 /// inside expressions to be treated as typenames so it will not be called 1561 /// for expressions in C. 1562 /// The benefit for C/ObjC is that a typename will be annotated and 1563 /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName 1564 /// will not be called twice, once to check whether we have a declaration 1565 /// specifier, and another one to get the actual type inside 1566 /// ParseDeclarationSpecifiers). 1567 /// 1568 /// This returns true if an error occurred. 1569 /// 1570 /// Note that this routine emits an error if you call it with ::new or ::delete 1571 /// as the current tokens, so only call it in contexts where these are invalid. 1572 bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) { 1573 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) 1574 || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) 1575 || Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) 1576 && "Cannot be a type or scope token!"); 1577 1578 if (Tok.is(tok::kw_typename)) { 1579 // MSVC lets you do stuff like: 1580 // typename typedef T_::D D; 1581 // 1582 // We will consume the typedef token here and put it back after we have 1583 // parsed the first identifier, transforming it into something more like: 1584 // typename T_::D typedef D; 1585 if (getLangOpts().MSVCCompat && NextToken().is(tok::kw_typedef)) { 1586 Token TypedefToken; 1587 PP.Lex(TypedefToken); 1588 bool Result = TryAnnotateTypeOrScopeToken(EnteringContext, NeedType); 1589 PP.EnterToken(Tok); 1590 Tok = TypedefToken; 1591 if (!Result) 1592 Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename); 1593 return Result; 1594 } 1595 1596 // Parse a C++ typename-specifier, e.g., "typename T::type". 1597 // 1598 // typename-specifier: 1599 // 'typename' '::' [opt] nested-name-specifier identifier 1600 // 'typename' '::' [opt] nested-name-specifier template [opt] 1601 // simple-template-id 1602 SourceLocation TypenameLoc = ConsumeToken(); 1603 CXXScopeSpec SS; 1604 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(), 1605 /*EnteringContext=*/false, 1606 0, /*IsTypename*/true)) 1607 return true; 1608 if (!SS.isSet()) { 1609 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) || 1610 Tok.is(tok::annot_decltype)) { 1611 // Attempt to recover by skipping the invalid 'typename' 1612 if (Tok.is(tok::annot_decltype) || 1613 (!TryAnnotateTypeOrScopeToken(EnteringContext, NeedType) && 1614 Tok.isAnnotation())) { 1615 unsigned DiagID = diag::err_expected_qualified_after_typename; 1616 // MS compatibility: MSVC permits using known types with typename. 1617 // e.g. "typedef typename T* pointer_type" 1618 if (getLangOpts().MicrosoftExt) 1619 DiagID = diag::warn_expected_qualified_after_typename; 1620 Diag(Tok.getLocation(), DiagID); 1621 return false; 1622 } 1623 } 1624 1625 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename); 1626 return true; 1627 } 1628 1629 TypeResult Ty; 1630 if (Tok.is(tok::identifier)) { 1631 // FIXME: check whether the next token is '<', first! 1632 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 1633 *Tok.getIdentifierInfo(), 1634 Tok.getLocation()); 1635 } else if (Tok.is(tok::annot_template_id)) { 1636 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1637 if (TemplateId->Kind != TNK_Type_template && 1638 TemplateId->Kind != TNK_Dependent_template_name) { 1639 Diag(Tok, diag::err_typename_refers_to_non_type_template) 1640 << Tok.getAnnotationRange(); 1641 return true; 1642 } 1643 1644 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), 1645 TemplateId->NumArgs); 1646 1647 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 1648 TemplateId->TemplateKWLoc, 1649 TemplateId->Template, 1650 TemplateId->TemplateNameLoc, 1651 TemplateId->LAngleLoc, 1652 TemplateArgsPtr, 1653 TemplateId->RAngleLoc); 1654 } else { 1655 Diag(Tok, diag::err_expected_type_name_after_typename) 1656 << SS.getRange(); 1657 return true; 1658 } 1659 1660 SourceLocation EndLoc = Tok.getLastLoc(); 1661 Tok.setKind(tok::annot_typename); 1662 setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get()); 1663 Tok.setAnnotationEndLoc(EndLoc); 1664 Tok.setLocation(TypenameLoc); 1665 PP.AnnotateCachedTokens(Tok); 1666 return false; 1667 } 1668 1669 // Remembers whether the token was originally a scope annotation. 1670 bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); 1671 1672 CXXScopeSpec SS; 1673 if (getLangOpts().CPlusPlus) 1674 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 1675 return true; 1676 1677 return TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, NeedType, 1678 SS, !WasScopeAnnotation); 1679 } 1680 1681 /// \brief Try to annotate a type or scope token, having already parsed an 1682 /// optional scope specifier. \p IsNewScope should be \c true unless the scope 1683 /// specifier was extracted from an existing tok::annot_cxxscope annotation. 1684 bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(bool EnteringContext, 1685 bool NeedType, 1686 CXXScopeSpec &SS, 1687 bool IsNewScope) { 1688 if (Tok.is(tok::identifier)) { 1689 IdentifierInfo *CorrectedII = 0; 1690 // Determine whether the identifier is a type name. 1691 if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(), 1692 Tok.getLocation(), getCurScope(), 1693 &SS, false, 1694 NextToken().is(tok::period), 1695 ParsedType(), 1696 /*IsCtorOrDtorName=*/false, 1697 /*NonTrivialTypeSourceInfo*/true, 1698 NeedType ? &CorrectedII : NULL)) { 1699 // A FixIt was applied as a result of typo correction 1700 if (CorrectedII) 1701 Tok.setIdentifierInfo(CorrectedII); 1702 // This is a typename. Replace the current token in-place with an 1703 // annotation type token. 1704 Tok.setKind(tok::annot_typename); 1705 setTypeAnnotation(Tok, Ty); 1706 Tok.setAnnotationEndLoc(Tok.getLocation()); 1707 if (SS.isNotEmpty()) // it was a C++ qualified type name. 1708 Tok.setLocation(SS.getBeginLoc()); 1709 1710 // In case the tokens were cached, have Preprocessor replace 1711 // them with the annotation token. 1712 PP.AnnotateCachedTokens(Tok); 1713 return false; 1714 } 1715 1716 if (!getLangOpts().CPlusPlus) { 1717 // If we're in C, we can't have :: tokens at all (the lexer won't return 1718 // them). If the identifier is not a type, then it can't be scope either, 1719 // just early exit. 1720 return false; 1721 } 1722 1723 // If this is a template-id, annotate with a template-id or type token. 1724 if (NextToken().is(tok::less)) { 1725 TemplateTy Template; 1726 UnqualifiedId TemplateName; 1727 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 1728 bool MemberOfUnknownSpecialization; 1729 if (TemplateNameKind TNK 1730 = Actions.isTemplateName(getCurScope(), SS, 1731 /*hasTemplateKeyword=*/false, TemplateName, 1732 /*ObjectType=*/ ParsedType(), 1733 EnteringContext, 1734 Template, MemberOfUnknownSpecialization)) { 1735 // Consume the identifier. 1736 ConsumeToken(); 1737 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), 1738 TemplateName)) { 1739 // If an unrecoverable error occurred, we need to return true here, 1740 // because the token stream is in a damaged state. We may not return 1741 // a valid identifier. 1742 return true; 1743 } 1744 } 1745 } 1746 1747 // The current token, which is either an identifier or a 1748 // template-id, is not part of the annotation. Fall through to 1749 // push that token back into the stream and complete the C++ scope 1750 // specifier annotation. 1751 } 1752 1753 if (Tok.is(tok::annot_template_id)) { 1754 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1755 if (TemplateId->Kind == TNK_Type_template) { 1756 // A template-id that refers to a type was parsed into a 1757 // template-id annotation in a context where we weren't allowed 1758 // to produce a type annotation token. Update the template-id 1759 // annotation token to a type annotation token now. 1760 AnnotateTemplateIdTokenAsType(); 1761 return false; 1762 } 1763 } 1764 1765 if (SS.isEmpty()) 1766 return false; 1767 1768 // A C++ scope specifier that isn't followed by a typename. 1769 AnnotateScopeToken(SS, IsNewScope); 1770 return false; 1771 } 1772 1773 /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only 1774 /// annotates C++ scope specifiers and template-ids. This returns 1775 /// true if there was an error that could not be recovered from. 1776 /// 1777 /// Note that this routine emits an error if you call it with ::new or ::delete 1778 /// as the current tokens, so only call it in contexts where these are invalid. 1779 bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) { 1780 assert(getLangOpts().CPlusPlus && 1781 "Call sites of this function should be guarded by checking for C++"); 1782 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || 1783 (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) || 1784 Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!"); 1785 1786 CXXScopeSpec SS; 1787 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 1788 return true; 1789 if (SS.isEmpty()) 1790 return false; 1791 1792 AnnotateScopeToken(SS, true); 1793 return false; 1794 } 1795 1796 bool Parser::isTokenEqualOrEqualTypo() { 1797 tok::TokenKind Kind = Tok.getKind(); 1798 switch (Kind) { 1799 default: 1800 return false; 1801 case tok::ampequal: // &= 1802 case tok::starequal: // *= 1803 case tok::plusequal: // += 1804 case tok::minusequal: // -= 1805 case tok::exclaimequal: // != 1806 case tok::slashequal: // /= 1807 case tok::percentequal: // %= 1808 case tok::lessequal: // <= 1809 case tok::lesslessequal: // <<= 1810 case tok::greaterequal: // >= 1811 case tok::greatergreaterequal: // >>= 1812 case tok::caretequal: // ^= 1813 case tok::pipeequal: // |= 1814 case tok::equalequal: // == 1815 Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal) 1816 << Kind 1817 << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "="); 1818 case tok::equal: 1819 return true; 1820 } 1821 } 1822 1823 SourceLocation Parser::handleUnexpectedCodeCompletionToken() { 1824 assert(Tok.is(tok::code_completion)); 1825 PrevTokLocation = Tok.getLocation(); 1826 1827 for (Scope *S = getCurScope(); S; S = S->getParent()) { 1828 if (S->getFlags() & Scope::FnScope) { 1829 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction); 1830 cutOffParsing(); 1831 return PrevTokLocation; 1832 } 1833 1834 if (S->getFlags() & Scope::ClassScope) { 1835 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class); 1836 cutOffParsing(); 1837 return PrevTokLocation; 1838 } 1839 } 1840 1841 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace); 1842 cutOffParsing(); 1843 return PrevTokLocation; 1844 } 1845 1846 // Anchor the Parser::FieldCallback vtable to this translation unit. 1847 // We use a spurious method instead of the destructor because 1848 // destroying FieldCallbacks can actually be slightly 1849 // performance-sensitive. 1850 void Parser::FieldCallback::_anchor() { 1851 } 1852 1853 // Code-completion pass-through functions 1854 1855 void Parser::CodeCompleteDirective(bool InConditional) { 1856 Actions.CodeCompletePreprocessorDirective(InConditional); 1857 } 1858 1859 void Parser::CodeCompleteInConditionalExclusion() { 1860 Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope()); 1861 } 1862 1863 void Parser::CodeCompleteMacroName(bool IsDefinition) { 1864 Actions.CodeCompletePreprocessorMacroName(IsDefinition); 1865 } 1866 1867 void Parser::CodeCompletePreprocessorExpression() { 1868 Actions.CodeCompletePreprocessorExpression(); 1869 } 1870 1871 void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro, 1872 MacroInfo *MacroInfo, 1873 unsigned ArgumentIndex) { 1874 Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo, 1875 ArgumentIndex); 1876 } 1877 1878 void Parser::CodeCompleteNaturalLanguage() { 1879 Actions.CodeCompleteNaturalLanguage(); 1880 } 1881 1882 bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) { 1883 assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) && 1884 "Expected '__if_exists' or '__if_not_exists'"); 1885 Result.IsIfExists = Tok.is(tok::kw___if_exists); 1886 Result.KeywordLoc = ConsumeToken(); 1887 1888 BalancedDelimiterTracker T(*this, tok::l_paren); 1889 if (T.consumeOpen()) { 1890 Diag(Tok, diag::err_expected_lparen_after) 1891 << (Result.IsIfExists? "__if_exists" : "__if_not_exists"); 1892 return true; 1893 } 1894 1895 // Parse nested-name-specifier. 1896 ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(), 1897 /*EnteringContext=*/false); 1898 1899 // Check nested-name specifier. 1900 if (Result.SS.isInvalid()) { 1901 T.skipToEnd(); 1902 return true; 1903 } 1904 1905 // Parse the unqualified-id. 1906 SourceLocation TemplateKWLoc; // FIXME: parsed, but unused. 1907 if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(), 1908 TemplateKWLoc, Result.Name)) { 1909 T.skipToEnd(); 1910 return true; 1911 } 1912 1913 if (T.consumeClose()) 1914 return true; 1915 1916 // Check if the symbol exists. 1917 switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc, 1918 Result.IsIfExists, Result.SS, 1919 Result.Name)) { 1920 case Sema::IER_Exists: 1921 Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip; 1922 break; 1923 1924 case Sema::IER_DoesNotExist: 1925 Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip; 1926 break; 1927 1928 case Sema::IER_Dependent: 1929 Result.Behavior = IEB_Dependent; 1930 break; 1931 1932 case Sema::IER_Error: 1933 return true; 1934 } 1935 1936 return false; 1937 } 1938 1939 void Parser::ParseMicrosoftIfExistsExternalDeclaration() { 1940 IfExistsCondition Result; 1941 if (ParseMicrosoftIfExistsCondition(Result)) 1942 return; 1943 1944 BalancedDelimiterTracker Braces(*this, tok::l_brace); 1945 if (Braces.consumeOpen()) { 1946 Diag(Tok, diag::err_expected) << tok::l_brace; 1947 return; 1948 } 1949 1950 switch (Result.Behavior) { 1951 case IEB_Parse: 1952 // Parse declarations below. 1953 break; 1954 1955 case IEB_Dependent: 1956 llvm_unreachable("Cannot have a dependent external declaration"); 1957 1958 case IEB_Skip: 1959 Braces.skipToEnd(); 1960 return; 1961 } 1962 1963 // Parse the declarations. 1964 // FIXME: Support module import within __if_exists? 1965 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { 1966 ParsedAttributesWithRange attrs(AttrFactory); 1967 MaybeParseCXX11Attributes(attrs); 1968 MaybeParseMicrosoftAttributes(attrs); 1969 DeclGroupPtrTy Result = ParseExternalDeclaration(attrs); 1970 if (Result && !getCurScope()->getParent()) 1971 Actions.getASTConsumer().HandleTopLevelDecl(Result.get()); 1972 } 1973 Braces.consumeClose(); 1974 } 1975 1976 Parser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) { 1977 assert(Tok.isObjCAtKeyword(tok::objc_import) && 1978 "Improper start to module import"); 1979 SourceLocation ImportLoc = ConsumeToken(); 1980 1981 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; 1982 1983 // Parse the module path. 1984 do { 1985 if (!Tok.is(tok::identifier)) { 1986 if (Tok.is(tok::code_completion)) { 1987 Actions.CodeCompleteModuleImport(ImportLoc, Path); 1988 ConsumeCodeCompletionToken(); 1989 SkipUntil(tok::semi); 1990 return DeclGroupPtrTy(); 1991 } 1992 1993 Diag(Tok, diag::err_module_expected_ident); 1994 SkipUntil(tok::semi); 1995 return DeclGroupPtrTy(); 1996 } 1997 1998 // Record this part of the module path. 1999 Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation())); 2000 ConsumeToken(); 2001 2002 if (Tok.is(tok::period)) { 2003 ConsumeToken(); 2004 continue; 2005 } 2006 2007 break; 2008 } while (true); 2009 2010 if (PP.hadModuleLoaderFatalFailure()) { 2011 // With a fatal failure in the module loader, we abort parsing. 2012 cutOffParsing(); 2013 return DeclGroupPtrTy(); 2014 } 2015 2016 DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path); 2017 ExpectAndConsumeSemi(diag::err_module_expected_semi); 2018 if (Import.isInvalid()) 2019 return DeclGroupPtrTy(); 2020 2021 return Actions.ConvertDeclToDeclGroup(Import.get()); 2022 } 2023 2024 bool BalancedDelimiterTracker::diagnoseOverflow() { 2025 P.Diag(P.Tok, diag::err_bracket_depth_exceeded) 2026 << P.getLangOpts().BracketDepth; 2027 P.Diag(P.Tok, diag::note_bracket_depth); 2028 P.cutOffParsing(); 2029 return true; 2030 } 2031 2032 bool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID, 2033 const char *Msg, 2034 tok::TokenKind SkipToTok) { 2035 LOpen = P.Tok.getLocation(); 2036 if (P.ExpectAndConsume(Kind, DiagID, Msg)) { 2037 if (SkipToTok != tok::unknown) 2038 P.SkipUntil(SkipToTok, Parser::StopAtSemi); 2039 return true; 2040 } 2041 2042 if (getDepth() < MaxDepth) 2043 return false; 2044 2045 return diagnoseOverflow(); 2046 } 2047 2048 bool BalancedDelimiterTracker::diagnoseMissingClose() { 2049 assert(!P.Tok.is(Close) && "Should have consumed closing delimiter"); 2050 2051 P.Diag(P.Tok, diag::err_expected) << Close; 2052 P.Diag(LOpen, diag::note_matching) << Kind; 2053 2054 // If we're not already at some kind of closing bracket, skip to our closing 2055 // token. 2056 if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) && 2057 P.Tok.isNot(tok::r_square) && 2058 P.SkipUntil(Close, FinalToken, 2059 Parser::StopAtSemi | Parser::StopBeforeMatch) && 2060 P.Tok.is(Close)) 2061 LClose = P.ConsumeAnyToken(); 2062 return true; 2063 } 2064 2065 void BalancedDelimiterTracker::skipToEnd() { 2066 P.SkipUntil(Close, Parser::StopBeforeMatch); 2067 consumeClose(); 2068 } 2069