xref: /freebsd/contrib/llvm-project/clang/lib/Sema/SemaModule.cpp (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
1 //===--- SemaModule.cpp - Semantic Analysis for Modules -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements semantic analysis for modules (C++ modules syntax,
10 //  Objective-C modules syntax, and Clang header modules).
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/ASTConsumer.h"
15 #include "clang/AST/ASTMutationListener.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Sema/ParsedAttr.h"
19 #include "clang/Sema/SemaInternal.h"
20 #include "llvm/ADT/StringExtras.h"
21 
22 using namespace clang;
23 using namespace sema;
24 
checkModuleImportContext(Sema & S,Module * M,SourceLocation ImportLoc,DeclContext * DC,bool FromInclude=false)25 static void checkModuleImportContext(Sema &S, Module *M,
26                                      SourceLocation ImportLoc, DeclContext *DC,
27                                      bool FromInclude = false) {
28   SourceLocation ExternCLoc;
29 
30   if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) {
31     switch (LSD->getLanguage()) {
32     case LinkageSpecLanguageIDs::C:
33       if (ExternCLoc.isInvalid())
34         ExternCLoc = LSD->getBeginLoc();
35       break;
36     case LinkageSpecLanguageIDs::CXX:
37       break;
38     }
39     DC = LSD->getParent();
40   }
41 
42   while (isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC))
43     DC = DC->getParent();
44 
45   if (!isa<TranslationUnitDecl>(DC)) {
46     S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M))
47                           ? diag::ext_module_import_not_at_top_level_noop
48                           : diag::err_module_import_not_at_top_level_fatal)
49         << M->getFullModuleName() << DC;
50     S.Diag(cast<Decl>(DC)->getBeginLoc(),
51            diag::note_module_import_not_at_top_level)
52         << DC;
53   } else if (!M->IsExternC && ExternCLoc.isValid()) {
54     S.Diag(ImportLoc, diag::ext_module_import_in_extern_c)
55       << M->getFullModuleName();
56     S.Diag(ExternCLoc, diag::note_extern_c_begins_here);
57   }
58 }
59 
60 // We represent the primary and partition names as 'Paths' which are sections
61 // of the hierarchical access path for a clang module.  However for C++20
62 // the periods in a name are just another character, and we will need to
63 // flatten them into a string.
stringFromPath(ModuleIdPath Path)64 static std::string stringFromPath(ModuleIdPath Path) {
65   std::string Name;
66   if (Path.empty())
67     return Name;
68 
69   for (auto &Piece : Path) {
70     if (!Name.empty())
71       Name += ".";
72     Name += Piece.getIdentifierInfo()->getName();
73   }
74   return Name;
75 }
76 
77 /// Helper function for makeTransitiveImportsVisible to decide whether
78 /// the \param Imported module unit is in the same module with the \param
79 /// CurrentModule.
80 /// \param FoundPrimaryModuleInterface is a helper parameter to record the
81 /// primary module interface unit corresponding to the module \param
82 /// CurrentModule. Since currently it is expensive to decide whether two module
83 /// units come from the same module by comparing the module name.
84 static bool
isImportingModuleUnitFromSameModule(ASTContext & Ctx,Module * Imported,Module * CurrentModule,Module * & FoundPrimaryModuleInterface)85 isImportingModuleUnitFromSameModule(ASTContext &Ctx, Module *Imported,
86                                     Module *CurrentModule,
87                                     Module *&FoundPrimaryModuleInterface) {
88   if (!Imported->isNamedModule())
89     return false;
90 
91   // The a partition unit we're importing must be in the same module of the
92   // current module.
93   if (Imported->isModulePartition())
94     return true;
95 
96   // If we found the primary module interface during the search process, we can
97   // return quickly to avoid expensive string comparison.
98   if (FoundPrimaryModuleInterface)
99     return Imported == FoundPrimaryModuleInterface;
100 
101   if (!CurrentModule)
102     return false;
103 
104   // Then the imported module must be a primary module interface unit.  It
105   // is only allowed to import the primary module interface unit from the same
106   // module in the implementation unit and the implementation partition unit.
107 
108   // Since we'll handle implementation unit above. We can only care
109   // about the implementation partition unit here.
110   if (!CurrentModule->isModulePartitionImplementation())
111     return false;
112 
113   if (Ctx.isInSameModule(Imported, CurrentModule)) {
114     assert(!FoundPrimaryModuleInterface ||
115            FoundPrimaryModuleInterface == Imported);
116     FoundPrimaryModuleInterface = Imported;
117     return true;
118   }
119 
120   return false;
121 }
122 
123 /// [module.import]p7:
124 ///   Additionally, when a module-import-declaration in a module unit of some
125 ///   module M imports another module unit U of M, it also imports all
126 ///   translation units imported by non-exported module-import-declarations in
127 ///   the module unit purview of U. These rules can in turn lead to the
128 ///   importation of yet more translation units.
129 static void
makeTransitiveImportsVisible(ASTContext & Ctx,VisibleModuleSet & VisibleModules,Module * Imported,Module * CurrentModule,SourceLocation ImportLoc,bool IsImportingPrimaryModuleInterface=false)130 makeTransitiveImportsVisible(ASTContext &Ctx, VisibleModuleSet &VisibleModules,
131                              Module *Imported, Module *CurrentModule,
132                              SourceLocation ImportLoc,
133                              bool IsImportingPrimaryModuleInterface = false) {
134   assert(Imported->isNamedModule() &&
135          "'makeTransitiveImportsVisible()' is intended for standard C++ named "
136          "modules only.");
137 
138   llvm::SmallVector<Module *, 4> Worklist;
139   llvm::SmallSet<Module *, 16> Visited;
140   Worklist.push_back(Imported);
141 
142   Module *FoundPrimaryModuleInterface =
143       IsImportingPrimaryModuleInterface ? Imported : nullptr;
144 
145   while (!Worklist.empty()) {
146     Module *Importing = Worklist.pop_back_val();
147 
148     if (Visited.count(Importing))
149       continue;
150     Visited.insert(Importing);
151 
152     // FIXME: The ImportLoc here is not meaningful. It may be problematic if we
153     // use the sourcelocation loaded from the visible modules.
154     VisibleModules.setVisible(Importing, ImportLoc);
155 
156     if (isImportingModuleUnitFromSameModule(Ctx, Importing, CurrentModule,
157                                             FoundPrimaryModuleInterface)) {
158       for (Module *TransImported : Importing->Imports)
159         Worklist.push_back(TransImported);
160 
161       for (auto [Exports, _] : Importing->Exports)
162         Worklist.push_back(Exports);
163     }
164   }
165 }
166 
167 Sema::DeclGroupPtrTy
ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc)168 Sema::ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc) {
169   // We start in the global module;
170   Module *GlobalModule =
171       PushGlobalModuleFragment(ModuleLoc);
172 
173   // All declarations created from now on are owned by the global module.
174   auto *TU = Context.getTranslationUnitDecl();
175   // [module.global.frag]p2
176   // A global-module-fragment specifies the contents of the global module
177   // fragment for a module unit. The global module fragment can be used to
178   // provide declarations that are attached to the global module and usable
179   // within the module unit.
180   //
181   // So the declations in the global module shouldn't be visible by default.
182   TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported);
183   TU->setLocalOwningModule(GlobalModule);
184 
185   // FIXME: Consider creating an explicit representation of this declaration.
186   return nullptr;
187 }
188 
HandleStartOfHeaderUnit()189 void Sema::HandleStartOfHeaderUnit() {
190   assert(getLangOpts().CPlusPlusModules &&
191          "Header units are only valid for C++20 modules");
192   SourceLocation StartOfTU =
193       SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
194 
195   StringRef HUName = getLangOpts().CurrentModule;
196   if (HUName.empty()) {
197     HUName =
198         SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID())->getName();
199     const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str();
200   }
201 
202   // TODO: Make the C++20 header lookup independent.
203   // When the input is pre-processed source, we need a file ref to the original
204   // file for the header map.
205   auto F = SourceMgr.getFileManager().getOptionalFileRef(HUName);
206   // For the sake of error recovery (if someone has moved the original header
207   // after creating the pre-processed output) fall back to obtaining the file
208   // ref for the input file, which must be present.
209   if (!F)
210     F = SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID());
211   assert(F && "failed to find the header unit source?");
212   Module::Header H{HUName.str(), HUName.str(), *F};
213   auto &Map = PP.getHeaderSearchInfo().getModuleMap();
214   Module *Mod = Map.createHeaderUnit(StartOfTU, HUName, H);
215   assert(Mod && "module creation should not fail");
216   ModuleScopes.push_back({}); // No GMF
217   ModuleScopes.back().BeginLoc = StartOfTU;
218   ModuleScopes.back().Module = Mod;
219   VisibleModules.setVisible(Mod, StartOfTU);
220 
221   // From now on, we have an owning module for all declarations we see.
222   // All of these are implicitly exported.
223   auto *TU = Context.getTranslationUnitDecl();
224   TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::Visible);
225   TU->setLocalOwningModule(Mod);
226 }
227 
228 /// Tests whether the given identifier is reserved as a module name and
229 /// diagnoses if it is. Returns true if a diagnostic is emitted and false
230 /// otherwise.
DiagReservedModuleName(Sema & S,const IdentifierInfo * II,SourceLocation Loc)231 static bool DiagReservedModuleName(Sema &S, const IdentifierInfo *II,
232                                    SourceLocation Loc) {
233   enum {
234     Valid = -1,
235     Invalid = 0,
236     Reserved = 1,
237   } Reason = Valid;
238 
239   if (II->isStr("module") || II->isStr("import"))
240     Reason = Invalid;
241   else if (II->isReserved(S.getLangOpts()) !=
242            ReservedIdentifierStatus::NotReserved)
243     Reason = Reserved;
244 
245   // If the identifier is reserved (not invalid) but is in a system header,
246   // we do not diagnose (because we expect system headers to use reserved
247   // identifiers).
248   if (Reason == Reserved && S.getSourceManager().isInSystemHeader(Loc))
249     Reason = Valid;
250 
251   switch (Reason) {
252   case Valid:
253     return false;
254   case Invalid:
255     return S.Diag(Loc, diag::err_invalid_module_name) << II;
256   case Reserved:
257     S.Diag(Loc, diag::warn_reserved_module_name) << II;
258     return false;
259   }
260   llvm_unreachable("fell off a fully covered switch");
261 }
262 
263 Sema::DeclGroupPtrTy
ActOnModuleDecl(SourceLocation StartLoc,SourceLocation ModuleLoc,ModuleDeclKind MDK,ModuleIdPath Path,ModuleIdPath Partition,ModuleImportState & ImportState,bool SeenNoTrivialPPDirective)264 Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
265                       ModuleDeclKind MDK, ModuleIdPath Path,
266                       ModuleIdPath Partition, ModuleImportState &ImportState,
267                       bool SeenNoTrivialPPDirective) {
268   assert(getLangOpts().CPlusPlusModules &&
269          "should only have module decl in standard C++ modules");
270 
271   bool IsFirstDecl = ImportState == ModuleImportState::FirstDecl;
272   bool SeenGMF = ImportState == ModuleImportState::GlobalFragment;
273   // If any of the steps here fail, we count that as invalidating C++20
274   // module state;
275   ImportState = ModuleImportState::NotACXX20Module;
276 
277   bool IsPartition = !Partition.empty();
278   if (IsPartition)
279     switch (MDK) {
280     case ModuleDeclKind::Implementation:
281       MDK = ModuleDeclKind::PartitionImplementation;
282       break;
283     case ModuleDeclKind::Interface:
284       MDK = ModuleDeclKind::PartitionInterface;
285       break;
286     default:
287       llvm_unreachable("how did we get a partition type set?");
288     }
289 
290   // A (non-partition) module implementation unit requires that we are not
291   // compiling a module of any kind.  A partition implementation emits an
292   // interface (and the AST for the implementation), which will subsequently
293   // be consumed to emit a binary.
294   // A module interface unit requires that we are not compiling a module map.
295   switch (getLangOpts().getCompilingModule()) {
296   case LangOptions::CMK_None:
297     // It's OK to compile a module interface as a normal translation unit.
298     break;
299 
300   case LangOptions::CMK_ModuleInterface:
301     if (MDK != ModuleDeclKind::Implementation)
302       break;
303 
304     // We were asked to compile a module interface unit but this is a module
305     // implementation unit.
306     Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch)
307       << FixItHint::CreateInsertion(ModuleLoc, "export ");
308     MDK = ModuleDeclKind::Interface;
309     break;
310 
311   case LangOptions::CMK_ModuleMap:
312     Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
313     return nullptr;
314 
315   case LangOptions::CMK_HeaderUnit:
316     Diag(ModuleLoc, diag::err_module_decl_in_header_unit);
317     return nullptr;
318   }
319 
320   assert(ModuleScopes.size() <= 1 && "expected to be at global module scope");
321 
322   // FIXME: Most of this work should be done by the preprocessor rather than
323   // here, in order to support macro import.
324 
325   // Only one module-declaration is permitted per source file.
326   if (isCurrentModulePurview()) {
327     Diag(ModuleLoc, diag::err_module_redeclaration);
328     Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
329          diag::note_prev_module_declaration);
330     return nullptr;
331   }
332 
333   assert((!getLangOpts().CPlusPlusModules ||
334           SeenGMF == (bool)this->TheGlobalModuleFragment) &&
335          "mismatched global module state");
336 
337   // In C++20, A module directive may only appear as the first preprocessing
338   // tokens in a file (excluding the global module fragment.).
339   if (getLangOpts().CPlusPlusModules &&
340       (!IsFirstDecl || SeenNoTrivialPPDirective) && !SeenGMF) {
341     Diag(ModuleLoc, diag::err_module_decl_not_at_start);
342     SourceLocation BeginLoc = PP.getMainFileFirstPPTokenLoc();
343     Diag(BeginLoc, diag::note_global_module_introducer_missing)
344         << FixItHint::CreateInsertion(BeginLoc, "module;\n");
345   }
346 
347   // C++23 [module.unit]p1: ... The identifiers module and import shall not
348   // appear as identifiers in a module-name or module-partition. All
349   // module-names either beginning with an identifier consisting of std
350   // followed by zero or more digits or containing a reserved identifier
351   // ([lex.name]) are reserved and shall not be specified in a
352   // module-declaration; no diagnostic is required.
353 
354   // Test the first part of the path to see if it's std[0-9]+ but allow the
355   // name in a system header.
356   StringRef FirstComponentName = Path[0].getIdentifierInfo()->getName();
357   if (!getSourceManager().isInSystemHeader(Path[0].getLoc()) &&
358       (FirstComponentName == "std" ||
359        (FirstComponentName.starts_with("std") &&
360         llvm::all_of(FirstComponentName.drop_front(3), &llvm::isDigit))))
361     Diag(Path[0].getLoc(), diag::warn_reserved_module_name)
362         << Path[0].getIdentifierInfo();
363 
364   // Then test all of the components in the path to see if any of them are
365   // using another kind of reserved or invalid identifier.
366   for (auto Part : Path) {
367     if (DiagReservedModuleName(*this, Part.getIdentifierInfo(), Part.getLoc()))
368       return nullptr;
369   }
370 
371   // Flatten the dots in a module name. Unlike Clang's hierarchical module map
372   // modules, the dots here are just another character that can appear in a
373   // module name.
374   std::string ModuleName = stringFromPath(Path);
375   if (IsPartition) {
376     ModuleName += ":";
377     ModuleName += stringFromPath(Partition);
378   }
379   // If a module name was explicitly specified on the command line, it must be
380   // correct.
381   if (!getLangOpts().CurrentModule.empty() &&
382       getLangOpts().CurrentModule != ModuleName) {
383     Diag(Path.front().getLoc(), diag::err_current_module_name_mismatch)
384         << SourceRange(Path.front().getLoc(), IsPartition
385                                                   ? Partition.back().getLoc()
386                                                   : Path.back().getLoc())
387         << getLangOpts().CurrentModule;
388     return nullptr;
389   }
390   const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
391 
392   auto &Map = PP.getHeaderSearchInfo().getModuleMap();
393   Module *Mod;                 // The module we are creating.
394   Module *Interface = nullptr; // The interface for an implementation.
395   switch (MDK) {
396   case ModuleDeclKind::Interface:
397   case ModuleDeclKind::PartitionInterface: {
398     // We can't have parsed or imported a definition of this module or parsed a
399     // module map defining it already.
400     if (auto *M = Map.findOrLoadModule(ModuleName)) {
401       Diag(Path[0].getLoc(), diag::err_module_redefinition) << ModuleName;
402       if (M->DefinitionLoc.isValid())
403         Diag(M->DefinitionLoc, diag::note_prev_module_definition);
404       else if (OptionalFileEntryRef FE = M->getASTFile())
405         Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
406             << FE->getName();
407       Mod = M;
408       break;
409     }
410 
411     // Create a Module for the module that we're defining.
412     Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
413     if (MDK == ModuleDeclKind::PartitionInterface)
414       Mod->Kind = Module::ModulePartitionInterface;
415     assert(Mod && "module creation should not fail");
416     break;
417   }
418 
419   case ModuleDeclKind::Implementation: {
420     // C++20 A module-declaration that contains neither an export-
421     // keyword nor a module-partition implicitly imports the primary
422     // module interface unit of the module as if by a module-import-
423     // declaration.
424     IdentifierLoc ModuleNameLoc(Path[0].getLoc(),
425                                 PP.getIdentifierInfo(ModuleName));
426 
427     // The module loader will assume we're trying to import the module that
428     // we're building if `LangOpts.CurrentModule` equals to 'ModuleName'.
429     // Change the value for `LangOpts.CurrentModule` temporarily to make the
430     // module loader work properly.
431     const_cast<LangOptions &>(getLangOpts()).CurrentModule = "";
432     Interface = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
433                                              Module::AllVisible,
434                                              /*IsInclusionDirective=*/false);
435     const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
436 
437     if (!Interface) {
438       Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
439       // Create an empty module interface unit for error recovery.
440       Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
441     } else {
442       Mod = Map.createModuleForImplementationUnit(ModuleLoc, ModuleName);
443     }
444   } break;
445 
446   case ModuleDeclKind::PartitionImplementation:
447     // Create an interface, but note that it is an implementation
448     // unit.
449     Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
450     Mod->Kind = Module::ModulePartitionImplementation;
451     break;
452   }
453 
454   if (!this->TheGlobalModuleFragment) {
455     ModuleScopes.push_back({});
456     if (getLangOpts().ModulesLocalVisibility)
457       ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
458   } else {
459     // We're done with the global module fragment now.
460     ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global);
461   }
462 
463   // Switch from the global module fragment (if any) to the named module.
464   ModuleScopes.back().BeginLoc = StartLoc;
465   ModuleScopes.back().Module = Mod;
466   VisibleModules.setVisible(Mod, ModuleLoc);
467 
468   // From now on, we have an owning module for all declarations we see.
469   // In C++20 modules, those declaration would be reachable when imported
470   // unless explicitily exported.
471   // Otherwise, those declarations are module-private unless explicitly
472   // exported.
473   auto *TU = Context.getTranslationUnitDecl();
474   TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported);
475   TU->setLocalOwningModule(Mod);
476 
477   // We are in the module purview, but before any other (non import)
478   // statements, so imports are allowed.
479   ImportState = ModuleImportState::ImportAllowed;
480 
481   getASTContext().setCurrentNamedModule(Mod);
482 
483   if (auto *Listener = getASTMutationListener())
484     Listener->EnteringModulePurview();
485 
486   // We already potentially made an implicit import (in the case of a module
487   // implementation unit importing its interface).  Make this module visible
488   // and return the import decl to be added to the current TU.
489   if (Interface) {
490 
491     makeTransitiveImportsVisible(getASTContext(), VisibleModules, Interface,
492                                  Mod, ModuleLoc,
493                                  /*IsImportingPrimaryModuleInterface=*/true);
494 
495     // Make the import decl for the interface in the impl module.
496     ImportDecl *Import = ImportDecl::Create(Context, CurContext, ModuleLoc,
497                                             Interface, Path[0].getLoc());
498     CurContext->addDecl(Import);
499 
500     // Sequence initialization of the imported module before that of the current
501     // module, if any.
502     Context.addModuleInitializer(ModuleScopes.back().Module, Import);
503     Mod->Imports.insert(Interface); // As if we imported it.
504     // Also save this as a shortcut to checking for decls in the interface
505     ThePrimaryInterface = Interface;
506     // If we made an implicit import of the module interface, then return the
507     // imported module decl.
508     return ConvertDeclToDeclGroup(Import);
509   }
510 
511   return nullptr;
512 }
513 
514 Sema::DeclGroupPtrTy
ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,SourceLocation PrivateLoc)515 Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,
516                                      SourceLocation PrivateLoc) {
517   // C++20 [basic.link]/2:
518   //   A private-module-fragment shall appear only in a primary module
519   //   interface unit.
520   switch (ModuleScopes.empty() ? Module::ExplicitGlobalModuleFragment
521                                : ModuleScopes.back().Module->Kind) {
522   case Module::ModuleMapModule:
523   case Module::ExplicitGlobalModuleFragment:
524   case Module::ImplicitGlobalModuleFragment:
525   case Module::ModulePartitionImplementation:
526   case Module::ModulePartitionInterface:
527   case Module::ModuleHeaderUnit:
528     Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
529     return nullptr;
530 
531   case Module::PrivateModuleFragment:
532     Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
533     Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
534     return nullptr;
535 
536   case Module::ModuleImplementationUnit:
537     Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface);
538     Diag(ModuleScopes.back().BeginLoc,
539          diag::note_not_module_interface_add_export)
540         << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
541     return nullptr;
542 
543   case Module::ModuleInterfaceUnit:
544     break;
545   }
546 
547   // FIXME: Check that this translation unit does not import any partitions;
548   // such imports would violate [basic.link]/2's "shall be the only module unit"
549   // restriction.
550 
551   // We've finished the public fragment of the translation unit.
552   ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal);
553 
554   auto &Map = PP.getHeaderSearchInfo().getModuleMap();
555   Module *PrivateModuleFragment =
556       Map.createPrivateModuleFragmentForInterfaceUnit(
557           ModuleScopes.back().Module, PrivateLoc);
558   assert(PrivateModuleFragment && "module creation should not fail");
559 
560   // Enter the scope of the private module fragment.
561   ModuleScopes.push_back({});
562   ModuleScopes.back().BeginLoc = ModuleLoc;
563   ModuleScopes.back().Module = PrivateModuleFragment;
564   VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
565 
566   // All declarations created from now on are scoped to the private module
567   // fragment (and are neither visible nor reachable in importers of the module
568   // interface).
569   auto *TU = Context.getTranslationUnitDecl();
570   TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
571   TU->setLocalOwningModule(PrivateModuleFragment);
572 
573   // FIXME: Consider creating an explicit representation of this declaration.
574   return nullptr;
575 }
576 
ActOnModuleImport(SourceLocation StartLoc,SourceLocation ExportLoc,SourceLocation ImportLoc,ModuleIdPath Path,bool IsPartition)577 DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
578                                    SourceLocation ExportLoc,
579                                    SourceLocation ImportLoc, ModuleIdPath Path,
580                                    bool IsPartition) {
581   assert((!IsPartition || getLangOpts().CPlusPlusModules) &&
582          "partition seen in non-C++20 code?");
583 
584   // For a C++20 module name, flatten into a single identifier with the source
585   // location of the first component.
586   IdentifierLoc ModuleNameLoc;
587 
588   std::string ModuleName;
589   if (IsPartition) {
590     // We already checked that we are in a module purview in the parser.
591     assert(!ModuleScopes.empty() && "in a module purview, but no module?");
592     Module *NamedMod = ModuleScopes.back().Module;
593     // If we are importing into a partition, find the owning named module,
594     // otherwise, the name of the importing named module.
595     ModuleName = NamedMod->getPrimaryModuleInterfaceName().str();
596     ModuleName += ":";
597     ModuleName += stringFromPath(Path);
598     ModuleNameLoc =
599         IdentifierLoc(Path[0].getLoc(), PP.getIdentifierInfo(ModuleName));
600     Path = ModuleIdPath(ModuleNameLoc);
601   } else if (getLangOpts().CPlusPlusModules) {
602     ModuleName = stringFromPath(Path);
603     ModuleNameLoc =
604         IdentifierLoc(Path[0].getLoc(), PP.getIdentifierInfo(ModuleName));
605     Path = ModuleIdPath(ModuleNameLoc);
606   }
607 
608   // Diagnose self-import before attempting a load.
609   // [module.import]/9
610   // A module implementation unit of a module M that is not a module partition
611   // shall not contain a module-import-declaration nominating M.
612   // (for an implementation, the module interface is imported implicitly,
613   //  but that's handled in the module decl code).
614 
615   if (getLangOpts().CPlusPlusModules && isCurrentModulePurview() &&
616       getCurrentModule()->Name == ModuleName) {
617     Diag(ImportLoc, diag::err_module_self_import_cxx20)
618         << ModuleName << currentModuleIsImplementation();
619     return true;
620   }
621 
622   Module *Mod = getModuleLoader().loadModule(
623       ImportLoc, Path, Module::AllVisible, /*IsInclusionDirective=*/false);
624   if (!Mod)
625     return true;
626 
627   if (!Mod->isInterfaceOrPartition() && !ModuleName.empty() &&
628       !getLangOpts().ObjC) {
629     Diag(ImportLoc, diag::err_module_import_non_interface_nor_parition)
630         << ModuleName;
631     return true;
632   }
633 
634   return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
635 }
636 
637 /// Determine whether \p D is lexically within an export-declaration.
getEnclosingExportDecl(const Decl * D)638 static const ExportDecl *getEnclosingExportDecl(const Decl *D) {
639   for (auto *DC = D->getLexicalDeclContext(); DC; DC = DC->getLexicalParent())
640     if (auto *ED = dyn_cast<ExportDecl>(DC))
641       return ED;
642   return nullptr;
643 }
644 
ActOnModuleImport(SourceLocation StartLoc,SourceLocation ExportLoc,SourceLocation ImportLoc,Module * Mod,ModuleIdPath Path)645 DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
646                                    SourceLocation ExportLoc,
647                                    SourceLocation ImportLoc, Module *Mod,
648                                    ModuleIdPath Path) {
649   if (Mod->isHeaderUnit())
650     Diag(ImportLoc, diag::warn_experimental_header_unit);
651 
652   if (Mod->isNamedModule())
653     makeTransitiveImportsVisible(getASTContext(), VisibleModules, Mod,
654                                  getCurrentModule(), ImportLoc);
655   else
656     VisibleModules.setVisible(Mod, ImportLoc);
657 
658   assert((!Mod->isModulePartitionImplementation() || getCurrentModule()) &&
659          "We can only import a partition unit in a named module.");
660   if (Mod->isModulePartitionImplementation() &&
661       getCurrentModule()->isModuleInterfaceUnit())
662     Diag(ImportLoc,
663          diag::warn_import_implementation_partition_unit_in_interface_unit)
664         << Mod->Name;
665 
666   checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
667 
668   // FIXME: we should support importing a submodule within a different submodule
669   // of the same top-level module. Until we do, make it an error rather than
670   // silently ignoring the import.
671   // FIXME: Should we warn on a redundant import of the current module?
672   if (Mod->isForBuilding(getLangOpts())) {
673     Diag(ImportLoc, getLangOpts().isCompilingModule()
674                         ? diag::err_module_self_import
675                         : diag::err_module_import_in_implementation)
676         << Mod->getFullModuleName() << getLangOpts().CurrentModule;
677   }
678 
679   SmallVector<SourceLocation, 2> IdentifierLocs;
680 
681   if (Path.empty()) {
682     // If this was a header import, pad out with dummy locations.
683     // FIXME: Pass in and use the location of the header-name token in this
684     // case.
685     for (Module *ModCheck = Mod; ModCheck; ModCheck = ModCheck->Parent)
686       IdentifierLocs.push_back(SourceLocation());
687   } else if (getLangOpts().CPlusPlusModules && !Mod->Parent) {
688     // A single identifier for the whole name.
689     IdentifierLocs.push_back(Path[0].getLoc());
690   } else {
691     Module *ModCheck = Mod;
692     for (unsigned I = 0, N = Path.size(); I != N; ++I) {
693       // If we've run out of module parents, just drop the remaining
694       // identifiers.  We need the length to be consistent.
695       if (!ModCheck)
696         break;
697       ModCheck = ModCheck->Parent;
698 
699       IdentifierLocs.push_back(Path[I].getLoc());
700     }
701   }
702 
703   ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc,
704                                           Mod, IdentifierLocs);
705   CurContext->addDecl(Import);
706 
707   // Sequence initialization of the imported module before that of the current
708   // module, if any.
709   if (!ModuleScopes.empty())
710     Context.addModuleInitializer(ModuleScopes.back().Module, Import);
711 
712   // A module (partition) implementation unit shall not be exported.
713   if (getLangOpts().CPlusPlusModules && ExportLoc.isValid() &&
714       Mod->Kind == Module::ModuleKind::ModulePartitionImplementation) {
715     Diag(ExportLoc, diag::err_export_partition_impl)
716         << SourceRange(ExportLoc, Path.back().getLoc());
717   } else if (ExportLoc.isValid() &&
718              (ModuleScopes.empty() || currentModuleIsImplementation())) {
719     // [module.interface]p1:
720     // An export-declaration shall inhabit a namespace scope and appear in the
721     // purview of a module interface unit.
722     Diag(ExportLoc, diag::err_export_not_in_module_interface);
723   } else if (!ModuleScopes.empty()) {
724     // Re-export the module if the imported module is exported.
725     // Note that we don't need to add re-exported module to Imports field
726     // since `Exports` implies the module is imported already.
727     if (ExportLoc.isValid() || getEnclosingExportDecl(Import))
728       getCurrentModule()->Exports.emplace_back(Mod, false);
729     else
730       getCurrentModule()->Imports.insert(Mod);
731   }
732 
733   return Import;
734 }
735 
ActOnAnnotModuleInclude(SourceLocation DirectiveLoc,Module * Mod)736 void Sema::ActOnAnnotModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
737   checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
738   BuildModuleInclude(DirectiveLoc, Mod);
739 }
740 
BuildModuleInclude(SourceLocation DirectiveLoc,Module * Mod)741 void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
742   // Determine whether we're in the #include buffer for a module. The #includes
743   // in that buffer do not qualify as module imports; they're just an
744   // implementation detail of us building the module.
745   //
746   // FIXME: Should we even get ActOnAnnotModuleInclude calls for those?
747   bool IsInModuleIncludes =
748       TUKind == TU_ClangModule &&
749       getSourceManager().isWrittenInMainFile(DirectiveLoc);
750 
751   // If we are really importing a module (not just checking layering) due to an
752   // #include in the main file, synthesize an ImportDecl.
753   if (getLangOpts().Modules && !IsInModuleIncludes) {
754     TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
755     ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
756                                                      DirectiveLoc, Mod,
757                                                      DirectiveLoc);
758     if (!ModuleScopes.empty())
759       Context.addModuleInitializer(ModuleScopes.back().Module, ImportD);
760     TU->addDecl(ImportD);
761     Consumer.HandleImplicitImportDecl(ImportD);
762   }
763 
764   getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc);
765   VisibleModules.setVisible(Mod, DirectiveLoc);
766 
767   if (getLangOpts().isCompilingModule()) {
768     Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
769         getLangOpts().CurrentModule, DirectiveLoc, false, false);
770     (void)ThisModule;
771     assert(ThisModule && "was expecting a module if building one");
772   }
773 }
774 
ActOnAnnotModuleBegin(SourceLocation DirectiveLoc,Module * Mod)775 void Sema::ActOnAnnotModuleBegin(SourceLocation DirectiveLoc, Module *Mod) {
776   checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
777 
778   ModuleScopes.push_back({});
779   ModuleScopes.back().Module = Mod;
780   if (getLangOpts().ModulesLocalVisibility)
781     ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
782 
783   VisibleModules.setVisible(Mod, DirectiveLoc);
784 
785   // The enclosing context is now part of this module.
786   // FIXME: Consider creating a child DeclContext to hold the entities
787   // lexically within the module.
788   if (getLangOpts().trackLocalOwningModule()) {
789     for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
790       cast<Decl>(DC)->setModuleOwnershipKind(
791           getLangOpts().ModulesLocalVisibility
792               ? Decl::ModuleOwnershipKind::VisibleWhenImported
793               : Decl::ModuleOwnershipKind::Visible);
794       cast<Decl>(DC)->setLocalOwningModule(Mod);
795     }
796   }
797 }
798 
ActOnAnnotModuleEnd(SourceLocation EomLoc,Module * Mod)799 void Sema::ActOnAnnotModuleEnd(SourceLocation EomLoc, Module *Mod) {
800   if (getLangOpts().ModulesLocalVisibility) {
801     VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules);
802     // Leaving a module hides namespace names, so our visible namespace cache
803     // is now out of date.
804     VisibleNamespaceCache.clear();
805   }
806 
807   assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod &&
808          "left the wrong module scope");
809   ModuleScopes.pop_back();
810 
811   // We got to the end of processing a local module. Create an
812   // ImportDecl as we would for an imported module.
813   FileID File = getSourceManager().getFileID(EomLoc);
814   SourceLocation DirectiveLoc;
815   if (EomLoc == getSourceManager().getLocForEndOfFile(File)) {
816     // We reached the end of a #included module header. Use the #include loc.
817     assert(File != getSourceManager().getMainFileID() &&
818            "end of submodule in main source file");
819     DirectiveLoc = getSourceManager().getIncludeLoc(File);
820   } else {
821     // We reached an EOM pragma. Use the pragma location.
822     DirectiveLoc = EomLoc;
823   }
824   BuildModuleInclude(DirectiveLoc, Mod);
825 
826   // Any further declarations are in whatever module we returned to.
827   if (getLangOpts().trackLocalOwningModule()) {
828     // The parser guarantees that this is the same context that we entered
829     // the module within.
830     for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
831       cast<Decl>(DC)->setLocalOwningModule(getCurrentModule());
832       if (!getCurrentModule())
833         cast<Decl>(DC)->setModuleOwnershipKind(
834             Decl::ModuleOwnershipKind::Unowned);
835     }
836   }
837 }
838 
createImplicitModuleImportForErrorRecovery(SourceLocation Loc,Module * Mod)839 void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc,
840                                                       Module *Mod) {
841   // Bail if we're not allowed to implicitly import a module here.
842   if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery ||
843       VisibleModules.isVisible(Mod))
844     return;
845 
846   // Create the implicit import declaration.
847   TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
848   ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
849                                                    Loc, Mod, Loc);
850   TU->addDecl(ImportD);
851   Consumer.HandleImplicitImportDecl(ImportD);
852 
853   // Make the module visible.
854   getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc);
855   VisibleModules.setVisible(Mod, Loc);
856 }
857 
ActOnStartExportDecl(Scope * S,SourceLocation ExportLoc,SourceLocation LBraceLoc)858 Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
859                                  SourceLocation LBraceLoc) {
860   ExportDecl *D = ExportDecl::Create(Context, CurContext, ExportLoc);
861 
862   // Set this temporarily so we know the export-declaration was braced.
863   D->setRBraceLoc(LBraceLoc);
864 
865   CurContext->addDecl(D);
866   PushDeclContext(S, D);
867 
868   // C++2a [module.interface]p1:
869   //   An export-declaration shall appear only [...] in the purview of a module
870   //   interface unit. An export-declaration shall not appear directly or
871   //   indirectly within [...] a private-module-fragment.
872   if (!getLangOpts().HLSL) {
873     if (!isCurrentModulePurview()) {
874       Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
875       D->setInvalidDecl();
876       return D;
877     } else if (currentModuleIsImplementation()) {
878       Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
879       Diag(ModuleScopes.back().BeginLoc,
880           diag::note_not_module_interface_add_export)
881           << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
882       D->setInvalidDecl();
883       return D;
884     } else if (ModuleScopes.back().Module->Kind ==
885               Module::PrivateModuleFragment) {
886       Diag(ExportLoc, diag::err_export_in_private_module_fragment);
887       Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
888       D->setInvalidDecl();
889       return D;
890     }
891   }
892 
893   for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) {
894     if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
895       //   An export-declaration shall not appear directly or indirectly within
896       //   an unnamed namespace [...]
897       if (ND->isAnonymousNamespace()) {
898         Diag(ExportLoc, diag::err_export_within_anonymous_namespace);
899         Diag(ND->getLocation(), diag::note_anonymous_namespace);
900         // Don't diagnose internal-linkage declarations in this region.
901         D->setInvalidDecl();
902         return D;
903       }
904 
905       //   A declaration is exported if it is [...] a namespace-definition
906       //   that contains an exported declaration.
907       //
908       // Defer exporting the namespace until after we leave it, in order to
909       // avoid marking all subsequent declarations in the namespace as exported.
910       if (!getLangOpts().HLSL && !DeferredExportedNamespaces.insert(ND).second)
911         break;
912     }
913   }
914 
915   //   [...] its declaration or declaration-seq shall not contain an
916   //   export-declaration.
917   if (auto *ED = getEnclosingExportDecl(D)) {
918     Diag(ExportLoc, diag::err_export_within_export);
919     if (ED->hasBraces())
920       Diag(ED->getLocation(), diag::note_export);
921     D->setInvalidDecl();
922     return D;
923   }
924 
925   if (!getLangOpts().HLSL)
926     D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
927 
928   return D;
929 }
930 
931 static bool checkExportedDecl(Sema &, Decl *, SourceLocation);
932 
933 /// Check that it's valid to export all the declarations in \p DC.
checkExportedDeclContext(Sema & S,DeclContext * DC,SourceLocation BlockStart)934 static bool checkExportedDeclContext(Sema &S, DeclContext *DC,
935                                      SourceLocation BlockStart) {
936   bool AllUnnamed = true;
937   for (auto *D : DC->decls())
938     AllUnnamed &= checkExportedDecl(S, D, BlockStart);
939   return AllUnnamed;
940 }
941 
942 /// Check that it's valid to export \p D.
checkExportedDecl(Sema & S,Decl * D,SourceLocation BlockStart)943 static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
944 
945   // HLSL: export declaration is valid only on functions
946   if (S.getLangOpts().HLSL) {
947     // Export-within-export was already diagnosed in ActOnStartExportDecl
948     if (!isa<FunctionDecl, ExportDecl>(D)) {
949       S.Diag(D->getBeginLoc(), diag::err_hlsl_export_not_on_function);
950       D->setInvalidDecl();
951       return false;
952     }
953   }
954 
955   //  C++20 [module.interface]p3:
956   //   [...] it shall not declare a name with internal linkage.
957   bool HasName = false;
958   if (auto *ND = dyn_cast<NamedDecl>(D)) {
959     // Don't diagnose anonymous union objects; we'll diagnose their members
960     // instead.
961     HasName = (bool)ND->getDeclName();
962     if (HasName && ND->getFormalLinkage() == Linkage::Internal) {
963       S.Diag(ND->getLocation(), diag::err_export_internal) << ND;
964       if (BlockStart.isValid())
965         S.Diag(BlockStart, diag::note_export);
966       return false;
967     }
968   }
969 
970   // C++2a [module.interface]p5:
971   //   all entities to which all of the using-declarators ultimately refer
972   //   shall have been introduced with a name having external linkage
973   if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
974     NamedDecl *Target = USD->getUnderlyingDecl();
975     Linkage Lk = Target->getFormalLinkage();
976     if (Lk == Linkage::Internal || Lk == Linkage::Module) {
977       S.Diag(USD->getLocation(), diag::err_export_using_internal)
978           << (Lk == Linkage::Internal ? 0 : 1) << Target;
979       S.Diag(Target->getLocation(), diag::note_using_decl_target);
980       if (BlockStart.isValid())
981         S.Diag(BlockStart, diag::note_export);
982       return false;
983     }
984   }
985 
986   // Recurse into namespace-scope DeclContexts. (Only namespace-scope
987   // declarations are exported).
988   if (auto *DC = dyn_cast<DeclContext>(D)) {
989     if (!isa<NamespaceDecl>(D))
990       return true;
991 
992     if (auto *ND = dyn_cast<NamedDecl>(D)) {
993       if (!ND->getDeclName()) {
994         S.Diag(ND->getLocation(), diag::err_export_anon_ns_internal);
995         if (BlockStart.isValid())
996           S.Diag(BlockStart, diag::note_export);
997         return false;
998       } else if (!DC->decls().empty() &&
999                  DC->getRedeclContext()->isFileContext()) {
1000         return checkExportedDeclContext(S, DC, BlockStart);
1001       }
1002     }
1003   }
1004   return true;
1005 }
1006 
ActOnFinishExportDecl(Scope * S,Decl * D,SourceLocation RBraceLoc)1007 Decl *Sema::ActOnFinishExportDecl(Scope *S, Decl *D, SourceLocation RBraceLoc) {
1008   auto *ED = cast<ExportDecl>(D);
1009   if (RBraceLoc.isValid())
1010     ED->setRBraceLoc(RBraceLoc);
1011 
1012   PopDeclContext();
1013 
1014   if (!D->isInvalidDecl()) {
1015     SourceLocation BlockStart =
1016         ED->hasBraces() ? ED->getBeginLoc() : SourceLocation();
1017     for (auto *Child : ED->decls()) {
1018       checkExportedDecl(*this, Child, BlockStart);
1019       if (auto *FD = dyn_cast<FunctionDecl>(Child)) {
1020         // [dcl.inline]/7
1021         // If an inline function or variable that is attached to a named module
1022         // is declared in a definition domain, it shall be defined in that
1023         // domain.
1024         // So, if the current declaration does not have a definition, we must
1025         // check at the end of the TU (or when the PMF starts) to see that we
1026         // have a definition at that point.
1027         if (FD->isInlineSpecified() && !FD->isDefined())
1028           PendingInlineFuncDecls.insert(FD);
1029       }
1030     }
1031   }
1032 
1033   // Anything exported from a module should never be considered unused.
1034   for (auto *Exported : ED->decls())
1035     Exported->markUsed(getASTContext());
1036 
1037   return D;
1038 }
1039 
PushGlobalModuleFragment(SourceLocation BeginLoc)1040 Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc) {
1041   // We shouldn't create new global module fragment if there is already
1042   // one.
1043   if (!TheGlobalModuleFragment) {
1044     ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
1045     TheGlobalModuleFragment = Map.createGlobalModuleFragmentForModuleUnit(
1046         BeginLoc, getCurrentModule());
1047   }
1048 
1049   assert(TheGlobalModuleFragment && "module creation should not fail");
1050 
1051   // Enter the scope of the global module.
1052   ModuleScopes.push_back({BeginLoc, TheGlobalModuleFragment,
1053                           /*OuterVisibleModules=*/{}});
1054   VisibleModules.setVisible(TheGlobalModuleFragment, BeginLoc);
1055 
1056   return TheGlobalModuleFragment;
1057 }
1058 
PopGlobalModuleFragment()1059 void Sema::PopGlobalModuleFragment() {
1060   assert(!ModuleScopes.empty() &&
1061          getCurrentModule()->isExplicitGlobalModule() &&
1062          "left the wrong module scope, which is not global module fragment");
1063   ModuleScopes.pop_back();
1064 }
1065 
PushImplicitGlobalModuleFragment(SourceLocation BeginLoc)1066 Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc) {
1067   if (!TheImplicitGlobalModuleFragment) {
1068     ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
1069     TheImplicitGlobalModuleFragment =
1070         Map.createImplicitGlobalModuleFragmentForModuleUnit(BeginLoc,
1071                                                             getCurrentModule());
1072   }
1073   assert(TheImplicitGlobalModuleFragment && "module creation should not fail");
1074 
1075   // Enter the scope of the global module.
1076   ModuleScopes.push_back({BeginLoc, TheImplicitGlobalModuleFragment,
1077                           /*OuterVisibleModules=*/{}});
1078   VisibleModules.setVisible(TheImplicitGlobalModuleFragment, BeginLoc);
1079   return TheImplicitGlobalModuleFragment;
1080 }
1081 
PopImplicitGlobalModuleFragment()1082 void Sema::PopImplicitGlobalModuleFragment() {
1083   assert(!ModuleScopes.empty() &&
1084          getCurrentModule()->isImplicitGlobalModule() &&
1085          "left the wrong module scope, which is not global module fragment");
1086   ModuleScopes.pop_back();
1087 }
1088 
isCurrentModulePurview() const1089 bool Sema::isCurrentModulePurview() const {
1090   if (!getCurrentModule())
1091     return false;
1092 
1093   /// Does this Module scope describe part of the purview of a standard named
1094   /// C++ module?
1095   switch (getCurrentModule()->Kind) {
1096   case Module::ModuleInterfaceUnit:
1097   case Module::ModuleImplementationUnit:
1098   case Module::ModulePartitionInterface:
1099   case Module::ModulePartitionImplementation:
1100   case Module::PrivateModuleFragment:
1101   case Module::ImplicitGlobalModuleFragment:
1102     return true;
1103   default:
1104     return false;
1105   }
1106 }
1107