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 "clang/Parse/ParseDiagnostic.h" 16 #include "clang/Sema/DeclSpec.h" 17 #include "clang/Sema/Scope.h" 18 #include "clang/Sema/ParsedTemplate.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include "RAIIObjectsForParser.h" 21 #include "ParsePragma.h" 22 #include "clang/AST/DeclTemplate.h" 23 #include "clang/AST/ASTConsumer.h" 24 using namespace clang; 25 26 Parser::Parser(Preprocessor &pp, Sema &actions) 27 : PP(pp), Actions(actions), Diags(PP.getDiagnostics()), 28 GreaterThanIsOperator(true), ColonIsSacred(false), 29 InMessageExpression(false), TemplateParameterDepth(0) { 30 Tok.setKind(tok::eof); 31 Actions.CurScope = 0; 32 NumCachedScopes = 0; 33 ParenCount = BracketCount = BraceCount = 0; 34 ObjCImpDecl = 0; 35 36 // Add #pragma handlers. These are removed and destroyed in the 37 // destructor. 38 AlignHandler.reset(new PragmaAlignHandler(actions)); 39 PP.AddPragmaHandler(AlignHandler.get()); 40 41 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler(actions)); 42 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get()); 43 44 OptionsHandler.reset(new PragmaOptionsHandler(actions)); 45 PP.AddPragmaHandler(OptionsHandler.get()); 46 47 PackHandler.reset(new PragmaPackHandler(actions)); 48 PP.AddPragmaHandler(PackHandler.get()); 49 50 MSStructHandler.reset(new PragmaMSStructHandler(actions)); 51 PP.AddPragmaHandler(MSStructHandler.get()); 52 53 UnusedHandler.reset(new PragmaUnusedHandler(actions, *this)); 54 PP.AddPragmaHandler(UnusedHandler.get()); 55 56 WeakHandler.reset(new PragmaWeakHandler(actions)); 57 PP.AddPragmaHandler(WeakHandler.get()); 58 59 FPContractHandler.reset(new PragmaFPContractHandler(actions, *this)); 60 PP.AddPragmaHandler("STDC", FPContractHandler.get()); 61 62 if (getLang().OpenCL) { 63 OpenCLExtensionHandler.reset( 64 new PragmaOpenCLExtensionHandler(actions, *this)); 65 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get()); 66 67 PP.AddPragmaHandler("OPENCL", FPContractHandler.get()); 68 } 69 70 PP.setCodeCompletionHandler(*this); 71 } 72 73 /// If a crash happens while the parser is active, print out a line indicating 74 /// what the current token is. 75 void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const { 76 const Token &Tok = P.getCurToken(); 77 if (Tok.is(tok::eof)) { 78 OS << "<eof> parser at end of file\n"; 79 return; 80 } 81 82 if (Tok.getLocation().isInvalid()) { 83 OS << "<unknown> parser at unknown location\n"; 84 return; 85 } 86 87 const Preprocessor &PP = P.getPreprocessor(); 88 Tok.getLocation().print(OS, PP.getSourceManager()); 89 if (Tok.isAnnotation()) 90 OS << ": at annotation token \n"; 91 else 92 OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n"; 93 } 94 95 96 DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) { 97 return Diags.Report(Loc, DiagID); 98 } 99 100 DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) { 101 return Diag(Tok.getLocation(), DiagID); 102 } 103 104 /// \brief Emits a diagnostic suggesting parentheses surrounding a 105 /// given range. 106 /// 107 /// \param Loc The location where we'll emit the diagnostic. 108 /// \param Loc The kind of diagnostic to emit. 109 /// \param ParenRange Source range enclosing code that should be parenthesized. 110 void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK, 111 SourceRange ParenRange) { 112 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd()); 113 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { 114 // We can't display the parentheses, so just dig the 115 // warning/error and return. 116 Diag(Loc, DK); 117 return; 118 } 119 120 Diag(Loc, DK) 121 << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") 122 << FixItHint::CreateInsertion(EndLoc, ")"); 123 } 124 125 /// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'), 126 /// this helper function matches and consumes the specified RHS token if 127 /// present. If not present, it emits a corresponding diagnostic indicating 128 /// that the parser failed to match the RHS of the token at LHSLoc. 129 SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok, 130 SourceLocation LHSLoc) { 131 132 if (Tok.is(RHSTok)) 133 return ConsumeAnyToken(); 134 135 SourceLocation R = Tok.getLocation(); 136 const char *LHSName = "unknown"; 137 diag::kind DID = diag::err_parse_error; 138 switch (RHSTok) { 139 default: break; 140 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break; 141 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break; 142 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break; 143 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break; 144 case tok::greatergreatergreater: 145 LHSName = "<<<"; DID = diag::err_expected_ggg; break; 146 } 147 Diag(Tok, DID); 148 Diag(LHSLoc, diag::note_matching) << LHSName; 149 SkipUntil(RHSTok); 150 return R; 151 } 152 153 static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) { 154 switch (ExpectedTok) { 155 case tok::semi: return Tok.is(tok::colon); // : for ; 156 default: return false; 157 } 158 } 159 160 /// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the 161 /// input. If so, it is consumed and false is returned. 162 /// 163 /// If the input is malformed, this emits the specified diagnostic. Next, if 164 /// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is 165 /// returned. 166 bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID, 167 const char *Msg, tok::TokenKind SkipToTok) { 168 if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) { 169 ConsumeAnyToken(); 170 return false; 171 } 172 173 // Detect common single-character typos and resume. 174 if (IsCommonTypo(ExpectedTok, Tok)) { 175 SourceLocation Loc = Tok.getLocation(); 176 Diag(Loc, DiagID) 177 << Msg 178 << FixItHint::CreateReplacement(SourceRange(Loc), 179 getTokenSimpleSpelling(ExpectedTok)); 180 ConsumeAnyToken(); 181 182 // Pretend there wasn't a problem. 183 return false; 184 } 185 186 const char *Spelling = 0; 187 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 188 if (EndLoc.isValid() && 189 (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) { 190 // Show what code to insert to fix this problem. 191 Diag(EndLoc, DiagID) 192 << Msg 193 << FixItHint::CreateInsertion(EndLoc, Spelling); 194 } else 195 Diag(Tok, DiagID) << Msg; 196 197 if (SkipToTok != tok::unknown) 198 SkipUntil(SkipToTok); 199 return true; 200 } 201 202 bool Parser::ExpectAndConsumeSemi(unsigned DiagID) { 203 if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) { 204 ConsumeAnyToken(); 205 return false; 206 } 207 208 if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) && 209 NextToken().is(tok::semi)) { 210 Diag(Tok, diag::err_extraneous_token_before_semi) 211 << PP.getSpelling(Tok) 212 << FixItHint::CreateRemoval(Tok.getLocation()); 213 ConsumeAnyToken(); // The ')' or ']'. 214 ConsumeToken(); // The ';'. 215 return false; 216 } 217 218 return ExpectAndConsume(tok::semi, DiagID); 219 } 220 221 //===----------------------------------------------------------------------===// 222 // Error recovery. 223 //===----------------------------------------------------------------------===// 224 225 /// SkipUntil - Read tokens until we get to the specified token, then consume 226 /// it (unless DontConsume is true). Because we cannot guarantee that the 227 /// token will ever occur, this skips to the next token, or to some likely 228 /// good stopping point. If StopAtSemi is true, skipping will stop at a ';' 229 /// character. 230 /// 231 /// If SkipUntil finds the specified token, it returns true, otherwise it 232 /// returns false. 233 bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks, 234 bool StopAtSemi, bool DontConsume, 235 bool StopAtCodeCompletion) { 236 // We always want this function to skip at least one token if the first token 237 // isn't T and if not at EOF. 238 bool isFirstTokenSkipped = true; 239 while (1) { 240 // If we found one of the tokens, stop and return true. 241 for (unsigned i = 0; i != NumToks; ++i) { 242 if (Tok.is(Toks[i])) { 243 if (DontConsume) { 244 // Noop, don't consume the token. 245 } else { 246 ConsumeAnyToken(); 247 } 248 return true; 249 } 250 } 251 252 switch (Tok.getKind()) { 253 case tok::eof: 254 // Ran out of tokens. 255 return false; 256 257 case tok::code_completion: 258 if (!StopAtCodeCompletion) 259 ConsumeToken(); 260 return false; 261 262 case tok::l_paren: 263 // Recursively skip properly-nested parens. 264 ConsumeParen(); 265 SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion); 266 break; 267 case tok::l_square: 268 // Recursively skip properly-nested square brackets. 269 ConsumeBracket(); 270 SkipUntil(tok::r_square, false, false, StopAtCodeCompletion); 271 break; 272 case tok::l_brace: 273 // Recursively skip properly-nested braces. 274 ConsumeBrace(); 275 SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion); 276 break; 277 278 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 279 // Since the user wasn't looking for this token (if they were, it would 280 // already be handled), this isn't balanced. If there is a LHS token at a 281 // higher level, we will assume that this matches the unbalanced token 282 // and return it. Otherwise, this is a spurious RHS token, which we skip. 283 case tok::r_paren: 284 if (ParenCount && !isFirstTokenSkipped) 285 return false; // Matches something. 286 ConsumeParen(); 287 break; 288 case tok::r_square: 289 if (BracketCount && !isFirstTokenSkipped) 290 return false; // Matches something. 291 ConsumeBracket(); 292 break; 293 case tok::r_brace: 294 if (BraceCount && !isFirstTokenSkipped) 295 return false; // Matches something. 296 ConsumeBrace(); 297 break; 298 299 case tok::string_literal: 300 case tok::wide_string_literal: 301 ConsumeStringToken(); 302 break; 303 304 case tok::at: 305 return false; 306 307 case tok::semi: 308 if (StopAtSemi) 309 return false; 310 // FALL THROUGH. 311 default: 312 // Skip this token. 313 ConsumeToken(); 314 break; 315 } 316 isFirstTokenSkipped = false; 317 } 318 } 319 320 //===----------------------------------------------------------------------===// 321 // Scope manipulation 322 //===----------------------------------------------------------------------===// 323 324 /// EnterScope - Start a new scope. 325 void Parser::EnterScope(unsigned ScopeFlags) { 326 if (NumCachedScopes) { 327 Scope *N = ScopeCache[--NumCachedScopes]; 328 N->Init(getCurScope(), ScopeFlags); 329 Actions.CurScope = N; 330 } else { 331 Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags); 332 } 333 } 334 335 /// ExitScope - Pop a scope off the scope stack. 336 void Parser::ExitScope() { 337 assert(getCurScope() && "Scope imbalance!"); 338 339 // Inform the actions module that this scope is going away if there are any 340 // decls in it. 341 if (!getCurScope()->decl_empty()) 342 Actions.ActOnPopScope(Tok.getLocation(), getCurScope()); 343 344 Scope *OldScope = getCurScope(); 345 Actions.CurScope = OldScope->getParent(); 346 347 if (NumCachedScopes == ScopeCacheSize) 348 delete OldScope; 349 else 350 ScopeCache[NumCachedScopes++] = OldScope; 351 } 352 353 /// Set the flags for the current scope to ScopeFlags. If ManageFlags is false, 354 /// this object does nothing. 355 Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags, 356 bool ManageFlags) 357 : CurScope(ManageFlags ? Self->getCurScope() : 0) { 358 if (CurScope) { 359 OldFlags = CurScope->getFlags(); 360 CurScope->setFlags(ScopeFlags); 361 } 362 } 363 364 /// Restore the flags for the current scope to what they were before this 365 /// object overrode them. 366 Parser::ParseScopeFlags::~ParseScopeFlags() { 367 if (CurScope) 368 CurScope->setFlags(OldFlags); 369 } 370 371 372 //===----------------------------------------------------------------------===// 373 // C99 6.9: External Definitions. 374 //===----------------------------------------------------------------------===// 375 376 Parser::~Parser() { 377 // If we still have scopes active, delete the scope tree. 378 delete getCurScope(); 379 Actions.CurScope = 0; 380 381 // Free the scope cache. 382 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i) 383 delete ScopeCache[i]; 384 385 // Free LateParsedTemplatedFunction nodes. 386 for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin(); 387 it != LateParsedTemplateMap.end(); ++it) 388 delete it->second; 389 390 // Remove the pragma handlers we installed. 391 PP.RemovePragmaHandler(AlignHandler.get()); 392 AlignHandler.reset(); 393 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get()); 394 GCCVisibilityHandler.reset(); 395 PP.RemovePragmaHandler(OptionsHandler.get()); 396 OptionsHandler.reset(); 397 PP.RemovePragmaHandler(PackHandler.get()); 398 PackHandler.reset(); 399 PP.RemovePragmaHandler(MSStructHandler.get()); 400 MSStructHandler.reset(); 401 PP.RemovePragmaHandler(UnusedHandler.get()); 402 UnusedHandler.reset(); 403 PP.RemovePragmaHandler(WeakHandler.get()); 404 WeakHandler.reset(); 405 406 if (getLang().OpenCL) { 407 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get()); 408 OpenCLExtensionHandler.reset(); 409 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get()); 410 } 411 412 PP.RemovePragmaHandler("STDC", FPContractHandler.get()); 413 FPContractHandler.reset(); 414 PP.clearCodeCompletionHandler(); 415 } 416 417 /// Initialize - Warm up the parser. 418 /// 419 void Parser::Initialize() { 420 // Create the translation unit scope. Install it as the current scope. 421 assert(getCurScope() == 0 && "A scope is already active?"); 422 EnterScope(Scope::DeclScope); 423 Actions.ActOnTranslationUnitScope(getCurScope()); 424 425 // Prime the lexer look-ahead. 426 ConsumeToken(); 427 428 if (Tok.is(tok::eof) && 429 !getLang().CPlusPlus) // Empty source file is an extension in C 430 Diag(Tok, diag::ext_empty_source_file); 431 432 // Initialization for Objective-C context sensitive keywords recognition. 433 // Referenced in Parser::ParseObjCTypeQualifierList. 434 if (getLang().ObjC1) { 435 ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in"); 436 ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out"); 437 ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout"); 438 ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway"); 439 ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy"); 440 ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref"); 441 } 442 443 Ident_final = 0; 444 Ident_override = 0; 445 446 Ident_super = &PP.getIdentifierTable().get("super"); 447 448 if (getLang().AltiVec) { 449 Ident_vector = &PP.getIdentifierTable().get("vector"); 450 Ident_pixel = &PP.getIdentifierTable().get("pixel"); 451 } 452 453 Ident_introduced = 0; 454 Ident_deprecated = 0; 455 Ident_obsoleted = 0; 456 Ident_unavailable = 0; 457 458 Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0; 459 Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0; 460 Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0; 461 462 if(getLang().Borland) { 463 Ident__exception_info = PP.getIdentifierInfo("_exception_info"); 464 Ident___exception_info = PP.getIdentifierInfo("__exception_info"); 465 Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation"); 466 Ident__exception_code = PP.getIdentifierInfo("_exception_code"); 467 Ident___exception_code = PP.getIdentifierInfo("__exception_code"); 468 Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode"); 469 Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination"); 470 Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination"); 471 Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination"); 472 473 PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block); 474 PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block); 475 PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block); 476 PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter); 477 PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter); 478 PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter); 479 PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block); 480 PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block); 481 PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block); 482 } 483 } 484 485 /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the 486 /// action tells us to. This returns true if the EOF was encountered. 487 bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) { 488 DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool); 489 490 while (Tok.is(tok::annot_pragma_unused)) 491 HandlePragmaUnused(); 492 493 Result = DeclGroupPtrTy(); 494 if (Tok.is(tok::eof)) { 495 // Late template parsing can begin. 496 if (getLang().DelayedTemplateParsing) 497 Actions.SetLateTemplateParser(LateTemplateParserCallback, this); 498 499 Actions.ActOnEndOfTranslationUnit(); 500 return true; 501 } 502 503 ParsedAttributesWithRange attrs(AttrFactory); 504 MaybeParseCXX0XAttributes(attrs); 505 MaybeParseMicrosoftAttributes(attrs); 506 507 Result = ParseExternalDeclaration(attrs); 508 return false; 509 } 510 511 /// ParseTranslationUnit: 512 /// translation-unit: [C99 6.9] 513 /// external-declaration 514 /// translation-unit external-declaration 515 void Parser::ParseTranslationUnit() { 516 Initialize(); 517 518 DeclGroupPtrTy Res; 519 while (!ParseTopLevelDecl(Res)) 520 /*parse them all*/; 521 522 ExitScope(); 523 assert(getCurScope() == 0 && "Scope imbalance!"); 524 } 525 526 /// ParseExternalDeclaration: 527 /// 528 /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl] 529 /// function-definition 530 /// declaration 531 /// [C++0x] empty-declaration 532 /// [GNU] asm-definition 533 /// [GNU] __extension__ external-declaration 534 /// [OBJC] objc-class-definition 535 /// [OBJC] objc-class-declaration 536 /// [OBJC] objc-alias-declaration 537 /// [OBJC] objc-protocol-definition 538 /// [OBJC] objc-method-definition 539 /// [OBJC] @end 540 /// [C++] linkage-specification 541 /// [GNU] asm-definition: 542 /// simple-asm-expr ';' 543 /// 544 /// [C++0x] empty-declaration: 545 /// ';' 546 /// 547 /// [C++0x/GNU] 'extern' 'template' declaration 548 Parser::DeclGroupPtrTy 549 Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs, 550 ParsingDeclSpec *DS) { 551 DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool); 552 ParenBraceBracketBalancer BalancerRAIIObj(*this); 553 554 Decl *SingleDecl = 0; 555 switch (Tok.getKind()) { 556 case tok::semi: 557 if (!getLang().CPlusPlus0x) 558 Diag(Tok, diag::ext_top_level_semi) 559 << FixItHint::CreateRemoval(Tok.getLocation()); 560 561 ConsumeToken(); 562 // TODO: Invoke action for top-level semicolon. 563 return DeclGroupPtrTy(); 564 case tok::r_brace: 565 Diag(Tok, diag::err_expected_external_declaration); 566 ConsumeBrace(); 567 return DeclGroupPtrTy(); 568 case tok::eof: 569 Diag(Tok, diag::err_expected_external_declaration); 570 return DeclGroupPtrTy(); 571 case tok::kw___extension__: { 572 // __extension__ silences extension warnings in the subexpression. 573 ExtensionRAIIObject O(Diags); // Use RAII to do this. 574 ConsumeToken(); 575 return ParseExternalDeclaration(attrs); 576 } 577 case tok::kw_asm: { 578 ProhibitAttributes(attrs); 579 580 SourceLocation StartLoc = Tok.getLocation(); 581 SourceLocation EndLoc; 582 ExprResult Result(ParseSimpleAsm(&EndLoc)); 583 584 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 585 "top-level asm block"); 586 587 if (Result.isInvalid()) 588 return DeclGroupPtrTy(); 589 SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc); 590 break; 591 } 592 case tok::at: 593 // @ is not a legal token unless objc is enabled, no need to check for ObjC. 594 /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like 595 /// @class foo, bar; 596 SingleDecl = ParseObjCAtDirectives(); 597 break; 598 case tok::minus: 599 case tok::plus: 600 if (!getLang().ObjC1) { 601 Diag(Tok, diag::err_expected_external_declaration); 602 ConsumeToken(); 603 return DeclGroupPtrTy(); 604 } 605 SingleDecl = ParseObjCMethodDefinition(); 606 break; 607 case tok::code_completion: 608 Actions.CodeCompleteOrdinaryName(getCurScope(), 609 ObjCImpDecl? Sema::PCC_ObjCImplementation 610 : Sema::PCC_Namespace); 611 ConsumeCodeCompletionToken(); 612 return ParseExternalDeclaration(attrs); 613 case tok::kw_using: 614 case tok::kw_namespace: 615 case tok::kw_typedef: 616 case tok::kw_template: 617 case tok::kw_export: // As in 'export template' 618 case tok::kw_static_assert: 619 case tok::kw__Static_assert: 620 // A function definition cannot start with a these keywords. 621 { 622 SourceLocation DeclEnd; 623 StmtVector Stmts(Actions); 624 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 625 } 626 627 case tok::kw_static: 628 // Parse (then ignore) 'static' prior to a template instantiation. This is 629 // a GCC extension that we intentionally do not support. 630 if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) { 631 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 632 << 0; 633 SourceLocation DeclEnd; 634 StmtVector Stmts(Actions); 635 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 636 } 637 goto dont_know; 638 639 case tok::kw_inline: 640 if (getLang().CPlusPlus) { 641 tok::TokenKind NextKind = NextToken().getKind(); 642 643 // Inline namespaces. Allowed as an extension even in C++03. 644 if (NextKind == tok::kw_namespace) { 645 SourceLocation DeclEnd; 646 StmtVector Stmts(Actions); 647 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 648 } 649 650 // Parse (then ignore) 'inline' prior to a template instantiation. This is 651 // a GCC extension that we intentionally do not support. 652 if (NextKind == tok::kw_template) { 653 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored) 654 << 1; 655 SourceLocation DeclEnd; 656 StmtVector Stmts(Actions); 657 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs); 658 } 659 } 660 goto dont_know; 661 662 case tok::kw_extern: 663 if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) { 664 // Extern templates 665 SourceLocation ExternLoc = ConsumeToken(); 666 SourceLocation TemplateLoc = ConsumeToken(); 667 SourceLocation DeclEnd; 668 return Actions.ConvertDeclToDeclGroup( 669 ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd)); 670 } 671 // FIXME: Detect C++ linkage specifications here? 672 goto dont_know; 673 674 case tok::kw___if_exists: 675 case tok::kw___if_not_exists: 676 ParseMicrosoftIfExistsExternalDeclaration(); 677 return DeclGroupPtrTy(); 678 679 default: 680 dont_know: 681 // We can't tell whether this is a function-definition or declaration yet. 682 if (DS) { 683 DS->takeAttributesFrom(attrs); 684 return ParseDeclarationOrFunctionDefinition(*DS); 685 } else { 686 return ParseDeclarationOrFunctionDefinition(attrs); 687 } 688 } 689 690 // This routine returns a DeclGroup, if the thing we parsed only contains a 691 // single decl, convert it now. 692 return Actions.ConvertDeclToDeclGroup(SingleDecl); 693 } 694 695 /// \brief Determine whether the current token, if it occurs after a 696 /// declarator, continues a declaration or declaration list. 697 bool Parser::isDeclarationAfterDeclarator() { 698 // Check for '= delete' or '= default' 699 if (getLang().CPlusPlus && Tok.is(tok::equal)) { 700 const Token &KW = NextToken(); 701 if (KW.is(tok::kw_default) || KW.is(tok::kw_delete)) 702 return false; 703 } 704 705 return Tok.is(tok::equal) || // int X()= -> not a function def 706 Tok.is(tok::comma) || // int X(), -> not a function def 707 Tok.is(tok::semi) || // int X(); -> not a function def 708 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def 709 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def 710 (getLang().CPlusPlus && 711 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++] 712 } 713 714 /// \brief Determine whether the current token, if it occurs after a 715 /// declarator, indicates the start of a function definition. 716 bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) { 717 assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator"); 718 if (Tok.is(tok::l_brace)) // int X() {} 719 return true; 720 721 // Handle K&R C argument lists: int X(f) int f; {} 722 if (!getLang().CPlusPlus && 723 Declarator.getFunctionTypeInfo().isKNRPrototype()) 724 return isDeclarationSpecifier(); 725 726 if (getLang().CPlusPlus && Tok.is(tok::equal)) { 727 const Token &KW = NextToken(); 728 return KW.is(tok::kw_default) || KW.is(tok::kw_delete); 729 } 730 731 return Tok.is(tok::colon) || // X() : Base() {} (used for ctors) 732 Tok.is(tok::kw_try); // X() try { ... } 733 } 734 735 /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or 736 /// a declaration. We can't tell which we have until we read up to the 737 /// compound-statement in function-definition. TemplateParams, if 738 /// non-NULL, provides the template parameters when we're parsing a 739 /// C++ template-declaration. 740 /// 741 /// function-definition: [C99 6.9.1] 742 /// decl-specs declarator declaration-list[opt] compound-statement 743 /// [C90] function-definition: [C99 6.7.1] - implicit int result 744 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 745 /// 746 /// declaration: [C99 6.7] 747 /// declaration-specifiers init-declarator-list[opt] ';' 748 /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode] 749 /// [OMP] threadprivate-directive [TODO] 750 /// 751 Parser::DeclGroupPtrTy 752 Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS, 753 AccessSpecifier AS) { 754 // Parse the common declaration-specifiers piece. 755 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level); 756 757 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" 758 // declaration-specifiers init-declarator-list[opt] ';' 759 if (Tok.is(tok::semi)) { 760 ConsumeToken(); 761 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); 762 DS.complete(TheDecl); 763 return Actions.ConvertDeclToDeclGroup(TheDecl); 764 } 765 766 // ObjC2 allows prefix attributes on class interfaces and protocols. 767 // FIXME: This still needs better diagnostics. We should only accept 768 // attributes here, no types, etc. 769 if (getLang().ObjC2 && Tok.is(tok::at)) { 770 SourceLocation AtLoc = ConsumeToken(); // the "@" 771 if (!Tok.isObjCAtKeyword(tok::objc_interface) && 772 !Tok.isObjCAtKeyword(tok::objc_protocol)) { 773 Diag(Tok, diag::err_objc_unexpected_attr); 774 SkipUntil(tok::semi); // FIXME: better skip? 775 return DeclGroupPtrTy(); 776 } 777 778 DS.abort(); 779 780 const char *PrevSpec = 0; 781 unsigned DiagID; 782 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID)) 783 Diag(AtLoc, DiagID) << PrevSpec; 784 785 Decl *TheDecl = 0; 786 if (Tok.isObjCAtKeyword(tok::objc_protocol)) 787 TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes()); 788 else 789 TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()); 790 return Actions.ConvertDeclToDeclGroup(TheDecl); 791 } 792 793 // If the declspec consisted only of 'extern' and we have a string 794 // literal following it, this must be a C++ linkage specifier like 795 // 'extern "C"'. 796 if (Tok.is(tok::string_literal) && getLang().CPlusPlus && 797 DS.getStorageClassSpec() == DeclSpec::SCS_extern && 798 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) { 799 Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext); 800 return Actions.ConvertDeclToDeclGroup(TheDecl); 801 } 802 803 return ParseDeclGroup(DS, Declarator::FileContext, true); 804 } 805 806 Parser::DeclGroupPtrTy 807 Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributes &attrs, 808 AccessSpecifier AS) { 809 ParsingDeclSpec DS(*this); 810 DS.takeAttributesFrom(attrs); 811 return ParseDeclarationOrFunctionDefinition(DS, AS); 812 } 813 814 /// ParseFunctionDefinition - We parsed and verified that the specified 815 /// Declarator is well formed. If this is a K&R-style function, read the 816 /// parameters declaration-list, then start the compound-statement. 817 /// 818 /// function-definition: [C99 6.9.1] 819 /// decl-specs declarator declaration-list[opt] compound-statement 820 /// [C90] function-definition: [C99 6.7.1] - implicit int result 821 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 822 /// [C++] function-definition: [C++ 8.4] 823 /// decl-specifier-seq[opt] declarator ctor-initializer[opt] 824 /// function-body 825 /// [C++] function-definition: [C++ 8.4] 826 /// decl-specifier-seq[opt] declarator function-try-block 827 /// 828 Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D, 829 const ParsedTemplateInfo &TemplateInfo) { 830 // Poison the SEH identifiers so they are flagged as illegal in function bodies 831 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); 832 const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 833 834 // If this is C90 and the declspecs were completely missing, fudge in an 835 // implicit int. We do this here because this is the only place where 836 // declaration-specifiers are completely optional in the grammar. 837 if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) { 838 const char *PrevSpec; 839 unsigned DiagID; 840 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int, 841 D.getIdentifierLoc(), 842 PrevSpec, DiagID); 843 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin()); 844 } 845 846 // If this declaration was formed with a K&R-style identifier list for the 847 // arguments, parse declarations for all of the args next. 848 // int foo(a,b) int a; float b; {} 849 if (FTI.isKNRPrototype()) 850 ParseKNRParamDeclarations(D); 851 852 853 // We should have either an opening brace or, in a C++ constructor, 854 // we may have a colon. 855 if (Tok.isNot(tok::l_brace) && 856 (!getLang().CPlusPlus || 857 (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) && 858 Tok.isNot(tok::equal)))) { 859 Diag(Tok, diag::err_expected_fn_body); 860 861 // Skip over garbage, until we get to '{'. Don't eat the '{'. 862 SkipUntil(tok::l_brace, true, true); 863 864 // If we didn't find the '{', bail out. 865 if (Tok.isNot(tok::l_brace)) 866 return 0; 867 } 868 869 // In delayed template parsing mode, for function template we consume the 870 // tokens and store them for late parsing at the end of the translation unit. 871 if (getLang().DelayedTemplateParsing && 872 TemplateInfo.Kind == ParsedTemplateInfo::Template) { 873 MultiTemplateParamsArg TemplateParameterLists(Actions, 874 TemplateInfo.TemplateParams->data(), 875 TemplateInfo.TemplateParams->size()); 876 877 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); 878 Scope *ParentScope = getCurScope()->getParent(); 879 880 Decl *DP = Actions.HandleDeclarator(ParentScope, D, 881 move(TemplateParameterLists), 882 /*IsFunctionDefinition=*/true); 883 D.complete(DP); 884 D.getMutableDeclSpec().abort(); 885 886 if (DP) { 887 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(this, DP); 888 889 FunctionDecl *FnD = 0; 890 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP)) 891 FnD = FunTmpl->getTemplatedDecl(); 892 else 893 FnD = cast<FunctionDecl>(DP); 894 Actions.CheckForFunctionRedefinition(FnD); 895 896 LateParsedTemplateMap[FnD] = LPT; 897 Actions.MarkAsLateParsedTemplate(FnD); 898 LexTemplateFunctionForLateParsing(LPT->Toks); 899 } else { 900 CachedTokens Toks; 901 LexTemplateFunctionForLateParsing(Toks); 902 } 903 return DP; 904 } 905 906 // Enter a scope for the function body. 907 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); 908 909 // Tell the actions module that we have entered a function definition with the 910 // specified Declarator for the function. 911 Decl *Res = TemplateInfo.TemplateParams? 912 Actions.ActOnStartOfFunctionTemplateDef(getCurScope(), 913 MultiTemplateParamsArg(Actions, 914 TemplateInfo.TemplateParams->data(), 915 TemplateInfo.TemplateParams->size()), 916 D) 917 : Actions.ActOnStartOfFunctionDef(getCurScope(), D); 918 919 // Break out of the ParsingDeclarator context before we parse the body. 920 D.complete(Res); 921 922 // Break out of the ParsingDeclSpec context, too. This const_cast is 923 // safe because we're always the sole owner. 924 D.getMutableDeclSpec().abort(); 925 926 if (Tok.is(tok::equal)) { 927 assert(getLang().CPlusPlus && "Only C++ function definitions have '='"); 928 ConsumeToken(); 929 930 Actions.ActOnFinishFunctionBody(Res, 0, false); 931 932 bool Delete = false; 933 SourceLocation KWLoc; 934 if (Tok.is(tok::kw_delete)) { 935 if (!getLang().CPlusPlus0x) 936 Diag(Tok, diag::warn_deleted_function_accepted_as_extension); 937 938 KWLoc = ConsumeToken(); 939 Actions.SetDeclDeleted(Res, KWLoc); 940 Delete = true; 941 } else if (Tok.is(tok::kw_default)) { 942 if (!getLang().CPlusPlus0x) 943 Diag(Tok, diag::warn_defaulted_function_accepted_as_extension); 944 945 KWLoc = ConsumeToken(); 946 Actions.SetDeclDefaulted(Res, KWLoc); 947 } else { 948 llvm_unreachable("function definition after = not 'delete' or 'default'"); 949 } 950 951 if (Tok.is(tok::comma)) { 952 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 953 << Delete; 954 SkipUntil(tok::semi); 955 } else { 956 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 957 Delete ? "delete" : "default", tok::semi); 958 } 959 960 return Res; 961 } 962 963 if (Tok.is(tok::kw_try)) 964 return ParseFunctionTryBlock(Res, BodyScope); 965 966 // If we have a colon, then we're probably parsing a C++ 967 // ctor-initializer. 968 if (Tok.is(tok::colon)) { 969 ParseConstructorInitializer(Res); 970 971 // Recover from error. 972 if (!Tok.is(tok::l_brace)) { 973 BodyScope.Exit(); 974 Actions.ActOnFinishFunctionBody(Res, 0); 975 return Res; 976 } 977 } else 978 Actions.ActOnDefaultCtorInitializers(Res); 979 980 return ParseFunctionStatementBody(Res, BodyScope); 981 } 982 983 /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides 984 /// types for a function with a K&R-style identifier list for arguments. 985 void Parser::ParseKNRParamDeclarations(Declarator &D) { 986 // We know that the top-level of this declarator is a function. 987 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); 988 989 // Enter function-declaration scope, limiting any declarators to the 990 // function prototype scope, including parameter declarators. 991 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope); 992 993 // Read all the argument declarations. 994 while (isDeclarationSpecifier()) { 995 SourceLocation DSStart = Tok.getLocation(); 996 997 // Parse the common declaration-specifiers piece. 998 DeclSpec DS(AttrFactory); 999 ParseDeclarationSpecifiers(DS); 1000 1001 // C99 6.9.1p6: 'each declaration in the declaration list shall have at 1002 // least one declarator'. 1003 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with 1004 // the declarations though. It's trivial to ignore them, really hard to do 1005 // anything else with them. 1006 if (Tok.is(tok::semi)) { 1007 Diag(DSStart, diag::err_declaration_does_not_declare_param); 1008 ConsumeToken(); 1009 continue; 1010 } 1011 1012 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other 1013 // than register. 1014 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && 1015 DS.getStorageClassSpec() != DeclSpec::SCS_register) { 1016 Diag(DS.getStorageClassSpecLoc(), 1017 diag::err_invalid_storage_class_in_func_decl); 1018 DS.ClearStorageClassSpecs(); 1019 } 1020 if (DS.isThreadSpecified()) { 1021 Diag(DS.getThreadSpecLoc(), 1022 diag::err_invalid_storage_class_in_func_decl); 1023 DS.ClearStorageClassSpecs(); 1024 } 1025 1026 // Parse the first declarator attached to this declspec. 1027 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext); 1028 ParseDeclarator(ParmDeclarator); 1029 1030 // Handle the full declarator list. 1031 while (1) { 1032 // If attributes are present, parse them. 1033 MaybeParseGNUAttributes(ParmDeclarator); 1034 1035 // Ask the actions module to compute the type for this declarator. 1036 Decl *Param = 1037 Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); 1038 1039 if (Param && 1040 // A missing identifier has already been diagnosed. 1041 ParmDeclarator.getIdentifier()) { 1042 1043 // Scan the argument list looking for the correct param to apply this 1044 // type. 1045 for (unsigned i = 0; ; ++i) { 1046 // C99 6.9.1p6: those declarators shall declare only identifiers from 1047 // the identifier list. 1048 if (i == FTI.NumArgs) { 1049 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param) 1050 << ParmDeclarator.getIdentifier(); 1051 break; 1052 } 1053 1054 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) { 1055 // Reject redefinitions of parameters. 1056 if (FTI.ArgInfo[i].Param) { 1057 Diag(ParmDeclarator.getIdentifierLoc(), 1058 diag::err_param_redefinition) 1059 << ParmDeclarator.getIdentifier(); 1060 } else { 1061 FTI.ArgInfo[i].Param = Param; 1062 } 1063 break; 1064 } 1065 } 1066 } 1067 1068 // If we don't have a comma, it is either the end of the list (a ';') or 1069 // an error, bail out. 1070 if (Tok.isNot(tok::comma)) 1071 break; 1072 1073 // Consume the comma. 1074 ConsumeToken(); 1075 1076 // Parse the next declarator. 1077 ParmDeclarator.clear(); 1078 ParseDeclarator(ParmDeclarator); 1079 } 1080 1081 if (Tok.is(tok::semi)) { 1082 ConsumeToken(); 1083 } else { 1084 Diag(Tok, diag::err_parse_error); 1085 // Skip to end of block or statement 1086 SkipUntil(tok::semi, true); 1087 if (Tok.is(tok::semi)) 1088 ConsumeToken(); 1089 } 1090 } 1091 1092 // The actions module must verify that all arguments were declared. 1093 Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation()); 1094 } 1095 1096 1097 /// ParseAsmStringLiteral - This is just a normal string-literal, but is not 1098 /// allowed to be a wide string, and is not subject to character translation. 1099 /// 1100 /// [GNU] asm-string-literal: 1101 /// string-literal 1102 /// 1103 Parser::ExprResult Parser::ParseAsmStringLiteral() { 1104 if (!isTokenStringLiteral()) { 1105 Diag(Tok, diag::err_expected_string_literal); 1106 return ExprError(); 1107 } 1108 1109 ExprResult Res(ParseStringLiteralExpression()); 1110 if (Res.isInvalid()) return move(Res); 1111 1112 // TODO: Diagnose: wide string literal in 'asm' 1113 1114 return move(Res); 1115 } 1116 1117 /// ParseSimpleAsm 1118 /// 1119 /// [GNU] simple-asm-expr: 1120 /// 'asm' '(' asm-string-literal ')' 1121 /// 1122 Parser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) { 1123 assert(Tok.is(tok::kw_asm) && "Not an asm!"); 1124 SourceLocation Loc = ConsumeToken(); 1125 1126 if (Tok.is(tok::kw_volatile)) { 1127 // Remove from the end of 'asm' to the end of 'volatile'. 1128 SourceRange RemovalRange(PP.getLocForEndOfToken(Loc), 1129 PP.getLocForEndOfToken(Tok.getLocation())); 1130 1131 Diag(Tok, diag::warn_file_asm_volatile) 1132 << FixItHint::CreateRemoval(RemovalRange); 1133 ConsumeToken(); 1134 } 1135 1136 if (Tok.isNot(tok::l_paren)) { 1137 Diag(Tok, diag::err_expected_lparen_after) << "asm"; 1138 return ExprError(); 1139 } 1140 1141 Loc = ConsumeParen(); 1142 1143 ExprResult Result(ParseAsmStringLiteral()); 1144 1145 if (Result.isInvalid()) { 1146 SkipUntil(tok::r_paren, true, true); 1147 if (EndLoc) 1148 *EndLoc = Tok.getLocation(); 1149 ConsumeAnyToken(); 1150 } else { 1151 Loc = MatchRHSPunctuation(tok::r_paren, Loc); 1152 if (EndLoc) 1153 *EndLoc = Loc; 1154 } 1155 1156 return move(Result); 1157 } 1158 1159 /// \brief Get the TemplateIdAnnotation from the token and put it in the 1160 /// cleanup pool so that it gets destroyed when parsing the current top level 1161 /// declaration is finished. 1162 TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) { 1163 assert(tok.is(tok::annot_template_id) && "Expected template-id token"); 1164 TemplateIdAnnotation * 1165 Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue()); 1166 TopLevelDeclCleanupPool.delayMemberFunc< TemplateIdAnnotation, 1167 &TemplateIdAnnotation::Destroy>(Id); 1168 return Id; 1169 } 1170 1171 /// TryAnnotateTypeOrScopeToken - If the current token position is on a 1172 /// typename (possibly qualified in C++) or a C++ scope specifier not followed 1173 /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens 1174 /// with a single annotation token representing the typename or C++ scope 1175 /// respectively. 1176 /// This simplifies handling of C++ scope specifiers and allows efficient 1177 /// backtracking without the need to re-parse and resolve nested-names and 1178 /// typenames. 1179 /// It will mainly be called when we expect to treat identifiers as typenames 1180 /// (if they are typenames). For example, in C we do not expect identifiers 1181 /// inside expressions to be treated as typenames so it will not be called 1182 /// for expressions in C. 1183 /// The benefit for C/ObjC is that a typename will be annotated and 1184 /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName 1185 /// will not be called twice, once to check whether we have a declaration 1186 /// specifier, and another one to get the actual type inside 1187 /// ParseDeclarationSpecifiers). 1188 /// 1189 /// This returns true if an error occurred. 1190 /// 1191 /// Note that this routine emits an error if you call it with ::new or ::delete 1192 /// as the current tokens, so only call it in contexts where these are invalid. 1193 bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext) { 1194 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) 1195 || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) && 1196 "Cannot be a type or scope token!"); 1197 1198 if (Tok.is(tok::kw_typename)) { 1199 // Parse a C++ typename-specifier, e.g., "typename T::type". 1200 // 1201 // typename-specifier: 1202 // 'typename' '::' [opt] nested-name-specifier identifier 1203 // 'typename' '::' [opt] nested-name-specifier template [opt] 1204 // simple-template-id 1205 SourceLocation TypenameLoc = ConsumeToken(); 1206 CXXScopeSpec SS; 1207 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(), false, 1208 0, /*IsTypename*/true)) 1209 return true; 1210 if (!SS.isSet()) { 1211 if (getLang().Microsoft) 1212 Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename); 1213 else 1214 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename); 1215 return true; 1216 } 1217 1218 TypeResult Ty; 1219 if (Tok.is(tok::identifier)) { 1220 // FIXME: check whether the next token is '<', first! 1221 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 1222 *Tok.getIdentifierInfo(), 1223 Tok.getLocation()); 1224 } else if (Tok.is(tok::annot_template_id)) { 1225 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1226 if (TemplateId->Kind == TNK_Function_template) { 1227 Diag(Tok, diag::err_typename_refers_to_non_type_template) 1228 << Tok.getAnnotationRange(); 1229 return true; 1230 } 1231 1232 ASTTemplateArgsPtr TemplateArgsPtr(Actions, 1233 TemplateId->getTemplateArgs(), 1234 TemplateId->NumArgs); 1235 1236 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 1237 /*FIXME:*/SourceLocation(), 1238 TemplateId->Template, 1239 TemplateId->TemplateNameLoc, 1240 TemplateId->LAngleLoc, 1241 TemplateArgsPtr, 1242 TemplateId->RAngleLoc); 1243 } else { 1244 Diag(Tok, diag::err_expected_type_name_after_typename) 1245 << SS.getRange(); 1246 return true; 1247 } 1248 1249 SourceLocation EndLoc = Tok.getLastLoc(); 1250 Tok.setKind(tok::annot_typename); 1251 setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get()); 1252 Tok.setAnnotationEndLoc(EndLoc); 1253 Tok.setLocation(TypenameLoc); 1254 PP.AnnotateCachedTokens(Tok); 1255 return false; 1256 } 1257 1258 // Remembers whether the token was originally a scope annotation. 1259 bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope); 1260 1261 CXXScopeSpec SS; 1262 if (getLang().CPlusPlus) 1263 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 1264 return true; 1265 1266 if (Tok.is(tok::identifier)) { 1267 // Determine whether the identifier is a type name. 1268 if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(), 1269 Tok.getLocation(), getCurScope(), 1270 &SS, false, 1271 NextToken().is(tok::period), 1272 ParsedType(), 1273 /*NonTrivialTypeSourceInfo*/true)) { 1274 // This is a typename. Replace the current token in-place with an 1275 // annotation type token. 1276 Tok.setKind(tok::annot_typename); 1277 setTypeAnnotation(Tok, Ty); 1278 Tok.setAnnotationEndLoc(Tok.getLocation()); 1279 if (SS.isNotEmpty()) // it was a C++ qualified type name. 1280 Tok.setLocation(SS.getBeginLoc()); 1281 1282 // In case the tokens were cached, have Preprocessor replace 1283 // them with the annotation token. 1284 PP.AnnotateCachedTokens(Tok); 1285 return false; 1286 } 1287 1288 if (!getLang().CPlusPlus) { 1289 // If we're in C, we can't have :: tokens at all (the lexer won't return 1290 // them). If the identifier is not a type, then it can't be scope either, 1291 // just early exit. 1292 return false; 1293 } 1294 1295 // If this is a template-id, annotate with a template-id or type token. 1296 if (NextToken().is(tok::less)) { 1297 TemplateTy Template; 1298 UnqualifiedId TemplateName; 1299 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 1300 bool MemberOfUnknownSpecialization; 1301 if (TemplateNameKind TNK 1302 = Actions.isTemplateName(getCurScope(), SS, 1303 /*hasTemplateKeyword=*/false, TemplateName, 1304 /*ObjectType=*/ ParsedType(), 1305 EnteringContext, 1306 Template, MemberOfUnknownSpecialization)) { 1307 // Consume the identifier. 1308 ConsumeToken(); 1309 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName)) { 1310 // If an unrecoverable error occurred, we need to return true here, 1311 // because the token stream is in a damaged state. We may not return 1312 // a valid identifier. 1313 return true; 1314 } 1315 } 1316 } 1317 1318 // The current token, which is either an identifier or a 1319 // template-id, is not part of the annotation. Fall through to 1320 // push that token back into the stream and complete the C++ scope 1321 // specifier annotation. 1322 } 1323 1324 if (Tok.is(tok::annot_template_id)) { 1325 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1326 if (TemplateId->Kind == TNK_Type_template) { 1327 // A template-id that refers to a type was parsed into a 1328 // template-id annotation in a context where we weren't allowed 1329 // to produce a type annotation token. Update the template-id 1330 // annotation token to a type annotation token now. 1331 AnnotateTemplateIdTokenAsType(); 1332 return false; 1333 } 1334 } 1335 1336 if (SS.isEmpty()) 1337 return false; 1338 1339 // A C++ scope specifier that isn't followed by a typename. 1340 // Push the current token back into the token stream (or revert it if it is 1341 // cached) and use an annotation scope token for current token. 1342 if (PP.isBacktrackEnabled()) 1343 PP.RevertCachedTokens(1); 1344 else 1345 PP.EnterToken(Tok); 1346 Tok.setKind(tok::annot_cxxscope); 1347 Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS)); 1348 Tok.setAnnotationRange(SS.getRange()); 1349 1350 // In case the tokens were cached, have Preprocessor replace them 1351 // with the annotation token. We don't need to do this if we've 1352 // just reverted back to the state we were in before being called. 1353 if (!wasScopeAnnotation) 1354 PP.AnnotateCachedTokens(Tok); 1355 return false; 1356 } 1357 1358 /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only 1359 /// annotates C++ scope specifiers and template-ids. This returns 1360 /// true if the token was annotated or there was an error that could not be 1361 /// recovered from. 1362 /// 1363 /// Note that this routine emits an error if you call it with ::new or ::delete 1364 /// as the current tokens, so only call it in contexts where these are invalid. 1365 bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) { 1366 assert(getLang().CPlusPlus && 1367 "Call sites of this function should be guarded by checking for C++"); 1368 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || 1369 (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)))&& 1370 "Cannot be a type or scope token!"); 1371 1372 CXXScopeSpec SS; 1373 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 1374 return true; 1375 if (SS.isEmpty()) 1376 return false; 1377 1378 // Push the current token back into the token stream (or revert it if it is 1379 // cached) and use an annotation scope token for current token. 1380 if (PP.isBacktrackEnabled()) 1381 PP.RevertCachedTokens(1); 1382 else 1383 PP.EnterToken(Tok); 1384 Tok.setKind(tok::annot_cxxscope); 1385 Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS)); 1386 Tok.setAnnotationRange(SS.getRange()); 1387 1388 // In case the tokens were cached, have Preprocessor replace them with the 1389 // annotation token. 1390 PP.AnnotateCachedTokens(Tok); 1391 return false; 1392 } 1393 1394 bool Parser::isTokenEqualOrMistypedEqualEqual(unsigned DiagID) { 1395 if (Tok.is(tok::equalequal)) { 1396 // We have '==' in a context that we would expect a '='. 1397 // The user probably made a typo, intending to type '='. Emit diagnostic, 1398 // fixit hint to turn '==' -> '=' and continue as if the user typed '='. 1399 Diag(Tok, DiagID) 1400 << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), 1401 getTokenSimpleSpelling(tok::equal)); 1402 return true; 1403 } 1404 1405 return Tok.is(tok::equal); 1406 } 1407 1408 void Parser::CodeCompletionRecovery() { 1409 for (Scope *S = getCurScope(); S; S = S->getParent()) { 1410 if (S->getFlags() & Scope::FnScope) { 1411 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction); 1412 return; 1413 } 1414 1415 if (S->getFlags() & Scope::ClassScope) { 1416 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class); 1417 return; 1418 } 1419 } 1420 1421 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace); 1422 } 1423 1424 // Anchor the Parser::FieldCallback vtable to this translation unit. 1425 // We use a spurious method instead of the destructor because 1426 // destroying FieldCallbacks can actually be slightly 1427 // performance-sensitive. 1428 void Parser::FieldCallback::_anchor() { 1429 } 1430 1431 // Code-completion pass-through functions 1432 1433 void Parser::CodeCompleteDirective(bool InConditional) { 1434 Actions.CodeCompletePreprocessorDirective(InConditional); 1435 } 1436 1437 void Parser::CodeCompleteInConditionalExclusion() { 1438 Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope()); 1439 } 1440 1441 void Parser::CodeCompleteMacroName(bool IsDefinition) { 1442 Actions.CodeCompletePreprocessorMacroName(IsDefinition); 1443 } 1444 1445 void Parser::CodeCompletePreprocessorExpression() { 1446 Actions.CodeCompletePreprocessorExpression(); 1447 } 1448 1449 void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro, 1450 MacroInfo *MacroInfo, 1451 unsigned ArgumentIndex) { 1452 Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo, 1453 ArgumentIndex); 1454 } 1455 1456 void Parser::CodeCompleteNaturalLanguage() { 1457 Actions.CodeCompleteNaturalLanguage(); 1458 } 1459 1460 bool Parser::ParseMicrosoftIfExistsCondition(bool& Result) { 1461 assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) && 1462 "Expected '__if_exists' or '__if_not_exists'"); 1463 Token Condition = Tok; 1464 SourceLocation IfExistsLoc = ConsumeToken(); 1465 1466 SourceLocation LParenLoc = Tok.getLocation(); 1467 if (Tok.isNot(tok::l_paren)) { 1468 Diag(Tok, diag::err_expected_lparen_after) << IfExistsLoc; 1469 SkipUntil(tok::semi); 1470 return true; 1471 } 1472 ConsumeParen(); // eat the '('. 1473 1474 // Parse nested-name-specifier. 1475 CXXScopeSpec SS; 1476 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); 1477 1478 // Check nested-name specifier. 1479 if (SS.isInvalid()) { 1480 SkipUntil(tok::semi); 1481 return true; 1482 } 1483 1484 // Parse the unqualified-id. 1485 UnqualifiedId Name; 1486 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) { 1487 SkipUntil(tok::semi); 1488 return true; 1489 } 1490 1491 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid()) 1492 return true; 1493 1494 // Check if the symbol exists. 1495 bool Exist = Actions.CheckMicrosoftIfExistsSymbol(SS, Name); 1496 1497 Result = ((Condition.is(tok::kw___if_exists) && Exist) || 1498 (Condition.is(tok::kw___if_not_exists) && !Exist)); 1499 1500 return false; 1501 } 1502 1503 void Parser::ParseMicrosoftIfExistsExternalDeclaration() { 1504 bool Result; 1505 if (ParseMicrosoftIfExistsCondition(Result)) 1506 return; 1507 1508 if (Tok.isNot(tok::l_brace)) { 1509 Diag(Tok, diag::err_expected_lbrace); 1510 return; 1511 } 1512 ConsumeBrace(); 1513 1514 // Condition is false skip all inside the {}. 1515 if (!Result) { 1516 SkipUntil(tok::r_brace, false); 1517 return; 1518 } 1519 1520 // Condition is true, parse the declaration. 1521 while (Tok.isNot(tok::r_brace)) { 1522 ParsedAttributesWithRange attrs(AttrFactory); 1523 MaybeParseCXX0XAttributes(attrs); 1524 MaybeParseMicrosoftAttributes(attrs); 1525 DeclGroupPtrTy Result = ParseExternalDeclaration(attrs); 1526 if (Result && !getCurScope()->getParent()) 1527 Actions.getASTConsumer().HandleTopLevelDecl(Result.get()); 1528 } 1529 1530 if (Tok.isNot(tok::r_brace)) { 1531 Diag(Tok, diag::err_expected_rbrace); 1532 return; 1533 } 1534 ConsumeBrace(); 1535 } 1536