19 using namespace clang;
24 bool FromInclude =
false) {
27 if (
auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) {
28 switch (LSD->getLanguage()) {
31 ExternCLoc = LSD->getBeginLoc();
39 while (isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC))
42 if (!isa<TranslationUnitDecl>(DC)) {
44 ? diag::ext_module_import_not_at_top_level_noop
45 : diag::err_module_import_not_at_top_level_fatal)
47 S.
Diag(cast<Decl>(DC)->getBeginLoc(),
48 diag::note_module_import_not_at_top_level)
51 S.
Diag(ImportLoc, diag::ext_module_import_in_extern_c)
53 S.
Diag(ExternCLoc, diag::note_extern_c_begins_here);
66 for (
auto &Piece : Path) {
69 Name += Piece.first->getName();
76 if (!ModuleScopes.empty() &&
80 assert(getLangOpts().CPlusPlusModules && getLangOpts().ModulesTS &&
81 "unexpectedly encountered multiple global module fragment decls");
82 ModuleScopes.back().BeginLoc = ModuleLoc;
89 PushGlobalModuleFragment(ModuleLoc,
false);
94 TU->setLocalOwningModule(GlobalModule);
100 void Sema::HandleStartOfHeaderUnit() {
101 assert(getLangOpts().CPlusPlusModules &&
102 "Header units are only valid for C++20 modules");
104 SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
106 StringRef HUName = getLangOpts().CurrentModule;
107 if (HUName.empty()) {
108 HUName = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())->getName();
109 const_cast<LangOptions &
>(getLangOpts()).CurrentModule = HUName.str();
115 auto F = SourceMgr.getFileManager().getFile(HUName);
120 F = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
121 assert(F &&
"failed to find the header unit source?");
122 Module::Header H{HUName.str(), HUName.str(), *F};
123 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
124 Module *Mod = Map.createHeaderUnit(StartOfTU, HUName, H);
125 assert(Mod &&
"module creation should not fail");
126 ModuleScopes.push_back({});
127 ModuleScopes.back().BeginLoc = StartOfTU;
128 ModuleScopes.back().Module = Mod;
129 ModuleScopes.back().ModuleInterface =
true;
130 ModuleScopes.back().IsPartition =
false;
131 VisibleModules.setVisible(Mod, StartOfTU);
137 TU->setLocalOwningModule(Mod);
144 assert((getLangOpts().ModulesTS || getLangOpts().CPlusPlusModules) &&
145 "should only have module decl in Modules TS or C++20");
147 bool IsFirstDecl = ImportState == ModuleImportState::FirstDecl;
148 bool SeenGMF = ImportState == ModuleImportState::GlobalFragment;
151 ImportState = ModuleImportState::NotACXX20Module;
153 bool IsPartition = !Partition.empty();
156 case ModuleDeclKind::Implementation:
157 MDK = ModuleDeclKind::PartitionImplementation;
159 case ModuleDeclKind::Interface:
160 MDK = ModuleDeclKind::PartitionInterface;
163 llvm_unreachable(
"how did we get a partition type set?");
171 switch (getLangOpts().getCompilingModule()) {
177 if (MDK != ModuleDeclKind::Implementation)
182 Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch)
184 MDK = ModuleDeclKind::Interface;
188 Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
193 Diag(ModuleLoc, diag::err_module_decl_in_header_module);
197 assert(ModuleScopes.size() <= 1 &&
"expected to be at global module scope");
203 if (!ModuleScopes.empty() &&
204 ModuleScopes.back().Module->isModulePurview()) {
205 Diag(ModuleLoc, diag::err_module_redeclaration);
206 Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
207 diag::note_prev_module_declaration);
212 Module *GlobalModuleFragment =
nullptr;
213 if (!ModuleScopes.empty() &&
215 GlobalModuleFragment = ModuleScopes.back().
Module;
217 assert((!getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS ||
218 SeenGMF == (
bool)GlobalModuleFragment) &&
219 "mismatched global module state");
223 if (getLangOpts().CPlusPlusModules && !IsFirstDecl && !SeenGMF) {
224 Diag(ModuleLoc, diag::err_module_decl_not_at_start);
227 ? SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID())
228 : ModuleScopes.back().BeginLoc;
230 Diag(BeginLoc, diag::note_global_module_introducer_missing)
245 if (!getLangOpts().CurrentModule.empty() &&
246 getLangOpts().CurrentModule != ModuleName) {
247 Diag(Path.front().second, diag::err_current_module_name_mismatch)
249 ? Partition.back().second
250 : Path.back().second)
251 << getLangOpts().CurrentModule;
254 const_cast<LangOptions&
>(getLangOpts()).CurrentModule = ModuleName;
256 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
260 case ModuleDeclKind::Interface:
261 case ModuleDeclKind::PartitionInterface: {
264 if (
auto *M = Map.findModule(ModuleName)) {
265 Diag(Path[0].second, diag::err_module_redefinition) << ModuleName;
266 if (M->DefinitionLoc.isValid())
267 Diag(M->DefinitionLoc, diag::note_prev_module_definition);
269 Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
276 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
277 GlobalModuleFragment);
278 if (MDK == ModuleDeclKind::PartitionInterface)
280 assert(Mod &&
"module creation should not fail");
284 case ModuleDeclKind::Implementation: {
285 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc(
286 PP.getIdentifierInfo(ModuleName), Path[0].second);
291 Mod = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
295 Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
297 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
298 GlobalModuleFragment);
302 case ModuleDeclKind::PartitionImplementation:
305 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
306 GlobalModuleFragment);
311 if (!GlobalModuleFragment) {
312 ModuleScopes.push_back({});
313 if (getLangOpts().ModulesLocalVisibility)
314 ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
317 ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global);
321 ModuleScopes.back().BeginLoc = StartLoc;
322 ModuleScopes.back().Module = Mod;
323 ModuleScopes.back().ModuleInterface = MDK != ModuleDeclKind::Implementation;
324 ModuleScopes.back().IsPartition = IsPartition;
325 VisibleModules.setVisible(Mod, ModuleLoc);
332 TU->setLocalOwningModule(Mod);
336 ImportState = ModuleImportState::ImportAllowed;
349 : ModuleScopes.back().Module->Kind) {
355 Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
359 Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
360 Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
367 if (!ModuleScopes.back().ModuleInterface) {
368 Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface);
369 Diag(ModuleScopes.back().BeginLoc,
370 diag::note_not_module_interface_add_export)
381 ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal);
383 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
384 Module *PrivateModuleFragment =
385 Map.createPrivateModuleFragmentForInterfaceUnit(
386 ModuleScopes.back().Module, PrivateLoc);
387 assert(PrivateModuleFragment &&
"module creation should not fail");
390 ModuleScopes.push_back({});
391 ModuleScopes.back().BeginLoc = ModuleLoc;
392 ModuleScopes.back().Module = PrivateModuleFragment;
393 ModuleScopes.back().ModuleInterface =
true;
394 VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
401 TU->setLocalOwningModule(PrivateModuleFragment);
412 bool Cxx20Mode = getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS;
413 assert((!IsPartition || Cxx20Mode) &&
"partition seen in non-C++20 code?");
417 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
422 assert(!ModuleScopes.empty() &&
"in a module purview, but no module?");
429 ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
431 }
else if (Cxx20Mode) {
433 ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
444 if (getLangOpts().CPlusPlusModules && isCurrentModulePurview() &&
445 getCurrentModule()->Name == ModuleName) {
446 Diag(ImportLoc, diag::err_module_self_import_cxx20)
447 << ModuleName << !ModuleScopes.back().ModuleInterface;
451 Module *Mod = getModuleLoader().loadModule(
456 return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
462 if (
auto *ED = dyn_cast<ExportDecl>(DC))
471 VisibleModules.setVisible(Mod, ImportLoc);
480 (getLangOpts().isCompilingModule() || !getLangOpts().ModulesTS)) {
481 Diag(ImportLoc, getLangOpts().isCompilingModule()
482 ? diag::err_module_self_import
483 : diag::err_module_import_in_implementation)
493 for (
Module *ModCheck = Mod; ModCheck; ModCheck = ModCheck->
Parent)
495 }
else if (getLangOpts().CPlusPlusModules && !Mod->
Parent) {
497 IdentifierLocs.push_back(Path[0].second);
500 for (
unsigned I = 0, N = Path.size(); I != N; ++I) {
505 ModCheck = ModCheck->
Parent;
507 IdentifierLocs.push_back(Path[I].second);
512 Mod, IdentifierLocs);
513 CurContext->addDecl(Import);
517 if (!ModuleScopes.empty())
521 if (getLangOpts().CPlusPlusModules && ExportLoc.
isValid() &&
522 Mod->
Kind == Module::ModuleKind::ModulePartitionImplementation) {
523 Diag(ExportLoc, diag::err_export_partition_impl)
525 }
else if (!ModuleScopes.empty() &&
526 (ModuleScopes.back().ModuleInterface ||
527 (getLangOpts().CPlusPlusModules &&
528 ModuleScopes.back().Module->isGlobalModule()))) {
529 assert((!ModuleScopes.back().Module->isGlobalModule() ||
530 Mod->
Kind == Module::ModuleKind::ModuleHeaderUnit) &&
531 "should only be importing a header unit into the GMF");
536 getCurrentModule()->Exports.emplace_back(Mod,
false);
538 getCurrentModule()->Imports.insert(Mod);
539 }
else if (ExportLoc.
isValid()) {
543 Diag(ExportLoc, diag::err_export_not_in_module_interface)
544 << (!ModuleScopes.empty() &&
545 !ModuleScopes.back().ImplicitGlobalModuleFragment);
546 }
else if (getLangOpts().isCompilingModule()) {
547 Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
548 getLangOpts().CurrentModule, ExportLoc,
false,
false);
550 assert(ThisModule &&
"was expecting a module if building one");
556 DirectModuleImports.insert(Mod);
563 BuildModuleInclude(DirectiveLoc, Mod);
572 bool IsInModuleIncludes =
574 getSourceManager().isWrittenInMainFile(DirectiveLoc);
576 bool ShouldAddImport = !IsInModuleIncludes;
580 if (ShouldAddImport) {
585 if (!ModuleScopes.empty())
588 Consumer.HandleImplicitImportDecl(ImportD);
592 VisibleModules.setVisible(Mod, DirectiveLoc);
594 if (getLangOpts().isCompilingModule()) {
595 Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
596 getLangOpts().CurrentModule, DirectiveLoc,
false,
false);
598 assert(ThisModule &&
"was expecting a module if building one");
605 ModuleScopes.push_back({});
606 ModuleScopes.back().Module = Mod;
607 if (getLangOpts().ModulesLocalVisibility)
608 ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
610 VisibleModules.setVisible(Mod, DirectiveLoc);
615 if (getLangOpts().trackLocalOwningModule()) {
616 for (
auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
617 cast<Decl>(DC)->setModuleOwnershipKind(
618 getLangOpts().ModulesLocalVisibility
621 cast<Decl>(DC)->setLocalOwningModule(Mod);
627 if (getLangOpts().ModulesLocalVisibility) {
628 VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules);
631 VisibleNamespaceCache.clear();
634 assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod &&
635 "left the wrong module scope");
636 ModuleScopes.pop_back();
640 FileID File = getSourceManager().getFileID(EomLoc);
642 if (EomLoc == getSourceManager().getLocForEndOfFile(
File)) {
644 assert(
File != getSourceManager().getMainFileID() &&
645 "end of submodule in main source file");
646 DirectiveLoc = getSourceManager().getIncludeLoc(
File);
649 DirectiveLoc = EomLoc;
651 BuildModuleInclude(DirectiveLoc, Mod);
654 if (getLangOpts().trackLocalOwningModule()) {
657 for (
auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
658 cast<Decl>(DC)->setLocalOwningModule(getCurrentModule());
659 if (!getCurrentModule())
660 cast<Decl>(DC)->setModuleOwnershipKind(
669 if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery ||
670 VisibleModules.isVisible(Mod))
678 Consumer.HandleImplicitImportDecl(ImportD);
682 VisibleModules.setVisible(Mod, Loc);
694 CurContext->addDecl(D);
695 PushDeclContext(S, D);
701 if (ModuleScopes.empty() || !ModuleScopes.back().Module->isModulePurview()) {
702 Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
705 }
else if (!ModuleScopes.back().ModuleInterface) {
706 Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
707 Diag(ModuleScopes.back().BeginLoc,
708 diag::note_not_module_interface_add_export)
712 }
else if (ModuleScopes.back().Module->Kind ==
714 Diag(ExportLoc, diag::err_export_in_private_module_fragment);
715 Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
721 if (
const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
724 if (ND->isAnonymousNamespace()) {
725 Diag(ExportLoc, diag::err_export_within_anonymous_namespace);
726 Diag(ND->getLocation(), diag::note_anonymous_namespace);
737 if (!DeferredExportedNamespaces.insert(ND).second)
745 Diag(ExportLoc, diag::err_export_within_export);
747 Diag(ED->getLocation(), diag::note_export);
760 enum class UnnamedDeclKind {
771 if (isa<EmptyDecl>(D))
772 return UnnamedDeclKind::Empty;
773 if (isa<StaticAssertDecl>(D))
774 return UnnamedDeclKind::StaticAssert;
775 if (isa<FileScopeAsmDecl>(D))
776 return UnnamedDeclKind::Asm;
777 if (isa<UsingDirectiveDecl>(D))
778 return UnnamedDeclKind::UsingDirective;
785 case UnnamedDeclKind::Empty:
786 case UnnamedDeclKind::StaticAssert:
789 return InBlock ? diag::ext_export_no_name_block : diag::err_export_no_name;
791 case UnnamedDeclKind::UsingDirective:
793 return diag::ext_export_using_directive;
795 case UnnamedDeclKind::Namespace:
797 return diag::introduces_no_names;
799 case UnnamedDeclKind::Context:
802 return diag::ext_export_no_names;
804 case UnnamedDeclKind::Asm:
805 return diag::err_export_no_name;
807 llvm_unreachable(
"unknown kind");
815 S.
Diag(BlockStart, diag::note_export);
826 bool HasName =
false;
827 if (
auto *ND = dyn_cast<NamedDecl>(D)) {
830 HasName = (
bool)ND->getDeclName();
832 S.
Diag(ND->getLocation(), diag::err_export_internal) << ND;
834 S.
Diag(BlockStart, diag::note_export);
841 if (
auto *USD = dyn_cast<UsingShadowDecl>(D)) {
845 S.
Diag(USD->getLocation(), diag::err_export_using_internal)
847 S.
Diag(
Target->getLocation(), diag::note_using_decl_target);
849 S.
Diag(BlockStart, diag::note_export);
855 if (
auto *DC = dyn_cast<DeclContext>(D)) {
856 if (isa<NamespaceDecl>(D) && DC->decls().empty()) {
862 }
else if (DC->getRedeclContext()->isFileContext() && !isa<EnumDecl>(D))
871 bool AllUnnamed =
true;
872 for (
auto *D : DC->
decls())
879 auto *ED = cast<ExportDecl>(D);
881 ED->setRBraceLoc(RBraceLoc);
888 for (
auto *Child : ED->decls()) {
905 if (!GlobalModuleFragment) {
906 ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
908 BeginLoc, getCurrentModule());
911 assert(GlobalModuleFragment &&
"module creation should not fail");
914 ModuleScopes.push_back({BeginLoc, GlobalModuleFragment,
919 VisibleModules.setVisible(GlobalModuleFragment, BeginLoc);
921 return GlobalModuleFragment;
924 void Sema::PopGlobalModuleFragment() {
925 assert(!ModuleScopes.empty() && getCurrentModule()->isGlobalModule() &&
926 "left the wrong module scope, which is not global module fragment");
927 ModuleScopes.pop_back();