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);
59 if (!ModuleScopes.empty() &&
63 assert(getLangOpts().CPlusPlusModules && getLangOpts().ModulesTS &&
64 "unexpectedly encountered multiple global module fragment decls");
65 ModuleScopes.back().BeginLoc = ModuleLoc;
71 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
72 auto *GlobalModule = Map.createGlobalModuleFragmentForModuleUnit(ModuleLoc);
73 assert(GlobalModule &&
"module creation should not fail");
76 ModuleScopes.push_back({});
77 ModuleScopes.back().BeginLoc = ModuleLoc;
78 ModuleScopes.back().Module = GlobalModule;
79 VisibleModules.setVisible(GlobalModule, ModuleLoc);
84 TU->setLocalOwningModule(GlobalModule);
93 assert((getLangOpts().ModulesTS || getLangOpts().CPlusPlusModules) &&
94 "should only have module decl in Modules TS or C++20");
99 switch (getLangOpts().getCompilingModule()) {
105 if (MDK != ModuleDeclKind::Implementation)
110 Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch)
112 MDK = ModuleDeclKind::Interface;
116 Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
120 Diag(ModuleLoc, diag::err_module_decl_in_header_module);
124 assert(ModuleScopes.size() <= 1 &&
"expected to be at global module scope");
130 if (!ModuleScopes.empty() &&
131 ModuleScopes.back().Module->isModulePurview()) {
132 Diag(ModuleLoc, diag::err_module_redeclaration);
133 Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
134 diag::note_prev_module_declaration);
139 Module *GlobalModuleFragment =
nullptr;
140 if (!ModuleScopes.empty() &&
142 GlobalModuleFragment = ModuleScopes.back().
Module;
146 if (getLangOpts().CPlusPlusModules && !IsFirstDecl && !GlobalModuleFragment) {
147 Diag(ModuleLoc, diag::err_module_decl_not_at_start);
150 ? SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID())
151 : ModuleScopes.back().BeginLoc;
153 Diag(BeginLoc, diag::note_global_module_introducer_missing)
162 for (
auto &Piece : Path) {
163 if (!ModuleName.empty())
165 ModuleName += Piece.first->getName();
170 if (!getLangOpts().CurrentModule.empty() &&
171 getLangOpts().CurrentModule != ModuleName) {
172 Diag(Path.front().second, diag::err_current_module_name_mismatch)
173 <<
SourceRange(Path.front().second, Path.back().second)
174 << getLangOpts().CurrentModule;
177 const_cast<LangOptions&
>(getLangOpts()).CurrentModule = ModuleName;
179 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
183 case ModuleDeclKind::Interface: {
186 if (
auto *M = Map.findModule(ModuleName)) {
187 Diag(Path[0].second, diag::err_module_redefinition) << ModuleName;
188 if (M->DefinitionLoc.isValid())
189 Diag(M->DefinitionLoc, diag::note_prev_module_definition);
191 Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
198 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
199 GlobalModuleFragment);
200 assert(Mod &&
"module creation should not fail");
204 case ModuleDeclKind::Implementation:
205 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc(
206 PP.getIdentifierInfo(ModuleName), Path[0].second);
207 Mod = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
211 Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
213 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
214 GlobalModuleFragment);
219 if (!GlobalModuleFragment) {
220 ModuleScopes.push_back({});
221 if (getLangOpts().ModulesLocalVisibility)
222 ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
225 ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global);
229 ModuleScopes.back().BeginLoc = StartLoc;
230 ModuleScopes.back().Module = Mod;
231 ModuleScopes.back().ModuleInterface = MDK != ModuleDeclKind::Implementation;
232 VisibleModules.setVisible(Mod, ModuleLoc);
239 TU->setLocalOwningModule(Mod);
252 : ModuleScopes.back().Module->Kind) {
255 Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
259 Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
260 Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
267 if (!ModuleScopes.back().ModuleInterface) {
268 Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface);
269 Diag(ModuleScopes.back().BeginLoc,
270 diag::note_not_module_interface_add_export)
281 ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal);
283 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
284 Module *PrivateModuleFragment =
285 Map.createPrivateModuleFragmentForInterfaceUnit(
286 ModuleScopes.back().Module, PrivateLoc);
287 assert(PrivateModuleFragment &&
"module creation should not fail");
290 ModuleScopes.push_back({});
291 ModuleScopes.back().BeginLoc = ModuleLoc;
292 ModuleScopes.back().Module = PrivateModuleFragment;
293 ModuleScopes.back().ModuleInterface =
true;
294 VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
301 TU->setLocalOwningModule(PrivateModuleFragment);
312 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
313 if (getLangOpts().ModulesTS) {
315 for (
auto &Piece : Path) {
316 if (!ModuleName.empty())
318 ModuleName += Piece.first->getName();
320 ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
330 return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
336 if (
auto *ED = dyn_cast<ExportDecl>(DC))
345 VisibleModules.setVisible(Mod, ImportLoc);
357 (getLangOpts().isCompilingModule() || !getLangOpts().ModulesTS)) {
358 Diag(ImportLoc, getLangOpts().isCompilingModule()
359 ? diag::err_module_self_import
360 : diag::err_module_import_in_implementation)
366 for (
unsigned I = 0, N = Path.size(); I != N; ++I) {
371 ModCheck = ModCheck->
Parent;
373 IdentifierLocs.push_back(Path[I].second);
379 for (; ModCheck; ModCheck = ModCheck->
Parent) {
385 Mod, IdentifierLocs);
386 CurContext->addDecl(Import);
390 if (!ModuleScopes.empty())
394 if (!ModuleScopes.empty() && ModuleScopes.back().ModuleInterface) {
396 getCurrentModule()->Exports.emplace_back(Mod,
false);
397 }
else if (ExportLoc.
isValid()) {
398 Diag(ExportLoc, diag::err_export_not_in_module_interface);
406 BuildModuleInclude(DirectiveLoc, Mod);
415 bool IsInModuleIncludes =
417 getSourceManager().isWrittenInMainFile(DirectiveLoc);
419 bool ShouldAddImport = !IsInModuleIncludes;
423 if (ShouldAddImport) {
428 if (!ModuleScopes.empty())
431 Consumer.HandleImplicitImportDecl(ImportD);
435 VisibleModules.setVisible(Mod, DirectiveLoc);
441 ModuleScopes.push_back({});
442 ModuleScopes.back().Module = Mod;
443 if (getLangOpts().ModulesLocalVisibility)
444 ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
446 VisibleModules.setVisible(Mod, DirectiveLoc);
451 if (getLangOpts().trackLocalOwningModule()) {
452 for (
auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
453 cast<Decl>(DC)->setModuleOwnershipKind(
454 getLangOpts().ModulesLocalVisibility
457 cast<Decl>(DC)->setLocalOwningModule(Mod);
463 if (getLangOpts().ModulesLocalVisibility) {
464 VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules);
467 VisibleNamespaceCache.clear();
470 assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod &&
471 "left the wrong module scope");
472 ModuleScopes.pop_back();
476 FileID File = getSourceManager().getFileID(EomLoc);
478 if (EomLoc == getSourceManager().getLocForEndOfFile(
File)) {
480 assert(
File != getSourceManager().getMainFileID() &&
481 "end of submodule in main source file");
482 DirectiveLoc = getSourceManager().getIncludeLoc(
File);
485 DirectiveLoc = EomLoc;
487 BuildModuleInclude(DirectiveLoc, Mod);
490 if (getLangOpts().trackLocalOwningModule()) {
493 for (
auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
494 cast<Decl>(DC)->setLocalOwningModule(getCurrentModule());
495 if (!getCurrentModule())
496 cast<Decl>(DC)->setModuleOwnershipKind(
505 if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery ||
506 VisibleModules.isVisible(Mod))
514 Consumer.HandleImplicitImportDecl(ImportD);
518 VisibleModules.setVisible(Mod, Loc);
534 if (ModuleScopes.empty() || !ModuleScopes.back().Module->isModulePurview()) {
535 Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
536 }
else if (!ModuleScopes.back().ModuleInterface) {
537 Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
538 Diag(ModuleScopes.back().BeginLoc,
539 diag::note_not_module_interface_add_export)
541 }
else if (ModuleScopes.back().Module->Kind ==
543 Diag(ExportLoc, diag::err_export_in_private_module_fragment);
544 Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
548 if (
const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
551 if (ND->isAnonymousNamespace()) {
552 Diag(ExportLoc, diag::err_export_within_anonymous_namespace);
553 Diag(ND->getLocation(), diag::note_anonymous_namespace);
564 if (!DeferredExportedNamespaces.insert(ND).second)
572 Diag(ExportLoc, diag::err_export_within_export);
574 Diag(ED->getLocation(), diag::note_export);
577 CurContext->addDecl(D);
578 PushDeclContext(S, D);
587 enum class UnnamedDeclKind {
597 if (isa<EmptyDecl>(D))
598 return UnnamedDeclKind::Empty;
599 if (isa<StaticAssertDecl>(D))
600 return UnnamedDeclKind::StaticAssert;
601 if (isa<FileScopeAsmDecl>(D))
602 return UnnamedDeclKind::Asm;
603 if (isa<UsingDirectiveDecl>(D))
604 return UnnamedDeclKind::UsingDirective;
611 case UnnamedDeclKind::Empty:
612 case UnnamedDeclKind::StaticAssert:
615 return InBlock ? diag::ext_export_no_name_block : diag::err_export_no_name;
617 case UnnamedDeclKind::UsingDirective:
619 return diag::ext_export_using_directive;
621 case UnnamedDeclKind::Context:
624 return diag::ext_export_no_names;
626 case UnnamedDeclKind::Asm:
627 return diag::err_export_no_name;
629 llvm_unreachable(
"unknown kind");
637 S.
Diag(BlockStart, diag::note_export);
648 if (
auto *ND = dyn_cast<NamedDecl>(D)) {
652 S.
Diag(ND->getLocation(), diag::err_export_internal) << ND;
654 S.
Diag(BlockStart, diag::note_export);
661 if (
auto *USD = dyn_cast<UsingShadowDecl>(D)) {
664 S.
Diag(USD->getLocation(), diag::err_export_using_internal) <<
Target;
665 S.
Diag(
Target->getLocation(), diag::note_using_decl_target);
667 S.
Diag(BlockStart, diag::note_export);
673 if (
auto *DC = dyn_cast<DeclContext>(D))
674 if (DC->getRedeclContext()->isFileContext() && !isa<EnumDecl>(D))
682 bool AllUnnamed =
true;
683 for (
auto *D : DC->
decls())
690 auto *ED = cast<ExportDecl>(D);
692 ED->setRBraceLoc(RBraceLoc);
699 for (
auto *Child : ED->decls()) {