Home | History | Annotate | Download | only in Lex
      1 //===--- ModuleMap.h - Describe the layout of modules -----------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the ModuleMap interface, which describes the layout of a
     11 // module as it relates to headers.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 
     16 #ifndef LLVM_CLANG_LEX_MODULEMAP_H
     17 #define LLVM_CLANG_LEX_MODULEMAP_H
     18 
     19 #include "clang/Basic/LangOptions.h"
     20 #include "clang/Basic/Module.h"
     21 #include "clang/Basic/SourceManager.h"
     22 #include "llvm/ADT/DenseMap.h"
     23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
     24 #include "llvm/ADT/SmallVector.h"
     25 #include "llvm/ADT/StringMap.h"
     26 #include "llvm/ADT/StringRef.h"
     27 #include <string>
     28 
     29 namespace clang {
     30 
     31 class DirectoryEntry;
     32 class FileEntry;
     33 class FileManager;
     34 class DiagnosticConsumer;
     35 class DiagnosticsEngine;
     36 class HeaderSearch;
     37 class ModuleMapParser;
     38 
     39 class ModuleMap {
     40   SourceManager *SourceMgr;
     41   IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
     42   const LangOptions &LangOpts;
     43   const TargetInfo *Target;
     44   HeaderSearch &HeaderInfo;
     45 
     46   /// \brief The directory used for Clang-supplied, builtin include headers,
     47   /// such as "stdint.h".
     48   const DirectoryEntry *BuiltinIncludeDir;
     49 
     50   /// \brief Language options used to parse the module map itself.
     51   ///
     52   /// These are always simple C language options.
     53   LangOptions MMapLangOpts;
     54 
     55   /// \brief The top-level modules that are known.
     56   llvm::StringMap<Module *> Modules;
     57 
     58   /// \brief A header that is known to reside within a given module,
     59   /// whether it was included or excluded.
     60   class KnownHeader {
     61     llvm::PointerIntPair<Module *, 1, bool> Storage;
     62 
     63   public:
     64     KnownHeader() : Storage(0, false) { }
     65     KnownHeader(Module *M, bool Excluded) : Storage(M, Excluded) { }
     66 
     67     /// \brief Retrieve the module the header is stored in.
     68     Module *getModule() const { return Storage.getPointer(); }
     69 
     70     /// \brief Whether this header is explicitly excluded from the module.
     71     bool isExcluded() const { return Storage.getInt(); }
     72 
     73     /// \brief Whether this header is available in the module.
     74     bool isAvailable() const {
     75       return !isExcluded() && getModule()->isAvailable();
     76     }
     77 
     78     // \brief Whether this known header is valid (i.e., it has an
     79     // associated module).
     80     operator bool() const { return Storage.getPointer() != 0; }
     81   };
     82 
     83   typedef llvm::DenseMap<const FileEntry *, KnownHeader> HeadersMap;
     84 
     85   /// \brief Mapping from each header to the module that owns the contents of the
     86   /// that header.
     87   HeadersMap Headers;
     88 
     89   /// \brief Mapping from directories with umbrella headers to the module
     90   /// that is generated from the umbrella header.
     91   ///
     92   /// This mapping is used to map headers that haven't explicitly been named
     93   /// in the module map over to the module that includes them via its umbrella
     94   /// header.
     95   llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
     96 
     97   /// \brief A directory for which framework modules can be inferred.
     98   struct InferredDirectory {
     99     InferredDirectory() : InferModules(), InferSystemModules() { }
    100 
    101     /// \brief Whether to infer modules from this directory.
    102     unsigned InferModules : 1;
    103 
    104     /// \brief Whether the modules we infer are [system] modules.
    105     unsigned InferSystemModules : 1;
    106 
    107     /// \brief The names of modules that cannot be inferred within this
    108     /// directory.
    109     SmallVector<std::string, 2> ExcludedModules;
    110   };
    111 
    112   /// \brief A mapping from directories to information about inferring
    113   /// framework modules from within those directories.
    114   llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
    115 
    116   /// \brief Describes whether we haved parsed a particular file as a module
    117   /// map.
    118   llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap;
    119 
    120   friend class ModuleMapParser;
    121 
    122   /// \brief Resolve the given export declaration into an actual export
    123   /// declaration.
    124   ///
    125   /// \param Mod The module in which we're resolving the export declaration.
    126   ///
    127   /// \param Unresolved The export declaration to resolve.
    128   ///
    129   /// \param Complain Whether this routine should complain about unresolvable
    130   /// exports.
    131   ///
    132   /// \returns The resolved export declaration, which will have a NULL pointer
    133   /// if the export could not be resolved.
    134   Module::ExportDecl
    135   resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
    136                 bool Complain) const;
    137 
    138 public:
    139   /// \brief Construct a new module map.
    140   ///
    141   /// \param FileMgr The file manager used to find module files and headers.
    142   /// This file manager should be shared with the header-search mechanism, since
    143   /// they will refer to the same headers.
    144   ///
    145   /// \param DC A diagnostic consumer that will be cloned for use in generating
    146   /// diagnostics.
    147   ///
    148   /// \param LangOpts Language options for this translation unit.
    149   ///
    150   /// \param Target The target for this translation unit.
    151   ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
    152             const LangOptions &LangOpts, const TargetInfo *Target,
    153             HeaderSearch &HeaderInfo);
    154 
    155   /// \brief Destroy the module map.
    156   ///
    157   ~ModuleMap();
    158 
    159   /// \brief Set the target information.
    160   void setTarget(const TargetInfo &Target);
    161 
    162   /// \brief Set the directory that contains Clang-supplied include
    163   /// files, such as our stdarg.h or tgmath.h.
    164   void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
    165     BuiltinIncludeDir = Dir;
    166   }
    167 
    168   /// \brief Retrieve the module that owns the given header file, if any.
    169   ///
    170   /// \param File The header file that is likely to be included.
    171   ///
    172   /// \returns The module that owns the given header file, or null to indicate
    173   /// that no module owns this header file.
    174   Module *findModuleForHeader(const FileEntry *File);
    175 
    176   /// \brief Determine whether the given header is part of a module
    177   /// marked 'unavailable'.
    178   bool isHeaderInUnavailableModule(const FileEntry *Header) const;
    179 
    180   /// \brief Retrieve a module with the given name.
    181   ///
    182   /// \param Name The name of the module to look up.
    183   ///
    184   /// \returns The named module, if known; otherwise, returns null.
    185   Module *findModule(StringRef Name) const;
    186 
    187   /// \brief Retrieve a module with the given name using lexical name lookup,
    188   /// starting at the given context.
    189   ///
    190   /// \param Name The name of the module to look up.
    191   ///
    192   /// \param Context The module context, from which we will perform lexical
    193   /// name lookup.
    194   ///
    195   /// \returns The named module, if known; otherwise, returns null.
    196   Module *lookupModuleUnqualified(StringRef Name, Module *Context) const;
    197 
    198   /// \brief Retrieve a module with the given name within the given context,
    199   /// using direct (qualified) name lookup.
    200   ///
    201   /// \param Name The name of the module to look up.
    202   ///
    203   /// \param Context The module for which we will look for a submodule. If
    204   /// null, we will look for a top-level module.
    205   ///
    206   /// \returns The named submodule, if known; otherwose, returns null.
    207   Module *lookupModuleQualified(StringRef Name, Module *Context) const;
    208 
    209   /// \brief Find a new module or submodule, or create it if it does not already
    210   /// exist.
    211   ///
    212   /// \param Name The name of the module to find or create.
    213   ///
    214   /// \param Parent The module that will act as the parent of this submodule,
    215   /// or NULL to indicate that this is a top-level module.
    216   ///
    217   /// \param IsFramework Whether this is a framework module.
    218   ///
    219   /// \param IsExplicit Whether this is an explicit submodule.
    220   ///
    221   /// \returns The found or newly-created module, along with a boolean value
    222   /// that will be true if the module is newly-created.
    223   std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
    224                                                bool IsFramework,
    225                                                bool IsExplicit);
    226 
    227   /// \brief Determine whether we can infer a framework module a framework
    228   /// with the given name in the given
    229   ///
    230   /// \param ParentDir The directory that is the parent of the framework
    231   /// directory.
    232   ///
    233   /// \param Name The name of the module.
    234   ///
    235   /// \param IsSystem Will be set to 'true' if the inferred module must be a
    236   /// system module.
    237   ///
    238   /// \returns true if we are allowed to infer a framework module, and false
    239   /// otherwise.
    240   bool canInferFrameworkModule(const DirectoryEntry *ParentDir,
    241                                StringRef Name, bool &IsSystem) const;
    242 
    243   /// \brief Infer the contents of a framework module map from the given
    244   /// framework directory.
    245   Module *inferFrameworkModule(StringRef ModuleName,
    246                                const DirectoryEntry *FrameworkDir,
    247                                bool IsSystem, Module *Parent);
    248 
    249   /// \brief Retrieve the module map file containing the definition of the given
    250   /// module.
    251   ///
    252   /// \param Module The module whose module map file will be returned, if known.
    253   ///
    254   /// \returns The file entry for the module map file containing the given
    255   /// module, or NULL if the module definition was inferred.
    256   const FileEntry *getContainingModuleMapFile(Module *Module) const;
    257 
    258   /// \brief Resolve all of the unresolved exports in the given module.
    259   ///
    260   /// \param Mod The module whose exports should be resolved.
    261   ///
    262   /// \param Complain Whether to emit diagnostics for failures.
    263   ///
    264   /// \returns true if any errors were encountered while resolving exports,
    265   /// false otherwise.
    266   bool resolveExports(Module *Mod, bool Complain);
    267 
    268   /// \brief Infers the (sub)module based on the given source location and
    269   /// source manager.
    270   ///
    271   /// \param Loc The location within the source that we are querying, along
    272   /// with its source manager.
    273   ///
    274   /// \returns The module that owns this source location, or null if no
    275   /// module owns this source location.
    276   Module *inferModuleFromLocation(FullSourceLoc Loc);
    277 
    278   /// \brief Sets the umbrella header of the given module to the given
    279   /// header.
    280   void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader);
    281 
    282   /// \brief Sets the umbrella directory of the given module to the given
    283   /// directory.
    284   void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir);
    285 
    286   /// \brief Adds this header to the given module.
    287   /// \param Excluded Whether this header is explicitly excluded from the
    288   /// module; otherwise, it's included in the module.
    289   void addHeader(Module *Mod, const FileEntry *Header, bool Excluded);
    290 
    291   /// \brief Parse the given module map file, and record any modules we
    292   /// encounter.
    293   ///
    294   /// \param File The file to be parsed.
    295   ///
    296   /// \returns true if an error occurred, false otherwise.
    297   bool parseModuleMapFile(const FileEntry *File);
    298 
    299   /// \brief Dump the contents of the module map, for debugging purposes.
    300   void dump();
    301 
    302   typedef llvm::StringMap<Module *>::const_iterator module_iterator;
    303   module_iterator module_begin() const { return Modules.begin(); }
    304   module_iterator module_end()   const { return Modules.end(); }
    305 };
    306 
    307 }
    308 #endif
    309