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