122989816SDimitry Andric //===--- SemaModule.cpp - Semantic Analysis for Modules -------------------===//
222989816SDimitry Andric //
322989816SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
422989816SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
522989816SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
622989816SDimitry Andric //
722989816SDimitry Andric //===----------------------------------------------------------------------===//
822989816SDimitry Andric //
922989816SDimitry Andric // This file implements semantic analysis for modules (C++ modules syntax,
1022989816SDimitry Andric // Objective-C modules syntax, and Clang header modules).
1122989816SDimitry Andric //
1222989816SDimitry Andric //===----------------------------------------------------------------------===//
1322989816SDimitry Andric
1422989816SDimitry Andric #include "clang/AST/ASTConsumer.h"
15ac9a064cSDimitry Andric #include "clang/AST/ASTMutationListener.h"
1622989816SDimitry Andric #include "clang/Lex/HeaderSearch.h"
1722989816SDimitry Andric #include "clang/Lex/Preprocessor.h"
1822989816SDimitry Andric #include "clang/Sema/SemaInternal.h"
197fa27ce4SDimitry Andric #include "llvm/ADT/StringExtras.h"
20e3b55780SDimitry Andric #include <optional>
2122989816SDimitry Andric
2222989816SDimitry Andric using namespace clang;
2322989816SDimitry Andric using namespace sema;
2422989816SDimitry Andric
checkModuleImportContext(Sema & S,Module * M,SourceLocation ImportLoc,DeclContext * DC,bool FromInclude=false)2522989816SDimitry Andric static void checkModuleImportContext(Sema &S, Module *M,
2622989816SDimitry Andric SourceLocation ImportLoc, DeclContext *DC,
2722989816SDimitry Andric bool FromInclude = false) {
2822989816SDimitry Andric SourceLocation ExternCLoc;
2922989816SDimitry Andric
3022989816SDimitry Andric if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) {
3122989816SDimitry Andric switch (LSD->getLanguage()) {
32b1c73532SDimitry Andric case LinkageSpecLanguageIDs::C:
3322989816SDimitry Andric if (ExternCLoc.isInvalid())
3422989816SDimitry Andric ExternCLoc = LSD->getBeginLoc();
3522989816SDimitry Andric break;
36b1c73532SDimitry Andric case LinkageSpecLanguageIDs::CXX:
3722989816SDimitry Andric break;
3822989816SDimitry Andric }
3922989816SDimitry Andric DC = LSD->getParent();
4022989816SDimitry Andric }
4122989816SDimitry Andric
4222989816SDimitry Andric while (isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC))
4322989816SDimitry Andric DC = DC->getParent();
4422989816SDimitry Andric
4522989816SDimitry Andric if (!isa<TranslationUnitDecl>(DC)) {
4622989816SDimitry Andric S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M))
4722989816SDimitry Andric ? diag::ext_module_import_not_at_top_level_noop
4822989816SDimitry Andric : diag::err_module_import_not_at_top_level_fatal)
4922989816SDimitry Andric << M->getFullModuleName() << DC;
5022989816SDimitry Andric S.Diag(cast<Decl>(DC)->getBeginLoc(),
5122989816SDimitry Andric diag::note_module_import_not_at_top_level)
5222989816SDimitry Andric << DC;
5322989816SDimitry Andric } else if (!M->IsExternC && ExternCLoc.isValid()) {
5422989816SDimitry Andric S.Diag(ImportLoc, diag::ext_module_import_in_extern_c)
5522989816SDimitry Andric << M->getFullModuleName();
5622989816SDimitry Andric S.Diag(ExternCLoc, diag::note_extern_c_begins_here);
5722989816SDimitry Andric }
5822989816SDimitry Andric }
5922989816SDimitry Andric
60145449b1SDimitry Andric // We represent the primary and partition names as 'Paths' which are sections
61145449b1SDimitry Andric // of the hierarchical access path for a clang module. However for C++20
62145449b1SDimitry Andric // the periods in a name are just another character, and we will need to
63145449b1SDimitry Andric // flatten them into a string.
stringFromPath(ModuleIdPath Path)64145449b1SDimitry Andric static std::string stringFromPath(ModuleIdPath Path) {
65145449b1SDimitry Andric std::string Name;
66145449b1SDimitry Andric if (Path.empty())
67145449b1SDimitry Andric return Name;
68145449b1SDimitry Andric
69145449b1SDimitry Andric for (auto &Piece : Path) {
70145449b1SDimitry Andric if (!Name.empty())
71145449b1SDimitry Andric Name += ".";
72145449b1SDimitry Andric Name += Piece.first->getName();
73145449b1SDimitry Andric }
74145449b1SDimitry Andric return Name;
75145449b1SDimitry Andric }
76145449b1SDimitry Andric
77ac9a064cSDimitry Andric /// Helper function for makeTransitiveImportsVisible to decide whether
78ac9a064cSDimitry Andric /// the \param Imported module unit is in the same module with the \param
79ac9a064cSDimitry Andric /// CurrentModule.
80ac9a064cSDimitry Andric /// \param FoundPrimaryModuleInterface is a helper parameter to record the
81ac9a064cSDimitry Andric /// primary module interface unit corresponding to the module \param
82ac9a064cSDimitry Andric /// CurrentModule. Since currently it is expensive to decide whether two module
83ac9a064cSDimitry Andric /// units come from the same module by comparing the module name.
84ac9a064cSDimitry Andric static bool
isImportingModuleUnitFromSameModule(ASTContext & Ctx,Module * Imported,Module * CurrentModule,Module * & FoundPrimaryModuleInterface)85ac9a064cSDimitry Andric isImportingModuleUnitFromSameModule(ASTContext &Ctx, Module *Imported,
86ac9a064cSDimitry Andric Module *CurrentModule,
87ac9a064cSDimitry Andric Module *&FoundPrimaryModuleInterface) {
88ac9a064cSDimitry Andric if (!Imported->isNamedModule())
89ac9a064cSDimitry Andric return false;
90ac9a064cSDimitry Andric
91ac9a064cSDimitry Andric // The a partition unit we're importing must be in the same module of the
92ac9a064cSDimitry Andric // current module.
93ac9a064cSDimitry Andric if (Imported->isModulePartition())
94ac9a064cSDimitry Andric return true;
95ac9a064cSDimitry Andric
96ac9a064cSDimitry Andric // If we found the primary module interface during the search process, we can
97ac9a064cSDimitry Andric // return quickly to avoid expensive string comparison.
98ac9a064cSDimitry Andric if (FoundPrimaryModuleInterface)
99ac9a064cSDimitry Andric return Imported == FoundPrimaryModuleInterface;
100ac9a064cSDimitry Andric
101ac9a064cSDimitry Andric if (!CurrentModule)
102ac9a064cSDimitry Andric return false;
103ac9a064cSDimitry Andric
104ac9a064cSDimitry Andric // Then the imported module must be a primary module interface unit. It
105ac9a064cSDimitry Andric // is only allowed to import the primary module interface unit from the same
106ac9a064cSDimitry Andric // module in the implementation unit and the implementation partition unit.
107ac9a064cSDimitry Andric
108ac9a064cSDimitry Andric // Since we'll handle implementation unit above. We can only care
109ac9a064cSDimitry Andric // about the implementation partition unit here.
110ac9a064cSDimitry Andric if (!CurrentModule->isModulePartitionImplementation())
111ac9a064cSDimitry Andric return false;
112ac9a064cSDimitry Andric
113ac9a064cSDimitry Andric if (Ctx.isInSameModule(Imported, CurrentModule)) {
114ac9a064cSDimitry Andric assert(!FoundPrimaryModuleInterface ||
115ac9a064cSDimitry Andric FoundPrimaryModuleInterface == Imported);
116ac9a064cSDimitry Andric FoundPrimaryModuleInterface = Imported;
117ac9a064cSDimitry Andric return true;
118ac9a064cSDimitry Andric }
119ac9a064cSDimitry Andric
120ac9a064cSDimitry Andric return false;
121ac9a064cSDimitry Andric }
122ac9a064cSDimitry Andric
123ac9a064cSDimitry Andric /// [module.import]p7:
124ac9a064cSDimitry Andric /// Additionally, when a module-import-declaration in a module unit of some
125ac9a064cSDimitry Andric /// module M imports another module unit U of M, it also imports all
126ac9a064cSDimitry Andric /// translation units imported by non-exported module-import-declarations in
127ac9a064cSDimitry Andric /// the module unit purview of U. These rules can in turn lead to the
128ac9a064cSDimitry Andric /// importation of yet more translation units.
129ac9a064cSDimitry Andric static void
makeTransitiveImportsVisible(ASTContext & Ctx,VisibleModuleSet & VisibleModules,Module * Imported,Module * CurrentModule,SourceLocation ImportLoc,bool IsImportingPrimaryModuleInterface=false)130ac9a064cSDimitry Andric makeTransitiveImportsVisible(ASTContext &Ctx, VisibleModuleSet &VisibleModules,
131ac9a064cSDimitry Andric Module *Imported, Module *CurrentModule,
132ac9a064cSDimitry Andric SourceLocation ImportLoc,
133ac9a064cSDimitry Andric bool IsImportingPrimaryModuleInterface = false) {
134ac9a064cSDimitry Andric assert(Imported->isNamedModule() &&
135ac9a064cSDimitry Andric "'makeTransitiveImportsVisible()' is intended for standard C++ named "
136ac9a064cSDimitry Andric "modules only.");
137ac9a064cSDimitry Andric
138ac9a064cSDimitry Andric llvm::SmallVector<Module *, 4> Worklist;
139ac9a064cSDimitry Andric Worklist.push_back(Imported);
140ac9a064cSDimitry Andric
141ac9a064cSDimitry Andric Module *FoundPrimaryModuleInterface =
142ac9a064cSDimitry Andric IsImportingPrimaryModuleInterface ? Imported : nullptr;
143ac9a064cSDimitry Andric
144ac9a064cSDimitry Andric while (!Worklist.empty()) {
145ac9a064cSDimitry Andric Module *Importing = Worklist.pop_back_val();
146ac9a064cSDimitry Andric
147ac9a064cSDimitry Andric if (VisibleModules.isVisible(Importing))
148ac9a064cSDimitry Andric continue;
149ac9a064cSDimitry Andric
150ac9a064cSDimitry Andric // FIXME: The ImportLoc here is not meaningful. It may be problematic if we
151ac9a064cSDimitry Andric // use the sourcelocation loaded from the visible modules.
152ac9a064cSDimitry Andric VisibleModules.setVisible(Importing, ImportLoc);
153ac9a064cSDimitry Andric
154ac9a064cSDimitry Andric if (isImportingModuleUnitFromSameModule(Ctx, Importing, CurrentModule,
155ac9a064cSDimitry Andric FoundPrimaryModuleInterface))
156ac9a064cSDimitry Andric for (Module *TransImported : Importing->Imports)
157ac9a064cSDimitry Andric if (!VisibleModules.isVisible(TransImported))
158ac9a064cSDimitry Andric Worklist.push_back(TransImported);
159ac9a064cSDimitry Andric }
160ac9a064cSDimitry Andric }
161ac9a064cSDimitry Andric
16222989816SDimitry Andric Sema::DeclGroupPtrTy
ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc)16322989816SDimitry Andric Sema::ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc) {
1647fa27ce4SDimitry Andric // We start in the global module;
16577fc4c14SDimitry Andric Module *GlobalModule =
1667fa27ce4SDimitry Andric PushGlobalModuleFragment(ModuleLoc);
16722989816SDimitry Andric
16822989816SDimitry Andric // All declarations created from now on are owned by the global module.
16922989816SDimitry Andric auto *TU = Context.getTranslationUnitDecl();
170145449b1SDimitry Andric // [module.global.frag]p2
171145449b1SDimitry Andric // A global-module-fragment specifies the contents of the global module
172145449b1SDimitry Andric // fragment for a module unit. The global module fragment can be used to
173145449b1SDimitry Andric // provide declarations that are attached to the global module and usable
174145449b1SDimitry Andric // within the module unit.
175145449b1SDimitry Andric //
176145449b1SDimitry Andric // So the declations in the global module shouldn't be visible by default.
177145449b1SDimitry Andric TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported);
17822989816SDimitry Andric TU->setLocalOwningModule(GlobalModule);
17922989816SDimitry Andric
18022989816SDimitry Andric // FIXME: Consider creating an explicit representation of this declaration.
18122989816SDimitry Andric return nullptr;
18222989816SDimitry Andric }
18322989816SDimitry Andric
HandleStartOfHeaderUnit()184145449b1SDimitry Andric void Sema::HandleStartOfHeaderUnit() {
185145449b1SDimitry Andric assert(getLangOpts().CPlusPlusModules &&
186145449b1SDimitry Andric "Header units are only valid for C++20 modules");
187145449b1SDimitry Andric SourceLocation StartOfTU =
188145449b1SDimitry Andric SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
189145449b1SDimitry Andric
190145449b1SDimitry Andric StringRef HUName = getLangOpts().CurrentModule;
191145449b1SDimitry Andric if (HUName.empty()) {
192b1c73532SDimitry Andric HUName =
193b1c73532SDimitry Andric SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID())->getName();
194145449b1SDimitry Andric const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str();
195145449b1SDimitry Andric }
196145449b1SDimitry Andric
197145449b1SDimitry Andric // TODO: Make the C++20 header lookup independent.
198145449b1SDimitry Andric // When the input is pre-processed source, we need a file ref to the original
199145449b1SDimitry Andric // file for the header map.
200e3b55780SDimitry Andric auto F = SourceMgr.getFileManager().getOptionalFileRef(HUName);
201145449b1SDimitry Andric // For the sake of error recovery (if someone has moved the original header
202145449b1SDimitry Andric // after creating the pre-processed output) fall back to obtaining the file
203145449b1SDimitry Andric // ref for the input file, which must be present.
204145449b1SDimitry Andric if (!F)
205e3b55780SDimitry Andric F = SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID());
206145449b1SDimitry Andric assert(F && "failed to find the header unit source?");
207145449b1SDimitry Andric Module::Header H{HUName.str(), HUName.str(), *F};
208145449b1SDimitry Andric auto &Map = PP.getHeaderSearchInfo().getModuleMap();
209145449b1SDimitry Andric Module *Mod = Map.createHeaderUnit(StartOfTU, HUName, H);
210145449b1SDimitry Andric assert(Mod && "module creation should not fail");
211145449b1SDimitry Andric ModuleScopes.push_back({}); // No GMF
212145449b1SDimitry Andric ModuleScopes.back().BeginLoc = StartOfTU;
213145449b1SDimitry Andric ModuleScopes.back().Module = Mod;
214145449b1SDimitry Andric VisibleModules.setVisible(Mod, StartOfTU);
215145449b1SDimitry Andric
216145449b1SDimitry Andric // From now on, we have an owning module for all declarations we see.
217145449b1SDimitry Andric // All of these are implicitly exported.
218145449b1SDimitry Andric auto *TU = Context.getTranslationUnitDecl();
219145449b1SDimitry Andric TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::Visible);
220145449b1SDimitry Andric TU->setLocalOwningModule(Mod);
221145449b1SDimitry Andric }
222145449b1SDimitry Andric
223e3b55780SDimitry Andric /// Tests whether the given identifier is reserved as a module name and
224e3b55780SDimitry Andric /// diagnoses if it is. Returns true if a diagnostic is emitted and false
225e3b55780SDimitry Andric /// otherwise.
DiagReservedModuleName(Sema & S,const IdentifierInfo * II,SourceLocation Loc)226e3b55780SDimitry Andric static bool DiagReservedModuleName(Sema &S, const IdentifierInfo *II,
227e3b55780SDimitry Andric SourceLocation Loc) {
228e3b55780SDimitry Andric enum {
229e3b55780SDimitry Andric Valid = -1,
230e3b55780SDimitry Andric Invalid = 0,
231e3b55780SDimitry Andric Reserved = 1,
232e3b55780SDimitry Andric } Reason = Valid;
233e3b55780SDimitry Andric
234e3b55780SDimitry Andric if (II->isStr("module") || II->isStr("import"))
235e3b55780SDimitry Andric Reason = Invalid;
236e3b55780SDimitry Andric else if (II->isReserved(S.getLangOpts()) !=
237e3b55780SDimitry Andric ReservedIdentifierStatus::NotReserved)
238e3b55780SDimitry Andric Reason = Reserved;
239e3b55780SDimitry Andric
240e3b55780SDimitry Andric // If the identifier is reserved (not invalid) but is in a system header,
241e3b55780SDimitry Andric // we do not diagnose (because we expect system headers to use reserved
242e3b55780SDimitry Andric // identifiers).
243e3b55780SDimitry Andric if (Reason == Reserved && S.getSourceManager().isInSystemHeader(Loc))
244e3b55780SDimitry Andric Reason = Valid;
245e3b55780SDimitry Andric
2467fa27ce4SDimitry Andric switch (Reason) {
2477fa27ce4SDimitry Andric case Valid:
248e3b55780SDimitry Andric return false;
2497fa27ce4SDimitry Andric case Invalid:
2507fa27ce4SDimitry Andric return S.Diag(Loc, diag::err_invalid_module_name) << II;
2517fa27ce4SDimitry Andric case Reserved:
2527fa27ce4SDimitry Andric S.Diag(Loc, diag::warn_reserved_module_name) << II;
2537fa27ce4SDimitry Andric return false;
2547fa27ce4SDimitry Andric }
2557fa27ce4SDimitry Andric llvm_unreachable("fell off a fully covered switch");
256e3b55780SDimitry Andric }
257e3b55780SDimitry Andric
25822989816SDimitry Andric Sema::DeclGroupPtrTy
ActOnModuleDecl(SourceLocation StartLoc,SourceLocation ModuleLoc,ModuleDeclKind MDK,ModuleIdPath Path,ModuleIdPath Partition,ModuleImportState & ImportState)25922989816SDimitry Andric Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
260145449b1SDimitry Andric ModuleDeclKind MDK, ModuleIdPath Path,
261145449b1SDimitry Andric ModuleIdPath Partition, ModuleImportState &ImportState) {
2627fa27ce4SDimitry Andric assert(getLangOpts().CPlusPlusModules &&
2637fa27ce4SDimitry Andric "should only have module decl in standard C++ modules");
26422989816SDimitry Andric
265145449b1SDimitry Andric bool IsFirstDecl = ImportState == ModuleImportState::FirstDecl;
266145449b1SDimitry Andric bool SeenGMF = ImportState == ModuleImportState::GlobalFragment;
267145449b1SDimitry Andric // If any of the steps here fail, we count that as invalidating C++20
268145449b1SDimitry Andric // module state;
269145449b1SDimitry Andric ImportState = ModuleImportState::NotACXX20Module;
270145449b1SDimitry Andric
271145449b1SDimitry Andric bool IsPartition = !Partition.empty();
272145449b1SDimitry Andric if (IsPartition)
273145449b1SDimitry Andric switch (MDK) {
274145449b1SDimitry Andric case ModuleDeclKind::Implementation:
275145449b1SDimitry Andric MDK = ModuleDeclKind::PartitionImplementation;
276145449b1SDimitry Andric break;
277145449b1SDimitry Andric case ModuleDeclKind::Interface:
278145449b1SDimitry Andric MDK = ModuleDeclKind::PartitionInterface;
279145449b1SDimitry Andric break;
280145449b1SDimitry Andric default:
281145449b1SDimitry Andric llvm_unreachable("how did we get a partition type set?");
282145449b1SDimitry Andric }
283145449b1SDimitry Andric
284145449b1SDimitry Andric // A (non-partition) module implementation unit requires that we are not
285145449b1SDimitry Andric // compiling a module of any kind. A partition implementation emits an
286145449b1SDimitry Andric // interface (and the AST for the implementation), which will subsequently
287145449b1SDimitry Andric // be consumed to emit a binary.
288145449b1SDimitry Andric // A module interface unit requires that we are not compiling a module map.
28922989816SDimitry Andric switch (getLangOpts().getCompilingModule()) {
29022989816SDimitry Andric case LangOptions::CMK_None:
29122989816SDimitry Andric // It's OK to compile a module interface as a normal translation unit.
29222989816SDimitry Andric break;
29322989816SDimitry Andric
29422989816SDimitry Andric case LangOptions::CMK_ModuleInterface:
29522989816SDimitry Andric if (MDK != ModuleDeclKind::Implementation)
29622989816SDimitry Andric break;
29722989816SDimitry Andric
29822989816SDimitry Andric // We were asked to compile a module interface unit but this is a module
299145449b1SDimitry Andric // implementation unit.
30022989816SDimitry Andric Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch)
30122989816SDimitry Andric << FixItHint::CreateInsertion(ModuleLoc, "export ");
30222989816SDimitry Andric MDK = ModuleDeclKind::Interface;
30322989816SDimitry Andric break;
30422989816SDimitry Andric
30522989816SDimitry Andric case LangOptions::CMK_ModuleMap:
30622989816SDimitry Andric Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
30722989816SDimitry Andric return nullptr;
30822989816SDimitry Andric
309145449b1SDimitry Andric case LangOptions::CMK_HeaderUnit:
310e3b55780SDimitry Andric Diag(ModuleLoc, diag::err_module_decl_in_header_unit);
31122989816SDimitry Andric return nullptr;
31222989816SDimitry Andric }
31322989816SDimitry Andric
31422989816SDimitry Andric assert(ModuleScopes.size() <= 1 && "expected to be at global module scope");
31522989816SDimitry Andric
31622989816SDimitry Andric // FIXME: Most of this work should be done by the preprocessor rather than
31722989816SDimitry Andric // here, in order to support macro import.
31822989816SDimitry Andric
31922989816SDimitry Andric // Only one module-declaration is permitted per source file.
320e3b55780SDimitry Andric if (isCurrentModulePurview()) {
32122989816SDimitry Andric Diag(ModuleLoc, diag::err_module_redeclaration);
32222989816SDimitry Andric Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
32322989816SDimitry Andric diag::note_prev_module_declaration);
32422989816SDimitry Andric return nullptr;
32522989816SDimitry Andric }
32622989816SDimitry Andric
3277fa27ce4SDimitry Andric assert((!getLangOpts().CPlusPlusModules ||
3287fa27ce4SDimitry Andric SeenGMF == (bool)this->TheGlobalModuleFragment) &&
329145449b1SDimitry Andric "mismatched global module state");
330145449b1SDimitry Andric
33122989816SDimitry Andric // In C++20, the module-declaration must be the first declaration if there
33222989816SDimitry Andric // is no global module fragment.
333145449b1SDimitry Andric if (getLangOpts().CPlusPlusModules && !IsFirstDecl && !SeenGMF) {
33422989816SDimitry Andric Diag(ModuleLoc, diag::err_module_decl_not_at_start);
33522989816SDimitry Andric SourceLocation BeginLoc =
33622989816SDimitry Andric ModuleScopes.empty()
33722989816SDimitry Andric ? SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID())
33822989816SDimitry Andric : ModuleScopes.back().BeginLoc;
33922989816SDimitry Andric if (BeginLoc.isValid()) {
34022989816SDimitry Andric Diag(BeginLoc, diag::note_global_module_introducer_missing)
34122989816SDimitry Andric << FixItHint::CreateInsertion(BeginLoc, "module;\n");
34222989816SDimitry Andric }
34322989816SDimitry Andric }
34422989816SDimitry Andric
3457fa27ce4SDimitry Andric // C++23 [module.unit]p1: ... The identifiers module and import shall not
346e3b55780SDimitry Andric // appear as identifiers in a module-name or module-partition. All
347e3b55780SDimitry Andric // module-names either beginning with an identifier consisting of std
348e3b55780SDimitry Andric // followed by zero or more digits or containing a reserved identifier
349e3b55780SDimitry Andric // ([lex.name]) are reserved and shall not be specified in a
350e3b55780SDimitry Andric // module-declaration; no diagnostic is required.
351e3b55780SDimitry Andric
352e3b55780SDimitry Andric // Test the first part of the path to see if it's std[0-9]+ but allow the
353e3b55780SDimitry Andric // name in a system header.
354e3b55780SDimitry Andric StringRef FirstComponentName = Path[0].first->getName();
355e3b55780SDimitry Andric if (!getSourceManager().isInSystemHeader(Path[0].second) &&
356e3b55780SDimitry Andric (FirstComponentName == "std" ||
357312c0ed1SDimitry Andric (FirstComponentName.starts_with("std") &&
3587fa27ce4SDimitry Andric llvm::all_of(FirstComponentName.drop_front(3), &llvm::isDigit))))
3597fa27ce4SDimitry Andric Diag(Path[0].second, diag::warn_reserved_module_name) << Path[0].first;
360e3b55780SDimitry Andric
361e3b55780SDimitry Andric // Then test all of the components in the path to see if any of them are
362e3b55780SDimitry Andric // using another kind of reserved or invalid identifier.
363e3b55780SDimitry Andric for (auto Part : Path) {
364e3b55780SDimitry Andric if (DiagReservedModuleName(*this, Part.first, Part.second))
365e3b55780SDimitry Andric return nullptr;
366e3b55780SDimitry Andric }
367e3b55780SDimitry Andric
36822989816SDimitry Andric // Flatten the dots in a module name. Unlike Clang's hierarchical module map
36922989816SDimitry Andric // modules, the dots here are just another character that can appear in a
37022989816SDimitry Andric // module name.
371145449b1SDimitry Andric std::string ModuleName = stringFromPath(Path);
372145449b1SDimitry Andric if (IsPartition) {
373145449b1SDimitry Andric ModuleName += ":";
374145449b1SDimitry Andric ModuleName += stringFromPath(Partition);
37522989816SDimitry Andric }
37622989816SDimitry Andric // If a module name was explicitly specified on the command line, it must be
37722989816SDimitry Andric // correct.
37822989816SDimitry Andric if (!getLangOpts().CurrentModule.empty() &&
37922989816SDimitry Andric getLangOpts().CurrentModule != ModuleName) {
38022989816SDimitry Andric Diag(Path.front().second, diag::err_current_module_name_mismatch)
381145449b1SDimitry Andric << SourceRange(Path.front().second, IsPartition
382145449b1SDimitry Andric ? Partition.back().second
383145449b1SDimitry Andric : Path.back().second)
38422989816SDimitry Andric << getLangOpts().CurrentModule;
38522989816SDimitry Andric return nullptr;
38622989816SDimitry Andric }
38722989816SDimitry Andric const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
38822989816SDimitry Andric
38922989816SDimitry Andric auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3907fa27ce4SDimitry Andric Module *Mod; // The module we are creating.
3917fa27ce4SDimitry Andric Module *Interface = nullptr; // The interface for an implementation.
39222989816SDimitry Andric switch (MDK) {
393145449b1SDimitry Andric case ModuleDeclKind::Interface:
394145449b1SDimitry Andric case ModuleDeclKind::PartitionInterface: {
39522989816SDimitry Andric // We can't have parsed or imported a definition of this module or parsed a
39622989816SDimitry Andric // module map defining it already.
39722989816SDimitry Andric if (auto *M = Map.findModule(ModuleName)) {
39822989816SDimitry Andric Diag(Path[0].second, diag::err_module_redefinition) << ModuleName;
39922989816SDimitry Andric if (M->DefinitionLoc.isValid())
40022989816SDimitry Andric Diag(M->DefinitionLoc, diag::note_prev_module_definition);
401e3b55780SDimitry Andric else if (OptionalFileEntryRef FE = M->getASTFile())
40222989816SDimitry Andric Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
40322989816SDimitry Andric << FE->getName();
40422989816SDimitry Andric Mod = M;
40522989816SDimitry Andric break;
40622989816SDimitry Andric }
40722989816SDimitry Andric
40822989816SDimitry Andric // Create a Module for the module that we're defining.
409e3b55780SDimitry Andric Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
410145449b1SDimitry Andric if (MDK == ModuleDeclKind::PartitionInterface)
411145449b1SDimitry Andric Mod->Kind = Module::ModulePartitionInterface;
41222989816SDimitry Andric assert(Mod && "module creation should not fail");
41322989816SDimitry Andric break;
41422989816SDimitry Andric }
41522989816SDimitry Andric
416145449b1SDimitry Andric case ModuleDeclKind::Implementation: {
417145449b1SDimitry Andric // C++20 A module-declaration that contains neither an export-
418145449b1SDimitry Andric // keyword nor a module-partition implicitly imports the primary
419145449b1SDimitry Andric // module interface unit of the module as if by a module-import-
420145449b1SDimitry Andric // declaration.
4217fa27ce4SDimitry Andric std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc(
4227fa27ce4SDimitry Andric PP.getIdentifierInfo(ModuleName), Path[0].second);
4237fa27ce4SDimitry Andric
4247fa27ce4SDimitry Andric // The module loader will assume we're trying to import the module that
4257fa27ce4SDimitry Andric // we're building if `LangOpts.CurrentModule` equals to 'ModuleName'.
4267fa27ce4SDimitry Andric // Change the value for `LangOpts.CurrentModule` temporarily to make the
4277fa27ce4SDimitry Andric // module loader work properly.
4287fa27ce4SDimitry Andric const_cast<LangOptions &>(getLangOpts()).CurrentModule = "";
4297fa27ce4SDimitry Andric Interface = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
43022989816SDimitry Andric Module::AllVisible,
43122989816SDimitry Andric /*IsInclusionDirective=*/false);
4327fa27ce4SDimitry Andric const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
4337fa27ce4SDimitry Andric
4347fa27ce4SDimitry Andric if (!Interface) {
43522989816SDimitry Andric Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
43622989816SDimitry Andric // Create an empty module interface unit for error recovery.
437e3b55780SDimitry Andric Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
4387fa27ce4SDimitry Andric } else {
4397fa27ce4SDimitry Andric Mod = Map.createModuleForImplementationUnit(ModuleLoc, ModuleName);
44022989816SDimitry Andric }
441145449b1SDimitry Andric } break;
442145449b1SDimitry Andric
443145449b1SDimitry Andric case ModuleDeclKind::PartitionImplementation:
444145449b1SDimitry Andric // Create an interface, but note that it is an implementation
445145449b1SDimitry Andric // unit.
446e3b55780SDimitry Andric Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
447145449b1SDimitry Andric Mod->Kind = Module::ModulePartitionImplementation;
44822989816SDimitry Andric break;
44922989816SDimitry Andric }
45022989816SDimitry Andric
4517fa27ce4SDimitry Andric if (!this->TheGlobalModuleFragment) {
45222989816SDimitry Andric ModuleScopes.push_back({});
45322989816SDimitry Andric if (getLangOpts().ModulesLocalVisibility)
45422989816SDimitry Andric ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
45522989816SDimitry Andric } else {
45622989816SDimitry Andric // We're done with the global module fragment now.
45722989816SDimitry Andric ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global);
45822989816SDimitry Andric }
45922989816SDimitry Andric
46022989816SDimitry Andric // Switch from the global module fragment (if any) to the named module.
46122989816SDimitry Andric ModuleScopes.back().BeginLoc = StartLoc;
46222989816SDimitry Andric ModuleScopes.back().Module = Mod;
46322989816SDimitry Andric VisibleModules.setVisible(Mod, ModuleLoc);
46422989816SDimitry Andric
46522989816SDimitry Andric // From now on, we have an owning module for all declarations we see.
466145449b1SDimitry Andric // In C++20 modules, those declaration would be reachable when imported
467145449b1SDimitry Andric // unless explicitily exported.
468145449b1SDimitry Andric // Otherwise, those declarations are module-private unless explicitly
46922989816SDimitry Andric // exported.
47022989816SDimitry Andric auto *TU = Context.getTranslationUnitDecl();
471145449b1SDimitry Andric TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported);
47222989816SDimitry Andric TU->setLocalOwningModule(Mod);
47322989816SDimitry Andric
474145449b1SDimitry Andric // We are in the module purview, but before any other (non import)
475145449b1SDimitry Andric // statements, so imports are allowed.
476145449b1SDimitry Andric ImportState = ModuleImportState::ImportAllowed;
477145449b1SDimitry Andric
4787fa27ce4SDimitry Andric getASTContext().setCurrentNamedModule(Mod);
4797fa27ce4SDimitry Andric
480ac9a064cSDimitry Andric if (auto *Listener = getASTMutationListener())
481ac9a064cSDimitry Andric Listener->EnteringModulePurview();
482ac9a064cSDimitry Andric
4837fa27ce4SDimitry Andric // We already potentially made an implicit import (in the case of a module
4847fa27ce4SDimitry Andric // implementation unit importing its interface). Make this module visible
4857fa27ce4SDimitry Andric // and return the import decl to be added to the current TU.
4867fa27ce4SDimitry Andric if (Interface) {
4877fa27ce4SDimitry Andric
488ac9a064cSDimitry Andric makeTransitiveImportsVisible(getASTContext(), VisibleModules, Interface,
489ac9a064cSDimitry Andric Mod, ModuleLoc,
490ac9a064cSDimitry Andric /*IsImportingPrimaryModuleInterface=*/true);
4917fa27ce4SDimitry Andric
4927fa27ce4SDimitry Andric // Make the import decl for the interface in the impl module.
4937fa27ce4SDimitry Andric ImportDecl *Import = ImportDecl::Create(Context, CurContext, ModuleLoc,
4947fa27ce4SDimitry Andric Interface, Path[0].second);
4957fa27ce4SDimitry Andric CurContext->addDecl(Import);
4967fa27ce4SDimitry Andric
4977fa27ce4SDimitry Andric // Sequence initialization of the imported module before that of the current
4987fa27ce4SDimitry Andric // module, if any.
4997fa27ce4SDimitry Andric Context.addModuleInitializer(ModuleScopes.back().Module, Import);
5007fa27ce4SDimitry Andric Mod->Imports.insert(Interface); // As if we imported it.
5017fa27ce4SDimitry Andric // Also save this as a shortcut to checking for decls in the interface
5027fa27ce4SDimitry Andric ThePrimaryInterface = Interface;
5037fa27ce4SDimitry Andric // If we made an implicit import of the module interface, then return the
5047fa27ce4SDimitry Andric // imported module decl.
5054b4fe385SDimitry Andric return ConvertDeclToDeclGroup(Import);
5064b4fe385SDimitry Andric }
5074b4fe385SDimitry Andric
50822989816SDimitry Andric return nullptr;
50922989816SDimitry Andric }
51022989816SDimitry Andric
51122989816SDimitry Andric Sema::DeclGroupPtrTy
ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,SourceLocation PrivateLoc)51222989816SDimitry Andric Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,
51322989816SDimitry Andric SourceLocation PrivateLoc) {
51422989816SDimitry Andric // C++20 [basic.link]/2:
51522989816SDimitry Andric // A private-module-fragment shall appear only in a primary module
51622989816SDimitry Andric // interface unit.
5177fa27ce4SDimitry Andric switch (ModuleScopes.empty() ? Module::ExplicitGlobalModuleFragment
51822989816SDimitry Andric : ModuleScopes.back().Module->Kind) {
51922989816SDimitry Andric case Module::ModuleMapModule:
5207fa27ce4SDimitry Andric case Module::ExplicitGlobalModuleFragment:
5217fa27ce4SDimitry Andric case Module::ImplicitGlobalModuleFragment:
522145449b1SDimitry Andric case Module::ModulePartitionImplementation:
523145449b1SDimitry Andric case Module::ModulePartitionInterface:
524145449b1SDimitry Andric case Module::ModuleHeaderUnit:
52522989816SDimitry Andric Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
52622989816SDimitry Andric return nullptr;
52722989816SDimitry Andric
52822989816SDimitry Andric case Module::PrivateModuleFragment:
52922989816SDimitry Andric Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
53022989816SDimitry Andric Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
53122989816SDimitry Andric return nullptr;
53222989816SDimitry Andric
5337fa27ce4SDimitry Andric case Module::ModuleImplementationUnit:
53422989816SDimitry Andric Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface);
53522989816SDimitry Andric Diag(ModuleScopes.back().BeginLoc,
53622989816SDimitry Andric diag::note_not_module_interface_add_export)
53722989816SDimitry Andric << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
53822989816SDimitry Andric return nullptr;
5397fa27ce4SDimitry Andric
5407fa27ce4SDimitry Andric case Module::ModuleInterfaceUnit:
5417fa27ce4SDimitry Andric break;
54222989816SDimitry Andric }
54322989816SDimitry Andric
54422989816SDimitry Andric // FIXME: Check that this translation unit does not import any partitions;
54522989816SDimitry Andric // such imports would violate [basic.link]/2's "shall be the only module unit"
54622989816SDimitry Andric // restriction.
54722989816SDimitry Andric
54822989816SDimitry Andric // We've finished the public fragment of the translation unit.
54922989816SDimitry Andric ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal);
55022989816SDimitry Andric
55122989816SDimitry Andric auto &Map = PP.getHeaderSearchInfo().getModuleMap();
55222989816SDimitry Andric Module *PrivateModuleFragment =
55322989816SDimitry Andric Map.createPrivateModuleFragmentForInterfaceUnit(
55422989816SDimitry Andric ModuleScopes.back().Module, PrivateLoc);
55522989816SDimitry Andric assert(PrivateModuleFragment && "module creation should not fail");
55622989816SDimitry Andric
55722989816SDimitry Andric // Enter the scope of the private module fragment.
55822989816SDimitry Andric ModuleScopes.push_back({});
55922989816SDimitry Andric ModuleScopes.back().BeginLoc = ModuleLoc;
56022989816SDimitry Andric ModuleScopes.back().Module = PrivateModuleFragment;
56122989816SDimitry Andric VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
56222989816SDimitry Andric
56322989816SDimitry Andric // All declarations created from now on are scoped to the private module
56422989816SDimitry Andric // fragment (and are neither visible nor reachable in importers of the module
56522989816SDimitry Andric // interface).
56622989816SDimitry Andric auto *TU = Context.getTranslationUnitDecl();
56722989816SDimitry Andric TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
56822989816SDimitry Andric TU->setLocalOwningModule(PrivateModuleFragment);
56922989816SDimitry Andric
57022989816SDimitry Andric // FIXME: Consider creating an explicit representation of this declaration.
57122989816SDimitry Andric return nullptr;
57222989816SDimitry Andric }
57322989816SDimitry Andric
ActOnModuleImport(SourceLocation StartLoc,SourceLocation ExportLoc,SourceLocation ImportLoc,ModuleIdPath Path,bool IsPartition)57422989816SDimitry Andric DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
57522989816SDimitry Andric SourceLocation ExportLoc,
576145449b1SDimitry Andric SourceLocation ImportLoc, ModuleIdPath Path,
577145449b1SDimitry Andric bool IsPartition) {
5787fa27ce4SDimitry Andric assert((!IsPartition || getLangOpts().CPlusPlusModules) &&
5797fa27ce4SDimitry Andric "partition seen in non-C++20 code?");
580145449b1SDimitry Andric
581145449b1SDimitry Andric // For a C++20 module name, flatten into a single identifier with the source
582145449b1SDimitry Andric // location of the first component.
58322989816SDimitry Andric std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
584145449b1SDimitry Andric
58522989816SDimitry Andric std::string ModuleName;
586145449b1SDimitry Andric if (IsPartition) {
587145449b1SDimitry Andric // We already checked that we are in a module purview in the parser.
588145449b1SDimitry Andric assert(!ModuleScopes.empty() && "in a module purview, but no module?");
589145449b1SDimitry Andric Module *NamedMod = ModuleScopes.back().Module;
590145449b1SDimitry Andric // If we are importing into a partition, find the owning named module,
591145449b1SDimitry Andric // otherwise, the name of the importing named module.
592145449b1SDimitry Andric ModuleName = NamedMod->getPrimaryModuleInterfaceName().str();
593145449b1SDimitry Andric ModuleName += ":";
594145449b1SDimitry Andric ModuleName += stringFromPath(Path);
595145449b1SDimitry Andric ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
596145449b1SDimitry Andric Path = ModuleIdPath(ModuleNameLoc);
5977fa27ce4SDimitry Andric } else if (getLangOpts().CPlusPlusModules) {
598145449b1SDimitry Andric ModuleName = stringFromPath(Path);
59922989816SDimitry Andric ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
60022989816SDimitry Andric Path = ModuleIdPath(ModuleNameLoc);
60122989816SDimitry Andric }
60222989816SDimitry Andric
603145449b1SDimitry Andric // Diagnose self-import before attempting a load.
604145449b1SDimitry Andric // [module.import]/9
605145449b1SDimitry Andric // A module implementation unit of a module M that is not a module partition
606145449b1SDimitry Andric // shall not contain a module-import-declaration nominating M.
607145449b1SDimitry Andric // (for an implementation, the module interface is imported implicitly,
608145449b1SDimitry Andric // but that's handled in the module decl code).
609145449b1SDimitry Andric
610145449b1SDimitry Andric if (getLangOpts().CPlusPlusModules && isCurrentModulePurview() &&
611145449b1SDimitry Andric getCurrentModule()->Name == ModuleName) {
612145449b1SDimitry Andric Diag(ImportLoc, diag::err_module_self_import_cxx20)
613b1c73532SDimitry Andric << ModuleName << currentModuleIsImplementation();
614145449b1SDimitry Andric return true;
615145449b1SDimitry Andric }
616145449b1SDimitry Andric
617145449b1SDimitry Andric Module *Mod = getModuleLoader().loadModule(
618145449b1SDimitry Andric ImportLoc, Path, Module::AllVisible, /*IsInclusionDirective=*/false);
61922989816SDimitry Andric if (!Mod)
62022989816SDimitry Andric return true;
62122989816SDimitry Andric
62277dbea07SDimitry Andric if (!Mod->isInterfaceOrPartition() && !ModuleName.empty() &&
62377dbea07SDimitry Andric !getLangOpts().ObjC) {
624b1c73532SDimitry Andric Diag(ImportLoc, diag::err_module_import_non_interface_nor_parition)
625b1c73532SDimitry Andric << ModuleName;
626b1c73532SDimitry Andric return true;
627b1c73532SDimitry Andric }
628b1c73532SDimitry Andric
62922989816SDimitry Andric return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
63022989816SDimitry Andric }
63122989816SDimitry Andric
63222989816SDimitry Andric /// Determine whether \p D is lexically within an export-declaration.
getEnclosingExportDecl(const Decl * D)63322989816SDimitry Andric static const ExportDecl *getEnclosingExportDecl(const Decl *D) {
63422989816SDimitry Andric for (auto *DC = D->getLexicalDeclContext(); DC; DC = DC->getLexicalParent())
63522989816SDimitry Andric if (auto *ED = dyn_cast<ExportDecl>(DC))
63622989816SDimitry Andric return ED;
63722989816SDimitry Andric return nullptr;
63822989816SDimitry Andric }
63922989816SDimitry Andric
ActOnModuleImport(SourceLocation StartLoc,SourceLocation ExportLoc,SourceLocation ImportLoc,Module * Mod,ModuleIdPath Path)64022989816SDimitry Andric DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
64122989816SDimitry Andric SourceLocation ExportLoc,
642145449b1SDimitry Andric SourceLocation ImportLoc, Module *Mod,
643145449b1SDimitry Andric ModuleIdPath Path) {
6447fa27ce4SDimitry Andric if (Mod->isHeaderUnit())
6457fa27ce4SDimitry Andric Diag(ImportLoc, diag::warn_experimental_header_unit);
6467fa27ce4SDimitry Andric
647ac9a064cSDimitry Andric if (Mod->isNamedModule())
648ac9a064cSDimitry Andric makeTransitiveImportsVisible(getASTContext(), VisibleModules, Mod,
649ac9a064cSDimitry Andric getCurrentModule(), ImportLoc);
650ac9a064cSDimitry Andric else
65122989816SDimitry Andric VisibleModules.setVisible(Mod, ImportLoc);
65222989816SDimitry Andric
65322989816SDimitry Andric checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
65422989816SDimitry Andric
65522989816SDimitry Andric // FIXME: we should support importing a submodule within a different submodule
65622989816SDimitry Andric // of the same top-level module. Until we do, make it an error rather than
65722989816SDimitry Andric // silently ignoring the import.
658145449b1SDimitry Andric // FIXME: Should we warn on a redundant import of the current module?
6597fa27ce4SDimitry Andric if (Mod->isForBuilding(getLangOpts())) {
66022989816SDimitry Andric Diag(ImportLoc, getLangOpts().isCompilingModule()
66122989816SDimitry Andric ? diag::err_module_self_import
66222989816SDimitry Andric : diag::err_module_import_in_implementation)
66322989816SDimitry Andric << Mod->getFullModuleName() << getLangOpts().CurrentModule;
66422989816SDimitry Andric }
66522989816SDimitry Andric
66622989816SDimitry Andric SmallVector<SourceLocation, 2> IdentifierLocs;
667145449b1SDimitry Andric
668145449b1SDimitry Andric if (Path.empty()) {
669145449b1SDimitry Andric // If this was a header import, pad out with dummy locations.
670145449b1SDimitry Andric // FIXME: Pass in and use the location of the header-name token in this
671145449b1SDimitry Andric // case.
672145449b1SDimitry Andric for (Module *ModCheck = Mod; ModCheck; ModCheck = ModCheck->Parent)
673145449b1SDimitry Andric IdentifierLocs.push_back(SourceLocation());
674145449b1SDimitry Andric } else if (getLangOpts().CPlusPlusModules && !Mod->Parent) {
675145449b1SDimitry Andric // A single identifier for the whole name.
676145449b1SDimitry Andric IdentifierLocs.push_back(Path[0].second);
677145449b1SDimitry Andric } else {
67822989816SDimitry Andric Module *ModCheck = Mod;
67922989816SDimitry Andric for (unsigned I = 0, N = Path.size(); I != N; ++I) {
680145449b1SDimitry Andric // If we've run out of module parents, just drop the remaining
681145449b1SDimitry Andric // identifiers. We need the length to be consistent.
68222989816SDimitry Andric if (!ModCheck)
68322989816SDimitry Andric break;
68422989816SDimitry Andric ModCheck = ModCheck->Parent;
68522989816SDimitry Andric
68622989816SDimitry Andric IdentifierLocs.push_back(Path[I].second);
68722989816SDimitry Andric }
68822989816SDimitry Andric }
68922989816SDimitry Andric
69022989816SDimitry Andric ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc,
69122989816SDimitry Andric Mod, IdentifierLocs);
69222989816SDimitry Andric CurContext->addDecl(Import);
69322989816SDimitry Andric
69422989816SDimitry Andric // Sequence initialization of the imported module before that of the current
69522989816SDimitry Andric // module, if any.
69622989816SDimitry Andric if (!ModuleScopes.empty())
69722989816SDimitry Andric Context.addModuleInitializer(ModuleScopes.back().Module, Import);
69822989816SDimitry Andric
699145449b1SDimitry Andric // A module (partition) implementation unit shall not be exported.
700145449b1SDimitry Andric if (getLangOpts().CPlusPlusModules && ExportLoc.isValid() &&
701145449b1SDimitry Andric Mod->Kind == Module::ModuleKind::ModulePartitionImplementation) {
702145449b1SDimitry Andric Diag(ExportLoc, diag::err_export_partition_impl)
703145449b1SDimitry Andric << SourceRange(ExportLoc, Path.back().second);
704b1c73532SDimitry Andric } else if (!ModuleScopes.empty() && !currentModuleIsImplementation()) {
70577fc4c14SDimitry Andric // Re-export the module if the imported module is exported.
70677fc4c14SDimitry Andric // Note that we don't need to add re-exported module to Imports field
70777fc4c14SDimitry Andric // since `Exports` implies the module is imported already.
70822989816SDimitry Andric if (ExportLoc.isValid() || getEnclosingExportDecl(Import))
70922989816SDimitry Andric getCurrentModule()->Exports.emplace_back(Mod, false);
71077fc4c14SDimitry Andric else
71177fc4c14SDimitry Andric getCurrentModule()->Imports.insert(Mod);
71222989816SDimitry Andric } else if (ExportLoc.isValid()) {
71377fc4c14SDimitry Andric // [module.interface]p1:
71477fc4c14SDimitry Andric // An export-declaration shall inhabit a namespace scope and appear in the
71577fc4c14SDimitry Andric // purview of a module interface unit.
7167fa27ce4SDimitry Andric Diag(ExportLoc, diag::err_export_not_in_module_interface);
71722989816SDimitry Andric }
71822989816SDimitry Andric
71922989816SDimitry Andric return Import;
72022989816SDimitry Andric }
72122989816SDimitry Andric
ActOnAnnotModuleInclude(SourceLocation DirectiveLoc,Module * Mod)722ac9a064cSDimitry Andric void Sema::ActOnAnnotModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
72322989816SDimitry Andric checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
72422989816SDimitry Andric BuildModuleInclude(DirectiveLoc, Mod);
72522989816SDimitry Andric }
72622989816SDimitry Andric
BuildModuleInclude(SourceLocation DirectiveLoc,Module * Mod)72722989816SDimitry Andric void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
72822989816SDimitry Andric // Determine whether we're in the #include buffer for a module. The #includes
72922989816SDimitry Andric // in that buffer do not qualify as module imports; they're just an
73022989816SDimitry Andric // implementation detail of us building the module.
73122989816SDimitry Andric //
732ac9a064cSDimitry Andric // FIXME: Should we even get ActOnAnnotModuleInclude calls for those?
73322989816SDimitry Andric bool IsInModuleIncludes =
734ac9a064cSDimitry Andric TUKind == TU_ClangModule &&
73522989816SDimitry Andric getSourceManager().isWrittenInMainFile(DirectiveLoc);
73622989816SDimitry Andric
7377fa27ce4SDimitry Andric // If we are really importing a module (not just checking layering) due to an
7387fa27ce4SDimitry Andric // #include in the main file, synthesize an ImportDecl.
7397fa27ce4SDimitry Andric if (getLangOpts().Modules && !IsInModuleIncludes) {
74022989816SDimitry Andric TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
74122989816SDimitry Andric ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
74222989816SDimitry Andric DirectiveLoc, Mod,
74322989816SDimitry Andric DirectiveLoc);
74422989816SDimitry Andric if (!ModuleScopes.empty())
74522989816SDimitry Andric Context.addModuleInitializer(ModuleScopes.back().Module, ImportD);
74622989816SDimitry Andric TU->addDecl(ImportD);
74722989816SDimitry Andric Consumer.HandleImplicitImportDecl(ImportD);
74822989816SDimitry Andric }
74922989816SDimitry Andric
75022989816SDimitry Andric getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc);
75122989816SDimitry Andric VisibleModules.setVisible(Mod, DirectiveLoc);
752145449b1SDimitry Andric
753145449b1SDimitry Andric if (getLangOpts().isCompilingModule()) {
754145449b1SDimitry Andric Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
755145449b1SDimitry Andric getLangOpts().CurrentModule, DirectiveLoc, false, false);
756145449b1SDimitry Andric (void)ThisModule;
757145449b1SDimitry Andric assert(ThisModule && "was expecting a module if building one");
758145449b1SDimitry Andric }
75922989816SDimitry Andric }
76022989816SDimitry Andric
ActOnAnnotModuleBegin(SourceLocation DirectiveLoc,Module * Mod)761ac9a064cSDimitry Andric void Sema::ActOnAnnotModuleBegin(SourceLocation DirectiveLoc, Module *Mod) {
76222989816SDimitry Andric checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
76322989816SDimitry Andric
76422989816SDimitry Andric ModuleScopes.push_back({});
76522989816SDimitry Andric ModuleScopes.back().Module = Mod;
76622989816SDimitry Andric if (getLangOpts().ModulesLocalVisibility)
76722989816SDimitry Andric ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
76822989816SDimitry Andric
76922989816SDimitry Andric VisibleModules.setVisible(Mod, DirectiveLoc);
77022989816SDimitry Andric
77122989816SDimitry Andric // The enclosing context is now part of this module.
77222989816SDimitry Andric // FIXME: Consider creating a child DeclContext to hold the entities
77322989816SDimitry Andric // lexically within the module.
77422989816SDimitry Andric if (getLangOpts().trackLocalOwningModule()) {
77522989816SDimitry Andric for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
77622989816SDimitry Andric cast<Decl>(DC)->setModuleOwnershipKind(
77722989816SDimitry Andric getLangOpts().ModulesLocalVisibility
77822989816SDimitry Andric ? Decl::ModuleOwnershipKind::VisibleWhenImported
77922989816SDimitry Andric : Decl::ModuleOwnershipKind::Visible);
78022989816SDimitry Andric cast<Decl>(DC)->setLocalOwningModule(Mod);
78122989816SDimitry Andric }
78222989816SDimitry Andric }
78322989816SDimitry Andric }
78422989816SDimitry Andric
ActOnAnnotModuleEnd(SourceLocation EomLoc,Module * Mod)785ac9a064cSDimitry Andric void Sema::ActOnAnnotModuleEnd(SourceLocation EomLoc, Module *Mod) {
78622989816SDimitry Andric if (getLangOpts().ModulesLocalVisibility) {
78722989816SDimitry Andric VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules);
78822989816SDimitry Andric // Leaving a module hides namespace names, so our visible namespace cache
78922989816SDimitry Andric // is now out of date.
79022989816SDimitry Andric VisibleNamespaceCache.clear();
79122989816SDimitry Andric }
79222989816SDimitry Andric
79322989816SDimitry Andric assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod &&
79422989816SDimitry Andric "left the wrong module scope");
79522989816SDimitry Andric ModuleScopes.pop_back();
79622989816SDimitry Andric
79722989816SDimitry Andric // We got to the end of processing a local module. Create an
79822989816SDimitry Andric // ImportDecl as we would for an imported module.
79922989816SDimitry Andric FileID File = getSourceManager().getFileID(EomLoc);
80022989816SDimitry Andric SourceLocation DirectiveLoc;
80122989816SDimitry Andric if (EomLoc == getSourceManager().getLocForEndOfFile(File)) {
80222989816SDimitry Andric // We reached the end of a #included module header. Use the #include loc.
80322989816SDimitry Andric assert(File != getSourceManager().getMainFileID() &&
80422989816SDimitry Andric "end of submodule in main source file");
80522989816SDimitry Andric DirectiveLoc = getSourceManager().getIncludeLoc(File);
80622989816SDimitry Andric } else {
80722989816SDimitry Andric // We reached an EOM pragma. Use the pragma location.
80822989816SDimitry Andric DirectiveLoc = EomLoc;
80922989816SDimitry Andric }
81022989816SDimitry Andric BuildModuleInclude(DirectiveLoc, Mod);
81122989816SDimitry Andric
81222989816SDimitry Andric // Any further declarations are in whatever module we returned to.
81322989816SDimitry Andric if (getLangOpts().trackLocalOwningModule()) {
81422989816SDimitry Andric // The parser guarantees that this is the same context that we entered
81522989816SDimitry Andric // the module within.
81622989816SDimitry Andric for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
81722989816SDimitry Andric cast<Decl>(DC)->setLocalOwningModule(getCurrentModule());
81822989816SDimitry Andric if (!getCurrentModule())
81922989816SDimitry Andric cast<Decl>(DC)->setModuleOwnershipKind(
82022989816SDimitry Andric Decl::ModuleOwnershipKind::Unowned);
82122989816SDimitry Andric }
82222989816SDimitry Andric }
82322989816SDimitry Andric }
82422989816SDimitry Andric
createImplicitModuleImportForErrorRecovery(SourceLocation Loc,Module * Mod)82522989816SDimitry Andric void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc,
82622989816SDimitry Andric Module *Mod) {
82722989816SDimitry Andric // Bail if we're not allowed to implicitly import a module here.
82822989816SDimitry Andric if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery ||
82922989816SDimitry Andric VisibleModules.isVisible(Mod))
83022989816SDimitry Andric return;
83122989816SDimitry Andric
83222989816SDimitry Andric // Create the implicit import declaration.
83322989816SDimitry Andric TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
83422989816SDimitry Andric ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
83522989816SDimitry Andric Loc, Mod, Loc);
83622989816SDimitry Andric TU->addDecl(ImportD);
83722989816SDimitry Andric Consumer.HandleImplicitImportDecl(ImportD);
83822989816SDimitry Andric
83922989816SDimitry Andric // Make the module visible.
84022989816SDimitry Andric getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc);
84122989816SDimitry Andric VisibleModules.setVisible(Mod, Loc);
84222989816SDimitry Andric }
84322989816SDimitry Andric
ActOnStartExportDecl(Scope * S,SourceLocation ExportLoc,SourceLocation LBraceLoc)84422989816SDimitry Andric Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
84522989816SDimitry Andric SourceLocation LBraceLoc) {
84622989816SDimitry Andric ExportDecl *D = ExportDecl::Create(Context, CurContext, ExportLoc);
84722989816SDimitry Andric
84822989816SDimitry Andric // Set this temporarily so we know the export-declaration was braced.
84922989816SDimitry Andric D->setRBraceLoc(LBraceLoc);
85022989816SDimitry Andric
8516f8fc217SDimitry Andric CurContext->addDecl(D);
8526f8fc217SDimitry Andric PushDeclContext(S, D);
8536f8fc217SDimitry Andric
85422989816SDimitry Andric // C++2a [module.interface]p1:
85522989816SDimitry Andric // An export-declaration shall appear only [...] in the purview of a module
85622989816SDimitry Andric // interface unit. An export-declaration shall not appear directly or
85722989816SDimitry Andric // indirectly within [...] a private-module-fragment.
858ac9a064cSDimitry Andric if (!getLangOpts().HLSL) {
859e3b55780SDimitry Andric if (!isCurrentModulePurview()) {
86022989816SDimitry Andric Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
8616f8fc217SDimitry Andric D->setInvalidDecl();
8626f8fc217SDimitry Andric return D;
863b1c73532SDimitry Andric } else if (currentModuleIsImplementation()) {
86422989816SDimitry Andric Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
86522989816SDimitry Andric Diag(ModuleScopes.back().BeginLoc,
86622989816SDimitry Andric diag::note_not_module_interface_add_export)
86722989816SDimitry Andric << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
8686f8fc217SDimitry Andric D->setInvalidDecl();
8696f8fc217SDimitry Andric return D;
87022989816SDimitry Andric } else if (ModuleScopes.back().Module->Kind ==
87122989816SDimitry Andric Module::PrivateModuleFragment) {
87222989816SDimitry Andric Diag(ExportLoc, diag::err_export_in_private_module_fragment);
87322989816SDimitry Andric Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
8746f8fc217SDimitry Andric D->setInvalidDecl();
8756f8fc217SDimitry Andric return D;
87622989816SDimitry Andric }
877ac9a064cSDimitry Andric }
87822989816SDimitry Andric
87922989816SDimitry Andric for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) {
88022989816SDimitry Andric if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
88122989816SDimitry Andric // An export-declaration shall not appear directly or indirectly within
88222989816SDimitry Andric // an unnamed namespace [...]
88322989816SDimitry Andric if (ND->isAnonymousNamespace()) {
88422989816SDimitry Andric Diag(ExportLoc, diag::err_export_within_anonymous_namespace);
88522989816SDimitry Andric Diag(ND->getLocation(), diag::note_anonymous_namespace);
88622989816SDimitry Andric // Don't diagnose internal-linkage declarations in this region.
88722989816SDimitry Andric D->setInvalidDecl();
8886f8fc217SDimitry Andric return D;
88922989816SDimitry Andric }
89022989816SDimitry Andric
89122989816SDimitry Andric // A declaration is exported if it is [...] a namespace-definition
89222989816SDimitry Andric // that contains an exported declaration.
89322989816SDimitry Andric //
89422989816SDimitry Andric // Defer exporting the namespace until after we leave it, in order to
89522989816SDimitry Andric // avoid marking all subsequent declarations in the namespace as exported.
896ac9a064cSDimitry Andric if (!getLangOpts().HLSL && !DeferredExportedNamespaces.insert(ND).second)
89722989816SDimitry Andric break;
89822989816SDimitry Andric }
89922989816SDimitry Andric }
90022989816SDimitry Andric
90122989816SDimitry Andric // [...] its declaration or declaration-seq shall not contain an
90222989816SDimitry Andric // export-declaration.
90322989816SDimitry Andric if (auto *ED = getEnclosingExportDecl(D)) {
90422989816SDimitry Andric Diag(ExportLoc, diag::err_export_within_export);
90522989816SDimitry Andric if (ED->hasBraces())
90622989816SDimitry Andric Diag(ED->getLocation(), diag::note_export);
9076f8fc217SDimitry Andric D->setInvalidDecl();
9086f8fc217SDimitry Andric return D;
90922989816SDimitry Andric }
91022989816SDimitry Andric
911ac9a064cSDimitry Andric if (!getLangOpts().HLSL)
91222989816SDimitry Andric D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
913ac9a064cSDimitry Andric
91422989816SDimitry Andric return D;
91522989816SDimitry Andric }
91622989816SDimitry Andric
9177fa27ce4SDimitry Andric static bool checkExportedDecl(Sema &, Decl *, SourceLocation);
9187fa27ce4SDimitry Andric
9197fa27ce4SDimitry Andric /// Check that it's valid to export all the declarations in \p DC.
checkExportedDeclContext(Sema & S,DeclContext * DC,SourceLocation BlockStart)92022989816SDimitry Andric static bool checkExportedDeclContext(Sema &S, DeclContext *DC,
92122989816SDimitry Andric SourceLocation BlockStart) {
9227fa27ce4SDimitry Andric bool AllUnnamed = true;
9237fa27ce4SDimitry Andric for (auto *D : DC->decls())
9247fa27ce4SDimitry Andric AllUnnamed &= checkExportedDecl(S, D, BlockStart);
9257fa27ce4SDimitry Andric return AllUnnamed;
92622989816SDimitry Andric }
92722989816SDimitry Andric
92822989816SDimitry Andric /// Check that it's valid to export \p D.
checkExportedDecl(Sema & S,Decl * D,SourceLocation BlockStart)92922989816SDimitry Andric static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
93022989816SDimitry Andric
931ac9a064cSDimitry Andric // HLSL: export declaration is valid only on functions
932ac9a064cSDimitry Andric if (S.getLangOpts().HLSL) {
933ac9a064cSDimitry Andric // Export-within-export was already diagnosed in ActOnStartExportDecl
934ac9a064cSDimitry Andric if (!dyn_cast<FunctionDecl>(D) && !dyn_cast<ExportDecl>(D)) {
935ac9a064cSDimitry Andric S.Diag(D->getBeginLoc(), diag::err_hlsl_export_not_on_function);
936ac9a064cSDimitry Andric D->setInvalidDecl();
937ac9a064cSDimitry Andric return false;
938ac9a064cSDimitry Andric }
939ac9a064cSDimitry Andric }
940ac9a064cSDimitry Andric
9417fa27ce4SDimitry Andric // C++20 [module.interface]p3:
9427fa27ce4SDimitry Andric // [...] it shall not declare a name with internal linkage.
943145449b1SDimitry Andric bool HasName = false;
94422989816SDimitry Andric if (auto *ND = dyn_cast<NamedDecl>(D)) {
94522989816SDimitry Andric // Don't diagnose anonymous union objects; we'll diagnose their members
94622989816SDimitry Andric // instead.
947145449b1SDimitry Andric HasName = (bool)ND->getDeclName();
948b1c73532SDimitry Andric if (HasName && ND->getFormalLinkage() == Linkage::Internal) {
94922989816SDimitry Andric S.Diag(ND->getLocation(), diag::err_export_internal) << ND;
95022989816SDimitry Andric if (BlockStart.isValid())
95122989816SDimitry Andric S.Diag(BlockStart, diag::note_export);
9527fa27ce4SDimitry Andric return false;
95322989816SDimitry Andric }
95422989816SDimitry Andric }
95522989816SDimitry Andric
95622989816SDimitry Andric // C++2a [module.interface]p5:
95722989816SDimitry Andric // all entities to which all of the using-declarators ultimately refer
95822989816SDimitry Andric // shall have been introduced with a name having external linkage
95922989816SDimitry Andric if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
96022989816SDimitry Andric NamedDecl *Target = USD->getUnderlyingDecl();
961145449b1SDimitry Andric Linkage Lk = Target->getFormalLinkage();
962b1c73532SDimitry Andric if (Lk == Linkage::Internal || Lk == Linkage::Module) {
963145449b1SDimitry Andric S.Diag(USD->getLocation(), diag::err_export_using_internal)
964b1c73532SDimitry Andric << (Lk == Linkage::Internal ? 0 : 1) << Target;
96522989816SDimitry Andric S.Diag(Target->getLocation(), diag::note_using_decl_target);
96622989816SDimitry Andric if (BlockStart.isValid())
96722989816SDimitry Andric S.Diag(BlockStart, diag::note_export);
9687fa27ce4SDimitry Andric return false;
96922989816SDimitry Andric }
97022989816SDimitry Andric }
97122989816SDimitry Andric
97222989816SDimitry Andric // Recurse into namespace-scope DeclContexts. (Only namespace-scope
9737fa27ce4SDimitry Andric // declarations are exported).
974145449b1SDimitry Andric if (auto *DC = dyn_cast<DeclContext>(D)) {
9757fa27ce4SDimitry Andric if (!isa<NamespaceDecl>(D))
9767fa27ce4SDimitry Andric return true;
9777fa27ce4SDimitry Andric
9787fa27ce4SDimitry Andric if (auto *ND = dyn_cast<NamedDecl>(D)) {
9797fa27ce4SDimitry Andric if (!ND->getDeclName()) {
9807fa27ce4SDimitry Andric S.Diag(ND->getLocation(), diag::err_export_anon_ns_internal);
9817fa27ce4SDimitry Andric if (BlockStart.isValid())
9827fa27ce4SDimitry Andric S.Diag(BlockStart, diag::note_export);
9837fa27ce4SDimitry Andric return false;
9847fa27ce4SDimitry Andric } else if (!DC->decls().empty() &&
9857fa27ce4SDimitry Andric DC->getRedeclContext()->isFileContext()) {
98622989816SDimitry Andric return checkExportedDeclContext(S, DC, BlockStart);
987145449b1SDimitry Andric }
98822989816SDimitry Andric }
9897fa27ce4SDimitry Andric }
9907fa27ce4SDimitry Andric return true;
99122989816SDimitry Andric }
99222989816SDimitry Andric
ActOnFinishExportDecl(Scope * S,Decl * D,SourceLocation RBraceLoc)99322989816SDimitry Andric Decl *Sema::ActOnFinishExportDecl(Scope *S, Decl *D, SourceLocation RBraceLoc) {
99422989816SDimitry Andric auto *ED = cast<ExportDecl>(D);
99522989816SDimitry Andric if (RBraceLoc.isValid())
99622989816SDimitry Andric ED->setRBraceLoc(RBraceLoc);
99722989816SDimitry Andric
99822989816SDimitry Andric PopDeclContext();
99922989816SDimitry Andric
100022989816SDimitry Andric if (!D->isInvalidDecl()) {
100122989816SDimitry Andric SourceLocation BlockStart =
100222989816SDimitry Andric ED->hasBraces() ? ED->getBeginLoc() : SourceLocation();
100322989816SDimitry Andric for (auto *Child : ED->decls()) {
10047fa27ce4SDimitry Andric checkExportedDecl(*this, Child, BlockStart);
1005e3b55780SDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(Child)) {
1006e3b55780SDimitry Andric // [dcl.inline]/7
1007e3b55780SDimitry Andric // If an inline function or variable that is attached to a named module
1008e3b55780SDimitry Andric // is declared in a definition domain, it shall be defined in that
1009e3b55780SDimitry Andric // domain.
1010e3b55780SDimitry Andric // So, if the current declaration does not have a definition, we must
1011e3b55780SDimitry Andric // check at the end of the TU (or when the PMF starts) to see that we
1012e3b55780SDimitry Andric // have a definition at that point.
1013e3b55780SDimitry Andric if (FD->isInlineSpecified() && !FD->isDefined())
1014e3b55780SDimitry Andric PendingInlineFuncDecls.insert(FD);
1015e3b55780SDimitry Andric }
101622989816SDimitry Andric }
101722989816SDimitry Andric }
101822989816SDimitry Andric
1019ac9a064cSDimitry Andric // Anything exported from a module should never be considered unused.
1020ac9a064cSDimitry Andric for (auto *Exported : ED->decls())
1021ac9a064cSDimitry Andric Exported->markUsed(getASTContext());
1022ac9a064cSDimitry Andric
102322989816SDimitry Andric return D;
102422989816SDimitry Andric }
102577fc4c14SDimitry Andric
PushGlobalModuleFragment(SourceLocation BeginLoc)10267fa27ce4SDimitry Andric Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc) {
1027145449b1SDimitry Andric // We shouldn't create new global module fragment if there is already
1028145449b1SDimitry Andric // one.
10297fa27ce4SDimitry Andric if (!TheGlobalModuleFragment) {
103077fc4c14SDimitry Andric ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
10317fa27ce4SDimitry Andric TheGlobalModuleFragment = Map.createGlobalModuleFragmentForModuleUnit(
1032145449b1SDimitry Andric BeginLoc, getCurrentModule());
1033145449b1SDimitry Andric }
1034145449b1SDimitry Andric
10357fa27ce4SDimitry Andric assert(TheGlobalModuleFragment && "module creation should not fail");
103677fc4c14SDimitry Andric
103777fc4c14SDimitry Andric // Enter the scope of the global module.
10387fa27ce4SDimitry Andric ModuleScopes.push_back({BeginLoc, TheGlobalModuleFragment,
1039145449b1SDimitry Andric /*OuterVisibleModules=*/{}});
10407fa27ce4SDimitry Andric VisibleModules.setVisible(TheGlobalModuleFragment, BeginLoc);
104177fc4c14SDimitry Andric
10427fa27ce4SDimitry Andric return TheGlobalModuleFragment;
104377fc4c14SDimitry Andric }
104477fc4c14SDimitry Andric
PopGlobalModuleFragment()104577fc4c14SDimitry Andric void Sema::PopGlobalModuleFragment() {
10467fa27ce4SDimitry Andric assert(!ModuleScopes.empty() &&
10477fa27ce4SDimitry Andric getCurrentModule()->isExplicitGlobalModule() &&
104877fc4c14SDimitry Andric "left the wrong module scope, which is not global module fragment");
104977fc4c14SDimitry Andric ModuleScopes.pop_back();
105077fc4c14SDimitry Andric }
10511f917f69SDimitry Andric
PushImplicitGlobalModuleFragment(SourceLocation BeginLoc)1052b1c73532SDimitry Andric Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc) {
1053b1c73532SDimitry Andric if (!TheImplicitGlobalModuleFragment) {
10547fa27ce4SDimitry Andric ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
1055b1c73532SDimitry Andric TheImplicitGlobalModuleFragment =
1056b1c73532SDimitry Andric Map.createImplicitGlobalModuleFragmentForModuleUnit(BeginLoc,
1057b1c73532SDimitry Andric getCurrentModule());
10587fa27ce4SDimitry Andric }
1059b1c73532SDimitry Andric assert(TheImplicitGlobalModuleFragment && "module creation should not fail");
10601f917f69SDimitry Andric
10617fa27ce4SDimitry Andric // Enter the scope of the global module.
1062b1c73532SDimitry Andric ModuleScopes.push_back({BeginLoc, TheImplicitGlobalModuleFragment,
10637fa27ce4SDimitry Andric /*OuterVisibleModules=*/{}});
1064b1c73532SDimitry Andric VisibleModules.setVisible(TheImplicitGlobalModuleFragment, BeginLoc);
1065b1c73532SDimitry Andric return TheImplicitGlobalModuleFragment;
10667fa27ce4SDimitry Andric }
10671f917f69SDimitry Andric
PopImplicitGlobalModuleFragment()10687fa27ce4SDimitry Andric void Sema::PopImplicitGlobalModuleFragment() {
10697fa27ce4SDimitry Andric assert(!ModuleScopes.empty() &&
10707fa27ce4SDimitry Andric getCurrentModule()->isImplicitGlobalModule() &&
10717fa27ce4SDimitry Andric "left the wrong module scope, which is not global module fragment");
10727fa27ce4SDimitry Andric ModuleScopes.pop_back();
10731f917f69SDimitry Andric }
1074b1c73532SDimitry Andric
isCurrentModulePurview() const1075b1c73532SDimitry Andric bool Sema::isCurrentModulePurview() const {
1076b1c73532SDimitry Andric if (!getCurrentModule())
1077b1c73532SDimitry Andric return false;
1078b1c73532SDimitry Andric
1079b1c73532SDimitry Andric /// Does this Module scope describe part of the purview of a standard named
1080b1c73532SDimitry Andric /// C++ module?
1081b1c73532SDimitry Andric switch (getCurrentModule()->Kind) {
1082b1c73532SDimitry Andric case Module::ModuleInterfaceUnit:
1083b1c73532SDimitry Andric case Module::ModuleImplementationUnit:
1084b1c73532SDimitry Andric case Module::ModulePartitionInterface:
1085b1c73532SDimitry Andric case Module::ModulePartitionImplementation:
1086b1c73532SDimitry Andric case Module::PrivateModuleFragment:
1087b1c73532SDimitry Andric case Module::ImplicitGlobalModuleFragment:
1088b1c73532SDimitry Andric return true;
1089b1c73532SDimitry Andric default:
1090b1c73532SDimitry Andric return false;
1091b1c73532SDimitry Andric }
1092b1c73532SDimitry Andric }
1093