1 //===--- Parser.cpp - C Language Family Parser ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements the Parser interfaces.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Parse/Parser.h"
14 #include "clang/AST/ASTConsumer.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclTemplate.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Parse/ParseDiagnostic.h"
19 #include "clang/Parse/RAIIObjectsForParser.h"
20 #include "clang/Sema/DeclSpec.h"
21 #include "clang/Sema/ParsedTemplate.h"
22 #include "clang/Sema/Scope.h"
23 #include "llvm/Support/Path.h"
24 using namespace clang;
25 
26 
27 namespace {
28 /// 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   bool HandleComment(Preprocessor &PP, SourceRange Comment) override {
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 = nullptr;
60   NumCachedScopes = 0;
61   CurParsedObjCImpl = nullptr;
62 
63   // Add #pragma handlers. These are removed and destroyed in the
64   // destructor.
65   initializePragmaHandlers();
66 
67   CommentSemaHandler.reset(new ActionCommentHandler(actions));
68   PP.addCommentHandler(CommentSemaHandler.get());
69 
70   PP.setCodeCompletionHandler(*this);
71 }
72 
73 DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
74   return Diags.Report(Loc, DiagID);
75 }
76 
77 DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
78   return Diag(Tok.getLocation(), DiagID);
79 }
80 
81 /// Emits a diagnostic suggesting parentheses surrounding a
82 /// given range.
83 ///
84 /// \param Loc The location where we'll emit the diagnostic.
85 /// \param DK The kind of diagnostic to emit.
86 /// \param ParenRange Source range enclosing code that should be parenthesized.
87 void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
88                                 SourceRange ParenRange) {
89   SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
90   if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
91     // We can't display the parentheses, so just dig the
92     // warning/error and return.
93     Diag(Loc, DK);
94     return;
95   }
96 
97   Diag(Loc, DK)
98     << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
99     << FixItHint::CreateInsertion(EndLoc, ")");
100 }
101 
102 static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
103   switch (ExpectedTok) {
104   case tok::semi:
105     return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ;
106   default: return false;
107   }
108 }
109 
110 bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
111                               StringRef Msg) {
112   if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
113     ConsumeAnyToken();
114     return false;
115   }
116 
117   // Detect common single-character typos and resume.
118   if (IsCommonTypo(ExpectedTok, Tok)) {
119     SourceLocation Loc = Tok.getLocation();
120     {
121       DiagnosticBuilder DB = Diag(Loc, DiagID);
122       DB << FixItHint::CreateReplacement(
123                 SourceRange(Loc), tok::getPunctuatorSpelling(ExpectedTok));
124       if (DiagID == diag::err_expected)
125         DB << ExpectedTok;
126       else if (DiagID == diag::err_expected_after)
127         DB << Msg << ExpectedTok;
128       else
129         DB << Msg;
130     }
131 
132     // Pretend there wasn't a problem.
133     ConsumeAnyToken();
134     return false;
135   }
136 
137   SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
138   const char *Spelling = nullptr;
139   if (EndLoc.isValid())
140     Spelling = tok::getPunctuatorSpelling(ExpectedTok);
141 
142   DiagnosticBuilder DB =
143       Spelling
144           ? Diag(EndLoc, DiagID) << FixItHint::CreateInsertion(EndLoc, Spelling)
145           : Diag(Tok, DiagID);
146   if (DiagID == diag::err_expected)
147     DB << ExpectedTok;
148   else if (DiagID == diag::err_expected_after)
149     DB << Msg << ExpectedTok;
150   else
151     DB << Msg;
152 
153   return true;
154 }
155 
156 bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
157   if (TryConsumeToken(tok::semi))
158     return false;
159 
160   if (Tok.is(tok::code_completion)) {
161     handleUnexpectedCodeCompletionToken();
162     return false;
163   }
164 
165   if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
166       NextToken().is(tok::semi)) {
167     Diag(Tok, diag::err_extraneous_token_before_semi)
168       << PP.getSpelling(Tok)
169       << FixItHint::CreateRemoval(Tok.getLocation());
170     ConsumeAnyToken(); // The ')' or ']'.
171     ConsumeToken(); // The ';'.
172     return false;
173   }
174 
175   return ExpectAndConsume(tok::semi, DiagID);
176 }
177 
178 void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, DeclSpec::TST TST) {
179   if (!Tok.is(tok::semi)) return;
180 
181   bool HadMultipleSemis = false;
182   SourceLocation StartLoc = Tok.getLocation();
183   SourceLocation EndLoc = Tok.getLocation();
184   ConsumeToken();
185 
186   while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) {
187     HadMultipleSemis = true;
188     EndLoc = Tok.getLocation();
189     ConsumeToken();
190   }
191 
192   // C++11 allows extra semicolons at namespace scope, but not in any of the
193   // other contexts.
194   if (Kind == OutsideFunction && getLangOpts().CPlusPlus) {
195     if (getLangOpts().CPlusPlus11)
196       Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi)
197           << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
198     else
199       Diag(StartLoc, diag::ext_extra_semi_cxx11)
200           << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
201     return;
202   }
203 
204   if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis)
205     Diag(StartLoc, diag::ext_extra_semi)
206         << Kind << DeclSpec::getSpecifierName(TST,
207                                     Actions.getASTContext().getPrintingPolicy())
208         << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
209   else
210     // A single semicolon is valid after a member function definition.
211     Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def)
212       << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
213 }
214 
215 bool Parser::expectIdentifier() {
216   if (Tok.is(tok::identifier))
217     return false;
218   if (const auto *II = Tok.getIdentifierInfo()) {
219     if (II->isCPlusPlusKeyword(getLangOpts())) {
220       Diag(Tok, diag::err_expected_token_instead_of_objcxx_keyword)
221           << tok::identifier << Tok.getIdentifierInfo();
222       // Objective-C++: Recover by treating this keyword as a valid identifier.
223       return false;
224     }
225   }
226   Diag(Tok, diag::err_expected) << tok::identifier;
227   return true;
228 }
229 
230 //===----------------------------------------------------------------------===//
231 // Error recovery.
232 //===----------------------------------------------------------------------===//
233 
234 static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) {
235   return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0;
236 }
237 
238 /// SkipUntil - Read tokens until we get to the specified token, then consume
239 /// it (unless no flag StopBeforeMatch).  Because we cannot guarantee that the
240 /// token will ever occur, this skips to the next token, or to some likely
241 /// good stopping point.  If StopAtSemi is true, skipping will stop at a ';'
242 /// character.
243 ///
244 /// If SkipUntil finds the specified token, it returns true, otherwise it
245 /// returns false.
246 bool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, SkipUntilFlags Flags) {
247   // We always want this function to skip at least one token if the first token
248   // isn't T and if not at EOF.
249   bool isFirstTokenSkipped = true;
250   while (1) {
251     // If we found one of the tokens, stop and return true.
252     for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) {
253       if (Tok.is(Toks[i])) {
254         if (HasFlagsSet(Flags, StopBeforeMatch)) {
255           // Noop, don't consume the token.
256         } else {
257           ConsumeAnyToken();
258         }
259         return true;
260       }
261     }
262 
263     // Important special case: The caller has given up and just wants us to
264     // skip the rest of the file. Do this without recursing, since we can
265     // get here precisely because the caller detected too much recursion.
266     if (Toks.size() == 1 && Toks[0] == tok::eof &&
267         !HasFlagsSet(Flags, StopAtSemi) &&
268         !HasFlagsSet(Flags, StopAtCodeCompletion)) {
269       while (Tok.isNot(tok::eof))
270         ConsumeAnyToken();
271       return true;
272     }
273 
274     switch (Tok.getKind()) {
275     case tok::eof:
276       // Ran out of tokens.
277       return false;
278 
279     case tok::annot_pragma_openmp:
280     case tok::annot_pragma_openmp_end:
281       // Stop before an OpenMP pragma boundary.
282       if (OpenMPDirectiveParsing)
283         return false;
284       ConsumeAnnotationToken();
285       break;
286     case tok::annot_module_begin:
287     case tok::annot_module_end:
288     case tok::annot_module_include:
289       // Stop before we change submodules. They generally indicate a "good"
290       // place to pick up parsing again (except in the special case where
291       // we're trying to skip to EOF).
292       return false;
293 
294     case tok::code_completion:
295       if (!HasFlagsSet(Flags, StopAtCodeCompletion))
296         handleUnexpectedCodeCompletionToken();
297       return false;
298 
299     case tok::l_paren:
300       // Recursively skip properly-nested parens.
301       ConsumeParen();
302       if (HasFlagsSet(Flags, StopAtCodeCompletion))
303         SkipUntil(tok::r_paren, StopAtCodeCompletion);
304       else
305         SkipUntil(tok::r_paren);
306       break;
307     case tok::l_square:
308       // Recursively skip properly-nested square brackets.
309       ConsumeBracket();
310       if (HasFlagsSet(Flags, StopAtCodeCompletion))
311         SkipUntil(tok::r_square, StopAtCodeCompletion);
312       else
313         SkipUntil(tok::r_square);
314       break;
315     case tok::l_brace:
316       // Recursively skip properly-nested braces.
317       ConsumeBrace();
318       if (HasFlagsSet(Flags, StopAtCodeCompletion))
319         SkipUntil(tok::r_brace, StopAtCodeCompletion);
320       else
321         SkipUntil(tok::r_brace);
322       break;
323     case tok::question:
324       // Recursively skip ? ... : pairs; these function as brackets. But
325       // still stop at a semicolon if requested.
326       ConsumeToken();
327       SkipUntil(tok::colon,
328                 SkipUntilFlags(unsigned(Flags) &
329                                unsigned(StopAtCodeCompletion | StopAtSemi)));
330       break;
331 
332     // Okay, we found a ']' or '}' or ')', which we think should be balanced.
333     // Since the user wasn't looking for this token (if they were, it would
334     // already be handled), this isn't balanced.  If there is a LHS token at a
335     // higher level, we will assume that this matches the unbalanced token
336     // and return it.  Otherwise, this is a spurious RHS token, which we skip.
337     case tok::r_paren:
338       if (ParenCount && !isFirstTokenSkipped)
339         return false;  // Matches something.
340       ConsumeParen();
341       break;
342     case tok::r_square:
343       if (BracketCount && !isFirstTokenSkipped)
344         return false;  // Matches something.
345       ConsumeBracket();
346       break;
347     case tok::r_brace:
348       if (BraceCount && !isFirstTokenSkipped)
349         return false;  // Matches something.
350       ConsumeBrace();
351       break;
352 
353     case tok::semi:
354       if (HasFlagsSet(Flags, StopAtSemi))
355         return false;
356       LLVM_FALLTHROUGH;
357     default:
358       // Skip this token.
359       ConsumeAnyToken();
360       break;
361     }
362     isFirstTokenSkipped = false;
363   }
364 }
365 
366 //===----------------------------------------------------------------------===//
367 // Scope manipulation
368 //===----------------------------------------------------------------------===//
369 
370 /// EnterScope - Start a new scope.
371 void Parser::EnterScope(unsigned ScopeFlags) {
372   if (NumCachedScopes) {
373     Scope *N = ScopeCache[--NumCachedScopes];
374     N->Init(getCurScope(), ScopeFlags);
375     Actions.CurScope = N;
376   } else {
377     Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
378   }
379 }
380 
381 /// ExitScope - Pop a scope off the scope stack.
382 void Parser::ExitScope() {
383   assert(getCurScope() && "Scope imbalance!");
384 
385   // Inform the actions module that this scope is going away if there are any
386   // decls in it.
387   Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
388 
389   Scope *OldScope = getCurScope();
390   Actions.CurScope = OldScope->getParent();
391 
392   if (NumCachedScopes == ScopeCacheSize)
393     delete OldScope;
394   else
395     ScopeCache[NumCachedScopes++] = OldScope;
396 }
397 
398 /// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
399 /// this object does nothing.
400 Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
401                                  bool ManageFlags)
402   : CurScope(ManageFlags ? Self->getCurScope() : nullptr) {
403   if (CurScope) {
404     OldFlags = CurScope->getFlags();
405     CurScope->setFlags(ScopeFlags);
406   }
407 }
408 
409 /// Restore the flags for the current scope to what they were before this
410 /// object overrode them.
411 Parser::ParseScopeFlags::~ParseScopeFlags() {
412   if (CurScope)
413     CurScope->setFlags(OldFlags);
414 }
415 
416 
417 //===----------------------------------------------------------------------===//
418 // C99 6.9: External Definitions.
419 //===----------------------------------------------------------------------===//
420 
421 Parser::~Parser() {
422   // If we still have scopes active, delete the scope tree.
423   delete getCurScope();
424   Actions.CurScope = nullptr;
425 
426   // Free the scope cache.
427   for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
428     delete ScopeCache[i];
429 
430   resetPragmaHandlers();
431 
432   PP.removeCommentHandler(CommentSemaHandler.get());
433 
434   PP.clearCodeCompletionHandler();
435 
436   DestroyTemplateIds();
437 }
438 
439 /// Initialize - Warm up the parser.
440 ///
441 void Parser::Initialize() {
442   // Create the translation unit scope.  Install it as the current scope.
443   assert(getCurScope() == nullptr && "A scope is already active?");
444   EnterScope(Scope::DeclScope);
445   Actions.ActOnTranslationUnitScope(getCurScope());
446 
447   // Initialization for Objective-C context sensitive keywords recognition.
448   // Referenced in Parser::ParseObjCTypeQualifierList.
449   if (getLangOpts().ObjC) {
450     ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
451     ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
452     ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
453     ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
454     ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
455     ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
456     ObjCTypeQuals[objc_nonnull] = &PP.getIdentifierTable().get("nonnull");
457     ObjCTypeQuals[objc_nullable] = &PP.getIdentifierTable().get("nullable");
458     ObjCTypeQuals[objc_null_unspecified]
459       = &PP.getIdentifierTable().get("null_unspecified");
460   }
461 
462   Ident_instancetype = nullptr;
463   Ident_final = nullptr;
464   Ident_sealed = nullptr;
465   Ident_override = nullptr;
466   Ident_GNU_final = nullptr;
467   Ident_import = nullptr;
468   Ident_module = nullptr;
469 
470   Ident_super = &PP.getIdentifierTable().get("super");
471 
472   Ident_vector = nullptr;
473   Ident_bool = nullptr;
474   Ident_pixel = nullptr;
475   if (getLangOpts().AltiVec || getLangOpts().ZVector) {
476     Ident_vector = &PP.getIdentifierTable().get("vector");
477     Ident_bool = &PP.getIdentifierTable().get("bool");
478   }
479   if (getLangOpts().AltiVec)
480     Ident_pixel = &PP.getIdentifierTable().get("pixel");
481 
482   Ident_introduced = nullptr;
483   Ident_deprecated = nullptr;
484   Ident_obsoleted = nullptr;
485   Ident_unavailable = nullptr;
486   Ident_strict = nullptr;
487   Ident_replacement = nullptr;
488 
489   Ident_language = Ident_defined_in = Ident_generated_declaration = nullptr;
490 
491   Ident__except = nullptr;
492 
493   Ident__exception_code = Ident__exception_info = nullptr;
494   Ident__abnormal_termination = Ident___exception_code = nullptr;
495   Ident___exception_info = Ident___abnormal_termination = nullptr;
496   Ident_GetExceptionCode = Ident_GetExceptionInfo = nullptr;
497   Ident_AbnormalTermination = nullptr;
498 
499   if(getLangOpts().Borland) {
500     Ident__exception_info        = PP.getIdentifierInfo("_exception_info");
501     Ident___exception_info       = PP.getIdentifierInfo("__exception_info");
502     Ident_GetExceptionInfo       = PP.getIdentifierInfo("GetExceptionInformation");
503     Ident__exception_code        = PP.getIdentifierInfo("_exception_code");
504     Ident___exception_code       = PP.getIdentifierInfo("__exception_code");
505     Ident_GetExceptionCode       = PP.getIdentifierInfo("GetExceptionCode");
506     Ident__abnormal_termination  = PP.getIdentifierInfo("_abnormal_termination");
507     Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
508     Ident_AbnormalTermination    = PP.getIdentifierInfo("AbnormalTermination");
509 
510     PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
511     PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
512     PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
513     PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
514     PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
515     PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
516     PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
517     PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
518     PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
519   }
520 
521   if (getLangOpts().CPlusPlusModules) {
522     Ident_import = PP.getIdentifierInfo("import");
523     Ident_module = PP.getIdentifierInfo("module");
524   }
525 
526   Actions.Initialize();
527 
528   // Prime the lexer look-ahead.
529   ConsumeToken();
530 }
531 
532 void Parser::DestroyTemplateIds() {
533   for (TemplateIdAnnotation *Id : TemplateIds)
534     Id->Destroy();
535   TemplateIds.clear();
536 }
537 
538 /// Parse the first top-level declaration in a translation unit.
539 ///
540 ///   translation-unit:
541 /// [C]     external-declaration
542 /// [C]     translation-unit external-declaration
543 /// [C++]   top-level-declaration-seq[opt]
544 /// [C++20] global-module-fragment[opt] module-declaration
545 ///                 top-level-declaration-seq[opt] private-module-fragment[opt]
546 ///
547 /// Note that in C, it is an error if there is no first declaration.
548 bool Parser::ParseFirstTopLevelDecl(DeclGroupPtrTy &Result) {
549   Actions.ActOnStartOfTranslationUnit();
550 
551   // C11 6.9p1 says translation units must have at least one top-level
552   // declaration. C++ doesn't have this restriction. We also don't want to
553   // complain if we have a precompiled header, although technically if the PCH
554   // is empty we should still emit the (pedantic) diagnostic.
555   bool NoTopLevelDecls = ParseTopLevelDecl(Result, true);
556   if (NoTopLevelDecls && !Actions.getASTContext().getExternalSource() &&
557       !getLangOpts().CPlusPlus)
558     Diag(diag::ext_empty_translation_unit);
559 
560   return NoTopLevelDecls;
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 ///
566 ///   top-level-declaration:
567 ///           declaration
568 /// [C++20]   module-import-declaration
569 bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result, bool IsFirstDecl) {
570   DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
571 
572   // Skip over the EOF token, flagging end of previous input for incremental
573   // processing
574   if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
575     ConsumeToken();
576 
577   Result = nullptr;
578   switch (Tok.getKind()) {
579   case tok::annot_pragma_unused:
580     HandlePragmaUnused();
581     return false;
582 
583   case tok::kw_export:
584     switch (NextToken().getKind()) {
585     case tok::kw_module:
586       goto module_decl;
587 
588     // Note: no need to handle kw_import here. We only form kw_import under
589     // the Modules TS, and in that case 'export import' is parsed as an
590     // export-declaration containing an import-declaration.
591 
592     // Recognize context-sensitive C++20 'export module' and 'export import'
593     // declarations.
594     case tok::identifier: {
595       IdentifierInfo *II = NextToken().getIdentifierInfo();
596       if ((II == Ident_module || II == Ident_import) &&
597           GetLookAheadToken(2).isNot(tok::coloncolon)) {
598         if (II == Ident_module)
599           goto module_decl;
600         else
601           goto import_decl;
602       }
603       break;
604     }
605 
606     default:
607       break;
608     }
609     break;
610 
611   case tok::kw_module:
612   module_decl:
613     Result = ParseModuleDecl(IsFirstDecl);
614     return false;
615 
616   // tok::kw_import is handled by ParseExternalDeclaration. (Under the Modules
617   // TS, an import can occur within an export block.)
618   import_decl: {
619     Decl *ImportDecl = ParseModuleImport(SourceLocation());
620     Result = Actions.ConvertDeclToDeclGroup(ImportDecl);
621     return false;
622   }
623 
624   case tok::annot_module_include:
625     Actions.ActOnModuleInclude(Tok.getLocation(),
626                                reinterpret_cast<Module *>(
627                                    Tok.getAnnotationValue()));
628     ConsumeAnnotationToken();
629     return false;
630 
631   case tok::annot_module_begin:
632     Actions.ActOnModuleBegin(Tok.getLocation(), reinterpret_cast<Module *>(
633                                                     Tok.getAnnotationValue()));
634     ConsumeAnnotationToken();
635     return false;
636 
637   case tok::annot_module_end:
638     Actions.ActOnModuleEnd(Tok.getLocation(), reinterpret_cast<Module *>(
639                                                   Tok.getAnnotationValue()));
640     ConsumeAnnotationToken();
641     return false;
642 
643   case tok::eof:
644     // Check whether -fmax-tokens= was reached.
645     if (PP.getMaxTokens() != 0 && PP.getTokenCount() > PP.getMaxTokens()) {
646       PP.Diag(Tok.getLocation(), diag::warn_max_tokens_total)
647           << PP.getTokenCount() << PP.getMaxTokens();
648       SourceLocation OverrideLoc = PP.getMaxTokensOverrideLoc();
649       if (OverrideLoc.isValid()) {
650         PP.Diag(OverrideLoc, diag::note_max_tokens_total_override);
651       }
652     }
653 
654     // Late template parsing can begin.
655     if (getLangOpts().DelayedTemplateParsing)
656       Actions.SetLateTemplateParser(LateTemplateParserCallback, nullptr,
657                                     this);
658     if (!PP.isIncrementalProcessingEnabled())
659       Actions.ActOnEndOfTranslationUnit();
660     //else don't tell Sema that we ended parsing: more input might come.
661     return true;
662 
663   case tok::identifier:
664     // C++2a [basic.link]p3:
665     //   A token sequence beginning with 'export[opt] module' or
666     //   'export[opt] import' and not immediately followed by '::'
667     //   is never interpreted as the declaration of a top-level-declaration.
668     if ((Tok.getIdentifierInfo() == Ident_module ||
669          Tok.getIdentifierInfo() == Ident_import) &&
670         NextToken().isNot(tok::coloncolon)) {
671       if (Tok.getIdentifierInfo() == Ident_module)
672         goto module_decl;
673       else
674         goto import_decl;
675     }
676     break;
677 
678   default:
679     break;
680   }
681 
682   ParsedAttributesWithRange attrs(AttrFactory);
683   MaybeParseCXX11Attributes(attrs);
684 
685   Result = ParseExternalDeclaration(attrs);
686   return false;
687 }
688 
689 /// ParseExternalDeclaration:
690 ///
691 ///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
692 ///         function-definition
693 ///         declaration
694 /// [GNU]   asm-definition
695 /// [GNU]   __extension__ external-declaration
696 /// [OBJC]  objc-class-definition
697 /// [OBJC]  objc-class-declaration
698 /// [OBJC]  objc-alias-declaration
699 /// [OBJC]  objc-protocol-definition
700 /// [OBJC]  objc-method-definition
701 /// [OBJC]  @end
702 /// [C++]   linkage-specification
703 /// [GNU] asm-definition:
704 ///         simple-asm-expr ';'
705 /// [C++11] empty-declaration
706 /// [C++11] attribute-declaration
707 ///
708 /// [C++11] empty-declaration:
709 ///           ';'
710 ///
711 /// [C++0x/GNU] 'extern' 'template' declaration
712 ///
713 /// [Modules-TS] module-import-declaration
714 ///
715 Parser::DeclGroupPtrTy
716 Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
717                                  ParsingDeclSpec *DS) {
718   DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
719   ParenBraceBracketBalancer BalancerRAIIObj(*this);
720 
721   if (PP.isCodeCompletionReached()) {
722     cutOffParsing();
723     return nullptr;
724   }
725 
726   Decl *SingleDecl = nullptr;
727   switch (Tok.getKind()) {
728   case tok::annot_pragma_vis:
729     HandlePragmaVisibility();
730     return nullptr;
731   case tok::annot_pragma_pack:
732     HandlePragmaPack();
733     return nullptr;
734   case tok::annot_pragma_msstruct:
735     HandlePragmaMSStruct();
736     return nullptr;
737   case tok::annot_pragma_align:
738     HandlePragmaAlign();
739     return nullptr;
740   case tok::annot_pragma_weak:
741     HandlePragmaWeak();
742     return nullptr;
743   case tok::annot_pragma_weakalias:
744     HandlePragmaWeakAlias();
745     return nullptr;
746   case tok::annot_pragma_redefine_extname:
747     HandlePragmaRedefineExtname();
748     return nullptr;
749   case tok::annot_pragma_fp_contract:
750     HandlePragmaFPContract();
751     return nullptr;
752   case tok::annot_pragma_fenv_access:
753     HandlePragmaFEnvAccess();
754     return nullptr;
755   case tok::annot_pragma_fp:
756     HandlePragmaFP();
757     break;
758   case tok::annot_pragma_opencl_extension:
759     HandlePragmaOpenCLExtension();
760     return nullptr;
761   case tok::annot_pragma_openmp: {
762     AccessSpecifier AS = AS_none;
763     return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, attrs);
764   }
765   case tok::annot_pragma_ms_pointers_to_members:
766     HandlePragmaMSPointersToMembers();
767     return nullptr;
768   case tok::annot_pragma_ms_vtordisp:
769     HandlePragmaMSVtorDisp();
770     return nullptr;
771   case tok::annot_pragma_ms_pragma:
772     HandlePragmaMSPragma();
773     return nullptr;
774   case tok::annot_pragma_dump:
775     HandlePragmaDump();
776     return nullptr;
777   case tok::annot_pragma_attribute:
778     HandlePragmaAttribute();
779     return nullptr;
780   case tok::semi:
781     // Either a C++11 empty-declaration or attribute-declaration.
782     SingleDecl =
783         Actions.ActOnEmptyDeclaration(getCurScope(), attrs, Tok.getLocation());
784     ConsumeExtraSemi(OutsideFunction);
785     break;
786   case tok::r_brace:
787     Diag(Tok, diag::err_extraneous_closing_brace);
788     ConsumeBrace();
789     return nullptr;
790   case tok::eof:
791     Diag(Tok, diag::err_expected_external_declaration);
792     return nullptr;
793   case tok::kw___extension__: {
794     // __extension__ silences extension warnings in the subexpression.
795     ExtensionRAIIObject O(Diags);  // Use RAII to do this.
796     ConsumeToken();
797     return ParseExternalDeclaration(attrs);
798   }
799   case tok::kw_asm: {
800     ProhibitAttributes(attrs);
801 
802     SourceLocation StartLoc = Tok.getLocation();
803     SourceLocation EndLoc;
804 
805     ExprResult Result(ParseSimpleAsm(/*ForAsmLabel*/ false, &EndLoc));
806 
807     // Check if GNU-style InlineAsm is disabled.
808     // Empty asm string is allowed because it will not introduce
809     // any assembly code.
810     if (!(getLangOpts().GNUAsm || Result.isInvalid())) {
811       const auto *SL = cast<StringLiteral>(Result.get());
812       if (!SL->getString().trim().empty())
813         Diag(StartLoc, diag::err_gnu_inline_asm_disabled);
814     }
815 
816     ExpectAndConsume(tok::semi, diag::err_expected_after,
817                      "top-level asm block");
818 
819     if (Result.isInvalid())
820       return nullptr;
821     SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
822     break;
823   }
824   case tok::at:
825     return ParseObjCAtDirectives(attrs);
826   case tok::minus:
827   case tok::plus:
828     if (!getLangOpts().ObjC) {
829       Diag(Tok, diag::err_expected_external_declaration);
830       ConsumeToken();
831       return nullptr;
832     }
833     SingleDecl = ParseObjCMethodDefinition();
834     break;
835   case tok::code_completion:
836     if (CurParsedObjCImpl) {
837       // Code-complete Objective-C methods even without leading '-'/'+' prefix.
838       Actions.CodeCompleteObjCMethodDecl(getCurScope(),
839                                          /*IsInstanceMethod=*/None,
840                                          /*ReturnType=*/nullptr);
841     }
842     Actions.CodeCompleteOrdinaryName(
843         getCurScope(),
844         CurParsedObjCImpl ? Sema::PCC_ObjCImplementation : Sema::PCC_Namespace);
845     cutOffParsing();
846     return nullptr;
847   case tok::kw_import:
848     SingleDecl = ParseModuleImport(SourceLocation());
849     break;
850   case tok::kw_export:
851     if (getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS) {
852       SingleDecl = ParseExportDeclaration();
853       break;
854     }
855     // This must be 'export template'. Parse it so we can diagnose our lack
856     // of support.
857     LLVM_FALLTHROUGH;
858   case tok::kw_using:
859   case tok::kw_namespace:
860   case tok::kw_typedef:
861   case tok::kw_template:
862   case tok::kw_static_assert:
863   case tok::kw__Static_assert:
864     // A function definition cannot start with any of these keywords.
865     {
866       SourceLocation DeclEnd;
867       return ParseDeclaration(DeclaratorContext::FileContext, DeclEnd, attrs);
868     }
869 
870   case tok::kw_static:
871     // Parse (then ignore) 'static' prior to a template instantiation. This is
872     // a GCC extension that we intentionally do not support.
873     if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
874       Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
875         << 0;
876       SourceLocation DeclEnd;
877       return ParseDeclaration(DeclaratorContext::FileContext, DeclEnd, attrs);
878     }
879     goto dont_know;
880 
881   case tok::kw_inline:
882     if (getLangOpts().CPlusPlus) {
883       tok::TokenKind NextKind = NextToken().getKind();
884 
885       // Inline namespaces. Allowed as an extension even in C++03.
886       if (NextKind == tok::kw_namespace) {
887         SourceLocation DeclEnd;
888         return ParseDeclaration(DeclaratorContext::FileContext, DeclEnd, attrs);
889       }
890 
891       // Parse (then ignore) 'inline' prior to a template instantiation. This is
892       // a GCC extension that we intentionally do not support.
893       if (NextKind == tok::kw_template) {
894         Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
895           << 1;
896         SourceLocation DeclEnd;
897         return ParseDeclaration(DeclaratorContext::FileContext, DeclEnd, attrs);
898       }
899     }
900     goto dont_know;
901 
902   case tok::kw_extern:
903     if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
904       // Extern templates
905       SourceLocation ExternLoc = ConsumeToken();
906       SourceLocation TemplateLoc = ConsumeToken();
907       Diag(ExternLoc, getLangOpts().CPlusPlus11 ?
908              diag::warn_cxx98_compat_extern_template :
909              diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
910       SourceLocation DeclEnd;
911       return Actions.ConvertDeclToDeclGroup(
912           ParseExplicitInstantiation(DeclaratorContext::FileContext, ExternLoc,
913                                      TemplateLoc, DeclEnd, attrs));
914     }
915     goto dont_know;
916 
917   case tok::kw___if_exists:
918   case tok::kw___if_not_exists:
919     ParseMicrosoftIfExistsExternalDeclaration();
920     return nullptr;
921 
922   case tok::kw_module:
923     Diag(Tok, diag::err_unexpected_module_decl);
924     SkipUntil(tok::semi);
925     return nullptr;
926 
927   default:
928   dont_know:
929     if (Tok.isEditorPlaceholder()) {
930       ConsumeToken();
931       return nullptr;
932     }
933     // We can't tell whether this is a function-definition or declaration yet.
934     return ParseDeclarationOrFunctionDefinition(attrs, DS);
935   }
936 
937   // This routine returns a DeclGroup, if the thing we parsed only contains a
938   // single decl, convert it now.
939   return Actions.ConvertDeclToDeclGroup(SingleDecl);
940 }
941 
942 /// Determine whether the current token, if it occurs after a
943 /// declarator, continues a declaration or declaration list.
944 bool Parser::isDeclarationAfterDeclarator() {
945   // Check for '= delete' or '= default'
946   if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
947     const Token &KW = NextToken();
948     if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
949       return false;
950   }
951 
952   return Tok.is(tok::equal) ||      // int X()=  -> not a function def
953     Tok.is(tok::comma) ||           // int X(),  -> not a function def
954     Tok.is(tok::semi)  ||           // int X();  -> not a function def
955     Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
956     Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
957     (getLangOpts().CPlusPlus &&
958      Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
959 }
960 
961 /// Determine whether the current token, if it occurs after a
962 /// declarator, indicates the start of a function definition.
963 bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
964   assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
965   if (Tok.is(tok::l_brace))   // int X() {}
966     return true;
967 
968   // Handle K&R C argument lists: int X(f) int f; {}
969   if (!getLangOpts().CPlusPlus &&
970       Declarator.getFunctionTypeInfo().isKNRPrototype())
971     return isDeclarationSpecifier();
972 
973   if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
974     const Token &KW = NextToken();
975     return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
976   }
977 
978   return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
979          Tok.is(tok::kw_try);          // X() try { ... }
980 }
981 
982 /// Parse either a function-definition or a declaration.  We can't tell which
983 /// we have until we read up to the compound-statement in function-definition.
984 /// TemplateParams, if non-NULL, provides the template parameters when we're
985 /// parsing a C++ template-declaration.
986 ///
987 ///       function-definition: [C99 6.9.1]
988 ///         decl-specs      declarator declaration-list[opt] compound-statement
989 /// [C90] function-definition: [C99 6.7.1] - implicit int result
990 /// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
991 ///
992 ///       declaration: [C99 6.7]
993 ///         declaration-specifiers init-declarator-list[opt] ';'
994 /// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
995 /// [OMP]   threadprivate-directive
996 /// [OMP]   allocate-directive                         [TODO]
997 ///
998 Parser::DeclGroupPtrTy
999 Parser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs,
1000                                        ParsingDeclSpec &DS,
1001                                        AccessSpecifier AS) {
1002   MaybeParseMicrosoftAttributes(DS.getAttributes());
1003   // Parse the common declaration-specifiers piece.
1004   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS,
1005                              DeclSpecContext::DSC_top_level);
1006 
1007   // If we had a free-standing type definition with a missing semicolon, we
1008   // may get this far before the problem becomes obvious.
1009   if (DS.hasTagDefinition() && DiagnoseMissingSemiAfterTagDefinition(
1010                                    DS, AS, DeclSpecContext::DSC_top_level))
1011     return nullptr;
1012 
1013   // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1014   // declaration-specifiers init-declarator-list[opt] ';'
1015   if (Tok.is(tok::semi)) {
1016     auto LengthOfTSTToken = [](DeclSpec::TST TKind) {
1017       assert(DeclSpec::isDeclRep(TKind));
1018       switch(TKind) {
1019       case DeclSpec::TST_class:
1020         return 5;
1021       case DeclSpec::TST_struct:
1022         return 6;
1023       case DeclSpec::TST_union:
1024         return 5;
1025       case DeclSpec::TST_enum:
1026         return 4;
1027       case DeclSpec::TST_interface:
1028         return 9;
1029       default:
1030         llvm_unreachable("we only expect to get the length of the class/struct/union/enum");
1031       }
1032 
1033     };
1034     // Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]'
1035     SourceLocation CorrectLocationForAttributes =
1036         DeclSpec::isDeclRep(DS.getTypeSpecType())
1037             ? DS.getTypeSpecTypeLoc().getLocWithOffset(
1038                   LengthOfTSTToken(DS.getTypeSpecType()))
1039             : SourceLocation();
1040     ProhibitAttributes(attrs, CorrectLocationForAttributes);
1041     ConsumeToken();
1042     RecordDecl *AnonRecord = nullptr;
1043     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
1044                                                        DS, AnonRecord);
1045     DS.complete(TheDecl);
1046     if (getLangOpts().OpenCL)
1047       Actions.setCurrentOpenCLExtensionForDecl(TheDecl);
1048     if (AnonRecord) {
1049       Decl* decls[] = {AnonRecord, TheDecl};
1050       return Actions.BuildDeclaratorGroup(decls);
1051     }
1052     return Actions.ConvertDeclToDeclGroup(TheDecl);
1053   }
1054 
1055   DS.takeAttributesFrom(attrs);
1056 
1057   // ObjC2 allows prefix attributes on class interfaces and protocols.
1058   // FIXME: This still needs better diagnostics. We should only accept
1059   // attributes here, no types, etc.
1060   if (getLangOpts().ObjC && Tok.is(tok::at)) {
1061     SourceLocation AtLoc = ConsumeToken(); // the "@"
1062     if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
1063         !Tok.isObjCAtKeyword(tok::objc_protocol) &&
1064         !Tok.isObjCAtKeyword(tok::objc_implementation)) {
1065       Diag(Tok, diag::err_objc_unexpected_attr);
1066       SkipUntil(tok::semi);
1067       return nullptr;
1068     }
1069 
1070     DS.abort();
1071 
1072     const char *PrevSpec = nullptr;
1073     unsigned DiagID;
1074     if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID,
1075                            Actions.getASTContext().getPrintingPolicy()))
1076       Diag(AtLoc, DiagID) << PrevSpec;
1077 
1078     if (Tok.isObjCAtKeyword(tok::objc_protocol))
1079       return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
1080 
1081     if (Tok.isObjCAtKeyword(tok::objc_implementation))
1082       return ParseObjCAtImplementationDeclaration(AtLoc, DS.getAttributes());
1083 
1084     return Actions.ConvertDeclToDeclGroup(
1085             ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
1086   }
1087 
1088   // If the declspec consisted only of 'extern' and we have a string
1089   // literal following it, this must be a C++ linkage specifier like
1090   // 'extern "C"'.
1091   if (getLangOpts().CPlusPlus && isTokenStringLiteral() &&
1092       DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
1093       DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
1094     Decl *TheDecl = ParseLinkage(DS, DeclaratorContext::FileContext);
1095     return Actions.ConvertDeclToDeclGroup(TheDecl);
1096   }
1097 
1098   return ParseDeclGroup(DS, DeclaratorContext::FileContext);
1099 }
1100 
1101 Parser::DeclGroupPtrTy
1102 Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs,
1103                                              ParsingDeclSpec *DS,
1104                                              AccessSpecifier AS) {
1105   if (DS) {
1106     return ParseDeclOrFunctionDefInternal(attrs, *DS, AS);
1107   } else {
1108     ParsingDeclSpec PDS(*this);
1109     // Must temporarily exit the objective-c container scope for
1110     // parsing c constructs and re-enter objc container scope
1111     // afterwards.
1112     ObjCDeclContextSwitch ObjCDC(*this);
1113 
1114     return ParseDeclOrFunctionDefInternal(attrs, PDS, AS);
1115   }
1116 }
1117 
1118 /// ParseFunctionDefinition - We parsed and verified that the specified
1119 /// Declarator is well formed.  If this is a K&R-style function, read the
1120 /// parameters declaration-list, then start the compound-statement.
1121 ///
1122 ///       function-definition: [C99 6.9.1]
1123 ///         decl-specs      declarator declaration-list[opt] compound-statement
1124 /// [C90] function-definition: [C99 6.7.1] - implicit int result
1125 /// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
1126 /// [C++] function-definition: [C++ 8.4]
1127 ///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
1128 ///         function-body
1129 /// [C++] function-definition: [C++ 8.4]
1130 ///         decl-specifier-seq[opt] declarator function-try-block
1131 ///
1132 Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
1133                                       const ParsedTemplateInfo &TemplateInfo,
1134                                       LateParsedAttrList *LateParsedAttrs) {
1135   // Poison SEH identifiers so they are flagged as illegal in function bodies.
1136   PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
1137   const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
1138   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
1139 
1140   // If this is C90 and the declspecs were completely missing, fudge in an
1141   // implicit int.  We do this here because this is the only place where
1142   // declaration-specifiers are completely optional in the grammar.
1143   if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
1144     const char *PrevSpec;
1145     unsigned DiagID;
1146     const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
1147     D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
1148                                            D.getIdentifierLoc(),
1149                                            PrevSpec, DiagID,
1150                                            Policy);
1151     D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
1152   }
1153 
1154   // If this declaration was formed with a K&R-style identifier list for the
1155   // arguments, parse declarations for all of the args next.
1156   // int foo(a,b) int a; float b; {}
1157   if (FTI.isKNRPrototype())
1158     ParseKNRParamDeclarations(D);
1159 
1160   // We should have either an opening brace or, in a C++ constructor,
1161   // we may have a colon.
1162   if (Tok.isNot(tok::l_brace) &&
1163       (!getLangOpts().CPlusPlus ||
1164        (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
1165         Tok.isNot(tok::equal)))) {
1166     Diag(Tok, diag::err_expected_fn_body);
1167 
1168     // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1169     SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
1170 
1171     // If we didn't find the '{', bail out.
1172     if (Tok.isNot(tok::l_brace))
1173       return nullptr;
1174   }
1175 
1176   // Check to make sure that any normal attributes are allowed to be on
1177   // a definition.  Late parsed attributes are checked at the end.
1178   if (Tok.isNot(tok::equal)) {
1179     for (const ParsedAttr &AL : D.getAttributes())
1180       if (AL.isKnownToGCC() && !AL.isCXX11Attribute())
1181         Diag(AL.getLoc(), diag::warn_attribute_on_function_definition) << AL;
1182   }
1183 
1184   // In delayed template parsing mode, for function template we consume the
1185   // tokens and store them for late parsing at the end of the translation unit.
1186   if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) &&
1187       TemplateInfo.Kind == ParsedTemplateInfo::Template &&
1188       Actions.canDelayFunctionBody(D)) {
1189     MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
1190 
1191     ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
1192                                    Scope::CompoundStmtScope);
1193     Scope *ParentScope = getCurScope()->getParent();
1194 
1195     D.setFunctionDefinitionKind(FDK_Definition);
1196     Decl *DP = Actions.HandleDeclarator(ParentScope, D,
1197                                         TemplateParameterLists);
1198     D.complete(DP);
1199     D.getMutableDeclSpec().abort();
1200 
1201     if (SkipFunctionBodies && (!DP || Actions.canSkipFunctionBody(DP)) &&
1202         trySkippingFunctionBody()) {
1203       BodyScope.Exit();
1204       return Actions.ActOnSkippedFunctionBody(DP);
1205     }
1206 
1207     CachedTokens Toks;
1208     LexTemplateFunctionForLateParsing(Toks);
1209 
1210     if (DP) {
1211       FunctionDecl *FnD = DP->getAsFunction();
1212       Actions.CheckForFunctionRedefinition(FnD);
1213       Actions.MarkAsLateParsedTemplate(FnD, DP, Toks);
1214     }
1215     return DP;
1216   }
1217   else if (CurParsedObjCImpl &&
1218            !TemplateInfo.TemplateParams &&
1219            (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
1220             Tok.is(tok::colon)) &&
1221       Actions.CurContext->isTranslationUnit()) {
1222     ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
1223                                    Scope::CompoundStmtScope);
1224     Scope *ParentScope = getCurScope()->getParent();
1225 
1226     D.setFunctionDefinitionKind(FDK_Definition);
1227     Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
1228                                               MultiTemplateParamsArg());
1229     D.complete(FuncDecl);
1230     D.getMutableDeclSpec().abort();
1231     if (FuncDecl) {
1232       // Consume the tokens and store them for later parsing.
1233       StashAwayMethodOrFunctionBodyTokens(FuncDecl);
1234       CurParsedObjCImpl->HasCFunction = true;
1235       return FuncDecl;
1236     }
1237     // FIXME: Should we really fall through here?
1238   }
1239 
1240   // Enter a scope for the function body.
1241   ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
1242                                  Scope::CompoundStmtScope);
1243 
1244   // Tell the actions module that we have entered a function definition with the
1245   // specified Declarator for the function.
1246   Sema::SkipBodyInfo SkipBody;
1247   Decl *Res = Actions.ActOnStartOfFunctionDef(getCurScope(), D,
1248                                               TemplateInfo.TemplateParams
1249                                                   ? *TemplateInfo.TemplateParams
1250                                                   : MultiTemplateParamsArg(),
1251                                               &SkipBody);
1252 
1253   if (SkipBody.ShouldSkip) {
1254     SkipFunctionBody();
1255     return Res;
1256   }
1257 
1258   // Break out of the ParsingDeclarator context before we parse the body.
1259   D.complete(Res);
1260 
1261   // Break out of the ParsingDeclSpec context, too.  This const_cast is
1262   // safe because we're always the sole owner.
1263   D.getMutableDeclSpec().abort();
1264 
1265   // With abbreviated function templates - we need to explicitly add depth to
1266   // account for the implicit template parameter list induced by the template.
1267   if (auto *Template = dyn_cast_or_null<FunctionTemplateDecl>(Res))
1268     if (Template->isAbbreviated() &&
1269         Template->getTemplateParameters()->getParam(0)->isImplicit())
1270       // First template parameter is implicit - meaning no explicit template
1271       // parameter list was specified.
1272       CurTemplateDepthTracker.addDepth(1);
1273 
1274   if (TryConsumeToken(tok::equal)) {
1275     assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
1276 
1277     bool Delete = false;
1278     SourceLocation KWLoc;
1279     if (TryConsumeToken(tok::kw_delete, KWLoc)) {
1280       Diag(KWLoc, getLangOpts().CPlusPlus11
1281                       ? diag::warn_cxx98_compat_defaulted_deleted_function
1282                       : diag::ext_defaulted_deleted_function)
1283         << 1 /* deleted */;
1284       Actions.SetDeclDeleted(Res, KWLoc);
1285       Delete = true;
1286     } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
1287       Diag(KWLoc, getLangOpts().CPlusPlus11
1288                       ? diag::warn_cxx98_compat_defaulted_deleted_function
1289                       : diag::ext_defaulted_deleted_function)
1290         << 0 /* defaulted */;
1291       Actions.SetDeclDefaulted(Res, KWLoc);
1292     } else {
1293       llvm_unreachable("function definition after = not 'delete' or 'default'");
1294     }
1295 
1296     if (Tok.is(tok::comma)) {
1297       Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
1298         << Delete;
1299       SkipUntil(tok::semi);
1300     } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
1301                                 Delete ? "delete" : "default")) {
1302       SkipUntil(tok::semi);
1303     }
1304 
1305     Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;
1306     Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);
1307     return Res;
1308   }
1309 
1310   if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) &&
1311       trySkippingFunctionBody()) {
1312     BodyScope.Exit();
1313     Actions.ActOnSkippedFunctionBody(Res);
1314     return Actions.ActOnFinishFunctionBody(Res, nullptr, false);
1315   }
1316 
1317   if (Tok.is(tok::kw_try))
1318     return ParseFunctionTryBlock(Res, BodyScope);
1319 
1320   // If we have a colon, then we're probably parsing a C++
1321   // ctor-initializer.
1322   if (Tok.is(tok::colon)) {
1323     ParseConstructorInitializer(Res);
1324 
1325     // Recover from error.
1326     if (!Tok.is(tok::l_brace)) {
1327       BodyScope.Exit();
1328       Actions.ActOnFinishFunctionBody(Res, nullptr);
1329       return Res;
1330     }
1331   } else
1332     Actions.ActOnDefaultCtorInitializers(Res);
1333 
1334   // Late attributes are parsed in the same scope as the function body.
1335   if (LateParsedAttrs)
1336     ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1337 
1338   return ParseFunctionStatementBody(Res, BodyScope);
1339 }
1340 
1341 void Parser::SkipFunctionBody() {
1342   if (Tok.is(tok::equal)) {
1343     SkipUntil(tok::semi);
1344     return;
1345   }
1346 
1347   bool IsFunctionTryBlock = Tok.is(tok::kw_try);
1348   if (IsFunctionTryBlock)
1349     ConsumeToken();
1350 
1351   CachedTokens Skipped;
1352   if (ConsumeAndStoreFunctionPrologue(Skipped))
1353     SkipMalformedDecl();
1354   else {
1355     SkipUntil(tok::r_brace);
1356     while (IsFunctionTryBlock && Tok.is(tok::kw_catch)) {
1357       SkipUntil(tok::l_brace);
1358       SkipUntil(tok::r_brace);
1359     }
1360   }
1361 }
1362 
1363 /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
1364 /// types for a function with a K&R-style identifier list for arguments.
1365 void Parser::ParseKNRParamDeclarations(Declarator &D) {
1366   // We know that the top-level of this declarator is a function.
1367   DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
1368 
1369   // Enter function-declaration scope, limiting any declarators to the
1370   // function prototype scope, including parameter declarators.
1371   ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1372                             Scope::FunctionDeclarationScope | Scope::DeclScope);
1373 
1374   // Read all the argument declarations.
1375   while (isDeclarationSpecifier()) {
1376     SourceLocation DSStart = Tok.getLocation();
1377 
1378     // Parse the common declaration-specifiers piece.
1379     DeclSpec DS(AttrFactory);
1380     ParseDeclarationSpecifiers(DS);
1381 
1382     // C99 6.9.1p6: 'each declaration in the declaration list shall have at
1383     // least one declarator'.
1384     // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
1385     // the declarations though.  It's trivial to ignore them, really hard to do
1386     // anything else with them.
1387     if (TryConsumeToken(tok::semi)) {
1388       Diag(DSStart, diag::err_declaration_does_not_declare_param);
1389       continue;
1390     }
1391 
1392     // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
1393     // than register.
1394     if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1395         DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1396       Diag(DS.getStorageClassSpecLoc(),
1397            diag::err_invalid_storage_class_in_func_decl);
1398       DS.ClearStorageClassSpecs();
1399     }
1400     if (DS.getThreadStorageClassSpec() != DeclSpec::TSCS_unspecified) {
1401       Diag(DS.getThreadStorageClassSpecLoc(),
1402            diag::err_invalid_storage_class_in_func_decl);
1403       DS.ClearStorageClassSpecs();
1404     }
1405 
1406     // Parse the first declarator attached to this declspec.
1407     Declarator ParmDeclarator(DS, DeclaratorContext::KNRTypeListContext);
1408     ParseDeclarator(ParmDeclarator);
1409 
1410     // Handle the full declarator list.
1411     while (1) {
1412       // If attributes are present, parse them.
1413       MaybeParseGNUAttributes(ParmDeclarator);
1414 
1415       // Ask the actions module to compute the type for this declarator.
1416       Decl *Param =
1417         Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
1418 
1419       if (Param &&
1420           // A missing identifier has already been diagnosed.
1421           ParmDeclarator.getIdentifier()) {
1422 
1423         // Scan the argument list looking for the correct param to apply this
1424         // type.
1425         for (unsigned i = 0; ; ++i) {
1426           // C99 6.9.1p6: those declarators shall declare only identifiers from
1427           // the identifier list.
1428           if (i == FTI.NumParams) {
1429             Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
1430               << ParmDeclarator.getIdentifier();
1431             break;
1432           }
1433 
1434           if (FTI.Params[i].Ident == ParmDeclarator.getIdentifier()) {
1435             // Reject redefinitions of parameters.
1436             if (FTI.Params[i].Param) {
1437               Diag(ParmDeclarator.getIdentifierLoc(),
1438                    diag::err_param_redefinition)
1439                  << ParmDeclarator.getIdentifier();
1440             } else {
1441               FTI.Params[i].Param = Param;
1442             }
1443             break;
1444           }
1445         }
1446       }
1447 
1448       // If we don't have a comma, it is either the end of the list (a ';') or
1449       // an error, bail out.
1450       if (Tok.isNot(tok::comma))
1451         break;
1452 
1453       ParmDeclarator.clear();
1454 
1455       // Consume the comma.
1456       ParmDeclarator.setCommaLoc(ConsumeToken());
1457 
1458       // Parse the next declarator.
1459       ParseDeclarator(ParmDeclarator);
1460     }
1461 
1462     // Consume ';' and continue parsing.
1463     if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration))
1464       continue;
1465 
1466     // Otherwise recover by skipping to next semi or mandatory function body.
1467     if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch))
1468       break;
1469     TryConsumeToken(tok::semi);
1470   }
1471 
1472   // The actions module must verify that all arguments were declared.
1473   Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
1474 }
1475 
1476 
1477 /// ParseAsmStringLiteral - This is just a normal string-literal, but is not
1478 /// allowed to be a wide string, and is not subject to character translation.
1479 /// Unlike GCC, we also diagnose an empty string literal when parsing for an
1480 /// asm label as opposed to an asm statement, because such a construct does not
1481 /// behave well.
1482 ///
1483 /// [GNU] asm-string-literal:
1484 ///         string-literal
1485 ///
1486 ExprResult Parser::ParseAsmStringLiteral(bool ForAsmLabel) {
1487   if (!isTokenStringLiteral()) {
1488     Diag(Tok, diag::err_expected_string_literal)
1489       << /*Source='in...'*/0 << "'asm'";
1490     return ExprError();
1491   }
1492 
1493   ExprResult AsmString(ParseStringLiteralExpression());
1494   if (!AsmString.isInvalid()) {
1495     const auto *SL = cast<StringLiteral>(AsmString.get());
1496     if (!SL->isAscii()) {
1497       Diag(Tok, diag::err_asm_operand_wide_string_literal)
1498         << SL->isWide()
1499         << SL->getSourceRange();
1500       return ExprError();
1501     }
1502     if (ForAsmLabel && SL->getString().empty()) {
1503       Diag(Tok, diag::err_asm_operand_wide_string_literal)
1504           << 2 /* an empty */ << SL->getSourceRange();
1505       return ExprError();
1506     }
1507   }
1508   return AsmString;
1509 }
1510 
1511 /// ParseSimpleAsm
1512 ///
1513 /// [GNU] simple-asm-expr:
1514 ///         'asm' '(' asm-string-literal ')'
1515 ///
1516 ExprResult Parser::ParseSimpleAsm(bool ForAsmLabel, SourceLocation *EndLoc) {
1517   assert(Tok.is(tok::kw_asm) && "Not an asm!");
1518   SourceLocation Loc = ConsumeToken();
1519 
1520   if (isGNUAsmQualifier(Tok)) {
1521     // Remove from the end of 'asm' to the end of the asm qualifier.
1522     SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1523                              PP.getLocForEndOfToken(Tok.getLocation()));
1524     Diag(Tok, diag::err_global_asm_qualifier_ignored)
1525         << GNUAsmQualifiers::getQualifierName(getGNUAsmQualifier(Tok))
1526         << FixItHint::CreateRemoval(RemovalRange);
1527     ConsumeToken();
1528   }
1529 
1530   BalancedDelimiterTracker T(*this, tok::l_paren);
1531   if (T.consumeOpen()) {
1532     Diag(Tok, diag::err_expected_lparen_after) << "asm";
1533     return ExprError();
1534   }
1535 
1536   ExprResult Result(ParseAsmStringLiteral(ForAsmLabel));
1537 
1538   if (!Result.isInvalid()) {
1539     // Close the paren and get the location of the end bracket
1540     T.consumeClose();
1541     if (EndLoc)
1542       *EndLoc = T.getCloseLocation();
1543   } else if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
1544     if (EndLoc)
1545       *EndLoc = Tok.getLocation();
1546     ConsumeParen();
1547   }
1548 
1549   return Result;
1550 }
1551 
1552 /// Get the TemplateIdAnnotation from the token and put it in the
1553 /// cleanup pool so that it gets destroyed when parsing the current top level
1554 /// declaration is finished.
1555 TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
1556   assert(tok.is(tok::annot_template_id) && "Expected template-id token");
1557   TemplateIdAnnotation *
1558       Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
1559   return Id;
1560 }
1561 
1562 void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
1563   // Push the current token back into the token stream (or revert it if it is
1564   // cached) and use an annotation scope token for current token.
1565   if (PP.isBacktrackEnabled())
1566     PP.RevertCachedTokens(1);
1567   else
1568     PP.EnterToken(Tok, /*IsReinject=*/true);
1569   Tok.setKind(tok::annot_cxxscope);
1570   Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
1571   Tok.setAnnotationRange(SS.getRange());
1572 
1573   // In case the tokens were cached, have Preprocessor replace them
1574   // with the annotation token.  We don't need to do this if we've
1575   // just reverted back to a prior state.
1576   if (IsNewAnnotation)
1577     PP.AnnotateCachedTokens(Tok);
1578 }
1579 
1580 /// Attempt to classify the name at the current token position. This may
1581 /// form a type, scope or primary expression annotation, or replace the token
1582 /// with a typo-corrected keyword. This is only appropriate when the current
1583 /// name must refer to an entity which has already been declared.
1584 ///
1585 /// \param CCC Indicates how to perform typo-correction for this name. If NULL,
1586 ///        no typo correction will be performed.
1587 Parser::AnnotatedNameKind
1588 Parser::TryAnnotateName(CorrectionCandidateCallback *CCC) {
1589   assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
1590 
1591   const bool EnteringContext = false;
1592   const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1593 
1594   CXXScopeSpec SS;
1595   if (getLangOpts().CPlusPlus &&
1596       ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1597                                      /*ObjectHadErrors=*/false,
1598                                      EnteringContext))
1599     return ANK_Error;
1600 
1601   if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
1602     if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation))
1603       return ANK_Error;
1604     return ANK_Unresolved;
1605   }
1606 
1607   IdentifierInfo *Name = Tok.getIdentifierInfo();
1608   SourceLocation NameLoc = Tok.getLocation();
1609 
1610   // FIXME: Move the tentative declaration logic into ClassifyName so we can
1611   // typo-correct to tentatively-declared identifiers.
1612   if (isTentativelyDeclared(Name)) {
1613     // Identifier has been tentatively declared, and thus cannot be resolved as
1614     // an expression. Fall back to annotating it as a type.
1615     if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation))
1616       return ANK_Error;
1617     return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
1618   }
1619 
1620   Token Next = NextToken();
1621 
1622   // Look up and classify the identifier. We don't perform any typo-correction
1623   // after a scope specifier, because in general we can't recover from typos
1624   // there (eg, after correcting 'A::template B<X>::C' [sic], we would need to
1625   // jump back into scope specifier parsing).
1626   Sema::NameClassification Classification = Actions.ClassifyName(
1627       getCurScope(), SS, Name, NameLoc, Next, SS.isEmpty() ? CCC : nullptr);
1628 
1629   // If name lookup found nothing and we guessed that this was a template name,
1630   // double-check before committing to that interpretation. C++20 requires that
1631   // we interpret this as a template-id if it can be, but if it can't be, then
1632   // this is an error recovery case.
1633   if (Classification.getKind() == Sema::NC_UndeclaredTemplate &&
1634       isTemplateArgumentList(1) == TPResult::False) {
1635     // It's not a template-id; re-classify without the '<' as a hint.
1636     Token FakeNext = Next;
1637     FakeNext.setKind(tok::unknown);
1638     Classification =
1639         Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, FakeNext,
1640                              SS.isEmpty() ? CCC : nullptr);
1641   }
1642 
1643   switch (Classification.getKind()) {
1644   case Sema::NC_Error:
1645     return ANK_Error;
1646 
1647   case Sema::NC_Keyword:
1648     // The identifier was typo-corrected to a keyword.
1649     Tok.setIdentifierInfo(Name);
1650     Tok.setKind(Name->getTokenID());
1651     PP.TypoCorrectToken(Tok);
1652     if (SS.isNotEmpty())
1653       AnnotateScopeToken(SS, !WasScopeAnnotation);
1654     // We've "annotated" this as a keyword.
1655     return ANK_Success;
1656 
1657   case Sema::NC_Unknown:
1658     // It's not something we know about. Leave it unannotated.
1659     break;
1660 
1661   case Sema::NC_Type: {
1662     SourceLocation BeginLoc = NameLoc;
1663     if (SS.isNotEmpty())
1664       BeginLoc = SS.getBeginLoc();
1665 
1666     /// An Objective-C object type followed by '<' is a specialization of
1667     /// a parameterized class type or a protocol-qualified type.
1668     ParsedType Ty = Classification.getType();
1669     if (getLangOpts().ObjC && NextToken().is(tok::less) &&
1670         (Ty.get()->isObjCObjectType() ||
1671          Ty.get()->isObjCObjectPointerType())) {
1672       // Consume the name.
1673       SourceLocation IdentifierLoc = ConsumeToken();
1674       SourceLocation NewEndLoc;
1675       TypeResult NewType
1676           = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
1677                                                    /*consumeLastToken=*/false,
1678                                                    NewEndLoc);
1679       if (NewType.isUsable())
1680         Ty = NewType.get();
1681       else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
1682         return ANK_Error;
1683     }
1684 
1685     Tok.setKind(tok::annot_typename);
1686     setTypeAnnotation(Tok, Ty);
1687     Tok.setAnnotationEndLoc(Tok.getLocation());
1688     Tok.setLocation(BeginLoc);
1689     PP.AnnotateCachedTokens(Tok);
1690     return ANK_Success;
1691   }
1692 
1693   case Sema::NC_ContextIndependentExpr:
1694     Tok.setKind(tok::annot_primary_expr);
1695     setExprAnnotation(Tok, Classification.getExpression());
1696     Tok.setAnnotationEndLoc(NameLoc);
1697     if (SS.isNotEmpty())
1698       Tok.setLocation(SS.getBeginLoc());
1699     PP.AnnotateCachedTokens(Tok);
1700     return ANK_Success;
1701 
1702   case Sema::NC_NonType:
1703     Tok.setKind(tok::annot_non_type);
1704     setNonTypeAnnotation(Tok, Classification.getNonTypeDecl());
1705     Tok.setLocation(NameLoc);
1706     Tok.setAnnotationEndLoc(NameLoc);
1707     PP.AnnotateCachedTokens(Tok);
1708     if (SS.isNotEmpty())
1709       AnnotateScopeToken(SS, !WasScopeAnnotation);
1710     return ANK_Success;
1711 
1712   case Sema::NC_UndeclaredNonType:
1713   case Sema::NC_DependentNonType:
1714     Tok.setKind(Classification.getKind() == Sema::NC_UndeclaredNonType
1715                     ? tok::annot_non_type_undeclared
1716                     : tok::annot_non_type_dependent);
1717     setIdentifierAnnotation(Tok, Name);
1718     Tok.setLocation(NameLoc);
1719     Tok.setAnnotationEndLoc(NameLoc);
1720     PP.AnnotateCachedTokens(Tok);
1721     if (SS.isNotEmpty())
1722       AnnotateScopeToken(SS, !WasScopeAnnotation);
1723     return ANK_Success;
1724 
1725   case Sema::NC_TypeTemplate:
1726     if (Next.isNot(tok::less)) {
1727       // This may be a type template being used as a template template argument.
1728       if (SS.isNotEmpty())
1729         AnnotateScopeToken(SS, !WasScopeAnnotation);
1730       return ANK_TemplateName;
1731     }
1732     LLVM_FALLTHROUGH;
1733   case Sema::NC_VarTemplate:
1734   case Sema::NC_FunctionTemplate:
1735   case Sema::NC_UndeclaredTemplate: {
1736     // We have a type, variable or function template followed by '<'.
1737     ConsumeToken();
1738     UnqualifiedId Id;
1739     Id.setIdentifier(Name, NameLoc);
1740     if (AnnotateTemplateIdToken(
1741             TemplateTy::make(Classification.getTemplateName()),
1742             Classification.getTemplateNameKind(), SS, SourceLocation(), Id))
1743       return ANK_Error;
1744     return ANK_Success;
1745   }
1746   case Sema::NC_Concept: {
1747     UnqualifiedId Id;
1748     Id.setIdentifier(Name, NameLoc);
1749     if (Next.is(tok::less))
1750       // We have a concept name followed by '<'. Consume the identifier token so
1751       // we reach the '<' and annotate it.
1752       ConsumeToken();
1753     if (AnnotateTemplateIdToken(
1754             TemplateTy::make(Classification.getTemplateName()),
1755             Classification.getTemplateNameKind(), SS, SourceLocation(), Id,
1756             /*AllowTypeAnnotation=*/false, /*TypeConstraint=*/true))
1757       return ANK_Error;
1758     return ANK_Success;
1759   }
1760   }
1761 
1762   // Unable to classify the name, but maybe we can annotate a scope specifier.
1763   if (SS.isNotEmpty())
1764     AnnotateScopeToken(SS, !WasScopeAnnotation);
1765   return ANK_Unresolved;
1766 }
1767 
1768 bool Parser::TryKeywordIdentFallback(bool DisableKeyword) {
1769   assert(Tok.isNot(tok::identifier));
1770   Diag(Tok, diag::ext_keyword_as_ident)
1771     << PP.getSpelling(Tok)
1772     << DisableKeyword;
1773   if (DisableKeyword)
1774     Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
1775   Tok.setKind(tok::identifier);
1776   return true;
1777 }
1778 
1779 /// TryAnnotateTypeOrScopeToken - If the current token position is on a
1780 /// typename (possibly qualified in C++) or a C++ scope specifier not followed
1781 /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1782 /// with a single annotation token representing the typename or C++ scope
1783 /// respectively.
1784 /// This simplifies handling of C++ scope specifiers and allows efficient
1785 /// backtracking without the need to re-parse and resolve nested-names and
1786 /// typenames.
1787 /// It will mainly be called when we expect to treat identifiers as typenames
1788 /// (if they are typenames). For example, in C we do not expect identifiers
1789 /// inside expressions to be treated as typenames so it will not be called
1790 /// for expressions in C.
1791 /// The benefit for C/ObjC is that a typename will be annotated and
1792 /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
1793 /// will not be called twice, once to check whether we have a declaration
1794 /// specifier, and another one to get the actual type inside
1795 /// ParseDeclarationSpecifiers).
1796 ///
1797 /// This returns true if an error occurred.
1798 ///
1799 /// Note that this routine emits an error if you call it with ::new or ::delete
1800 /// as the current tokens, so only call it in contexts where these are invalid.
1801 bool Parser::TryAnnotateTypeOrScopeToken() {
1802   assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1803           Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) ||
1804           Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id) ||
1805           Tok.is(tok::kw___super)) &&
1806          "Cannot be a type or scope token!");
1807 
1808   if (Tok.is(tok::kw_typename)) {
1809     // MSVC lets you do stuff like:
1810     //   typename typedef T_::D D;
1811     //
1812     // We will consume the typedef token here and put it back after we have
1813     // parsed the first identifier, transforming it into something more like:
1814     //   typename T_::D typedef D;
1815     if (getLangOpts().MSVCCompat && NextToken().is(tok::kw_typedef)) {
1816       Token TypedefToken;
1817       PP.Lex(TypedefToken);
1818       bool Result = TryAnnotateTypeOrScopeToken();
1819       PP.EnterToken(Tok, /*IsReinject=*/true);
1820       Tok = TypedefToken;
1821       if (!Result)
1822         Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
1823       return Result;
1824     }
1825 
1826     // Parse a C++ typename-specifier, e.g., "typename T::type".
1827     //
1828     //   typename-specifier:
1829     //     'typename' '::' [opt] nested-name-specifier identifier
1830     //     'typename' '::' [opt] nested-name-specifier template [opt]
1831     //            simple-template-id
1832     SourceLocation TypenameLoc = ConsumeToken();
1833     CXXScopeSpec SS;
1834     if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1835                                        /*ObjectHadErrors=*/false,
1836                                        /*EnteringContext=*/false, nullptr,
1837                                        /*IsTypename*/ true))
1838       return true;
1839     if (SS.isEmpty()) {
1840       if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
1841           Tok.is(tok::annot_decltype)) {
1842         // Attempt to recover by skipping the invalid 'typename'
1843         if (Tok.is(tok::annot_decltype) ||
1844             (!TryAnnotateTypeOrScopeToken() && Tok.isAnnotation())) {
1845           unsigned DiagID = diag::err_expected_qualified_after_typename;
1846           // MS compatibility: MSVC permits using known types with typename.
1847           // e.g. "typedef typename T* pointer_type"
1848           if (getLangOpts().MicrosoftExt)
1849             DiagID = diag::warn_expected_qualified_after_typename;
1850           Diag(Tok.getLocation(), DiagID);
1851           return false;
1852         }
1853       }
1854       if (Tok.isEditorPlaceholder())
1855         return true;
1856 
1857       Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
1858       return true;
1859     }
1860 
1861     TypeResult Ty;
1862     if (Tok.is(tok::identifier)) {
1863       // FIXME: check whether the next token is '<', first!
1864       Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1865                                      *Tok.getIdentifierInfo(),
1866                                      Tok.getLocation());
1867     } else if (Tok.is(tok::annot_template_id)) {
1868       TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1869       if (!TemplateId->mightBeType()) {
1870         Diag(Tok, diag::err_typename_refers_to_non_type_template)
1871           << Tok.getAnnotationRange();
1872         return true;
1873       }
1874 
1875       ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1876                                          TemplateId->NumArgs);
1877 
1878       Ty = TemplateId->isInvalid()
1879                ? TypeError()
1880                : Actions.ActOnTypenameType(
1881                      getCurScope(), TypenameLoc, SS, TemplateId->TemplateKWLoc,
1882                      TemplateId->Template, TemplateId->Name,
1883                      TemplateId->TemplateNameLoc, TemplateId->LAngleLoc,
1884                      TemplateArgsPtr, TemplateId->RAngleLoc);
1885     } else {
1886       Diag(Tok, diag::err_expected_type_name_after_typename)
1887         << SS.getRange();
1888       return true;
1889     }
1890 
1891     SourceLocation EndLoc = Tok.getLastLoc();
1892     Tok.setKind(tok::annot_typename);
1893     setTypeAnnotation(Tok, Ty);
1894     Tok.setAnnotationEndLoc(EndLoc);
1895     Tok.setLocation(TypenameLoc);
1896     PP.AnnotateCachedTokens(Tok);
1897     return false;
1898   }
1899 
1900   // Remembers whether the token was originally a scope annotation.
1901   bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1902 
1903   CXXScopeSpec SS;
1904   if (getLangOpts().CPlusPlus)
1905     if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1906                                        /*ObjectHadErrors=*/false,
1907                                        /*EnteringContext*/ false))
1908       return true;
1909 
1910   return TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation);
1911 }
1912 
1913 /// Try to annotate a type or scope token, having already parsed an
1914 /// optional scope specifier. \p IsNewScope should be \c true unless the scope
1915 /// specifier was extracted from an existing tok::annot_cxxscope annotation.
1916 bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(CXXScopeSpec &SS,
1917                                                        bool IsNewScope) {
1918   if (Tok.is(tok::identifier)) {
1919     // Determine whether the identifier is a type name.
1920     if (ParsedType Ty = Actions.getTypeName(
1921             *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), &SS,
1922             false, NextToken().is(tok::period), nullptr,
1923             /*IsCtorOrDtorName=*/false,
1924             /*NonTrivialTypeSourceInfo*/true,
1925             /*IsClassTemplateDeductionContext*/true)) {
1926       SourceLocation BeginLoc = Tok.getLocation();
1927       if (SS.isNotEmpty()) // it was a C++ qualified type name.
1928         BeginLoc = SS.getBeginLoc();
1929 
1930       /// An Objective-C object type followed by '<' is a specialization of
1931       /// a parameterized class type or a protocol-qualified type.
1932       if (getLangOpts().ObjC && NextToken().is(tok::less) &&
1933           (Ty.get()->isObjCObjectType() ||
1934            Ty.get()->isObjCObjectPointerType())) {
1935         // Consume the name.
1936         SourceLocation IdentifierLoc = ConsumeToken();
1937         SourceLocation NewEndLoc;
1938         TypeResult NewType
1939           = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
1940                                                    /*consumeLastToken=*/false,
1941                                                    NewEndLoc);
1942         if (NewType.isUsable())
1943           Ty = NewType.get();
1944         else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
1945           return false;
1946       }
1947 
1948       // This is a typename. Replace the current token in-place with an
1949       // annotation type token.
1950       Tok.setKind(tok::annot_typename);
1951       setTypeAnnotation(Tok, Ty);
1952       Tok.setAnnotationEndLoc(Tok.getLocation());
1953       Tok.setLocation(BeginLoc);
1954 
1955       // In case the tokens were cached, have Preprocessor replace
1956       // them with the annotation token.
1957       PP.AnnotateCachedTokens(Tok);
1958       return false;
1959     }
1960 
1961     if (!getLangOpts().CPlusPlus) {
1962       // If we're in C, we can't have :: tokens at all (the lexer won't return
1963       // them).  If the identifier is not a type, then it can't be scope either,
1964       // just early exit.
1965       return false;
1966     }
1967 
1968     // If this is a template-id, annotate with a template-id or type token.
1969     // FIXME: This appears to be dead code. We already have formed template-id
1970     // tokens when parsing the scope specifier; this can never form a new one.
1971     if (NextToken().is(tok::less)) {
1972       TemplateTy Template;
1973       UnqualifiedId TemplateName;
1974       TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1975       bool MemberOfUnknownSpecialization;
1976       if (TemplateNameKind TNK = Actions.isTemplateName(
1977               getCurScope(), SS,
1978               /*hasTemplateKeyword=*/false, TemplateName,
1979               /*ObjectType=*/nullptr, /*EnteringContext*/false, Template,
1980               MemberOfUnknownSpecialization)) {
1981         // Only annotate an undeclared template name as a template-id if the
1982         // following tokens have the form of a template argument list.
1983         if (TNK != TNK_Undeclared_template ||
1984             isTemplateArgumentList(1) != TPResult::False) {
1985           // Consume the identifier.
1986           ConsumeToken();
1987           if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1988                                       TemplateName)) {
1989             // If an unrecoverable error occurred, we need to return true here,
1990             // because the token stream is in a damaged state.  We may not
1991             // return a valid identifier.
1992             return true;
1993           }
1994         }
1995       }
1996     }
1997 
1998     // The current token, which is either an identifier or a
1999     // template-id, is not part of the annotation. Fall through to
2000     // push that token back into the stream and complete the C++ scope
2001     // specifier annotation.
2002   }
2003 
2004   if (Tok.is(tok::annot_template_id)) {
2005     TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
2006     if (TemplateId->Kind == TNK_Type_template) {
2007       // A template-id that refers to a type was parsed into a
2008       // template-id annotation in a context where we weren't allowed
2009       // to produce a type annotation token. Update the template-id
2010       // annotation token to a type annotation token now.
2011       AnnotateTemplateIdTokenAsType(SS);
2012       return false;
2013     }
2014   }
2015 
2016   if (SS.isEmpty())
2017     return false;
2018 
2019   // A C++ scope specifier that isn't followed by a typename.
2020   AnnotateScopeToken(SS, IsNewScope);
2021   return false;
2022 }
2023 
2024 /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
2025 /// annotates C++ scope specifiers and template-ids.  This returns
2026 /// true if there was an error that could not be recovered from.
2027 ///
2028 /// Note that this routine emits an error if you call it with ::new or ::delete
2029 /// as the current tokens, so only call it in contexts where these are invalid.
2030 bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
2031   assert(getLangOpts().CPlusPlus &&
2032          "Call sites of this function should be guarded by checking for C++");
2033   assert(MightBeCXXScopeToken() && "Cannot be a type or scope token!");
2034 
2035   CXXScopeSpec SS;
2036   if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
2037                                      /*ObjectHadErrors=*/false,
2038                                      EnteringContext))
2039     return true;
2040   if (SS.isEmpty())
2041     return false;
2042 
2043   AnnotateScopeToken(SS, true);
2044   return false;
2045 }
2046 
2047 bool Parser::isTokenEqualOrEqualTypo() {
2048   tok::TokenKind Kind = Tok.getKind();
2049   switch (Kind) {
2050   default:
2051     return false;
2052   case tok::ampequal:            // &=
2053   case tok::starequal:           // *=
2054   case tok::plusequal:           // +=
2055   case tok::minusequal:          // -=
2056   case tok::exclaimequal:        // !=
2057   case tok::slashequal:          // /=
2058   case tok::percentequal:        // %=
2059   case tok::lessequal:           // <=
2060   case tok::lesslessequal:       // <<=
2061   case tok::greaterequal:        // >=
2062   case tok::greatergreaterequal: // >>=
2063   case tok::caretequal:          // ^=
2064   case tok::pipeequal:           // |=
2065   case tok::equalequal:          // ==
2066     Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
2067         << Kind
2068         << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
2069     LLVM_FALLTHROUGH;
2070   case tok::equal:
2071     return true;
2072   }
2073 }
2074 
2075 SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
2076   assert(Tok.is(tok::code_completion));
2077   PrevTokLocation = Tok.getLocation();
2078 
2079   for (Scope *S = getCurScope(); S; S = S->getParent()) {
2080     if (S->getFlags() & Scope::FnScope) {
2081       Actions.CodeCompleteOrdinaryName(getCurScope(),
2082                                        Sema::PCC_RecoveryInFunction);
2083       cutOffParsing();
2084       return PrevTokLocation;
2085     }
2086 
2087     if (S->getFlags() & Scope::ClassScope) {
2088       Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
2089       cutOffParsing();
2090       return PrevTokLocation;
2091     }
2092   }
2093 
2094   Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
2095   cutOffParsing();
2096   return PrevTokLocation;
2097 }
2098 
2099 // Code-completion pass-through functions
2100 
2101 void Parser::CodeCompleteDirective(bool InConditional) {
2102   Actions.CodeCompletePreprocessorDirective(InConditional);
2103 }
2104 
2105 void Parser::CodeCompleteInConditionalExclusion() {
2106   Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
2107 }
2108 
2109 void Parser::CodeCompleteMacroName(bool IsDefinition) {
2110   Actions.CodeCompletePreprocessorMacroName(IsDefinition);
2111 }
2112 
2113 void Parser::CodeCompletePreprocessorExpression() {
2114   Actions.CodeCompletePreprocessorExpression();
2115 }
2116 
2117 void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
2118                                        MacroInfo *MacroInfo,
2119                                        unsigned ArgumentIndex) {
2120   Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
2121                                                 ArgumentIndex);
2122 }
2123 
2124 void Parser::CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled) {
2125   Actions.CodeCompleteIncludedFile(Dir, IsAngled);
2126 }
2127 
2128 void Parser::CodeCompleteNaturalLanguage() {
2129   Actions.CodeCompleteNaturalLanguage();
2130 }
2131 
2132 bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
2133   assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
2134          "Expected '__if_exists' or '__if_not_exists'");
2135   Result.IsIfExists = Tok.is(tok::kw___if_exists);
2136   Result.KeywordLoc = ConsumeToken();
2137 
2138   BalancedDelimiterTracker T(*this, tok::l_paren);
2139   if (T.consumeOpen()) {
2140     Diag(Tok, diag::err_expected_lparen_after)
2141       << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
2142     return true;
2143   }
2144 
2145   // Parse nested-name-specifier.
2146   if (getLangOpts().CPlusPlus)
2147     ParseOptionalCXXScopeSpecifier(Result.SS, /*ObjectType=*/nullptr,
2148                                    /*ObjectHadErrors=*/false,
2149                                    /*EnteringContext=*/false);
2150 
2151   // Check nested-name specifier.
2152   if (Result.SS.isInvalid()) {
2153     T.skipToEnd();
2154     return true;
2155   }
2156 
2157   // Parse the unqualified-id.
2158   SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
2159   if (ParseUnqualifiedId(Result.SS, /*ObjectType=*/nullptr,
2160                          /*ObjectHadErrors=*/false, /*EnteringContext*/ false,
2161                          /*AllowDestructorName*/ true,
2162                          /*AllowConstructorName*/ true,
2163                          /*AllowDeductionGuide*/ false, &TemplateKWLoc,
2164                          Result.Name)) {
2165     T.skipToEnd();
2166     return true;
2167   }
2168 
2169   if (T.consumeClose())
2170     return true;
2171 
2172   // Check if the symbol exists.
2173   switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
2174                                                Result.IsIfExists, Result.SS,
2175                                                Result.Name)) {
2176   case Sema::IER_Exists:
2177     Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
2178     break;
2179 
2180   case Sema::IER_DoesNotExist:
2181     Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
2182     break;
2183 
2184   case Sema::IER_Dependent:
2185     Result.Behavior = IEB_Dependent;
2186     break;
2187 
2188   case Sema::IER_Error:
2189     return true;
2190   }
2191 
2192   return false;
2193 }
2194 
2195 void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
2196   IfExistsCondition Result;
2197   if (ParseMicrosoftIfExistsCondition(Result))
2198     return;
2199 
2200   BalancedDelimiterTracker Braces(*this, tok::l_brace);
2201   if (Braces.consumeOpen()) {
2202     Diag(Tok, diag::err_expected) << tok::l_brace;
2203     return;
2204   }
2205 
2206   switch (Result.Behavior) {
2207   case IEB_Parse:
2208     // Parse declarations below.
2209     break;
2210 
2211   case IEB_Dependent:
2212     llvm_unreachable("Cannot have a dependent external declaration");
2213 
2214   case IEB_Skip:
2215     Braces.skipToEnd();
2216     return;
2217   }
2218 
2219   // Parse the declarations.
2220   // FIXME: Support module import within __if_exists?
2221   while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
2222     ParsedAttributesWithRange attrs(AttrFactory);
2223     MaybeParseCXX11Attributes(attrs);
2224     DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
2225     if (Result && !getCurScope()->getParent())
2226       Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
2227   }
2228   Braces.consumeClose();
2229 }
2230 
2231 /// Parse a declaration beginning with the 'module' keyword or C++20
2232 /// context-sensitive keyword (optionally preceded by 'export').
2233 ///
2234 ///   module-declaration:   [Modules TS + P0629R0]
2235 ///     'export'[opt] 'module' module-name attribute-specifier-seq[opt] ';'
2236 ///
2237 ///   global-module-fragment:  [C++2a]
2238 ///     'module' ';' top-level-declaration-seq[opt]
2239 ///   module-declaration:      [C++2a]
2240 ///     'export'[opt] 'module' module-name module-partition[opt]
2241 ///            attribute-specifier-seq[opt] ';'
2242 ///   private-module-fragment: [C++2a]
2243 ///     'module' ':' 'private' ';' top-level-declaration-seq[opt]
2244 Parser::DeclGroupPtrTy Parser::ParseModuleDecl(bool IsFirstDecl) {
2245   SourceLocation StartLoc = Tok.getLocation();
2246 
2247   Sema::ModuleDeclKind MDK = TryConsumeToken(tok::kw_export)
2248                                  ? Sema::ModuleDeclKind::Interface
2249                                  : Sema::ModuleDeclKind::Implementation;
2250 
2251   assert(
2252       (Tok.is(tok::kw_module) ||
2253        (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_module)) &&
2254       "not a module declaration");
2255   SourceLocation ModuleLoc = ConsumeToken();
2256 
2257   // Attributes appear after the module name, not before.
2258   // FIXME: Suggest moving the attributes later with a fixit.
2259   DiagnoseAndSkipCXX11Attributes();
2260 
2261   // Parse a global-module-fragment, if present.
2262   if (getLangOpts().CPlusPlusModules && Tok.is(tok::semi)) {
2263     SourceLocation SemiLoc = ConsumeToken();
2264     if (!IsFirstDecl) {
2265       Diag(StartLoc, diag::err_global_module_introducer_not_at_start)
2266         << SourceRange(StartLoc, SemiLoc);
2267       return nullptr;
2268     }
2269     if (MDK == Sema::ModuleDeclKind::Interface) {
2270       Diag(StartLoc, diag::err_module_fragment_exported)
2271         << /*global*/0 << FixItHint::CreateRemoval(StartLoc);
2272     }
2273     return Actions.ActOnGlobalModuleFragmentDecl(ModuleLoc);
2274   }
2275 
2276   // Parse a private-module-fragment, if present.
2277   if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon) &&
2278       NextToken().is(tok::kw_private)) {
2279     if (MDK == Sema::ModuleDeclKind::Interface) {
2280       Diag(StartLoc, diag::err_module_fragment_exported)
2281         << /*private*/1 << FixItHint::CreateRemoval(StartLoc);
2282     }
2283     ConsumeToken();
2284     SourceLocation PrivateLoc = ConsumeToken();
2285     DiagnoseAndSkipCXX11Attributes();
2286     ExpectAndConsumeSemi(diag::err_private_module_fragment_expected_semi);
2287     return Actions.ActOnPrivateModuleFragmentDecl(ModuleLoc, PrivateLoc);
2288   }
2289 
2290   SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
2291   if (ParseModuleName(ModuleLoc, Path, /*IsImport*/false))
2292     return nullptr;
2293 
2294   // Parse the optional module-partition.
2295   if (Tok.is(tok::colon)) {
2296     SourceLocation ColonLoc = ConsumeToken();
2297     SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Partition;
2298     if (ParseModuleName(ModuleLoc, Partition, /*IsImport*/false))
2299       return nullptr;
2300 
2301     // FIXME: Support module partition declarations.
2302     Diag(ColonLoc, diag::err_unsupported_module_partition)
2303       << SourceRange(ColonLoc, Partition.back().second);
2304     // Recover by parsing as a non-partition.
2305   }
2306 
2307   // We don't support any module attributes yet; just parse them and diagnose.
2308   ParsedAttributesWithRange Attrs(AttrFactory);
2309   MaybeParseCXX11Attributes(Attrs);
2310   ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr);
2311 
2312   ExpectAndConsumeSemi(diag::err_module_expected_semi);
2313 
2314   return Actions.ActOnModuleDecl(StartLoc, ModuleLoc, MDK, Path, IsFirstDecl);
2315 }
2316 
2317 /// Parse a module import declaration. This is essentially the same for
2318 /// Objective-C and the C++ Modules TS, except for the leading '@' (in ObjC)
2319 /// and the trailing optional attributes (in C++).
2320 ///
2321 /// [ObjC]  @import declaration:
2322 ///           '@' 'import' module-name ';'
2323 /// [ModTS] module-import-declaration:
2324 ///           'import' module-name attribute-specifier-seq[opt] ';'
2325 /// [C++2a] module-import-declaration:
2326 ///           'export'[opt] 'import' module-name
2327 ///                   attribute-specifier-seq[opt] ';'
2328 ///           'export'[opt] 'import' module-partition
2329 ///                   attribute-specifier-seq[opt] ';'
2330 ///           'export'[opt] 'import' header-name
2331 ///                   attribute-specifier-seq[opt] ';'
2332 Decl *Parser::ParseModuleImport(SourceLocation AtLoc) {
2333   SourceLocation StartLoc = AtLoc.isInvalid() ? Tok.getLocation() : AtLoc;
2334 
2335   SourceLocation ExportLoc;
2336   TryConsumeToken(tok::kw_export, ExportLoc);
2337 
2338   assert((AtLoc.isInvalid() ? Tok.isOneOf(tok::kw_import, tok::identifier)
2339                             : Tok.isObjCAtKeyword(tok::objc_import)) &&
2340          "Improper start to module import");
2341   bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import);
2342   SourceLocation ImportLoc = ConsumeToken();
2343 
2344   SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
2345   Module *HeaderUnit = nullptr;
2346 
2347   if (Tok.is(tok::header_name)) {
2348     // This is a header import that the preprocessor decided we should skip
2349     // because it was malformed in some way. Parse and ignore it; it's already
2350     // been diagnosed.
2351     ConsumeToken();
2352   } else if (Tok.is(tok::annot_header_unit)) {
2353     // This is a header import that the preprocessor mapped to a module import.
2354     HeaderUnit = reinterpret_cast<Module *>(Tok.getAnnotationValue());
2355     ConsumeAnnotationToken();
2356   } else if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon)) {
2357     SourceLocation ColonLoc = ConsumeToken();
2358     if (ParseModuleName(ImportLoc, Path, /*IsImport*/true))
2359       return nullptr;
2360 
2361     // FIXME: Support module partition import.
2362     Diag(ColonLoc, diag::err_unsupported_module_partition)
2363       << SourceRange(ColonLoc, Path.back().second);
2364     return nullptr;
2365   } else {
2366     if (ParseModuleName(ImportLoc, Path, /*IsImport*/true))
2367       return nullptr;
2368   }
2369 
2370   ParsedAttributesWithRange Attrs(AttrFactory);
2371   MaybeParseCXX11Attributes(Attrs);
2372   // We don't support any module import attributes yet.
2373   ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr);
2374 
2375   if (PP.hadModuleLoaderFatalFailure()) {
2376     // With a fatal failure in the module loader, we abort parsing.
2377     cutOffParsing();
2378     return nullptr;
2379   }
2380 
2381   DeclResult Import;
2382   if (HeaderUnit)
2383     Import =
2384         Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit);
2385   else if (!Path.empty())
2386     Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path);
2387   ExpectAndConsumeSemi(diag::err_module_expected_semi);
2388   if (Import.isInvalid())
2389     return nullptr;
2390 
2391   // Using '@import' in framework headers requires modules to be enabled so that
2392   // the header is parseable. Emit a warning to make the user aware.
2393   if (IsObjCAtImport && AtLoc.isValid()) {
2394     auto &SrcMgr = PP.getSourceManager();
2395     auto *FE = SrcMgr.getFileEntryForID(SrcMgr.getFileID(AtLoc));
2396     if (FE && llvm::sys::path::parent_path(FE->getDir()->getName())
2397                   .endswith(".framework"))
2398       Diags.Report(AtLoc, diag::warn_atimport_in_framework_header);
2399   }
2400 
2401   return Import.get();
2402 }
2403 
2404 /// Parse a C++ Modules TS / Objective-C module name (both forms use the same
2405 /// grammar).
2406 ///
2407 ///         module-name:
2408 ///           module-name-qualifier[opt] identifier
2409 ///         module-name-qualifier:
2410 ///           module-name-qualifier[opt] identifier '.'
2411 bool Parser::ParseModuleName(
2412     SourceLocation UseLoc,
2413     SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> &Path,
2414     bool IsImport) {
2415   // Parse the module path.
2416   while (true) {
2417     if (!Tok.is(tok::identifier)) {
2418       if (Tok.is(tok::code_completion)) {
2419         Actions.CodeCompleteModuleImport(UseLoc, Path);
2420         cutOffParsing();
2421         return true;
2422       }
2423 
2424       Diag(Tok, diag::err_module_expected_ident) << IsImport;
2425       SkipUntil(tok::semi);
2426       return true;
2427     }
2428 
2429     // Record this part of the module path.
2430     Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
2431     ConsumeToken();
2432 
2433     if (Tok.isNot(tok::period))
2434       return false;
2435 
2436     ConsumeToken();
2437   }
2438 }
2439 
2440 /// Try recover parser when module annotation appears where it must not
2441 /// be found.
2442 /// \returns false if the recover was successful and parsing may be continued, or
2443 /// true if parser must bail out to top level and handle the token there.
2444 bool Parser::parseMisplacedModuleImport() {
2445   while (true) {
2446     switch (Tok.getKind()) {
2447     case tok::annot_module_end:
2448       // If we recovered from a misplaced module begin, we expect to hit a
2449       // misplaced module end too. Stay in the current context when this
2450       // happens.
2451       if (MisplacedModuleBeginCount) {
2452         --MisplacedModuleBeginCount;
2453         Actions.ActOnModuleEnd(Tok.getLocation(),
2454                                reinterpret_cast<Module *>(
2455                                    Tok.getAnnotationValue()));
2456         ConsumeAnnotationToken();
2457         continue;
2458       }
2459       // Inform caller that recovery failed, the error must be handled at upper
2460       // level. This will generate the desired "missing '}' at end of module"
2461       // diagnostics on the way out.
2462       return true;
2463     case tok::annot_module_begin:
2464       // Recover by entering the module (Sema will diagnose).
2465       Actions.ActOnModuleBegin(Tok.getLocation(),
2466                                reinterpret_cast<Module *>(
2467                                    Tok.getAnnotationValue()));
2468       ConsumeAnnotationToken();
2469       ++MisplacedModuleBeginCount;
2470       continue;
2471     case tok::annot_module_include:
2472       // Module import found where it should not be, for instance, inside a
2473       // namespace. Recover by importing the module.
2474       Actions.ActOnModuleInclude(Tok.getLocation(),
2475                                  reinterpret_cast<Module *>(
2476                                      Tok.getAnnotationValue()));
2477       ConsumeAnnotationToken();
2478       // If there is another module import, process it.
2479       continue;
2480     default:
2481       return false;
2482     }
2483   }
2484   return false;
2485 }
2486 
2487 bool BalancedDelimiterTracker::diagnoseOverflow() {
2488   P.Diag(P.Tok, diag::err_bracket_depth_exceeded)
2489     << P.getLangOpts().BracketDepth;
2490   P.Diag(P.Tok, diag::note_bracket_depth);
2491   P.cutOffParsing();
2492   return true;
2493 }
2494 
2495 bool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
2496                                                 const char *Msg,
2497                                                 tok::TokenKind SkipToTok) {
2498   LOpen = P.Tok.getLocation();
2499   if (P.ExpectAndConsume(Kind, DiagID, Msg)) {
2500     if (SkipToTok != tok::unknown)
2501       P.SkipUntil(SkipToTok, Parser::StopAtSemi);
2502     return true;
2503   }
2504 
2505   if (getDepth() < P.getLangOpts().BracketDepth)
2506     return false;
2507 
2508   return diagnoseOverflow();
2509 }
2510 
2511 bool BalancedDelimiterTracker::diagnoseMissingClose() {
2512   assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
2513 
2514   if (P.Tok.is(tok::annot_module_end))
2515     P.Diag(P.Tok, diag::err_missing_before_module_end) << Close;
2516   else
2517     P.Diag(P.Tok, diag::err_expected) << Close;
2518   P.Diag(LOpen, diag::note_matching) << Kind;
2519 
2520   // If we're not already at some kind of closing bracket, skip to our closing
2521   // token.
2522   if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) &&
2523       P.Tok.isNot(tok::r_square) &&
2524       P.SkipUntil(Close, FinalToken,
2525                   Parser::StopAtSemi | Parser::StopBeforeMatch) &&
2526       P.Tok.is(Close))
2527     LClose = P.ConsumeAnyToken();
2528   return true;
2529 }
2530 
2531 void BalancedDelimiterTracker::skipToEnd() {
2532   P.SkipUntil(Close, Parser::StopBeforeMatch);
2533   consumeClose();
2534 }
2535