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