clang API Documentation

Basic/Module.h
Go to the documentation of this file.
00001 //===--- Module.h - Describe a module ---------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief Defines the clang::Module class, which describes a module in the
00012 /// source code.
00013 ///
00014 //===----------------------------------------------------------------------===//
00015 #ifndef LLVM_CLANG_BASIC_MODULE_H
00016 #define LLVM_CLANG_BASIC_MODULE_H
00017 
00018 #include "clang/Basic/SourceLocation.h"
00019 #include "llvm/ADT/DenseSet.h"
00020 #include "llvm/ADT/PointerIntPair.h"
00021 #include "llvm/ADT/PointerUnion.h"
00022 #include "llvm/ADT/SetVector.h"
00023 #include "llvm/ADT/SmallVector.h"
00024 #include "llvm/ADT/StringMap.h"
00025 #include "llvm/ADT/StringRef.h"
00026 #include <string>
00027 #include <utility>
00028 #include <vector>
00029 
00030 namespace llvm {
00031   class raw_ostream;
00032 }
00033 
00034 namespace clang {
00035   
00036 class DirectoryEntry;
00037 class FileEntry;
00038 class FileManager;
00039 class LangOptions;
00040 class TargetInfo;
00041 class IdentifierInfo;
00042   
00043 /// \brief Describes the name of a module.
00044 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
00045   
00046 /// \brief Describes a module or submodule.
00047 class Module {
00048 public:
00049   /// \brief The name of this module.
00050   std::string Name;
00051   
00052   /// \brief The location of the module definition.
00053   SourceLocation DefinitionLoc;
00054 
00055   /// \brief The parent of this module. This will be NULL for the top-level
00056   /// module.
00057   Module *Parent;
00058   
00059   /// \brief The umbrella header or directory.
00060   llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
00061   
00062 private:
00063   /// \brief The submodules of this module, indexed by name.
00064   std::vector<Module *> SubModules;
00065   
00066   /// \brief A mapping from the submodule name to the index into the 
00067   /// \c SubModules vector at which that submodule resides.
00068   llvm::StringMap<unsigned> SubModuleIndex;
00069 
00070   /// \brief The AST file if this is a top-level module which has a
00071   /// corresponding serialized AST file, or null otherwise.
00072   const FileEntry *ASTFile;
00073 
00074   /// \brief The top-level headers associated with this module.
00075   llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
00076 
00077   /// \brief top-level header filenames that aren't resolved to FileEntries yet.
00078   std::vector<std::string> TopHeaderNames;
00079 
00080   /// \brief Cache of modules visible to lookup in this module.
00081   mutable llvm::DenseSet<const Module*> VisibleModulesCache;
00082 
00083 public:
00084   /// \brief The headers that are part of this module.
00085   SmallVector<const FileEntry *, 2> NormalHeaders;
00086 
00087   /// \brief The headers that are logically part of this module but
00088   /// must be textually included.
00089   SmallVector<const FileEntry *, 2> TextualHeaders;
00090 
00091   /// \brief The headers that are private to this module.
00092   SmallVector<const FileEntry *, 2> PrivateHeaders;
00093 
00094   /// \brief The headers that are private to this module and are to be
00095   /// included textually.
00096   SmallVector<const FileEntry *, 2> PrivateTextualHeaders;
00097 
00098   /// \brief The headers that are explicitly excluded from this module.
00099   SmallVector<const FileEntry *, 2> ExcludedHeaders;
00100 
00101   /// \brief Information about a header directive as found in the module map
00102   /// file.
00103   struct HeaderDirective {
00104     SourceLocation FileNameLoc;
00105     std::string FileName;
00106     bool IsUmbrella;
00107   };
00108 
00109   /// \brief Headers that are mentioned in the module map file but could not be
00110   /// found on the file system.
00111   SmallVector<HeaderDirective, 1> MissingHeaders;
00112 
00113   /// \brief An individual requirement: a feature name and a flag indicating
00114   /// the required state of that feature.
00115   typedef std::pair<std::string, bool> Requirement;
00116 
00117   /// \brief The set of language features required to use this module.
00118   ///
00119   /// If any of these requirements are not available, the \c IsAvailable bit
00120   /// will be false to indicate that this (sub)module is not available.
00121   SmallVector<Requirement, 2> Requirements;
00122 
00123   /// \brief Whether this module is missing a feature from \c Requirements.
00124   unsigned IsMissingRequirement : 1;
00125 
00126   /// \brief Whether this module is available in the current translation unit.
00127   ///
00128   /// If the module is missing headers or does not meet all requirements then
00129   /// this bit will be 0.
00130   unsigned IsAvailable : 1;
00131 
00132   /// \brief Whether this module was loaded from a module file.
00133   unsigned IsFromModuleFile : 1;
00134   
00135   /// \brief Whether this is a framework module.
00136   unsigned IsFramework : 1;
00137   
00138   /// \brief Whether this is an explicit submodule.
00139   unsigned IsExplicit : 1;
00140   
00141   /// \brief Whether this is a "system" module (which assumes that all
00142   /// headers in it are system headers).
00143   unsigned IsSystem : 1;
00144 
00145   /// \brief Whether this is an 'extern "C"' module (which implicitly puts all
00146   /// headers in it within an 'extern "C"' block, and allows the module to be
00147   /// imported within such a block).
00148   unsigned IsExternC : 1;
00149 
00150   /// \brief Whether this is an inferred submodule (module * { ... }).
00151   unsigned IsInferred : 1;
00152 
00153   /// \brief Whether we should infer submodules for this module based on 
00154   /// the headers.
00155   ///
00156   /// Submodules can only be inferred for modules with an umbrella header.
00157   unsigned InferSubmodules : 1;
00158   
00159   /// \brief Whether, when inferring submodules, the inferred submodules
00160   /// should be explicit.
00161   unsigned InferExplicitSubmodules : 1;
00162   
00163   /// \brief Whether, when inferring submodules, the inferr submodules should
00164   /// export all modules they import (e.g., the equivalent of "export *").
00165   unsigned InferExportWildcard : 1;
00166 
00167   /// \brief Whether the set of configuration macros is exhaustive.
00168   ///
00169   /// When the set of configuration macros is exhaustive, meaning
00170   /// that no identifier not in this list should affect how the module is
00171   /// built.
00172   unsigned ConfigMacrosExhaustive : 1;
00173 
00174   /// \brief Describes the visibility of the various names within a
00175   /// particular module.
00176   enum NameVisibilityKind {
00177     /// \brief All of the names in this module are hidden.
00178     ///
00179     Hidden,
00180     /// \brief Only the macro names in this module are visible.
00181     MacrosVisible,
00182     /// \brief All of the names in this module are visible.
00183     AllVisible
00184   };
00185 
00186   /// \brief The visibility of names within this particular module.
00187   NameVisibilityKind NameVisibility;
00188 
00189   /// \brief The location at which macros within this module became visible.
00190   SourceLocation MacroVisibilityLoc;
00191 
00192   /// \brief The location of the inferred submodule.
00193   SourceLocation InferredSubmoduleLoc;
00194 
00195   /// \brief The set of modules imported by this module, and on which this
00196   /// module depends.
00197   SmallVector<Module *, 2> Imports;
00198   
00199   /// \brief Describes an exported module.
00200   ///
00201   /// The pointer is the module being re-exported, while the bit will be true
00202   /// to indicate that this is a wildcard export.
00203   typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
00204   
00205   /// \brief The set of export declarations.
00206   SmallVector<ExportDecl, 2> Exports;
00207   
00208   /// \brief Describes an exported module that has not yet been resolved
00209   /// (perhaps because the module it refers to has not yet been loaded).
00210   struct UnresolvedExportDecl {
00211     /// \brief The location of the 'export' keyword in the module map file.
00212     SourceLocation ExportLoc;
00213     
00214     /// \brief The name of the module.
00215     ModuleId Id;
00216     
00217     /// \brief Whether this export declaration ends in a wildcard, indicating
00218     /// that all of its submodules should be exported (rather than the named
00219     /// module itself).
00220     bool Wildcard;
00221   };
00222   
00223   /// \brief The set of export declarations that have yet to be resolved.
00224   SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
00225 
00226   /// \brief The directly used modules.
00227   SmallVector<Module *, 2> DirectUses;
00228 
00229   /// \brief The set of use declarations that have yet to be resolved.
00230   SmallVector<ModuleId, 2> UnresolvedDirectUses;
00231 
00232   /// \brief A library or framework to link against when an entity from this
00233   /// module is used.
00234   struct LinkLibrary {
00235     LinkLibrary() : IsFramework(false) { }
00236     LinkLibrary(const std::string &Library, bool IsFramework)
00237       : Library(Library), IsFramework(IsFramework) { }
00238     
00239     /// \brief The library to link against.
00240     ///
00241     /// This will typically be a library or framework name, but can also
00242     /// be an absolute path to the library or framework.
00243     std::string Library;
00244 
00245     /// \brief Whether this is a framework rather than a library.
00246     bool IsFramework;
00247   };
00248 
00249   /// \brief The set of libraries or frameworks to link against when
00250   /// an entity from this module is used.
00251   llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
00252 
00253   /// \brief The set of "configuration macros", which are macros that
00254   /// (intentionally) change how this module is built.
00255   std::vector<std::string> ConfigMacros;
00256 
00257   /// \brief An unresolved conflict with another module.
00258   struct UnresolvedConflict {
00259     /// \brief The (unresolved) module id.
00260     ModuleId Id;
00261 
00262     /// \brief The message provided to the user when there is a conflict.
00263     std::string Message;
00264   };
00265 
00266   /// \brief The list of conflicts for which the module-id has not yet been
00267   /// resolved.
00268   std::vector<UnresolvedConflict> UnresolvedConflicts;
00269 
00270   /// \brief A conflict between two modules.
00271   struct Conflict {
00272     /// \brief The module that this module conflicts with.
00273     Module *Other;
00274 
00275     /// \brief The message provided to the user when there is a conflict.
00276     std::string Message;
00277   };
00278 
00279   /// \brief The list of conflicts.
00280   std::vector<Conflict> Conflicts;
00281 
00282   /// \brief Construct a new module or submodule.
00283   Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
00284          bool IsFramework, bool IsExplicit);
00285   
00286   ~Module();
00287   
00288   /// \brief Determine whether this module is available for use within the
00289   /// current translation unit.
00290   bool isAvailable() const { return IsAvailable; }
00291 
00292   /// \brief Determine whether this module is available for use within the
00293   /// current translation unit.
00294   ///
00295   /// \param LangOpts The language options used for the current
00296   /// translation unit.
00297   ///
00298   /// \param Target The target options used for the current translation unit.
00299   ///
00300   /// \param Req If this module is unavailable, this parameter
00301   /// will be set to one of the requirements that is not met for use of
00302   /// this module.
00303   bool isAvailable(const LangOptions &LangOpts, 
00304                    const TargetInfo &Target,
00305                    Requirement &Req,
00306                    HeaderDirective &MissingHeader) const;
00307 
00308   /// \brief Determine whether this module is a submodule.
00309   bool isSubModule() const { return Parent != nullptr; }
00310   
00311   /// \brief Determine whether this module is a submodule of the given other
00312   /// module.
00313   bool isSubModuleOf(const Module *Other) const;
00314   
00315   /// \brief Determine whether this module is a part of a framework,
00316   /// either because it is a framework module or because it is a submodule
00317   /// of a framework module.
00318   bool isPartOfFramework() const {
00319     for (const Module *Mod = this; Mod; Mod = Mod->Parent) 
00320       if (Mod->IsFramework)
00321         return true;
00322     
00323     return false;
00324   }
00325 
00326   /// \brief Determine whether this module is a subframework of another
00327   /// framework.
00328   bool isSubFramework() const {
00329     return IsFramework && Parent && Parent->isPartOfFramework();
00330   }
00331 
00332   /// \brief Retrieve the full name of this module, including the path from
00333   /// its top-level module.
00334   std::string getFullModuleName() const;
00335 
00336   /// \brief Retrieve the top-level module for this (sub)module, which may
00337   /// be this module.
00338   Module *getTopLevelModule() {
00339     return const_cast<Module *>(
00340              const_cast<const Module *>(this)->getTopLevelModule());
00341   }
00342 
00343   /// \brief Retrieve the top-level module for this (sub)module, which may
00344   /// be this module.
00345   const Module *getTopLevelModule() const;
00346   
00347   /// \brief Retrieve the name of the top-level module.
00348   ///
00349   StringRef getTopLevelModuleName() const {
00350     return getTopLevelModule()->Name;
00351   }
00352 
00353   /// \brief The serialized AST file for this module, if one was created.
00354   const FileEntry *getASTFile() const {
00355     return getTopLevelModule()->ASTFile;
00356   }
00357 
00358   /// \brief Set the serialized AST file for the top-level module of this module.
00359   void setASTFile(const FileEntry *File) {
00360     assert((File == nullptr || getASTFile() == nullptr ||
00361             getASTFile() == File) && "file path changed");
00362     getTopLevelModule()->ASTFile = File;
00363   }
00364 
00365   /// \brief Retrieve the directory for which this module serves as the
00366   /// umbrella.
00367   const DirectoryEntry *getUmbrellaDir() const;
00368 
00369   /// \brief Retrieve the header that serves as the umbrella header for this
00370   /// module.
00371   const FileEntry *getUmbrellaHeader() const {
00372     return Umbrella.dyn_cast<const FileEntry *>();
00373   }
00374 
00375   /// \brief Determine whether this module has an umbrella directory that is
00376   /// not based on an umbrella header.
00377   bool hasUmbrellaDir() const {
00378     return Umbrella && Umbrella.is<const DirectoryEntry *>();
00379   }
00380 
00381   /// \brief Add a top-level header associated with this module.
00382   void addTopHeader(const FileEntry *File) {
00383     assert(File);
00384     TopHeaders.insert(File);
00385   }
00386 
00387   /// \brief Add a top-level header filename associated with this module.
00388   void addTopHeaderFilename(StringRef Filename) {
00389     TopHeaderNames.push_back(Filename);
00390   }
00391 
00392   /// \brief The top-level headers associated with this module.
00393   ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
00394 
00395   /// \brief Add the given feature requirement to the list of features
00396   /// required by this module.
00397   ///
00398   /// \param Feature The feature that is required by this module (and
00399   /// its submodules).
00400   ///
00401   /// \param RequiredState The required state of this feature: \c true
00402   /// if it must be present, \c false if it must be absent.
00403   ///
00404   /// \param LangOpts The set of language options that will be used to
00405   /// evaluate the availability of this feature.
00406   ///
00407   /// \param Target The target options that will be used to evaluate the
00408   /// availability of this feature.
00409   void addRequirement(StringRef Feature, bool RequiredState,
00410                       const LangOptions &LangOpts,
00411                       const TargetInfo &Target);
00412 
00413   /// \brief Mark this module and all of its submodules as unavailable.
00414   void markUnavailable(bool MissingRequirement = false);
00415 
00416   /// \brief Find the submodule with the given name.
00417   ///
00418   /// \returns The submodule if found, or NULL otherwise.
00419   Module *findSubmodule(StringRef Name) const;
00420 
00421   /// \brief Determine whether the specified module would be visible to
00422   /// a lookup at the end of this module.
00423   ///
00424   /// FIXME: This may return incorrect results for (submodules of) the
00425   /// module currently being built, if it's queried before we see all
00426   /// of its imports.
00427   bool isModuleVisible(const Module *M) const {
00428     if (VisibleModulesCache.empty())
00429       buildVisibleModulesCache();
00430     return VisibleModulesCache.count(M);
00431   }
00432 
00433   typedef std::vector<Module *>::iterator submodule_iterator;
00434   typedef std::vector<Module *>::const_iterator submodule_const_iterator;
00435   
00436   submodule_iterator submodule_begin() { return SubModules.begin(); }
00437   submodule_const_iterator submodule_begin() const {return SubModules.begin();}
00438   submodule_iterator submodule_end()   { return SubModules.end(); }
00439   submodule_const_iterator submodule_end() const { return SubModules.end(); }
00440 
00441   /// \brief Appends this module's list of exported modules to \p Exported.
00442   ///
00443   /// This provides a subset of immediately imported modules (the ones that are
00444   /// directly exported), not the complete set of exported modules.
00445   void getExportedModules(SmallVectorImpl<Module *> &Exported) const;
00446 
00447   static StringRef getModuleInputBufferName() {
00448     return "<module-includes>";
00449   }
00450 
00451   /// \brief Print the module map for this module to the given stream. 
00452   ///
00453   void print(raw_ostream &OS, unsigned Indent = 0) const;
00454   
00455   /// \brief Dump the contents of this module to the given output stream.
00456   void dump() const;
00457 
00458 private:
00459   void buildVisibleModulesCache() const;
00460 };
00461 
00462 } // end namespace clang
00463 
00464 
00465 #endif // LLVM_CLANG_BASIC_MODULE_H