#include "clang/AST/ASTConsumer.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/SemaInternal.h"
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 LinkageSpecDecl::lang_c:
if (ExternCLoc.isInvalid())
ExternCLoc = LSD->getBeginLoc();
break;
case LinkageSpecDecl::lang_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;
}
Sema::DeclGroupPtrTy
Sema::ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc) {
if (!ModuleScopes.empty() &&
ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment) {
assert(getLangOpts().CPlusPlusModules && getLangOpts().ModulesTS &&
"unexpectedly encountered multiple global module fragment decls");
ModuleScopes.back().BeginLoc = ModuleLoc;
return nullptr;
}
Module *GlobalModule =
PushGlobalModuleFragment(ModuleLoc, false);
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.getFileEntryForID(SourceMgr.getMainFileID())->getName();
const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str();
}
auto F = SourceMgr.getFileManager().getFile(HUName);
if (!F)
F = SourceMgr.getFileEntryForID(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;
ModuleScopes.back().ModuleInterface = true;
ModuleScopes.back().IsPartition = false;
VisibleModules.setVisible(Mod, StartOfTU);
auto *TU = Context.getTranslationUnitDecl();
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::Visible);
TU->setLocalOwningModule(Mod);
}
Sema::DeclGroupPtrTy
Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
ModuleDeclKind MDK, ModuleIdPath Path,
ModuleIdPath Partition, ModuleImportState &ImportState) {
assert((getLangOpts().ModulesTS || getLangOpts().CPlusPlusModules) &&
"should only have module decl in Modules TS or C++20");
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_HeaderModule:
case LangOptions::CMK_HeaderUnit:
Diag(ModuleLoc, diag::err_module_decl_in_header_module);
return nullptr;
}
assert(ModuleScopes.size() <= 1 && "expected to be at global module scope");
if (!ModuleScopes.empty() &&
ModuleScopes.back().Module->isModulePurview()) {
Diag(ModuleLoc, diag::err_module_redeclaration);
Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
diag::note_prev_module_declaration);
return nullptr;
}
Module *GlobalModuleFragment = nullptr;
if (!ModuleScopes.empty() &&
ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment)
GlobalModuleFragment = ModuleScopes.back().Module;
assert((!getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS ||
SeenGMF == (bool)GlobalModuleFragment) &&
"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");
}
}
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;
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 (Optional<FileEntryRef> FE = M->getASTFile())
Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
<< FE->getName();
Mod = M;
break;
}
Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
GlobalModuleFragment);
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);
Mod = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
Module::AllVisible,
false);
if (!Mod) {
Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
GlobalModuleFragment);
}
} break;
case ModuleDeclKind::PartitionImplementation:
Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
GlobalModuleFragment);
Mod->Kind = Module::ModulePartitionImplementation;
break;
}
if (!GlobalModuleFragment) {
ModuleScopes.push_back({});
if (getLangOpts().ModulesLocalVisibility)
ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
} else {
ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global);
}
ModuleScopes.back().BeginLoc = StartLoc;
ModuleScopes.back().Module = Mod;
ModuleScopes.back().ModuleInterface = MDK != ModuleDeclKind::Implementation;
ModuleScopes.back().IsPartition = IsPartition;
VisibleModules.setVisible(Mod, ModuleLoc);
auto *TU = Context.getTranslationUnitDecl();
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported);
TU->setLocalOwningModule(Mod);
ImportState = ModuleImportState::ImportAllowed;
if (MDK == ModuleDeclKind::Implementation) {
ImportDecl *Import =
ImportDecl::Create(Context, CurContext, ModuleLoc, Mod, Path[0].second);
return ConvertDeclToDeclGroup(Import);
}
return nullptr;
}
Sema::DeclGroupPtrTy
Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,
SourceLocation PrivateLoc) {
switch (ModuleScopes.empty() ? Module::GlobalModuleFragment
: ModuleScopes.back().Module->Kind) {
case Module::ModuleMapModule:
case Module::GlobalModuleFragment:
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::ModuleInterfaceUnit:
break;
}
if (!ModuleScopes.back().ModuleInterface) {
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;
}
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;
ModuleScopes.back().ModuleInterface = true;
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) {
bool Cxx20Mode = getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS;
assert((!IsPartition || Cxx20Mode) && "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 (Cxx20Mode) {
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 << !ModuleScopes.back().ModuleInterface;
return true;
}
Module *Mod = getModuleLoader().loadModule(
ImportLoc, Path, Module::AllVisible, false);
if (!Mod)
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) {
VisibleModules.setVisible(Mod, ImportLoc);
checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
if (Mod->getTopLevelModuleName() == getLangOpts().CurrentModule &&
(getLangOpts().isCompilingModule() || !getLangOpts().ModulesTS)) {
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() &&
(ModuleScopes.back().ModuleInterface ||
(getLangOpts().CPlusPlusModules &&
ModuleScopes.back().Module->isGlobalModule()))) {
assert((!ModuleScopes.back().Module->isGlobalModule() ||
Mod->Kind == Module::ModuleKind::ModuleHeaderUnit) &&
"should only be importing a header unit into the GMF");
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)
<< (!ModuleScopes.empty() &&
!ModuleScopes.back().ImplicitGlobalModuleFragment);
} else if (getLangOpts().isCompilingModule()) {
Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
getLangOpts().CurrentModule, ExportLoc, false, false);
(void)ThisModule;
assert(ThisModule && "was expecting a module if building one");
}
DirectModuleImports.insert(Mod);
return Import;
}
void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
BuildModuleInclude(DirectiveLoc, Mod);
}
void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
bool IsInModuleIncludes =
TUKind == TU_Module &&
getSourceManager().isWrittenInMainFile(DirectiveLoc);
bool ShouldAddImport = !IsInModuleIncludes;
if (ShouldAddImport) {
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::ActOnModuleBegin(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::ActOnModuleEnd(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 (ModuleScopes.empty() || !ModuleScopes.back().Module->isModulePurview()) {
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
D->setInvalidDecl();
return D;
} else if (!ModuleScopes.back().ModuleInterface) {
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 (!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;
}
D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
return D;
}
static bool checkExportedDeclContext(Sema &S, DeclContext *DC,
SourceLocation BlockStart);
namespace {
enum class UnnamedDeclKind {
Empty,
StaticAssert,
Asm,
UsingDirective,
Namespace,
Context
};
}
static llvm::Optional<UnnamedDeclKind> getUnnamedDeclKind(Decl *D) {
if (isa<EmptyDecl>(D))
return UnnamedDeclKind::Empty;
if (isa<StaticAssertDecl>(D))
return UnnamedDeclKind::StaticAssert;
if (isa<FileScopeAsmDecl>(D))
return UnnamedDeclKind::Asm;
if (isa<UsingDirectiveDecl>(D))
return UnnamedDeclKind::UsingDirective;
return llvm::None;
}
unsigned getUnnamedDeclDiag(UnnamedDeclKind UDK, bool InBlock) {
switch (UDK) {
case UnnamedDeclKind::Empty:
case UnnamedDeclKind::StaticAssert:
return InBlock ? diag::ext_export_no_name_block : diag::err_export_no_name;
case UnnamedDeclKind::UsingDirective:
return diag::ext_export_using_directive;
case UnnamedDeclKind::Namespace:
return diag::introduces_no_names;
case UnnamedDeclKind::Context:
return diag::ext_export_no_names;
case UnnamedDeclKind::Asm:
return diag::err_export_no_name;
}
llvm_unreachable("unknown kind");
}
static void diagExportedUnnamedDecl(Sema &S, UnnamedDeclKind UDK, Decl *D,
SourceLocation BlockStart) {
S.Diag(D->getLocation(), getUnnamedDeclDiag(UDK, BlockStart.isValid()))
<< (unsigned)UDK;
if (BlockStart.isValid())
S.Diag(BlockStart, diag::note_export);
}
static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
if (auto UDK = getUnnamedDeclKind(D))
diagExportedUnnamedDecl(S, *UDK, D, BlockStart);
bool HasName = false;
if (auto *ND = dyn_cast<NamedDecl>(D)) {
HasName = (bool)ND->getDeclName();
if (HasName && ND->getFormalLinkage() == InternalLinkage) {
S.Diag(ND->getLocation(), diag::err_export_internal) << ND;
if (BlockStart.isValid())
S.Diag(BlockStart, diag::note_export);
}
}
if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
NamedDecl *Target = USD->getUnderlyingDecl();
Linkage Lk = Target->getFormalLinkage();
if (Lk == InternalLinkage || Lk == ModuleLinkage) {
S.Diag(USD->getLocation(), diag::err_export_using_internal)
<< (Lk == InternalLinkage ? 0 : 1) << Target;
S.Diag(Target->getLocation(), diag::note_using_decl_target);
if (BlockStart.isValid())
S.Diag(BlockStart, diag::note_export);
}
}
if (auto *DC = dyn_cast<DeclContext>(D)) {
if (isa<NamespaceDecl>(D) && DC->decls().empty()) {
if (!HasName)
diagExportedUnnamedDecl(S, UnnamedDeclKind::Namespace, D, BlockStart);
} else if (DC->getRedeclContext()->isFileContext() && !isa<EnumDecl>(D))
return checkExportedDeclContext(S, DC, BlockStart);
}
return false;
}
static bool checkExportedDeclContext(Sema &S, DeclContext *DC,
SourceLocation BlockStart) {
bool AllUnnamed = true;
for (auto *D : DC->decls())
AllUnnamed &= checkExportedDecl(S, D, BlockStart);
return AllUnnamed;
}
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()) {
if (checkExportedDecl(*this, Child, BlockStart)) {
diagExportedUnnamedDecl(*this, UnnamedDeclKind::Context, Child,
BlockStart);
}
}
}
return D;
}
Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc,
bool IsImplicit) {
if (!GlobalModuleFragment) {
ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
GlobalModuleFragment = Map.createGlobalModuleFragmentForModuleUnit(
BeginLoc, getCurrentModule());
}
assert(GlobalModuleFragment && "module creation should not fail");
ModuleScopes.push_back({BeginLoc, GlobalModuleFragment,
false,
false,
IsImplicit,
{}});
VisibleModules.setVisible(GlobalModuleFragment, BeginLoc);
return GlobalModuleFragment;
}
void Sema::PopGlobalModuleFragment() {
assert(!ModuleScopes.empty() && getCurrentModule()->isGlobalModule() &&
"left the wrong module scope, which is not global module fragment");
ModuleScopes.pop_back();
}
bool Sema::isModuleUnitOfCurrentTU(const Module *M) const {
assert(M);
Module *CurrentModuleUnit = getCurrentModule();
if (!CurrentModuleUnit)
return false;
return M->isSubModuleOf(CurrentModuleUnit->getTopLevelModule());
}