xref: /netbsd-src/external/apache2/llvm/dist/clang/include/clang/Lex/ModuleMap.h (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===- ModuleMap.h - Describe the layout of modules -------------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines the ModuleMap interface, which describes the layout of a
107330f729Sjoerg // module as it relates to headers.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg 
147330f729Sjoerg #ifndef LLVM_CLANG_LEX_MODULEMAP_H
157330f729Sjoerg #define LLVM_CLANG_LEX_MODULEMAP_H
167330f729Sjoerg 
17*e038c9c4Sjoerg #include "clang/Basic/IdentifierTable.h"
187330f729Sjoerg #include "clang/Basic/LangOptions.h"
197330f729Sjoerg #include "clang/Basic/Module.h"
207330f729Sjoerg #include "clang/Basic/SourceLocation.h"
217330f729Sjoerg #include "llvm/ADT/ArrayRef.h"
227330f729Sjoerg #include "llvm/ADT/DenseMap.h"
237330f729Sjoerg #include "llvm/ADT/PointerIntPair.h"
247330f729Sjoerg #include "llvm/ADT/SmallPtrSet.h"
257330f729Sjoerg #include "llvm/ADT/SmallVector.h"
267330f729Sjoerg #include "llvm/ADT/StringMap.h"
277330f729Sjoerg #include "llvm/ADT/StringRef.h"
28*e038c9c4Sjoerg #include "llvm/ADT/StringSet.h"
297330f729Sjoerg #include "llvm/ADT/TinyPtrVector.h"
307330f729Sjoerg #include "llvm/ADT/Twine.h"
317330f729Sjoerg #include <ctime>
327330f729Sjoerg #include <memory>
337330f729Sjoerg #include <string>
347330f729Sjoerg #include <utility>
357330f729Sjoerg 
367330f729Sjoerg namespace clang {
377330f729Sjoerg 
387330f729Sjoerg class DiagnosticsEngine;
397330f729Sjoerg class DirectoryEntry;
407330f729Sjoerg class FileEntry;
417330f729Sjoerg class FileManager;
427330f729Sjoerg class HeaderSearch;
437330f729Sjoerg class SourceManager;
447330f729Sjoerg 
457330f729Sjoerg /// A mechanism to observe the actions of the module map parser as it
467330f729Sjoerg /// reads module map files.
477330f729Sjoerg class ModuleMapCallbacks {
487330f729Sjoerg   virtual void anchor();
497330f729Sjoerg 
507330f729Sjoerg public:
517330f729Sjoerg   virtual ~ModuleMapCallbacks() = default;
527330f729Sjoerg 
537330f729Sjoerg   /// Called when a module map file has been read.
547330f729Sjoerg   ///
557330f729Sjoerg   /// \param FileStart A SourceLocation referring to the start of the file's
567330f729Sjoerg   /// contents.
577330f729Sjoerg   /// \param File The file itself.
587330f729Sjoerg   /// \param IsSystem Whether this is a module map from a system include path.
moduleMapFileRead(SourceLocation FileStart,const FileEntry & File,bool IsSystem)597330f729Sjoerg   virtual void moduleMapFileRead(SourceLocation FileStart,
607330f729Sjoerg                                  const FileEntry &File, bool IsSystem) {}
617330f729Sjoerg 
627330f729Sjoerg   /// Called when a header is added during module map parsing.
637330f729Sjoerg   ///
647330f729Sjoerg   /// \param Filename The header file itself.
moduleMapAddHeader(StringRef Filename)657330f729Sjoerg   virtual void moduleMapAddHeader(StringRef Filename) {}
667330f729Sjoerg 
677330f729Sjoerg   /// Called when an umbrella header is added during module map parsing.
687330f729Sjoerg   ///
697330f729Sjoerg   /// \param FileMgr FileManager instance
707330f729Sjoerg   /// \param Header The umbrella header to collect.
moduleMapAddUmbrellaHeader(FileManager * FileMgr,const FileEntry * Header)717330f729Sjoerg   virtual void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
727330f729Sjoerg                                           const FileEntry *Header) {}
737330f729Sjoerg };
747330f729Sjoerg 
757330f729Sjoerg class ModuleMap {
767330f729Sjoerg   SourceManager &SourceMgr;
777330f729Sjoerg   DiagnosticsEngine &Diags;
787330f729Sjoerg   const LangOptions &LangOpts;
797330f729Sjoerg   const TargetInfo *Target;
807330f729Sjoerg   HeaderSearch &HeaderInfo;
817330f729Sjoerg 
827330f729Sjoerg   llvm::SmallVector<std::unique_ptr<ModuleMapCallbacks>, 1> Callbacks;
837330f729Sjoerg 
847330f729Sjoerg   /// The directory used for Clang-supplied, builtin include headers,
857330f729Sjoerg   /// such as "stdint.h".
867330f729Sjoerg   const DirectoryEntry *BuiltinIncludeDir = nullptr;
877330f729Sjoerg 
887330f729Sjoerg   /// Language options used to parse the module map itself.
897330f729Sjoerg   ///
907330f729Sjoerg   /// These are always simple C language options.
917330f729Sjoerg   LangOptions MMapLangOpts;
927330f729Sjoerg 
937330f729Sjoerg   /// The module that the main source file is associated with (the module
947330f729Sjoerg   /// named LangOpts::CurrentModule, if we've loaded it).
957330f729Sjoerg   Module *SourceModule = nullptr;
967330f729Sjoerg 
977330f729Sjoerg   /// Submodules of the current module that have not yet been attached to it.
987330f729Sjoerg   /// (Ownership is transferred if/when we create an enclosing module.)
997330f729Sjoerg   llvm::SmallVector<std::unique_ptr<Module>, 8> PendingSubmodules;
1007330f729Sjoerg 
1017330f729Sjoerg   /// The top-level modules that are known.
1027330f729Sjoerg   llvm::StringMap<Module *> Modules;
1037330f729Sjoerg 
104*e038c9c4Sjoerg   /// Module loading cache that includes submodules, indexed by IdentifierInfo.
105*e038c9c4Sjoerg   /// nullptr is stored for modules that are known to fail to load.
106*e038c9c4Sjoerg   llvm::DenseMap<const IdentifierInfo *, Module *> CachedModuleLoads;
107*e038c9c4Sjoerg 
1087330f729Sjoerg   /// Shadow modules created while building this module map.
1097330f729Sjoerg   llvm::SmallVector<Module*, 2> ShadowModules;
1107330f729Sjoerg 
1117330f729Sjoerg   /// The number of modules we have created in total.
1127330f729Sjoerg   unsigned NumCreatedModules = 0;
1137330f729Sjoerg 
1147330f729Sjoerg   /// In case a module has a export_as entry, it might have a pending link
1157330f729Sjoerg   /// name to be determined if that module is imported.
1167330f729Sjoerg   llvm::StringMap<llvm::StringSet<>> PendingLinkAsModule;
1177330f729Sjoerg 
1187330f729Sjoerg public:
1197330f729Sjoerg   /// Use PendingLinkAsModule information to mark top level link names that
1207330f729Sjoerg   /// are going to be replaced by export_as aliases.
1217330f729Sjoerg   void resolveLinkAsDependencies(Module *Mod);
1227330f729Sjoerg 
1237330f729Sjoerg   /// Make module to use export_as as the link dependency name if enough
1247330f729Sjoerg   /// information is available or add it to a pending list otherwise.
1257330f729Sjoerg   void addLinkAsDependency(Module *Mod);
1267330f729Sjoerg 
1277330f729Sjoerg   /// Flags describing the role of a module header.
1287330f729Sjoerg   enum ModuleHeaderRole {
1297330f729Sjoerg     /// This header is normally included in the module.
1307330f729Sjoerg     NormalHeader  = 0x0,
1317330f729Sjoerg 
1327330f729Sjoerg     /// This header is included but private.
1337330f729Sjoerg     PrivateHeader = 0x1,
1347330f729Sjoerg 
1357330f729Sjoerg     /// This header is part of the module (for layering purposes) but
1367330f729Sjoerg     /// should be textually included.
1377330f729Sjoerg     TextualHeader = 0x2,
1387330f729Sjoerg 
1397330f729Sjoerg     // Caution: Adding an enumerator needs other changes.
1407330f729Sjoerg     // Adjust the number of bits for KnownHeader::Storage.
1417330f729Sjoerg     // Adjust the bitfield HeaderFileInfo::HeaderRole size.
1427330f729Sjoerg     // Adjust the HeaderFileInfoTrait::ReadData streaming.
1437330f729Sjoerg     // Adjust the HeaderFileInfoTrait::EmitData streaming.
1447330f729Sjoerg     // Adjust ModuleMap::addHeader.
1457330f729Sjoerg   };
1467330f729Sjoerg 
1477330f729Sjoerg   /// Convert a header kind to a role. Requires Kind to not be HK_Excluded.
1487330f729Sjoerg   static ModuleHeaderRole headerKindToRole(Module::HeaderKind Kind);
1497330f729Sjoerg 
1507330f729Sjoerg   /// Convert a header role to a kind.
1517330f729Sjoerg   static Module::HeaderKind headerRoleToKind(ModuleHeaderRole Role);
1527330f729Sjoerg 
1537330f729Sjoerg   /// A header that is known to reside within a given module,
1547330f729Sjoerg   /// whether it was included or excluded.
1557330f729Sjoerg   class KnownHeader {
1567330f729Sjoerg     llvm::PointerIntPair<Module *, 2, ModuleHeaderRole> Storage;
1577330f729Sjoerg 
1587330f729Sjoerg   public:
KnownHeader()1597330f729Sjoerg     KnownHeader() : Storage(nullptr, NormalHeader) {}
KnownHeader(Module * M,ModuleHeaderRole Role)1607330f729Sjoerg     KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) {}
1617330f729Sjoerg 
1627330f729Sjoerg     friend bool operator==(const KnownHeader &A, const KnownHeader &B) {
1637330f729Sjoerg       return A.Storage == B.Storage;
1647330f729Sjoerg     }
1657330f729Sjoerg     friend bool operator!=(const KnownHeader &A, const KnownHeader &B) {
1667330f729Sjoerg       return A.Storage != B.Storage;
1677330f729Sjoerg     }
1687330f729Sjoerg 
1697330f729Sjoerg     /// Retrieve the module the header is stored in.
getModule()1707330f729Sjoerg     Module *getModule() const { return Storage.getPointer(); }
1717330f729Sjoerg 
1727330f729Sjoerg     /// The role of this header within the module.
getRole()1737330f729Sjoerg     ModuleHeaderRole getRole() const { return Storage.getInt(); }
1747330f729Sjoerg 
1757330f729Sjoerg     /// Whether this header is available in the module.
isAvailable()1767330f729Sjoerg     bool isAvailable() const {
1777330f729Sjoerg       return getModule()->isAvailable();
1787330f729Sjoerg     }
1797330f729Sjoerg 
1807330f729Sjoerg     /// Whether this header is accessible from the specified module.
isAccessibleFrom(Module * M)1817330f729Sjoerg     bool isAccessibleFrom(Module *M) const {
1827330f729Sjoerg       return !(getRole() & PrivateHeader) ||
1837330f729Sjoerg              (M && M->getTopLevelModule() == getModule()->getTopLevelModule());
1847330f729Sjoerg     }
1857330f729Sjoerg 
1867330f729Sjoerg     // Whether this known header is valid (i.e., it has an
1877330f729Sjoerg     // associated module).
1887330f729Sjoerg     explicit operator bool() const {
1897330f729Sjoerg       return Storage.getPointer() != nullptr;
1907330f729Sjoerg     }
1917330f729Sjoerg   };
1927330f729Sjoerg 
1937330f729Sjoerg   using AdditionalModMapsSet = llvm::SmallPtrSet<const FileEntry *, 1>;
1947330f729Sjoerg 
1957330f729Sjoerg private:
1967330f729Sjoerg   friend class ModuleMapParser;
1977330f729Sjoerg 
1987330f729Sjoerg   using HeadersMap =
1997330f729Sjoerg       llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1>>;
2007330f729Sjoerg 
2017330f729Sjoerg   /// Mapping from each header to the module that owns the contents of
2027330f729Sjoerg   /// that header.
2037330f729Sjoerg   HeadersMap Headers;
2047330f729Sjoerg 
2057330f729Sjoerg   /// Map from file sizes to modules with lazy header directives of that size.
2067330f729Sjoerg   mutable llvm::DenseMap<off_t, llvm::TinyPtrVector<Module*>> LazyHeadersBySize;
2077330f729Sjoerg 
2087330f729Sjoerg   /// Map from mtimes to modules with lazy header directives with those mtimes.
2097330f729Sjoerg   mutable llvm::DenseMap<time_t, llvm::TinyPtrVector<Module*>>
2107330f729Sjoerg               LazyHeadersByModTime;
2117330f729Sjoerg 
2127330f729Sjoerg   /// Mapping from directories with umbrella headers to the module
2137330f729Sjoerg   /// that is generated from the umbrella header.
2147330f729Sjoerg   ///
2157330f729Sjoerg   /// This mapping is used to map headers that haven't explicitly been named
2167330f729Sjoerg   /// in the module map over to the module that includes them via its umbrella
2177330f729Sjoerg   /// header.
2187330f729Sjoerg   llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
2197330f729Sjoerg 
2207330f729Sjoerg   /// A generation counter that is used to test whether modules of the
2217330f729Sjoerg   /// same name may shadow or are illegal redefinitions.
2227330f729Sjoerg   ///
2237330f729Sjoerg   /// Modules from earlier scopes may shadow modules from later ones.
2247330f729Sjoerg   /// Modules from the same scope may not have the same name.
2257330f729Sjoerg   unsigned CurrentModuleScopeID = 0;
2267330f729Sjoerg 
2277330f729Sjoerg   llvm::DenseMap<Module *, unsigned> ModuleScopeIDs;
2287330f729Sjoerg 
2297330f729Sjoerg   /// The set of attributes that can be attached to a module.
2307330f729Sjoerg   struct Attributes {
2317330f729Sjoerg     /// Whether this is a system module.
2327330f729Sjoerg     unsigned IsSystem : 1;
2337330f729Sjoerg 
2347330f729Sjoerg     /// Whether this is an extern "C" module.
2357330f729Sjoerg     unsigned IsExternC : 1;
2367330f729Sjoerg 
2377330f729Sjoerg     /// Whether this is an exhaustive set of configuration macros.
2387330f729Sjoerg     unsigned IsExhaustive : 1;
2397330f729Sjoerg 
2407330f729Sjoerg     /// Whether files in this module can only include non-modular headers
2417330f729Sjoerg     /// and headers from used modules.
2427330f729Sjoerg     unsigned NoUndeclaredIncludes : 1;
2437330f729Sjoerg 
AttributesAttributes2447330f729Sjoerg     Attributes()
2457330f729Sjoerg         : IsSystem(false), IsExternC(false), IsExhaustive(false),
2467330f729Sjoerg           NoUndeclaredIncludes(false) {}
2477330f729Sjoerg   };
2487330f729Sjoerg 
2497330f729Sjoerg   /// A directory for which framework modules can be inferred.
2507330f729Sjoerg   struct InferredDirectory {
2517330f729Sjoerg     /// Whether to infer modules from this directory.
2527330f729Sjoerg     unsigned InferModules : 1;
2537330f729Sjoerg 
2547330f729Sjoerg     /// The attributes to use for inferred modules.
2557330f729Sjoerg     Attributes Attrs;
2567330f729Sjoerg 
2577330f729Sjoerg     /// If \c InferModules is non-zero, the module map file that allowed
2587330f729Sjoerg     /// inferred modules.  Otherwise, nullptr.
2597330f729Sjoerg     const FileEntry *ModuleMapFile;
2607330f729Sjoerg 
2617330f729Sjoerg     /// The names of modules that cannot be inferred within this
2627330f729Sjoerg     /// directory.
2637330f729Sjoerg     SmallVector<std::string, 2> ExcludedModules;
2647330f729Sjoerg 
InferredDirectoryInferredDirectory2657330f729Sjoerg     InferredDirectory() : InferModules(false) {}
2667330f729Sjoerg   };
2677330f729Sjoerg 
2687330f729Sjoerg   /// A mapping from directories to information about inferring
2697330f729Sjoerg   /// framework modules from within those directories.
2707330f729Sjoerg   llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
2717330f729Sjoerg 
2727330f729Sjoerg   /// A mapping from an inferred module to the module map that allowed the
2737330f729Sjoerg   /// inference.
2747330f729Sjoerg   llvm::DenseMap<const Module *, const FileEntry *> InferredModuleAllowedBy;
2757330f729Sjoerg 
2767330f729Sjoerg   llvm::DenseMap<const Module *, AdditionalModMapsSet> AdditionalModMaps;
2777330f729Sjoerg 
2787330f729Sjoerg   /// Describes whether we haved parsed a particular file as a module
2797330f729Sjoerg   /// map.
2807330f729Sjoerg   llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap;
2817330f729Sjoerg 
2827330f729Sjoerg   /// Resolve the given export declaration into an actual export
2837330f729Sjoerg   /// declaration.
2847330f729Sjoerg   ///
2857330f729Sjoerg   /// \param Mod The module in which we're resolving the export declaration.
2867330f729Sjoerg   ///
2877330f729Sjoerg   /// \param Unresolved The export declaration to resolve.
2887330f729Sjoerg   ///
2897330f729Sjoerg   /// \param Complain Whether this routine should complain about unresolvable
2907330f729Sjoerg   /// exports.
2917330f729Sjoerg   ///
2927330f729Sjoerg   /// \returns The resolved export declaration, which will have a NULL pointer
2937330f729Sjoerg   /// if the export could not be resolved.
2947330f729Sjoerg   Module::ExportDecl
2957330f729Sjoerg   resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
2967330f729Sjoerg                 bool Complain) const;
2977330f729Sjoerg 
2987330f729Sjoerg   /// Resolve the given module id to an actual module.
2997330f729Sjoerg   ///
3007330f729Sjoerg   /// \param Id The module-id to resolve.
3017330f729Sjoerg   ///
3027330f729Sjoerg   /// \param Mod The module in which we're resolving the module-id.
3037330f729Sjoerg   ///
3047330f729Sjoerg   /// \param Complain Whether this routine should complain about unresolvable
3057330f729Sjoerg   /// module-ids.
3067330f729Sjoerg   ///
3077330f729Sjoerg   /// \returns The resolved module, or null if the module-id could not be
3087330f729Sjoerg   /// resolved.
3097330f729Sjoerg   Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const;
3107330f729Sjoerg 
3117330f729Sjoerg   /// Add an unresolved header to a module.
3127330f729Sjoerg   ///
3137330f729Sjoerg   /// \param Mod The module in which we're adding the unresolved header
3147330f729Sjoerg   ///        directive.
3157330f729Sjoerg   /// \param Header The unresolved header directive.
3167330f729Sjoerg   /// \param NeedsFramework If Mod is not a framework but a missing header would
3177330f729Sjoerg   ///        be found in case Mod was, set it to true. False otherwise.
3187330f729Sjoerg   void addUnresolvedHeader(Module *Mod,
3197330f729Sjoerg                            Module::UnresolvedHeaderDirective Header,
3207330f729Sjoerg                            bool &NeedsFramework);
3217330f729Sjoerg 
3227330f729Sjoerg   /// Look up the given header directive to find an actual header file.
3237330f729Sjoerg   ///
3247330f729Sjoerg   /// \param M The module in which we're resolving the header directive.
3257330f729Sjoerg   /// \param Header The header directive to resolve.
3267330f729Sjoerg   /// \param RelativePathName Filled in with the relative path name from the
3277330f729Sjoerg   ///        module to the resolved header.
3287330f729Sjoerg   /// \param NeedsFramework If M is not a framework but a missing header would
3297330f729Sjoerg   ///        be found in case M was, set it to true. False otherwise.
3307330f729Sjoerg   /// \return The resolved file, if any.
331*e038c9c4Sjoerg   Optional<FileEntryRef>
332*e038c9c4Sjoerg   findHeader(Module *M, const Module::UnresolvedHeaderDirective &Header,
333*e038c9c4Sjoerg              SmallVectorImpl<char> &RelativePathName, bool &NeedsFramework);
3347330f729Sjoerg 
3357330f729Sjoerg   /// Resolve the given header directive.
3367330f729Sjoerg   ///
3377330f729Sjoerg   /// \param M The module in which we're resolving the header directive.
3387330f729Sjoerg   /// \param Header The header directive to resolve.
3397330f729Sjoerg   /// \param NeedsFramework If M is not a framework but a missing header would
3407330f729Sjoerg   ///        be found in case M was, set it to true. False otherwise.
3417330f729Sjoerg   void resolveHeader(Module *M, const Module::UnresolvedHeaderDirective &Header,
3427330f729Sjoerg                      bool &NeedsFramework);
3437330f729Sjoerg 
3447330f729Sjoerg   /// Attempt to resolve the specified header directive as naming a builtin
3457330f729Sjoerg   /// header.
3467330f729Sjoerg   /// \return \c true if a corresponding builtin header was found.
3477330f729Sjoerg   bool resolveAsBuiltinHeader(Module *M,
3487330f729Sjoerg                               const Module::UnresolvedHeaderDirective &Header);
3497330f729Sjoerg 
3507330f729Sjoerg   /// Looks up the modules that \p File corresponds to.
3517330f729Sjoerg   ///
3527330f729Sjoerg   /// If \p File represents a builtin header within Clang's builtin include
3537330f729Sjoerg   /// directory, this also loads all of the module maps to see if it will get
3547330f729Sjoerg   /// associated with a specific module (e.g. in /usr/include).
3557330f729Sjoerg   HeadersMap::iterator findKnownHeader(const FileEntry *File);
3567330f729Sjoerg 
3577330f729Sjoerg   /// Searches for a module whose umbrella directory contains \p File.
3587330f729Sjoerg   ///
3597330f729Sjoerg   /// \param File The header to search for.
3607330f729Sjoerg   ///
3617330f729Sjoerg   /// \param IntermediateDirs On success, contains the set of directories
3627330f729Sjoerg   /// searched before finding \p File.
3637330f729Sjoerg   KnownHeader findHeaderInUmbrellaDirs(const FileEntry *File,
3647330f729Sjoerg                     SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs);
3657330f729Sjoerg 
3667330f729Sjoerg   /// Given that \p File is not in the Headers map, look it up within
3677330f729Sjoerg   /// umbrella directories and find or create a module for it.
3687330f729Sjoerg   KnownHeader findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File);
3697330f729Sjoerg 
3707330f729Sjoerg   /// A convenience method to determine if \p File is (possibly nested)
3717330f729Sjoerg   /// in an umbrella directory.
isHeaderInUmbrellaDirs(const FileEntry * File)3727330f729Sjoerg   bool isHeaderInUmbrellaDirs(const FileEntry *File) {
3737330f729Sjoerg     SmallVector<const DirectoryEntry *, 2> IntermediateDirs;
3747330f729Sjoerg     return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs));
3757330f729Sjoerg   }
3767330f729Sjoerg 
3777330f729Sjoerg   Module *inferFrameworkModule(const DirectoryEntry *FrameworkDir,
3787330f729Sjoerg                                Attributes Attrs, Module *Parent);
3797330f729Sjoerg 
3807330f729Sjoerg public:
3817330f729Sjoerg   /// Construct a new module map.
3827330f729Sjoerg   ///
3837330f729Sjoerg   /// \param SourceMgr The source manager used to find module files and headers.
3847330f729Sjoerg   /// This source manager should be shared with the header-search mechanism,
3857330f729Sjoerg   /// since they will refer to the same headers.
3867330f729Sjoerg   ///
3877330f729Sjoerg   /// \param Diags A diagnostic engine used for diagnostics.
3887330f729Sjoerg   ///
3897330f729Sjoerg   /// \param LangOpts Language options for this translation unit.
3907330f729Sjoerg   ///
3917330f729Sjoerg   /// \param Target The target for this translation unit.
3927330f729Sjoerg   ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
3937330f729Sjoerg             const LangOptions &LangOpts, const TargetInfo *Target,
3947330f729Sjoerg             HeaderSearch &HeaderInfo);
3957330f729Sjoerg 
3967330f729Sjoerg   /// Destroy the module map.
3977330f729Sjoerg   ~ModuleMap();
3987330f729Sjoerg 
3997330f729Sjoerg   /// Set the target information.
4007330f729Sjoerg   void setTarget(const TargetInfo &Target);
4017330f729Sjoerg 
4027330f729Sjoerg   /// Set the directory that contains Clang-supplied include
4037330f729Sjoerg   /// files, such as our stdarg.h or tgmath.h.
setBuiltinIncludeDir(const DirectoryEntry * Dir)4047330f729Sjoerg   void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
4057330f729Sjoerg     BuiltinIncludeDir = Dir;
4067330f729Sjoerg   }
4077330f729Sjoerg 
4087330f729Sjoerg   /// Get the directory that contains Clang-supplied include files.
getBuiltinDir()4097330f729Sjoerg   const DirectoryEntry *getBuiltinDir() const {
4107330f729Sjoerg     return BuiltinIncludeDir;
4117330f729Sjoerg   }
4127330f729Sjoerg 
4137330f729Sjoerg   /// Is this a compiler builtin header?
4147330f729Sjoerg   static bool isBuiltinHeader(StringRef FileName);
415*e038c9c4Sjoerg   bool isBuiltinHeader(const FileEntry *File);
4167330f729Sjoerg 
4177330f729Sjoerg   /// Add a module map callback.
addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback)4187330f729Sjoerg   void addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback) {
4197330f729Sjoerg     Callbacks.push_back(std::move(Callback));
4207330f729Sjoerg   }
4217330f729Sjoerg 
422*e038c9c4Sjoerg   /// Retrieve the module that owns the given header file, if any. Note that
423*e038c9c4Sjoerg   /// this does not implicitly load module maps, except for builtin headers,
424*e038c9c4Sjoerg   /// and does not consult the external source. (Those checks are the
425*e038c9c4Sjoerg   /// responsibility of \ref HeaderSearch.)
4267330f729Sjoerg   ///
4277330f729Sjoerg   /// \param File The header file that is likely to be included.
4287330f729Sjoerg   ///
4297330f729Sjoerg   /// \param AllowTextual If \c true and \p File is a textual header, return
4307330f729Sjoerg   /// its owning module. Otherwise, no KnownHeader will be returned if the
4317330f729Sjoerg   /// file is only known as a textual header.
4327330f729Sjoerg   ///
4337330f729Sjoerg   /// \returns The module KnownHeader, which provides the module that owns the
4347330f729Sjoerg   /// given header file.  The KnownHeader is default constructed to indicate
4357330f729Sjoerg   /// that no module owns this header file.
4367330f729Sjoerg   KnownHeader findModuleForHeader(const FileEntry *File,
4377330f729Sjoerg                                   bool AllowTextual = false);
4387330f729Sjoerg 
439*e038c9c4Sjoerg   /// Retrieve all the modules that contain the given header file. Note that
440*e038c9c4Sjoerg   /// this does not implicitly load module maps, except for builtin headers,
441*e038c9c4Sjoerg   /// and does not consult the external source. (Those checks are the
442*e038c9c4Sjoerg   /// responsibility of \ref HeaderSearch.)
4437330f729Sjoerg   ///
4447330f729Sjoerg   /// Typically, \ref findModuleForHeader should be used instead, as it picks
4457330f729Sjoerg   /// the preferred module for the header.
446*e038c9c4Sjoerg   ArrayRef<KnownHeader> findAllModulesForHeader(const FileEntry *File);
447*e038c9c4Sjoerg 
448*e038c9c4Sjoerg   /// Like \ref findAllModulesForHeader, but do not attempt to infer module
449*e038c9c4Sjoerg   /// ownership from umbrella headers if we've not already done so.
450*e038c9c4Sjoerg   ArrayRef<KnownHeader>
451*e038c9c4Sjoerg   findResolvedModulesForHeader(const FileEntry *File) const;
4527330f729Sjoerg 
4537330f729Sjoerg   /// Resolve all lazy header directives for the specified file.
4547330f729Sjoerg   ///
4557330f729Sjoerg   /// This ensures that the HeaderFileInfo on HeaderSearch is up to date. This
4567330f729Sjoerg   /// is effectively internal, but is exposed so HeaderSearch can call it.
4577330f729Sjoerg   void resolveHeaderDirectives(const FileEntry *File) const;
4587330f729Sjoerg 
4597330f729Sjoerg   /// Resolve all lazy header directives for the specified module.
4607330f729Sjoerg   void resolveHeaderDirectives(Module *Mod) const;
4617330f729Sjoerg 
4627330f729Sjoerg   /// Reports errors if a module must not include a specific file.
4637330f729Sjoerg   ///
4647330f729Sjoerg   /// \param RequestingModule The module including a file.
4657330f729Sjoerg   ///
4667330f729Sjoerg   /// \param RequestingModuleIsModuleInterface \c true if the inclusion is in
4677330f729Sjoerg   ///        the interface of RequestingModule, \c false if it's in the
4687330f729Sjoerg   ///        implementation of RequestingModule. Value is ignored and
4697330f729Sjoerg   ///        meaningless if RequestingModule is nullptr.
4707330f729Sjoerg   ///
4717330f729Sjoerg   /// \param FilenameLoc The location of the inclusion's filename.
4727330f729Sjoerg   ///
4737330f729Sjoerg   /// \param Filename The included filename as written.
4747330f729Sjoerg   ///
4757330f729Sjoerg   /// \param File The included file.
4767330f729Sjoerg   void diagnoseHeaderInclusion(Module *RequestingModule,
4777330f729Sjoerg                                bool RequestingModuleIsModuleInterface,
4787330f729Sjoerg                                SourceLocation FilenameLoc, StringRef Filename,
4797330f729Sjoerg                                const FileEntry *File);
4807330f729Sjoerg 
4817330f729Sjoerg   /// Determine whether the given header is part of a module
4827330f729Sjoerg   /// marked 'unavailable'.
4837330f729Sjoerg   bool isHeaderInUnavailableModule(const FileEntry *Header) const;
4847330f729Sjoerg 
4857330f729Sjoerg   /// Determine whether the given header is unavailable as part
4867330f729Sjoerg   /// of the specified module.
4877330f729Sjoerg   bool isHeaderUnavailableInModule(const FileEntry *Header,
4887330f729Sjoerg                                    const Module *RequestingModule) const;
4897330f729Sjoerg 
4907330f729Sjoerg   /// Retrieve a module with the given name.
4917330f729Sjoerg   ///
4927330f729Sjoerg   /// \param Name The name of the module to look up.
4937330f729Sjoerg   ///
4947330f729Sjoerg   /// \returns The named module, if known; otherwise, returns null.
4957330f729Sjoerg   Module *findModule(StringRef Name) const;
4967330f729Sjoerg 
4977330f729Sjoerg   /// Retrieve a module with the given name using lexical name lookup,
4987330f729Sjoerg   /// starting at the given context.
4997330f729Sjoerg   ///
5007330f729Sjoerg   /// \param Name The name of the module to look up.
5017330f729Sjoerg   ///
5027330f729Sjoerg   /// \param Context The module context, from which we will perform lexical
5037330f729Sjoerg   /// name lookup.
5047330f729Sjoerg   ///
5057330f729Sjoerg   /// \returns The named module, if known; otherwise, returns null.
5067330f729Sjoerg   Module *lookupModuleUnqualified(StringRef Name, Module *Context) const;
5077330f729Sjoerg 
5087330f729Sjoerg   /// Retrieve a module with the given name within the given context,
5097330f729Sjoerg   /// using direct (qualified) name lookup.
5107330f729Sjoerg   ///
5117330f729Sjoerg   /// \param Name The name of the module to look up.
5127330f729Sjoerg   ///
5137330f729Sjoerg   /// \param Context The module for which we will look for a submodule. If
5147330f729Sjoerg   /// null, we will look for a top-level module.
5157330f729Sjoerg   ///
5167330f729Sjoerg   /// \returns The named submodule, if known; otherwose, returns null.
5177330f729Sjoerg   Module *lookupModuleQualified(StringRef Name, Module *Context) const;
5187330f729Sjoerg 
5197330f729Sjoerg   /// Find a new module or submodule, or create it if it does not already
5207330f729Sjoerg   /// exist.
5217330f729Sjoerg   ///
5227330f729Sjoerg   /// \param Name The name of the module to find or create.
5237330f729Sjoerg   ///
5247330f729Sjoerg   /// \param Parent The module that will act as the parent of this submodule,
5257330f729Sjoerg   /// or nullptr to indicate that this is a top-level module.
5267330f729Sjoerg   ///
5277330f729Sjoerg   /// \param IsFramework Whether this is a framework module.
5287330f729Sjoerg   ///
5297330f729Sjoerg   /// \param IsExplicit Whether this is an explicit submodule.
5307330f729Sjoerg   ///
5317330f729Sjoerg   /// \returns The found or newly-created module, along with a boolean value
5327330f729Sjoerg   /// that will be true if the module is newly-created.
5337330f729Sjoerg   std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
5347330f729Sjoerg                                                bool IsFramework,
5357330f729Sjoerg                                                bool IsExplicit);
5367330f729Sjoerg 
5377330f729Sjoerg   /// Create a global module fragment for a C++ module unit.
5387330f729Sjoerg   ///
5397330f729Sjoerg   /// We model the global module fragment as a submodule of the module
5407330f729Sjoerg   /// interface unit. Unfortunately, we can't create the module interface
5417330f729Sjoerg   /// unit's Module until later, because we don't know what it will be called.
5427330f729Sjoerg   Module *createGlobalModuleFragmentForModuleUnit(SourceLocation Loc);
5437330f729Sjoerg 
5447330f729Sjoerg   /// Create a global module fragment for a C++ module interface unit.
5457330f729Sjoerg   Module *createPrivateModuleFragmentForInterfaceUnit(Module *Parent,
5467330f729Sjoerg                                                       SourceLocation Loc);
5477330f729Sjoerg 
5487330f729Sjoerg   /// Create a new module for a C++ module interface unit.
5497330f729Sjoerg   /// The module must not already exist, and will be configured for the current
5507330f729Sjoerg   /// compilation.
5517330f729Sjoerg   ///
5527330f729Sjoerg   /// Note that this also sets the current module to the newly-created module.
5537330f729Sjoerg   ///
5547330f729Sjoerg   /// \returns The newly-created module.
5557330f729Sjoerg   Module *createModuleForInterfaceUnit(SourceLocation Loc, StringRef Name,
5567330f729Sjoerg                                        Module *GlobalModule);
5577330f729Sjoerg 
5587330f729Sjoerg   /// Create a header module from the specified list of headers.
5597330f729Sjoerg   Module *createHeaderModule(StringRef Name, ArrayRef<Module::Header> Headers);
5607330f729Sjoerg 
5617330f729Sjoerg   /// Infer the contents of a framework module map from the given
5627330f729Sjoerg   /// framework directory.
5637330f729Sjoerg   Module *inferFrameworkModule(const DirectoryEntry *FrameworkDir,
5647330f729Sjoerg                                bool IsSystem, Module *Parent);
5657330f729Sjoerg 
5667330f729Sjoerg   /// Create a new top-level module that is shadowed by
5677330f729Sjoerg   /// \p ShadowingModule.
5687330f729Sjoerg   Module *createShadowedModule(StringRef Name, bool IsFramework,
5697330f729Sjoerg                                Module *ShadowingModule);
5707330f729Sjoerg 
5717330f729Sjoerg   /// Creates a new declaration scope for module names, allowing
5727330f729Sjoerg   /// previously defined modules to shadow definitions from the new scope.
5737330f729Sjoerg   ///
5747330f729Sjoerg   /// \note Module names from earlier scopes will shadow names from the new
5757330f729Sjoerg   /// scope, which is the opposite of how shadowing works for variables.
finishModuleDeclarationScope()5767330f729Sjoerg   void finishModuleDeclarationScope() { CurrentModuleScopeID += 1; }
5777330f729Sjoerg 
mayShadowNewModule(Module * ExistingModule)5787330f729Sjoerg   bool mayShadowNewModule(Module *ExistingModule) {
5797330f729Sjoerg     assert(!ExistingModule->Parent && "expected top-level module");
5807330f729Sjoerg     assert(ModuleScopeIDs.count(ExistingModule) && "unknown module");
5817330f729Sjoerg     return ModuleScopeIDs[ExistingModule] < CurrentModuleScopeID;
5827330f729Sjoerg   }
5837330f729Sjoerg 
5847330f729Sjoerg   /// Retrieve the module map file containing the definition of the given
5857330f729Sjoerg   /// module.
5867330f729Sjoerg   ///
5877330f729Sjoerg   /// \param Module The module whose module map file will be returned, if known.
5887330f729Sjoerg   ///
5897330f729Sjoerg   /// \returns The file entry for the module map file containing the given
5907330f729Sjoerg   /// module, or nullptr if the module definition was inferred.
5917330f729Sjoerg   const FileEntry *getContainingModuleMapFile(const Module *Module) const;
5927330f729Sjoerg 
5937330f729Sjoerg   /// Get the module map file that (along with the module name) uniquely
5947330f729Sjoerg   /// identifies this module.
5957330f729Sjoerg   ///
5967330f729Sjoerg   /// The particular module that \c Name refers to may depend on how the module
5977330f729Sjoerg   /// was found in header search. However, the combination of \c Name and
5987330f729Sjoerg   /// this module map will be globally unique for top-level modules. In the case
5997330f729Sjoerg   /// of inferred modules, returns the module map that allowed the inference
6007330f729Sjoerg   /// (e.g. contained 'module *'). Otherwise, returns
6017330f729Sjoerg   /// getContainingModuleMapFile().
6027330f729Sjoerg   const FileEntry *getModuleMapFileForUniquing(const Module *M) const;
6037330f729Sjoerg 
6047330f729Sjoerg   void setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap);
6057330f729Sjoerg 
6067330f729Sjoerg   /// Get any module map files other than getModuleMapFileForUniquing(M)
6077330f729Sjoerg   /// that define submodules of a top-level module \p M. This is cheaper than
6087330f729Sjoerg   /// getting the module map file for each submodule individually, since the
6097330f729Sjoerg   /// expected number of results is very small.
getAdditionalModuleMapFiles(const Module * M)6107330f729Sjoerg   AdditionalModMapsSet *getAdditionalModuleMapFiles(const Module *M) {
6117330f729Sjoerg     auto I = AdditionalModMaps.find(M);
6127330f729Sjoerg     if (I == AdditionalModMaps.end())
6137330f729Sjoerg       return nullptr;
6147330f729Sjoerg     return &I->second;
6157330f729Sjoerg   }
6167330f729Sjoerg 
617*e038c9c4Sjoerg   void addAdditionalModuleMapFile(const Module *M, const FileEntry *ModuleMap);
6187330f729Sjoerg 
6197330f729Sjoerg   /// Resolve all of the unresolved exports in the given module.
6207330f729Sjoerg   ///
6217330f729Sjoerg   /// \param Mod The module whose exports should be resolved.
6227330f729Sjoerg   ///
6237330f729Sjoerg   /// \param Complain Whether to emit diagnostics for failures.
6247330f729Sjoerg   ///
6257330f729Sjoerg   /// \returns true if any errors were encountered while resolving exports,
6267330f729Sjoerg   /// false otherwise.
6277330f729Sjoerg   bool resolveExports(Module *Mod, bool Complain);
6287330f729Sjoerg 
6297330f729Sjoerg   /// Resolve all of the unresolved uses in the given module.
6307330f729Sjoerg   ///
6317330f729Sjoerg   /// \param Mod The module whose uses should be resolved.
6327330f729Sjoerg   ///
6337330f729Sjoerg   /// \param Complain Whether to emit diagnostics for failures.
6347330f729Sjoerg   ///
6357330f729Sjoerg   /// \returns true if any errors were encountered while resolving uses,
6367330f729Sjoerg   /// false otherwise.
6377330f729Sjoerg   bool resolveUses(Module *Mod, bool Complain);
6387330f729Sjoerg 
6397330f729Sjoerg   /// Resolve all of the unresolved conflicts in the given module.
6407330f729Sjoerg   ///
6417330f729Sjoerg   /// \param Mod The module whose conflicts should be resolved.
6427330f729Sjoerg   ///
6437330f729Sjoerg   /// \param Complain Whether to emit diagnostics for failures.
6447330f729Sjoerg   ///
6457330f729Sjoerg   /// \returns true if any errors were encountered while resolving conflicts,
6467330f729Sjoerg   /// false otherwise.
6477330f729Sjoerg   bool resolveConflicts(Module *Mod, bool Complain);
6487330f729Sjoerg 
6497330f729Sjoerg   /// Sets the umbrella header of the given module to the given
6507330f729Sjoerg   /// header.
6517330f729Sjoerg   void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
652*e038c9c4Sjoerg                          const Twine &NameAsWritten,
653*e038c9c4Sjoerg                          const Twine &PathRelativeToRootModuleDirectory);
6547330f729Sjoerg 
6557330f729Sjoerg   /// Sets the umbrella directory of the given module to the given
6567330f729Sjoerg   /// directory.
6577330f729Sjoerg   void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
658*e038c9c4Sjoerg                       const Twine &NameAsWritten,
659*e038c9c4Sjoerg                       const Twine &PathRelativeToRootModuleDirectory);
6607330f729Sjoerg 
6617330f729Sjoerg   /// Adds this header to the given module.
6627330f729Sjoerg   /// \param Role The role of the header wrt the module.
6637330f729Sjoerg   void addHeader(Module *Mod, Module::Header Header,
6647330f729Sjoerg                  ModuleHeaderRole Role, bool Imported = false);
6657330f729Sjoerg 
6667330f729Sjoerg   /// Marks this header as being excluded from the given module.
6677330f729Sjoerg   void excludeHeader(Module *Mod, Module::Header Header);
6687330f729Sjoerg 
6697330f729Sjoerg   /// Parse the given module map file, and record any modules we
6707330f729Sjoerg   /// encounter.
6717330f729Sjoerg   ///
6727330f729Sjoerg   /// \param File The file to be parsed.
6737330f729Sjoerg   ///
6747330f729Sjoerg   /// \param IsSystem Whether this module map file is in a system header
6757330f729Sjoerg   /// directory, and therefore should be considered a system module.
6767330f729Sjoerg   ///
6777330f729Sjoerg   /// \param HomeDir The directory in which relative paths within this module
6787330f729Sjoerg   ///        map file will be resolved.
6797330f729Sjoerg   ///
6807330f729Sjoerg   /// \param ID The FileID of the file to process, if we've already entered it.
6817330f729Sjoerg   ///
6827330f729Sjoerg   /// \param Offset [inout] On input the offset at which to start parsing. On
6837330f729Sjoerg   ///        output, the offset at which the module map terminated.
6847330f729Sjoerg   ///
6857330f729Sjoerg   /// \param ExternModuleLoc The location of the "extern module" declaration
6867330f729Sjoerg   ///        that caused us to load this module map file, if any.
6877330f729Sjoerg   ///
6887330f729Sjoerg   /// \returns true if an error occurred, false otherwise.
6897330f729Sjoerg   bool parseModuleMapFile(const FileEntry *File, bool IsSystem,
6907330f729Sjoerg                           const DirectoryEntry *HomeDir,
6917330f729Sjoerg                           FileID ID = FileID(), unsigned *Offset = nullptr,
6927330f729Sjoerg                           SourceLocation ExternModuleLoc = SourceLocation());
6937330f729Sjoerg 
6947330f729Sjoerg   /// Dump the contents of the module map, for debugging purposes.
6957330f729Sjoerg   void dump();
6967330f729Sjoerg 
6977330f729Sjoerg   using module_iterator = llvm::StringMap<Module *>::const_iterator;
6987330f729Sjoerg 
module_begin()6997330f729Sjoerg   module_iterator module_begin() const { return Modules.begin(); }
module_end()7007330f729Sjoerg   module_iterator module_end()   const { return Modules.end(); }
modules()701*e038c9c4Sjoerg   llvm::iterator_range<module_iterator> modules() const {
702*e038c9c4Sjoerg     return {module_begin(), module_end()};
703*e038c9c4Sjoerg   }
704*e038c9c4Sjoerg 
705*e038c9c4Sjoerg   /// Cache a module load.  M might be nullptr.
cacheModuleLoad(const IdentifierInfo & II,Module * M)706*e038c9c4Sjoerg   void cacheModuleLoad(const IdentifierInfo &II, Module *M) {
707*e038c9c4Sjoerg     CachedModuleLoads[&II] = M;
708*e038c9c4Sjoerg   }
709*e038c9c4Sjoerg 
710*e038c9c4Sjoerg   /// Return a cached module load.
getCachedModuleLoad(const IdentifierInfo & II)711*e038c9c4Sjoerg   llvm::Optional<Module *> getCachedModuleLoad(const IdentifierInfo &II) {
712*e038c9c4Sjoerg     auto I = CachedModuleLoads.find(&II);
713*e038c9c4Sjoerg     if (I == CachedModuleLoads.end())
714*e038c9c4Sjoerg       return None;
715*e038c9c4Sjoerg     return I->second;
716*e038c9c4Sjoerg   }
7177330f729Sjoerg };
7187330f729Sjoerg 
7197330f729Sjoerg } // namespace clang
7207330f729Sjoerg 
7217330f729Sjoerg #endif // LLVM_CLANG_LEX_MODULEMAP_H
722