clang API Documentation

Serialization/Module.h
Go to the documentation of this file.
00001 //===--- Module.h - Module description --------------------------*- 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 that has
00011 //  been loaded from an AST file.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_SERIALIZATION_MODULE_H
00016 #define LLVM_CLANG_SERIALIZATION_MODULE_H
00017 
00018 #include "clang/Serialization/ASTBitCodes.h"
00019 #include "clang/Serialization/ContinuousRangeMap.h"
00020 #include "clang/Basic/SourceLocation.h"
00021 #include "llvm/ADT/OwningPtr.h"
00022 #include "llvm/ADT/SetVector.h"
00023 #include "llvm/Bitcode/BitstreamReader.h"
00024 #include <string>
00025 
00026 namespace clang {
00027 
00028 class DeclContext;
00029 class Module;
00030 template<typename Info> class OnDiskChainedHashTable;
00031 
00032 namespace serialization {
00033 
00034 namespace reader {
00035   class ASTDeclContextNameLookupTrait;
00036 }
00037 
00038 /// \brief Specifies the kind of module that has been loaded.
00039 enum ModuleKind {
00040   MK_Module,   ///< File is a module proper.
00041   MK_PCH,      ///< File is a PCH file treated as such.
00042   MK_Preamble, ///< File is a PCH file treated as the preamble.
00043   MK_MainFile  ///< File is a PCH file treated as the actual main file.
00044 };
00045 
00046 /// \brief Information about the contents of a DeclContext.
00047 struct DeclContextInfo {
00048   DeclContextInfo()
00049     : NameLookupTableData(), LexicalDecls(), NumLexicalDecls() {}
00050 
00051   OnDiskChainedHashTable<reader::ASTDeclContextNameLookupTrait>
00052     *NameLookupTableData; // an ASTDeclContextNameLookupTable.
00053   const KindDeclIDPair *LexicalDecls;
00054   unsigned NumLexicalDecls;
00055 };
00056 
00057 /// \brief Information about a module that has been loaded by the ASTReader.
00058 ///
00059 /// Each instance of the Module class corresponds to a single AST file, which
00060 /// may be a precompiled header, precompiled preamble, a module, or an AST file
00061 /// of some sort loaded as the main file, all of which are specific formulations
00062 /// of the general notion of a "module". A module may depend on any number of
00063 /// other modules.
00064 class ModuleFile {
00065 public:
00066   ModuleFile(ModuleKind Kind, unsigned Generation);
00067   ~ModuleFile();
00068 
00069   // === General information ===
00070 
00071   /// \brief The type of this module.
00072   ModuleKind Kind;
00073 
00074   /// \brief The file name of the module file.
00075   std::string FileName;
00076 
00077   /// \brief Whether this module has been directly imported by the
00078   /// user.
00079   bool DirectlyImported;
00080 
00081   /// \brief The generation of which this module file is a part.
00082   unsigned Generation;
00083   
00084   /// \brief The memory buffer that stores the data associated with
00085   /// this AST file.
00086   OwningPtr<llvm::MemoryBuffer> Buffer;
00087 
00088   /// \brief The size of this file, in bits.
00089   uint64_t SizeInBits;
00090 
00091   /// \brief The global bit offset (or base) of this module
00092   uint64_t GlobalBitOffset;
00093 
00094   /// \brief The bitstream reader from which we'll read the AST file.
00095   llvm::BitstreamReader StreamFile;
00096 
00097   /// \brief The main bitstream cursor for the main block.
00098   llvm::BitstreamCursor Stream;
00099 
00100   /// \brief The source location where this module was first imported.
00101   SourceLocation ImportLoc;
00102 
00103   /// \brief The first source location in this module.
00104   SourceLocation FirstLoc;
00105 
00106   // === Source Locations ===
00107 
00108   /// \brief Cursor used to read source location entries.
00109   llvm::BitstreamCursor SLocEntryCursor;
00110 
00111   /// \brief The number of source location entries in this AST file.
00112   unsigned LocalNumSLocEntries;
00113 
00114   /// \brief The base ID in the source manager's view of this module.
00115   int SLocEntryBaseID;
00116 
00117   /// \brief The base offset in the source manager's view of this module.
00118   unsigned SLocEntryBaseOffset;
00119 
00120   /// \brief Offsets for all of the source location entries in the
00121   /// AST file.
00122   const uint32_t *SLocEntryOffsets;
00123 
00124   /// \brief SLocEntries that we're going to preload.
00125   SmallVector<uint64_t, 4> PreloadSLocEntries;
00126 
00127   /// \brief The number of source location file entries in this AST file.
00128   unsigned LocalNumSLocFileEntries;
00129 
00130   /// \brief Offsets for all of the source location file entries in the
00131   /// AST file.
00132   const uint32_t *SLocFileOffsets;
00133 
00134   /// \brief Remapping table for source locations in this module.
00135   ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
00136 
00137   // === Identifiers ===
00138 
00139   /// \brief The number of identifiers in this AST file.
00140   unsigned LocalNumIdentifiers;
00141 
00142   /// \brief Offsets into the identifier table data.
00143   ///
00144   /// This array is indexed by the identifier ID (-1), and provides
00145   /// the offset into IdentifierTableData where the string data is
00146   /// stored.
00147   const uint32_t *IdentifierOffsets;
00148 
00149   /// \brief Base identifier ID for identifiers local to this module.
00150   serialization::IdentID BaseIdentifierID;
00151 
00152   /// \brief Remapping table for identifier IDs in this module.
00153   ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
00154 
00155   /// \brief Actual data for the on-disk hash table of identifiers.
00156   ///
00157   /// This pointer points into a memory buffer, where the on-disk hash
00158   /// table for identifiers actually lives.
00159   const char *IdentifierTableData;
00160 
00161   /// \brief A pointer to an on-disk hash table of opaque type
00162   /// IdentifierHashTable.
00163   void *IdentifierLookupTable;
00164 
00165   // === Macros ===
00166 
00167   /// \brief The cursor to the start of the preprocessor block, which stores
00168   /// all of the macro definitions.
00169   llvm::BitstreamCursor MacroCursor;
00170 
00171   /// \brief The offset of the start of the set of defined macros.
00172   uint64_t MacroStartOffset;
00173 
00174   // === Detailed PreprocessingRecord ===
00175 
00176   /// \brief The cursor to the start of the (optional) detailed preprocessing
00177   /// record block.
00178   llvm::BitstreamCursor PreprocessorDetailCursor;
00179 
00180   /// \brief The offset of the start of the preprocessor detail cursor.
00181   uint64_t PreprocessorDetailStartOffset;
00182 
00183   /// \brief Base preprocessed entity ID for preprocessed entities local to
00184   /// this module.
00185   serialization::PreprocessedEntityID BasePreprocessedEntityID;
00186 
00187   /// \brief Remapping table for preprocessed entity IDs in this module.
00188   ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
00189 
00190   const PPEntityOffset *PreprocessedEntityOffsets;
00191   unsigned NumPreprocessedEntities;
00192 
00193   // === Header search information ===
00194 
00195   /// \brief The number of local HeaderFileInfo structures.
00196   unsigned LocalNumHeaderFileInfos;
00197 
00198   /// \brief Actual data for the on-disk hash table of header file
00199   /// information.
00200   ///
00201   /// This pointer points into a memory buffer, where the on-disk hash
00202   /// table for header file information actually lives.
00203   const char *HeaderFileInfoTableData;
00204 
00205   /// \brief The on-disk hash table that contains information about each of
00206   /// the header files.
00207   void *HeaderFileInfoTable;
00208 
00209   /// \brief Actual data for the list of framework names used in the header
00210   /// search information.
00211   const char *HeaderFileFrameworkStrings;
00212 
00213   // === Submodule information ===  
00214   /// \brief The number of submodules in this module.
00215   unsigned LocalNumSubmodules;
00216   
00217   /// \brief Base submodule ID for submodules local to this module.
00218   serialization::SubmoduleID BaseSubmoduleID;
00219   
00220   /// \brief Remapping table for submodule IDs in this module.
00221   ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
00222   
00223   // === Selectors ===
00224 
00225   /// \brief The number of selectors new to this file.
00226   ///
00227   /// This is the number of entries in SelectorOffsets.
00228   unsigned LocalNumSelectors;
00229 
00230   /// \brief Offsets into the selector lookup table's data array
00231   /// where each selector resides.
00232   const uint32_t *SelectorOffsets;
00233 
00234   /// \brief Base selector ID for selectors local to this module.
00235   serialization::SelectorID BaseSelectorID;
00236 
00237   /// \brief Remapping table for selector IDs in this module.
00238   ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
00239 
00240   /// \brief A pointer to the character data that comprises the selector table
00241   ///
00242   /// The SelectorOffsets table refers into this memory.
00243   const unsigned char *SelectorLookupTableData;
00244 
00245   /// \brief A pointer to an on-disk hash table of opaque type
00246   /// ASTSelectorLookupTable.
00247   ///
00248   /// This hash table provides the IDs of all selectors, and the associated
00249   /// instance and factory methods.
00250   void *SelectorLookupTable;
00251 
00252   // === Declarations ===
00253 
00254   /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
00255   /// has read all the abbreviations at the start of the block and is ready to
00256   /// jump around with these in context.
00257   llvm::BitstreamCursor DeclsCursor;
00258 
00259   /// \brief The number of declarations in this AST file.
00260   unsigned LocalNumDecls;
00261 
00262   /// \brief Offset of each declaration within the bitstream, indexed
00263   /// by the declaration ID (-1).
00264   const DeclOffset *DeclOffsets;
00265 
00266   /// \brief Base declaration ID for declarations local to this module.
00267   serialization::DeclID BaseDeclID;
00268 
00269   /// \brief Remapping table for declaration IDs in this module.
00270   ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
00271 
00272   /// \brief Mapping from the module files that this module file depends on
00273   /// to the base declaration ID for that module as it is understood within this
00274   /// module.
00275   ///
00276   /// This is effectively a reverse global-to-local mapping for declaration
00277   /// IDs, so that we can interpret a true global ID (for this translation unit)
00278   /// as a local ID (for this module file).
00279   llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs;
00280 
00281   /// \brief The number of C++ base specifier sets in this AST file.
00282   unsigned LocalNumCXXBaseSpecifiers;
00283 
00284   /// \brief Offset of each C++ base specifier set within the bitstream,
00285   /// indexed by the C++ base specifier set ID (-1).
00286   const uint32_t *CXXBaseSpecifiersOffsets;
00287 
00288   typedef llvm::DenseMap<const DeclContext *, DeclContextInfo>
00289   DeclContextInfosMap;
00290 
00291   /// \brief Information about the lexical and visible declarations
00292   /// for each DeclContext.
00293   DeclContextInfosMap DeclContextInfos;
00294 
00295   /// \brief Array of file-level DeclIDs sorted by file.
00296   const serialization::DeclID *FileSortedDecls;
00297 
00298   /// \brief Array of redeclaration chain location information within this 
00299   /// module file, sorted by the first declaration ID.
00300   const serialization::LocalRedeclarationsInfo *RedeclarationsMap;
00301 
00302   /// \brief The number of redeclaration info entries in RedeclarationsMap.
00303   unsigned LocalNumRedeclarationsInMap;
00304   
00305   /// \brief The redeclaration chains for declarations local to this
00306   /// module file.
00307   SmallVector<uint64_t, 1> RedeclarationChains;
00308   
00309   /// \brief Array of category list location information within this 
00310   /// module file, sorted by the definition ID.
00311   const serialization::ObjCCategoriesInfo *ObjCCategoriesMap;
00312   
00313   /// \brief The number of redeclaration info entries in ObjCCategoriesMap.
00314   unsigned LocalNumObjCCategoriesInMap;
00315   
00316   /// \brief The Objective-C category lists for categories known to this
00317   /// module.
00318   SmallVector<uint64_t, 1> ObjCCategories;
00319 
00320   // === Types ===
00321 
00322   /// \brief The number of types in this AST file.
00323   unsigned LocalNumTypes;
00324 
00325   /// \brief Offset of each type within the bitstream, indexed by the
00326   /// type ID, or the representation of a Type*.
00327   const uint32_t *TypeOffsets;
00328 
00329   /// \brief Base type ID for types local to this module as represented in
00330   /// the global type ID space.
00331   serialization::TypeID BaseTypeIndex;
00332 
00333   /// \brief Remapping table for type IDs in this module.
00334   ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
00335 
00336   // === Miscellaneous ===
00337 
00338   /// \brief Diagnostic IDs and their mappings that the user changed.
00339   SmallVector<uint64_t, 8> PragmaDiagMappings;
00340 
00341   /// \brief The AST stat cache installed for this file, if any.
00342   ///
00343   /// The dynamic type of this stat cache is always ASTStatCache
00344   void *StatCache;
00345 
00346   /// \brief List of modules which depend on this module
00347   llvm::SetVector<ModuleFile *> ImportedBy;
00348 
00349   /// \brief List of modules which this module depends on
00350   llvm::SetVector<ModuleFile *> Imports;
00351 
00352   /// \brief Determine whether this module was directly imported at
00353   /// any point during translation.
00354   bool isDirectlyImported() const { return DirectlyImported; }
00355 
00356   /// \brief Dump debugging output for this module.
00357   void dump();
00358 };
00359 
00360 } // end namespace serialization
00361 
00362 } // end namespace clang
00363 
00364 #endif