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 // This file defines the Module class, which describes a module in the source
00011 // code.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 #ifndef LLVM_CLANG_BASIC_MODULE_H
00015 #define LLVM_CLANG_BASIC_MODULE_H
00016 
00017 #include "clang/Basic/SourceLocation.h"
00018 #include "llvm/ADT/PointerIntPair.h"
00019 #include "llvm/ADT/PointerUnion.h"
00020 #include "llvm/ADT/SmallVector.h"
00021 #include "llvm/ADT/StringMap.h"
00022 #include "llvm/ADT/StringRef.h"
00023 #include <string>
00024 #include <utility>
00025 #include <vector>
00026 
00027 namespace llvm {
00028   class raw_ostream;
00029 }
00030 
00031 namespace clang {
00032   
00033 class DirectoryEntry;
00034 class FileEntry;
00035 class LangOptions;
00036 class TargetInfo;
00037   
00038 /// \brief Describes the name of a module.
00039 typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
00040   ModuleId;
00041   
00042 /// \brief Describes a module or submodule.
00043 class Module {
00044 public:
00045   /// \brief The name of this module.
00046   std::string Name;
00047   
00048   /// \brief The location of the module definition.
00049   SourceLocation DefinitionLoc;
00050   
00051   /// \brief The parent of this module. This will be NULL for the top-level
00052   /// module.
00053   Module *Parent;
00054   
00055   /// \brief The umbrella header or directory.
00056   llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
00057   
00058 private:
00059   /// \brief The submodules of this module, indexed by name.
00060   std::vector<Module *> SubModules;
00061   
00062   /// \brief A mapping from the submodule name to the index into the 
00063   /// \c SubModules vector at which that submodule resides.
00064   llvm::StringMap<unsigned> SubModuleIndex;
00065   
00066 public:
00067   /// \brief The headers that are part of this module.
00068   llvm::SmallVector<const FileEntry *, 2> Headers;
00069 
00070   /// \brief The set of language features required to use this module.
00071   ///
00072   /// If any of these features is not present, the \c IsAvailable bit
00073   /// will be false to indicate that this (sub)module is not
00074   /// available.
00075   llvm::SmallVector<std::string, 2> Requires;
00076 
00077   /// \brief Whether this module is available in the current
00078   /// translation unit.
00079   unsigned IsAvailable : 1;
00080 
00081   /// \brief Whether this module was loaded from a module file.
00082   unsigned IsFromModuleFile : 1;
00083   
00084   /// \brief Whether this is a framework module.
00085   unsigned IsFramework : 1;
00086   
00087   /// \brief Whether this is an explicit submodule.
00088   unsigned IsExplicit : 1;
00089   
00090   /// \brief Whether this is a "system" module (which assumes that all
00091   /// headers in it are system headers).
00092   unsigned IsSystem : 1;
00093   
00094   /// \brief Whether we should infer submodules for this module based on 
00095   /// the headers.
00096   ///
00097   /// Submodules can only be inferred for modules with an umbrella header.
00098   unsigned InferSubmodules : 1;
00099   
00100   /// \brief Whether, when inferring submodules, the inferred submodules
00101   /// should be explicit.
00102   unsigned InferExplicitSubmodules : 1;
00103   
00104   /// \brief Whether, when inferring submodules, the inferr submodules should
00105   /// export all modules they import (e.g., the equivalent of "export *").
00106   unsigned InferExportWildcard : 1;
00107   
00108   /// \brief Describes the visibility of the various names within a
00109   /// particular module.
00110   enum NameVisibilityKind {
00111     /// \brief All of the names in this module are hidden.
00112     ///
00113     Hidden,
00114     /// \brief Only the macro names in this module are visible.
00115     MacrosVisible,
00116     /// \brief All of the names in this module are visible.
00117     AllVisible
00118   };  
00119   
00120   ///\ brief The visibility of names within this particular module.
00121   NameVisibilityKind NameVisibility;
00122 
00123   /// \brief The location of the inferred submodule.
00124   SourceLocation InferredSubmoduleLoc;
00125 
00126   /// \brief The set of modules imported by this module, and on which this
00127   /// module depends.
00128   llvm::SmallVector<Module *, 2> Imports;
00129   
00130   /// \brief Describes an exported module.
00131   ///
00132   /// The pointer is the module being re-exported, while the bit will be true
00133   /// to indicate that this is a wildcard export.
00134   typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
00135   
00136   /// \brief The set of export declarations.
00137   llvm::SmallVector<ExportDecl, 2> Exports;
00138   
00139   /// \brief Describes an exported module that has not yet been resolved
00140   /// (perhaps because tASThe module it refers to has not yet been loaded).
00141   struct UnresolvedExportDecl {
00142     /// \brief The location of the 'export' keyword in the module map file.
00143     SourceLocation ExportLoc;
00144     
00145     /// \brief The name of the module.
00146     ModuleId Id;
00147     
00148     /// \brief Whether this export declaration ends in a wildcard, indicating
00149     /// that all of its submodules should be exported (rather than the named
00150     /// module itself).
00151     bool Wildcard;
00152   };
00153   
00154   /// \brief The set of export declarations that have yet to be resolved.
00155   llvm::SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
00156   
00157   /// \brief Construct a top-level module.
00158   explicit Module(StringRef Name, SourceLocation DefinitionLoc,
00159                   bool IsFramework)
00160     : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), Umbrella(),
00161       IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework), 
00162       IsExplicit(false), IsSystem(false),
00163       InferSubmodules(false), InferExplicitSubmodules(false),
00164       InferExportWildcard(false), NameVisibility(Hidden) { }
00165   
00166   /// \brief Construct a new module or submodule.
00167   Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 
00168          bool IsFramework, bool IsExplicit);
00169   
00170   ~Module();
00171   
00172   /// \brief Determine whether this module is available for use within the
00173   /// current translation unit.
00174   bool isAvailable() const { return IsAvailable; }
00175 
00176   /// \brief Determine whether this module is available for use within the
00177   /// current translation unit.
00178   ///
00179   /// \param LangOpts The language options used for the current
00180   /// translation unit.
00181   ///
00182   /// \param Target The target options used for the current translation unit.
00183   ///
00184   /// \param Feature If this module is unavailable, this parameter
00185   /// will be set to one of the features that is required for use of
00186   /// this module (but is not available).
00187   bool isAvailable(const LangOptions &LangOpts, 
00188                    const TargetInfo &Target,
00189                    StringRef &Feature) const;
00190 
00191   /// \brief Determine whether this module is a submodule.
00192   bool isSubModule() const { return Parent != 0; }
00193   
00194   /// \brief Determine whether this module is a submodule of the given other
00195   /// module.
00196   bool isSubModuleOf(Module *Other) const;
00197   
00198   /// \brief Determine whether this module is a part of a framework,
00199   /// either because it is a framework module or because it is a submodule
00200   /// of a framework module.
00201   bool isPartOfFramework() const {
00202     for (const Module *Mod = this; Mod; Mod = Mod->Parent) 
00203       if (Mod->IsFramework)
00204         return true;
00205     
00206     return false;
00207   }
00208   
00209   /// \brief Retrieve the full name of this module, including the path from
00210   /// its top-level module.
00211   std::string getFullModuleName() const;
00212 
00213   /// \brief Retrieve the top-level module for this (sub)module, which may
00214   /// be this module.
00215   Module *getTopLevelModule() {
00216     return const_cast<Module *>(
00217              const_cast<const Module *>(this)->getTopLevelModule());
00218   }
00219 
00220   /// \brief Retrieve the top-level module for this (sub)module, which may
00221   /// be this module.
00222   const Module *getTopLevelModule() const;
00223   
00224   /// \brief Retrieve the name of the top-level module.
00225   ///
00226   StringRef getTopLevelModuleName() const {
00227     return getTopLevelModule()->Name;
00228   }
00229   
00230   /// \brief Retrieve the directory for which this module serves as the
00231   /// umbrella.
00232   const DirectoryEntry *getUmbrellaDir() const;
00233 
00234   /// \brief Retrieve the header that serves as the umbrella header for this
00235   /// module.
00236   const FileEntry *getUmbrellaHeader() const {
00237     return Umbrella.dyn_cast<const FileEntry *>();
00238   }
00239 
00240   /// \brief Determine whether this module has an umbrella directory that is
00241   /// not based on an umbrella header.
00242   bool hasUmbrellaDir() const {
00243     return Umbrella && Umbrella.is<const DirectoryEntry *>();
00244   }
00245 
00246   /// \briaf Add the given feature requirement to the list of features
00247   /// required by this module.
00248   ///
00249   /// \param Feature The feature that is required by this module (and
00250   /// its submodules).
00251   ///
00252   /// \param LangOpts The set of language options that will be used to
00253   /// evaluate the availability of this feature.
00254   ///
00255   /// \param Target The target options that will be used to evaluate the
00256   /// availability of this feature.
00257   void addRequirement(StringRef Feature, const LangOptions &LangOpts,
00258                       const TargetInfo &Target);
00259 
00260   /// \brief Find the submodule with the given name.
00261   ///
00262   /// \returns The submodule if found, or NULL otherwise.
00263   Module *findSubmodule(StringRef Name) const;
00264   
00265   typedef std::vector<Module *>::iterator submodule_iterator;
00266   typedef std::vector<Module *>::const_iterator submodule_const_iterator;
00267   
00268   submodule_iterator submodule_begin() { return SubModules.begin(); }
00269   submodule_const_iterator submodule_begin() const {return SubModules.begin();}
00270   submodule_iterator submodule_end()   { return SubModules.end(); }
00271   submodule_const_iterator submodule_end() const { return SubModules.end(); }
00272   
00273   /// \brief Print the module map for this module to the given stream. 
00274   ///
00275   void print(llvm::raw_ostream &OS, unsigned Indent = 0) const;
00276   
00277   /// \brief Dump the contents of this module to the given output stream.
00278   void dump() const;
00279 };
00280 
00281 } // end namespace clang
00282 
00283 
00284 #endif // LLVM_CLANG_BASIC_MODULE_H