Home | History | Annotate | Download | only in Serialization
      1 //===--- Module.h - Module description --------------------------*- 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 Module class, which describes a module that has
     11 //  been loaded from an AST file.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_SERIALIZATION_MODULE_H
     16 #define LLVM_CLANG_SERIALIZATION_MODULE_H
     17 
     18 #include "clang/Serialization/ASTBitCodes.h"
     19 #include "clang/Serialization/ContinuousRangeMap.h"
     20 #include "clang/Basic/SourceLocation.h"
     21 #include "llvm/ADT/OwningPtr.h"
     22 #include "llvm/ADT/SetVector.h"
     23 #include "llvm/Bitcode/BitstreamReader.h"
     24 #include <string>
     25 
     26 namespace clang {
     27 
     28 class DeclContext;
     29 class Module;
     30 template<typename Info> class OnDiskChainedHashTable;
     31 
     32 namespace serialization {
     33 
     34 namespace reader {
     35   class ASTDeclContextNameLookupTrait;
     36 }
     37 
     38 /// \brief Specifies the kind of module that has been loaded.
     39 enum ModuleKind {
     40   MK_Module,   ///< File is a module proper.
     41   MK_PCH,      ///< File is a PCH file treated as such.
     42   MK_Preamble, ///< File is a PCH file treated as the preamble.
     43   MK_MainFile  ///< File is a PCH file treated as the actual main file.
     44 };
     45 
     46 /// \brief Information about the contents of a DeclContext.
     47 struct DeclContextInfo {
     48   DeclContextInfo()
     49     : NameLookupTableData(), LexicalDecls(), NumLexicalDecls() {}
     50 
     51   OnDiskChainedHashTable<reader::ASTDeclContextNameLookupTrait>
     52     *NameLookupTableData; // an ASTDeclContextNameLookupTable.
     53   const KindDeclIDPair *LexicalDecls;
     54   unsigned NumLexicalDecls;
     55 };
     56 
     57 /// \brief Information about a module that has been loaded by the ASTReader.
     58 ///
     59 /// Each instance of the Module class corresponds to a single AST file, which
     60 /// may be a precompiled header, precompiled preamble, a module, or an AST file
     61 /// of some sort loaded as the main file, all of which are specific formulations
     62 /// of the general notion of a "module". A module may depend on any number of
     63 /// other modules.
     64 class ModuleFile {
     65 public:
     66   ModuleFile(ModuleKind Kind, unsigned Generation);
     67   ~ModuleFile();
     68 
     69   // === General information ===
     70 
     71   /// \brief The type of this module.
     72   ModuleKind Kind;
     73 
     74   /// \brief The file name of the module file.
     75   std::string FileName;
     76 
     77   /// \brief Whether this module has been directly imported by the
     78   /// user.
     79   bool DirectlyImported;
     80 
     81   /// \brief The generation of which this module file is a part.
     82   unsigned Generation;
     83 
     84   /// \brief The memory buffer that stores the data associated with
     85   /// this AST file.
     86   OwningPtr<llvm::MemoryBuffer> Buffer;
     87 
     88   /// \brief The size of this file, in bits.
     89   uint64_t SizeInBits;
     90 
     91   /// \brief The global bit offset (or base) of this module
     92   uint64_t GlobalBitOffset;
     93 
     94   /// \brief The bitstream reader from which we'll read the AST file.
     95   llvm::BitstreamReader StreamFile;
     96 
     97   /// \brief The main bitstream cursor for the main block.
     98   llvm::BitstreamCursor Stream;
     99 
    100   /// \brief The source location where this module was first imported.
    101   SourceLocation ImportLoc;
    102 
    103   /// \brief The first source location in this module.
    104   SourceLocation FirstLoc;
    105 
    106   // === Source Locations ===
    107 
    108   /// \brief Cursor used to read source location entries.
    109   llvm::BitstreamCursor SLocEntryCursor;
    110 
    111   /// \brief The number of source location entries in this AST file.
    112   unsigned LocalNumSLocEntries;
    113 
    114   /// \brief The base ID in the source manager's view of this module.
    115   int SLocEntryBaseID;
    116 
    117   /// \brief The base offset in the source manager's view of this module.
    118   unsigned SLocEntryBaseOffset;
    119 
    120   /// \brief Offsets for all of the source location entries in the
    121   /// AST file.
    122   const uint32_t *SLocEntryOffsets;
    123 
    124   /// \brief SLocEntries that we're going to preload.
    125   SmallVector<uint64_t, 4> PreloadSLocEntries;
    126 
    127   /// \brief The number of source location file entries in this AST file.
    128   unsigned LocalNumSLocFileEntries;
    129 
    130   /// \brief Offsets for all of the source location file entries in the
    131   /// AST file.
    132   const uint32_t *SLocFileOffsets;
    133 
    134   /// \brief Remapping table for source locations in this module.
    135   ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
    136 
    137   // === Identifiers ===
    138 
    139   /// \brief The number of identifiers in this AST file.
    140   unsigned LocalNumIdentifiers;
    141 
    142   /// \brief Offsets into the identifier table data.
    143   ///
    144   /// This array is indexed by the identifier ID (-1), and provides
    145   /// the offset into IdentifierTableData where the string data is
    146   /// stored.
    147   const uint32_t *IdentifierOffsets;
    148 
    149   /// \brief Base identifier ID for identifiers local to this module.
    150   serialization::IdentID BaseIdentifierID;
    151 
    152   /// \brief Remapping table for identifier IDs in this module.
    153   ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
    154 
    155   /// \brief Actual data for the on-disk hash table of identifiers.
    156   ///
    157   /// This pointer points into a memory buffer, where the on-disk hash
    158   /// table for identifiers actually lives.
    159   const char *IdentifierTableData;
    160 
    161   /// \brief A pointer to an on-disk hash table of opaque type
    162   /// IdentifierHashTable.
    163   void *IdentifierLookupTable;
    164 
    165   // === Macros ===
    166 
    167   /// \brief The cursor to the start of the preprocessor block, which stores
    168   /// all of the macro definitions.
    169   llvm::BitstreamCursor MacroCursor;
    170 
    171   /// \brief The offset of the start of the set of defined macros.
    172   uint64_t MacroStartOffset;
    173 
    174   // === Detailed PreprocessingRecord ===
    175 
    176   /// \brief The cursor to the start of the (optional) detailed preprocessing
    177   /// record block.
    178   llvm::BitstreamCursor PreprocessorDetailCursor;
    179 
    180   /// \brief The offset of the start of the preprocessor detail cursor.
    181   uint64_t PreprocessorDetailStartOffset;
    182 
    183   /// \brief Base preprocessed entity ID for preprocessed entities local to
    184   /// this module.
    185   serialization::PreprocessedEntityID BasePreprocessedEntityID;
    186 
    187   /// \brief Remapping table for preprocessed entity IDs in this module.
    188   ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
    189 
    190   const PPEntityOffset *PreprocessedEntityOffsets;
    191   unsigned NumPreprocessedEntities;
    192 
    193   // === Header search information ===
    194 
    195   /// \brief The number of local HeaderFileInfo structures.
    196   unsigned LocalNumHeaderFileInfos;
    197 
    198   /// \brief Actual data for the on-disk hash table of header file
    199   /// information.
    200   ///
    201   /// This pointer points into a memory buffer, where the on-disk hash
    202   /// table for header file information actually lives.
    203   const char *HeaderFileInfoTableData;
    204 
    205   /// \brief The on-disk hash table that contains information about each of
    206   /// the header files.
    207   void *HeaderFileInfoTable;
    208 
    209   /// \brief Actual data for the list of framework names used in the header
    210   /// search information.
    211   const char *HeaderFileFrameworkStrings;
    212 
    213   // === Submodule information ===
    214   /// \brief The number of submodules in this module.
    215   unsigned LocalNumSubmodules;
    216 
    217   /// \brief Base submodule ID for submodules local to this module.
    218   serialization::SubmoduleID BaseSubmoduleID;
    219 
    220   /// \brief Remapping table for submodule IDs in this module.
    221   ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
    222 
    223   // === Selectors ===
    224 
    225   /// \brief The number of selectors new to this file.
    226   ///
    227   /// This is the number of entries in SelectorOffsets.
    228   unsigned LocalNumSelectors;
    229 
    230   /// \brief Offsets into the selector lookup table's data array
    231   /// where each selector resides.
    232   const uint32_t *SelectorOffsets;
    233 
    234   /// \brief Base selector ID for selectors local to this module.
    235   serialization::SelectorID BaseSelectorID;
    236 
    237   /// \brief Remapping table for selector IDs in this module.
    238   ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
    239 
    240   /// \brief A pointer to the character data that comprises the selector table
    241   ///
    242   /// The SelectorOffsets table refers into this memory.
    243   const unsigned char *SelectorLookupTableData;
    244 
    245   /// \brief A pointer to an on-disk hash table of opaque type
    246   /// ASTSelectorLookupTable.
    247   ///
    248   /// This hash table provides the IDs of all selectors, and the associated
    249   /// instance and factory methods.
    250   void *SelectorLookupTable;
    251 
    252   // === Declarations ===
    253 
    254   /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
    255   /// has read all the abbreviations at the start of the block and is ready to
    256   /// jump around with these in context.
    257   llvm::BitstreamCursor DeclsCursor;
    258 
    259   /// \brief The number of declarations in this AST file.
    260   unsigned LocalNumDecls;
    261 
    262   /// \brief Offset of each declaration within the bitstream, indexed
    263   /// by the declaration ID (-1).
    264   const DeclOffset *DeclOffsets;
    265 
    266   /// \brief Base declaration ID for declarations local to this module.
    267   serialization::DeclID BaseDeclID;
    268 
    269   /// \brief Remapping table for declaration IDs in this module.
    270   ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
    271 
    272   /// \brief Mapping from the module files that this module file depends on
    273   /// to the base declaration ID for that module as it is understood within this
    274   /// module.
    275   ///
    276   /// This is effectively a reverse global-to-local mapping for declaration
    277   /// IDs, so that we can interpret a true global ID (for this translation unit)
    278   /// as a local ID (for this module file).
    279   llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs;
    280 
    281   /// \brief The number of C++ base specifier sets in this AST file.
    282   unsigned LocalNumCXXBaseSpecifiers;
    283 
    284   /// \brief Offset of each C++ base specifier set within the bitstream,
    285   /// indexed by the C++ base specifier set ID (-1).
    286   const uint32_t *CXXBaseSpecifiersOffsets;
    287 
    288   typedef llvm::DenseMap<const DeclContext *, DeclContextInfo>
    289   DeclContextInfosMap;
    290 
    291   /// \brief Information about the lexical and visible declarations
    292   /// for each DeclContext.
    293   DeclContextInfosMap DeclContextInfos;
    294 
    295   /// \brief Array of file-level DeclIDs sorted by file.
    296   const serialization::DeclID *FileSortedDecls;
    297 
    298   /// \brief Array of redeclaration chain location information within this
    299   /// module file, sorted by the first declaration ID.
    300   const serialization::LocalRedeclarationsInfo *RedeclarationsMap;
    301 
    302   /// \brief The number of redeclaration info entries in RedeclarationsMap.
    303   unsigned LocalNumRedeclarationsInMap;
    304 
    305   /// \brief The redeclaration chains for declarations local to this
    306   /// module file.
    307   SmallVector<uint64_t, 1> RedeclarationChains;
    308 
    309   /// \brief Array of category list location information within this
    310   /// module file, sorted by the definition ID.
    311   const serialization::ObjCCategoriesInfo *ObjCCategoriesMap;
    312 
    313   /// \brief The number of redeclaration info entries in ObjCCategoriesMap.
    314   unsigned LocalNumObjCCategoriesInMap;
    315 
    316   /// \brief The Objective-C category lists for categories known to this
    317   /// module.
    318   SmallVector<uint64_t, 1> ObjCCategories;
    319 
    320   // === Types ===
    321 
    322   /// \brief The number of types in this AST file.
    323   unsigned LocalNumTypes;
    324 
    325   /// \brief Offset of each type within the bitstream, indexed by the
    326   /// type ID, or the representation of a Type*.
    327   const uint32_t *TypeOffsets;
    328 
    329   /// \brief Base type ID for types local to this module as represented in
    330   /// the global type ID space.
    331   serialization::TypeID BaseTypeIndex;
    332 
    333   /// \brief Remapping table for type IDs in this module.
    334   ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
    335 
    336   // === Miscellaneous ===
    337 
    338   /// \brief Diagnostic IDs and their mappings that the user changed.
    339   SmallVector<uint64_t, 8> PragmaDiagMappings;
    340 
    341   /// \brief The AST stat cache installed for this file, if any.
    342   ///
    343   /// The dynamic type of this stat cache is always ASTStatCache
    344   void *StatCache;
    345 
    346   /// \brief List of modules which depend on this module
    347   llvm::SetVector<ModuleFile *> ImportedBy;
    348 
    349   /// \brief List of modules which this module depends on
    350   llvm::SetVector<ModuleFile *> Imports;
    351 
    352   /// \brief Determine whether this module was directly imported at
    353   /// any point during translation.
    354   bool isDirectlyImported() const { return DirectlyImported; }
    355 
    356   /// \brief Dump debugging output for this module.
    357   void dump();
    358 };
    359 
    360 } // end namespace serialization
    361 
    362 } // end namespace clang
    363 
    364 #endif
    365