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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang/lib/Parse/Parser.cpp