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