Parser.cpp revision 9ba23b4ceacd77cd264501690a7a9e94184ef71b
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" 22using namespace clang; 23 24Parser::Parser(Preprocessor &pp, Sema &actions) 25 : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()), 26 GreaterThanIsOperator(true), ColonIsSacred(false), 27 TemplateParameterDepth(0) { 28 Tok.setKind(tok::eof); 29 Actions.CurScope = 0; 30 NumCachedScopes = 0; 31 ParenCount = BracketCount = BraceCount = 0; 32 ObjCImpDecl = 0; 33 34 // Add #pragma handlers. These are removed and destroyed in the 35 // destructor. 36 AlignHandler.reset(new PragmaAlignHandler(actions)); 37 PP.AddPragmaHandler(AlignHandler.get()); 38 39 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler(actions)); 40 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get()); 41 42 OptionsHandler.reset(new PragmaOptionsHandler(actions)); 43 PP.AddPragmaHandler(OptionsHandler.get()); 44 45 PackHandler.reset(new PragmaPackHandler(actions)); 46 PP.AddPragmaHandler(PackHandler.get()); 47 48 UnusedHandler.reset(new PragmaUnusedHandler(actions, *this)); 49 PP.AddPragmaHandler(UnusedHandler.get()); 50 51 WeakHandler.reset(new PragmaWeakHandler(actions)); 52 PP.AddPragmaHandler(WeakHandler.get()); 53 54 PP.setCodeCompletionHandler(*this); 55} 56 57/// If a crash happens while the parser is active, print out a line indicating 58/// what the current token is. 59void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const { 60 const Token &Tok = P.getCurToken(); 61 if (Tok.is(tok::eof)) { 62 OS << "<eof> parser at end of file\n"; 63 return; 64 } 65 66 if (Tok.getLocation().isInvalid()) { 67 OS << "<unknown> parser at unknown location\n"; 68 return; 69 } 70 71 const Preprocessor &PP = P.getPreprocessor(); 72 Tok.getLocation().print(OS, PP.getSourceManager()); 73 if (Tok.isAnnotation()) 74 OS << ": at annotation token \n"; 75 else 76 OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n"; 77} 78 79 80DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) { 81 return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID); 82} 83 84DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) { 85 return Diag(Tok.getLocation(), DiagID); 86} 87 88/// \brief Emits a diagnostic suggesting parentheses surrounding a 89/// given range. 90/// 91/// \param Loc The location where we'll emit the diagnostic. 92/// \param Loc The kind of diagnostic to emit. 93/// \param ParenRange Source range enclosing code that should be parenthesized. 94void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK, 95 SourceRange ParenRange) { 96 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd()); 97 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { 98 // We can't display the parentheses, so just dig the 99 // warning/error and return. 100 Diag(Loc, DK); 101 return; 102 } 103 104 Diag(Loc, DK) 105 << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") 106 << FixItHint::CreateInsertion(EndLoc, ")"); 107} 108 109/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'), 110/// this helper function matches and consumes the specified RHS token if 111/// present. If not present, it emits the specified diagnostic indicating 112/// that the parser failed to match the RHS of the token at LHSLoc. LHSName 113/// should be the name of the unmatched LHS token. 114SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok, 115 SourceLocation LHSLoc) { 116 117 if (Tok.is(RHSTok)) 118 return ConsumeAnyToken(); 119 120 SourceLocation R = Tok.getLocation(); 121 const char *LHSName = "unknown"; 122 diag::kind DID = diag::err_parse_error; 123 switch (RHSTok) { 124 default: break; 125 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break; 126 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break; 127 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break; 128 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break; 129 } 130 Diag(Tok, DID); 131 Diag(LHSLoc, diag::note_matching) << LHSName; 132 SkipUntil(RHSTok); 133 return R; 134} 135 136/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the 137/// input. If so, it is consumed and false is returned. 138/// 139/// If the input is malformed, this emits the specified diagnostic. Next, if 140/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is 141/// returned. 142bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID, 143 const char *Msg, tok::TokenKind SkipToTok) { 144 if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) { 145 ConsumeAnyToken(); 146 return false; 147 } 148 149 const char *Spelling = 0; 150 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 151 if (EndLoc.isValid() && 152 (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) { 153 // Show what code to insert to fix this problem. 154 Diag(EndLoc, DiagID) 155 << Msg 156 << FixItHint::CreateInsertion(EndLoc, Spelling); 157 } else 158 Diag(Tok, DiagID) << Msg; 159 160 if (SkipToTok != tok::unknown) 161 SkipUntil(SkipToTok); 162 return true; 163} 164 165bool Parser::ExpectAndConsumeSemi(unsigned DiagID) { 166 if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) { 167 ConsumeAnyToken(); 168 return false; 169 } 170 171 if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) && 172 NextToken().is(tok::semi)) { 173 Diag(Tok, diag::err_extraneous_token_before_semi) 174 << PP.getSpelling(Tok) 175 << FixItHint::CreateRemoval(Tok.getLocation()); 176 ConsumeAnyToken(); // The ')' or ']'. 177 ConsumeToken(); // The ';'. 178 return false; 179 } 180 181 return ExpectAndConsume(tok::semi, DiagID); 182} 183 184//===----------------------------------------------------------------------===// 185// Error recovery. 186//===----------------------------------------------------------------------===// 187 188/// SkipUntil - Read tokens until we get to the specified token, then consume 189/// it (unless DontConsume is true). Because we cannot guarantee that the 190/// token will ever occur, this skips to the next token, or to some likely 191/// good stopping point. If StopAtSemi is true, skipping will stop at a ';' 192/// character. 193/// 194/// If SkipUntil finds the specified token, it returns true, otherwise it 195/// returns false. 196bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks, 197 bool StopAtSemi, bool DontConsume) { 198 // We always want this function to skip at least one token if the first token 199 // isn't T and if not at EOF. 200 bool isFirstTokenSkipped = true; 201 while (1) { 202 // If we found one of the tokens, stop and return true. 203 for (unsigned i = 0; i != NumToks; ++i) { 204 if (Tok.is(Toks[i])) { 205 if (DontConsume) { 206 // Noop, don't consume the token. 207 } else { 208 ConsumeAnyToken(); 209 } 210 return true; 211 } 212 } 213 214 switch (Tok.getKind()) { 215 case tok::eof: 216 // Ran out of tokens. 217 return false; 218 219 case tok::code_completion: 220 ConsumeToken(); 221 return false; 222 223 case tok::l_paren: 224 // Recursively skip properly-nested parens. 225 ConsumeParen(); 226 SkipUntil(tok::r_paren, false); 227 break; 228 case tok::l_square: 229 // Recursively skip properly-nested square brackets. 230 ConsumeBracket(); 231 SkipUntil(tok::r_square, false); 232 break; 233 case tok::l_brace: 234 // Recursively skip properly-nested braces. 235 ConsumeBrace(); 236 SkipUntil(tok::r_brace, false); 237 break; 238 239 // Okay, we found a ']' or '}' or ')', which we think should be balanced. 240 // Since the user wasn't looking for this token (if they were, it would 241 // already be handled), this isn't balanced. If there is a LHS token at a 242 // higher level, we will assume that this matches the unbalanced token 243 // and return it. Otherwise, this is a spurious RHS token, which we skip. 244 case tok::r_paren: 245 if (ParenCount && !isFirstTokenSkipped) 246 return false; // Matches something. 247 ConsumeParen(); 248 break; 249 case tok::r_square: 250 if (BracketCount && !isFirstTokenSkipped) 251 return false; // Matches something. 252 ConsumeBracket(); 253 break; 254 case tok::r_brace: 255 if (BraceCount && !isFirstTokenSkipped) 256 return false; // Matches something. 257 ConsumeBrace(); 258 break; 259 260 case tok::string_literal: 261 case tok::wide_string_literal: 262 ConsumeStringToken(); 263 break; 264 case tok::semi: 265 if (StopAtSemi) 266 return false; 267 // FALL THROUGH. 268 default: 269 // Skip this token. 270 ConsumeToken(); 271 break; 272 } 273 isFirstTokenSkipped = false; 274 } 275} 276 277//===----------------------------------------------------------------------===// 278// Scope manipulation 279//===----------------------------------------------------------------------===// 280 281/// EnterScope - Start a new scope. 282void Parser::EnterScope(unsigned ScopeFlags) { 283 if (NumCachedScopes) { 284 Scope *N = ScopeCache[--NumCachedScopes]; 285 N->Init(getCurScope(), ScopeFlags); 286 Actions.CurScope = N; 287 } else { 288 Actions.CurScope = new Scope(getCurScope(), ScopeFlags); 289 } 290 getCurScope()->setNumErrorsAtStart(Diags.getNumErrors()); 291} 292 293/// ExitScope - Pop a scope off the scope stack. 294void Parser::ExitScope() { 295 assert(getCurScope() && "Scope imbalance!"); 296 297 // Inform the actions module that this scope is going away if there are any 298 // decls in it. 299 if (!getCurScope()->decl_empty()) 300 Actions.ActOnPopScope(Tok.getLocation(), getCurScope()); 301 302 Scope *OldScope = getCurScope(); 303 Actions.CurScope = OldScope->getParent(); 304 305 if (NumCachedScopes == ScopeCacheSize) 306 delete OldScope; 307 else 308 ScopeCache[NumCachedScopes++] = OldScope; 309} 310 311 312 313 314//===----------------------------------------------------------------------===// 315// C99 6.9: External Definitions. 316//===----------------------------------------------------------------------===// 317 318Parser::~Parser() { 319 // If we still have scopes active, delete the scope tree. 320 delete getCurScope(); 321 Actions.CurScope = 0; 322 323 // Free the scope cache. 324 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i) 325 delete ScopeCache[i]; 326 327 // Remove the pragma handlers we installed. 328 PP.RemovePragmaHandler(AlignHandler.get()); 329 AlignHandler.reset(); 330 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get()); 331 GCCVisibilityHandler.reset(); 332 PP.RemovePragmaHandler(OptionsHandler.get()); 333 OptionsHandler.reset(); 334 PP.RemovePragmaHandler(PackHandler.get()); 335 PackHandler.reset(); 336 PP.RemovePragmaHandler(UnusedHandler.get()); 337 UnusedHandler.reset(); 338 PP.RemovePragmaHandler(WeakHandler.get()); 339 WeakHandler.reset(); 340 PP.clearCodeCompletionHandler(); 341} 342 343/// Initialize - Warm up the parser. 344/// 345void Parser::Initialize() { 346 // Create the translation unit scope. Install it as the current scope. 347 assert(getCurScope() == 0 && "A scope is already active?"); 348 EnterScope(Scope::DeclScope); 349 Actions.ActOnTranslationUnitScope(getCurScope()); 350 351 // Prime the lexer look-ahead. 352 ConsumeToken(); 353 354 if (Tok.is(tok::eof) && 355 !getLang().CPlusPlus) // Empty source file is an extension in C 356 Diag(Tok, diag::ext_empty_source_file); 357 358 // Initialization for Objective-C context sensitive keywords recognition. 359 // Referenced in Parser::ParseObjCTypeQualifierList. 360 if (getLang().ObjC1) { 361 ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in"); 362 ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out"); 363 ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout"); 364 ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway"); 365 ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy"); 366 ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref"); 367 } 368 369 Ident_super = &PP.getIdentifierTable().get("super"); 370 371 if (getLang().AltiVec) { 372 Ident_vector = &PP.getIdentifierTable().get("vector"); 373 Ident_pixel = &PP.getIdentifierTable().get("pixel"); 374 } 375} 376 377/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the 378/// action tells us to. This returns true if the EOF was encountered. 379bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) { 380 Result = DeclGroupPtrTy(); 381 if (Tok.is(tok::eof)) { 382 Actions.ActOnEndOfTranslationUnit(); 383 return true; 384 } 385 386 CXX0XAttributeList Attr; 387 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 388 Attr = ParseCXX0XAttributes(); 389 Result = ParseExternalDeclaration(Attr); 390 return false; 391} 392 393/// ParseTranslationUnit: 394/// translation-unit: [C99 6.9] 395/// external-declaration 396/// translation-unit external-declaration 397void Parser::ParseTranslationUnit() { 398 Initialize(); 399 400 DeclGroupPtrTy Res; 401 while (!ParseTopLevelDecl(Res)) 402 /*parse them all*/; 403 404 ExitScope(); 405 assert(getCurScope() == 0 && "Scope imbalance!"); 406} 407 408/// ParseExternalDeclaration: 409/// 410/// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl] 411/// function-definition 412/// declaration 413/// [C++0x] empty-declaration 414/// [GNU] asm-definition 415/// [GNU] __extension__ external-declaration 416/// [OBJC] objc-class-definition 417/// [OBJC] objc-class-declaration 418/// [OBJC] objc-alias-declaration 419/// [OBJC] objc-protocol-definition 420/// [OBJC] objc-method-definition 421/// [OBJC] @end 422/// [C++] linkage-specification 423/// [GNU] asm-definition: 424/// simple-asm-expr ';' 425/// 426/// [C++0x] empty-declaration: 427/// ';' 428/// 429/// [C++0x/GNU] 'extern' 'template' declaration 430Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(CXX0XAttributeList Attr, 431 ParsingDeclSpec *DS) { 432 ParenBraceBracketBalancer BalancerRAIIObj(*this); 433 434 Decl *SingleDecl = 0; 435 switch (Tok.getKind()) { 436 case tok::semi: 437 if (!getLang().CPlusPlus0x) 438 Diag(Tok, diag::ext_top_level_semi) 439 << FixItHint::CreateRemoval(Tok.getLocation()); 440 441 ConsumeToken(); 442 // TODO: Invoke action for top-level semicolon. 443 return DeclGroupPtrTy(); 444 case tok::r_brace: 445 Diag(Tok, diag::err_expected_external_declaration); 446 ConsumeBrace(); 447 return DeclGroupPtrTy(); 448 case tok::eof: 449 Diag(Tok, diag::err_expected_external_declaration); 450 return DeclGroupPtrTy(); 451 case tok::kw___extension__: { 452 // __extension__ silences extension warnings in the subexpression. 453 ExtensionRAIIObject O(Diags); // Use RAII to do this. 454 ConsumeToken(); 455 return ParseExternalDeclaration(Attr); 456 } 457 case tok::kw_asm: { 458 if (Attr.HasAttr) 459 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) 460 << Attr.Range; 461 462 ExprResult Result(ParseSimpleAsm()); 463 464 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 465 "top-level asm block"); 466 467 if (Result.isInvalid()) 468 return DeclGroupPtrTy(); 469 SingleDecl = Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), Result.get()); 470 break; 471 } 472 case tok::at: 473 // @ is not a legal token unless objc is enabled, no need to check for ObjC. 474 /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like 475 /// @class foo, bar; 476 SingleDecl = ParseObjCAtDirectives(); 477 break; 478 case tok::minus: 479 case tok::plus: 480 if (!getLang().ObjC1) { 481 Diag(Tok, diag::err_expected_external_declaration); 482 ConsumeToken(); 483 return DeclGroupPtrTy(); 484 } 485 SingleDecl = ParseObjCMethodDefinition(); 486 break; 487 case tok::code_completion: 488 Actions.CodeCompleteOrdinaryName(getCurScope(), 489 ObjCImpDecl? Sema::PCC_ObjCImplementation 490 : Sema::PCC_Namespace); 491 ConsumeCodeCompletionToken(); 492 return ParseExternalDeclaration(Attr); 493 case tok::kw_using: 494 case tok::kw_namespace: 495 case tok::kw_typedef: 496 case tok::kw_template: 497 case tok::kw_export: // As in 'export template' 498 case tok::kw_static_assert: 499 // A function definition cannot start with a these keywords. 500 { 501 SourceLocation DeclEnd; 502 return ParseDeclaration(Declarator::FileContext, DeclEnd, Attr); 503 } 504 505 case tok::kw_inline: 506 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) { 507 // Inline namespaces. Allowed as an extension even in C++03. 508 SourceLocation DeclEnd; 509 return ParseDeclaration(Declarator::FileContext, DeclEnd, Attr); 510 } 511 goto dont_know; 512 513 case tok::kw_extern: 514 if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) { 515 // Extern templates 516 SourceLocation ExternLoc = ConsumeToken(); 517 SourceLocation TemplateLoc = ConsumeToken(); 518 SourceLocation DeclEnd; 519 return Actions.ConvertDeclToDeclGroup( 520 ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd)); 521 } 522 // FIXME: Detect C++ linkage specifications here? 523 goto dont_know; 524 525 default: 526 dont_know: 527 // We can't tell whether this is a function-definition or declaration yet. 528 if (DS) 529 return ParseDeclarationOrFunctionDefinition(*DS, Attr.AttrList); 530 else 531 return ParseDeclarationOrFunctionDefinition(Attr.AttrList); 532 } 533 534 // This routine returns a DeclGroup, if the thing we parsed only contains a 535 // single decl, convert it now. 536 return Actions.ConvertDeclToDeclGroup(SingleDecl); 537} 538 539/// \brief Determine whether the current token, if it occurs after a 540/// declarator, continues a declaration or declaration list. 541bool Parser::isDeclarationAfterDeclarator() const { 542 return Tok.is(tok::equal) || // int X()= -> not a function def 543 Tok.is(tok::comma) || // int X(), -> not a function def 544 Tok.is(tok::semi) || // int X(); -> not a function def 545 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def 546 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def 547 (getLang().CPlusPlus && 548 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++] 549} 550 551/// \brief Determine whether the current token, if it occurs after a 552/// declarator, indicates the start of a function definition. 553bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) { 554 assert(Declarator.getTypeObject(0).Kind == DeclaratorChunk::Function && 555 "Isn't a function declarator"); 556 if (Tok.is(tok::l_brace)) // int X() {} 557 return true; 558 559 // Handle K&R C argument lists: int X(f) int f; {} 560 if (!getLang().CPlusPlus && 561 Declarator.getTypeObject(0).Fun.isKNRPrototype()) 562 return isDeclarationSpecifier(); 563 564 return Tok.is(tok::colon) || // X() : Base() {} (used for ctors) 565 Tok.is(tok::kw_try); // X() try { ... } 566} 567 568/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or 569/// a declaration. We can't tell which we have until we read up to the 570/// compound-statement in function-definition. TemplateParams, if 571/// non-NULL, provides the template parameters when we're parsing a 572/// C++ template-declaration. 573/// 574/// function-definition: [C99 6.9.1] 575/// decl-specs declarator declaration-list[opt] compound-statement 576/// [C90] function-definition: [C99 6.7.1] - implicit int result 577/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 578/// 579/// declaration: [C99 6.7] 580/// declaration-specifiers init-declarator-list[opt] ';' 581/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode] 582/// [OMP] threadprivate-directive [TODO] 583/// 584Parser::DeclGroupPtrTy 585Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS, 586 AttributeList *Attr, 587 AccessSpecifier AS) { 588 // Parse the common declaration-specifiers piece. 589 if (Attr) 590 DS.AddAttributes(Attr); 591 592 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level); 593 594 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" 595 // declaration-specifiers init-declarator-list[opt] ';' 596 if (Tok.is(tok::semi)) { 597 ConsumeToken(); 598 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); 599 DS.complete(TheDecl); 600 return Actions.ConvertDeclToDeclGroup(TheDecl); 601 } 602 603 // ObjC2 allows prefix attributes on class interfaces and protocols. 604 // FIXME: This still needs better diagnostics. We should only accept 605 // attributes here, no types, etc. 606 if (getLang().ObjC2 && Tok.is(tok::at)) { 607 SourceLocation AtLoc = ConsumeToken(); // the "@" 608 if (!Tok.isObjCAtKeyword(tok::objc_interface) && 609 !Tok.isObjCAtKeyword(tok::objc_protocol)) { 610 Diag(Tok, diag::err_objc_unexpected_attr); 611 SkipUntil(tok::semi); // FIXME: better skip? 612 return DeclGroupPtrTy(); 613 } 614 615 DS.abort(); 616 617 const char *PrevSpec = 0; 618 unsigned DiagID; 619 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID)) 620 Diag(AtLoc, DiagID) << PrevSpec; 621 622 Decl *TheDecl = 0; 623 if (Tok.isObjCAtKeyword(tok::objc_protocol)) 624 TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes()); 625 else 626 TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()); 627 return Actions.ConvertDeclToDeclGroup(TheDecl); 628 } 629 630 // If the declspec consisted only of 'extern' and we have a string 631 // literal following it, this must be a C++ linkage specifier like 632 // 'extern "C"'. 633 if (Tok.is(tok::string_literal) && getLang().CPlusPlus && 634 DS.getStorageClassSpec() == DeclSpec::SCS_extern && 635 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) { 636 Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext); 637 return Actions.ConvertDeclToDeclGroup(TheDecl); 638 } 639 640 return ParseDeclGroup(DS, Declarator::FileContext, true); 641} 642 643Parser::DeclGroupPtrTy 644Parser::ParseDeclarationOrFunctionDefinition(AttributeList *Attr, 645 AccessSpecifier AS) { 646 ParsingDeclSpec DS(*this); 647 return ParseDeclarationOrFunctionDefinition(DS, Attr, AS); 648} 649 650/// ParseFunctionDefinition - We parsed and verified that the specified 651/// Declarator is well formed. If this is a K&R-style function, read the 652/// parameters declaration-list, then start the compound-statement. 653/// 654/// function-definition: [C99 6.9.1] 655/// decl-specs declarator declaration-list[opt] compound-statement 656/// [C90] function-definition: [C99 6.7.1] - implicit int result 657/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement 658/// [C++] function-definition: [C++ 8.4] 659/// decl-specifier-seq[opt] declarator ctor-initializer[opt] 660/// function-body 661/// [C++] function-definition: [C++ 8.4] 662/// decl-specifier-seq[opt] declarator function-try-block 663/// 664Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D, 665 const ParsedTemplateInfo &TemplateInfo) { 666 const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0); 667 assert(FnTypeInfo.Kind == DeclaratorChunk::Function && 668 "This isn't a function declarator!"); 669 const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun; 670 671 // If this is C90 and the declspecs were completely missing, fudge in an 672 // implicit int. We do this here because this is the only place where 673 // declaration-specifiers are completely optional in the grammar. 674 if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) { 675 const char *PrevSpec; 676 unsigned DiagID; 677 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int, 678 D.getIdentifierLoc(), 679 PrevSpec, DiagID); 680 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin()); 681 } 682 683 // If this declaration was formed with a K&R-style identifier list for the 684 // arguments, parse declarations for all of the args next. 685 // int foo(a,b) int a; float b; {} 686 if (FTI.isKNRPrototype()) 687 ParseKNRParamDeclarations(D); 688 689 // We should have either an opening brace or, in a C++ constructor, 690 // we may have a colon. 691 if (Tok.isNot(tok::l_brace) && Tok.isNot(tok::colon) && 692 Tok.isNot(tok::kw_try)) { 693 Diag(Tok, diag::err_expected_fn_body); 694 695 // Skip over garbage, until we get to '{'. Don't eat the '{'. 696 SkipUntil(tok::l_brace, true, true); 697 698 // If we didn't find the '{', bail out. 699 if (Tok.isNot(tok::l_brace)) 700 return 0; 701 } 702 703 // Enter a scope for the function body. 704 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); 705 706 // Tell the actions module that we have entered a function definition with the 707 // specified Declarator for the function. 708 Decl *Res = TemplateInfo.TemplateParams? 709 Actions.ActOnStartOfFunctionTemplateDef(getCurScope(), 710 MultiTemplateParamsArg(Actions, 711 TemplateInfo.TemplateParams->data(), 712 TemplateInfo.TemplateParams->size()), 713 D) 714 : Actions.ActOnStartOfFunctionDef(getCurScope(), D); 715 716 // Break out of the ParsingDeclarator context before we parse the body. 717 D.complete(Res); 718 719 // Break out of the ParsingDeclSpec context, too. This const_cast is 720 // safe because we're always the sole owner. 721 D.getMutableDeclSpec().abort(); 722 723 if (Tok.is(tok::kw_try)) 724 return ParseFunctionTryBlock(Res); 725 726 // If we have a colon, then we're probably parsing a C++ 727 // ctor-initializer. 728 if (Tok.is(tok::colon)) { 729 ParseConstructorInitializer(Res); 730 731 // Recover from error. 732 if (!Tok.is(tok::l_brace)) { 733 Actions.ActOnFinishFunctionBody(Res, 0); 734 return Res; 735 } 736 } else 737 Actions.ActOnDefaultCtorInitializers(Res); 738 739 return ParseFunctionStatementBody(Res); 740} 741 742/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides 743/// types for a function with a K&R-style identifier list for arguments. 744void Parser::ParseKNRParamDeclarations(Declarator &D) { 745 // We know that the top-level of this declarator is a function. 746 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; 747 748 // Enter function-declaration scope, limiting any declarators to the 749 // function prototype scope, including parameter declarators. 750 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope); 751 752 // Read all the argument declarations. 753 while (isDeclarationSpecifier()) { 754 SourceLocation DSStart = Tok.getLocation(); 755 756 // Parse the common declaration-specifiers piece. 757 DeclSpec DS; 758 ParseDeclarationSpecifiers(DS); 759 760 // C99 6.9.1p6: 'each declaration in the declaration list shall have at 761 // least one declarator'. 762 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with 763 // the declarations though. It's trivial to ignore them, really hard to do 764 // anything else with them. 765 if (Tok.is(tok::semi)) { 766 Diag(DSStart, diag::err_declaration_does_not_declare_param); 767 ConsumeToken(); 768 continue; 769 } 770 771 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other 772 // than register. 773 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && 774 DS.getStorageClassSpec() != DeclSpec::SCS_register) { 775 Diag(DS.getStorageClassSpecLoc(), 776 diag::err_invalid_storage_class_in_func_decl); 777 DS.ClearStorageClassSpecs(); 778 } 779 if (DS.isThreadSpecified()) { 780 Diag(DS.getThreadSpecLoc(), 781 diag::err_invalid_storage_class_in_func_decl); 782 DS.ClearStorageClassSpecs(); 783 } 784 785 // Parse the first declarator attached to this declspec. 786 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext); 787 ParseDeclarator(ParmDeclarator); 788 789 // Handle the full declarator list. 790 while (1) { 791 // If attributes are present, parse them. 792 if (Tok.is(tok::kw___attribute)) { 793 SourceLocation Loc; 794 AttributeList *AttrList = ParseGNUAttributes(&Loc); 795 ParmDeclarator.AddAttributes(AttrList, Loc); 796 } 797 798 // Ask the actions module to compute the type for this declarator. 799 Decl *Param = 800 Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); 801 802 if (Param && 803 // A missing identifier has already been diagnosed. 804 ParmDeclarator.getIdentifier()) { 805 806 // Scan the argument list looking for the correct param to apply this 807 // type. 808 for (unsigned i = 0; ; ++i) { 809 // C99 6.9.1p6: those declarators shall declare only identifiers from 810 // the identifier list. 811 if (i == FTI.NumArgs) { 812 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param) 813 << ParmDeclarator.getIdentifier(); 814 break; 815 } 816 817 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) { 818 // Reject redefinitions of parameters. 819 if (FTI.ArgInfo[i].Param) { 820 Diag(ParmDeclarator.getIdentifierLoc(), 821 diag::err_param_redefinition) 822 << ParmDeclarator.getIdentifier(); 823 } else { 824 FTI.ArgInfo[i].Param = Param; 825 } 826 break; 827 } 828 } 829 } 830 831 // If we don't have a comma, it is either the end of the list (a ';') or 832 // an error, bail out. 833 if (Tok.isNot(tok::comma)) 834 break; 835 836 // Consume the comma. 837 ConsumeToken(); 838 839 // Parse the next declarator. 840 ParmDeclarator.clear(); 841 ParseDeclarator(ParmDeclarator); 842 } 843 844 if (Tok.is(tok::semi)) { 845 ConsumeToken(); 846 } else { 847 Diag(Tok, diag::err_parse_error); 848 // Skip to end of block or statement 849 SkipUntil(tok::semi, true); 850 if (Tok.is(tok::semi)) 851 ConsumeToken(); 852 } 853 } 854 855 // The actions module must verify that all arguments were declared. 856 Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation()); 857} 858 859 860/// ParseAsmStringLiteral - This is just a normal string-literal, but is not 861/// allowed to be a wide string, and is not subject to character translation. 862/// 863/// [GNU] asm-string-literal: 864/// string-literal 865/// 866Parser::ExprResult Parser::ParseAsmStringLiteral() { 867 if (!isTokenStringLiteral()) { 868 Diag(Tok, diag::err_expected_string_literal); 869 return ExprError(); 870 } 871 872 ExprResult Res(ParseStringLiteralExpression()); 873 if (Res.isInvalid()) return move(Res); 874 875 // TODO: Diagnose: wide string literal in 'asm' 876 877 return move(Res); 878} 879 880/// ParseSimpleAsm 881/// 882/// [GNU] simple-asm-expr: 883/// 'asm' '(' asm-string-literal ')' 884/// 885Parser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) { 886 assert(Tok.is(tok::kw_asm) && "Not an asm!"); 887 SourceLocation Loc = ConsumeToken(); 888 889 if (Tok.is(tok::kw_volatile)) { 890 // Remove from the end of 'asm' to the end of 'volatile'. 891 SourceRange RemovalRange(PP.getLocForEndOfToken(Loc), 892 PP.getLocForEndOfToken(Tok.getLocation())); 893 894 Diag(Tok, diag::warn_file_asm_volatile) 895 << FixItHint::CreateRemoval(RemovalRange); 896 ConsumeToken(); 897 } 898 899 if (Tok.isNot(tok::l_paren)) { 900 Diag(Tok, diag::err_expected_lparen_after) << "asm"; 901 return ExprError(); 902 } 903 904 Loc = ConsumeParen(); 905 906 ExprResult Result(ParseAsmStringLiteral()); 907 908 if (Result.isInvalid()) { 909 SkipUntil(tok::r_paren, true, true); 910 if (EndLoc) 911 *EndLoc = Tok.getLocation(); 912 ConsumeAnyToken(); 913 } else { 914 Loc = MatchRHSPunctuation(tok::r_paren, Loc); 915 if (EndLoc) 916 *EndLoc = Loc; 917 } 918 919 return move(Result); 920} 921 922/// TryAnnotateTypeOrScopeToken - If the current token position is on a 923/// typename (possibly qualified in C++) or a C++ scope specifier not followed 924/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens 925/// with a single annotation token representing the typename or C++ scope 926/// respectively. 927/// This simplifies handling of C++ scope specifiers and allows efficient 928/// backtracking without the need to re-parse and resolve nested-names and 929/// typenames. 930/// It will mainly be called when we expect to treat identifiers as typenames 931/// (if they are typenames). For example, in C we do not expect identifiers 932/// inside expressions to be treated as typenames so it will not be called 933/// for expressions in C. 934/// The benefit for C/ObjC is that a typename will be annotated and 935/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName 936/// will not be called twice, once to check whether we have a declaration 937/// specifier, and another one to get the actual type inside 938/// ParseDeclarationSpecifiers). 939/// 940/// This returns true if an error occurred. 941/// 942/// Note that this routine emits an error if you call it with ::new or ::delete 943/// as the current tokens, so only call it in contexts where these are invalid. 944bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext) { 945 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) 946 || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) && 947 "Cannot be a type or scope token!"); 948 949 if (Tok.is(tok::kw_typename)) { 950 // Parse a C++ typename-specifier, e.g., "typename T::type". 951 // 952 // typename-specifier: 953 // 'typename' '::' [opt] nested-name-specifier identifier 954 // 'typename' '::' [opt] nested-name-specifier template [opt] 955 // simple-template-id 956 SourceLocation TypenameLoc = ConsumeToken(); 957 CXXScopeSpec SS; 958 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(), false)) 959 return true; 960 if (!SS.isSet()) { 961 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename); 962 return true; 963 } 964 965 TypeResult Ty; 966 if (Tok.is(tok::identifier)) { 967 // FIXME: check whether the next token is '<', first! 968 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 969 *Tok.getIdentifierInfo(), 970 Tok.getLocation()); 971 } else if (Tok.is(tok::annot_template_id)) { 972 TemplateIdAnnotation *TemplateId 973 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 974 if (TemplateId->Kind == TNK_Function_template) { 975 Diag(Tok, diag::err_typename_refers_to_non_type_template) 976 << Tok.getAnnotationRange(); 977 return true; 978 } 979 980 AnnotateTemplateIdTokenAsType(0); 981 assert(Tok.is(tok::annot_typename) && 982 "AnnotateTemplateIdTokenAsType isn't working properly"); 983 if (Tok.getAnnotationValue()) 984 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS, 985 SourceLocation(), 986 getTypeAnnotation(Tok)); 987 else 988 Ty = true; 989 } else { 990 Diag(Tok, diag::err_expected_type_name_after_typename) 991 << SS.getRange(); 992 return true; 993 } 994 995 SourceLocation EndLoc = Tok.getLastLoc(); 996 Tok.setKind(tok::annot_typename); 997 setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get()); 998 Tok.setAnnotationEndLoc(EndLoc); 999 Tok.setLocation(TypenameLoc); 1000 PP.AnnotateCachedTokens(Tok); 1001 return false; 1002 } 1003 1004 // Remembers whether the token was originally a scope annotation. 1005 bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope); 1006 1007 CXXScopeSpec SS; 1008 if (getLang().CPlusPlus) 1009 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 1010 return true; 1011 1012 if (Tok.is(tok::identifier)) { 1013 // Determine whether the identifier is a type name. 1014 if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(), 1015 Tok.getLocation(), getCurScope(), 1016 &SS)) { 1017 // This is a typename. Replace the current token in-place with an 1018 // annotation type token. 1019 Tok.setKind(tok::annot_typename); 1020 setTypeAnnotation(Tok, Ty); 1021 Tok.setAnnotationEndLoc(Tok.getLocation()); 1022 if (SS.isNotEmpty()) // it was a C++ qualified type name. 1023 Tok.setLocation(SS.getBeginLoc()); 1024 1025 // In case the tokens were cached, have Preprocessor replace 1026 // them with the annotation token. 1027 PP.AnnotateCachedTokens(Tok); 1028 return false; 1029 } 1030 1031 if (!getLang().CPlusPlus) { 1032 // If we're in C, we can't have :: tokens at all (the lexer won't return 1033 // them). If the identifier is not a type, then it can't be scope either, 1034 // just early exit. 1035 return false; 1036 } 1037 1038 // If this is a template-id, annotate with a template-id or type token. 1039 if (NextToken().is(tok::less)) { 1040 TemplateTy Template; 1041 UnqualifiedId TemplateName; 1042 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 1043 bool MemberOfUnknownSpecialization; 1044 if (TemplateNameKind TNK 1045 = Actions.isTemplateName(getCurScope(), SS, 1046 /*hasTemplateKeyword=*/false, TemplateName, 1047 /*ObjectType=*/ ParsedType(), 1048 EnteringContext, 1049 Template, MemberOfUnknownSpecialization)) { 1050 // Consume the identifier. 1051 ConsumeToken(); 1052 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName)) { 1053 // If an unrecoverable error occurred, we need to return true here, 1054 // because the token stream is in a damaged state. We may not return 1055 // a valid identifier. 1056 return true; 1057 } 1058 } 1059 } 1060 1061 // The current token, which is either an identifier or a 1062 // template-id, is not part of the annotation. Fall through to 1063 // push that token back into the stream and complete the C++ scope 1064 // specifier annotation. 1065 } 1066 1067 if (Tok.is(tok::annot_template_id)) { 1068 TemplateIdAnnotation *TemplateId 1069 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 1070 if (TemplateId->Kind == TNK_Type_template) { 1071 // A template-id that refers to a type was parsed into a 1072 // template-id annotation in a context where we weren't allowed 1073 // to produce a type annotation token. Update the template-id 1074 // annotation token to a type annotation token now. 1075 AnnotateTemplateIdTokenAsType(&SS); 1076 return false; 1077 } 1078 } 1079 1080 if (SS.isEmpty()) 1081 return false; 1082 1083 // A C++ scope specifier that isn't followed by a typename. 1084 // Push the current token back into the token stream (or revert it if it is 1085 // cached) and use an annotation scope token for current token. 1086 if (PP.isBacktrackEnabled()) 1087 PP.RevertCachedTokens(1); 1088 else 1089 PP.EnterToken(Tok); 1090 Tok.setKind(tok::annot_cxxscope); 1091 Tok.setAnnotationValue(SS.getScopeRep()); 1092 Tok.setAnnotationRange(SS.getRange()); 1093 1094 // In case the tokens were cached, have Preprocessor replace them 1095 // with the annotation token. We don't need to do this if we've 1096 // just reverted back to the state we were in before being called. 1097 if (!wasScopeAnnotation) 1098 PP.AnnotateCachedTokens(Tok); 1099 return false; 1100} 1101 1102/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only 1103/// annotates C++ scope specifiers and template-ids. This returns 1104/// true if the token was annotated or there was an error that could not be 1105/// recovered from. 1106/// 1107/// Note that this routine emits an error if you call it with ::new or ::delete 1108/// as the current tokens, so only call it in contexts where these are invalid. 1109bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) { 1110 assert(getLang().CPlusPlus && 1111 "Call sites of this function should be guarded by checking for C++"); 1112 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && 1113 "Cannot be a type or scope token!"); 1114 1115 CXXScopeSpec SS; 1116 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) 1117 return true; 1118 if (SS.isEmpty()) 1119 return false; 1120 1121 // Push the current token back into the token stream (or revert it if it is 1122 // cached) and use an annotation scope token for current token. 1123 if (PP.isBacktrackEnabled()) 1124 PP.RevertCachedTokens(1); 1125 else 1126 PP.EnterToken(Tok); 1127 Tok.setKind(tok::annot_cxxscope); 1128 Tok.setAnnotationValue(SS.getScopeRep()); 1129 Tok.setAnnotationRange(SS.getRange()); 1130 1131 // In case the tokens were cached, have Preprocessor replace them with the 1132 // annotation token. 1133 PP.AnnotateCachedTokens(Tok); 1134 return false; 1135} 1136 1137void Parser::CodeCompletionRecovery() { 1138 for (Scope *S = getCurScope(); S; S = S->getParent()) { 1139 if (S->getFlags() & Scope::FnScope) { 1140 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction); 1141 return; 1142 } 1143 1144 if (S->getFlags() & Scope::ClassScope) { 1145 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class); 1146 return; 1147 } 1148 } 1149 1150 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace); 1151} 1152 1153// Anchor the Parser::FieldCallback vtable to this translation unit. 1154// We use a spurious method instead of the destructor because 1155// destroying FieldCallbacks can actually be slightly 1156// performance-sensitive. 1157void Parser::FieldCallback::_anchor() { 1158} 1159 1160// Code-completion pass-through functions 1161 1162void Parser::CodeCompleteDirective(bool InConditional) { 1163 Actions.CodeCompletePreprocessorDirective(InConditional); 1164} 1165 1166void Parser::CodeCompleteInConditionalExclusion() { 1167 Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope()); 1168} 1169 1170void Parser::CodeCompleteMacroName(bool IsDefinition) { 1171 Actions.CodeCompletePreprocessorMacroName(IsDefinition); 1172} 1173 1174void Parser::CodeCompletePreprocessorExpression() { 1175 Actions.CodeCompletePreprocessorExpression(); 1176} 1177 1178void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro, 1179 MacroInfo *MacroInfo, 1180 unsigned ArgumentIndex) { 1181 Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo, 1182 ArgumentIndex); 1183} 1184 1185void Parser::CodeCompleteNaturalLanguage() { 1186 Actions.CodeCompleteNaturalLanguage(); 1187} 1188