Home | History | Annotate | Download | only in Basic
      1 //===--- Module.h - Describe a module ---------------------------*- 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 /// \file
     11 /// \brief Defines the clang::Module class, which describes a module in the
     12 /// source code.
     13 ///
     14 //===----------------------------------------------------------------------===//
     15 #ifndef LLVM_CLANG_BASIC_MODULE_H
     16 #define LLVM_CLANG_BASIC_MODULE_H
     17 
     18 #include "clang/Basic/SourceLocation.h"
     19 #include "llvm/ADT/DenseSet.h"
     20 #include "llvm/ADT/PointerIntPair.h"
     21 #include "llvm/ADT/PointerUnion.h"
     22 #include "llvm/ADT/SetVector.h"
     23 #include "llvm/ADT/SmallVector.h"
     24 #include "llvm/ADT/StringMap.h"
     25 #include "llvm/ADT/StringRef.h"
     26 #include <string>
     27 #include <utility>
     28 #include <vector>
     29 
     30 namespace llvm {
     31   class raw_ostream;
     32 }
     33 
     34 namespace clang {
     35 
     36 class DirectoryEntry;
     37 class FileEntry;
     38 class FileManager;
     39 class LangOptions;
     40 class TargetInfo;
     41 class IdentifierInfo;
     42 
     43 /// \brief Describes the name of a module.
     44 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
     45 
     46 /// \brief Describes a module or submodule.
     47 class Module {
     48 public:
     49   /// \brief The name of this module.
     50   std::string Name;
     51 
     52   /// \brief The location of the module definition.
     53   SourceLocation DefinitionLoc;
     54 
     55   /// \brief The parent of this module. This will be NULL for the top-level
     56   /// module.
     57   Module *Parent;
     58 
     59   /// \brief The build directory of this module. This is the directory in
     60   /// which the module is notionally built, and relative to which its headers
     61   /// are found.
     62   const DirectoryEntry *Directory;
     63 
     64   /// \brief The umbrella header or directory.
     65   llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
     66 
     67 private:
     68   /// \brief The submodules of this module, indexed by name.
     69   std::vector<Module *> SubModules;
     70 
     71   /// \brief A mapping from the submodule name to the index into the
     72   /// \c SubModules vector at which that submodule resides.
     73   llvm::StringMap<unsigned> SubModuleIndex;
     74 
     75   /// \brief The AST file if this is a top-level module which has a
     76   /// corresponding serialized AST file, or null otherwise.
     77   const FileEntry *ASTFile;
     78 
     79   /// \brief The top-level headers associated with this module.
     80   llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
     81 
     82   /// \brief top-level header filenames that aren't resolved to FileEntries yet.
     83   std::vector<std::string> TopHeaderNames;
     84 
     85   /// \brief Cache of modules visible to lookup in this module.
     86   mutable llvm::DenseSet<const Module*> VisibleModulesCache;
     87 
     88 public:
     89   enum HeaderKind {
     90     HK_Normal,
     91     HK_Textual,
     92     HK_Private,
     93     HK_PrivateTextual,
     94     HK_Excluded
     95   };
     96   static const int NumHeaderKinds = HK_Excluded + 1;
     97 
     98   /// \brief Information about a header directive as found in the module map
     99   /// file.
    100   struct Header {
    101     std::string NameAsWritten;
    102     const FileEntry *Entry;
    103   };
    104 
    105   /// \brief The headers that are part of this module.
    106   SmallVector<Header, 2> Headers[5];
    107 
    108   /// \brief Stored information about a header directive that was found in the
    109   /// module map file but has not been resolved to a file.
    110   struct UnresolvedHeaderDirective {
    111     SourceLocation FileNameLoc;
    112     std::string FileName;
    113     bool IsUmbrella;
    114   };
    115 
    116   /// \brief Headers that are mentioned in the module map file but could not be
    117   /// found on the file system.
    118   SmallVector<UnresolvedHeaderDirective, 1> MissingHeaders;
    119 
    120   /// \brief An individual requirement: a feature name and a flag indicating
    121   /// the required state of that feature.
    122   typedef std::pair<std::string, bool> Requirement;
    123 
    124   /// \brief The set of language features required to use this module.
    125   ///
    126   /// If any of these requirements are not available, the \c IsAvailable bit
    127   /// will be false to indicate that this (sub)module is not available.
    128   SmallVector<Requirement, 2> Requirements;
    129 
    130   /// \brief Whether this module is missing a feature from \c Requirements.
    131   unsigned IsMissingRequirement : 1;
    132 
    133   /// \brief Whether this module is available in the current translation unit.
    134   ///
    135   /// If the module is missing headers or does not meet all requirements then
    136   /// this bit will be 0.
    137   unsigned IsAvailable : 1;
    138 
    139   /// \brief Whether this module was loaded from a module file.
    140   unsigned IsFromModuleFile : 1;
    141 
    142   /// \brief Whether this is a framework module.
    143   unsigned IsFramework : 1;
    144 
    145   /// \brief Whether this is an explicit submodule.
    146   unsigned IsExplicit : 1;
    147 
    148   /// \brief Whether this is a "system" module (which assumes that all
    149   /// headers in it are system headers).
    150   unsigned IsSystem : 1;
    151 
    152   /// \brief Whether this is an 'extern "C"' module (which implicitly puts all
    153   /// headers in it within an 'extern "C"' block, and allows the module to be
    154   /// imported within such a block).
    155   unsigned IsExternC : 1;
    156 
    157   /// \brief Whether this is an inferred submodule (module * { ... }).
    158   unsigned IsInferred : 1;
    159 
    160   /// \brief Whether we should infer submodules for this module based on
    161   /// the headers.
    162   ///
    163   /// Submodules can only be inferred for modules with an umbrella header.
    164   unsigned InferSubmodules : 1;
    165 
    166   /// \brief Whether, when inferring submodules, the inferred submodules
    167   /// should be explicit.
    168   unsigned InferExplicitSubmodules : 1;
    169 
    170   /// \brief Whether, when inferring submodules, the inferr submodules should
    171   /// export all modules they import (e.g., the equivalent of "export *").
    172   unsigned InferExportWildcard : 1;
    173 
    174   /// \brief Whether the set of configuration macros is exhaustive.
    175   ///
    176   /// When the set of configuration macros is exhaustive, meaning
    177   /// that no identifier not in this list should affect how the module is
    178   /// built.
    179   unsigned ConfigMacrosExhaustive : 1;
    180 
    181   /// \brief Describes the visibility of the various names within a
    182   /// particular module.
    183   enum NameVisibilityKind {
    184     /// \brief All of the names in this module are hidden.
    185     ///
    186     Hidden,
    187     /// \brief Only the macro names in this module are visible.
    188     MacrosVisible,
    189     /// \brief All of the names in this module are visible.
    190     AllVisible
    191   };
    192 
    193   /// \brief The visibility of names within this particular module.
    194   NameVisibilityKind NameVisibility;
    195 
    196   /// \brief The location at which macros within this module became visible.
    197   SourceLocation MacroVisibilityLoc;
    198 
    199   /// \brief The location of the inferred submodule.
    200   SourceLocation InferredSubmoduleLoc;
    201 
    202   /// \brief The set of modules imported by this module, and on which this
    203   /// module depends.
    204   SmallVector<Module *, 2> Imports;
    205 
    206   /// \brief Describes an exported module.
    207   ///
    208   /// The pointer is the module being re-exported, while the bit will be true
    209   /// to indicate that this is a wildcard export.
    210   typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
    211 
    212   /// \brief The set of export declarations.
    213   SmallVector<ExportDecl, 2> Exports;
    214 
    215   /// \brief Describes an exported module that has not yet been resolved
    216   /// (perhaps because the module it refers to has not yet been loaded).
    217   struct UnresolvedExportDecl {
    218     /// \brief The location of the 'export' keyword in the module map file.
    219     SourceLocation ExportLoc;
    220 
    221     /// \brief The name of the module.
    222     ModuleId Id;
    223 
    224     /// \brief Whether this export declaration ends in a wildcard, indicating
    225     /// that all of its submodules should be exported (rather than the named
    226     /// module itself).
    227     bool Wildcard;
    228   };
    229 
    230   /// \brief The set of export declarations that have yet to be resolved.
    231   SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
    232 
    233   /// \brief The directly used modules.
    234   SmallVector<Module *, 2> DirectUses;
    235 
    236   /// \brief The set of use declarations that have yet to be resolved.
    237   SmallVector<ModuleId, 2> UnresolvedDirectUses;
    238 
    239   /// \brief A library or framework to link against when an entity from this
    240   /// module is used.
    241   struct LinkLibrary {
    242     LinkLibrary() : IsFramework(false) { }
    243     LinkLibrary(const std::string &Library, bool IsFramework)
    244       : Library(Library), IsFramework(IsFramework) { }
    245 
    246     /// \brief The library to link against.
    247     ///
    248     /// This will typically be a library or framework name, but can also
    249     /// be an absolute path to the library or framework.
    250     std::string Library;
    251 
    252     /// \brief Whether this is a framework rather than a library.
    253     bool IsFramework;
    254   };
    255 
    256   /// \brief The set of libraries or frameworks to link against when
    257   /// an entity from this module is used.
    258   llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
    259 
    260   /// \brief The set of "configuration macros", which are macros that
    261   /// (intentionally) change how this module is built.
    262   std::vector<std::string> ConfigMacros;
    263 
    264   /// \brief An unresolved conflict with another module.
    265   struct UnresolvedConflict {
    266     /// \brief The (unresolved) module id.
    267     ModuleId Id;
    268 
    269     /// \brief The message provided to the user when there is a conflict.
    270     std::string Message;
    271   };
    272 
    273   /// \brief The list of conflicts for which the module-id has not yet been
    274   /// resolved.
    275   std::vector<UnresolvedConflict> UnresolvedConflicts;
    276 
    277   /// \brief A conflict between two modules.
    278   struct Conflict {
    279     /// \brief The module that this module conflicts with.
    280     Module *Other;
    281 
    282     /// \brief The message provided to the user when there is a conflict.
    283     std::string Message;
    284   };
    285 
    286   /// \brief The list of conflicts.
    287   std::vector<Conflict> Conflicts;
    288 
    289   /// \brief Construct a new module or submodule.
    290   Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
    291          bool IsFramework, bool IsExplicit);
    292 
    293   ~Module();
    294 
    295   /// \brief Determine whether this module is available for use within the
    296   /// current translation unit.
    297   bool isAvailable() const { return IsAvailable; }
    298 
    299   /// \brief Determine whether this module is available for use within the
    300   /// current translation unit.
    301   ///
    302   /// \param LangOpts The language options used for the current
    303   /// translation unit.
    304   ///
    305   /// \param Target The target options used for the current translation unit.
    306   ///
    307   /// \param Req If this module is unavailable, this parameter
    308   /// will be set to one of the requirements that is not met for use of
    309   /// this module.
    310   bool isAvailable(const LangOptions &LangOpts,
    311                    const TargetInfo &Target,
    312                    Requirement &Req,
    313                    UnresolvedHeaderDirective &MissingHeader) const;
    314 
    315   /// \brief Determine whether this module is a submodule.
    316   bool isSubModule() const { return Parent != nullptr; }
    317 
    318   /// \brief Determine whether this module is a submodule of the given other
    319   /// module.
    320   bool isSubModuleOf(const Module *Other) const;
    321 
    322   /// \brief Determine whether this module is a part of a framework,
    323   /// either because it is a framework module or because it is a submodule
    324   /// of a framework module.
    325   bool isPartOfFramework() const {
    326     for (const Module *Mod = this; Mod; Mod = Mod->Parent)
    327       if (Mod->IsFramework)
    328         return true;
    329 
    330     return false;
    331   }
    332 
    333   /// \brief Determine whether this module is a subframework of another
    334   /// framework.
    335   bool isSubFramework() const {
    336     return IsFramework && Parent && Parent->isPartOfFramework();
    337   }
    338 
    339   /// \brief Retrieve the full name of this module, including the path from
    340   /// its top-level module.
    341   std::string getFullModuleName() const;
    342 
    343   /// \brief Retrieve the top-level module for this (sub)module, which may
    344   /// be this module.
    345   Module *getTopLevelModule() {
    346     return const_cast<Module *>(
    347              const_cast<const Module *>(this)->getTopLevelModule());
    348   }
    349 
    350   /// \brief Retrieve the top-level module for this (sub)module, which may
    351   /// be this module.
    352   const Module *getTopLevelModule() const;
    353 
    354   /// \brief Retrieve the name of the top-level module.
    355   ///
    356   StringRef getTopLevelModuleName() const {
    357     return getTopLevelModule()->Name;
    358   }
    359 
    360   /// \brief The serialized AST file for this module, if one was created.
    361   const FileEntry *getASTFile() const {
    362     return getTopLevelModule()->ASTFile;
    363   }
    364 
    365   /// \brief Set the serialized AST file for the top-level module of this module.
    366   void setASTFile(const FileEntry *File) {
    367     assert((File == nullptr || getASTFile() == nullptr ||
    368             getASTFile() == File) && "file path changed");
    369     getTopLevelModule()->ASTFile = File;
    370   }
    371 
    372   /// \brief Retrieve the directory for which this module serves as the
    373   /// umbrella.
    374   const DirectoryEntry *getUmbrellaDir() const;
    375 
    376   /// \brief Retrieve the header that serves as the umbrella header for this
    377   /// module.
    378   const FileEntry *getUmbrellaHeader() const {
    379     return Umbrella.dyn_cast<const FileEntry *>();
    380   }
    381 
    382   /// \brief Determine whether this module has an umbrella directory that is
    383   /// not based on an umbrella header.
    384   bool hasUmbrellaDir() const {
    385     return Umbrella && Umbrella.is<const DirectoryEntry *>();
    386   }
    387 
    388   /// \brief Add a top-level header associated with this module.
    389   void addTopHeader(const FileEntry *File) {
    390     assert(File);
    391     TopHeaders.insert(File);
    392   }
    393 
    394   /// \brief Add a top-level header filename associated with this module.
    395   void addTopHeaderFilename(StringRef Filename) {
    396     TopHeaderNames.push_back(Filename);
    397   }
    398 
    399   /// \brief The top-level headers associated with this module.
    400   ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
    401 
    402   /// \brief Determine whether this module has declared its intention to
    403   /// directly use another module.
    404   bool directlyUses(const Module *Requested) const;
    405 
    406   /// \brief Add the given feature requirement to the list of features
    407   /// required by this module.
    408   ///
    409   /// \param Feature The feature that is required by this module (and
    410   /// its submodules).
    411   ///
    412   /// \param RequiredState The required state of this feature: \c true
    413   /// if it must be present, \c false if it must be absent.
    414   ///
    415   /// \param LangOpts The set of language options that will be used to
    416   /// evaluate the availability of this feature.
    417   ///
    418   /// \param Target The target options that will be used to evaluate the
    419   /// availability of this feature.
    420   void addRequirement(StringRef Feature, bool RequiredState,
    421                       const LangOptions &LangOpts,
    422                       const TargetInfo &Target);
    423 
    424   /// \brief Mark this module and all of its submodules as unavailable.
    425   void markUnavailable(bool MissingRequirement = false);
    426 
    427   /// \brief Find the submodule with the given name.
    428   ///
    429   /// \returns The submodule if found, or NULL otherwise.
    430   Module *findSubmodule(StringRef Name) const;
    431 
    432   /// \brief Determine whether the specified module would be visible to
    433   /// a lookup at the end of this module.
    434   ///
    435   /// FIXME: This may return incorrect results for (submodules of) the
    436   /// module currently being built, if it's queried before we see all
    437   /// of its imports.
    438   bool isModuleVisible(const Module *M) const {
    439     if (VisibleModulesCache.empty())
    440       buildVisibleModulesCache();
    441     return VisibleModulesCache.count(M);
    442   }
    443 
    444   typedef std::vector<Module *>::iterator submodule_iterator;
    445   typedef std::vector<Module *>::const_iterator submodule_const_iterator;
    446 
    447   submodule_iterator submodule_begin() { return SubModules.begin(); }
    448   submodule_const_iterator submodule_begin() const {return SubModules.begin();}
    449   submodule_iterator submodule_end()   { return SubModules.end(); }
    450   submodule_const_iterator submodule_end() const { return SubModules.end(); }
    451 
    452   /// \brief Appends this module's list of exported modules to \p Exported.
    453   ///
    454   /// This provides a subset of immediately imported modules (the ones that are
    455   /// directly exported), not the complete set of exported modules.
    456   void getExportedModules(SmallVectorImpl<Module *> &Exported) const;
    457 
    458   static StringRef getModuleInputBufferName() {
    459     return "<module-includes>";
    460   }
    461 
    462   /// \brief Print the module map for this module to the given stream.
    463   ///
    464   void print(raw_ostream &OS, unsigned Indent = 0) const;
    465 
    466   /// \brief Dump the contents of this module to the given output stream.
    467   void dump() const;
    468 
    469 private:
    470   void buildVisibleModulesCache() const;
    471 };
    472 
    473 } // end namespace clang
    474 
    475 
    476 #endif // LLVM_CLANG_BASIC_MODULE_H
    477