#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTMutationListener.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/SemaInternal.h"
#include "llvm/ADT/StringExtras.h"
#include <optional>
using namespace clang;
using namespace sema;
static void checkModuleImportContext(Sema &S, Module *M,
SourceLocation ImportLoc, DeclContext *DC,
bool FromInclude = false) {
SourceLocation ExternCLoc;
if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) {
switch (LSD->getLanguage()) {
case LinkageSpecLanguageIDs::C:
if (ExternCLoc.isInvalid())
ExternCLoc = LSD->getBeginLoc();
break;
case LinkageSpecLanguageIDs::CXX:
break;
}
DC = LSD->getParent();
}
while (isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC))
DC = DC->getParent();
if (!isa<TranslationUnitDecl>(DC)) {
S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M))
? diag::ext_module_import_not_at_top_level_noop
: diag::err_module_import_not_at_top_level_fatal)
<< M->getFullModuleName() << DC;
S.Diag(cast<Decl>(DC)->getBeginLoc(),
diag::note_module_import_not_at_top_level)
<< DC;
} else if (!M->IsExternC && ExternCLoc.isValid()) {
S.Diag(ImportLoc, diag::ext_module_import_in_extern_c)
<< M->getFullModuleName();
S.Diag(ExternCLoc, diag::note_extern_c_begins_here);
}
}
static std::string stringFromPath(ModuleIdPath Path) {
std::string Name;
if (Path.empty())
return Name;
for (auto &Piece : Path) {
if (!Name.empty())
Name += ".";
Name += Piece.first->getName();
}
return Name;
}
static bool
isImportingModuleUnitFromSameModule(ASTContext &Ctx, Module *Imported,
Module *CurrentModule,
Module *&FoundPrimaryModuleInterface) {
if (!Imported->isNamedModule())
return false;
if (Imported->isModulePartition())
return true;
if (FoundPrimaryModuleInterface)
return Imported == FoundPrimaryModuleInterface;
if (!CurrentModule)
return false;
if (!CurrentModule->isModulePartitionImplementation())
return false;
if (Ctx.isInSameModule(Imported, CurrentModule)) {
assert(!FoundPrimaryModuleInterface ||
FoundPrimaryModuleInterface == Imported);
FoundPrimaryModuleInterface = Imported;
return true;
}
return false;
}
static void
makeTransitiveImportsVisible(ASTContext &Ctx, VisibleModuleSet &VisibleModules,
Module *Imported, Module *CurrentModule,
SourceLocation ImportLoc,
bool IsImportingPrimaryModuleInterface = false) {
assert(Imported->isNamedModule() &&
"'makeTransitiveImportsVisible()' is intended for standard C++ named "
"modules only.");
llvm::SmallVector<Module *, 4> Worklist;
Worklist.push_back(Imported);
Module *FoundPrimaryModuleInterface =
IsImportingPrimaryModuleInterface ? Imported : nullptr;
while (!Worklist.empty()) {
Module *Importing = Worklist.pop_back_val();
if (VisibleModules.isVisible(Importing))
continue;
VisibleModules.setVisible(Importing, ImportLoc);
if (isImportingModuleUnitFromSameModule(Ctx, Importing, CurrentModule,
FoundPrimaryModuleInterface))
for (Module *TransImported : Importing->Imports)
if (!VisibleModules.isVisible(TransImported))
Worklist.push_back(TransImported);
}
}
Sema::DeclGroupPtrTy
Sema::ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc) {
Module *GlobalModule =
PushGlobalModuleFragment(ModuleLoc);
auto *TU = Context.getTranslationUnitDecl();
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported);
TU->setLocalOwningModule(GlobalModule);
return nullptr;
}
void Sema::HandleStartOfHeaderUnit() {
assert(getLangOpts().CPlusPlusModules &&
"Header units are only valid for C++20 modules");
SourceLocation StartOfTU =
SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
StringRef HUName = getLangOpts().CurrentModule;
if (HUName.empty()) {
HUName =
SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID())->getName();
const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str();
}
auto F = SourceMgr.getFileManager().getOptionalFileRef(HUName);
if (!F)
F = SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID());
assert(F && "failed to find the header unit source?");
Module::Header H{HUName.str(), HUName.str(), *F};
auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Module *Mod = Map.createHeaderUnit(StartOfTU, HUName, H);
assert(Mod && "module creation should not fail");
ModuleScopes.push_back({});
ModuleScopes.back().BeginLoc = StartOfTU;
ModuleScopes.back().Module = Mod;
VisibleModules.setVisible(Mod, StartOfTU);
auto *TU = Context.getTranslationUnitDecl();
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::Visible);
TU->setLocalOwningModule(Mod);
}
static bool DiagReservedModuleName(Sema &S, const IdentifierInfo *II,
SourceLocation Loc) {
enum {
Valid = -1,
Invalid = 0,
Reserved = 1,
} Reason = Valid;
if (II->isStr("module") || II->isStr("import"))
Reason = Invalid;
else if (II->isReserved(S.getLangOpts()) !=
ReservedIdentifierStatus::NotReserved)
Reason = Reserved;
if (Reason == Reserved && S.getSourceManager().isInSystemHeader(Loc))
Reason = Valid;
switch (Reason) {
case Valid:
return false;
case Invalid:
return S.Diag(Loc, diag::err_invalid_module_name) << II;
case Reserved:
S.Diag(Loc, diag::warn_reserved_module_name) << II;
return false;
}
llvm_unreachable("fell off a fully covered switch");
}
Sema::DeclGroupPtrTy
Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
ModuleDeclKind MDK, ModuleIdPath Path,
ModuleIdPath Partition, ModuleImportState &ImportState) {
assert(getLangOpts().CPlusPlusModules &&
"should only have module decl in standard C++ modules");
bool IsFirstDecl = ImportState == ModuleImportState::FirstDecl;
bool SeenGMF = ImportState == ModuleImportState::GlobalFragment;
ImportState = ModuleImportState::NotACXX20Module;
bool IsPartition = !Partition.empty();
if (IsPartition)
switch (MDK) {
case ModuleDeclKind::Implementation:
MDK = ModuleDeclKind::PartitionImplementation;
break;
case ModuleDeclKind::Interface:
MDK = ModuleDeclKind::PartitionInterface;
break;
default:
llvm_unreachable("how did we get a partition type set?");
}
switch (getLangOpts().getCompilingModule()) {
case LangOptions::CMK_None:
break;
case LangOptions::CMK_ModuleInterface:
if (MDK != ModuleDeclKind::Implementation)
break;
Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch)
<< FixItHint::CreateInsertion(ModuleLoc, "export ");
MDK = ModuleDeclKind::Interface;
break;
case LangOptions::CMK_ModuleMap:
Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
return nullptr;
case LangOptions::CMK_HeaderUnit:
Diag(ModuleLoc, diag::err_module_decl_in_header_unit);
return nullptr;
}
assert(ModuleScopes.size() <= 1 && "expected to be at global module scope");
if (isCurrentModulePurview()) {
Diag(ModuleLoc, diag::err_module_redeclaration);
Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
diag::note_prev_module_declaration);
return nullptr;
}
assert((!getLangOpts().CPlusPlusModules ||
SeenGMF == (bool)this->TheGlobalModuleFragment) &&
"mismatched global module state");
if (getLangOpts().CPlusPlusModules && !IsFirstDecl && !SeenGMF) {
Diag(ModuleLoc, diag::err_module_decl_not_at_start);
SourceLocation BeginLoc =
ModuleScopes.empty()
? SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID())
: ModuleScopes.back().BeginLoc;
if (BeginLoc.isValid()) {
Diag(BeginLoc, diag::note_global_module_introducer_missing)
<< FixItHint::CreateInsertion(BeginLoc, "module;\n");
}
}
StringRef FirstComponentName = Path[0].first->getName();
if (!getSourceManager().isInSystemHeader(Path[0].second) &&
(FirstComponentName == "std" ||
(FirstComponentName.starts_with("std") &&
llvm::all_of(FirstComponentName.drop_front(3), &llvm::isDigit))))
Diag(Path[0].second, diag::warn_reserved_module_name) << Path[0].first;
for (auto Part : Path) {
if (DiagReservedModuleName(*this, Part.first, Part.second))
return nullptr;
}
std::string ModuleName = stringFromPath(Path);
if (IsPartition) {
ModuleName += ":";
ModuleName += stringFromPath(Partition);
}
if (!getLangOpts().CurrentModule.empty() &&
getLangOpts().CurrentModule != ModuleName) {
Diag(Path.front().second, diag::err_current_module_name_mismatch)
<< SourceRange(Path.front().second, IsPartition
? Partition.back().second
: Path.back().second)
<< getLangOpts().CurrentModule;
return nullptr;
}
const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Module *Mod;
Module *Interface = nullptr;
switch (MDK) {
case ModuleDeclKind::Interface:
case ModuleDeclKind::PartitionInterface: {
if (auto *M = Map.findModule(ModuleName)) {
Diag(Path[0].second, diag::err_module_redefinition) << ModuleName;
if (M->DefinitionLoc.isValid())
Diag(M->DefinitionLoc, diag::note_prev_module_definition);
else if (OptionalFileEntryRef FE = M->getASTFile())
Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
<< FE->getName();
Mod = M;
break;
}
Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
if (MDK == ModuleDeclKind::PartitionInterface)
Mod->Kind = Module::ModulePartitionInterface;
assert(Mod && "module creation should not fail");
break;
}
case ModuleDeclKind::Implementation: {
std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc(
PP.getIdentifierInfo(ModuleName), Path[0].second);
const_cast<LangOptions &>(getLangOpts()).CurrentModule = "";
Interface = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
Module::AllVisible,
false);
const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
if (!Interface) {
Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
} else {
Mod = Map.createModuleForImplementationUnit(ModuleLoc, ModuleName);
}
} break;
case ModuleDeclKind::PartitionImplementation:
Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
Mod->Kind = Module::ModulePartitionImplementation;
break;
}
if (!this->TheGlobalModuleFragment) {
ModuleScopes.push_back({});
if (getLangOpts().ModulesLocalVisibility)
ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
} else {
ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global);
}
ModuleScopes.back().BeginLoc = StartLoc;
ModuleScopes.back().Module = Mod;
VisibleModules.setVisible(Mod, ModuleLoc);
auto *TU = Context.getTranslationUnitDecl();
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported);
TU->setLocalOwningModule(Mod);
ImportState = ModuleImportState::ImportAllowed;
getASTContext().setCurrentNamedModule(Mod);
if (auto *Listener = getASTMutationListener())
Listener->EnteringModulePurview();
if (Interface) {
makeTransitiveImportsVisible(getASTContext(), VisibleModules, Interface,
Mod, ModuleLoc,
true);
ImportDecl *Import = ImportDecl::Create(Context, CurContext, ModuleLoc,
Interface, Path[0].second);
CurContext->addDecl(Import);
Context.addModuleInitializer(ModuleScopes.back().Module, Import);
Mod->Imports.insert(Interface);
ThePrimaryInterface = Interface;
return ConvertDeclToDeclGroup(Import);
}
return nullptr;
}
Sema::DeclGroupPtrTy
Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,
SourceLocation PrivateLoc) {
switch (ModuleScopes.empty() ? Module::ExplicitGlobalModuleFragment
: ModuleScopes.back().Module->Kind) {
case Module::ModuleMapModule:
case Module::ExplicitGlobalModuleFragment:
case Module::ImplicitGlobalModuleFragment:
case Module::ModulePartitionImplementation:
case Module::ModulePartitionInterface:
case Module::ModuleHeaderUnit:
Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
return nullptr;
case Module::PrivateModuleFragment:
Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
return nullptr;
case Module::ModuleImplementationUnit:
Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface);
Diag(ModuleScopes.back().BeginLoc,
diag::note_not_module_interface_add_export)
<< FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
return nullptr;
case Module::ModuleInterfaceUnit:
break;
}
ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal);
auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Module *PrivateModuleFragment =
Map.createPrivateModuleFragmentForInterfaceUnit(
ModuleScopes.back().Module, PrivateLoc);
assert(PrivateModuleFragment && "module creation should not fail");
ModuleScopes.push_back({});
ModuleScopes.back().BeginLoc = ModuleLoc;
ModuleScopes.back().Module = PrivateModuleFragment;
VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
auto *TU = Context.getTranslationUnitDecl();
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
TU->setLocalOwningModule(PrivateModuleFragment);
return nullptr;
}
DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
SourceLocation ExportLoc,
SourceLocation ImportLoc, ModuleIdPath Path,
bool IsPartition) {
assert((!IsPartition || getLangOpts().CPlusPlusModules) &&
"partition seen in non-C++20 code?");
std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
std::string ModuleName;
if (IsPartition) {
assert(!ModuleScopes.empty() && "in a module purview, but no module?");
Module *NamedMod = ModuleScopes.back().Module;
ModuleName = NamedMod->getPrimaryModuleInterfaceName().str();
ModuleName += ":";
ModuleName += stringFromPath(Path);
ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
Path = ModuleIdPath(ModuleNameLoc);
} else if (getLangOpts().CPlusPlusModules) {
ModuleName = stringFromPath(Path);
ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
Path = ModuleIdPath(ModuleNameLoc);
}
if (getLangOpts().CPlusPlusModules && isCurrentModulePurview() &&
getCurrentModule()->Name == ModuleName) {
Diag(ImportLoc, diag::err_module_self_import_cxx20)
<< ModuleName << currentModuleIsImplementation();
return true;
}
Module *Mod = getModuleLoader().loadModule(
ImportLoc, Path, Module::AllVisible, false);
if (!Mod)
return true;
if (!Mod->isInterfaceOrPartition() && !ModuleName.empty() &&
!getLangOpts().ObjC) {
Diag(ImportLoc, diag::err_module_import_non_interface_nor_parition)
<< ModuleName;
return true;
}
return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
}
static const ExportDecl *getEnclosingExportDecl(const Decl *D) {
for (auto *DC = D->getLexicalDeclContext(); DC; DC = DC->getLexicalParent())
if (auto *ED = dyn_cast<ExportDecl>(DC))
return ED;
return nullptr;
}
DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
SourceLocation ExportLoc,
SourceLocation ImportLoc, Module *Mod,
ModuleIdPath Path) {
if (Mod->isHeaderUnit())
Diag(ImportLoc, diag::warn_experimental_header_unit);
if (Mod->isNamedModule())
makeTransitiveImportsVisible(getASTContext(), VisibleModules, Mod,
getCurrentModule(), ImportLoc);
else
VisibleModules.setVisible(Mod, ImportLoc);
checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
if (Mod->isForBuilding(getLangOpts())) {
Diag(ImportLoc, getLangOpts().isCompilingModule()
? diag::err_module_self_import
: diag::err_module_import_in_implementation)
<< Mod->getFullModuleName() << getLangOpts().CurrentModule;
}
SmallVector<SourceLocation, 2> IdentifierLocs;
if (Path.empty()) {
for (Module *ModCheck = Mod; ModCheck; ModCheck = ModCheck->Parent)
IdentifierLocs.push_back(SourceLocation());
} else if (getLangOpts().CPlusPlusModules && !Mod->Parent) {
IdentifierLocs.push_back(Path[0].second);
} else {
Module *ModCheck = Mod;
for (unsigned I = 0, N = Path.size(); I != N; ++I) {
if (!ModCheck)
break;
ModCheck = ModCheck->Parent;
IdentifierLocs.push_back(Path[I].second);
}
}
ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc,
Mod, IdentifierLocs);
CurContext->addDecl(Import);
if (!ModuleScopes.empty())
Context.addModuleInitializer(ModuleScopes.back().Module, Import);
if (getLangOpts().CPlusPlusModules && ExportLoc.isValid() &&
Mod->Kind == Module::ModuleKind::ModulePartitionImplementation) {
Diag(ExportLoc, diag::err_export_partition_impl)
<< SourceRange(ExportLoc, Path.back().second);
} else if (!ModuleScopes.empty() && !currentModuleIsImplementation()) {
if (ExportLoc.isValid() || getEnclosingExportDecl(Import))
getCurrentModule()->Exports.emplace_back(Mod, false);
else
getCurrentModule()->Imports.insert(Mod);
} else if (ExportLoc.isValid()) {
Diag(ExportLoc, diag::err_export_not_in_module_interface);
}
return Import;
}
void Sema::ActOnAnnotModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
BuildModuleInclude(DirectiveLoc, Mod);
}
void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
bool IsInModuleIncludes =
TUKind == TU_ClangModule &&
getSourceManager().isWrittenInMainFile(DirectiveLoc);
if (getLangOpts().Modules && !IsInModuleIncludes) {
TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
DirectiveLoc, Mod,
DirectiveLoc);
if (!ModuleScopes.empty())
Context.addModuleInitializer(ModuleScopes.back().Module, ImportD);
TU->addDecl(ImportD);
Consumer.HandleImplicitImportDecl(ImportD);
}
getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc);
VisibleModules.setVisible(Mod, DirectiveLoc);
if (getLangOpts().isCompilingModule()) {
Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
getLangOpts().CurrentModule, DirectiveLoc, false, false);
(void)ThisModule;
assert(ThisModule && "was expecting a module if building one");
}
}
void Sema::ActOnAnnotModuleBegin(SourceLocation DirectiveLoc, Module *Mod) {
checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
ModuleScopes.push_back({});
ModuleScopes.back().Module = Mod;
if (getLangOpts().ModulesLocalVisibility)
ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
VisibleModules.setVisible(Mod, DirectiveLoc);
if (getLangOpts().trackLocalOwningModule()) {
for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
cast<Decl>(DC)->setModuleOwnershipKind(
getLangOpts().ModulesLocalVisibility
? Decl::ModuleOwnershipKind::VisibleWhenImported
: Decl::ModuleOwnershipKind::Visible);
cast<Decl>(DC)->setLocalOwningModule(Mod);
}
}
}
void Sema::ActOnAnnotModuleEnd(SourceLocation EomLoc, Module *Mod) {
if (getLangOpts().ModulesLocalVisibility) {
VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules);
VisibleNamespaceCache.clear();
}
assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod &&
"left the wrong module scope");
ModuleScopes.pop_back();
FileID File = getSourceManager().getFileID(EomLoc);
SourceLocation DirectiveLoc;
if (EomLoc == getSourceManager().getLocForEndOfFile(File)) {
assert(File != getSourceManager().getMainFileID() &&
"end of submodule in main source file");
DirectiveLoc = getSourceManager().getIncludeLoc(File);
} else {
DirectiveLoc = EomLoc;
}
BuildModuleInclude(DirectiveLoc, Mod);
if (getLangOpts().trackLocalOwningModule()) {
for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
cast<Decl>(DC)->setLocalOwningModule(getCurrentModule());
if (!getCurrentModule())
cast<Decl>(DC)->setModuleOwnershipKind(
Decl::ModuleOwnershipKind::Unowned);
}
}
}
void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc,
Module *Mod) {
if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery ||
VisibleModules.isVisible(Mod))
return;
TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
Loc, Mod, Loc);
TU->addDecl(ImportD);
Consumer.HandleImplicitImportDecl(ImportD);
getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc);
VisibleModules.setVisible(Mod, Loc);
}
Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
SourceLocation LBraceLoc) {
ExportDecl *D = ExportDecl::Create(Context, CurContext, ExportLoc);
D->setRBraceLoc(LBraceLoc);
CurContext->addDecl(D);
PushDeclContext(S, D);
if (!getLangOpts().HLSL) {
if (!isCurrentModulePurview()) {
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
D->setInvalidDecl();
return D;
} else if (currentModuleIsImplementation()) {
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
Diag(ModuleScopes.back().BeginLoc,
diag::note_not_module_interface_add_export)
<< FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
D->setInvalidDecl();
return D;
} else if (ModuleScopes.back().Module->Kind ==
Module::PrivateModuleFragment) {
Diag(ExportLoc, diag::err_export_in_private_module_fragment);
Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
D->setInvalidDecl();
return D;
}
}
for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) {
if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
if (ND->isAnonymousNamespace()) {
Diag(ExportLoc, diag::err_export_within_anonymous_namespace);
Diag(ND->getLocation(), diag::note_anonymous_namespace);
D->setInvalidDecl();
return D;
}
if (!getLangOpts().HLSL && !DeferredExportedNamespaces.insert(ND).second)
break;
}
}
if (auto *ED = getEnclosingExportDecl(D)) {
Diag(ExportLoc, diag::err_export_within_export);
if (ED->hasBraces())
Diag(ED->getLocation(), diag::note_export);
D->setInvalidDecl();
return D;
}
if (!getLangOpts().HLSL)
D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
return D;
}
static bool checkExportedDecl(Sema &, Decl *, SourceLocation);
static bool checkExportedDeclContext(Sema &S, DeclContext *DC,
SourceLocation BlockStart) {
bool AllUnnamed = true;
for (auto *D : DC->decls())
AllUnnamed &= checkExportedDecl(S, D, BlockStart);
return AllUnnamed;
}
static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
if (S.getLangOpts().HLSL) {
if (!dyn_cast<FunctionDecl>(D) && !dyn_cast<ExportDecl>(D)) {
S.Diag(D->getBeginLoc(), diag::err_hlsl_export_not_on_function);
D->setInvalidDecl();
return false;
}
}
bool HasName = false;
if (auto *ND = dyn_cast<NamedDecl>(D)) {
HasName = (bool)ND->getDeclName();
if (HasName && ND->getFormalLinkage() == Linkage::Internal) {
S.Diag(ND->getLocation(), diag::err_export_internal) << ND;
if (BlockStart.isValid())
S.Diag(BlockStart, diag::note_export);
return false;
}
}
if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
NamedDecl *Target = USD->getUnderlyingDecl();
Linkage Lk = Target->getFormalLinkage();
if (Lk == Linkage::Internal || Lk == Linkage::Module) {
S.Diag(USD->getLocation(), diag::err_export_using_internal)
<< (Lk == Linkage::Internal ? 0 : 1) << Target;
S.Diag(Target->getLocation(), diag::note_using_decl_target);
if (BlockStart.isValid())
S.Diag(BlockStart, diag::note_export);
return false;
}
}
if (auto *DC = dyn_cast<DeclContext>(D)) {
if (!isa<NamespaceDecl>(D))
return true;
if (auto *ND = dyn_cast<NamedDecl>(D)) {
if (!ND->getDeclName()) {
S.Diag(ND->getLocation(), diag::err_export_anon_ns_internal);
if (BlockStart.isValid())
S.Diag(BlockStart, diag::note_export);
return false;
} else if (!DC->decls().empty() &&
DC->getRedeclContext()->isFileContext()) {
return checkExportedDeclContext(S, DC, BlockStart);
}
}
}
return true;
}
Decl *Sema::ActOnFinishExportDecl(Scope *S, Decl *D, SourceLocation RBraceLoc) {
auto *ED = cast<ExportDecl>(D);
if (RBraceLoc.isValid())
ED->setRBraceLoc(RBraceLoc);
PopDeclContext();
if (!D->isInvalidDecl()) {
SourceLocation BlockStart =
ED->hasBraces() ? ED->getBeginLoc() : SourceLocation();
for (auto *Child : ED->decls()) {
checkExportedDecl(*this, Child, BlockStart);
if (auto *FD = dyn_cast<FunctionDecl>(Child)) {
if (FD->isInlineSpecified() && !FD->isDefined())
PendingInlineFuncDecls.insert(FD);
}
}
}
for (auto *Exported : ED->decls())
Exported->markUsed(getASTContext());
return D;
}
Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc) {
if (!TheGlobalModuleFragment) {
ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
TheGlobalModuleFragment = Map.createGlobalModuleFragmentForModuleUnit(
BeginLoc, getCurrentModule());
}
assert(TheGlobalModuleFragment && "module creation should not fail");
ModuleScopes.push_back({BeginLoc, TheGlobalModuleFragment,
{}});
VisibleModules.setVisible(TheGlobalModuleFragment, BeginLoc);
return TheGlobalModuleFragment;
}
void Sema::PopGlobalModuleFragment() {
assert(!ModuleScopes.empty() &&
getCurrentModule()->isExplicitGlobalModule() &&
"left the wrong module scope, which is not global module fragment");
ModuleScopes.pop_back();
}
Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc) {
if (!TheImplicitGlobalModuleFragment) {
ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
TheImplicitGlobalModuleFragment =
Map.createImplicitGlobalModuleFragmentForModuleUnit(BeginLoc,
getCurrentModule());
}
assert(TheImplicitGlobalModuleFragment && "module creation should not fail");
ModuleScopes.push_back({BeginLoc, TheImplicitGlobalModuleFragment,
{}});
VisibleModules.setVisible(TheImplicitGlobalModuleFragment, BeginLoc);
return TheImplicitGlobalModuleFragment;
}
void Sema::PopImplicitGlobalModuleFragment() {
assert(!ModuleScopes.empty() &&
getCurrentModule()->isImplicitGlobalModule() &&
"left the wrong module scope, which is not global module fragment");
ModuleScopes.pop_back();
}
bool Sema::isCurrentModulePurview() const {
if (!getCurrentModule())
return false;
switch (getCurrentModule()->Kind) {
case Module::ModuleInterfaceUnit:
case Module::ModuleImplementationUnit:
case Module::ModulePartitionInterface:
case Module::ModulePartitionImplementation:
case Module::PrivateModuleFragment:
case Module::ImplicitGlobalModuleFragment:
return true;
default:
return false;
}
}