xref: /freebsd/contrib/llvm-project/clang/lib/Sema/SemaModule.cpp (revision 770cf0a5f02dc8983a89c6568d741fbc25baa999)
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 
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.
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
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
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
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 
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.
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
264 Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
265                       ModuleDeclKind MDK, ModuleIdPath Path,
266                       ModuleIdPath Partition, ModuleImportState &ImportState,
267                       bool IntroducerIsFirstPPToken) {
268   assert(getLangOpts().CPlusPlusModules &&
269          "should only have module decl in standard C++ modules");
270 
271   bool SeenGMF = ImportState == ModuleImportState::GlobalFragment;
272   // If any of the steps here fail, we count that as invalidating C++20
273   // module state;
274   ImportState = ModuleImportState::NotACXX20Module;
275 
276   bool IsPartition = !Partition.empty();
277   if (IsPartition)
278     switch (MDK) {
279     case ModuleDeclKind::Implementation:
280       MDK = ModuleDeclKind::PartitionImplementation;
281       break;
282     case ModuleDeclKind::Interface:
283       MDK = ModuleDeclKind::PartitionInterface;
284       break;
285     default:
286       llvm_unreachable("how did we get a partition type set?");
287     }
288 
289   // A (non-partition) module implementation unit requires that we are not
290   // compiling a module of any kind.  A partition implementation emits an
291   // interface (and the AST for the implementation), which will subsequently
292   // be consumed to emit a binary.
293   // A module interface unit requires that we are not compiling a module map.
294   switch (getLangOpts().getCompilingModule()) {
295   case LangOptions::CMK_None:
296     // It's OK to compile a module interface as a normal translation unit.
297     break;
298 
299   case LangOptions::CMK_ModuleInterface:
300     if (MDK != ModuleDeclKind::Implementation)
301       break;
302 
303     // We were asked to compile a module interface unit but this is a module
304     // implementation unit.
305     Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch)
306       << FixItHint::CreateInsertion(ModuleLoc, "export ");
307     MDK = ModuleDeclKind::Interface;
308     break;
309 
310   case LangOptions::CMK_ModuleMap:
311     Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
312     return nullptr;
313 
314   case LangOptions::CMK_HeaderUnit:
315     Diag(ModuleLoc, diag::err_module_decl_in_header_unit);
316     return nullptr;
317   }
318 
319   assert(ModuleScopes.size() <= 1 && "expected to be at global module scope");
320 
321   // FIXME: Most of this work should be done by the preprocessor rather than
322   // here, in order to support macro import.
323 
324   // Only one module-declaration is permitted per source file.
325   if (isCurrentModulePurview()) {
326     Diag(ModuleLoc, diag::err_module_redeclaration);
327     Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
328          diag::note_prev_module_declaration);
329     return nullptr;
330   }
331 
332   assert((!getLangOpts().CPlusPlusModules ||
333           SeenGMF == (bool)this->TheGlobalModuleFragment) &&
334          "mismatched global module state");
335 
336   // In C++20, A module directive may only appear as the first preprocessing
337   // tokens in a file (excluding the global module fragment.).
338   if (getLangOpts().CPlusPlusModules && !IntroducerIsFirstPPToken && !SeenGMF) {
339     Diag(ModuleLoc, diag::err_module_decl_not_at_start);
340     SourceLocation BeginLoc = PP.getMainFileFirstPPTokenLoc();
341     Diag(BeginLoc, diag::note_global_module_introducer_missing)
342         << FixItHint::CreateInsertion(BeginLoc, "module;\n");
343   }
344 
345   // C++23 [module.unit]p1: ... The identifiers module and import shall not
346   // appear as identifiers in a module-name or module-partition. All
347   // module-names either beginning with an identifier consisting of std
348   // followed by zero or more digits or containing a reserved identifier
349   // ([lex.name]) are reserved and shall not be specified in a
350   // module-declaration; no diagnostic is required.
351 
352   // Test the first part of the path to see if it's std[0-9]+ but allow the
353   // name in a system header.
354   StringRef FirstComponentName = Path[0].getIdentifierInfo()->getName();
355   if (!getSourceManager().isInSystemHeader(Path[0].getLoc()) &&
356       (FirstComponentName == "std" ||
357        (FirstComponentName.starts_with("std") &&
358         llvm::all_of(FirstComponentName.drop_front(3), &llvm::isDigit))))
359     Diag(Path[0].getLoc(), diag::warn_reserved_module_name)
360         << Path[0].getIdentifierInfo();
361 
362   // Then test all of the components in the path to see if any of them are
363   // using another kind of reserved or invalid identifier.
364   for (auto Part : Path) {
365     if (DiagReservedModuleName(*this, Part.getIdentifierInfo(), Part.getLoc()))
366       return nullptr;
367   }
368 
369   // Flatten the dots in a module name. Unlike Clang's hierarchical module map
370   // modules, the dots here are just another character that can appear in a
371   // module name.
372   std::string ModuleName = stringFromPath(Path);
373   if (IsPartition) {
374     ModuleName += ":";
375     ModuleName += stringFromPath(Partition);
376   }
377   // If a module name was explicitly specified on the command line, it must be
378   // correct.
379   if (!getLangOpts().CurrentModule.empty() &&
380       getLangOpts().CurrentModule != ModuleName) {
381     Diag(Path.front().getLoc(), diag::err_current_module_name_mismatch)
382         << SourceRange(Path.front().getLoc(), IsPartition
383                                                   ? Partition.back().getLoc()
384                                                   : Path.back().getLoc())
385         << getLangOpts().CurrentModule;
386     return nullptr;
387   }
388   const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
389 
390   auto &Map = PP.getHeaderSearchInfo().getModuleMap();
391   Module *Mod;                 // The module we are creating.
392   Module *Interface = nullptr; // The interface for an implementation.
393   switch (MDK) {
394   case ModuleDeclKind::Interface:
395   case ModuleDeclKind::PartitionInterface: {
396     // We can't have parsed or imported a definition of this module or parsed a
397     // module map defining it already.
398     if (auto *M = Map.findOrLoadModule(ModuleName)) {
399       Diag(Path[0].getLoc(), diag::err_module_redefinition) << ModuleName;
400       if (M->DefinitionLoc.isValid())
401         Diag(M->DefinitionLoc, diag::note_prev_module_definition);
402       else if (OptionalFileEntryRef FE = M->getASTFile())
403         Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
404             << FE->getName();
405       Mod = M;
406       break;
407     }
408 
409     // Create a Module for the module that we're defining.
410     Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
411     if (MDK == ModuleDeclKind::PartitionInterface)
412       Mod->Kind = Module::ModulePartitionInterface;
413     assert(Mod && "module creation should not fail");
414     break;
415   }
416 
417   case ModuleDeclKind::Implementation: {
418     // C++20 A module-declaration that contains neither an export-
419     // keyword nor a module-partition implicitly imports the primary
420     // module interface unit of the module as if by a module-import-
421     // declaration.
422     IdentifierLoc ModuleNameLoc(Path[0].getLoc(),
423                                 PP.getIdentifierInfo(ModuleName));
424 
425     // The module loader will assume we're trying to import the module that
426     // we're building if `LangOpts.CurrentModule` equals to 'ModuleName'.
427     // Change the value for `LangOpts.CurrentModule` temporarily to make the
428     // module loader work properly.
429     const_cast<LangOptions &>(getLangOpts()).CurrentModule = "";
430     Interface = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
431                                              Module::AllVisible,
432                                              /*IsInclusionDirective=*/false);
433     const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
434 
435     if (!Interface) {
436       Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
437       // Create an empty module interface unit for error recovery.
438       Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
439     } else {
440       Mod = Map.createModuleForImplementationUnit(ModuleLoc, ModuleName);
441     }
442   } break;
443 
444   case ModuleDeclKind::PartitionImplementation:
445     // Create an interface, but note that it is an implementation
446     // unit.
447     Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
448     Mod->Kind = Module::ModulePartitionImplementation;
449     break;
450   }
451 
452   if (!this->TheGlobalModuleFragment) {
453     ModuleScopes.push_back({});
454     if (getLangOpts().ModulesLocalVisibility)
455       ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
456   } else {
457     // We're done with the global module fragment now.
458     ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global);
459   }
460 
461   // Switch from the global module fragment (if any) to the named module.
462   ModuleScopes.back().BeginLoc = StartLoc;
463   ModuleScopes.back().Module = Mod;
464   VisibleModules.setVisible(Mod, ModuleLoc);
465 
466   // From now on, we have an owning module for all declarations we see.
467   // In C++20 modules, those declaration would be reachable when imported
468   // unless explicitily exported.
469   // Otherwise, those declarations are module-private unless explicitly
470   // exported.
471   auto *TU = Context.getTranslationUnitDecl();
472   TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported);
473   TU->setLocalOwningModule(Mod);
474 
475   // We are in the module purview, but before any other (non import)
476   // statements, so imports are allowed.
477   ImportState = ModuleImportState::ImportAllowed;
478 
479   getASTContext().setCurrentNamedModule(Mod);
480 
481   if (auto *Listener = getASTMutationListener())
482     Listener->EnteringModulePurview();
483 
484   // We already potentially made an implicit import (in the case of a module
485   // implementation unit importing its interface).  Make this module visible
486   // and return the import decl to be added to the current TU.
487   if (Interface) {
488 
489     makeTransitiveImportsVisible(getASTContext(), VisibleModules, Interface,
490                                  Mod, ModuleLoc,
491                                  /*IsImportingPrimaryModuleInterface=*/true);
492 
493     // Make the import decl for the interface in the impl module.
494     ImportDecl *Import = ImportDecl::Create(Context, CurContext, ModuleLoc,
495                                             Interface, Path[0].getLoc());
496     CurContext->addDecl(Import);
497 
498     // Sequence initialization of the imported module before that of the current
499     // module, if any.
500     Context.addModuleInitializer(ModuleScopes.back().Module, Import);
501     Mod->Imports.insert(Interface); // As if we imported it.
502     // Also save this as a shortcut to checking for decls in the interface
503     ThePrimaryInterface = Interface;
504     // If we made an implicit import of the module interface, then return the
505     // imported module decl.
506     return ConvertDeclToDeclGroup(Import);
507   }
508 
509   return nullptr;
510 }
511 
512 Sema::DeclGroupPtrTy
513 Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,
514                                      SourceLocation PrivateLoc) {
515   // C++20 [basic.link]/2:
516   //   A private-module-fragment shall appear only in a primary module
517   //   interface unit.
518   switch (ModuleScopes.empty() ? Module::ExplicitGlobalModuleFragment
519                                : ModuleScopes.back().Module->Kind) {
520   case Module::ModuleMapModule:
521   case Module::ExplicitGlobalModuleFragment:
522   case Module::ImplicitGlobalModuleFragment:
523   case Module::ModulePartitionImplementation:
524   case Module::ModulePartitionInterface:
525   case Module::ModuleHeaderUnit:
526     Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
527     return nullptr;
528 
529   case Module::PrivateModuleFragment:
530     Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
531     Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
532     return nullptr;
533 
534   case Module::ModuleImplementationUnit:
535     Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface);
536     Diag(ModuleScopes.back().BeginLoc,
537          diag::note_not_module_interface_add_export)
538         << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
539     return nullptr;
540 
541   case Module::ModuleInterfaceUnit:
542     break;
543   }
544 
545   // FIXME: Check that this translation unit does not import any partitions;
546   // such imports would violate [basic.link]/2's "shall be the only module unit"
547   // restriction.
548 
549   // We've finished the public fragment of the translation unit.
550   ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal);
551 
552   auto &Map = PP.getHeaderSearchInfo().getModuleMap();
553   Module *PrivateModuleFragment =
554       Map.createPrivateModuleFragmentForInterfaceUnit(
555           ModuleScopes.back().Module, PrivateLoc);
556   assert(PrivateModuleFragment && "module creation should not fail");
557 
558   // Enter the scope of the private module fragment.
559   ModuleScopes.push_back({});
560   ModuleScopes.back().BeginLoc = ModuleLoc;
561   ModuleScopes.back().Module = PrivateModuleFragment;
562   VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
563 
564   // All declarations created from now on are scoped to the private module
565   // fragment (and are neither visible nor reachable in importers of the module
566   // interface).
567   auto *TU = Context.getTranslationUnitDecl();
568   TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
569   TU->setLocalOwningModule(PrivateModuleFragment);
570 
571   // FIXME: Consider creating an explicit representation of this declaration.
572   return nullptr;
573 }
574 
575 DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
576                                    SourceLocation ExportLoc,
577                                    SourceLocation ImportLoc, ModuleIdPath Path,
578                                    bool IsPartition) {
579   assert((!IsPartition || getLangOpts().CPlusPlusModules) &&
580          "partition seen in non-C++20 code?");
581 
582   // For a C++20 module name, flatten into a single identifier with the source
583   // location of the first component.
584   IdentifierLoc ModuleNameLoc;
585 
586   std::string ModuleName;
587   if (IsPartition) {
588     // We already checked that we are in a module purview in the parser.
589     assert(!ModuleScopes.empty() && "in a module purview, but no module?");
590     Module *NamedMod = ModuleScopes.back().Module;
591     // If we are importing into a partition, find the owning named module,
592     // otherwise, the name of the importing named module.
593     ModuleName = NamedMod->getPrimaryModuleInterfaceName().str();
594     ModuleName += ":";
595     ModuleName += stringFromPath(Path);
596     ModuleNameLoc =
597         IdentifierLoc(Path[0].getLoc(), PP.getIdentifierInfo(ModuleName));
598     Path = ModuleIdPath(ModuleNameLoc);
599   } else if (getLangOpts().CPlusPlusModules) {
600     ModuleName = stringFromPath(Path);
601     ModuleNameLoc =
602         IdentifierLoc(Path[0].getLoc(), PP.getIdentifierInfo(ModuleName));
603     Path = ModuleIdPath(ModuleNameLoc);
604   }
605 
606   // Diagnose self-import before attempting a load.
607   // [module.import]/9
608   // A module implementation unit of a module M that is not a module partition
609   // shall not contain a module-import-declaration nominating M.
610   // (for an implementation, the module interface is imported implicitly,
611   //  but that's handled in the module decl code).
612 
613   if (getLangOpts().CPlusPlusModules && isCurrentModulePurview() &&
614       getCurrentModule()->Name == ModuleName) {
615     Diag(ImportLoc, diag::err_module_self_import_cxx20)
616         << ModuleName << currentModuleIsImplementation();
617     return true;
618   }
619 
620   Module *Mod = getModuleLoader().loadModule(
621       ImportLoc, Path, Module::AllVisible, /*IsInclusionDirective=*/false);
622   if (!Mod)
623     return true;
624 
625   if (!Mod->isInterfaceOrPartition() && !ModuleName.empty() &&
626       !getLangOpts().ObjC) {
627     Diag(ImportLoc, diag::err_module_import_non_interface_nor_parition)
628         << ModuleName;
629     return true;
630   }
631 
632   return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
633 }
634 
635 /// Determine whether \p D is lexically within an export-declaration.
636 static const ExportDecl *getEnclosingExportDecl(const Decl *D) {
637   for (auto *DC = D->getLexicalDeclContext(); DC; DC = DC->getLexicalParent())
638     if (auto *ED = dyn_cast<ExportDecl>(DC))
639       return ED;
640   return nullptr;
641 }
642 
643 DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
644                                    SourceLocation ExportLoc,
645                                    SourceLocation ImportLoc, Module *Mod,
646                                    ModuleIdPath Path) {
647   if (Mod->isHeaderUnit())
648     Diag(ImportLoc, diag::warn_experimental_header_unit);
649 
650   if (Mod->isNamedModule())
651     makeTransitiveImportsVisible(getASTContext(), VisibleModules, Mod,
652                                  getCurrentModule(), ImportLoc);
653   else
654     VisibleModules.setVisible(Mod, ImportLoc);
655 
656   assert((!Mod->isModulePartitionImplementation() || getCurrentModule()) &&
657          "We can only import a partition unit in a named module.");
658   if (Mod->isModulePartitionImplementation() &&
659       getCurrentModule()->isModuleInterfaceUnit())
660     Diag(ImportLoc,
661          diag::warn_import_implementation_partition_unit_in_interface_unit)
662         << Mod->Name;
663 
664   checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
665 
666   // FIXME: we should support importing a submodule within a different submodule
667   // of the same top-level module. Until we do, make it an error rather than
668   // silently ignoring the import.
669   // FIXME: Should we warn on a redundant import of the current module?
670   if (Mod->isForBuilding(getLangOpts())) {
671     Diag(ImportLoc, getLangOpts().isCompilingModule()
672                         ? diag::err_module_self_import
673                         : diag::err_module_import_in_implementation)
674         << Mod->getFullModuleName() << getLangOpts().CurrentModule;
675   }
676 
677   SmallVector<SourceLocation, 2> IdentifierLocs;
678 
679   if (Path.empty()) {
680     // If this was a header import, pad out with dummy locations.
681     // FIXME: Pass in and use the location of the header-name token in this
682     // case.
683     for (Module *ModCheck = Mod; ModCheck; ModCheck = ModCheck->Parent)
684       IdentifierLocs.push_back(SourceLocation());
685   } else if (getLangOpts().CPlusPlusModules && !Mod->Parent) {
686     // A single identifier for the whole name.
687     IdentifierLocs.push_back(Path[0].getLoc());
688   } else {
689     Module *ModCheck = Mod;
690     for (unsigned I = 0, N = Path.size(); I != N; ++I) {
691       // If we've run out of module parents, just drop the remaining
692       // identifiers.  We need the length to be consistent.
693       if (!ModCheck)
694         break;
695       ModCheck = ModCheck->Parent;
696 
697       IdentifierLocs.push_back(Path[I].getLoc());
698     }
699   }
700 
701   ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc,
702                                           Mod, IdentifierLocs);
703   CurContext->addDecl(Import);
704 
705   // Sequence initialization of the imported module before that of the current
706   // module, if any.
707   if (!ModuleScopes.empty())
708     Context.addModuleInitializer(ModuleScopes.back().Module, Import);
709 
710   // A module (partition) implementation unit shall not be exported.
711   if (getLangOpts().CPlusPlusModules && ExportLoc.isValid() &&
712       Mod->Kind == Module::ModuleKind::ModulePartitionImplementation) {
713     Diag(ExportLoc, diag::err_export_partition_impl)
714         << SourceRange(ExportLoc, Path.back().getLoc());
715   } else if (ExportLoc.isValid() &&
716              (ModuleScopes.empty() || currentModuleIsImplementation())) {
717     // [module.interface]p1:
718     // An export-declaration shall inhabit a namespace scope and appear in the
719     // purview of a module interface unit.
720     Diag(ExportLoc, diag::err_export_not_in_module_interface);
721   } else if (!ModuleScopes.empty()) {
722     // Re-export the module if the imported module is exported.
723     // Note that we don't need to add re-exported module to Imports field
724     // since `Exports` implies the module is imported already.
725     if (ExportLoc.isValid() || getEnclosingExportDecl(Import))
726       getCurrentModule()->Exports.emplace_back(Mod, false);
727     else
728       getCurrentModule()->Imports.insert(Mod);
729   }
730 
731   return Import;
732 }
733 
734 void Sema::ActOnAnnotModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
735   checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
736   BuildModuleInclude(DirectiveLoc, Mod);
737 }
738 
739 void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
740   // Determine whether we're in the #include buffer for a module. The #includes
741   // in that buffer do not qualify as module imports; they're just an
742   // implementation detail of us building the module.
743   //
744   // FIXME: Should we even get ActOnAnnotModuleInclude calls for those?
745   bool IsInModuleIncludes =
746       TUKind == TU_ClangModule &&
747       getSourceManager().isWrittenInMainFile(DirectiveLoc);
748 
749   // If we are really importing a module (not just checking layering) due to an
750   // #include in the main file, synthesize an ImportDecl.
751   if (getLangOpts().Modules && !IsInModuleIncludes) {
752     TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
753     ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
754                                                      DirectiveLoc, Mod,
755                                                      DirectiveLoc);
756     if (!ModuleScopes.empty())
757       Context.addModuleInitializer(ModuleScopes.back().Module, ImportD);
758     TU->addDecl(ImportD);
759     Consumer.HandleImplicitImportDecl(ImportD);
760   }
761 
762   getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc);
763   VisibleModules.setVisible(Mod, DirectiveLoc);
764 
765   if (getLangOpts().isCompilingModule()) {
766     Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
767         getLangOpts().CurrentModule, DirectiveLoc, false, false);
768     (void)ThisModule;
769     assert(ThisModule && "was expecting a module if building one");
770   }
771 }
772 
773 void Sema::ActOnAnnotModuleBegin(SourceLocation DirectiveLoc, Module *Mod) {
774   checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
775 
776   ModuleScopes.push_back({});
777   ModuleScopes.back().Module = Mod;
778   if (getLangOpts().ModulesLocalVisibility)
779     ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
780 
781   VisibleModules.setVisible(Mod, DirectiveLoc);
782 
783   // The enclosing context is now part of this module.
784   // FIXME: Consider creating a child DeclContext to hold the entities
785   // lexically within the module.
786   if (getLangOpts().trackLocalOwningModule()) {
787     for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
788       cast<Decl>(DC)->setModuleOwnershipKind(
789           getLangOpts().ModulesLocalVisibility
790               ? Decl::ModuleOwnershipKind::VisibleWhenImported
791               : Decl::ModuleOwnershipKind::Visible);
792       cast<Decl>(DC)->setLocalOwningModule(Mod);
793     }
794   }
795 }
796 
797 void Sema::ActOnAnnotModuleEnd(SourceLocation EomLoc, Module *Mod) {
798   if (getLangOpts().ModulesLocalVisibility) {
799     VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules);
800     // Leaving a module hides namespace names, so our visible namespace cache
801     // is now out of date.
802     VisibleNamespaceCache.clear();
803   }
804 
805   assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod &&
806          "left the wrong module scope");
807   ModuleScopes.pop_back();
808 
809   // We got to the end of processing a local module. Create an
810   // ImportDecl as we would for an imported module.
811   FileID File = getSourceManager().getFileID(EomLoc);
812   SourceLocation DirectiveLoc;
813   if (EomLoc == getSourceManager().getLocForEndOfFile(File)) {
814     // We reached the end of a #included module header. Use the #include loc.
815     assert(File != getSourceManager().getMainFileID() &&
816            "end of submodule in main source file");
817     DirectiveLoc = getSourceManager().getIncludeLoc(File);
818   } else {
819     // We reached an EOM pragma. Use the pragma location.
820     DirectiveLoc = EomLoc;
821   }
822   BuildModuleInclude(DirectiveLoc, Mod);
823 
824   // Any further declarations are in whatever module we returned to.
825   if (getLangOpts().trackLocalOwningModule()) {
826     // The parser guarantees that this is the same context that we entered
827     // the module within.
828     for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
829       cast<Decl>(DC)->setLocalOwningModule(getCurrentModule());
830       if (!getCurrentModule())
831         cast<Decl>(DC)->setModuleOwnershipKind(
832             Decl::ModuleOwnershipKind::Unowned);
833     }
834   }
835 }
836 
837 void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc,
838                                                       Module *Mod) {
839   // Bail if we're not allowed to implicitly import a module here.
840   if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery ||
841       VisibleModules.isVisible(Mod))
842     return;
843 
844   // Create the implicit import declaration.
845   TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
846   ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
847                                                    Loc, Mod, Loc);
848   TU->addDecl(ImportD);
849   Consumer.HandleImplicitImportDecl(ImportD);
850 
851   // Make the module visible.
852   getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc);
853   VisibleModules.setVisible(Mod, Loc);
854 }
855 
856 Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
857                                  SourceLocation LBraceLoc) {
858   ExportDecl *D = ExportDecl::Create(Context, CurContext, ExportLoc);
859 
860   // Set this temporarily so we know the export-declaration was braced.
861   D->setRBraceLoc(LBraceLoc);
862 
863   CurContext->addDecl(D);
864   PushDeclContext(S, D);
865 
866   // C++2a [module.interface]p1:
867   //   An export-declaration shall appear only [...] in the purview of a module
868   //   interface unit. An export-declaration shall not appear directly or
869   //   indirectly within [...] a private-module-fragment.
870   if (!getLangOpts().HLSL) {
871     if (!isCurrentModulePurview()) {
872       Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
873       D->setInvalidDecl();
874       return D;
875     } else if (currentModuleIsImplementation()) {
876       Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
877       Diag(ModuleScopes.back().BeginLoc,
878           diag::note_not_module_interface_add_export)
879           << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
880       D->setInvalidDecl();
881       return D;
882     } else if (ModuleScopes.back().Module->Kind ==
883               Module::PrivateModuleFragment) {
884       Diag(ExportLoc, diag::err_export_in_private_module_fragment);
885       Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
886       D->setInvalidDecl();
887       return D;
888     }
889   }
890 
891   for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) {
892     if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
893       //   An export-declaration shall not appear directly or indirectly within
894       //   an unnamed namespace [...]
895       if (ND->isAnonymousNamespace()) {
896         Diag(ExportLoc, diag::err_export_within_anonymous_namespace);
897         Diag(ND->getLocation(), diag::note_anonymous_namespace);
898         // Don't diagnose internal-linkage declarations in this region.
899         D->setInvalidDecl();
900         return D;
901       }
902 
903       //   A declaration is exported if it is [...] a namespace-definition
904       //   that contains an exported declaration.
905       //
906       // Defer exporting the namespace until after we leave it, in order to
907       // avoid marking all subsequent declarations in the namespace as exported.
908       if (!getLangOpts().HLSL && !DeferredExportedNamespaces.insert(ND).second)
909         break;
910     }
911   }
912 
913   //   [...] its declaration or declaration-seq shall not contain an
914   //   export-declaration.
915   if (auto *ED = getEnclosingExportDecl(D)) {
916     Diag(ExportLoc, diag::err_export_within_export);
917     if (ED->hasBraces())
918       Diag(ED->getLocation(), diag::note_export);
919     D->setInvalidDecl();
920     return D;
921   }
922 
923   if (!getLangOpts().HLSL)
924     D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
925 
926   return D;
927 }
928 
929 static bool checkExportedDecl(Sema &, Decl *, SourceLocation);
930 
931 /// Check that it's valid to export all the declarations in \p DC.
932 static bool checkExportedDeclContext(Sema &S, DeclContext *DC,
933                                      SourceLocation BlockStart) {
934   bool AllUnnamed = true;
935   for (auto *D : DC->decls())
936     AllUnnamed &= checkExportedDecl(S, D, BlockStart);
937   return AllUnnamed;
938 }
939 
940 /// Check that it's valid to export \p D.
941 static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
942 
943   // HLSL: export declaration is valid only on functions
944   if (S.getLangOpts().HLSL) {
945     // Export-within-export was already diagnosed in ActOnStartExportDecl
946     if (!isa<FunctionDecl, ExportDecl>(D)) {
947       S.Diag(D->getBeginLoc(), diag::err_hlsl_export_not_on_function);
948       D->setInvalidDecl();
949       return false;
950     }
951   }
952 
953   //  C++20 [module.interface]p3:
954   //   [...] it shall not declare a name with internal linkage.
955   bool HasName = false;
956   if (auto *ND = dyn_cast<NamedDecl>(D)) {
957     // Don't diagnose anonymous union objects; we'll diagnose their members
958     // instead.
959     HasName = (bool)ND->getDeclName();
960     if (HasName && ND->getFormalLinkage() == Linkage::Internal) {
961       S.Diag(ND->getLocation(), diag::err_export_internal) << ND;
962       if (BlockStart.isValid())
963         S.Diag(BlockStart, diag::note_export);
964       return false;
965     }
966   }
967 
968   // C++2a [module.interface]p5:
969   //   all entities to which all of the using-declarators ultimately refer
970   //   shall have been introduced with a name having external linkage
971   if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
972     NamedDecl *Target = USD->getUnderlyingDecl();
973     Linkage Lk = Target->getFormalLinkage();
974     if (Lk == Linkage::Internal || Lk == Linkage::Module) {
975       S.Diag(USD->getLocation(), diag::err_export_using_internal)
976           << (Lk == Linkage::Internal ? 0 : 1) << Target;
977       S.Diag(Target->getLocation(), diag::note_using_decl_target);
978       if (BlockStart.isValid())
979         S.Diag(BlockStart, diag::note_export);
980       return false;
981     }
982   }
983 
984   // Recurse into namespace-scope DeclContexts. (Only namespace-scope
985   // declarations are exported).
986   if (auto *DC = dyn_cast<DeclContext>(D)) {
987     if (!isa<NamespaceDecl>(D))
988       return true;
989 
990     if (auto *ND = dyn_cast<NamedDecl>(D)) {
991       if (!ND->getDeclName()) {
992         S.Diag(ND->getLocation(), diag::err_export_anon_ns_internal);
993         if (BlockStart.isValid())
994           S.Diag(BlockStart, diag::note_export);
995         return false;
996       } else if (!DC->decls().empty() &&
997                  DC->getRedeclContext()->isFileContext()) {
998         return checkExportedDeclContext(S, DC, BlockStart);
999       }
1000     }
1001   }
1002   return true;
1003 }
1004 
1005 Decl *Sema::ActOnFinishExportDecl(Scope *S, Decl *D, SourceLocation RBraceLoc) {
1006   auto *ED = cast<ExportDecl>(D);
1007   if (RBraceLoc.isValid())
1008     ED->setRBraceLoc(RBraceLoc);
1009 
1010   PopDeclContext();
1011 
1012   if (!D->isInvalidDecl()) {
1013     SourceLocation BlockStart =
1014         ED->hasBraces() ? ED->getBeginLoc() : SourceLocation();
1015     for (auto *Child : ED->decls()) {
1016       checkExportedDecl(*this, Child, BlockStart);
1017       if (auto *FD = dyn_cast<FunctionDecl>(Child)) {
1018         // [dcl.inline]/7
1019         // If an inline function or variable that is attached to a named module
1020         // is declared in a definition domain, it shall be defined in that
1021         // domain.
1022         // So, if the current declaration does not have a definition, we must
1023         // check at the end of the TU (or when the PMF starts) to see that we
1024         // have a definition at that point.
1025         if (FD->isInlineSpecified() && !FD->isDefined())
1026           PendingInlineFuncDecls.insert(FD);
1027       }
1028     }
1029   }
1030 
1031   // Anything exported from a module should never be considered unused.
1032   for (auto *Exported : ED->decls())
1033     Exported->markUsed(getASTContext());
1034 
1035   return D;
1036 }
1037 
1038 Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc) {
1039   // We shouldn't create new global module fragment if there is already
1040   // one.
1041   if (!TheGlobalModuleFragment) {
1042     ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
1043     TheGlobalModuleFragment = Map.createGlobalModuleFragmentForModuleUnit(
1044         BeginLoc, getCurrentModule());
1045   }
1046 
1047   assert(TheGlobalModuleFragment && "module creation should not fail");
1048 
1049   // Enter the scope of the global module.
1050   ModuleScopes.push_back({BeginLoc, TheGlobalModuleFragment,
1051                           /*OuterVisibleModules=*/{}});
1052   VisibleModules.setVisible(TheGlobalModuleFragment, BeginLoc);
1053 
1054   return TheGlobalModuleFragment;
1055 }
1056 
1057 void Sema::PopGlobalModuleFragment() {
1058   assert(!ModuleScopes.empty() &&
1059          getCurrentModule()->isExplicitGlobalModule() &&
1060          "left the wrong module scope, which is not global module fragment");
1061   ModuleScopes.pop_back();
1062 }
1063 
1064 Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc) {
1065   if (!TheImplicitGlobalModuleFragment) {
1066     ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
1067     TheImplicitGlobalModuleFragment =
1068         Map.createImplicitGlobalModuleFragmentForModuleUnit(BeginLoc,
1069                                                             getCurrentModule());
1070   }
1071   assert(TheImplicitGlobalModuleFragment && "module creation should not fail");
1072 
1073   // Enter the scope of the global module.
1074   ModuleScopes.push_back({BeginLoc, TheImplicitGlobalModuleFragment,
1075                           /*OuterVisibleModules=*/{}});
1076   VisibleModules.setVisible(TheImplicitGlobalModuleFragment, BeginLoc);
1077   return TheImplicitGlobalModuleFragment;
1078 }
1079 
1080 void Sema::PopImplicitGlobalModuleFragment() {
1081   assert(!ModuleScopes.empty() &&
1082          getCurrentModule()->isImplicitGlobalModule() &&
1083          "left the wrong module scope, which is not global module fragment");
1084   ModuleScopes.pop_back();
1085 }
1086 
1087 bool Sema::isCurrentModulePurview() const {
1088   if (!getCurrentModule())
1089     return false;
1090 
1091   /// Does this Module scope describe part of the purview of a standard named
1092   /// C++ module?
1093   switch (getCurrentModule()->Kind) {
1094   case Module::ModuleInterfaceUnit:
1095   case Module::ModuleImplementationUnit:
1096   case Module::ModulePartitionInterface:
1097   case Module::ModulePartitionImplementation:
1098   case Module::PrivateModuleFragment:
1099   case Module::ImplicitGlobalModuleFragment:
1100     return true;
1101   default:
1102     return false;
1103   }
1104 }
1105