1 //===- Module.h - Describe a module -----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Defines the clang::Module class, which describes a module in the
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_BASIC_MODULE_H
16 #define LLVM_CLANG_BASIC_MODULE_H
18 #include "clang/Basic/SourceLocation.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/PointerIntPair.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringMap.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/iterator_range.h"
52 /// Describes the name of a module.
53 using ModuleId = SmallVector<std::pair<std::string, SourceLocation>, 2>;
55 /// The signature of a module, which is a hash of the AST content.
56 struct ASTFileSignature : std::array<uint8_t, 20> {
57 using BaseT = std::array<uint8_t, 20>;
59 static constexpr size_t size = std::tuple_size<BaseT>::value;
61 ASTFileSignature(BaseT S = {{0}}) : BaseT(std::move(S)) {}
63 explicit operator bool() const { return *this != BaseT({{0}}); }
65 static ASTFileSignature create(StringRef Bytes) {
66 return create(Bytes.bytes_begin(), Bytes.bytes_end());
69 static ASTFileSignature createDISentinel() {
70 ASTFileSignature Sentinel;
75 template <typename InputIt>
76 static ASTFileSignature create(InputIt First, InputIt Last) {
77 assert(std::distance(First, Last) == size &&
78 "Wrong amount of bytes to create an ASTFileSignature");
80 ASTFileSignature Signature;
81 std::copy(First, Last, Signature.begin());
86 /// Describes a module or submodule.
89 /// The name of this module.
92 /// The location of the module definition.
93 SourceLocation DefinitionLoc;
96 /// This is a module that was defined by a module map and built out
100 /// This is a C++ Modules TS module interface unit.
103 /// This is a fragment of the global module within some C++ module.
104 GlobalModuleFragment,
106 /// This is the private module fragment within some C++ module.
107 PrivateModuleFragment,
110 /// The kind of this module.
111 ModuleKind Kind = ModuleMapModule;
113 /// The parent of this module. This will be NULL for the top-level
117 /// The build directory of this module. This is the directory in
118 /// which the module is notionally built, and relative to which its headers
120 const DirectoryEntry *Directory = nullptr;
122 /// The presumed file name for the module map defining this module.
123 /// Only non-empty when building from preprocessed source.
124 std::string PresumedModuleMapFile;
126 /// The umbrella header or directory.
127 const void *Umbrella = nullptr;
129 /// The module signature.
130 ASTFileSignature Signature;
132 /// The name of the umbrella entry, as written in the module map.
133 std::string UmbrellaAsWritten;
135 /// The module through which entities defined in this module will
136 /// eventually be exposed, for use in "private" modules.
137 std::string ExportAsModule;
139 /// Does this Module scope describe part of the purview of a named C++ module?
140 bool isModulePurview() const {
141 return Kind == ModuleInterfaceUnit || Kind == PrivateModuleFragment;
145 /// The submodules of this module, indexed by name.
146 std::vector<Module *> SubModules;
148 /// A mapping from the submodule name to the index into the
149 /// \c SubModules vector at which that submodule resides.
150 llvm::StringMap<unsigned> SubModuleIndex;
152 /// The AST file if this is a top-level module which has a
153 /// corresponding serialized AST file, or null otherwise.
154 const FileEntry *ASTFile = nullptr;
156 /// The top-level headers associated with this module.
157 llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
159 /// top-level header filenames that aren't resolved to FileEntries yet.
160 std::vector<std::string> TopHeaderNames;
162 /// Cache of modules visible to lookup in this module.
163 mutable llvm::DenseSet<const Module*> VisibleModulesCache;
165 /// The ID used when referencing this module within a VisibleModuleSet.
166 unsigned VisibilityID;
176 static const int NumHeaderKinds = HK_Excluded + 1;
178 /// Information about a header directive as found in the module map
181 std::string NameAsWritten;
182 const FileEntry *Entry;
184 explicit operator bool() { return Entry; }
187 /// Information about a directory name as found in the module map
189 struct DirectoryName {
190 std::string NameAsWritten;
191 const DirectoryEntry *Entry;
193 explicit operator bool() { return Entry; }
196 /// The headers that are part of this module.
197 SmallVector<Header, 2> Headers[5];
199 /// Stored information about a header directive that was found in the
200 /// module map file but has not been resolved to a file.
201 struct UnresolvedHeaderDirective {
202 HeaderKind Kind = HK_Normal;
203 SourceLocation FileNameLoc;
204 std::string FileName;
205 bool IsUmbrella = false;
206 bool HasBuiltinHeader = false;
207 Optional<off_t> Size;
208 Optional<time_t> ModTime;
211 /// Headers that are mentioned in the module map file but that we have not
212 /// yet attempted to resolve to a file on the file system.
213 SmallVector<UnresolvedHeaderDirective, 1> UnresolvedHeaders;
215 /// Headers that are mentioned in the module map file but could not be
216 /// found on the file system.
217 SmallVector<UnresolvedHeaderDirective, 1> MissingHeaders;
219 /// An individual requirement: a feature name and a flag indicating
220 /// the required state of that feature.
221 using Requirement = std::pair<std::string, bool>;
223 /// The set of language features required to use this module.
225 /// If any of these requirements are not available, the \c IsAvailable bit
226 /// will be false to indicate that this (sub)module is not available.
227 SmallVector<Requirement, 2> Requirements;
229 /// A module with the same name that shadows this module.
230 Module *ShadowingModule = nullptr;
232 /// Whether this module has declared itself unimportable, either because
233 /// it's missing a requirement from \p Requirements or because it's been
234 /// shadowed by another module.
235 unsigned IsUnimportable : 1;
237 /// Whether we tried and failed to load a module file for this module.
238 unsigned HasIncompatibleModuleFile : 1;
240 /// Whether this module is available in the current translation unit.
242 /// If the module is missing headers or does not meet all requirements then
243 /// this bit will be 0.
244 unsigned IsAvailable : 1;
246 /// Whether this module was loaded from a module file.
247 unsigned IsFromModuleFile : 1;
249 /// Whether this is a framework module.
250 unsigned IsFramework : 1;
252 /// Whether this is an explicit submodule.
253 unsigned IsExplicit : 1;
255 /// Whether this is a "system" module (which assumes that all
256 /// headers in it are system headers).
257 unsigned IsSystem : 1;
259 /// Whether this is an 'extern "C"' module (which implicitly puts all
260 /// headers in it within an 'extern "C"' block, and allows the module to be
261 /// imported within such a block).
262 unsigned IsExternC : 1;
264 /// Whether this is an inferred submodule (module * { ... }).
265 unsigned IsInferred : 1;
267 /// Whether we should infer submodules for this module based on
270 /// Submodules can only be inferred for modules with an umbrella header.
271 unsigned InferSubmodules : 1;
273 /// Whether, when inferring submodules, the inferred submodules
274 /// should be explicit.
275 unsigned InferExplicitSubmodules : 1;
277 /// Whether, when inferring submodules, the inferr submodules should
278 /// export all modules they import (e.g., the equivalent of "export *").
279 unsigned InferExportWildcard : 1;
281 /// Whether the set of configuration macros is exhaustive.
283 /// When the set of configuration macros is exhaustive, meaning
284 /// that no identifier not in this list should affect how the module is
286 unsigned ConfigMacrosExhaustive : 1;
288 /// Whether files in this module can only include non-modular headers
289 /// and headers from used modules.
290 unsigned NoUndeclaredIncludes : 1;
292 /// Whether this module came from a "private" module map, found next
293 /// to a regular (public) module map.
294 unsigned ModuleMapIsPrivate : 1;
296 /// Whether Umbrella is a directory or header.
297 unsigned HasUmbrellaDir : 1;
299 /// Describes the visibility of the various names within a
300 /// particular module.
301 enum NameVisibilityKind {
302 /// All of the names in this module are hidden.
304 /// All of the names in this module are visible.
308 /// The visibility of names within this particular module.
309 NameVisibilityKind NameVisibility;
311 /// The location of the inferred submodule.
312 SourceLocation InferredSubmoduleLoc;
314 /// The set of modules imported by this module, and on which this
316 llvm::SmallSetVector<Module *, 2> Imports;
318 /// Describes an exported module.
320 /// The pointer is the module being re-exported, while the bit will be true
321 /// to indicate that this is a wildcard export.
322 using ExportDecl = llvm::PointerIntPair<Module *, 1, bool>;
324 /// The set of export declarations.
325 SmallVector<ExportDecl, 2> Exports;
327 /// Describes an exported module that has not yet been resolved
328 /// (perhaps because the module it refers to has not yet been loaded).
329 struct UnresolvedExportDecl {
330 /// The location of the 'export' keyword in the module map file.
331 SourceLocation ExportLoc;
333 /// The name of the module.
336 /// Whether this export declaration ends in a wildcard, indicating
337 /// that all of its submodules should be exported (rather than the named
342 /// The set of export declarations that have yet to be resolved.
343 SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
345 /// The directly used modules.
346 SmallVector<Module *, 2> DirectUses;
348 /// The set of use declarations that have yet to be resolved.
349 SmallVector<ModuleId, 2> UnresolvedDirectUses;
351 /// A library or framework to link against when an entity from this
354 LinkLibrary() = default;
355 LinkLibrary(const std::string &Library, bool IsFramework)
356 : Library(Library), IsFramework(IsFramework) {}
358 /// The library to link against.
360 /// This will typically be a library or framework name, but can also
361 /// be an absolute path to the library or framework.
364 /// Whether this is a framework rather than a library.
365 bool IsFramework = false;
368 /// The set of libraries or frameworks to link against when
369 /// an entity from this module is used.
370 llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
372 /// Autolinking uses the framework name for linking purposes
373 /// when this is false and the export_as name otherwise.
374 bool UseExportAsModuleLinkName = false;
376 /// The set of "configuration macros", which are macros that
377 /// (intentionally) change how this module is built.
378 std::vector<std::string> ConfigMacros;
380 /// An unresolved conflict with another module.
381 struct UnresolvedConflict {
382 /// The (unresolved) module id.
385 /// The message provided to the user when there is a conflict.
389 /// The list of conflicts for which the module-id has not yet been
391 std::vector<UnresolvedConflict> UnresolvedConflicts;
393 /// A conflict between two modules.
395 /// The module that this module conflicts with.
398 /// The message provided to the user when there is a conflict.
402 /// The list of conflicts.
403 std::vector<Conflict> Conflicts;
405 /// Construct a new module or submodule.
406 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
407 bool IsFramework, bool IsExplicit, unsigned VisibilityID);
411 /// Determine whether this module has been declared unimportable.
412 bool isUnimportable() const { return IsUnimportable; }
414 /// Determine whether this module has been declared unimportable.
416 /// \param LangOpts The language options used for the current
417 /// translation unit.
419 /// \param Target The target options used for the current translation unit.
421 /// \param Req If this module is unimportable because of a missing
422 /// requirement, this parameter will be set to one of the requirements that
423 /// is not met for use of this module.
425 /// \param ShadowingModule If this module is unimportable because it is
426 /// shadowed, this parameter will be set to the shadowing module.
427 bool isUnimportable(const LangOptions &LangOpts, const TargetInfo &Target,
428 Requirement &Req, Module *&ShadowingModule) const;
430 /// Determine whether this module is available for use within the
431 /// current translation unit.
432 bool isAvailable() const { return IsAvailable; }
434 /// Determine whether this module is available for use within the
435 /// current translation unit.
437 /// \param LangOpts The language options used for the current
438 /// translation unit.
440 /// \param Target The target options used for the current translation unit.
442 /// \param Req If this module is unavailable because of a missing requirement,
443 /// this parameter will be set to one of the requirements that is not met for
444 /// use of this module.
446 /// \param MissingHeader If this module is unavailable because of a missing
447 /// header, this parameter will be set to one of the missing headers.
449 /// \param ShadowingModule If this module is unavailable because it is
450 /// shadowed, this parameter will be set to the shadowing module.
451 bool isAvailable(const LangOptions &LangOpts,
452 const TargetInfo &Target,
454 UnresolvedHeaderDirective &MissingHeader,
455 Module *&ShadowingModule) const;
457 /// Determine whether this module is a submodule.
458 bool isSubModule() const { return Parent != nullptr; }
460 /// Determine whether this module is a submodule of the given other
462 bool isSubModuleOf(const Module *Other) const;
464 /// Determine whether this module is a part of a framework,
465 /// either because it is a framework module or because it is a submodule
466 /// of a framework module.
467 bool isPartOfFramework() const {
468 for (const Module *Mod = this; Mod; Mod = Mod->Parent)
469 if (Mod->IsFramework)
475 /// Determine whether this module is a subframework of another
477 bool isSubFramework() const {
478 return IsFramework && Parent && Parent->isPartOfFramework();
481 /// Set the parent of this module. This should only be used if the parent
482 /// could not be set during module creation.
483 void setParent(Module *M) {
486 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
487 Parent->SubModules.push_back(this);
490 /// Retrieve the full name of this module, including the path from
491 /// its top-level module.
492 /// \param AllowStringLiterals If \c true, components that might not be
493 /// lexically valid as identifiers will be emitted as string literals.
494 std::string getFullModuleName(bool AllowStringLiterals = false) const;
496 /// Whether the full name of this module is equal to joining
497 /// \p nameParts with "."s.
499 /// This is more efficient than getFullModuleName().
500 bool fullModuleNameIs(ArrayRef<StringRef> nameParts) const;
502 /// Retrieve the top-level module for this (sub)module, which may
504 Module *getTopLevelModule() {
505 return const_cast<Module *>(
506 const_cast<const Module *>(this)->getTopLevelModule());
509 /// Retrieve the top-level module for this (sub)module, which may
511 const Module *getTopLevelModule() const;
513 /// Retrieve the name of the top-level module.
514 StringRef getTopLevelModuleName() const {
515 return getTopLevelModule()->Name;
518 /// The serialized AST file for this module, if one was created.
519 const FileEntry *getASTFile() const {
520 return getTopLevelModule()->ASTFile;
523 /// Set the serialized AST file for the top-level module of this module.
524 void setASTFile(const FileEntry *File) {
525 assert((File == nullptr || getASTFile() == nullptr ||
526 getASTFile() == File) && "file path changed");
527 getTopLevelModule()->ASTFile = File;
530 /// Retrieve the directory for which this module serves as the
532 DirectoryName getUmbrellaDir() const;
534 /// Retrieve the header that serves as the umbrella header for this
536 Header getUmbrellaHeader() const {
538 return Header{UmbrellaAsWritten,
539 static_cast<const FileEntry *>(Umbrella)};
543 /// Determine whether this module has an umbrella directory that is
544 /// not based on an umbrella header.
545 bool hasUmbrellaDir() const { return Umbrella && HasUmbrellaDir; }
547 /// Add a top-level header associated with this module.
548 void addTopHeader(const FileEntry *File);
550 /// Add a top-level header filename associated with this module.
551 void addTopHeaderFilename(StringRef Filename) {
552 TopHeaderNames.push_back(std::string(Filename));
555 /// The top-level headers associated with this module.
556 ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
558 /// Determine whether this module has declared its intention to
559 /// directly use another module.
560 bool directlyUses(const Module *Requested) const;
562 /// Add the given feature requirement to the list of features
563 /// required by this module.
565 /// \param Feature The feature that is required by this module (and
568 /// \param RequiredState The required state of this feature: \c true
569 /// if it must be present, \c false if it must be absent.
571 /// \param LangOpts The set of language options that will be used to
572 /// evaluate the availability of this feature.
574 /// \param Target The target options that will be used to evaluate the
575 /// availability of this feature.
576 void addRequirement(StringRef Feature, bool RequiredState,
577 const LangOptions &LangOpts,
578 const TargetInfo &Target);
580 /// Mark this module and all of its submodules as unavailable.
581 void markUnavailable(bool Unimportable);
583 /// Find the submodule with the given name.
585 /// \returns The submodule if found, or NULL otherwise.
586 Module *findSubmodule(StringRef Name) const;
587 Module *findOrInferSubmodule(StringRef Name);
589 /// Determine whether the specified module would be visible to
590 /// a lookup at the end of this module.
592 /// FIXME: This may return incorrect results for (submodules of) the
593 /// module currently being built, if it's queried before we see all
595 bool isModuleVisible(const Module *M) const {
596 if (VisibleModulesCache.empty())
597 buildVisibleModulesCache();
598 return VisibleModulesCache.count(M);
601 unsigned getVisibilityID() const { return VisibilityID; }
603 using submodule_iterator = std::vector<Module *>::iterator;
604 using submodule_const_iterator = std::vector<Module *>::const_iterator;
606 submodule_iterator submodule_begin() { return SubModules.begin(); }
607 submodule_const_iterator submodule_begin() const {return SubModules.begin();}
608 submodule_iterator submodule_end() { return SubModules.end(); }
609 submodule_const_iterator submodule_end() const { return SubModules.end(); }
611 llvm::iterator_range<submodule_iterator> submodules() {
612 return llvm::make_range(submodule_begin(), submodule_end());
614 llvm::iterator_range<submodule_const_iterator> submodules() const {
615 return llvm::make_range(submodule_begin(), submodule_end());
618 /// Appends this module's list of exported modules to \p Exported.
620 /// This provides a subset of immediately imported modules (the ones that are
621 /// directly exported), not the complete set of exported modules.
622 void getExportedModules(SmallVectorImpl<Module *> &Exported) const;
624 static StringRef getModuleInputBufferName() {
625 return "<module-includes>";
628 /// Print the module map for this module to the given stream.
629 void print(raw_ostream &OS, unsigned Indent = 0) const;
631 /// Dump the contents of this module to the given output stream.
635 void buildVisibleModulesCache() const;
638 /// A set of visible modules.
639 class VisibleModuleSet {
641 VisibleModuleSet() = default;
642 VisibleModuleSet(VisibleModuleSet &&O)
643 : ImportLocs(std::move(O.ImportLocs)), Generation(O.Generation ? 1 : 0) {
644 O.ImportLocs.clear();
648 /// Move from another visible modules set. Guaranteed to leave the source
649 /// empty and bump the generation on both.
650 VisibleModuleSet &operator=(VisibleModuleSet &&O) {
651 ImportLocs = std::move(O.ImportLocs);
652 O.ImportLocs.clear();
658 /// Get the current visibility generation. Incremented each time the
659 /// set of visible modules changes in any way.
660 unsigned getGeneration() const { return Generation; }
662 /// Determine whether a module is visible.
663 bool isVisible(const Module *M) const {
664 return getImportLoc(M).isValid();
667 /// Get the location at which the import of a module was triggered.
668 SourceLocation getImportLoc(const Module *M) const {
669 return M->getVisibilityID() < ImportLocs.size()
670 ? ImportLocs[M->getVisibilityID()]
674 /// A callback to call when a module is made visible (directly or
675 /// indirectly) by a call to \ref setVisible.
676 using VisibleCallback = llvm::function_ref<void(Module *M)>;
678 /// A callback to call when a module conflict is found. \p Path
679 /// consists of a sequence of modules from the conflicting module to the one
680 /// made visible, where each was exported by the next.
681 using ConflictCallback =
682 llvm::function_ref<void(ArrayRef<Module *> Path, Module *Conflict,
685 /// Make a specific module visible.
686 void setVisible(Module *M, SourceLocation Loc,
687 VisibleCallback Vis = [](Module *) {},
688 ConflictCallback Cb = [](ArrayRef<Module *>, Module *,
692 /// Import locations for each visible module. Indexed by the module's
694 std::vector<SourceLocation> ImportLocs;
696 /// Visibility generation, bumped every time the visibility state changes.
697 unsigned Generation = 0;
700 /// Abstracts clang modules and precompiled header files and holds
701 /// everything needed to generate debug info for an imported module
703 class ASTSourceDescriptor {
704 StringRef PCHModuleName;
707 ASTFileSignature Signature;
708 Module *ClangModule = nullptr;
711 ASTSourceDescriptor() = default;
712 ASTSourceDescriptor(StringRef Name, StringRef Path, StringRef ASTFile,
713 ASTFileSignature Signature)
714 : PCHModuleName(std::move(Name)), Path(std::move(Path)),
715 ASTFile(std::move(ASTFile)), Signature(Signature) {}
716 ASTSourceDescriptor(Module &M);
718 std::string getModuleName() const;
719 StringRef getPath() const { return Path; }
720 StringRef getASTFile() const { return ASTFile; }
721 ASTFileSignature getSignature() const { return Signature; }
722 Module *getModuleOrNull() const { return ClangModule; }
728 #endif // LLVM_CLANG_BASIC_MODULE_H