clang 22.0.0git
Module.h
Go to the documentation of this file.
1//===- Module.h - Describe a module -----------------------------*- C++ -*-===//
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/// \file
10/// Defines the clang::Module class, which describes a module in the
11/// source code.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_MODULE_H
16#define LLVM_CLANG_BASIC_MODULE_H
17
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/DenseSet.h"
23#include "llvm/ADT/PointerIntPair.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SetVector.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/StringMap.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/iterator_range.h"
30#include <array>
31#include <cassert>
32#include <cstdint>
33#include <ctime>
34#include <iterator>
35#include <optional>
36#include <string>
37#include <utility>
38#include <variant>
39#include <vector>
40
41namespace llvm {
42
43class raw_ostream;
44
45} // namespace llvm
46
47namespace clang {
48
49class FileManager;
50class LangOptions;
51class ModuleMap;
52class TargetInfo;
53
54/// Describes the name of a module.
56
57/// The signature of a module, which is a hash of the AST content.
58struct ASTFileSignature : std::array<uint8_t, 20> {
59 using BaseT = std::array<uint8_t, 20>;
60
61 static constexpr size_t size = std::tuple_size<BaseT>::value;
62
63 ASTFileSignature(BaseT S = {{0}}) : BaseT(std::move(S)) {}
64
65 explicit operator bool() const { return *this != BaseT({{0}}); }
66
67 // Support implicit cast to ArrayRef. Note that ASTFileSignature::size
68 // prevents implicit cast to ArrayRef because one of the implicit constructors
69 // of ArrayRef requires access to BaseT::size.
70 operator ArrayRef<uint8_t>() const { return ArrayRef<uint8_t>(data(), size); }
71
72 /// Returns the value truncated to the size of an uint64_t.
73 uint64_t truncatedValue() const {
74 uint64_t Value = 0;
75 static_assert(sizeof(*this) >= sizeof(uint64_t), "No need to truncate.");
76 for (unsigned I = 0; I < sizeof(uint64_t); ++I)
77 Value |= static_cast<uint64_t>((*this)[I]) << (I * 8);
78 return Value;
79 }
80
81 static ASTFileSignature create(std::array<uint8_t, 20> Bytes) {
82 return ASTFileSignature(std::move(Bytes));
83 }
84
86 ASTFileSignature Sentinel;
87 Sentinel.fill(0xFF);
88 return Sentinel;
89 }
90
92 ASTFileSignature Dummy;
93 Dummy.fill(0x00);
94 return Dummy;
95 }
96
97 template <typename InputIt>
98 static ASTFileSignature create(InputIt First, InputIt Last) {
99 assert(std::distance(First, Last) == size &&
100 "Wrong amount of bytes to create an ASTFileSignature");
101
102 ASTFileSignature Signature;
103 std::copy(First, Last, Signature.begin());
104 return Signature;
105 }
106};
107
108/// The set of attributes that can be attached to a module.
110 /// Whether this is a system module.
111 LLVM_PREFERRED_TYPE(bool)
113
114 /// Whether this is an extern "C" module.
115 LLVM_PREFERRED_TYPE(bool)
116 unsigned IsExternC : 1;
117
118 /// Whether this is an exhaustive set of configuration macros.
119 LLVM_PREFERRED_TYPE(bool)
120 unsigned IsExhaustive : 1;
121
122 /// Whether files in this module can only include non-modular headers
123 /// and headers from used modules.
124 LLVM_PREFERRED_TYPE(bool)
126
130};
131
132/// Required to construct a Module.
133///
134/// This tag type is only constructible by ModuleMap, guaranteeing it ownership
135/// of all Module instances.
137 explicit ModuleConstructorTag() = default;
138 friend ModuleMap;
139};
140
141/// Describes a module or submodule.
142///
143/// Aligned to 8 bytes to allow for llvm::PointerIntPair<Module *, 3>.
144class alignas(8) Module {
145public:
146 /// The name of this module.
147 std::string Name;
148
149 /// The location of the module definition.
151
152 // FIXME: Consider if reducing the size of this enum (having Partition and
153 // Named modules only) then representing interface/implementation separately
154 // is more efficient.
156 /// This is a module that was defined by a module map and built out
157 /// of header files.
159
160 /// This is a C++20 header unit.
162
163 /// This is a C++20 module interface unit.
165
166 /// This is a C++20 module implementation unit.
168
169 /// This is a C++20 module partition interface.
171
172 /// This is a C++20 module partition implementation.
174
175 /// This is the explicit Global Module Fragment of a modular TU.
176 /// As per C++ [module.global.frag].
178
179 /// This is the private module fragment within some C++ module.
181
182 /// This is an implicit fragment of the global module which contains
183 /// only language linkage declarations (made in the purview of the
184 /// named module).
186 };
187
188 /// The kind of this module.
190
191 /// The parent of this module. This will be NULL for the top-level
192 /// module.
194
195 /// The build directory of this module. This is the directory in
196 /// which the module is notionally built, and relative to which its headers
197 /// are found.
199
200 /// The presumed file name for the module map defining this module.
201 /// Only non-empty when building from preprocessed source.
203
204 /// The umbrella header or directory.
205 std::variant<std::monostate, FileEntryRef, DirectoryEntryRef> Umbrella;
206
207 /// The module signature.
209
210 /// The name of the umbrella entry, as written in the module map.
211 std::string UmbrellaAsWritten;
212
213 // The path to the umbrella entry relative to the root module's \c Directory.
215
216 /// The module through which entities defined in this module will
217 /// eventually be exposed, for use in "private" modules.
218 std::string ExportAsModule;
219
220 /// For the debug info, the path to this module's .apinotes file, if any.
221 std::string APINotesFile;
222
223 /// Does this Module is a named module of a standard named module?
224 bool isNamedModule() const {
225 switch (Kind) {
231 return true;
232 default:
233 return false;
234 }
235 }
236
237 /// Does this Module scope describe a fragment of the global module within
238 /// some C++ module.
239 bool isGlobalModule() const {
241 }
244 }
247 }
248
249 bool isPrivateModule() const { return Kind == PrivateModuleFragment; }
250
251 bool isModuleMapModule() const { return Kind == ModuleMapModule; }
252
253private:
254 /// The submodules of this module, indexed by name.
255 std::vector<Module *> SubModules;
256
257 /// A mapping from the submodule name to the index into the
258 /// \c SubModules vector at which that submodule resides.
259 mutable llvm::StringMap<unsigned> SubModuleIndex;
260
261 /// The AST file if this is a top-level module which has a
262 /// corresponding serialized AST file, or null otherwise.
263 OptionalFileEntryRef ASTFile;
264
265 /// The top-level headers associated with this module.
267
268 /// top-level header filenames that aren't resolved to FileEntries yet.
269 std::vector<std::string> TopHeaderNames;
270
271 /// Cache of modules visible to lookup in this module.
272 mutable llvm::DenseSet<const Module*> VisibleModulesCache;
273
274 /// The ID used when referencing this module within a VisibleModuleSet.
275 unsigned VisibilityID;
276
277public:
284 };
285 /// Information about a header directive as found in the module map
286 /// file.
287 struct Header {
288 std::string NameAsWritten;
291 };
292
293private:
294 static const int NumHeaderKinds = HK_Excluded + 1;
295 // The begin index for a HeaderKind also acts the end index of HeaderKind - 1.
296 // The extra element at the end acts as the end index of the last HeaderKind.
297 unsigned HeaderKindBeginIndex[NumHeaderKinds + 1] = {};
298 SmallVector<Header, 2> HeadersStorage;
299
300public:
301 ArrayRef<Header> getAllHeaders() const { return HeadersStorage; }
303 assert(HK < NumHeaderKinds && "Invalid Module::HeaderKind");
304 auto BeginIt = HeadersStorage.begin() + HeaderKindBeginIndex[HK];
305 auto EndIt = HeadersStorage.begin() + HeaderKindBeginIndex[HK + 1];
306 return {BeginIt, EndIt};
307 }
309 assert(HK < NumHeaderKinds && "Invalid Module::HeaderKind");
310 auto EndIt = HeadersStorage.begin() + HeaderKindBeginIndex[HK + 1];
311 HeadersStorage.insert(EndIt, std::move(H));
312 for (unsigned HKI = HK + 1; HKI != NumHeaderKinds + 1; ++HKI)
313 ++HeaderKindBeginIndex[HKI];
314 }
315
316 /// Information about a directory name as found in the module map file.
318 std::string NameAsWritten;
321 };
322
323 /// Stored information about a header directive that was found in the
324 /// module map file but has not been resolved to a file.
328 std::string FileName;
329 bool IsUmbrella = false;
330 bool HasBuiltinHeader = false;
331 std::optional<off_t> Size;
332 std::optional<time_t> ModTime;
333 };
334
335 /// Headers that are mentioned in the module map file but that we have not
336 /// yet attempted to resolve to a file on the file system.
338
339 /// Headers that are mentioned in the module map file but could not be
340 /// found on the file system.
342
343 struct Requirement {
344 std::string FeatureName;
346 };
347
348 /// The set of language features required to use this module.
349 ///
350 /// If any of these requirements are not available, the \c IsAvailable bit
351 /// will be false to indicate that this (sub)module is not available.
353
354 /// A module with the same name that shadows this module.
356
357 /// Whether this module has declared itself unimportable, either because
358 /// it's missing a requirement from \p Requirements or because it's been
359 /// shadowed by another module.
360 LLVM_PREFERRED_TYPE(bool)
362
363 /// Whether we tried and failed to load a module file for this module.
364 LLVM_PREFERRED_TYPE(bool)
366
367 /// Whether this module is available in the current translation unit.
368 ///
369 /// If the module is missing headers or does not meet all requirements then
370 /// this bit will be 0.
371 LLVM_PREFERRED_TYPE(bool)
372 unsigned IsAvailable : 1;
373
374 /// Whether this module was loaded from a module file.
375 LLVM_PREFERRED_TYPE(bool)
376 unsigned IsFromModuleFile : 1;
377
378 /// Whether this is a framework module.
379 LLVM_PREFERRED_TYPE(bool)
380 unsigned IsFramework : 1;
381
382 /// Whether this is an explicit submodule.
383 LLVM_PREFERRED_TYPE(bool)
384 unsigned IsExplicit : 1;
385
386 /// Whether this is a "system" module (which assumes that all
387 /// headers in it are system headers).
388 LLVM_PREFERRED_TYPE(bool)
389 unsigned IsSystem : 1;
390
391 /// Whether this is an 'extern "C"' module (which implicitly puts all
392 /// headers in it within an 'extern "C"' block, and allows the module to be
393 /// imported within such a block).
394 LLVM_PREFERRED_TYPE(bool)
395 unsigned IsExternC : 1;
396
397 /// Whether this is an inferred submodule (module * { ... }).
398 LLVM_PREFERRED_TYPE(bool)
399 unsigned IsInferred : 1;
400
401 /// Whether we should infer submodules for this module based on
402 /// the headers.
403 ///
404 /// Submodules can only be inferred for modules with an umbrella header.
405 LLVM_PREFERRED_TYPE(bool)
406 unsigned InferSubmodules : 1;
407
408 /// Whether, when inferring submodules, the inferred submodules
409 /// should be explicit.
410 LLVM_PREFERRED_TYPE(bool)
412
413 /// Whether, when inferring submodules, the inferr submodules should
414 /// export all modules they import (e.g., the equivalent of "export *").
415 LLVM_PREFERRED_TYPE(bool)
417
418 /// Whether the set of configuration macros is exhaustive.
419 ///
420 /// When the set of configuration macros is exhaustive, meaning
421 /// that no identifier not in this list should affect how the module is
422 /// built.
423 LLVM_PREFERRED_TYPE(bool)
425
426 /// Whether files in this module can only include non-modular headers
427 /// and headers from used modules.
428 LLVM_PREFERRED_TYPE(bool)
430
431 /// Whether this module came from a "private" module map, found next
432 /// to a regular (public) module map.
433 LLVM_PREFERRED_TYPE(bool)
434 unsigned ModuleMapIsPrivate : 1;
435
436 /// Whether this C++20 named modules doesn't need an initializer.
437 /// This is only meaningful for C++20 modules.
438 LLVM_PREFERRED_TYPE(bool)
439 unsigned NamedModuleHasInit : 1;
440
441 /// Describes the visibility of the various names within a
442 /// particular module.
444 /// All of the names in this module are hidden.
446 /// All of the names in this module are visible.
448 };
449
450 /// The visibility of names within this particular module.
452
453 /// The location of the inferred submodule.
455
456 /// The set of modules imported by this module, and on which this
457 /// module depends.
459
460 /// The set of top-level modules that affected the compilation of this module,
461 /// but were not imported.
463
464 /// Describes an exported module.
465 ///
466 /// The pointer is the module being re-exported, while the bit will be true
467 /// to indicate that this is a wildcard export.
468 using ExportDecl = llvm::PointerIntPair<Module *, 1, bool>;
469
470 /// The set of export declarations.
472
473 /// Describes an exported module that has not yet been resolved
474 /// (perhaps because the module it refers to has not yet been loaded).
476 /// The location of the 'export' keyword in the module map file.
478
479 /// The name of the module.
481
482 /// Whether this export declaration ends in a wildcard, indicating
483 /// that all of its submodules should be exported (rather than the named
484 /// module itself).
486 };
487
488 /// The set of export declarations that have yet to be resolved.
490
491 /// The directly used modules.
493
494 /// The set of use declarations that have yet to be resolved.
496
497 /// When \c NoUndeclaredIncludes is true, the set of modules this module tried
498 /// to import but didn't because they are not direct uses.
500
501 /// A library or framework to link against when an entity from this
502 /// module is used.
503 struct LinkLibrary {
504 LinkLibrary() = default;
505 LinkLibrary(const std::string &Library, bool IsFramework)
507
508 /// The library to link against.
509 ///
510 /// This will typically be a library or framework name, but can also
511 /// be an absolute path to the library or framework.
512 std::string Library;
513
514 /// Whether this is a framework rather than a library.
515 bool IsFramework = false;
516 };
517
518 /// The set of libraries or frameworks to link against when
519 /// an entity from this module is used.
521
522 /// Autolinking uses the framework name for linking purposes
523 /// when this is false and the export_as name otherwise.
525
526 /// The set of "configuration macros", which are macros that
527 /// (intentionally) change how this module is built.
528 std::vector<std::string> ConfigMacros;
529
530 /// An unresolved conflict with another module.
532 /// The (unresolved) module id.
534
535 /// The message provided to the user when there is a conflict.
536 std::string Message;
537 };
538
539 /// The list of conflicts for which the module-id has not yet been
540 /// resolved.
541 std::vector<UnresolvedConflict> UnresolvedConflicts;
542
543 /// A conflict between two modules.
544 struct Conflict {
545 /// The module that this module conflicts with.
547
548 /// The message provided to the user when there is a conflict.
549 std::string Message;
550 };
551
552 /// The list of conflicts.
553 std::vector<Conflict> Conflicts;
554
555 /// Construct a new module or submodule.
557 Module *Parent, bool IsFramework, bool IsExplicit,
558 unsigned VisibilityID);
559
561
562 /// Determine whether this module has been declared unimportable.
563 bool isUnimportable() const { return IsUnimportable; }
564
565 /// Determine whether this module has been declared unimportable.
566 ///
567 /// \param LangOpts The language options used for the current
568 /// translation unit.
569 ///
570 /// \param Target The target options used for the current translation unit.
571 ///
572 /// \param Req If this module is unimportable because of a missing
573 /// requirement, this parameter will be set to one of the requirements that
574 /// is not met for use of this module.
575 ///
576 /// \param ShadowingModule If this module is unimportable because it is
577 /// shadowed, this parameter will be set to the shadowing module.
578 bool isUnimportable(const LangOptions &LangOpts, const TargetInfo &Target,
579 Requirement &Req, Module *&ShadowingModule) const;
580
581 /// Determine whether this module can be built in this compilation.
582 bool isForBuilding(const LangOptions &LangOpts) const;
583
584 /// Determine whether this module is available for use within the
585 /// current translation unit.
586 bool isAvailable() const { return IsAvailable; }
587
588 /// Determine whether this module is available for use within the
589 /// current translation unit.
590 ///
591 /// \param LangOpts The language options used for the current
592 /// translation unit.
593 ///
594 /// \param Target The target options used for the current translation unit.
595 ///
596 /// \param Req If this module is unavailable because of a missing requirement,
597 /// this parameter will be set to one of the requirements that is not met for
598 /// use of this module.
599 ///
600 /// \param MissingHeader If this module is unavailable because of a missing
601 /// header, this parameter will be set to one of the missing headers.
602 ///
603 /// \param ShadowingModule If this module is unavailable because it is
604 /// shadowed, this parameter will be set to the shadowing module.
605 bool isAvailable(const LangOptions &LangOpts,
606 const TargetInfo &Target,
607 Requirement &Req,
608 UnresolvedHeaderDirective &MissingHeader,
609 Module *&ShadowingModule) const;
610
611 /// Determine whether this module is a submodule.
612 bool isSubModule() const { return Parent != nullptr; }
613
614 /// Check if this module is a (possibly transitive) submodule of \p Other.
615 ///
616 /// The 'A is a submodule of B' relation is a partial order based on the
617 /// the parent-child relationship between individual modules.
618 ///
619 /// Returns \c false if \p Other is \c nullptr.
620 bool isSubModuleOf(const Module *Other) const;
621
622 /// Determine whether this module is a part of a framework,
623 /// either because it is a framework module or because it is a submodule
624 /// of a framework module.
625 bool isPartOfFramework() const {
626 for (const Module *Mod = this; Mod; Mod = Mod->Parent)
627 if (Mod->IsFramework)
628 return true;
629
630 return false;
631 }
632
633 /// Determine whether this module is a subframework of another
634 /// framework.
635 bool isSubFramework() const {
637 }
638
639 /// Set the parent of this module. This should only be used if the parent
640 /// could not be set during module creation.
641 void setParent(Module *M) {
642 assert(!Parent);
643 Parent = M;
644 Parent->SubModules.push_back(this);
645 }
646
647 /// Is this module have similar semantics as headers.
648 bool isHeaderLikeModule() const {
649 return isModuleMapModule() || isHeaderUnit();
650 }
651
652 /// Is this a module partition.
653 bool isModulePartition() const {
654 return Kind == ModulePartitionInterface ||
656 }
657
658 /// Is this a module partition implementation unit.
661 }
662
663 /// Is this a module implementation.
666 }
667
668 /// Is this module a header unit.
669 bool isHeaderUnit() const { return Kind == ModuleHeaderUnit; }
670 // Is this a C++20 module interface or a partition.
673 }
674
675 /// Is this a C++20 named module unit.
676 bool isNamedModuleUnit() const {
678 }
679
682 }
683
685
686 /// Get the primary module interface name from a partition.
688 // Technically, global module fragment belongs to global module. And global
689 // module has no name: [module.unit]p6:
690 // The global module has no name, no module interface unit, and is not
691 // introduced by any module-declaration.
692 //
693 // <global> is the default name showed in module map.
694 if (isGlobalModule())
695 return "<global>";
696
697 if (isModulePartition()) {
698 auto pos = Name.find(':');
699 return StringRef(Name.data(), pos);
700 }
701
702 if (isPrivateModule())
703 return getTopLevelModuleName();
704
705 return Name;
706 }
707
708 /// Retrieve the full name of this module, including the path from
709 /// its top-level module.
710 /// \param AllowStringLiterals If \c true, components that might not be
711 /// lexically valid as identifiers will be emitted as string literals.
712 std::string getFullModuleName(bool AllowStringLiterals = false) const;
713
714 /// Whether the full name of this module is equal to joining
715 /// \p nameParts with "."s.
716 ///
717 /// This is more efficient than getFullModuleName().
718 bool fullModuleNameIs(ArrayRef<StringRef> nameParts) const;
719
720 /// Retrieve the top-level module for this (sub)module, which may
721 /// be this module.
723 return const_cast<Module *>(
724 const_cast<const Module *>(this)->getTopLevelModule());
725 }
726
727 /// Retrieve the top-level module for this (sub)module, which may
728 /// be this module.
729 const Module *getTopLevelModule() const;
730
731 /// Retrieve the name of the top-level module.
732 StringRef getTopLevelModuleName() const {
733 return getTopLevelModule()->Name;
734 }
735
736 /// The serialized AST file for this module, if one was created.
738 return getTopLevelModule()->ASTFile;
739 }
740
741 /// Set the serialized AST file for the top-level module of this module.
743 assert((!getASTFile() || getASTFile() == File) && "file path changed");
744 getTopLevelModule()->ASTFile = File;
745 }
746
747 /// Retrieve the umbrella directory as written.
748 std::optional<DirectoryName> getUmbrellaDirAsWritten() const {
749 if (const auto *Dir = std::get_if<DirectoryEntryRef>(&Umbrella))
752 return std::nullopt;
753 }
754
755 /// Retrieve the umbrella header as written.
756 std::optional<Header> getUmbrellaHeaderAsWritten() const {
757 if (const auto *Hdr = std::get_if<FileEntryRef>(&Umbrella))
759 *Hdr};
760 return std::nullopt;
761 }
762
763 /// Get the effective umbrella directory for this module: either the one
764 /// explicitly written in the module map file, or the parent of the umbrella
765 /// header.
767
768 /// Add a top-level header associated with this module.
770
771 /// Add a top-level header filename associated with this module.
773 TopHeaderNames.push_back(std::string(Filename));
774 }
775
776 /// The top-level headers associated with this module.
778
779 /// Determine whether this module has declared its intention to
780 /// directly use another module.
781 bool directlyUses(const Module *Requested);
782
783 /// Add the given feature requirement to the list of features
784 /// required by this module.
785 ///
786 /// \param Feature The feature that is required by this module (and
787 /// its submodules).
788 ///
789 /// \param RequiredState The required state of this feature: \c true
790 /// if it must be present, \c false if it must be absent.
791 ///
792 /// \param LangOpts The set of language options that will be used to
793 /// evaluate the availability of this feature.
794 ///
795 /// \param Target The target options that will be used to evaluate the
796 /// availability of this feature.
797 void addRequirement(StringRef Feature, bool RequiredState,
798 const LangOptions &LangOpts,
799 const TargetInfo &Target);
800
801 /// Mark this module and all of its submodules as unavailable.
802 void markUnavailable(bool Unimportable);
803
804 /// Find the submodule with the given name.
805 ///
806 /// \returns The submodule if found, or NULL otherwise.
807 Module *findSubmodule(StringRef Name) const;
808
809 /// Get the Global Module Fragment (sub-module) for this module, it there is
810 /// one.
811 ///
812 /// \returns The GMF sub-module if found, or NULL otherwise.
814
815 /// Get the Private Module Fragment (sub-module) for this module, it there is
816 /// one.
817 ///
818 /// \returns The PMF sub-module if found, or NULL otherwise.
820
821 /// Determine whether the specified module would be visible to
822 /// a lookup at the end of this module.
823 ///
824 /// FIXME: This may return incorrect results for (submodules of) the
825 /// module currently being built, if it's queried before we see all
826 /// of its imports.
827 bool isModuleVisible(const Module *M) const {
828 if (VisibleModulesCache.empty())
829 buildVisibleModulesCache();
830 return VisibleModulesCache.count(M);
831 }
832
833 unsigned getVisibilityID() const { return VisibilityID; }
834
835 using submodule_iterator = std::vector<Module *>::iterator;
836 using submodule_const_iterator = std::vector<Module *>::const_iterator;
837
838 llvm::iterator_range<submodule_iterator> submodules() {
839 return llvm::make_range(SubModules.begin(), SubModules.end());
840 }
841 llvm::iterator_range<submodule_const_iterator> submodules() const {
842 return llvm::make_range(SubModules.begin(), SubModules.end());
843 }
844
845 /// Appends this module's list of exported modules to \p Exported.
846 ///
847 /// This provides a subset of immediately imported modules (the ones that are
848 /// directly exported), not the complete set of exported modules.
849 void getExportedModules(SmallVectorImpl<Module *> &Exported) const;
850
851 static StringRef getModuleInputBufferName() {
852 return "<module-includes>";
853 }
854
855 /// Print the module map for this module to the given stream.
856 void print(raw_ostream &OS, unsigned Indent = 0, bool Dump = false) const;
857
858 /// Dump the contents of this module to the given output stream.
859 void dump() const;
860
861private:
862 void buildVisibleModulesCache() const;
863};
864
865/// A set of visible modules.
867public:
868 VisibleModuleSet() = default;
870 : ImportLocs(std::move(O.ImportLocs)), Generation(O.Generation ? 1 : 0) {
871 O.ImportLocs.clear();
872 ++O.Generation;
873 }
874
875 /// Move from another visible modules set. Guaranteed to leave the source
876 /// empty and bump the generation on both.
878 ImportLocs = std::move(O.ImportLocs);
879 O.ImportLocs.clear();
880 ++O.Generation;
881 ++Generation;
882 return *this;
883 }
884
885 /// Get the current visibility generation. Incremented each time the
886 /// set of visible modules changes in any way.
887 unsigned getGeneration() const { return Generation; }
888
889 /// Determine whether a module is visible.
890 bool isVisible(const Module *M) const {
891 return getImportLoc(M).isValid();
892 }
893
894 /// Get the location at which the import of a module was triggered.
896 return M && M->getVisibilityID() < ImportLocs.size()
897 ? ImportLocs[M->getVisibilityID()]
898 : SourceLocation();
899 }
900
901 /// A callback to call when a module is made visible (directly or
902 /// indirectly) by a call to \ref setVisible.
903 using VisibleCallback = llvm::function_ref<void(Module *M)>;
904
905 /// A callback to call when a module conflict is found. \p Path
906 /// consists of a sequence of modules from the conflicting module to the one
907 /// made visible, where each was exported by the next.
909 llvm::function_ref<void(ArrayRef<Module *> Path, Module *Conflict,
910 StringRef Message)>;
911
912 /// Make a specific module visible.
913 void setVisible(
914 Module *M, SourceLocation Loc, bool IncludeExports = true,
915 VisibleCallback Vis = [](Module *) {},
916 ConflictCallback Cb = [](ArrayRef<Module *>, Module *, StringRef) {});
917
918private:
919 /// Import locations for each visible module. Indexed by the module's
920 /// VisibilityID.
921 std::vector<SourceLocation> ImportLocs;
922
923 /// Visibility generation, bumped every time the visibility state changes.
924 unsigned Generation = 0;
925};
926
927} // namespace clang
928
929#endif // LLVM_CLANG_BASIC_MODULE_H
IndirectLocalPath & Path
Defines interfaces for clang::DirectoryEntry and clang::DirectoryEntryRef.
Defines interfaces for clang::FileEntry and clang::FileEntryRef.
StringRef Filename
Definition: Format.cpp:3177
llvm::MachO::Target Target
Definition: MachO.h:51
SourceLocation Loc
Definition: SemaObjC.cpp:754
Defines the clang::SourceLocation class and associated facilities.
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:434
Required to construct a Module.
Definition: Module.h:136
Describes a module or submodule.
Definition: Module.h:144
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition: Module.h:732
void addRequirement(StringRef Feature, bool RequiredState, const LangOptions &LangOpts, const TargetInfo &Target)
Add the given feature requirement to the list of features required by this module.
Definition: Module.cpp:313
unsigned IsExplicit
Whether this is an explicit submodule.
Definition: Module.h:384
SmallVector< ExportDecl, 2 > Exports
The set of export declarations.
Definition: Module.h:471
bool isForBuilding(const LangOptions &LangOpts) const
Determine whether this module can be built in this compilation.
Definition: Module.cpp:155
std::variant< std::monostate, FileEntryRef, DirectoryEntryRef > Umbrella
The umbrella header or directory.
Definition: Module.h:205
unsigned InferSubmodules
Whether we should infer submodules for this module based on the headers.
Definition: Module.h:406
ArrayRef< Header > getAllHeaders() const
Definition: Module.h:301
Module * findSubmodule(StringRef Name) const
Find the submodule with the given name.
Definition: Module.cpp:350
bool directlyUses(const Module *Requested)
Determine whether this module has declared its intention to directly use another module.
Definition: Module.cpp:287
bool isNamedModuleInterfaceHasInit() const
Definition: Module.h:684
std::vector< std::string > ConfigMacros
The set of "configuration macros", which are macros that (intentionally) change how this module is bu...
Definition: Module.h:528
SourceLocation InferredSubmoduleLoc
The location of the inferred submodule.
Definition: Module.h:454
unsigned IsUnimportable
Whether this module has declared itself unimportable, either because it's missing a requirement from ...
Definition: Module.h:361
bool isInterfaceOrPartition() const
Definition: Module.h:671
NameVisibilityKind NameVisibility
The visibility of names within this particular module.
Definition: Module.h:451
bool isModulePartitionImplementation() const
Is this a module partition implementation unit.
Definition: Module.h:659
NameVisibilityKind
Describes the visibility of the various names within a particular module.
Definition: Module.h:443
@ Hidden
All of the names in this module are hidden.
Definition: Module.h:445
@ AllVisible
All of the names in this module are visible.
Definition: Module.h:447
void print(raw_ostream &OS, unsigned Indent=0, bool Dump=false) const
Print the module map for this module to the given stream.
Definition: Module.cpp:463
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
Definition: Module.h:676
SourceLocation DefinitionLoc
The location of the module definition.
Definition: Module.h:150
SmallVector< UnresolvedHeaderDirective, 1 > MissingHeaders
Headers that are mentioned in the module map file but could not be found on the file system.
Definition: Module.h:341
Module * Parent
The parent of this module.
Definition: Module.h:193
void markUnavailable(bool Unimportable)
Mark this module and all of its submodules as unavailable.
Definition: Module.cpp:325
SmallVector< UnresolvedHeaderDirective, 1 > UnresolvedHeaders
Headers that are mentioned in the module map file but that we have not yet attempted to resolve to a ...
Definition: Module.h:337
ModuleKind Kind
The kind of this module.
Definition: Module.h:189
bool isPrivateModule() const
Definition: Module.h:249
@ HK_PrivateTextual
Definition: Module.h:282
void addTopHeaderFilename(StringRef Filename)
Add a top-level header filename associated with this module.
Definition: Module.h:772
bool isUnimportable() const
Determine whether this module has been declared unimportable.
Definition: Module.h:563
bool fullModuleNameIs(ArrayRef< StringRef > nameParts) const
Whether the full name of this module is equal to joining nameParts with "."s.
Definition: Module.cpp:254
Module * getPrivateModuleFragment() const
Get the Private Module Fragment (sub-module) for this module, it there is one.
Definition: Module.cpp:372
void setASTFile(OptionalFileEntryRef File)
Set the serialized AST file for the top-level module of this module.
Definition: Module.h:742
unsigned IsInferred
Whether this is an inferred submodule (module * { ... }).
Definition: Module.h:399
llvm::SmallSetVector< Module *, 2 > Imports
The set of modules imported by this module, and on which this module depends.
Definition: Module.h:458
bool isModuleVisible(const Module *M) const
Determine whether the specified module would be visible to a lookup at the end of this module.
Definition: Module.h:827
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers).
Definition: Module.h:389
bool isModuleInterfaceUnit() const
Definition: Module.h:680
static StringRef getModuleInputBufferName()
Definition: Module.h:851
std::string Name
The name of this module.
Definition: Module.h:147
Module * getGlobalModuleFragment() const
Get the Global Module Fragment (sub-module) for this module, it there is one.
Definition: Module.cpp:361
bool isSubFramework() const
Determine whether this module is a subframework of another framework.
Definition: Module.h:635
llvm::iterator_range< submodule_iterator > submodules()
Definition: Module.h:838
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "C"...
Definition: Module.h:395
bool isModuleMapModule() const
Definition: Module.h:251
unsigned ModuleMapIsPrivate
Whether this module came from a "private" module map, found next to a regular (public) module map.
Definition: Module.h:434
llvm::SmallVector< LinkLibrary, 2 > LinkLibraries
The set of libraries or frameworks to link against when an entity from this module is used.
Definition: Module.h:520
SmallVector< UnresolvedExportDecl, 2 > UnresolvedExports
The set of export declarations that have yet to be resolved.
Definition: Module.h:489
void addHeader(HeaderKind HK, Header H)
Definition: Module.h:308
void setParent(Module *M)
Set the parent of this module.
Definition: Module.h:641
std::optional< Header > getUmbrellaHeaderAsWritten() const
Retrieve the umbrella header as written.
Definition: Module.h:756
unsigned getVisibilityID() const
Definition: Module.h:833
SmallVector< Requirement, 2 > Requirements
The set of language features required to use this module.
Definition: Module.h:352
bool isHeaderLikeModule() const
Is this module have similar semantics as headers.
Definition: Module.h:648
bool isModuleImplementation() const
Is this a module implementation.
Definition: Module.h:664
llvm::SmallSetVector< const Module *, 2 > UndeclaredUses
When NoUndeclaredIncludes is true, the set of modules this module tried to import but didn't because ...
Definition: Module.h:499
std::string UmbrellaRelativeToRootModuleDirectory
Definition: Module.h:214
OptionalDirectoryEntryRef Directory
The build directory of this module.
Definition: Module.h:198
std::vector< Module * >::iterator submodule_iterator
Definition: Module.h:835
llvm::iterator_range< submodule_const_iterator > submodules() const
Definition: Module.h:841
SmallVector< ModuleId, 2 > UnresolvedDirectUses
The set of use declarations that have yet to be resolved.
Definition: Module.h:495
unsigned NamedModuleHasInit
Whether this C++20 named modules doesn't need an initializer.
Definition: Module.h:439
unsigned NoUndeclaredIncludes
Whether files in this module can only include non-modular headers and headers from used modules.
Definition: Module.h:429
StringRef getPrimaryModuleInterfaceName() const
Get the primary module interface name from a partition.
Definition: Module.h:687
bool isModulePartition() const
Is this a module partition.
Definition: Module.h:653
llvm::SmallSetVector< Module *, 2 > AffectingClangModules
The set of top-level modules that affected the compilation of this module, but were not imported.
Definition: Module.h:462
SmallVector< Module *, 2 > DirectUses
The directly used modules.
Definition: Module.h:492
unsigned ConfigMacrosExhaustive
Whether the set of configuration macros is exhaustive.
Definition: Module.h:424
std::string PresumedModuleMapFile
The presumed file name for the module map defining this module.
Definition: Module.h:202
std::string APINotesFile
For the debug info, the path to this module's .apinotes file, if any.
Definition: Module.h:221
ASTFileSignature Signature
The module signature.
Definition: Module.h:208
bool isExplicitGlobalModule() const
Definition: Module.h:242
ArrayRef< Header > getHeaders(HeaderKind HK) const
Definition: Module.h:302
bool isGlobalModule() const
Does this Module scope describe a fragment of the global module within some C++ module.
Definition: Module.h:239
unsigned InferExportWildcard
Whether, when inferring submodules, the inferr submodules should export all modules they import (e....
Definition: Module.h:416
bool isSubModule() const
Determine whether this module is a submodule.
Definition: Module.h:612
void getExportedModules(SmallVectorImpl< Module * > &Exported) const
Appends this module's list of exported modules to Exported.
Definition: Module.cpp:383
std::vector< UnresolvedConflict > UnresolvedConflicts
The list of conflicts for which the module-id has not yet been resolved.
Definition: Module.h:541
unsigned IsFromModuleFile
Whether this module was loaded from a module file.
Definition: Module.h:376
bool isSubModuleOf(const Module *Other) const
Check if this module is a (possibly transitive) submodule of Other.
Definition: Module.cpp:193
bool isPartOfFramework() const
Determine whether this module is a part of a framework, either because it is a framework module or be...
Definition: Module.h:625
ArrayRef< FileEntryRef > getTopHeaders(FileManager &FileMgr)
The top-level headers associated with this module.
Definition: Module.cpp:276
bool isAvailable() const
Determine whether this module is available for use within the current translation unit.
Definition: Module.h:586
llvm::PointerIntPair< Module *, 1, bool > ExportDecl
Describes an exported module.
Definition: Module.h:468
std::optional< DirectoryName > getUmbrellaDirAsWritten() const
Retrieve the umbrella directory as written.
Definition: Module.h:748
unsigned HasIncompatibleModuleFile
Whether we tried and failed to load a module file for this module.
Definition: Module.h:365
bool isImplicitGlobalModule() const
Definition: Module.h:245
bool isHeaderUnit() const
Is this module a header unit.
Definition: Module.h:669
@ ModuleImplementationUnit
This is a C++20 module implementation unit.
Definition: Module.h:167
@ ModuleMapModule
This is a module that was defined by a module map and built out of header files.
Definition: Module.h:158
@ ImplicitGlobalModuleFragment
This is an implicit fragment of the global module which contains only language linkage declarations (...
Definition: Module.h:185
@ ModulePartitionInterface
This is a C++20 module partition interface.
Definition: Module.h:170
@ ModuleInterfaceUnit
This is a C++20 module interface unit.
Definition: Module.h:164
@ ModuleHeaderUnit
This is a C++20 header unit.
Definition: Module.h:161
@ ModulePartitionImplementation
This is a C++20 module partition implementation.
Definition: Module.h:173
@ PrivateModuleFragment
This is the private module fragment within some C++ module.
Definition: Module.h:180
@ ExplicitGlobalModuleFragment
This is the explicit Global Module Fragment of a modular TU.
Definition: Module.h:177
void dump() const
Dump the contents of this module to the given output stream.
Module * ShadowingModule
A module with the same name that shadows this module.
Definition: Module.h:355
unsigned IsFramework
Whether this is a framework module.
Definition: Module.h:380
std::string ExportAsModule
The module through which entities defined in this module will eventually be exposed,...
Definition: Module.h:218
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition: Module.cpp:239
bool isNamedModule() const
Does this Module is a named module of a standard named module?
Definition: Module.h:224
std::string UmbrellaAsWritten
The name of the umbrella entry, as written in the module map.
Definition: Module.h:211
void addTopHeader(FileEntryRef File)
Add a top-level header associated with this module.
Definition: Module.cpp:271
std::vector< Module * >::const_iterator submodule_const_iterator
Definition: Module.h:836
unsigned IsAvailable
Whether this module is available in the current translation unit.
Definition: Module.h:372
unsigned InferExplicitSubmodules
Whether, when inferring submodules, the inferred submodules should be explicit.
Definition: Module.h:411
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition: Module.h:722
OptionalFileEntryRef getASTFile() const
The serialized AST file for this module, if one was created.
Definition: Module.h:737
OptionalDirectoryEntryRef getEffectiveUmbrellaDir() const
Get the effective umbrella directory for this module: either the one explicitly written in the module...
Definition: Module.cpp:263
bool UseExportAsModuleLinkName
Autolinking uses the framework name for linking purposes when this is false and the export_as name ot...
Definition: Module.h:524
std::vector< Conflict > Conflicts
The list of conflicts.
Definition: Module.h:553
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
Exposes information about the current target.
Definition: TargetInfo.h:226
A set of visible modules.
Definition: Module.h:866
llvm::function_ref< void(ArrayRef< Module * > Path, Module *Conflict, StringRef Message)> ConflictCallback
A callback to call when a module conflict is found.
Definition: Module.h:910
void setVisible(Module *M, SourceLocation Loc, bool IncludeExports=true, VisibleCallback Vis=[](Module *) {}, ConflictCallback Cb=[](ArrayRef< Module * >, Module *, StringRef) {})
Make a specific module visible.
Definition: Module.cpp:662
llvm::function_ref< void(Module *M)> VisibleCallback
A callback to call when a module is made visible (directly or indirectly) by a call to setVisible.
Definition: Module.h:903
SourceLocation getImportLoc(const Module *M) const
Get the location at which the import of a module was triggered.
Definition: Module.h:895
bool isVisible(const Module *M) const
Determine whether a module is visible.
Definition: Module.h:890
unsigned getGeneration() const
Get the current visibility generation.
Definition: Module.h:887
VisibleModuleSet & operator=(VisibleModuleSet &&O)
Move from another visible modules set.
Definition: Module.h:877
VisibleModuleSet(VisibleModuleSet &&O)
Definition: Module.h:869
#define bool
Definition: gpuintrin.h:32
The JSON file list parser is used to communicate input to InstallAPI.
@ Other
Other implicit parameter.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
#define false
Definition: stdbool.h:26
The signature of a module, which is a hash of the AST content.
Definition: Module.h:58
uint64_t truncatedValue() const
Returns the value truncated to the size of an uint64_t.
Definition: Module.h:73
static constexpr size_t size
Definition: Module.h:61
static ASTFileSignature create(std::array< uint8_t, 20 > Bytes)
Definition: Module.h:81
ASTFileSignature(BaseT S={{0}})
Definition: Module.h:63
static ASTFileSignature createDummy()
Definition: Module.h:91
std::array< uint8_t, 20 > BaseT
Definition: Module.h:59
static ASTFileSignature createDISentinel()
Definition: Module.h:85
static ASTFileSignature create(InputIt First, InputIt Last)
Definition: Module.h:98
The set of attributes that can be attached to a module.
Definition: Module.h:109
unsigned IsExternC
Whether this is an extern "C" module.
Definition: Module.h:116
unsigned IsSystem
Whether this is a system module.
Definition: Module.h:112
unsigned IsExhaustive
Whether this is an exhaustive set of configuration macros.
Definition: Module.h:120
unsigned NoUndeclaredIncludes
Whether files in this module can only include non-modular headers and headers from used modules.
Definition: Module.h:125
A conflict between two modules.
Definition: Module.h:544
Module * Other
The module that this module conflicts with.
Definition: Module.h:546
std::string Message
The message provided to the user when there is a conflict.
Definition: Module.h:549
Information about a directory name as found in the module map file.
Definition: Module.h:317
std::string PathRelativeToRootModuleDirectory
Definition: Module.h:319
DirectoryEntryRef Entry
Definition: Module.h:320
std::string NameAsWritten
Definition: Module.h:318
Information about a header directive as found in the module map file.
Definition: Module.h:287
std::string PathRelativeToRootModuleDirectory
Definition: Module.h:289
std::string NameAsWritten
Definition: Module.h:288
FileEntryRef Entry
Definition: Module.h:290
A library or framework to link against when an entity from this module is used.
Definition: Module.h:503
bool IsFramework
Whether this is a framework rather than a library.
Definition: Module.h:515
LinkLibrary(const std::string &Library, bool IsFramework)
Definition: Module.h:505
std::string Library
The library to link against.
Definition: Module.h:512
std::string FeatureName
Definition: Module.h:344
An unresolved conflict with another module.
Definition: Module.h:531
std::string Message
The message provided to the user when there is a conflict.
Definition: Module.h:536
ModuleId Id
The (unresolved) module id.
Definition: Module.h:533
Describes an exported module that has not yet been resolved (perhaps because the module it refers to ...
Definition: Module.h:475
bool Wildcard
Whether this export declaration ends in a wildcard, indicating that all of its submodules should be e...
Definition: Module.h:485
ModuleId Id
The name of the module.
Definition: Module.h:480
SourceLocation ExportLoc
The location of the 'export' keyword in the module map file.
Definition: Module.h:477
Stored information about a header directive that was found in the module map file but has not been re...
Definition: Module.h:325
std::optional< off_t > Size
Definition: Module.h:331
std::optional< time_t > ModTime
Definition: Module.h:332