1 //===- Module.h - Describe a module -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Defines the clang::Module class, which describes a module in the
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_BASIC_MODULE_H
17 #define LLVM_CLANG_BASIC_MODULE_H
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/DenseSet.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/ADT/PointerUnion.h"
26 #include "llvm/ADT/SetVector.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/ADT/iterator_range.h"
51 /// Describes the name of a module.
52 using ModuleId = SmallVector<std::pair<std::string, SourceLocation>, 2>;
54 /// The signature of a module, which is a hash of the AST content.
55 struct ASTFileSignature : std::array<uint32_t, 5> {
56 ASTFileSignature(std::array<uint32_t, 5> S = {{0}})
57 : std::array<uint32_t, 5>(std::move(S)) {}
59 explicit operator bool() const {
60 return *this != std::array<uint32_t, 5>({{0}});
64 /// Describes a module or submodule.
67 /// The name of this module.
70 /// The location of the module definition.
71 SourceLocation DefinitionLoc;
74 /// This is a module that was defined by a module map and built out
78 /// This is a C++ Modules TS module interface unit.
81 /// This is a fragment of the global module within some C++ Modules
86 /// The kind of this module.
87 ModuleKind Kind = ModuleMapModule;
89 /// The parent of this module. This will be NULL for the top-level
93 /// The build directory of this module. This is the directory in
94 /// which the module is notionally built, and relative to which its headers
96 const DirectoryEntry *Directory = nullptr;
98 /// The presumed file name for the module map defining this module.
99 /// Only non-empty when building from preprocessed source.
100 std::string PresumedModuleMapFile;
102 /// The umbrella header or directory.
103 llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
105 /// The module signature.
106 ASTFileSignature Signature;
108 /// The name of the umbrella entry, as written in the module map.
109 std::string UmbrellaAsWritten;
111 /// The module through which entities defined in this module will
112 /// eventually be exposed, for use in "private" modules.
113 std::string ExportAsModule;
116 /// The submodules of this module, indexed by name.
117 std::vector<Module *> SubModules;
119 /// A mapping from the submodule name to the index into the
120 /// \c SubModules vector at which that submodule resides.
121 llvm::StringMap<unsigned> SubModuleIndex;
123 /// The AST file if this is a top-level module which has a
124 /// corresponding serialized AST file, or null otherwise.
125 const FileEntry *ASTFile = nullptr;
127 /// The top-level headers associated with this module.
128 llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
130 /// top-level header filenames that aren't resolved to FileEntries yet.
131 std::vector<std::string> TopHeaderNames;
133 /// Cache of modules visible to lookup in this module.
134 mutable llvm::DenseSet<const Module*> VisibleModulesCache;
136 /// The ID used when referencing this module within a VisibleModuleSet.
137 unsigned VisibilityID;
147 static const int NumHeaderKinds = HK_Excluded + 1;
149 /// Information about a header directive as found in the module map
152 std::string NameAsWritten;
153 const FileEntry *Entry;
155 explicit operator bool() { return Entry; }
158 /// Information about a directory name as found in the module map
160 struct DirectoryName {
161 std::string NameAsWritten;
162 const DirectoryEntry *Entry;
164 explicit operator bool() { return Entry; }
167 /// The headers that are part of this module.
168 SmallVector<Header, 2> Headers[5];
170 /// Stored information about a header directive that was found in the
171 /// module map file but has not been resolved to a file.
172 struct UnresolvedHeaderDirective {
173 HeaderKind Kind = HK_Normal;
174 SourceLocation FileNameLoc;
175 std::string FileName;
176 bool IsUmbrella = false;
177 bool HasBuiltinHeader = false;
178 Optional<off_t> Size;
179 Optional<time_t> ModTime;
182 /// Headers that are mentioned in the module map file but that we have not
183 /// yet attempted to resolve to a file on the file system.
184 SmallVector<UnresolvedHeaderDirective, 1> UnresolvedHeaders;
186 /// Headers that are mentioned in the module map file but could not be
187 /// found on the file system.
188 SmallVector<UnresolvedHeaderDirective, 1> MissingHeaders;
190 /// An individual requirement: a feature name and a flag indicating
191 /// the required state of that feature.
192 using Requirement = std::pair<std::string, bool>;
194 /// The set of language features required to use this module.
196 /// If any of these requirements are not available, the \c IsAvailable bit
197 /// will be false to indicate that this (sub)module is not available.
198 SmallVector<Requirement, 2> Requirements;
200 /// A module with the same name that shadows this module.
201 Module *ShadowingModule = nullptr;
203 /// Whether this module is missing a feature from \c Requirements.
204 unsigned IsMissingRequirement : 1;
206 /// Whether we tried and failed to load a module file for this module.
207 unsigned HasIncompatibleModuleFile : 1;
209 /// Whether this module is available in the current translation unit.
211 /// If the module is missing headers or does not meet all requirements then
212 /// this bit will be 0.
213 unsigned IsAvailable : 1;
215 /// Whether this module was loaded from a module file.
216 unsigned IsFromModuleFile : 1;
218 /// Whether this is a framework module.
219 unsigned IsFramework : 1;
221 /// Whether this is an explicit submodule.
222 unsigned IsExplicit : 1;
224 /// Whether this is a "system" module (which assumes that all
225 /// headers in it are system headers).
226 unsigned IsSystem : 1;
228 /// Whether this is an 'extern "C"' module (which implicitly puts all
229 /// headers in it within an 'extern "C"' block, and allows the module to be
230 /// imported within such a block).
231 unsigned IsExternC : 1;
233 /// Whether this is an inferred submodule (module * { ... }).
234 unsigned IsInferred : 1;
236 /// Whether we should infer submodules for this module based on
239 /// Submodules can only be inferred for modules with an umbrella header.
240 unsigned InferSubmodules : 1;
242 /// Whether, when inferring submodules, the inferred submodules
243 /// should be explicit.
244 unsigned InferExplicitSubmodules : 1;
246 /// Whether, when inferring submodules, the inferr submodules should
247 /// export all modules they import (e.g., the equivalent of "export *").
248 unsigned InferExportWildcard : 1;
250 /// Whether the set of configuration macros is exhaustive.
252 /// When the set of configuration macros is exhaustive, meaning
253 /// that no identifier not in this list should affect how the module is
255 unsigned ConfigMacrosExhaustive : 1;
257 /// Whether files in this module can only include non-modular headers
258 /// and headers from used modules.
259 unsigned NoUndeclaredIncludes : 1;
261 /// Whether this module came from a "private" module map, found next
262 /// to a regular (public) module map.
263 unsigned ModuleMapIsPrivate : 1;
265 /// Describes the visibility of the various names within a
266 /// particular module.
267 enum NameVisibilityKind {
268 /// All of the names in this module are hidden.
270 /// All of the names in this module are visible.
274 /// The visibility of names within this particular module.
275 NameVisibilityKind NameVisibility;
277 /// The location of the inferred submodule.
278 SourceLocation InferredSubmoduleLoc;
280 /// The set of modules imported by this module, and on which this
282 llvm::SmallSetVector<Module *, 2> Imports;
284 /// Describes an exported module.
286 /// The pointer is the module being re-exported, while the bit will be true
287 /// to indicate that this is a wildcard export.
288 using ExportDecl = llvm::PointerIntPair<Module *, 1, bool>;
290 /// The set of export declarations.
291 SmallVector<ExportDecl, 2> Exports;
293 /// Describes an exported module that has not yet been resolved
294 /// (perhaps because the module it refers to has not yet been loaded).
295 struct UnresolvedExportDecl {
296 /// The location of the 'export' keyword in the module map file.
297 SourceLocation ExportLoc;
299 /// The name of the module.
302 /// Whether this export declaration ends in a wildcard, indicating
303 /// that all of its submodules should be exported (rather than the named
308 /// The set of export declarations that have yet to be resolved.
309 SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
311 /// The directly used modules.
312 SmallVector<Module *, 2> DirectUses;
314 /// The set of use declarations that have yet to be resolved.
315 SmallVector<ModuleId, 2> UnresolvedDirectUses;
317 /// A library or framework to link against when an entity from this
320 LinkLibrary() = default;
321 LinkLibrary(const std::string &Library, bool IsFramework)
322 : Library(Library), IsFramework(IsFramework) {}
324 /// The library to link against.
326 /// This will typically be a library or framework name, but can also
327 /// be an absolute path to the library or framework.
330 /// Whether this is a framework rather than a library.
331 bool IsFramework = false;
334 /// The set of libraries or frameworks to link against when
335 /// an entity from this module is used.
336 llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
338 /// Autolinking uses the framework name for linking purposes
339 /// when this is false and the export_as name otherwise.
340 bool UseExportAsModuleLinkName = false;
342 /// The set of "configuration macros", which are macros that
343 /// (intentionally) change how this module is built.
344 std::vector<std::string> ConfigMacros;
346 /// An unresolved conflict with another module.
347 struct UnresolvedConflict {
348 /// The (unresolved) module id.
351 /// The message provided to the user when there is a conflict.
355 /// The list of conflicts for which the module-id has not yet been
357 std::vector<UnresolvedConflict> UnresolvedConflicts;
359 /// A conflict between two modules.
361 /// The module that this module conflicts with.
364 /// The message provided to the user when there is a conflict.
368 /// The list of conflicts.
369 std::vector<Conflict> Conflicts;
371 /// Construct a new module or submodule.
372 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
373 bool IsFramework, bool IsExplicit, unsigned VisibilityID);
377 /// Determine whether this module is available for use within the
378 /// current translation unit.
379 bool isAvailable() const { return IsAvailable; }
381 /// Determine whether this module is available for use within the
382 /// current translation unit.
384 /// \param LangOpts The language options used for the current
385 /// translation unit.
387 /// \param Target The target options used for the current translation unit.
389 /// \param Req If this module is unavailable because of a missing requirement,
390 /// this parameter will be set to one of the requirements that is not met for
391 /// use of this module.
393 /// \param MissingHeader If this module is unavailable because of a missing
394 /// header, this parameter will be set to one of the missing headers.
396 /// \param ShadowingModule If this module is unavailable because it is
397 /// shadowed, this parameter will be set to the shadowing module.
398 bool isAvailable(const LangOptions &LangOpts,
399 const TargetInfo &Target,
401 UnresolvedHeaderDirective &MissingHeader,
402 Module *&ShadowingModule) const;
404 /// Determine whether this module is a submodule.
405 bool isSubModule() const { return Parent != nullptr; }
407 /// Determine whether this module is a submodule of the given other
409 bool isSubModuleOf(const Module *Other) const;
411 /// Determine whether this module is a part of a framework,
412 /// either because it is a framework module or because it is a submodule
413 /// of a framework module.
414 bool isPartOfFramework() const {
415 for (const Module *Mod = this; Mod; Mod = Mod->Parent)
416 if (Mod->IsFramework)
422 /// Determine whether this module is a subframework of another
424 bool isSubFramework() const {
425 return IsFramework && Parent && Parent->isPartOfFramework();
428 /// Set the parent of this module. This should only be used if the parent
429 /// could not be set during module creation.
430 void setParent(Module *M) {
433 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
434 Parent->SubModules.push_back(this);
437 /// Retrieve the full name of this module, including the path from
438 /// its top-level module.
439 /// \param AllowStringLiterals If \c true, components that might not be
440 /// lexically valid as identifiers will be emitted as string literals.
441 std::string getFullModuleName(bool AllowStringLiterals = false) const;
443 /// Whether the full name of this module is equal to joining
444 /// \p nameParts with "."s.
446 /// This is more efficient than getFullModuleName().
447 bool fullModuleNameIs(ArrayRef<StringRef> nameParts) const;
449 /// Retrieve the top-level module for this (sub)module, which may
451 Module *getTopLevelModule() {
452 return const_cast<Module *>(
453 const_cast<const Module *>(this)->getTopLevelModule());
456 /// Retrieve the top-level module for this (sub)module, which may
458 const Module *getTopLevelModule() const;
460 /// Retrieve the name of the top-level module.
461 StringRef getTopLevelModuleName() const {
462 return getTopLevelModule()->Name;
465 /// The serialized AST file for this module, if one was created.
466 const FileEntry *getASTFile() const {
467 return getTopLevelModule()->ASTFile;
470 /// Set the serialized AST file for the top-level module of this module.
471 void setASTFile(const FileEntry *File) {
472 assert((File == nullptr || getASTFile() == nullptr ||
473 getASTFile() == File) && "file path changed");
474 getTopLevelModule()->ASTFile = File;
477 /// Retrieve the directory for which this module serves as the
479 DirectoryName getUmbrellaDir() const;
481 /// Retrieve the header that serves as the umbrella header for this
483 Header getUmbrellaHeader() const {
484 if (auto *E = Umbrella.dyn_cast<const FileEntry *>())
485 return Header{UmbrellaAsWritten, E};
489 /// Determine whether this module has an umbrella directory that is
490 /// not based on an umbrella header.
491 bool hasUmbrellaDir() const {
492 return Umbrella && Umbrella.is<const DirectoryEntry *>();
495 /// Add a top-level header associated with this module.
496 void addTopHeader(const FileEntry *File) {
498 TopHeaders.insert(File);
501 /// Add a top-level header filename associated with this module.
502 void addTopHeaderFilename(StringRef Filename) {
503 TopHeaderNames.push_back(Filename);
506 /// The top-level headers associated with this module.
507 ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
509 /// Determine whether this module has declared its intention to
510 /// directly use another module.
511 bool directlyUses(const Module *Requested) const;
513 /// Add the given feature requirement to the list of features
514 /// required by this module.
516 /// \param Feature The feature that is required by this module (and
519 /// \param RequiredState The required state of this feature: \c true
520 /// if it must be present, \c false if it must be absent.
522 /// \param LangOpts The set of language options that will be used to
523 /// evaluate the availability of this feature.
525 /// \param Target The target options that will be used to evaluate the
526 /// availability of this feature.
527 void addRequirement(StringRef Feature, bool RequiredState,
528 const LangOptions &LangOpts,
529 const TargetInfo &Target);
531 /// Mark this module and all of its submodules as unavailable.
532 void markUnavailable(bool MissingRequirement = false);
534 /// Find the submodule with the given name.
536 /// \returns The submodule if found, or NULL otherwise.
537 Module *findSubmodule(StringRef Name) const;
539 /// Determine whether the specified module would be visible to
540 /// a lookup at the end of this module.
542 /// FIXME: This may return incorrect results for (submodules of) the
543 /// module currently being built, if it's queried before we see all
545 bool isModuleVisible(const Module *M) const {
546 if (VisibleModulesCache.empty())
547 buildVisibleModulesCache();
548 return VisibleModulesCache.count(M);
551 unsigned getVisibilityID() const { return VisibilityID; }
553 using submodule_iterator = std::vector<Module *>::iterator;
554 using submodule_const_iterator = std::vector<Module *>::const_iterator;
556 submodule_iterator submodule_begin() { return SubModules.begin(); }
557 submodule_const_iterator submodule_begin() const {return SubModules.begin();}
558 submodule_iterator submodule_end() { return SubModules.end(); }
559 submodule_const_iterator submodule_end() const { return SubModules.end(); }
561 llvm::iterator_range<submodule_iterator> submodules() {
562 return llvm::make_range(submodule_begin(), submodule_end());
564 llvm::iterator_range<submodule_const_iterator> submodules() const {
565 return llvm::make_range(submodule_begin(), submodule_end());
568 /// Appends this module's list of exported modules to \p Exported.
570 /// This provides a subset of immediately imported modules (the ones that are
571 /// directly exported), not the complete set of exported modules.
572 void getExportedModules(SmallVectorImpl<Module *> &Exported) const;
574 static StringRef getModuleInputBufferName() {
575 return "<module-includes>";
578 /// Print the module map for this module to the given stream.
579 void print(raw_ostream &OS, unsigned Indent = 0) const;
581 /// Dump the contents of this module to the given output stream.
585 void buildVisibleModulesCache() const;
588 /// A set of visible modules.
589 class VisibleModuleSet {
591 VisibleModuleSet() = default;
592 VisibleModuleSet(VisibleModuleSet &&O)
593 : ImportLocs(std::move(O.ImportLocs)), Generation(O.Generation ? 1 : 0) {
594 O.ImportLocs.clear();
598 /// Move from another visible modules set. Guaranteed to leave the source
599 /// empty and bump the generation on both.
600 VisibleModuleSet &operator=(VisibleModuleSet &&O) {
601 ImportLocs = std::move(O.ImportLocs);
602 O.ImportLocs.clear();
608 /// Get the current visibility generation. Incremented each time the
609 /// set of visible modules changes in any way.
610 unsigned getGeneration() const { return Generation; }
612 /// Determine whether a module is visible.
613 bool isVisible(const Module *M) const {
614 return getImportLoc(M).isValid();
617 /// Get the location at which the import of a module was triggered.
618 SourceLocation getImportLoc(const Module *M) const {
619 return M->getVisibilityID() < ImportLocs.size()
620 ? ImportLocs[M->getVisibilityID()]
624 /// A callback to call when a module is made visible (directly or
625 /// indirectly) by a call to \ref setVisible.
626 using VisibleCallback = llvm::function_ref<void(Module *M)>;
628 /// A callback to call when a module conflict is found. \p Path
629 /// consists of a sequence of modules from the conflicting module to the one
630 /// made visible, where each was exported by the next.
631 using ConflictCallback =
632 llvm::function_ref<void(ArrayRef<Module *> Path, Module *Conflict,
635 /// Make a specific module visible.
636 void setVisible(Module *M, SourceLocation Loc,
637 VisibleCallback Vis = [](Module *) {},
638 ConflictCallback Cb = [](ArrayRef<Module *>, Module *,
642 /// Import locations for each visible module. Indexed by the module's
644 std::vector<SourceLocation> ImportLocs;
646 /// Visibility generation, bumped every time the visibility state changes.
647 unsigned Generation = 0;
652 #endif // LLVM_CLANG_BASIC_MODULE_H