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/StringRef.h"
     26 #include "llvm/ADT/StringMap.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 ModuleMapParser;
     37 
     38 class ModuleMap {
     39   SourceManager *SourceMgr;
     40   IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
     41   const LangOptions &LangOpts;
     42   const TargetInfo *Target;
     43 
     44   /// \brief The directory used for Clang-supplied, builtin include headers,
     45   /// such as "stdint.h".
     46   const DirectoryEntry *BuiltinIncludeDir;
     47 
     48   /// \brief Language options used to parse the module map itself.
     49   ///
     50   /// These are always simple C language options.
     51   LangOptions MMapLangOpts;
     52 
     53   /// \brief The top-level modules that are known.
     54   llvm::StringMap<Module *> Modules;
     55 
     56   /// \brief Mapping from each header to the module that owns the contents of the
     57   /// that header.
     58   llvm::DenseMap<const FileEntry *, Module *> Headers;
     59 
     60   /// \brief Mapping from directories with umbrella headers to the module
     61   /// that is generated from the umbrella header.
     62   ///
     63   /// This mapping is used to map headers that haven't explicitly been named
     64   /// in the module map over to the module that includes them via its umbrella
     65   /// header.
     66   llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
     67 
     68   friend class ModuleMapParser;
     69 
     70   /// \brief Resolve the given export declaration into an actual export
     71   /// declaration.
     72   ///
     73   /// \param Mod The module in which we're resolving the export declaration.
     74   ///
     75   /// \param Unresolved The export declaration to resolve.
     76   ///
     77   /// \param Complain Whether this routine should complain about unresolvable
     78   /// exports.
     79   ///
     80   /// \returns The resolved export declaration, which will have a NULL pointer
     81   /// if the export could not be resolved.
     82   Module::ExportDecl
     83   resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
     84                 bool Complain);
     85 
     86 public:
     87   /// \brief Construct a new module map.
     88   ///
     89   /// \param FileMgr The file manager used to find module files and headers.
     90   /// This file manager should be shared with the header-search mechanism, since
     91   /// they will refer to the same headers.
     92   ///
     93   /// \param DC A diagnostic consumer that will be cloned for use in generating
     94   /// diagnostics.
     95   ///
     96   /// \param LangOpts Language options for this translation unit.
     97   ///
     98   /// \param Target The target for this translation unit.
     99   ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
    100             const LangOptions &LangOpts, const TargetInfo *Target);
    101 
    102   /// \brief Destroy the module map.
    103   ///
    104   ~ModuleMap();
    105 
    106   /// \brief Set the target information.
    107   void setTarget(const TargetInfo &Target);
    108 
    109   /// \brief Set the directory that contains Clang-supplied include
    110   /// files, such as our stdarg.h or tgmath.h.
    111   void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
    112     BuiltinIncludeDir = Dir;
    113   }
    114 
    115   /// \brief Retrieve the module that owns the given header file, if any.
    116   ///
    117   /// \param File The header file that is likely to be included.
    118   ///
    119   /// \returns The module that owns the given header file, or null to indicate
    120   /// that no module owns this header file.
    121   Module *findModuleForHeader(const FileEntry *File);
    122 
    123   /// \brief Determine whether the given header is part of a module
    124   /// marked 'unavailable'.
    125   bool isHeaderInUnavailableModule(const FileEntry *Header);
    126 
    127   /// \brief Retrieve a module with the given name.
    128   ///
    129   /// \param The name of the module to look up.
    130   ///
    131   /// \returns The named module, if known; otherwise, returns null.
    132   Module *findModule(StringRef Name);
    133 
    134   /// \brief Retrieve a module with the given name using lexical name lookup,
    135   /// starting at the given context.
    136   ///
    137   /// \param The name of the module to look up.
    138   ///
    139   /// \param Context The module context, from which we will perform lexical
    140   /// name lookup.
    141   ///
    142   /// \returns The named module, if known; otherwise, returns null.
    143   Module *lookupModuleUnqualified(StringRef Name, Module *Context);
    144 
    145   /// \brief Retrieve a module with the given name within the given context,
    146   /// using direct (qualified) name lookup.
    147   ///
    148   /// \param The name of the module to look up.
    149   ///
    150   /// \param Context The module for which we will look for a submodule. If
    151   /// null, we will look for a top-level module.
    152   ///
    153   /// \returns The named submodule, if known; otherwose, returns null.
    154   Module *lookupModuleQualified(StringRef Name, Module *Context);
    155 
    156   /// \brief Find a new module or submodule, or create it if it does not already
    157   /// exist.
    158   ///
    159   /// \param Name The name of the module to find or create.
    160   ///
    161   /// \param Parent The module that will act as the parent of this submodule,
    162   /// or NULL to indicate that this is a top-level module.
    163   ///
    164   /// \param IsFramework Whether this is a framework module.
    165   ///
    166   /// \param IsExplicit Whether this is an explicit submodule.
    167   ///
    168   /// \returns The found or newly-created module, along with a boolean value
    169   /// that will be true if the module is newly-created.
    170   std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
    171                                                bool IsFramework,
    172                                                bool IsExplicit);
    173 
    174   /// \brief Infer the contents of a framework module map from the given
    175   /// framework directory.
    176   Module *inferFrameworkModule(StringRef ModuleName,
    177                                const DirectoryEntry *FrameworkDir,
    178                                bool IsSystem, Module *Parent);
    179 
    180   /// \brief Retrieve the module map file containing the definition of the given
    181   /// module.
    182   ///
    183   /// \param Module The module whose module map file will be returned, if known.
    184   ///
    185   /// \returns The file entry for the module map file containing the given
    186   /// module, or NULL if the module definition was inferred.
    187   const FileEntry *getContainingModuleMapFile(Module *Module);
    188 
    189   /// \brief Resolve all of the unresolved exports in the given module.
    190   ///
    191   /// \param Mod The module whose exports should be resolved.
    192   ///
    193   /// \param Complain Whether to emit diagnostics for failures.
    194   ///
    195   /// \returns true if any errors were encountered while resolving exports,
    196   /// false otherwise.
    197   bool resolveExports(Module *Mod, bool Complain);
    198 
    199   /// \brief Infers the (sub)module based on the given source location and
    200   /// source manager.
    201   ///
    202   /// \param Loc The location within the source that we are querying, along
    203   /// with its source manager.
    204   ///
    205   /// \returns The module that owns this source location, or null if no
    206   /// module owns this source location.
    207   Module *inferModuleFromLocation(FullSourceLoc Loc);
    208 
    209   /// \brief Sets the umbrella header of the given module to the given
    210   /// header.
    211   void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader);
    212 
    213   /// \brief Sets the umbrella directory of the given module to the given
    214   /// directory.
    215   void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir);
    216 
    217   /// \brief Adds this header to the given module.
    218   void addHeader(Module *Mod, const FileEntry *Header);
    219 
    220   /// \brief Parse the given module map file, and record any modules we
    221   /// encounter.
    222   ///
    223   /// \param File The file to be parsed.
    224   ///
    225   /// \returns true if an error occurred, false otherwise.
    226   bool parseModuleMapFile(const FileEntry *File);
    227 
    228   /// \brief Dump the contents of the module map, for debugging purposes.
    229   void dump();
    230 
    231   typedef llvm::StringMap<Module *>::const_iterator module_iterator;
    232   module_iterator module_begin() const { return Modules.begin(); }
    233   module_iterator module_end()   const { return Modules.end(); }
    234 };
    235 
    236 }
    237 #endif
    238