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