clang 17.0.0git
SemaModule.cpp
Go to the documentation of this file.
1//===--- SemaModule.cpp - Semantic Analysis for Modules -------------------===//
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 semantic analysis for modules (C++ modules syntax,
10// Objective-C modules syntax, and Clang header modules).
11//
12//===----------------------------------------------------------------------===//
13
18#include <optional>
19
20using namespace clang;
21using namespace sema;
22
24 SourceLocation ImportLoc, DeclContext *DC,
25 bool FromInclude = false) {
26 SourceLocation ExternCLoc;
27
28 if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) {
29 switch (LSD->getLanguage()) {
31 if (ExternCLoc.isInvalid())
32 ExternCLoc = LSD->getBeginLoc();
33 break;
35 break;
36 }
37 DC = LSD->getParent();
38 }
39
40 while (isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC))
41 DC = DC->getParent();
42
43 if (!isa<TranslationUnitDecl>(DC)) {
44 S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M))
45 ? diag::ext_module_import_not_at_top_level_noop
46 : diag::err_module_import_not_at_top_level_fatal)
47 << M->getFullModuleName() << DC;
48 S.Diag(cast<Decl>(DC)->getBeginLoc(),
49 diag::note_module_import_not_at_top_level)
50 << DC;
51 } else if (!M->IsExternC && ExternCLoc.isValid()) {
52 S.Diag(ImportLoc, diag::ext_module_import_in_extern_c)
53 << M->getFullModuleName();
54 S.Diag(ExternCLoc, diag::note_extern_c_begins_here);
55 }
56}
57
58// We represent the primary and partition names as 'Paths' which are sections
59// of the hierarchical access path for a clang module. However for C++20
60// the periods in a name are just another character, and we will need to
61// flatten them into a string.
62static std::string stringFromPath(ModuleIdPath Path) {
63 std::string Name;
64 if (Path.empty())
65 return Name;
66
67 for (auto &Piece : Path) {
68 if (!Name.empty())
69 Name += ".";
70 Name += Piece.first->getName();
71 }
72 return Name;
73}
74
77 // We start in the global module;
78 Module *GlobalModule =
79 PushGlobalModuleFragment(ModuleLoc);
80
81 // All declarations created from now on are owned by the global module.
83 // [module.global.frag]p2
84 // A global-module-fragment specifies the contents of the global module
85 // fragment for a module unit. The global module fragment can be used to
86 // provide declarations that are attached to the global module and usable
87 // within the module unit.
88 //
89 // So the declations in the global module shouldn't be visible by default.
91 TU->setLocalOwningModule(GlobalModule);
92
93 // FIXME: Consider creating an explicit representation of this declaration.
94 return nullptr;
95}
96
97void Sema::HandleStartOfHeaderUnit() {
98 assert(getLangOpts().CPlusPlusModules &&
99 "Header units are only valid for C++20 modules");
100 SourceLocation StartOfTU =
102
103 StringRef HUName = getLangOpts().CurrentModule;
104 if (HUName.empty()) {
106 const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str();
107 }
108
109 // TODO: Make the C++20 header lookup independent.
110 // When the input is pre-processed source, we need a file ref to the original
111 // file for the header map.
113 // For the sake of error recovery (if someone has moved the original header
114 // after creating the pre-processed output) fall back to obtaining the file
115 // ref for the input file, which must be present.
116 if (!F)
118 assert(F && "failed to find the header unit source?");
119 Module::Header H{HUName.str(), HUName.str(), *F};
120 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
121 Module *Mod = Map.createHeaderUnit(StartOfTU, HUName, H);
122 assert(Mod && "module creation should not fail");
123 ModuleScopes.push_back({}); // No GMF
124 ModuleScopes.back().BeginLoc = StartOfTU;
125 ModuleScopes.back().Module = Mod;
126 ModuleScopes.back().ModuleInterface = true;
127 VisibleModules.setVisible(Mod, StartOfTU);
128
129 // From now on, we have an owning module for all declarations we see.
130 // All of these are implicitly exported.
131 auto *TU = Context.getTranslationUnitDecl();
133 TU->setLocalOwningModule(Mod);
134}
135
136/// Tests whether the given identifier is reserved as a module name and
137/// diagnoses if it is. Returns true if a diagnostic is emitted and false
138/// otherwise.
140 SourceLocation Loc) {
141 enum {
142 Valid = -1,
143 Invalid = 0,
144 Reserved = 1,
145 } Reason = Valid;
146
147 if (II->isStr("module") || II->isStr("import"))
148 Reason = Invalid;
149 else if (II->isReserved(S.getLangOpts()) !=
151 Reason = Reserved;
152
153 // If the identifier is reserved (not invalid) but is in a system header,
154 // we do not diagnose (because we expect system headers to use reserved
155 // identifiers).
156 if (Reason == Reserved && S.getSourceManager().isInSystemHeader(Loc))
157 Reason = Valid;
158
159 if (Reason != Valid) {
160 S.Diag(Loc, diag::err_invalid_module_name) << II << (int)Reason;
161 return true;
162 }
163 return false;
164}
165
169 ModuleIdPath Partition, ModuleImportState &ImportState) {
170 assert(getLangOpts().CPlusPlusModules &&
171 "should only have module decl in standard C++ modules");
172
173 bool IsFirstDecl = ImportState == ModuleImportState::FirstDecl;
174 bool SeenGMF = ImportState == ModuleImportState::GlobalFragment;
175 // If any of the steps here fail, we count that as invalidating C++20
176 // module state;
178
179 bool IsPartition = !Partition.empty();
180 if (IsPartition)
181 switch (MDK) {
184 break;
187 break;
188 default:
189 llvm_unreachable("how did we get a partition type set?");
190 }
191
192 // A (non-partition) module implementation unit requires that we are not
193 // compiling a module of any kind. A partition implementation emits an
194 // interface (and the AST for the implementation), which will subsequently
195 // be consumed to emit a binary.
196 // A module interface unit requires that we are not compiling a module map.
197 switch (getLangOpts().getCompilingModule()) {
199 // It's OK to compile a module interface as a normal translation unit.
200 break;
201
204 break;
205
206 // We were asked to compile a module interface unit but this is a module
207 // implementation unit.
208 Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch)
209 << FixItHint::CreateInsertion(ModuleLoc, "export ");
211 break;
212
214 Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
215 return nullptr;
216
218 Diag(ModuleLoc, diag::err_module_decl_in_header_unit);
219 return nullptr;
220 }
221
222 assert(ModuleScopes.size() <= 1 && "expected to be at global module scope");
223
224 // FIXME: Most of this work should be done by the preprocessor rather than
225 // here, in order to support macro import.
226
227 // Only one module-declaration is permitted per source file.
228 if (isCurrentModulePurview()) {
229 Diag(ModuleLoc, diag::err_module_redeclaration);
230 Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
231 diag::note_prev_module_declaration);
232 return nullptr;
233 }
234
235 assert((!getLangOpts().CPlusPlusModules ||
236 SeenGMF == (bool)this->TheGlobalModuleFragment) &&
237 "mismatched global module state");
238
239 // In C++20, the module-declaration must be the first declaration if there
240 // is no global module fragment.
241 if (getLangOpts().CPlusPlusModules && !IsFirstDecl && !SeenGMF) {
242 Diag(ModuleLoc, diag::err_module_decl_not_at_start);
243 SourceLocation BeginLoc =
244 ModuleScopes.empty()
246 : ModuleScopes.back().BeginLoc;
247 if (BeginLoc.isValid()) {
248 Diag(BeginLoc, diag::note_global_module_introducer_missing)
249 << FixItHint::CreateInsertion(BeginLoc, "module;\n");
250 }
251 }
252
253 // C++2b [module.unit]p1: ... The identifiers module and import shall not
254 // appear as identifiers in a module-name or module-partition. All
255 // module-names either beginning with an identifier consisting of std
256 // followed by zero or more digits or containing a reserved identifier
257 // ([lex.name]) are reserved and shall not be specified in a
258 // module-declaration; no diagnostic is required.
259
260 // Test the first part of the path to see if it's std[0-9]+ but allow the
261 // name in a system header.
262 StringRef FirstComponentName = Path[0].first->getName();
263 if (!getSourceManager().isInSystemHeader(Path[0].second) &&
264 (FirstComponentName == "std" ||
265 (FirstComponentName.startswith("std") &&
266 llvm::all_of(FirstComponentName.drop_front(3), &llvm::isDigit)))) {
267 Diag(Path[0].second, diag::err_invalid_module_name)
268 << Path[0].first << /*reserved*/ 1;
269 return nullptr;
270 }
271
272 // Then test all of the components in the path to see if any of them are
273 // using another kind of reserved or invalid identifier.
274 for (auto Part : Path) {
275 if (DiagReservedModuleName(*this, Part.first, Part.second))
276 return nullptr;
277 }
278
279 // Flatten the dots in a module name. Unlike Clang's hierarchical module map
280 // modules, the dots here are just another character that can appear in a
281 // module name.
282 std::string ModuleName = stringFromPath(Path);
283 if (IsPartition) {
284 ModuleName += ":";
285 ModuleName += stringFromPath(Partition);
286 }
287 // If a module name was explicitly specified on the command line, it must be
288 // correct.
289 if (!getLangOpts().CurrentModule.empty() &&
290 getLangOpts().CurrentModule != ModuleName) {
291 Diag(Path.front().second, diag::err_current_module_name_mismatch)
292 << SourceRange(Path.front().second, IsPartition
293 ? Partition.back().second
294 : Path.back().second)
296 return nullptr;
297 }
298 const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
299
300 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
301 Module *Mod; // The module we are creating.
302 Module *Interface = nullptr; // The interface for an implementation.
303 switch (MDK) {
306 // We can't have parsed or imported a definition of this module or parsed a
307 // module map defining it already.
308 if (auto *M = Map.findModule(ModuleName)) {
309 Diag(Path[0].second, diag::err_module_redefinition) << ModuleName;
310 if (M->DefinitionLoc.isValid())
311 Diag(M->DefinitionLoc, diag::note_prev_module_definition);
312 else if (OptionalFileEntryRef FE = M->getASTFile())
313 Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
314 << FE->getName();
315 Mod = M;
316 break;
317 }
318
319 // Create a Module for the module that we're defining.
320 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
323 assert(Mod && "module creation should not fail");
324 break;
325 }
326
328 // C++20 A module-declaration that contains neither an export-
329 // keyword nor a module-partition implicitly imports the primary
330 // module interface unit of the module as if by a module-import-
331 // declaration.
332 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc(
333 PP.getIdentifierInfo(ModuleName), Path[0].second);
334
335 // The module loader will assume we're trying to import the module that
336 // we're building if `LangOpts.CurrentModule` equals to 'ModuleName'.
337 // Change the value for `LangOpts.CurrentModule` temporarily to make the
338 // module loader work properly.
339 const_cast<LangOptions &>(getLangOpts()).CurrentModule = "";
340 Interface = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
342 /*IsInclusionDirective=*/false);
343 const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
344
345 if (!Interface) {
346 Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
347 // Create an empty module interface unit for error recovery.
348 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
349 } else {
350 Mod = Map.createModuleForImplementationUnit(ModuleLoc, ModuleName);
351 }
352 } break;
353
355 // Create an interface, but note that it is an implementation
356 // unit.
357 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
359 break;
360 }
361
362 if (!this->TheGlobalModuleFragment) {
363 ModuleScopes.push_back({});
364 if (getLangOpts().ModulesLocalVisibility)
365 ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
366 } else {
367 // We're done with the global module fragment now.
369 }
370
371 // Switch from the global module fragment (if any) to the named module.
372 ModuleScopes.back().BeginLoc = StartLoc;
373 ModuleScopes.back().Module = Mod;
374 ModuleScopes.back().ModuleInterface = MDK != ModuleDeclKind::Implementation;
375 VisibleModules.setVisible(Mod, ModuleLoc);
376
377 // From now on, we have an owning module for all declarations we see.
378 // In C++20 modules, those declaration would be reachable when imported
379 // unless explicitily exported.
380 // Otherwise, those declarations are module-private unless explicitly
381 // exported.
382 auto *TU = Context.getTranslationUnitDecl();
384 TU->setLocalOwningModule(Mod);
385
386 // We are in the module purview, but before any other (non import)
387 // statements, so imports are allowed.
389
391
392 // We already potentially made an implicit import (in the case of a module
393 // implementation unit importing its interface). Make this module visible
394 // and return the import decl to be added to the current TU.
395 if (Interface) {
396
397 VisibleModules.setVisible(Interface, ModuleLoc);
398
399 // Make the import decl for the interface in the impl module.
400 ImportDecl *Import = ImportDecl::Create(Context, CurContext, ModuleLoc,
401 Interface, Path[0].second);
402 CurContext->addDecl(Import);
403
404 // Sequence initialization of the imported module before that of the current
405 // module, if any.
406 Context.addModuleInitializer(ModuleScopes.back().Module, Import);
407 Mod->Imports.insert(Interface); // As if we imported it.
408 // Also save this as a shortcut to checking for decls in the interface
409 ThePrimaryInterface = Interface;
410 // If we made an implicit import of the module interface, then return the
411 // imported module decl.
412 return ConvertDeclToDeclGroup(Import);
413 }
414
415 return nullptr;
416}
417
420 SourceLocation PrivateLoc) {
421 // C++20 [basic.link]/2:
422 // A private-module-fragment shall appear only in a primary module
423 // interface unit.
424 switch (ModuleScopes.empty() ? Module::ExplicitGlobalModuleFragment
425 : ModuleScopes.back().Module->Kind) {
432 Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
433 return nullptr;
434
436 Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
437 Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
438 return nullptr;
439
441 Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface);
442 Diag(ModuleScopes.back().BeginLoc,
443 diag::note_not_module_interface_add_export)
444 << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
445 return nullptr;
446
448 break;
449 }
450
451 // FIXME: Check that this translation unit does not import any partitions;
452 // such imports would violate [basic.link]/2's "shall be the only module unit"
453 // restriction.
454
455 // We've finished the public fragment of the translation unit.
457
458 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
459 Module *PrivateModuleFragment =
460 Map.createPrivateModuleFragmentForInterfaceUnit(
461 ModuleScopes.back().Module, PrivateLoc);
462 assert(PrivateModuleFragment && "module creation should not fail");
463
464 // Enter the scope of the private module fragment.
465 ModuleScopes.push_back({});
466 ModuleScopes.back().BeginLoc = ModuleLoc;
467 ModuleScopes.back().Module = PrivateModuleFragment;
468 ModuleScopes.back().ModuleInterface = true;
469 VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
470
471 // All declarations created from now on are scoped to the private module
472 // fragment (and are neither visible nor reachable in importers of the module
473 // interface).
474 auto *TU = Context.getTranslationUnitDecl();
476 TU->setLocalOwningModule(PrivateModuleFragment);
477
478 // FIXME: Consider creating an explicit representation of this declaration.
479 return nullptr;
480}
481
483 SourceLocation ExportLoc,
484 SourceLocation ImportLoc, ModuleIdPath Path,
485 bool IsPartition) {
486 assert((!IsPartition || getLangOpts().CPlusPlusModules) &&
487 "partition seen in non-C++20 code?");
488
489 // For a C++20 module name, flatten into a single identifier with the source
490 // location of the first component.
491 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
492
493 std::string ModuleName;
494 if (IsPartition) {
495 // We already checked that we are in a module purview in the parser.
496 assert(!ModuleScopes.empty() && "in a module purview, but no module?");
497 Module *NamedMod = ModuleScopes.back().Module;
498 // If we are importing into a partition, find the owning named module,
499 // otherwise, the name of the importing named module.
500 ModuleName = NamedMod->getPrimaryModuleInterfaceName().str();
501 ModuleName += ":";
502 ModuleName += stringFromPath(Path);
503 ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
504 Path = ModuleIdPath(ModuleNameLoc);
505 } else if (getLangOpts().CPlusPlusModules) {
506 ModuleName = stringFromPath(Path);
507 ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
508 Path = ModuleIdPath(ModuleNameLoc);
509 }
510
511 // Diagnose self-import before attempting a load.
512 // [module.import]/9
513 // A module implementation unit of a module M that is not a module partition
514 // shall not contain a module-import-declaration nominating M.
515 // (for an implementation, the module interface is imported implicitly,
516 // but that's handled in the module decl code).
517
518 if (getLangOpts().CPlusPlusModules && isCurrentModulePurview() &&
519 getCurrentModule()->Name == ModuleName) {
520 Diag(ImportLoc, diag::err_module_self_import_cxx20)
521 << ModuleName << !ModuleScopes.back().ModuleInterface;
522 return true;
523 }
524
526 ImportLoc, Path, Module::AllVisible, /*IsInclusionDirective=*/false);
527 if (!Mod)
528 return true;
529
530 return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
531}
532
533/// Determine whether \p D is lexically within an export-declaration.
534static const ExportDecl *getEnclosingExportDecl(const Decl *D) {
535 for (auto *DC = D->getLexicalDeclContext(); DC; DC = DC->getLexicalParent())
536 if (auto *ED = dyn_cast<ExportDecl>(DC))
537 return ED;
538 return nullptr;
539}
540
542 SourceLocation ExportLoc,
543 SourceLocation ImportLoc, Module *Mod,
544 ModuleIdPath Path) {
545 VisibleModules.setVisible(Mod, ImportLoc);
546
547 checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
548
549 // FIXME: we should support importing a submodule within a different submodule
550 // of the same top-level module. Until we do, make it an error rather than
551 // silently ignoring the import.
552 // FIXME: Should we warn on a redundant import of the current module?
553 if (Mod->isForBuilding(getLangOpts())) {
554 Diag(ImportLoc, getLangOpts().isCompilingModule()
555 ? diag::err_module_self_import
556 : diag::err_module_import_in_implementation)
558 }
559
560 SmallVector<SourceLocation, 2> IdentifierLocs;
561
562 if (Path.empty()) {
563 // If this was a header import, pad out with dummy locations.
564 // FIXME: Pass in and use the location of the header-name token in this
565 // case.
566 for (Module *ModCheck = Mod; ModCheck; ModCheck = ModCheck->Parent)
567 IdentifierLocs.push_back(SourceLocation());
568 } else if (getLangOpts().CPlusPlusModules && !Mod->Parent) {
569 // A single identifier for the whole name.
570 IdentifierLocs.push_back(Path[0].second);
571 } else {
572 Module *ModCheck = Mod;
573 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
574 // If we've run out of module parents, just drop the remaining
575 // identifiers. We need the length to be consistent.
576 if (!ModCheck)
577 break;
578 ModCheck = ModCheck->Parent;
579
580 IdentifierLocs.push_back(Path[I].second);
581 }
582 }
583
584 ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc,
585 Mod, IdentifierLocs);
586 CurContext->addDecl(Import);
587
588 // Sequence initialization of the imported module before that of the current
589 // module, if any.
590 if (!ModuleScopes.empty())
591 Context.addModuleInitializer(ModuleScopes.back().Module, Import);
592
593 // A module (partition) implementation unit shall not be exported.
594 if (getLangOpts().CPlusPlusModules && ExportLoc.isValid() &&
596 Diag(ExportLoc, diag::err_export_partition_impl)
597 << SourceRange(ExportLoc, Path.back().second);
598 } else if (!ModuleScopes.empty() &&
599 (ModuleScopes.back().ModuleInterface ||
600 (getLangOpts().CPlusPlusModules &&
601 ModuleScopes.back().Module->isGlobalModule()))) {
602 // Re-export the module if the imported module is exported.
603 // Note that we don't need to add re-exported module to Imports field
604 // since `Exports` implies the module is imported already.
605 if (ExportLoc.isValid() || getEnclosingExportDecl(Import))
606 getCurrentModule()->Exports.emplace_back(Mod, false);
607 else
608 getCurrentModule()->Imports.insert(Mod);
609 } else if (ExportLoc.isValid()) {
610 // [module.interface]p1:
611 // An export-declaration shall inhabit a namespace scope and appear in the
612 // purview of a module interface unit.
613 Diag(ExportLoc, diag::err_export_not_in_module_interface);
614 }
615
616 return Import;
617}
618
620 checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
621 BuildModuleInclude(DirectiveLoc, Mod);
622}
623
625 // Determine whether we're in the #include buffer for a module. The #includes
626 // in that buffer do not qualify as module imports; they're just an
627 // implementation detail of us building the module.
628 //
629 // FIXME: Should we even get ActOnModuleInclude calls for those?
630 bool IsInModuleIncludes =
631 TUKind == TU_Module &&
633
634 bool ShouldAddImport = !IsInModuleIncludes;
635
636 // If this module import was due to an inclusion directive, create an
637 // implicit import declaration to capture it in the AST.
638 if (ShouldAddImport) {
641 DirectiveLoc, Mod,
642 DirectiveLoc);
643 if (!ModuleScopes.empty())
644 Context.addModuleInitializer(ModuleScopes.back().Module, ImportD);
645 TU->addDecl(ImportD);
647 }
648
650 VisibleModules.setVisible(Mod, DirectiveLoc);
651
652 if (getLangOpts().isCompilingModule()) {
654 getLangOpts().CurrentModule, DirectiveLoc, false, false);
655 (void)ThisModule;
656 assert(ThisModule && "was expecting a module if building one");
657 }
658}
659
661 checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
662
663 ModuleScopes.push_back({});
664 ModuleScopes.back().Module = Mod;
665 if (getLangOpts().ModulesLocalVisibility)
666 ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
667
668 VisibleModules.setVisible(Mod, DirectiveLoc);
669
670 // The enclosing context is now part of this module.
671 // FIXME: Consider creating a child DeclContext to hold the entities
672 // lexically within the module.
673 if (getLangOpts().trackLocalOwningModule()) {
674 for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
675 cast<Decl>(DC)->setModuleOwnershipKind(
676 getLangOpts().ModulesLocalVisibility
679 cast<Decl>(DC)->setLocalOwningModule(Mod);
680 }
681 }
682}
683
685 if (getLangOpts().ModulesLocalVisibility) {
686 VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules);
687 // Leaving a module hides namespace names, so our visible namespace cache
688 // is now out of date.
689 VisibleNamespaceCache.clear();
690 }
691
692 assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod &&
693 "left the wrong module scope");
694 ModuleScopes.pop_back();
695
696 // We got to the end of processing a local module. Create an
697 // ImportDecl as we would for an imported module.
699 SourceLocation DirectiveLoc;
700 if (EomLoc == getSourceManager().getLocForEndOfFile(File)) {
701 // We reached the end of a #included module header. Use the #include loc.
702 assert(File != getSourceManager().getMainFileID() &&
703 "end of submodule in main source file");
704 DirectiveLoc = getSourceManager().getIncludeLoc(File);
705 } else {
706 // We reached an EOM pragma. Use the pragma location.
707 DirectiveLoc = EomLoc;
708 }
709 BuildModuleInclude(DirectiveLoc, Mod);
710
711 // Any further declarations are in whatever module we returned to.
712 if (getLangOpts().trackLocalOwningModule()) {
713 // The parser guarantees that this is the same context that we entered
714 // the module within.
715 for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
716 cast<Decl>(DC)->setLocalOwningModule(getCurrentModule());
717 if (!getCurrentModule())
718 cast<Decl>(DC)->setModuleOwnershipKind(
720 }
721 }
722}
723
725 Module *Mod) {
726 // Bail if we're not allowed to implicitly import a module here.
727 if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery ||
728 VisibleModules.isVisible(Mod))
729 return;
730
731 // Create the implicit import declaration.
734 Loc, Mod, Loc);
735 TU->addDecl(ImportD);
737
738 // Make the module visible.
740 VisibleModules.setVisible(Mod, Loc);
741}
742
743/// We have parsed the start of an export declaration, including the '{'
744/// (if present).
746 SourceLocation LBraceLoc) {
748
749 // Set this temporarily so we know the export-declaration was braced.
750 D->setRBraceLoc(LBraceLoc);
751
753 PushDeclContext(S, D);
754
755 // C++2a [module.interface]p1:
756 // An export-declaration shall appear only [...] in the purview of a module
757 // interface unit. An export-declaration shall not appear directly or
758 // indirectly within [...] a private-module-fragment.
759 if (!isCurrentModulePurview()) {
760 Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
761 D->setInvalidDecl();
762 return D;
763 } else if (!ModuleScopes.back().ModuleInterface) {
764 Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
765 Diag(ModuleScopes.back().BeginLoc,
766 diag::note_not_module_interface_add_export)
767 << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
768 D->setInvalidDecl();
769 return D;
770 } else if (ModuleScopes.back().Module->Kind ==
772 Diag(ExportLoc, diag::err_export_in_private_module_fragment);
773 Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
774 D->setInvalidDecl();
775 return D;
776 }
777
778 for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) {
779 if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
780 // An export-declaration shall not appear directly or indirectly within
781 // an unnamed namespace [...]
782 if (ND->isAnonymousNamespace()) {
783 Diag(ExportLoc, diag::err_export_within_anonymous_namespace);
784 Diag(ND->getLocation(), diag::note_anonymous_namespace);
785 // Don't diagnose internal-linkage declarations in this region.
786 D->setInvalidDecl();
787 return D;
788 }
789
790 // A declaration is exported if it is [...] a namespace-definition
791 // that contains an exported declaration.
792 //
793 // Defer exporting the namespace until after we leave it, in order to
794 // avoid marking all subsequent declarations in the namespace as exported.
795 if (!DeferredExportedNamespaces.insert(ND).second)
796 break;
797 }
798 }
799
800 // [...] its declaration or declaration-seq shall not contain an
801 // export-declaration.
802 if (auto *ED = getEnclosingExportDecl(D)) {
803 Diag(ExportLoc, diag::err_export_within_export);
804 if (ED->hasBraces())
805 Diag(ED->getLocation(), diag::note_export);
806 D->setInvalidDecl();
807 return D;
808 }
809
811 return D;
812}
813
814static bool checkExportedDeclContext(Sema &S, DeclContext *DC,
815 SourceLocation BlockStart);
816
817namespace {
818enum class UnnamedDeclKind {
819 Empty,
820 StaticAssert,
821 Asm,
822 UsingDirective,
823 Namespace,
824 Context
825};
826}
827
828static std::optional<UnnamedDeclKind> getUnnamedDeclKind(Decl *D) {
829 if (isa<EmptyDecl>(D))
830 return UnnamedDeclKind::Empty;
831 if (isa<StaticAssertDecl>(D))
832 return UnnamedDeclKind::StaticAssert;
833 if (isa<FileScopeAsmDecl>(D))
834 return UnnamedDeclKind::Asm;
835 if (isa<UsingDirectiveDecl>(D))
836 return UnnamedDeclKind::UsingDirective;
837 // Everything else either introduces one or more names or is ill-formed.
838 return std::nullopt;
839}
840
841unsigned getUnnamedDeclDiag(UnnamedDeclKind UDK, bool InBlock) {
842 switch (UDK) {
843 case UnnamedDeclKind::Empty:
844 case UnnamedDeclKind::StaticAssert:
845 // Allow empty-declarations and static_asserts in an export block as an
846 // extension.
847 return InBlock ? diag::ext_export_no_name_block : diag::err_export_no_name;
848
849 case UnnamedDeclKind::UsingDirective:
850 // Allow exporting using-directives as an extension.
851 return diag::ext_export_using_directive;
852
853 case UnnamedDeclKind::Namespace:
854 // Anonymous namespace with no content.
855 return diag::introduces_no_names;
856
857 case UnnamedDeclKind::Context:
858 // Allow exporting DeclContexts that transitively contain no declarations
859 // as an extension.
860 return diag::ext_export_no_names;
861
862 case UnnamedDeclKind::Asm:
863 return diag::err_export_no_name;
864 }
865 llvm_unreachable("unknown kind");
866}
867
868static void diagExportedUnnamedDecl(Sema &S, UnnamedDeclKind UDK, Decl *D,
869 SourceLocation BlockStart) {
870 S.Diag(D->getLocation(), getUnnamedDeclDiag(UDK, BlockStart.isValid()))
871 << (unsigned)UDK;
872 if (BlockStart.isValid())
873 S.Diag(BlockStart, diag::note_export);
874}
875
876/// Check that it's valid to export \p D.
877static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
878 // C++2a [module.interface]p3:
879 // An exported declaration shall declare at least one name
880 if (auto UDK = getUnnamedDeclKind(D))
881 diagExportedUnnamedDecl(S, *UDK, D, BlockStart);
882
883 // [...] shall not declare a name with internal linkage.
884 bool HasName = false;
885 if (auto *ND = dyn_cast<NamedDecl>(D)) {
886 // Don't diagnose anonymous union objects; we'll diagnose their members
887 // instead.
888 HasName = (bool)ND->getDeclName();
889 if (HasName && ND->getFormalLinkage() == InternalLinkage) {
890 S.Diag(ND->getLocation(), diag::err_export_internal) << ND;
891 if (BlockStart.isValid())
892 S.Diag(BlockStart, diag::note_export);
893 }
894 }
895
896 // C++2a [module.interface]p5:
897 // all entities to which all of the using-declarators ultimately refer
898 // shall have been introduced with a name having external linkage
899 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
900 NamedDecl *Target = USD->getUnderlyingDecl();
901 Linkage Lk = Target->getFormalLinkage();
902 if (Lk == InternalLinkage || Lk == ModuleLinkage) {
903 S.Diag(USD->getLocation(), diag::err_export_using_internal)
904 << (Lk == InternalLinkage ? 0 : 1) << Target;
905 S.Diag(Target->getLocation(), diag::note_using_decl_target);
906 if (BlockStart.isValid())
907 S.Diag(BlockStart, diag::note_export);
908 }
909 }
910
911 // Recurse into namespace-scope DeclContexts. (Only namespace-scope
912 // declarations are exported.).
913 if (auto *DC = dyn_cast<DeclContext>(D)) {
914 if (isa<NamespaceDecl>(D) && DC->decls().empty()) {
915 if (!HasName)
916 // We don't allow an empty anonymous namespace (we don't allow decls
917 // in them either, but that's handled in the recursion).
918 diagExportedUnnamedDecl(S, UnnamedDeclKind::Namespace, D, BlockStart);
919 // We allow an empty named namespace decl.
920 } else if (DC->getRedeclContext()->isFileContext() && !isa<EnumDecl>(D))
921 return checkExportedDeclContext(S, DC, BlockStart);
922 }
923 return false;
924}
925
926/// Check that it's valid to export all the declarations in \p DC.
928 SourceLocation BlockStart) {
929 bool AllUnnamed = true;
930 for (auto *D : DC->decls())
931 AllUnnamed &= checkExportedDecl(S, D, BlockStart);
932 return AllUnnamed;
933}
934
935/// Complete the definition of an export declaration.
937 auto *ED = cast<ExportDecl>(D);
938 if (RBraceLoc.isValid())
939 ED->setRBraceLoc(RBraceLoc);
940
942
943 if (!D->isInvalidDecl()) {
944 SourceLocation BlockStart =
945 ED->hasBraces() ? ED->getBeginLoc() : SourceLocation();
946 for (auto *Child : ED->decls()) {
947 if (checkExportedDecl(*this, Child, BlockStart)) {
948 // If a top-level child is a linkage-spec declaration, it might contain
949 // no declarations (transitively), in which case it's ill-formed.
950 diagExportedUnnamedDecl(*this, UnnamedDeclKind::Context, Child,
951 BlockStart);
952 }
953 if (auto *FD = dyn_cast<FunctionDecl>(Child)) {
954 // [dcl.inline]/7
955 // If an inline function or variable that is attached to a named module
956 // is declared in a definition domain, it shall be defined in that
957 // domain.
958 // So, if the current declaration does not have a definition, we must
959 // check at the end of the TU (or when the PMF starts) to see that we
960 // have a definition at that point.
961 if (FD->isInlineSpecified() && !FD->isDefined())
962 PendingInlineFuncDecls.insert(FD);
963 }
964 }
965 }
966
967 return D;
968}
969
970Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc) {
971 // We shouldn't create new global module fragment if there is already
972 // one.
973 if (!TheGlobalModuleFragment) {
975 TheGlobalModuleFragment = Map.createGlobalModuleFragmentForModuleUnit(
976 BeginLoc, getCurrentModule());
977 }
978
979 assert(TheGlobalModuleFragment && "module creation should not fail");
980
981 // Enter the scope of the global module.
982 ModuleScopes.push_back({BeginLoc, TheGlobalModuleFragment,
983 /*ModuleInterface=*/false,
984 /*OuterVisibleModules=*/{}});
985 VisibleModules.setVisible(TheGlobalModuleFragment, BeginLoc);
986
987 return TheGlobalModuleFragment;
988}
989
990void Sema::PopGlobalModuleFragment() {
991 assert(!ModuleScopes.empty() &&
992 getCurrentModule()->isExplicitGlobalModule() &&
993 "left the wrong module scope, which is not global module fragment");
994 ModuleScopes.pop_back();
995}
996
997Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc,
998 bool IsExported) {
999 Module **M = IsExported ? &TheExportedImplicitGlobalModuleFragment
1000 : &TheImplicitGlobalModuleFragment;
1001 if (!*M) {
1004 BeginLoc, IsExported, getCurrentModule());
1005 }
1006 assert(*M && "module creation should not fail");
1007
1008 // Enter the scope of the global module.
1009 ModuleScopes.push_back({BeginLoc, *M,
1010 /*ModuleInterface=*/false,
1011 /*OuterVisibleModules=*/{}});
1012 VisibleModules.setVisible(*M, BeginLoc);
1013 return *M;
1014}
1015
1016void Sema::PopImplicitGlobalModuleFragment() {
1017 assert(!ModuleScopes.empty() &&
1018 getCurrentModule()->isImplicitGlobalModule() &&
1019 "left the wrong module scope, which is not global module fragment");
1020 ModuleScopes.pop_back();
1021}
1022
1024 assert(M);
1025
1026 Module *CurrentModuleUnit = getCurrentModule();
1027
1028 // If we are not in a module currently, M must not be the module unit of
1029 // current TU.
1030 if (!CurrentModuleUnit)
1031 return false;
1032
1033 return M->isSubModuleOf(CurrentModuleUnit->getTopLevelModule());
1034}
Defines the clang::Preprocessor interface.
static std::optional< UnnamedDeclKind > getUnnamedDeclKind(Decl *D)
Definition: SemaModule.cpp:828
static bool DiagReservedModuleName(Sema &S, const IdentifierInfo *II, SourceLocation Loc)
Tests whether the given identifier is reserved as a module name and diagnoses if it is.
Definition: SemaModule.cpp:139
static const ExportDecl * getEnclosingExportDecl(const Decl *D)
Determine whether D is lexically within an export-declaration.
Definition: SemaModule.cpp:534
static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart)
Check that it's valid to export D.
Definition: SemaModule.cpp:877
static std::string stringFromPath(ModuleIdPath Path)
Definition: SemaModule.cpp:62
static void checkModuleImportContext(Sema &S, Module *M, SourceLocation ImportLoc, DeclContext *DC, bool FromInclude=false)
Definition: SemaModule.cpp:23
unsigned getUnnamedDeclDiag(UnnamedDeclKind UDK, bool InBlock)
Definition: SemaModule.cpp:841
static void diagExportedUnnamedDecl(Sema &S, UnnamedDeclKind UDK, Decl *D, SourceLocation BlockStart)
Definition: SemaModule.cpp:868
static bool checkExportedDeclContext(Sema &S, DeclContext *DC, SourceLocation BlockStart)
Check that it's valid to export all the declarations in DC.
Definition: SemaModule.cpp:927
__device__ int
virtual void HandleImplicitImportDecl(ImportDecl *D)
Handle an ImportDecl that was implicitly created due to an inclusion directive.
Definition: ASTConsumer.cpp:28
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1060
void addModuleInitializer(Module *M, Decl *Init)
Add a declaration to the list of declarations that are initialized for a module.
void setNamedModuleForCodeGen(Module *M)
Set the (C++20) module we are building.
Definition: ASTContext.h:1055
ActionResult - This structure is used while parsing/acting on expressions, stmts, etc.
Definition: Ownership.h:152
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1393
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1933
DeclContext * getLexicalParent()
getLexicalParent - Returns the containing lexical DeclContext.
Definition: DeclBase.h:1949
void addDecl(Decl *D)
Add the declaration D into this context.
Definition: DeclBase.cpp:1619
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:2188
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
void setInvalidDecl(bool Invalid=true)
setInvalidDecl - Indicates the Decl had a semantic error.
Definition: DeclBase.cpp:132
bool isInvalidDecl() const
Definition: DeclBase.h:571
SourceLocation getLocation() const
Definition: DeclBase.h:432
@ VisibleWhenImported
This declaration has an owning module, and is visible when that module is imported.
@ Unowned
This declaration is not owned by a module.
@ ReachableWhenImported
This declaration has an owning module, and is visible to lookups that occurs within that module.
@ ModulePrivate
This declaration has an owning module, but is only visible to lookups that occur within that module.
@ Visible
This declaration has an owning module, but is globally visible (typically because its owning module i...
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
Definition: DeclBase.h:883
void setModuleOwnershipKind(ModuleOwnershipKind MOK)
Set whether this declaration is hidden from name lookup.
Definition: DeclBase.h:846
Represents a standard C++ module export declaration.
Definition: Decl.h:4718
static ExportDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation ExportLoc)
Definition: Decl.cpp:5431
void setRBraceLoc(SourceLocation L)
Definition: Decl.h:4738
StringRef getName() const
Definition: FileEntry.h:384
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Get a FileEntryRef if it exists, without doing anything on error.
Definition: FileManager.h:234
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:97
Module * lookupModule(StringRef ModuleName, SourceLocation ImportLoc=SourceLocation(), bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:791
One of these records is kept for each identifier that is lexed.
ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const
Determine whether this is a name reserved for the implementation (C99 7.1.3, C++ [lib....
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition: Decl.h:4639
static ImportDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, Module *Imported, ArrayRef< SourceLocation > IdentifierLocs)
Create a new module import declaration.
Definition: Decl.cpp:5385
static ImportDecl * CreateImplicit(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, Module *Imported, SourceLocation EndLoc)
Create a new module import declaration for an implicitly-generated import.
Definition: Decl.cpp:5393
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:82
@ CMK_HeaderUnit
Compiling a module header unit.
Definition: LangOptions.h:115
@ CMK_ModuleMap
Compiling a module from a module map.
Definition: LangOptions.h:112
@ CMK_ModuleInterface
Compiling a C++ modules interface unit.
Definition: LangOptions.h:118
@ CMK_None
Not compiling a module interface at all.
Definition: LangOptions.h:109
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:437
virtual ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective)=0
Attempt to load the given module.
virtual void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, SourceLocation ImportLoc)=0
Make the given module visible.
Module * createImplicitGlobalModuleFragmentForModuleUnit(SourceLocation Loc, bool IsExported, Module *Parent=nullptr)
Definition: ModuleMap.cpp:866
Module * createGlobalModuleFragmentForModuleUnit(SourceLocation Loc, Module *Parent=nullptr)
Create a global module fragment for a C++ module unit.
Definition: ModuleMap.cpp:854
Describes a module or submodule.
Definition: Module.h:98
SmallVector< ExportDecl, 2 > Exports
The set of export declarations.
Definition: Module.h:390
bool isForBuilding(const LangOptions &LangOpts) const
Determine whether this module can be built in this compilation.
Definition: Module.cpp:157
@ AllVisible
All of the names in this module are visible.
Definition: Module.h:366
Module * Parent
The parent of this module.
Definition: Module.h:147
ModuleKind Kind
The kind of this module.
Definition: Module.h:143
llvm::SmallSetVector< Module *, 2 > Imports
The set of modules imported by this module, and on which this module depends.
Definition: Module.h:377
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "C"...
Definition: Module.h:326
StringRef getPrimaryModuleInterfaceName() const
Get the primary module interface name from a partition.
Definition: Module.h:594
bool isSubModuleOf(const Module *Other) const
Check if this module is a (possibly transitive) submodule of Other.
Definition: Module.cpp:193
@ ModuleImplementationUnit
This is a C++20 module implementation unit.
Definition: Module.h:121
@ ModuleMapModule
This is a module that was defined by a module map and built out of header files.
Definition: Module.h:112
@ ImplicitGlobalModuleFragment
This is an implicit fragment of the global module which contains only language linkage declarations (...
Definition: Module.h:139
@ ModulePartitionInterface
This is a C++ 20 module partition interface.
Definition: Module.h:124
@ ModuleInterfaceUnit
This is a C++20 module interface unit.
Definition: Module.h:118
@ ModuleHeaderUnit
This is a C++ 20 header unit.
Definition: Module.h:115
@ ModulePartitionImplementation
This is a C++ 20 module partition implementation.
Definition: Module.h:127
@ PrivateModuleFragment
This is the private module fragment within some C++ module.
Definition: Module.h:134
@ ExplicitGlobalModuleFragment
This is the explicit Global Module Fragment of a modular TU.
Definition: Module.h:131
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition: Module.cpp:239
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition: Module.h:629
This represents a decl that may have a name.
Definition: Decl.h:247
Wrapper for void* pointer.
Definition: Ownership.h:50
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
HeaderSearch & getHeaderSearchInfo() const
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:356
void ActOnModuleBegin(SourceLocation DirectiveLoc, Module *Mod)
The parsed has entered a submodule.
Definition: SemaModule.cpp:660
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition: Sema.cpp:1884
const TranslationUnitKind TUKind
The kind of translation unit we are processing.
Definition: Sema.h:1459
ModuleDeclKind
Definition: Sema.h:3147
@ PartitionImplementation
'module X:Y;'
@ Interface
'export module X;'
@ PartitionInterface
'export module X:Y;'
llvm::DenseMap< NamedDecl *, NamedDecl * > VisibleNamespaceCache
Map from the most recent declaration of a namespace to the most recent visible declaration of that na...
Definition: Sema.h:9412
DeclGroupPtrTy ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc, ModuleDeclKind MDK, ModuleIdPath Path, ModuleIdPath Partition, ModuleImportState &ImportState)
The parser has processed a module-declaration that begins the definition of a module interface or imp...
Definition: SemaModule.cpp:167
ASTContext & Context
Definition: Sema.h:407
DeclResult ActOnModuleImport(SourceLocation StartLoc, SourceLocation ExportLoc, SourceLocation ImportLoc, ModuleIdPath Path, bool IsPartition=false)
The parser has processed a module import declaration.
Definition: SemaModule.cpp:482
DeclGroupPtrTy ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType=nullptr)
Definition: SemaDecl.cpp:59
ASTContext & getASTContext() const
Definition: Sema.h:1652
Decl * ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc, SourceLocation LBraceLoc)
We have parsed the start of an export declaration, including the '{' (if present).
Definition: SemaModule.cpp:745
const LangOptions & getLangOpts() const
Definition: Sema.h:1645
bool isModuleUnitOfCurrentTU(const Module *M) const
Preprocessor & PP
Definition: Sema.h:406
void ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind)
Definition: Sema.cpp:1036
DeclGroupPtrTy ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc)
The parser has processed a global-module-fragment declaration that begins the definition of the globa...
Definition: SemaModule.cpp:76
std::optional< sema::TemplateDeductionInfo * > isSFINAEContext() const
Determines whether we are currently in a context where template argument substitution failures are no...
Module * getCurrentModule() const
Get the module unit whose scope we are currently within.
Definition: Sema.h:2332
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition: Sema.h:419
DeclGroupPtrTy ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc, SourceLocation PrivateLoc)
The parser has processed a private-module-fragment declaration that begins the definition of the priv...
Definition: SemaModule.cpp:419
SourceManager & getSourceManager() const
Definition: Sema.h:1650
void BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod)
Definition: SemaModule.cpp:624
bool isModuleVisible(const Module *M, bool ModulePrivate=false)
void ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod)
The parser has processed a module import translated from a #include or similar preprocessing directiv...
Definition: SemaModule.cpp:619
@ Global
The global module fragment, between 'module;' and a module-declaration.
Definition: Sema.h:1928
@ Normal
A normal translation unit fragment.
Definition: Sema.h:1932
ASTConsumer & Consumer
Definition: Sema.h:408
ModuleImportState
An enumeration to represent the transition of states in parsing module fragments and imports.
Definition: Sema.h:3157
@ FirstDecl
Parsing the first decl in a TU.
@ GlobalFragment
after 'module;' but before 'module X;'
@ NotACXX20Module
Not a C++20 TU, or an invalid state was found.
@ ImportAllowed
after 'module X;' but before any non-import decl.
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with the preprocessor.
Definition: Sema.cpp:61
void PushDeclContext(Scope *S, DeclContext *DC)
Set the current declaration context until it gets popped.
Definition: SemaDecl.cpp:1344
SourceManager & SourceMgr
Definition: Sema.h:410
void PopDeclContext()
Definition: SemaDecl.cpp:1351
void ActOnModuleEnd(SourceLocation DirectiveLoc, Module *Mod)
The parser has left a submodule.
Definition: SemaModule.cpp:684
Decl * ActOnFinishExportDecl(Scope *S, Decl *ExportDecl, SourceLocation RBraceLoc)
Complete the definition of an export declaration.
Definition: SemaModule.cpp:936
void createImplicitModuleImportForErrorRecovery(SourceLocation Loc, Module *Mod)
Create an implicit import of the given module at the given source location, for error recovery,...
Definition: SemaModule.cpp:724
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
OptionalFileEntryRef getFileEntryRefForID(FileID FID) const
Returns the FileEntryRef for the provided FileID.
FileManager & getFileManager() const
FileID getMainFileID() const
Returns the FileID of the main source file.
SourceLocation getIncludeLoc(FileID FID) const
Returns the include location if FID is a #include'd file otherwise it returns an invalid location.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file.
bool isWrittenInMainFile(SourceLocation Loc) const
Returns true if the spelling location for the given location is in the main file buffer.
A trivial tuple used to represent a source range.
The top declaration context.
Definition: Decl.h:82
SourceLocation getImportLoc(const Module *M) const
Get the location at which the import of a module was triggered.
Definition: Module.h:806
bool isVisible(const Module *M) const
Determine whether a module is visible.
Definition: Module.h:801
void setVisible(Module *M, SourceLocation Loc, VisibleCallback Vis=[](Module *) {}, ConflictCallback Cb=[](ArrayRef< Module * >, Module *, StringRef) {})
Make a specific module visible.
Definition: Module.cpp:654
ArrayRef< std::pair< IdentifierInfo *, SourceLocation > > ModuleIdPath
A sequence of identifier/location pairs used to describe a particular module or submodule,...
Definition: ModuleLoader.h:32
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:23
@ InternalLinkage
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:31
@ ModuleLinkage
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition: Linkage.h:50
@ Asm
Assembly: we accept this only so that we can preprocess it.
@ TU_Module
The translation unit is a module.
Definition: LangOptions.h:926
#define bool
Definition: stdbool.h:20
Information about a header directive as found in the module map file.
Definition: Module.h:242