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