clang 22.0.0git
Parser.cpp
Go to the documentation of this file.
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"
16#include "clang/AST/ASTLambda.h"
21#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/Scope.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("__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(new ActionCommentHandler(actions));
75 PP.addCommentHandler(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
86 return Diags.Report(Loc, DiagID);
87}
88
89DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
90 return Diag(Tok.getLocation(), DiagID);
91}
92
94 unsigned CompatDiagId) {
95 return Diag(Loc,
97}
98
99DiagnosticBuilder Parser::DiagCompat(const Token &Tok, unsigned CompatDiagId) {
100 return DiagCompat(Tok.getLocation(), CompatDiagId);
101}
102
103void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
104 SourceRange ParenRange) {
105 SourceLocation EndLoc = PP.getLocForEndOfToken(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, DK);
110 return;
111 }
112
113 Diag(Loc, DK)
114 << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
115 << FixItHint::CreateInsertion(EndLoc, ")");
116}
117
118static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
119 switch (ExpectedTok) {
120 case tok::semi:
121 return Tok.is(tok::colon) || Tok.is(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(ExpectedTok) || Tok.is(tok::code_completion)) {
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);
139 SourceRange(Loc), tok::getPunctuatorSpelling(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.
150 return false;
151 }
152
153 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
154 const char *Spelling = nullptr;
155 if (EndLoc.isValid())
156 Spelling = tok::getPunctuatorSpelling(ExpectedTok);
157
158 DiagnosticBuilder DB =
159 Spelling
160 ? Diag(EndLoc, DiagID) << FixItHint::CreateInsertion(EndLoc, 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(tok::semi))
174 return false;
175
176 if (Tok.is(tok::code_completion)) {
177 handleUnexpectedCodeCompletionToken();
178 return false;
179 }
180
181 if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
182 NextToken().is(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(tok::semi, DiagID , TokenUsed);
192}
193
194void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, DeclSpec::TST TST) {
195 if (!Tok.is(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(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.
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
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(tok::identifier))
234 return false;
235 if (const auto *II = Tok.getIdentifierInfo()) {
236 if (II->isCPlusPlusKeyword(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(FirstTokLoc) !=
257 PP.getSourceManager().getFileID(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(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
284 return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0;
285}
286
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(Toks[i])) {
295 if (HasFlagsSet(Flags, StopBeforeMatch)) {
296 // Noop, don't consume the token.
297 } else {
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(Flags, StopAtSemi) &&
310 while (Tok.isNot(tok::eof))
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:
346 handleUnexpectedCodeCompletionToken();
347 return false;
348
349 case tok::l_paren:
350 // Recursively skip properly-nested parens.
351 ConsumeParen();
353 SkipUntil(tok::r_paren, StopAtCodeCompletion);
354 else
355 SkipUntil(tok::r_paren);
356 break;
357 case tok::l_square:
358 // Recursively skip properly-nested square brackets.
359 ConsumeBracket();
361 SkipUntil(tok::r_square, StopAtCodeCompletion);
362 else
363 SkipUntil(tok::r_square);
364 break;
365 case tok::l_brace:
366 // Recursively skip properly-nested braces.
367 ConsumeBrace();
369 SkipUntil(tok::r_brace, StopAtCodeCompletion);
370 else
371 SkipUntil(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(tok::colon,
378 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(Flags, StopAtSemi))
405 return false;
406 [[fallthrough]];
407 default:
408 // Skip this token.
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(getCurScope(), ScopeFlags);
424 Actions.CurScope = N;
425 } else {
426 Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
427 }
428}
429
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(Tok.getLocation(), 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
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(CommentSemaHandler.get());
477
478 PP.clearCodeCompletionHandler();
479
480 DestroyTemplateIds();
481}
482
484 // Create the translation unit scope. Install it as the current scope.
485 assert(getCurScope() == nullptr && "A scope is already active?");
487 Actions.ActOnTranslationUnitScope(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(ObjCTypeQual::in)] =
493 &PP.getIdentifierTable().get("in");
494 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::out)] =
495 &PP.getIdentifierTable().get("out");
496 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::inout)] =
497 &PP.getIdentifierTable().get("inout");
498 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::oneway)] =
499 &PP.getIdentifierTable().get("oneway");
500 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::bycopy)] =
501 &PP.getIdentifierTable().get("bycopy");
502 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::byref)] =
503 &PP.getIdentifierTable().get("byref");
504 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::nonnull)] =
505 &PP.getIdentifierTable().get("nonnull");
506 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::nullable)] =
507 &PP.getIdentifierTable().get("nullable");
508 ObjCTypeQuals[llvm::to_underlying(ObjCTypeQual::null_unspecified)] =
509 &PP.getIdentifierTable().get("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("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("vector");
531 Ident_bool = &PP.getIdentifierTable().get("bool");
532 Ident_Bool = &PP.getIdentifierTable().get("_Bool");
533 }
534 if (getLangOpts().AltiVec)
535 Ident_pixel = &PP.getIdentifierTable().get("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("_exception_info");
557 Ident___exception_info = PP.getIdentifierInfo("__exception_info");
558 Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation");
559 Ident__exception_code = PP.getIdentifierInfo("_exception_code");
560 Ident___exception_code = PP.getIdentifierInfo("__exception_code");
561 Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode");
562 Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination");
563 Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
564 Ident_AbnormalTermination = PP.getIdentifierInfo("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("import");
579 Ident_module = PP.getIdentifierInfo("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
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.
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
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: {
638 if ((II == Ident_module || II == Ident_import) &&
639 GetLookAheadToken(2).isNot(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(SourceLocation(), ImportState);
661 Result = Actions.ConvertDeclToDeclGroup(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(Loc, Mod);
672 else {
673 DeclResult Import =
674 Actions.ActOnModuleImport(Loc, SourceLocation(), Loc, Mod);
675 Decl *ImportDecl = Import.isInvalid() ? nullptr : Import.get();
676 Result = Actions.ConvertDeclToDeclGroup(ImportDecl);
677 }
678 ConsumeAnnotationToken();
679 return false;
680 }
681
682 case tok::annot_module_begin:
683 Actions.ActOnAnnotModuleBegin(
684 Tok.getLocation(),
685 reinterpret_cast<Module *>(Tok.getAnnotationValue()));
686 ConsumeAnnotationToken();
688 return false;
689
690 case tok::annot_module_end:
691 Actions.ActOnAnnotModuleEnd(
692 Tok.getLocation(),
693 reinterpret_cast<Module *>(Tok.getAnnotationValue()));
694 ConsumeAnnotationToken();
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(LateTemplateParserCallback, 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(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(DeclAttrs) ||
742 MaybeParseGNUAttributes(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.
752 else if (ImportState == Sema::ModuleImportState::ImportAllowed)
753 // Non-imports disallow further imports.
755 else if (ImportState ==
757 // Non-imports disallow further imports.
759 }
760 return false;
761}
762
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: {
823 return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
824 }
825 case tok::annot_pragma_openacc: {
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(getCurScope(), Attrs, Tok.getLocation());
849 ConsumeExtraSemi(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));
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>(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(Result.get(), StartLoc, EndLoc);
887 break;
888 }
889 case tok::at:
890 return ParseObjCAtDirectives(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 getCurScope(),
906 /*IsInstanceMethod=*/std::nullopt,
907 /*ReturnType=*/nullptr);
908 }
909
911 if (CurParsedObjCImpl) {
913 } else if (PP.isIncrementalProcessingEnabled()) {
915 } else {
917 };
918 Actions.CodeCompletion().CodeCompleteOrdinaryName(getCurScope(), PCC);
919 return nullptr;
920 case tok::kw_import: {
922 if (getLangOpts().CPlusPlusModules) {
923 llvm_unreachable("not expecting a c++20 import here");
924 ProhibitAttributes(Attrs);
925 }
926 SingleDecl = ParseModuleImport(SourceLocation(), 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(DeclaratorContext::File, DeclEnd, 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(DeclaratorContext::File, DeclEnd, 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(tok::kw_template)) {
963 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
964 << 0;
965 SourceLocation DeclEnd;
966 return ParseDeclaration(DeclaratorContext::File, DeclEnd, 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(DeclaratorContext::File, DeclEnd, 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(DeclaratorContext::File, DeclEnd, Attrs,
989 DeclSpecAttrs);
990 }
991 }
992 goto dont_know;
993
994 case tok::kw_extern:
995 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
996 ProhibitAttributes(Attrs);
997 ProhibitAttributes(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(DeclaratorContext::File, ExternLoc,
1006 TemplateLoc, DeclEnd, 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(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(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(SingleDecl);
1038}
1039
1040bool Parser::isDeclarationAfterDeclarator() {
1041 // Check for '= delete' or '= default'
1042 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
1043 const Token &KW = NextToken();
1044 if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
1045 return false;
1046 }
1047
1048 return Tok.is(tok::equal) || // int X()= -> not a function def
1049 Tok.is(tok::comma) || // int X(), -> not a function def
1050 Tok.is(tok::semi) || // int X(); -> not a function def
1051 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
1052 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
1053 (getLangOpts().CPlusPlus &&
1054 Tok.is(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(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(ImplicitTypenameContext::No);
1066
1067 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
1068 const Token &KW = NextToken();
1069 return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
1070 }
1071
1072 return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
1073 Tok.is(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.takeAttributesAppendingingFrom(DeclSpecAttrs);
1087
1088 ParsedTemplateInfo TemplateInfo;
1089 MaybeParseMicrosoftAttributes(DS.getAttributes());
1090 // Parse the common declaration-specifiers piece.
1091 ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
1092 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, 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(tok::semi)) {
1103 auto LengthOfTSTToken = [](DeclSpec::TST TKind) {
1104 assert(DeclSpec::isDeclRep(TKind));
1105 switch(TKind) {
1107 return 5;
1109 return 6;
1111 return 5;
1112 case DeclSpec::TST_enum:
1113 return 4;
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 =
1125 LengthOfTSTToken(DS.getTypeSpecType()))
1126 : SourceLocation();
1127 ProhibitAttributes(Attrs, CorrectLocationForAttributes);
1128 ConsumeToken();
1129 RecordDecl *AnonRecord = nullptr;
1130 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
1131 getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord);
1132 DS.complete(TheDecl);
1133 Actions.ActOnDefinedDeclarationSpecifier(TheDecl);
1134 if (AnonRecord) {
1135 Decl* decls[] = {AnonRecord, TheDecl};
1136 return Actions.BuildDeclaratorGroup(decls);
1137 }
1138 return Actions.ConvertDeclToDeclGroup(TheDecl);
1139 }
1140
1141 if (DS.hasTagDefinition())
1142 Actions.ActOnDefinedDeclarationSpecifier(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(tok::at)) {
1148 SourceLocation AtLoc = ConsumeToken(); // the "@"
1149 if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
1150 !Tok.isObjCAtKeyword(tok::objc_protocol) &&
1151 !Tok.isObjCAtKeyword(tok::objc_implementation)) {
1152 Diag(Tok, diag::err_objc_unexpected_attr);
1153 SkipUntil(tok::semi);
1154 return nullptr;
1155 }
1156
1157 DS.abort();
1159
1160 const char *PrevSpec = nullptr;
1161 unsigned DiagID;
1162 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID,
1163 Actions.getASTContext().getPrintingPolicy()))
1164 Diag(AtLoc, DiagID) << PrevSpec;
1165
1166 if (Tok.isObjCAtKeyword(tok::objc_protocol))
1167 return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
1168
1169 if (Tok.isObjCAtKeyword(tok::objc_implementation))
1170 return ParseObjCAtImplementationDeclaration(AtLoc, DS.getAttributes());
1171
1172 return Actions.ConvertDeclToDeclGroup(
1173 ParseObjCAtInterfaceDeclaration(AtLoc, 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() &&
1182 ProhibitAttributes(Attrs);
1183 Decl *TheDecl = ParseLinkage(DS, DeclaratorContext::File);
1184 return Actions.ConvertDeclToDeclGroup(TheDecl);
1185 }
1186
1187 return ParseDeclGroup(DS, 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 Actions.getASTContext().getSourceManager());
1198 });
1199
1200 if (DS) {
1201 return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, *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, 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();
1235 D.getIdentifierLoc(),
1236 PrevSpec, DiagID,
1237 Policy);
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(tok::l_brace) &&
1250 (!getLangOpts().CPlusPlus ||
1251 (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
1252 Tok.isNot(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(tok::l_brace, StopAtSemi | StopBeforeMatch);
1257
1258 // If we didn't find the '{', bail out.
1259 if (Tok.isNot(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(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(tok::equal) &&
1274 TemplateInfo.Kind == ParsedTemplateKind::Template &&
1275 LateParsedAttrs->empty() && Actions.canDelayFunctionBody(D)) {
1276 MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
1277
1278 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
1280 Scope *ParentScope = getCurScope()->getParent();
1281
1283 Decl *DP = Actions.HandleDeclarator(ParentScope, D,
1285 D.complete(DP);
1287
1288 if (SkipFunctionBodies && (!DP || Actions.canSkipFunctionBody(DP)) &&
1289 trySkippingFunctionBody()) {
1290 BodyScope.Exit();
1291 return Actions.ActOnSkippedFunctionBody(DP);
1292 }
1293
1294 CachedTokens Toks;
1295 LexTemplateFunctionForLateParsing(Toks);
1296
1297 if (DP) {
1298 FunctionDecl *FnD = DP->getAsFunction();
1299 Actions.CheckForFunctionRedefinition(FnD);
1300 Actions.MarkAsLateParsedTemplate(FnD, DP, Toks);
1301 }
1302 return DP;
1303 }
1304 if (CurParsedObjCImpl && !TemplateInfo.TemplateParams &&
1305 (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) || Tok.is(tok::colon)) &&
1306 Actions.CurContext->isTranslationUnit()) {
1307 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
1309 Scope *ParentScope = getCurScope()->getParent();
1310
1312 Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
1314 D.complete(FuncDecl);
1316 if (FuncDecl) {
1317 // Consume the tokens and store them for later parsing.
1318 StashAwayMethodOrFunctionBodyTokens(FuncDecl);
1319 CurParsedObjCImpl->HasCFunction = true;
1320 return FuncDecl;
1321 }
1322 // FIXME: Should we really fall through here?
1323 }
1324
1325 // Enter a scope for the function body.
1326 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
1328
1329 // Parse function body eagerly if it is either '= delete;' or '= default;' as
1330 // ActOnStartOfFunctionDef needs to know whether the function is deleted.
1331 StringLiteral *DeletedMessage = nullptr;
1333 SourceLocation KWLoc;
1334 if (TryConsumeToken(tok::equal)) {
1335 assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
1336
1337 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
1339 ? diag::warn_cxx98_compat_defaulted_deleted_function
1340 : diag::ext_defaulted_deleted_function)
1341 << 1 /* deleted */;
1342 BodyKind = Sema::FnBodyKind::Delete;
1343 DeletedMessage = ParseCXXDeletedFunctionMessage();
1344 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
1346 ? diag::warn_cxx98_compat_defaulted_deleted_function
1347 : diag::ext_defaulted_deleted_function)
1348 << 0 /* defaulted */;
1349 BodyKind = Sema::FnBodyKind::Default;
1350 } else {
1351 llvm_unreachable("function definition after = not 'delete' or 'default'");
1352 }
1353
1354 if (Tok.is(tok::comma)) {
1355 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
1356 << (BodyKind == Sema::FnBodyKind::Delete);
1357 SkipUntil(tok::semi);
1358 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
1359 BodyKind == Sema::FnBodyKind::Delete
1360 ? "delete"
1361 : "default")) {
1362 SkipUntil(tok::semi);
1363 }
1364 }
1365
1366 Sema::FPFeaturesStateRAII SaveFPFeatures(Actions);
1367
1368 // Tell the actions module that we have entered a function definition with the
1369 // specified Declarator for the function.
1370 SkipBodyInfo SkipBody;
1371 Decl *Res = Actions.ActOnStartOfFunctionDef(getCurScope(), D,
1372 TemplateInfo.TemplateParams
1373 ? *TemplateInfo.TemplateParams
1375 &SkipBody, BodyKind);
1376
1377 if (SkipBody.ShouldSkip) {
1378 // Do NOT enter SkipFunctionBody if we already consumed the tokens.
1379 if (BodyKind == Sema::FnBodyKind::Other)
1380 SkipFunctionBody();
1381
1382 // ExpressionEvaluationContext is pushed in ActOnStartOfFunctionDef
1383 // and it would be popped in ActOnFinishFunctionBody.
1384 // We pop it explcitly here since ActOnFinishFunctionBody won't get called.
1385 //
1386 // Do not call PopExpressionEvaluationContext() if it is a lambda because
1387 // one is already popped when finishing the lambda in BuildLambdaExpr().
1388 //
1389 // FIXME: It looks not easy to balance PushExpressionEvaluationContext()
1390 // and PopExpressionEvaluationContext().
1391 if (!isLambdaCallOperator(dyn_cast_if_present<FunctionDecl>(Res)))
1392 Actions.PopExpressionEvaluationContext();
1393 return Res;
1394 }
1395
1396 // Break out of the ParsingDeclarator context before we parse the body.
1397 D.complete(Res);
1398
1399 // Break out of the ParsingDeclSpec context, too. This const_cast is
1400 // safe because we're always the sole owner.
1402
1403 if (BodyKind != Sema::FnBodyKind::Other) {
1404 Actions.SetFunctionBodyKind(Res, KWLoc, BodyKind, DeletedMessage);
1405 Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;
1406 Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);
1407 return Res;
1408 }
1409
1410 // With abbreviated function templates - we need to explicitly add depth to
1411 // account for the implicit template parameter list induced by the template.
1412 if (const auto *Template = dyn_cast_if_present<FunctionTemplateDecl>(Res);
1413 Template && Template->isAbbreviated() &&
1414 Template->getTemplateParameters()->getParam(0)->isImplicit())
1415 // First template parameter is implicit - meaning no explicit template
1416 // parameter list was specified.
1417 CurTemplateDepthTracker.addDepth(1);
1418
1419 // Late attributes are parsed in the same scope as the function body.
1420 if (LateParsedAttrs)
1421 ParseLexedAttributeList(*LateParsedAttrs, Res, /*EnterScope=*/false,
1422 /*OnDefinition=*/true);
1423
1424 if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) &&
1425 trySkippingFunctionBody()) {
1426 BodyScope.Exit();
1427 Actions.ActOnSkippedFunctionBody(Res);
1428 return Actions.ActOnFinishFunctionBody(Res, nullptr, false);
1429 }
1430
1431 if (Tok.is(tok::kw_try))
1432 return ParseFunctionTryBlock(Res, BodyScope);
1433
1434 // If we have a colon, then we're probably parsing a C++
1435 // ctor-initializer.
1436 if (Tok.is(tok::colon)) {
1437 ParseConstructorInitializer(Res);
1438
1439 // Recover from error.
1440 if (!Tok.is(tok::l_brace)) {
1441 BodyScope.Exit();
1442 Actions.ActOnFinishFunctionBody(Res, nullptr);
1443 return Res;
1444 }
1445 } else
1446 Actions.ActOnDefaultCtorInitializers(Res);
1447
1448 return ParseFunctionStatementBody(Res, BodyScope);
1449}
1450
1451void Parser::SkipFunctionBody() {
1452 if (Tok.is(tok::equal)) {
1453 SkipUntil(tok::semi);
1454 return;
1455 }
1456
1457 bool IsFunctionTryBlock = Tok.is(tok::kw_try);
1458 if (IsFunctionTryBlock)
1459 ConsumeToken();
1460
1461 CachedTokens Skipped;
1462 if (ConsumeAndStoreFunctionPrologue(Skipped))
1464 else {
1465 SkipUntil(tok::r_brace);
1466 while (IsFunctionTryBlock && Tok.is(tok::kw_catch)) {
1467 SkipUntil(tok::l_brace);
1468 SkipUntil(tok::r_brace);
1469 }
1470 }
1471}
1472
1473void Parser::ParseKNRParamDeclarations(Declarator &D) {
1474 // We know that the top-level of this declarator is a function.
1475 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
1476
1477 // Enter function-declaration scope, limiting any declarators to the
1478 // function prototype scope, including parameter declarators.
1479 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1481
1482 // Read all the argument declarations.
1483 while (isDeclarationSpecifier(ImplicitTypenameContext::No)) {
1484 SourceLocation DSStart = Tok.getLocation();
1485
1486 // Parse the common declaration-specifiers piece.
1487 DeclSpec DS(AttrFactory);
1488 ParsedTemplateInfo TemplateInfo;
1489 ParseDeclarationSpecifiers(DS, TemplateInfo);
1490
1491 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
1492 // least one declarator'.
1493 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
1494 // the declarations though. It's trivial to ignore them, really hard to do
1495 // anything else with them.
1496 if (TryConsumeToken(tok::semi)) {
1497 Diag(DSStart, diag::err_declaration_does_not_declare_param);
1498 continue;
1499 }
1500
1501 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
1502 // than register.
1506 diag::err_invalid_storage_class_in_func_decl);
1508 }
1511 diag::err_invalid_storage_class_in_func_decl);
1513 }
1514
1515 // Parse the first declarator attached to this declspec.
1516 Declarator ParmDeclarator(DS, ParsedAttributesView::none(),
1518 ParseDeclarator(ParmDeclarator);
1519
1520 // Handle the full declarator list.
1521 while (true) {
1522 // If attributes are present, parse them.
1523 MaybeParseGNUAttributes(ParmDeclarator);
1524
1525 // Ask the actions module to compute the type for this declarator.
1526 Decl *Param =
1527 Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
1528
1529 if (Param &&
1530 // A missing identifier has already been diagnosed.
1531 ParmDeclarator.getIdentifier()) {
1532
1533 // Scan the argument list looking for the correct param to apply this
1534 // type.
1535 for (unsigned i = 0; ; ++i) {
1536 // C99 6.9.1p6: those declarators shall declare only identifiers from
1537 // the identifier list.
1538 if (i == FTI.NumParams) {
1539 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
1540 << ParmDeclarator.getIdentifier();
1541 break;
1542 }
1543
1544 if (FTI.Params[i].Ident == ParmDeclarator.getIdentifier()) {
1545 // Reject redefinitions of parameters.
1546 if (FTI.Params[i].Param) {
1547 Diag(ParmDeclarator.getIdentifierLoc(),
1548 diag::err_param_redefinition)
1549 << ParmDeclarator.getIdentifier();
1550 } else {
1551 FTI.Params[i].Param = Param;
1552 }
1553 break;
1554 }
1555 }
1556 }
1557
1558 // If we don't have a comma, it is either the end of the list (a ';') or
1559 // an error, bail out.
1560 if (Tok.isNot(tok::comma))
1561 break;
1562
1563 ParmDeclarator.clear();
1564
1565 // Consume the comma.
1566 ParmDeclarator.setCommaLoc(ConsumeToken());
1567
1568 // Parse the next declarator.
1569 ParseDeclarator(ParmDeclarator);
1570 }
1571
1572 // Consume ';' and continue parsing.
1573 if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration))
1574 continue;
1575
1576 // Otherwise recover by skipping to next semi or mandatory function body.
1577 if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch))
1578 break;
1579 TryConsumeToken(tok::semi);
1580 }
1581
1582 // The actions module must verify that all arguments were declared.
1583 Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
1584}
1585
1586ExprResult Parser::ParseAsmStringLiteral(bool ForAsmLabel) {
1587
1588 ExprResult AsmString;
1589 if (isTokenStringLiteral()) {
1590 AsmString = ParseStringLiteralExpression();
1591 if (AsmString.isInvalid())
1592 return AsmString;
1593
1594 const auto *SL = cast<StringLiteral>(AsmString.get());
1595 if (!SL->isOrdinary()) {
1596 Diag(Tok, diag::err_asm_operand_wide_string_literal)
1597 << SL->isWide() << SL->getSourceRange();
1598 return ExprError();
1599 }
1600 } else if (!ForAsmLabel && getLangOpts().CPlusPlus11 &&
1601 Tok.is(tok::l_paren)) {
1603 SourceLocation RParenLoc;
1604 ParsedType CastTy;
1605
1606 EnterExpressionEvaluationContext ConstantEvaluated(
1608 AsmString = ParseParenExpression(
1609 ExprType, /*StopIfCastExr=*/true, ParenExprKind::Unknown,
1610 TypoCorrectionTypeBehavior::AllowBoth, CastTy, RParenLoc);
1611 if (!AsmString.isInvalid())
1612 AsmString = Actions.ActOnConstantExpression(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(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(Tok)) {
1629 // Remove from the end of 'asm' to the end of the asm qualifier.
1630 SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1631 PP.getLocForEndOfToken(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(tok::r_paren, 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(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
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(tok::annot_cxxscope);
1692
1693 CXXScopeSpec SS;
1694 if (getLangOpts().CPlusPlus &&
1695 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1696 /*ObjectHasErrors=*/false,
1697 EnteringContext))
1699
1700 if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
1701 if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation,
1702 AllowImplicitTypename))
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(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, !WasScopeAnnotation,
1716 AllowImplicitTypename))
1718 return Tok.is(tok::annot_typename) ? AnnotatedNameKind::Success
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 getCurScope(), SS, Name, NameLoc, Next, 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(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(getCurScope(), SS, Name, NameLoc, FakeNext,
1742 SS.isEmpty() ? CCC : nullptr);
1743 }
1744
1745 switch (Classification.getKind()) {
1748
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, !WasScopeAnnotation);
1756 // We've "annotated" this as a keyword.
1758
1760 // It's not something we know about. Leave it unannotated.
1761 break;
1762
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 QualType T = Actions.GetTypeFromParser(Ty);
1777 if (getLangOpts().ObjC && NextToken().is(tok::less) &&
1779 // Consume the name.
1780 SourceLocation IdentifierLoc = ConsumeToken();
1781 SourceLocation NewEndLoc;
1782 TypeResult NewType
1783 = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
1784 /*consumeLastToken=*/false,
1785 NewEndLoc);
1786 if (NewType.isUsable())
1787 Ty = NewType.get();
1788 else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
1790 }
1791
1792 Tok.setKind(tok::annot_typename);
1793 setTypeAnnotation(Tok, Ty);
1794 Tok.setAnnotationEndLoc(Tok.getLocation());
1795 Tok.setLocation(BeginLoc);
1796 PP.AnnotateCachedTokens(Tok);
1798 }
1799
1801 Tok.setKind(tok::annot_overload_set);
1802 setExprAnnotation(Tok, Classification.getExpression());
1803 Tok.setAnnotationEndLoc(NameLoc);
1804 if (SS.isNotEmpty())
1805 Tok.setLocation(SS.getBeginLoc());
1806 PP.AnnotateCachedTokens(Tok);
1808
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, Classification.getNonTypeDecl());
1817 Tok.setLocation(NameLoc);
1818 Tok.setAnnotationEndLoc(NameLoc);
1819 PP.AnnotateCachedTokens(Tok);
1820 if (SS.isNotEmpty())
1821 AnnotateScopeToken(SS, !WasScopeAnnotation);
1823
1826 Tok.setKind(Classification.getKind() ==
1828 ? tok::annot_non_type_undeclared
1829 : tok::annot_non_type_dependent);
1830 setIdentifierAnnotation(Tok, Name);
1831 Tok.setLocation(NameLoc);
1832 Tok.setAnnotationEndLoc(NameLoc);
1833 PP.AnnotateCachedTokens(Tok);
1834 if (SS.isNotEmpty())
1835 AnnotateScopeToken(SS, !WasScopeAnnotation);
1837
1839 if (Next.isNot(tok::less)) {
1840 // This may be a type or variable template being used as a template
1841 // template argument.
1842 if (SS.isNotEmpty())
1843 AnnotateScopeToken(SS, !WasScopeAnnotation);
1845 }
1846 [[fallthrough]];
1851 bool IsConceptName =
1852 Classification.getKind() == NameClassificationKind::Concept;
1853 // We have a template name followed by '<'. Consume the identifier token so
1854 // we reach the '<' and annotate it.
1855 UnqualifiedId Id;
1856 Id.setIdentifier(Name, NameLoc);
1857 if (Next.is(tok::less))
1858 ConsumeToken();
1859 if (AnnotateTemplateIdToken(
1860 TemplateTy::make(Classification.getTemplateName()),
1861 Classification.getTemplateNameKind(), SS, SourceLocation(), Id,
1862 /*AllowTypeAnnotation=*/!IsConceptName,
1863 /*TypeConstraint=*/IsConceptName))
1865 if (SS.isNotEmpty())
1866 AnnotateScopeToken(SS, !WasScopeAnnotation);
1868 }
1869 }
1870
1871 // Unable to classify the name, but maybe we can annotate a scope specifier.
1872 if (SS.isNotEmpty())
1873 AnnotateScopeToken(SS, !WasScopeAnnotation);
1875}
1876
1878 SourceLocation TokenEndLoc = PP.getLocForEndOfToken(PrevTokLocation);
1879 return TokenEndLoc.isValid() ? TokenEndLoc : Tok.getLocation();
1880}
1881
1882bool Parser::TryKeywordIdentFallback(bool DisableKeyword) {
1883 assert(Tok.isNot(tok::identifier));
1884 Diag(Tok, diag::ext_keyword_as_ident)
1885 << PP.getSpelling(Tok)
1886 << DisableKeyword;
1887 if (DisableKeyword)
1888 Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
1889 Tok.setKind(tok::identifier);
1890 return true;
1891}
1892
1894 ImplicitTypenameContext AllowImplicitTypename) {
1895 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1896 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) ||
1897 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id) ||
1898 Tok.is(tok::kw___super) || Tok.is(tok::kw_auto) ||
1899 Tok.is(tok::annot_pack_indexing_type)) &&
1900 "Cannot be a type or scope token!");
1901
1902 if (Tok.is(tok::kw_typename)) {
1903 // MSVC lets you do stuff like:
1904 // typename typedef T_::D D;
1905 //
1906 // We will consume the typedef token here and put it back after we have
1907 // parsed the first identifier, transforming it into something more like:
1908 // typename T_::D typedef D;
1909 if (getLangOpts().MSVCCompat && NextToken().is(tok::kw_typedef)) {
1910 Token TypedefToken;
1911 PP.Lex(TypedefToken);
1912 bool Result = TryAnnotateTypeOrScopeToken(AllowImplicitTypename);
1913 PP.EnterToken(Tok, /*IsReinject=*/true);
1914 Tok = TypedefToken;
1915 if (!Result)
1916 Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
1917 return Result;
1918 }
1919
1920 // Parse a C++ typename-specifier, e.g., "typename T::type".
1921 //
1922 // typename-specifier:
1923 // 'typename' '::' [opt] nested-name-specifier identifier
1924 // 'typename' '::' [opt] nested-name-specifier template [opt]
1925 // simple-template-id
1926 SourceLocation TypenameLoc = ConsumeToken();
1927 CXXScopeSpec SS;
1928 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1929 /*ObjectHasErrors=*/false,
1930 /*EnteringContext=*/false, nullptr,
1931 /*IsTypename*/ true))
1932 return true;
1933 if (SS.isEmpty()) {
1934 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
1935 Tok.is(tok::annot_decltype)) {
1936 // Attempt to recover by skipping the invalid 'typename'
1937 if (Tok.is(tok::annot_decltype) ||
1938 (!TryAnnotateTypeOrScopeToken(AllowImplicitTypename) &&
1939 Tok.isAnnotation())) {
1940 unsigned DiagID = diag::err_expected_qualified_after_typename;
1941 // MS compatibility: MSVC permits using known types with typename.
1942 // e.g. "typedef typename T* pointer_type"
1943 if (getLangOpts().MicrosoftExt)
1944 DiagID = diag::warn_expected_qualified_after_typename;
1945 Diag(Tok.getLocation(), DiagID);
1946 return false;
1947 }
1948 }
1949 if (Tok.isEditorPlaceholder())
1950 return true;
1951
1952 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
1953 return true;
1954 }
1955
1956 bool TemplateKWPresent = false;
1957 if (Tok.is(tok::kw_template)) {
1958 ConsumeToken();
1959 TemplateKWPresent = true;
1960 }
1961
1962 TypeResult Ty;
1963 if (Tok.is(tok::identifier)) {
1964 if (TemplateKWPresent && NextToken().isNot(tok::less)) {
1965 Diag(Tok.getLocation(),
1966 diag::missing_template_arg_list_after_template_kw);
1967 return true;
1968 }
1969 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1970 *Tok.getIdentifierInfo(),
1971 Tok.getLocation());
1972 } else if (Tok.is(tok::annot_template_id)) {
1973 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1974 if (!TemplateId->mightBeType()) {
1975 Diag(Tok, diag::err_typename_refers_to_non_type_template)
1976 << Tok.getAnnotationRange();
1977 return true;
1978 }
1979
1980 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1981 TemplateId->NumArgs);
1982
1983 Ty = TemplateId->isInvalid()
1984 ? TypeError()
1985 : Actions.ActOnTypenameType(
1986 getCurScope(), TypenameLoc, SS, TemplateId->TemplateKWLoc,
1987 TemplateId->Template, TemplateId->Name,
1988 TemplateId->TemplateNameLoc, TemplateId->LAngleLoc,
1989 TemplateArgsPtr, TemplateId->RAngleLoc);
1990 } else {
1991 Diag(Tok, diag::err_expected_type_name_after_typename)
1992 << SS.getRange();
1993 return true;
1994 }
1995
1996 SourceLocation EndLoc = Tok.getLastLoc();
1997 Tok.setKind(tok::annot_typename);
1998 setTypeAnnotation(Tok, Ty);
1999 Tok.setAnnotationEndLoc(EndLoc);
2000 Tok.setLocation(TypenameLoc);
2001 PP.AnnotateCachedTokens(Tok);
2002 return false;
2003 }
2004
2005 // Remembers whether the token was originally a scope annotation.
2006 bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
2007
2008 CXXScopeSpec SS;
2009 if (getLangOpts().CPlusPlus)
2010 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
2011 /*ObjectHasErrors=*/false,
2012 /*EnteringContext*/ false))
2013 return true;
2014
2015 return TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation,
2016 AllowImplicitTypename);
2017}
2018
2020 CXXScopeSpec &SS, bool IsNewScope,
2021 ImplicitTypenameContext AllowImplicitTypename) {
2022 if (Tok.is(tok::identifier)) {
2023 // Determine whether the identifier is a type name.
2024 if (ParsedType Ty = Actions.getTypeName(
2025 *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), &SS,
2026 false, NextToken().is(tok::period), nullptr,
2027 /*IsCtorOrDtorName=*/false,
2028 /*NonTrivialTypeSourceInfo=*/true,
2029 /*IsClassTemplateDeductionContext=*/true, AllowImplicitTypename)) {
2030 SourceLocation BeginLoc = Tok.getLocation();
2031 if (SS.isNotEmpty()) // it was a C++ qualified type name.
2032 BeginLoc = SS.getBeginLoc();
2033
2034 QualType T = Actions.GetTypeFromParser(Ty);
2035
2036 /// An Objective-C object type followed by '<' is a specialization of
2037 /// a parameterized class type or a protocol-qualified type.
2038 if (getLangOpts().ObjC && NextToken().is(tok::less) &&
2039 (T->isObjCObjectType() || T->isObjCObjectPointerType())) {
2040 // Consume the name.
2042 SourceLocation NewEndLoc;
2043 TypeResult NewType
2044 = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
2045 /*consumeLastToken=*/false,
2046 NewEndLoc);
2047 if (NewType.isUsable())
2048 Ty = NewType.get();
2049 else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
2050 return false;
2051 }
2052
2053 // This is a typename. Replace the current token in-place with an
2054 // annotation type token.
2055 Tok.setKind(tok::annot_typename);
2056 setTypeAnnotation(Tok, Ty);
2057 Tok.setAnnotationEndLoc(Tok.getLocation());
2058 Tok.setLocation(BeginLoc);
2059
2060 // In case the tokens were cached, have Preprocessor replace
2061 // them with the annotation token.
2062 PP.AnnotateCachedTokens(Tok);
2063 return false;
2064 }
2065
2066 if (!getLangOpts().CPlusPlus) {
2067 // If we're in C, the only place we can have :: tokens is C23
2068 // attribute which is parsed elsewhere. If the identifier is not a type,
2069 // then it can't be scope either, just early exit.
2070 return false;
2071 }
2072
2073 // If this is a template-id, annotate with a template-id or type token.
2074 // FIXME: This appears to be dead code. We already have formed template-id
2075 // tokens when parsing the scope specifier; this can never form a new one.
2076 if (NextToken().is(tok::less)) {
2079 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2080 bool MemberOfUnknownSpecialization;
2081 if (TemplateNameKind TNK = Actions.isTemplateName(
2082 getCurScope(), SS,
2083 /*hasTemplateKeyword=*/false, TemplateName,
2084 /*ObjectType=*/nullptr, /*EnteringContext*/false, Template,
2085 MemberOfUnknownSpecialization)) {
2086 // Only annotate an undeclared template name as a template-id if the
2087 // following tokens have the form of a template argument list.
2088 if (TNK != TNK_Undeclared_template ||
2089 isTemplateArgumentList(1) != TPResult::False) {
2090 // Consume the identifier.
2091 ConsumeToken();
2092 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
2093 TemplateName)) {
2094 // If an unrecoverable error occurred, we need to return true here,
2095 // because the token stream is in a damaged state. We may not
2096 // return a valid identifier.
2097 return true;
2098 }
2099 }
2100 }
2101 }
2102
2103 // The current token, which is either an identifier or a
2104 // template-id, is not part of the annotation. Fall through to
2105 // push that token back into the stream and complete the C++ scope
2106 // specifier annotation.
2107 }
2108
2109 if (Tok.is(tok::annot_template_id)) {
2110 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
2111 if (TemplateId->Kind == TNK_Type_template) {
2112 // A template-id that refers to a type was parsed into a
2113 // template-id annotation in a context where we weren't allowed
2114 // to produce a type annotation token. Update the template-id
2115 // annotation token to a type annotation token now.
2116 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
2117 return false;
2118 }
2119 }
2120
2121 if (SS.isEmpty()) {
2122 if (getLangOpts().ObjC && !getLangOpts().CPlusPlus &&
2123 Tok.is(tok::coloncolon)) {
2124 // ObjectiveC does not allow :: as as a scope token.
2125 Diag(ConsumeToken(), diag::err_expected_type);
2126 return true;
2127 }
2128 return false;
2129 }
2130
2131 // A C++ scope specifier that isn't followed by a typename.
2132 AnnotateScopeToken(SS, IsNewScope);
2133 return false;
2134}
2135
2136bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
2137 assert(getLangOpts().CPlusPlus &&
2138 "Call sites of this function should be guarded by checking for C++");
2139 assert(MightBeCXXScopeToken() && "Cannot be a type or scope token!");
2140
2141 CXXScopeSpec SS;
2142 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
2143 /*ObjectHasErrors=*/false,
2144 EnteringContext))
2145 return true;
2146 if (SS.isEmpty())
2147 return false;
2148
2149 AnnotateScopeToken(SS, true);
2150 return false;
2151}
2152
2153bool Parser::isTokenEqualOrEqualTypo() {
2154 tok::TokenKind Kind = Tok.getKind();
2155 switch (Kind) {
2156 default:
2157 return false;
2158 case tok::ampequal: // &=
2159 case tok::starequal: // *=
2160 case tok::plusequal: // +=
2161 case tok::minusequal: // -=
2162 case tok::exclaimequal: // !=
2163 case tok::slashequal: // /=
2164 case tok::percentequal: // %=
2165 case tok::lessequal: // <=
2166 case tok::lesslessequal: // <<=
2167 case tok::greaterequal: // >=
2168 case tok::greatergreaterequal: // >>=
2169 case tok::caretequal: // ^=
2170 case tok::pipeequal: // |=
2171 case tok::equalequal: // ==
2172 Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
2173 << Kind
2174 << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
2175 [[fallthrough]];
2176 case tok::equal:
2177 return true;
2178 }
2179}
2180
2181SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
2182 assert(Tok.is(tok::code_completion));
2183 PrevTokLocation = Tok.getLocation();
2184
2185 for (Scope *S = getCurScope(); S; S = S->getParent()) {
2186 if (S->isFunctionScope()) {
2187 cutOffParsing();
2188 Actions.CodeCompletion().CodeCompleteOrdinaryName(
2190 return PrevTokLocation;
2191 }
2192
2193 if (S->isClassScope()) {
2194 cutOffParsing();
2195 Actions.CodeCompletion().CodeCompleteOrdinaryName(
2197 return PrevTokLocation;
2198 }
2199 }
2200
2201 cutOffParsing();
2202 Actions.CodeCompletion().CodeCompleteOrdinaryName(
2204 return PrevTokLocation;
2205}
2206
2207// Code-completion pass-through functions
2208
2209void Parser::CodeCompleteDirective(bool InConditional) {
2210 Actions.CodeCompletion().CodeCompletePreprocessorDirective(InConditional);
2211}
2212
2214 Actions.CodeCompletion().CodeCompleteInPreprocessorConditionalExclusion(
2215 getCurScope());
2216}
2217
2218void Parser::CodeCompleteMacroName(bool IsDefinition) {
2219 Actions.CodeCompletion().CodeCompletePreprocessorMacroName(IsDefinition);
2220}
2221
2223 Actions.CodeCompletion().CodeCompletePreprocessorExpression();
2224}
2225
2226void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
2227 MacroInfo *MacroInfo,
2228 unsigned ArgumentIndex) {
2229 Actions.CodeCompletion().CodeCompletePreprocessorMacroArgument(
2230 getCurScope(), Macro, MacroInfo, ArgumentIndex);
2231}
2232
2233void Parser::CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled) {
2234 Actions.CodeCompletion().CodeCompleteIncludedFile(Dir, IsAngled);
2235}
2236
2238 Actions.CodeCompletion().CodeCompleteNaturalLanguage();
2239}
2240
2241bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
2242 assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
2243 "Expected '__if_exists' or '__if_not_exists'");
2244 Result.IsIfExists = Tok.is(tok::kw___if_exists);
2245 Result.KeywordLoc = ConsumeToken();
2246
2247 BalancedDelimiterTracker T(*this, tok::l_paren);
2248 if (T.consumeOpen()) {
2249 Diag(Tok, diag::err_expected_lparen_after)
2250 << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
2251 return true;
2252 }
2253
2254 // Parse nested-name-specifier.
2255 if (getLangOpts().CPlusPlus)
2256 ParseOptionalCXXScopeSpecifier(Result.SS, /*ObjectType=*/nullptr,
2257 /*ObjectHasErrors=*/false,
2258 /*EnteringContext=*/false);
2259
2260 // Check nested-name specifier.
2261 if (Result.SS.isInvalid()) {
2262 T.skipToEnd();
2263 return true;
2264 }
2265
2266 // Parse the unqualified-id.
2267 SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
2268 if (ParseUnqualifiedId(Result.SS, /*ObjectType=*/nullptr,
2269 /*ObjectHadErrors=*/false, /*EnteringContext*/ false,
2270 /*AllowDestructorName*/ true,
2271 /*AllowConstructorName*/ true,
2272 /*AllowDeductionGuide*/ false, &TemplateKWLoc,
2273 Result.Name)) {
2274 T.skipToEnd();
2275 return true;
2276 }
2277
2278 if (T.consumeClose())
2279 return true;
2280
2281 // Check if the symbol exists.
2282 switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
2283 Result.IsIfExists, Result.SS,
2284 Result.Name)) {
2286 Result.Behavior =
2288 break;
2289
2291 Result.Behavior =
2293 break;
2294
2297 break;
2298
2300 return true;
2301 }
2302
2303 return false;
2304}
2305
2306void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
2307 IfExistsCondition Result;
2308 if (ParseMicrosoftIfExistsCondition(Result))
2309 return;
2310
2311 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2312 if (Braces.consumeOpen()) {
2313 Diag(Tok, diag::err_expected) << tok::l_brace;
2314 return;
2315 }
2316
2317 switch (Result.Behavior) {
2319 // Parse declarations below.
2320 break;
2321
2323 llvm_unreachable("Cannot have a dependent external declaration");
2324
2326 Braces.skipToEnd();
2327 return;
2328 }
2329
2330 // Parse the declarations.
2331 // FIXME: Support module import within __if_exists?
2332 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
2333 ParsedAttributes Attrs(AttrFactory);
2334 MaybeParseCXX11Attributes(Attrs);
2335 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
2336 DeclGroupPtrTy Result = ParseExternalDeclaration(Attrs, EmptyDeclSpecAttrs);
2337 if (Result && !getCurScope()->getParent())
2338 Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
2339 }
2340 Braces.consumeClose();
2341}
2342
2344Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) {
2345 Token Introducer = Tok;
2346 SourceLocation StartLoc = Introducer.getLocation();
2347
2348 Sema::ModuleDeclKind MDK = TryConsumeToken(tok::kw_export)
2351
2352 assert(
2353 (Tok.is(tok::kw_module) ||
2354 (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_module)) &&
2355 "not a module declaration");
2356 SourceLocation ModuleLoc = ConsumeToken();
2357
2358 // Attributes appear after the module name, not before.
2359 // FIXME: Suggest moving the attributes later with a fixit.
2360 DiagnoseAndSkipCXX11Attributes();
2361
2362 // Parse a global-module-fragment, if present.
2363 if (getLangOpts().CPlusPlusModules && Tok.is(tok::semi)) {
2364 SourceLocation SemiLoc = ConsumeToken();
2365 if (ImportState != Sema::ModuleImportState::FirstDecl ||
2366 Introducer.hasSeenNoTrivialPPDirective()) {
2367 Diag(StartLoc, diag::err_global_module_introducer_not_at_start)
2368 << SourceRange(StartLoc, SemiLoc);
2369 return nullptr;
2370 }
2372 Diag(StartLoc, diag::err_module_fragment_exported)
2373 << /*global*/0 << FixItHint::CreateRemoval(StartLoc);
2374 }
2376 return Actions.ActOnGlobalModuleFragmentDecl(ModuleLoc);
2377 }
2378
2379 // Parse a private-module-fragment, if present.
2380 if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon) &&
2381 NextToken().is(tok::kw_private)) {
2383 Diag(StartLoc, diag::err_module_fragment_exported)
2384 << /*private*/1 << FixItHint::CreateRemoval(StartLoc);
2385 }
2386 ConsumeToken();
2387 SourceLocation PrivateLoc = ConsumeToken();
2388 DiagnoseAndSkipCXX11Attributes();
2389 ExpectAndConsumeSemi(diag::err_private_module_fragment_expected_semi);
2390 ImportState = ImportState == Sema::ModuleImportState::ImportAllowed
2393 return Actions.ActOnPrivateModuleFragmentDecl(ModuleLoc, PrivateLoc);
2394 }
2395
2396 SmallVector<IdentifierLoc, 2> Path;
2397 if (ParseModuleName(ModuleLoc, Path, /*IsImport*/ false))
2398 return nullptr;
2399
2400 // Parse the optional module-partition.
2401 SmallVector<IdentifierLoc, 2> Partition;
2402 if (Tok.is(tok::colon)) {
2403 SourceLocation ColonLoc = ConsumeToken();
2404 if (!getLangOpts().CPlusPlusModules)
2405 Diag(ColonLoc, diag::err_unsupported_module_partition)
2406 << SourceRange(ColonLoc, Partition.back().getLoc());
2407 // Recover by ignoring the partition name.
2408 else if (ParseModuleName(ModuleLoc, Partition, /*IsImport*/ false))
2409 return nullptr;
2410 }
2411
2412 // We don't support any module attributes yet; just parse them and diagnose.
2413 ParsedAttributes Attrs(AttrFactory);
2414 MaybeParseCXX11Attributes(Attrs);
2415 ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr,
2416 diag::err_keyword_not_module_attr,
2417 /*DiagnoseEmptyAttrs=*/false,
2418 /*WarnOnUnknownAttrs=*/true);
2419
2420 ExpectAndConsumeSemi(diag::err_module_expected_semi);
2421
2422 return Actions.ActOnModuleDecl(StartLoc, ModuleLoc, MDK, Path, Partition,
2423 ImportState,
2424 Introducer.hasSeenNoTrivialPPDirective());
2425}
2426
2427Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
2428 Sema::ModuleImportState &ImportState) {
2429 SourceLocation StartLoc = AtLoc.isInvalid() ? Tok.getLocation() : AtLoc;
2430
2431 SourceLocation ExportLoc;
2432 TryConsumeToken(tok::kw_export, ExportLoc);
2433
2434 assert((AtLoc.isInvalid() ? Tok.isOneOf(tok::kw_import, tok::identifier)
2435 : Tok.isObjCAtKeyword(tok::objc_import)) &&
2436 "Improper start to module import");
2437 bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import);
2438 SourceLocation ImportLoc = ConsumeToken();
2439
2440 // For C++20 modules, we can have "name" or ":Partition name" as valid input.
2441 SmallVector<IdentifierLoc, 2> Path;
2442 bool IsPartition = false;
2443 Module *HeaderUnit = nullptr;
2444 if (Tok.is(tok::header_name)) {
2445 // This is a header import that the preprocessor decided we should skip
2446 // because it was malformed in some way. Parse and ignore it; it's already
2447 // been diagnosed.
2448 ConsumeToken();
2449 } else if (Tok.is(tok::annot_header_unit)) {
2450 // This is a header import that the preprocessor mapped to a module import.
2451 HeaderUnit = reinterpret_cast<Module *>(Tok.getAnnotationValue());
2452 ConsumeAnnotationToken();
2453 } else if (Tok.is(tok::colon)) {
2454 SourceLocation ColonLoc = ConsumeToken();
2455 if (!getLangOpts().CPlusPlusModules)
2456 Diag(ColonLoc, diag::err_unsupported_module_partition)
2457 << SourceRange(ColonLoc, Path.back().getLoc());
2458 // Recover by leaving partition empty.
2459 else if (ParseModuleName(ColonLoc, Path, /*IsImport*/ true))
2460 return nullptr;
2461 else
2462 IsPartition = true;
2463 } else {
2464 if (ParseModuleName(ImportLoc, Path, /*IsImport*/ true))
2465 return nullptr;
2466 }
2467
2468 ParsedAttributes Attrs(AttrFactory);
2469 MaybeParseCXX11Attributes(Attrs);
2470 // We don't support any module import attributes yet.
2471 ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr,
2472 diag::err_keyword_not_import_attr,
2473 /*DiagnoseEmptyAttrs=*/false,
2474 /*WarnOnUnknownAttrs=*/true);
2475
2476 if (PP.hadModuleLoaderFatalFailure()) {
2477 // With a fatal failure in the module loader, we abort parsing.
2478 cutOffParsing();
2479 return nullptr;
2480 }
2481
2482 // Diagnose mis-imports.
2483 bool SeenError = true;
2484 switch (ImportState) {
2486 SeenError = false;
2487 break;
2489 // If we found an import decl as the first declaration, we must be not in
2490 // a C++20 module unit or we are in an invalid state.
2492 [[fallthrough]];
2494 // We can only import a partition within a module purview.
2495 if (IsPartition)
2496 Diag(ImportLoc, diag::err_partition_import_outside_module);
2497 else
2498 SeenError = false;
2499 break;
2502 // We can only have pre-processor directives in the global module fragment
2503 // which allows pp-import, but not of a partition (since the global module
2504 // does not have partitions).
2505 // We cannot import a partition into a private module fragment, since
2506 // [module.private.frag]/1 disallows private module fragments in a multi-
2507 // TU module.
2508 if (IsPartition || (HeaderUnit && HeaderUnit->Kind !=
2510 Diag(ImportLoc, diag::err_import_in_wrong_fragment)
2511 << IsPartition
2512 << (ImportState == Sema::ModuleImportState::GlobalFragment ? 0 : 1);
2513 else
2514 SeenError = false;
2515 break;
2518 if (getLangOpts().CPlusPlusModules)
2519 Diag(ImportLoc, diag::err_import_not_allowed_here);
2520 else
2521 SeenError = false;
2522 break;
2523 }
2524 ExpectAndConsumeSemi(diag::err_module_expected_semi);
2525 TryConsumeToken(tok::eod);
2526
2527 if (SeenError)
2528 return nullptr;
2529
2531 if (HeaderUnit)
2532 Import =
2533 Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit);
2534 else if (!Path.empty())
2535 Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path,
2536 IsPartition);
2537 if (Import.isInvalid())
2538 return nullptr;
2539
2540 // Using '@import' in framework headers requires modules to be enabled so that
2541 // the header is parseable. Emit a warning to make the user aware.
2542 if (IsObjCAtImport && AtLoc.isValid()) {
2543 auto &SrcMgr = PP.getSourceManager();
2544 auto FE = SrcMgr.getFileEntryRefForID(SrcMgr.getFileID(AtLoc));
2545 if (FE && llvm::sys::path::parent_path(FE->getDir().getName())
2546 .ends_with(".framework"))
2547 Diags.Report(AtLoc, diag::warn_atimport_in_framework_header);
2548 }
2549
2550 return Import.get();
2551}
2552
2553bool Parser::ParseModuleName(SourceLocation UseLoc,
2554 SmallVectorImpl<IdentifierLoc> &Path,
2555 bool IsImport) {
2556 // Parse the module path.
2557 while (true) {
2558 if (!Tok.is(tok::identifier)) {
2559 if (Tok.is(tok::code_completion)) {
2560 cutOffParsing();
2561 Actions.CodeCompletion().CodeCompleteModuleImport(UseLoc, Path);
2562 return true;
2563 }
2564
2565 Diag(Tok, diag::err_module_expected_ident) << IsImport;
2566 SkipUntil(tok::semi);
2567 return true;
2568 }
2569
2570 // Record this part of the module path.
2571 Path.emplace_back(Tok.getLocation(), Tok.getIdentifierInfo());
2572 ConsumeToken();
2573
2574 if (Tok.isNot(tok::period))
2575 return false;
2576
2577 ConsumeToken();
2578 }
2579}
2580
2581bool Parser::parseMisplacedModuleImport() {
2582 while (true) {
2583 switch (Tok.getKind()) {
2584 case tok::annot_module_end:
2585 // If we recovered from a misplaced module begin, we expect to hit a
2586 // misplaced module end too. Stay in the current context when this
2587 // happens.
2588 if (MisplacedModuleBeginCount) {
2589 --MisplacedModuleBeginCount;
2590 Actions.ActOnAnnotModuleEnd(
2591 Tok.getLocation(),
2592 reinterpret_cast<Module *>(Tok.getAnnotationValue()));
2593 ConsumeAnnotationToken();
2594 continue;
2595 }
2596 // Inform caller that recovery failed, the error must be handled at upper
2597 // level. This will generate the desired "missing '}' at end of module"
2598 // diagnostics on the way out.
2599 return true;
2600 case tok::annot_module_begin:
2601 // Recover by entering the module (Sema will diagnose).
2602 Actions.ActOnAnnotModuleBegin(
2603 Tok.getLocation(),
2604 reinterpret_cast<Module *>(Tok.getAnnotationValue()));
2605 ConsumeAnnotationToken();
2606 ++MisplacedModuleBeginCount;
2607 continue;
2608 case tok::annot_module_include:
2609 // Module import found where it should not be, for instance, inside a
2610 // namespace. Recover by importing the module.
2611 Actions.ActOnAnnotModuleInclude(
2612 Tok.getLocation(),
2613 reinterpret_cast<Module *>(Tok.getAnnotationValue()));
2614 ConsumeAnnotationToken();
2615 // If there is another module import, process it.
2616 continue;
2617 default:
2618 return false;
2619 }
2620 }
2621 return false;
2622}
2623
2624void Parser::diagnoseUseOfC11Keyword(const Token &Tok) {
2625 // Warn that this is a C11 extension if in an older mode or if in C++.
2626 // Otherwise, warn that it is incompatible with standards before C11 if in
2627 // C11 or later.
2628 Diag(Tok, getLangOpts().C11 ? diag::warn_c11_compat_keyword
2629 : diag::ext_c11_feature)
2630 << Tok.getName();
2631}
2632
2633bool BalancedDelimiterTracker::diagnoseOverflow() {
2634 P.Diag(P.Tok, diag::err_bracket_depth_exceeded)
2635 << P.getLangOpts().BracketDepth;
2636 P.Diag(P.Tok, diag::note_bracket_depth);
2637 P.cutOffParsing();
2638 return true;
2639}
2640
2642 const char *Msg,
2643 tok::TokenKind SkipToTok) {
2644 LOpen = P.Tok.getLocation();
2645 if (P.ExpectAndConsume(Kind, DiagID, Msg)) {
2646 if (SkipToTok != tok::unknown)
2647 P.SkipUntil(SkipToTok, Parser::StopAtSemi);
2648 return true;
2649 }
2650
2651 if (getDepth() < P.getLangOpts().BracketDepth)
2652 return false;
2653
2654 return diagnoseOverflow();
2655}
2656
2657bool BalancedDelimiterTracker::diagnoseMissingClose() {
2658 assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
2659
2660 if (P.Tok.is(tok::annot_module_end))
2661 P.Diag(P.Tok, diag::err_missing_before_module_end) << Close;
2662 else
2663 P.Diag(P.Tok, diag::err_expected) << Close;
2664 P.Diag(LOpen, diag::note_matching) << Kind;
2665
2666 // If we're not already at some kind of closing bracket, skip to our closing
2667 // token.
2668 if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) &&
2669 P.Tok.isNot(tok::r_square) &&
2670 P.SkipUntil(Close, FinalToken,
2672 P.Tok.is(Close))
2673 LClose = P.ConsumeAnyToken();
2674 return true;
2675}
2676
2678 P.SkipUntil(Close, Parser::StopBeforeMatch);
2679 consumeClose();
2680}
Defines the clang::ASTContext interface.
This file provides some common utility functions for processing Lambda related AST Constructs.
static Decl::Kind getKind(const Decl *D)
This file defines the classes used to store parsed information about declaration-specifiers and decla...
Defines the C++ template declaration subclasses.
bool isNot(T Kind) const
Token Tok
The Token.
FormatToken * Next
The next token in the unwrapped line.
bool is(tok::TokenKind Kind) const
static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok)
Definition Parser.cpp:118
static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R)
Definition Parser.cpp:283
This file declares facilities that support code completion.
Defines a utilitiy for warning once when close to out of stack space.
PtrTy get() const
Definition Ownership.h:171
bool isInvalid() const
Definition Ownership.h:167
bool isUsable() const
Definition Ownership.h:169
bool expectAndConsume(unsigned DiagID=diag::err_expected, const char *Msg="", tok::TokenKind SkipToTok=tok::unknown)
Definition Parser.cpp:2641
Represents a C++ nested-name-specifier or a global scope specifier.
Definition DeclSpec.h:73
bool isNotEmpty() const
A scope specifier is present, but may be valid or invalid.
Definition DeclSpec.h:180
SourceRange getRange() const
Definition DeclSpec.h:79
SourceLocation getBeginLoc() const
Definition DeclSpec.h:83
bool isInvalid() const
An error occurred during parsing of the scope specifier.
Definition DeclSpec.h:183
bool isEmpty() const
No scope specifier.
Definition DeclSpec.h:178
virtual void CodeCompletePreprocessorExpression()
Callback invoked when performing code completion in a preprocessor expression, such as the condition ...
virtual void CodeCompleteNaturalLanguage()
Callback invoked when performing code completion in a part of the file where we expect natural langua...
virtual void CodeCompleteInConditionalExclusion()
Callback invoked when performing code completion within a block of code that was excluded due to prep...
Abstract base class that describes a handler that will receive source ranges for each of the comments...
void ClearStorageClassSpecs()
Definition DeclSpec.h:485
TST getTypeSpecType() const
Definition DeclSpec.h:507
SourceLocation getStorageClassSpecLoc() const
Definition DeclSpec.h:480
SCS getStorageClassSpec() const
Definition DeclSpec.h:471
bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, unsigned &DiagID, const PrintingPolicy &Policy)
Definition DeclSpec.cpp:834
SourceRange getSourceRange() const LLVM_READONLY
Definition DeclSpec.h:544
void SetRangeEnd(SourceLocation Loc)
Definition DeclSpec.h:679
static const TST TST_interface
Definition DeclSpec.h:274
void SetRangeStart(SourceLocation Loc)
Definition DeclSpec.h:678
static const TST TST_union
Definition DeclSpec.h:272
static const TST TST_int
Definition DeclSpec.h:255
TSCS getThreadStorageClassSpec() const
Definition DeclSpec.h:472
ParsedAttributes & getAttributes()
Definition DeclSpec.h:843
static const TST TST_enum
Definition DeclSpec.h:271
static bool isDeclRep(TST T)
Definition DeclSpec.h:439
void takeAttributesAppendingingFrom(ParsedAttributes &attrs)
Definition DeclSpec.h:846
static const TST TST_class
Definition DeclSpec.h:275
TypeSpecifierType TST
Definition DeclSpec.h:247
bool hasTagDefinition() const
Definition DeclSpec.cpp:433
unsigned getParsedSpecifiers() const
Return a bitmask of which flavors of specifiers this DeclSpec includes.
Definition DeclSpec.cpp:442
static const TSCS TSCS_unspecified
Definition DeclSpec.h:235
static const char * getSpecifierName(DeclSpec::TST T, const PrintingPolicy &Policy)
Turn a type-specifier-type into a string like "_Bool" or "union".
Definition DeclSpec.cpp:532
SourceLocation getThreadStorageClassSpecLoc() const
Definition DeclSpec.h:481
Decl * getRepAsDecl() const
Definition DeclSpec.h:521
static const TST TST_unspecified
Definition DeclSpec.h:248
bool isEmpty() const
isEmpty - Return true if this declaration specifier is completely empty: no tokens were parsed in the...
Definition DeclSpec.h:674
SourceLocation getTypeSpecTypeLoc() const
Definition DeclSpec.h:552
@ PQ_StorageClassSpecifier
Definition DeclSpec.h:316
static const TST TST_struct
Definition DeclSpec.h:273
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition DeclBase.h:1087
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition DeclBase.cpp:273
void SetRangeBegin(SourceLocation Loc)
SetRangeBegin - Set the start of the source range to Loc, unless it's invalid.
Definition DeclSpec.h:2063
const ParsedAttributes & getAttributes() const
Definition DeclSpec.h:2657
SourceLocation getIdentifierLoc() const
Definition DeclSpec.h:2310
void setFunctionDefinitionKind(FunctionDefinitionKind Val)
Definition DeclSpec.h:2707
DeclaratorChunk::FunctionTypeInfo & getFunctionTypeInfo()
getFunctionTypeInfo - Retrieves the function type info object (looking through parentheses).
Definition DeclSpec.h:2461
A little helper class used to produce diagnostics.
static unsigned getCXXCompatDiagId(const LangOptions &LangOpts, unsigned CompatDiagId)
Get the appropriate diagnostic Id to use for issuing a compatibility diagnostic.
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition Diagnostic.h:140
static FixItHint CreateRemoval(CharSourceRange RemoveRange)
Create a code modification hint that removes the given source range.
Definition Diagnostic.h:129
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition Diagnostic.h:103
One of these records is kept for each identifier that is lexed.
tok::TokenKind getTokenID() const
If this is a source-language token (e.g.
A simple pair of identifier info and location.
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition Decl.h:5035
Describes a module or submodule.
Definition Module.h:144
ModuleKind Kind
The kind of this module.
Definition Module.h:189
bool isHeaderUnit() const
Is this module a header unit.
Definition Module.h:669
@ ModuleHeaderUnit
This is a C++20 header unit.
Definition Module.h:161
static OpaquePtr make(TemplateName P)
Definition Ownership.h:61
RAII object that makes sure paren/bracket/brace count is correct after declaration/statement parsing,...
static const ParsedAttributesView & none()
Definition ParsedAttr.h:817
ParsedAttributes - A collection of parsed attributes.
Definition ParsedAttr.h:937
ParseScope - Introduces a new scope for parsing.
Definition Parser.h:396
Parser - This implements a parser for the C family of languages.
Definition Parser.h:171
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Definition Parser.cpp:85
SourceLocation getEndOfPreviousToken() const
Definition Parser.cpp:1877
bool TryAnnotateTypeOrScopeTokenAfterScopeSpec(CXXScopeSpec &SS, bool IsNewScope, ImplicitTypenameContext AllowImplicitTypename)
Try to annotate a type or scope token, having already parsed an optional scope specifier.
Definition Parser.cpp:2019
DiagnosticBuilder DiagCompat(SourceLocation Loc, unsigned CompatDiagId)
Definition Parser.cpp:93
ExprResult ParseStringLiteralExpression(bool AllowUserDefinedLiteral=false)
ParseStringLiteralExpression - This handles the various token types that form string literals,...
SourceLocation ConsumeToken()
ConsumeToken - Consume the current 'peek token' and lex the next one.
Definition Parser.h:262
Parser(Preprocessor &PP, Sema &Actions, bool SkipFunctionBodies)
Definition Parser.cpp:56
bool ParseTopLevelDecl()
Definition Parser.h:251
void EnterScope(unsigned ScopeFlags)
EnterScope - Start a new scope.
Definition Parser.cpp:420
bool ParseUnqualifiedId(CXXScopeSpec &SS, ParsedType ObjectType, bool ObjectHadErrors, bool EnteringContext, bool AllowDestructorName, bool AllowConstructorName, bool AllowDeductionGuide, SourceLocation *TemplateKWLoc, UnqualifiedId &Result)
Parse a C++ unqualified-id (or a C identifier), which describes the name of an entity.
DeclGroupPtrTy ParseOpenACCDirectiveDecl(AccessSpecifier &AS, ParsedAttributes &Attrs, DeclSpec::TST TagType, Decl *TagDecl)
Parse OpenACC directive on a declaration.
~Parser() override
Definition Parser.cpp:465
SourceLocation ConsumeAnyToken(bool ConsumeCodeCompletionTok=false)
ConsumeAnyToken - Dispatch to the right Consume* method based on the current token type.
Definition Parser.h:290
const Token & GetLookAheadToken(unsigned N)
GetLookAheadToken - This peeks ahead N tokens and returns that token without consuming any tokens.
Definition Parser.h:316
bool TryConsumeToken(tok::TokenKind Expected)
Definition Parser.h:270
OpaquePtr< DeclGroupRef > DeclGroupPtrTy
Definition Parser.h:219
Scope * getCurScope() const
Definition Parser.h:211
OpaquePtr< TemplateName > TemplateTy
Definition Parser.h:220
bool SkipUntil(tok::TokenKind T, SkipUntilFlags Flags=static_cast< SkipUntilFlags >(0))
SkipUntil - Read tokens until we get to the specified token, then consume it (unless StopBeforeMatch ...
Definition Parser.h:495
void SkipMalformedDecl()
SkipMalformedDecl - Read tokens until we get to some likely good stopping point for skipping past a s...
friend class PoisonSEHIdentifiersRAIIObject
Definition Parser.h:197
void ExitScope()
ExitScope - Pop a scope off the scope stack.
Definition Parser.cpp:430
const LangOptions & getLangOpts() const
Definition Parser.h:204
bool ParseFirstTopLevelDecl(DeclGroupPtrTy &Result, Sema::ModuleImportState &ImportState)
Parse the first top-level declaration in a translation unit.
Definition Parser.cpp:594
SkipUntilFlags
Control flags for SkipUntil functions.
Definition Parser.h:473
@ StopBeforeMatch
Stop skipping at specified token, but don't skip the token itself.
Definition Parser.h:476
@ StopAtCodeCompletion
Stop at code completion.
Definition Parser.h:477
@ StopAtSemi
Stop skipping at semicolon.
Definition Parser.h:474
bool TryAnnotateTypeOrScopeToken(ImplicitTypenameContext AllowImplicitTypename=ImplicitTypenameContext::No)
TryAnnotateTypeOrScopeToken - If the current token position is on a typename (possibly qualified in C...
Definition Parser.cpp:1893
bool MightBeCXXScopeToken()
Definition Parser.h:376
const Token & NextToken()
NextToken - This peeks ahead one token and returns it without consuming it.
Definition Parser.h:324
friend class BalancedDelimiterTracker
Definition Parser.h:199
SmallVector< TemplateParameterList *, 4 > TemplateParameterLists
Definition Parser.h:7770
void Initialize()
Initialize - Warm up the parser.
Definition Parser.cpp:483
bool TryAnnotateCXXScopeToken(bool EnteringContext=false)
TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only annotates C++ scope specifiers and ...
Definition Parser.cpp:2136
A class for parsing a DeclSpec.
const ParsingDeclSpec & getDeclSpec() const
ParsingDeclSpec & getMutableDeclSpec() const
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
bool isCodeCompletionEnabled() const
Determine if we are performing code completion.
bool isCodeCompletionReached() const
Returns true if code-completion is enabled and we have hit the code-completion point.
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Computes the source location just past the end of the token at this source location.
A (possibly-)qualified type.
Definition TypeBase.h:937
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
void Init(Scope *parent, unsigned flags)
Init - This is used by the parser to implement scope caching.
Definition Scope.cpp:95
const Scope * getParent() const
getParent - Return the scope that this is nested in.
Definition Scope.h:287
@ FunctionPrototypeScope
This is a scope that corresponds to the parameters within a function prototype.
Definition Scope.h:85
@ CompoundStmtScope
This is a compound statement scope.
Definition Scope.h:134
@ FunctionDeclarationScope
This is a scope that corresponds to the parameters within a function prototype for a function declara...
Definition Scope.h:91
@ FnScope
This indicates that the scope corresponds to a function, which means that labels are set here.
Definition Scope.h:51
@ DeclScope
This is a scope that can contain a declaration.
Definition Scope.h:63
ParserCompletionContext
Describes the context in which code completion occurs.
@ PCC_TopLevelOrExpression
Code completion occurs at top-level in a REPL session.
@ PCC_Class
Code completion occurs within a class, struct, or union.
@ PCC_ObjCImplementation
Code completion occurs within an Objective-C implementation or category implementation.
@ PCC_Namespace
Code completion occurs at top-level or namespace context.
@ PCC_RecoveryInFunction
Code completion occurs within the body of a function on a recovery path, where we do not have a speci...
ExprResult getExpression() const
Definition Sema.h:3717
NameClassificationKind getKind() const
Definition Sema.h:3715
NamedDecl * getNonTypeDecl() const
Definition Sema.h:3727
TemplateName getTemplateName() const
Definition Sema.h:3732
ParsedType getType() const
Definition Sema.h:3722
TemplateNameKind getTemplateNameKind() const
Definition Sema.h:3741
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:854
@ Interface
'export module X;'
Definition Sema.h:9841
@ Implementation
'module X;'
Definition Sema.h:9842
@ Other
C++26 [dcl.fct.def.general]p1 function-body: ctor-initializer[opt] compound-statement function-try-bl...
Definition Sema.h:4130
@ Default
= default ;
Definition Sema.h:4132
@ Delete
deleted-function-body
Definition Sema.h:4138
ModuleImportState
An enumeration to represent the transition of states in parsing module fragments and imports.
Definition Sema.h:9850
@ PrivateFragmentImportFinished
after 'module :private;' but a non-import decl has already been seen.
Definition Sema.h:9857
@ ImportFinished
after any non-import decl.
Definition Sema.h:9854
@ PrivateFragmentImportAllowed
after 'module :private;' but before any non-import decl.
Definition Sema.h:9855
@ FirstDecl
Parsing the first decl in a TU.
Definition Sema.h:9851
@ GlobalFragment
after 'module;' but before 'module X;'
Definition Sema.h:9852
@ NotACXX20Module
Not a C++20 TU, or an invalid state was found.
Definition Sema.h:9859
@ ImportAllowed
after 'module X;' but before any non-import decl.
Definition Sema.h:9853
@ ConstantEvaluated
The current context is "potentially evaluated" in C++11 terms, but the expression is evaluated at com...
Definition Sema.h:6702
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
A trivial tuple used to represent a source range.
bool isInvalid() const
SourceLocation getEnd() const
SourceLocation getBegin() const
Represents a C++ template name within the type system.
Token - This structure provides full information about a lexed token.
Definition Token.h:36
IdentifierInfo * getIdentifierInfo() const
Definition Token.h:189
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition Token.h:134
void setKind(tok::TokenKind K)
Definition Token.h:98
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition Token.h:102
void * getAnnotationValue() const
Definition Token.h:236
tok::TokenKind getKind() const
Definition Token.h:97
bool hasSeenNoTrivialPPDirective() const
Definition Token.h:324
bool isObjCObjectType() const
Definition TypeBase.h:8688
bool isObjCObjectPointerType() const
Definition TypeBase.h:8684
Represents a C++ unqualified-id that has been parsed.
Definition DeclSpec.h:998
void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc)
Specify that this unqualified-id was parsed as an identifier.
Definition DeclSpec.h:1086
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition TokenKinds.h:25
const char * getPunctuatorSpelling(TokenKind Kind) LLVM_READNONE
Determines the spelling of simple punctuation tokens like '!
The JSON file list parser is used to communicate input to InstallAPI.
ImplicitTypenameContext
Definition DeclSpec.h:1857
@ CPlusPlus
@ CPlusPlus11
MutableArrayRef< TemplateParameterList * > MultiTemplateParamsArg
Definition Ownership.h:263
AnnotatedNameKind
Definition Parser.h:55
@ Unresolved
The identifier can't be resolved.
Definition Parser.h:63
@ Success
Annotation was successful.
Definition Parser.h:65
@ Error
Annotation has failed and emitted an error.
Definition Parser.h:57
@ TentativeDecl
The identifier is a tentatively-declared name.
Definition Parser.h:59
@ TemplateName
The identifier is a template name. FIXME: Add an annotation for that.
Definition Parser.h:61
@ Self
'self' clause, allowed on Compute and Combined Constructs, plus 'update'.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition Specifiers.h:123
@ AS_none
Definition Specifiers.h:127
ActionResult< Decl * > DeclResult
Definition Ownership.h:255
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
TypeResult TypeError()
Definition Ownership.h:267
@ Dependent
Parse the block as a dependent block, which may be used in some template instantiations but not other...
Definition Parser.h:142
@ Skip
Skip the block entirely; this code is never used.
Definition Parser.h:139
@ Parse
Parse the block; this code is always used.
Definition Parser.h:137
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition ASTLambda.h:28
@ Result
The result type of a method or function.
Definition TypeBase.h:905
ActionResult< ParsedType > TypeResult
Definition Ownership.h:251
const FunctionProtoType * T
@ Template
We are parsing a template declaration.
Definition Parser.h:81
ExprResult ExprError()
Definition Ownership.h:265
@ FunctionTemplate
The name was classified as a function template name.
Definition Sema.h:585
@ Keyword
The name has been typo-corrected to a keyword.
Definition Sema.h:560
@ DependentNonType
The name denotes a member of a dependent type that could not be resolved.
Definition Sema.h:574
@ UndeclaredTemplate
The name was classified as an ADL-only function template name.
Definition Sema.h:587
@ NonType
The name was classified as a specific non-type, non-template declaration.
Definition Sema.h:566
@ Unknown
This name is not a type or template in this context, but might be something else.
Definition Sema.h:556
@ Error
Classification failed; an error has been produced.
Definition Sema.h:558
@ Type
The name was classified as a type.
Definition Sema.h:562
@ TypeTemplate
The name was classified as a template whose specializations are types.
Definition Sema.h:581
@ Concept
The name was classified as a concept name.
Definition Sema.h:589
@ OverloadSet
The name was classified as an overload set, and an expression representing that overload set has been...
Definition Sema.h:579
@ UndeclaredNonType
The name was classified as an ADL-only function name.
Definition Sema.h:570
@ VarTemplate
The name was classified as a variable template name.
Definition Sema.h:583
MutableArrayRef< ParsedTemplateArgument > ASTTemplateArgsPtr
Definition Ownership.h:261
ExtraSemiKind
The kind of extra semi diagnostic to emit.
Definition Parser.h:69
@ AfterMemberFunctionDefinition
Definition Parser.h:73
TemplateNameKind
Specifies the kind of template name that an identifier refers to.
@ TNK_Type_template
The name refers to a template whose specialization produces a type.
@ TNK_Undeclared_template
Lookup for the name failed, but we're assuming it was a template name anyway.
@ Dependent
The name is a dependent name, so the results will differ from one instantiation to the next.
Definition Sema.h:798
@ Exists
The symbol exists.
Definition Sema.h:791
@ Error
An error occurred.
Definition Sema.h:801
@ DoesNotExist
The symbol does not exist.
Definition Sema.h:794
U cast(CodeGen::Address addr)
Definition Address.h:327
SmallVector< Token, 4 > CachedTokens
A set of tokens that has been cached for later parsing.
Definition DeclSpec.h:1215
OpaquePtr< QualType > ParsedType
An opaque type for threading parsed type information through the parser.
Definition Ownership.h:230
ParenParseOption
ParenParseOption - Control what ParseParenExpression will parse.
Definition Parser.h:116
ActionResult< Expr * > ExprResult
Definition Ownership.h:249
@ Braces
New-expression has a C++11 list-initializer.
Definition ExprCXX.h:2250
#define false
Definition stdbool.h:26
#define true
Definition stdbool.h:25
ParamInfo * Params
Params - This is a pointer to a new[]'d array of ParamInfo objects that describe the parameters speci...
Definition DeclSpec.h:1398
unsigned NumParams
NumParams - This is the number of formal parameters specified by the declarator.
Definition DeclSpec.h:1373
bool isKNRPrototype() const
isKNRPrototype - Return true if this is a K&R style identifier list, like "void foo(a,...
Definition DeclSpec.h:1478
const IdentifierInfo * Ident
Definition DeclSpec.h:1304
Information about a template-id annotation token.
const IdentifierInfo * Name
FIXME: Temporarily stores the name of a specialization.
TemplateNameKind Kind
The kind of template that Template refers to.
unsigned NumArgs
NumArgs - The number of template arguments.
SourceLocation TemplateNameLoc
TemplateNameLoc - The location of the template name within the source.
ParsedTemplateArgument * getTemplateArgs()
Retrieves a pointer to the template arguments.
SourceLocation RAngleLoc
The location of the '>' after the template argument list.
SourceLocation LAngleLoc
The location of the '<' before the template argument list.
SourceLocation TemplateKWLoc
TemplateKWLoc - The location of the template keyword.
bool mightBeType() const
Determine whether this might be a type template.
ParsedTemplateTy Template
The declaration of the template corresponding to the template-name.