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