diff options
Diffstat (limited to 'contrib/llvm-project/clang/include/clang/Basic/Module.h')
-rw-r--r-- | contrib/llvm-project/clang/include/clang/Basic/Module.h | 254 |
1 files changed, 204 insertions, 50 deletions
diff --git a/contrib/llvm-project/clang/include/clang/Basic/Module.h b/contrib/llvm-project/clang/include/clang/Basic/Module.h index 3476b05d2e92..62786e3ac865 100644 --- a/contrib/llvm-project/clang/include/clang/Basic/Module.h +++ b/contrib/llvm-project/clang/include/clang/Basic/Module.h @@ -20,7 +20,6 @@ #include "clang/Basic/SourceLocation.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseSet.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/PointerIntPair.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SetVector.h" @@ -33,8 +32,10 @@ #include <cstdint> #include <ctime> #include <iterator> +#include <optional> #include <string> #include <utility> +#include <variant> #include <vector> namespace llvm { @@ -71,8 +72,8 @@ struct ASTFileSignature : std::array<uint8_t, 20> { return Value; } - static ASTFileSignature create(StringRef Bytes) { - return create(Bytes.bytes_begin(), Bytes.bytes_end()); + static ASTFileSignature create(std::array<uint8_t, 20> Bytes) { + return ASTFileSignature(std::move(Bytes)); } static ASTFileSignature createDISentinel() { @@ -81,6 +82,12 @@ struct ASTFileSignature : std::array<uint8_t, 20> { return Sentinel; } + static ASTFileSignature createDummy() { + ASTFileSignature Dummy; + Dummy.fill(0x00); + return Dummy; + } + template <typename InputIt> static ASTFileSignature create(InputIt First, InputIt Last) { assert(std::distance(First, Last) == size && @@ -93,7 +100,9 @@ struct ASTFileSignature : std::array<uint8_t, 20> { }; /// Describes a module or submodule. -class Module { +/// +/// Aligned to 8 bytes to allow for llvm::PointerIntPair<Module *, 3>. +class alignas(8) Module { public: /// The name of this module. std::string Name; @@ -101,19 +110,40 @@ public: /// The location of the module definition. SourceLocation DefinitionLoc; + // FIXME: Consider if reducing the size of this enum (having Partition and + // Named modules only) then representing interface/implementation separately + // is more efficient. enum ModuleKind { /// This is a module that was defined by a module map and built out /// of header files. ModuleMapModule, - /// This is a C++ Modules TS module interface unit. + /// This is a C++ 20 header unit. + ModuleHeaderUnit, + + /// This is a C++20 module interface unit. ModuleInterfaceUnit, - /// This is a fragment of the global module within some C++ module. - GlobalModuleFragment, + /// This is a C++20 module implementation unit. + ModuleImplementationUnit, + + /// This is a C++ 20 module partition interface. + ModulePartitionInterface, + + /// This is a C++ 20 module partition implementation. + ModulePartitionImplementation, + + /// This is the explicit Global Module Fragment of a modular TU. + /// As per C++ [module.global.frag]. + ExplicitGlobalModuleFragment, /// This is the private module fragment within some C++ module. PrivateModuleFragment, + + /// This is an implicit fragment of the global module which contains + /// only language linkage declarations (made in the purview of the + /// named module). + ImplicitGlobalModuleFragment, }; /// The kind of this module. @@ -126,14 +156,14 @@ public: /// The build directory of this module. This is the directory in /// which the module is notionally built, and relative to which its headers /// are found. - const DirectoryEntry *Directory = nullptr; + OptionalDirectoryEntryRef Directory; /// The presumed file name for the module map defining this module. /// Only non-empty when building from preprocessed source. std::string PresumedModuleMapFile; /// The umbrella header or directory. - llvm::PointerUnion<const FileEntry *, const DirectoryEntry *> Umbrella; + std::variant<std::monostate, FileEntryRef, DirectoryEntryRef> Umbrella; /// The module signature. ASTFileSignature Signature; @@ -148,10 +178,38 @@ public: /// eventually be exposed, for use in "private" modules. std::string ExportAsModule; - /// Does this Module scope describe part of the purview of a named C++ module? - bool isModulePurview() const { - return Kind == ModuleInterfaceUnit || Kind == PrivateModuleFragment; + /// For the debug info, the path to this module's .apinotes file, if any. + std::string APINotesFile; + + /// Does this Module is a named module of a standard named module? + bool isNamedModule() const { + switch (Kind) { + case ModuleInterfaceUnit: + case ModuleImplementationUnit: + case ModulePartitionInterface: + case ModulePartitionImplementation: + case PrivateModuleFragment: + return true; + default: + return false; + } + } + + /// Does this Module scope describe a fragment of the global module within + /// some C++ module. + bool isGlobalModule() const { + return isExplicitGlobalModule() || isImplicitGlobalModule(); } + bool isExplicitGlobalModule() const { + return Kind == ExplicitGlobalModuleFragment; + } + bool isImplicitGlobalModule() const { + return Kind == ImplicitGlobalModuleFragment; + } + + bool isPrivateModule() const { return Kind == PrivateModuleFragment; } + + bool isModuleMapModule() const { return Kind == ModuleMapModule; } private: /// The submodules of this module, indexed by name. @@ -163,10 +221,10 @@ private: /// The AST file if this is a top-level module which has a /// corresponding serialized AST file, or null otherwise. - Optional<FileEntryRef> ASTFile; + OptionalFileEntryRef ASTFile; /// The top-level headers associated with this module. - llvm::SmallSetVector<const FileEntry *, 2> TopHeaders; + llvm::SmallSetVector<FileEntryRef, 2> TopHeaders; /// top-level header filenames that aren't resolved to FileEntries yet. std::vector<std::string> TopHeaderNames; @@ -192,9 +250,7 @@ public: struct Header { std::string NameAsWritten; std::string PathRelativeToRootModuleDirectory; - const FileEntry *Entry; - - explicit operator bool() { return Entry; } + FileEntryRef Entry; }; /// Information about a directory name as found in the module map @@ -202,9 +258,7 @@ public: struct DirectoryName { std::string NameAsWritten; std::string PathRelativeToRootModuleDirectory; - const DirectoryEntry *Entry; - - explicit operator bool() { return Entry; } + DirectoryEntryRef Entry; }; /// The headers that are part of this module. @@ -218,8 +272,8 @@ public: std::string FileName; bool IsUmbrella = false; bool HasBuiltinHeader = false; - Optional<off_t> Size; - Optional<time_t> ModTime; + std::optional<off_t> Size; + std::optional<time_t> ModTime; }; /// Headers that are mentioned in the module map file but that we have not @@ -246,50 +300,62 @@ public: /// Whether this module has declared itself unimportable, either because /// it's missing a requirement from \p Requirements or because it's been /// shadowed by another module. + LLVM_PREFERRED_TYPE(bool) unsigned IsUnimportable : 1; /// Whether we tried and failed to load a module file for this module. + LLVM_PREFERRED_TYPE(bool) unsigned HasIncompatibleModuleFile : 1; /// Whether this module is available in the current translation unit. /// /// If the module is missing headers or does not meet all requirements then /// this bit will be 0. + LLVM_PREFERRED_TYPE(bool) unsigned IsAvailable : 1; /// Whether this module was loaded from a module file. + LLVM_PREFERRED_TYPE(bool) unsigned IsFromModuleFile : 1; /// Whether this is a framework module. + LLVM_PREFERRED_TYPE(bool) unsigned IsFramework : 1; /// Whether this is an explicit submodule. + LLVM_PREFERRED_TYPE(bool) unsigned IsExplicit : 1; /// Whether this is a "system" module (which assumes that all /// headers in it are system headers). + LLVM_PREFERRED_TYPE(bool) unsigned IsSystem : 1; /// Whether this is an 'extern "C"' module (which implicitly puts all /// headers in it within an 'extern "C"' block, and allows the module to be /// imported within such a block). + LLVM_PREFERRED_TYPE(bool) unsigned IsExternC : 1; /// Whether this is an inferred submodule (module * { ... }). + LLVM_PREFERRED_TYPE(bool) unsigned IsInferred : 1; /// Whether we should infer submodules for this module based on /// the headers. /// /// Submodules can only be inferred for modules with an umbrella header. + LLVM_PREFERRED_TYPE(bool) unsigned InferSubmodules : 1; /// Whether, when inferring submodules, the inferred submodules /// should be explicit. + LLVM_PREFERRED_TYPE(bool) unsigned InferExplicitSubmodules : 1; /// Whether, when inferring submodules, the inferr submodules should /// export all modules they import (e.g., the equivalent of "export *"). + LLVM_PREFERRED_TYPE(bool) unsigned InferExportWildcard : 1; /// Whether the set of configuration macros is exhaustive. @@ -297,16 +363,24 @@ public: /// When the set of configuration macros is exhaustive, meaning /// that no identifier not in this list should affect how the module is /// built. + LLVM_PREFERRED_TYPE(bool) unsigned ConfigMacrosExhaustive : 1; /// Whether files in this module can only include non-modular headers /// and headers from used modules. + LLVM_PREFERRED_TYPE(bool) unsigned NoUndeclaredIncludes : 1; /// Whether this module came from a "private" module map, found next /// to a regular (public) module map. + LLVM_PREFERRED_TYPE(bool) unsigned ModuleMapIsPrivate : 1; + /// Whether this C++20 named modules doesn't need an initializer. + /// This is only meaningful for C++20 modules. + LLVM_PREFERRED_TYPE(bool) + unsigned NamedModuleHasInit : 1; + /// Describes the visibility of the various names within a /// particular module. enum NameVisibilityKind { @@ -326,6 +400,10 @@ public: /// module depends. llvm::SmallSetVector<Module *, 2> Imports; + /// The set of top-level modules that affected the compilation of this module, + /// but were not imported. + llvm::SmallSetVector<Module *, 2> AffectingClangModules; + /// Describes an exported module. /// /// The pointer is the module being re-exported, while the bit will be true @@ -359,6 +437,10 @@ public: /// The set of use declarations that have yet to be resolved. SmallVector<ModuleId, 2> UnresolvedDirectUses; + /// When \c NoUndeclaredIncludes is true, the set of modules this module tried + /// to import but didn't because they are not direct uses. + llvm::SmallSetVector<const Module *, 2> UndeclaredUses; + /// A library or framework to link against when an entity from this /// module is used. struct LinkLibrary { @@ -438,6 +520,9 @@ public: bool isUnimportable(const LangOptions &LangOpts, const TargetInfo &Target, Requirement &Req, Module *&ShadowingModule) const; + /// Determine whether this module can be built in this compilation. + bool isForBuilding(const LangOptions &LangOpts) const; + /// Determine whether this module is available for use within the /// current translation unit. bool isAvailable() const { return IsAvailable; } @@ -502,6 +587,62 @@ public: Parent->SubModules.push_back(this); } + /// Is this module have similar semantics as headers. + bool isHeaderLikeModule() const { + return isModuleMapModule() || isHeaderUnit(); + } + + /// Is this a module partition. + bool isModulePartition() const { + return Kind == ModulePartitionInterface || + Kind == ModulePartitionImplementation; + } + + /// Is this a module implementation. + bool isModuleImplementation() const { + return Kind == ModuleImplementationUnit; + } + + /// Is this module a header unit. + bool isHeaderUnit() const { return Kind == ModuleHeaderUnit; } + // Is this a C++20 module interface or a partition. + bool isInterfaceOrPartition() const { + return Kind == ModuleInterfaceUnit || isModulePartition(); + } + + /// Is this a C++20 named module unit. + bool isNamedModuleUnit() const { + return isInterfaceOrPartition() || isModuleImplementation(); + } + + bool isModuleInterfaceUnit() const { + return Kind == ModuleInterfaceUnit || Kind == ModulePartitionInterface; + } + + bool isNamedModuleInterfaceHasInit() const { return NamedModuleHasInit; } + + /// Get the primary module interface name from a partition. + StringRef getPrimaryModuleInterfaceName() const { + // Technically, global module fragment belongs to global module. And global + // module has no name: [module.unit]p6: + // The global module has no name, no module interface unit, and is not + // introduced by any module-declaration. + // + // <global> is the default name showed in module map. + if (isGlobalModule()) + return "<global>"; + + if (isModulePartition()) { + auto pos = Name.find(':'); + return StringRef(Name.data(), pos); + } + + if (isPrivateModule()) + return getTopLevelModuleName(); + + return Name; + } + /// Retrieve the full name of this module, including the path from /// its top-level module. /// \param AllowStringLiterals If \c true, components that might not be @@ -531,38 +672,39 @@ public: } /// The serialized AST file for this module, if one was created. - OptionalFileEntryRefDegradesToFileEntryPtr getASTFile() const { + OptionalFileEntryRef getASTFile() const { return getTopLevelModule()->ASTFile; } /// Set the serialized AST file for the top-level module of this module. - void setASTFile(Optional<FileEntryRef> File) { - assert((!File || !getASTFile() || getASTFile() == File) && - "file path changed"); + void setASTFile(OptionalFileEntryRef File) { + assert((!getASTFile() || getASTFile() == File) && "file path changed"); getTopLevelModule()->ASTFile = File; } - /// Retrieve the directory for which this module serves as the - /// umbrella. - DirectoryName getUmbrellaDir() const; + /// Retrieve the umbrella directory as written. + std::optional<DirectoryName> getUmbrellaDirAsWritten() const { + if (const auto *Dir = std::get_if<DirectoryEntryRef>(&Umbrella)) + return DirectoryName{UmbrellaAsWritten, + UmbrellaRelativeToRootModuleDirectory, *Dir}; + return std::nullopt; + } - /// Retrieve the header that serves as the umbrella header for this - /// module. - Header getUmbrellaHeader() const { - if (auto *FE = Umbrella.dyn_cast<const FileEntry *>()) + /// Retrieve the umbrella header as written. + std::optional<Header> getUmbrellaHeaderAsWritten() const { + if (const auto *Hdr = std::get_if<FileEntryRef>(&Umbrella)) return Header{UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory, - FE}; - return Header{}; + *Hdr}; + return std::nullopt; } - /// Determine whether this module has an umbrella directory that is - /// not based on an umbrella header. - bool hasUmbrellaDir() const { - return Umbrella && Umbrella.is<const DirectoryEntry *>(); - } + /// Get the effective umbrella directory for this module: either the one + /// explicitly written in the module map file, or the parent of the umbrella + /// header. + OptionalDirectoryEntryRef getEffectiveUmbrellaDir() const; /// Add a top-level header associated with this module. - void addTopHeader(const FileEntry *File); + void addTopHeader(FileEntryRef File); /// Add a top-level header filename associated with this module. void addTopHeaderFilename(StringRef Filename) { @@ -570,11 +712,11 @@ public: } /// The top-level headers associated with this module. - ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr); + ArrayRef<FileEntryRef> getTopHeaders(FileManager &FileMgr); /// Determine whether this module has declared its intention to /// directly use another module. - bool directlyUses(const Module *Requested) const; + bool directlyUses(const Module *Requested); /// Add the given feature requirement to the list of features /// required by this module. @@ -603,6 +745,18 @@ public: Module *findSubmodule(StringRef Name) const; Module *findOrInferSubmodule(StringRef Name); + /// Get the Global Module Fragment (sub-module) for this module, it there is + /// one. + /// + /// \returns The GMF sub-module if found, or NULL otherwise. + Module *getGlobalModuleFragment() const; + + /// Get the Private Module Fragment (sub-module) for this module, it there is + /// one. + /// + /// \returns The PMF sub-module if found, or NULL otherwise. + Module *getPrivateModuleFragment() const; + /// Determine whether the specified module would be visible to /// a lookup at the end of this module. /// @@ -620,16 +774,11 @@ public: using submodule_iterator = std::vector<Module *>::iterator; using submodule_const_iterator = std::vector<Module *>::const_iterator; - submodule_iterator submodule_begin() { return SubModules.begin(); } - submodule_const_iterator submodule_begin() const {return SubModules.begin();} - submodule_iterator submodule_end() { return SubModules.end(); } - submodule_const_iterator submodule_end() const { return SubModules.end(); } - llvm::iterator_range<submodule_iterator> submodules() { - return llvm::make_range(submodule_begin(), submodule_end()); + return llvm::make_range(SubModules.begin(), SubModules.end()); } llvm::iterator_range<submodule_const_iterator> submodules() const { - return llvm::make_range(submodule_begin(), submodule_end()); + return llvm::make_range(SubModules.begin(), SubModules.end()); } /// Appends this module's list of exported modules to \p Exported. @@ -705,6 +854,11 @@ public: ConflictCallback Cb = [](ArrayRef<Module *>, Module *, StringRef) {}); + /// Make transitive imports visible for [module.import]/7. + void makeTransitiveImportsVisible( + Module *M, SourceLocation Loc, VisibleCallback Vis = [](Module *) {}, + ConflictCallback Cb = [](ArrayRef<Module *>, Module *, StringRef) {}); + private: /// Import locations for each visible module. Indexed by the module's /// VisibilityID. |