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/Basic/SourceLocation.h"
     19 #include "clang/Serialization/ASTBitCodes.h"
     20 #include "clang/Serialization/ContinuousRangeMap.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 FileEntry;
     29 class DeclContext;
     30 class Module;
     31 template<typename Info> class OnDiskChainedHashTable;
     32 
     33 namespace serialization {
     34 
     35 namespace reader {
     36   class ASTDeclContextNameLookupTrait;
     37 }
     38 
     39 /// \brief Specifies the kind of module that has been loaded.
     40 enum ModuleKind {
     41   MK_Module,   ///< File is a module proper.
     42   MK_PCH,      ///< File is a PCH file treated as such.
     43   MK_Preamble, ///< File is a PCH file treated as the preamble.
     44   MK_MainFile  ///< File is a PCH file treated as the actual main file.
     45 };
     46 
     47 /// \brief Information about the contents of a DeclContext.
     48 struct DeclContextInfo {
     49   DeclContextInfo()
     50     : NameLookupTableData(), LexicalDecls(), NumLexicalDecls() {}
     51 
     52   OnDiskChainedHashTable<reader::ASTDeclContextNameLookupTrait>
     53     *NameLookupTableData; // an ASTDeclContextNameLookupTable.
     54   const KindDeclIDPair *LexicalDecls;
     55   unsigned NumLexicalDecls;
     56 };
     57 
     58 /// \brief The input file that has been loaded from this AST file, along with
     59 /// bools indicating whether this was an overridden buffer or if it was
     60 /// out-of-date.
     61 class InputFile {
     62   enum {
     63     Overridden = 1,
     64     OutOfDate = 2
     65   };
     66   llvm::PointerIntPair<const FileEntry *, 2, unsigned> Val;
     67 
     68 public:
     69   InputFile() {}
     70   InputFile(const FileEntry *File,
     71             bool isOverridden = false, bool isOutOfDate = false) {
     72     assert(!(isOverridden && isOutOfDate) &&
     73            "an overridden cannot be out-of-date");
     74     unsigned intVal = 0;
     75     if (isOverridden)
     76       intVal = Overridden;
     77     else if (isOutOfDate)
     78       intVal = OutOfDate;
     79     Val.setPointerAndInt(File, intVal);
     80   }
     81 
     82   const FileEntry *getFile() const { return Val.getPointer(); }
     83   bool isOverridden() const { return Val.getInt() == Overridden; }
     84   bool isOutOfDate() const { return Val.getInt() == OutOfDate; }
     85 };
     86 
     87 /// \brief Information about a module that has been loaded by the ASTReader.
     88 ///
     89 /// Each instance of the Module class corresponds to a single AST file, which
     90 /// may be a precompiled header, precompiled preamble, a module, or an AST file
     91 /// of some sort loaded as the main file, all of which are specific formulations
     92 /// of the general notion of a "module". A module may depend on any number of
     93 /// other modules.
     94 class ModuleFile {
     95 public:
     96   ModuleFile(ModuleKind Kind, unsigned Generation);
     97   ~ModuleFile();
     98 
     99   // === General information ===
    100 
    101   /// \brief The index of this module in the list of modules.
    102   unsigned Index;
    103 
    104   /// \brief The type of this module.
    105   ModuleKind Kind;
    106 
    107   /// \brief The file name of the module file.
    108   std::string FileName;
    109 
    110   /// \brief The original source file name that was used to build the
    111   /// primary AST file, which may have been modified for
    112   /// relocatable-pch support.
    113   std::string OriginalSourceFileName;
    114 
    115   /// \brief The actual original source file name that was used to
    116   /// build this AST file.
    117   std::string ActualOriginalSourceFileName;
    118 
    119   /// \brief The file ID for the original source file that was used to
    120   /// build this AST file.
    121   FileID OriginalSourceFileID;
    122 
    123   /// \brief The directory that the PCH was originally created in. Used to
    124   /// allow resolving headers even after headers+PCH was moved to a new path.
    125   std::string OriginalDir;
    126 
    127   /// \brief Whether this precompiled header is a relocatable PCH file.
    128   bool RelocatablePCH;
    129 
    130   /// \brief The file entry for the module file.
    131   const FileEntry *File;
    132 
    133   /// \brief Whether this module has been directly imported by the
    134   /// user.
    135   bool DirectlyImported;
    136 
    137   /// \brief The generation of which this module file is a part.
    138   unsigned Generation;
    139 
    140   /// \brief The memory buffer that stores the data associated with
    141   /// this AST file.
    142   OwningPtr<llvm::MemoryBuffer> Buffer;
    143 
    144   /// \brief The size of this file, in bits.
    145   uint64_t SizeInBits;
    146 
    147   /// \brief The global bit offset (or base) of this module
    148   uint64_t GlobalBitOffset;
    149 
    150   /// \brief The bitstream reader from which we'll read the AST file.
    151   llvm::BitstreamReader StreamFile;
    152 
    153   /// \brief The main bitstream cursor for the main block.
    154   llvm::BitstreamCursor Stream;
    155 
    156   /// \brief The source location where the module was explicitly or implicitly
    157   /// imported in the local translation unit.
    158   ///
    159   /// If module A depends on and imports module B, both modules will have the
    160   /// same DirectImportLoc, but different ImportLoc (B's ImportLoc will be a
    161   /// source location inside module A).
    162   SourceLocation DirectImportLoc;
    163 
    164   /// \brief The source location where this module was first imported.
    165   SourceLocation ImportLoc;
    166 
    167   /// \brief The first source location in this module.
    168   SourceLocation FirstLoc;
    169 
    170   // === Input Files ===
    171   /// \brief The cursor to the start of the input-files block.
    172   llvm::BitstreamCursor InputFilesCursor;
    173 
    174   /// \brief Offsets for all of the input file entries in the AST file.
    175   const uint32_t *InputFileOffsets;
    176 
    177   /// \brief The input files that have been loaded from this AST file.
    178   std::vector<InputFile> InputFilesLoaded;
    179 
    180   // === Source Locations ===
    181 
    182   /// \brief Cursor used to read source location entries.
    183   llvm::BitstreamCursor SLocEntryCursor;
    184 
    185   /// \brief The number of source location entries in this AST file.
    186   unsigned LocalNumSLocEntries;
    187 
    188   /// \brief The base ID in the source manager's view of this module.
    189   int SLocEntryBaseID;
    190 
    191   /// \brief The base offset in the source manager's view of this module.
    192   unsigned SLocEntryBaseOffset;
    193 
    194   /// \brief Offsets for all of the source location entries in the
    195   /// AST file.
    196   const uint32_t *SLocEntryOffsets;
    197 
    198   /// \brief SLocEntries that we're going to preload.
    199   SmallVector<uint64_t, 4> PreloadSLocEntries;
    200 
    201   /// \brief Remapping table for source locations in this module.
    202   ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
    203 
    204   // === Identifiers ===
    205 
    206   /// \brief The number of identifiers in this AST file.
    207   unsigned LocalNumIdentifiers;
    208 
    209   /// \brief Offsets into the identifier table data.
    210   ///
    211   /// This array is indexed by the identifier ID (-1), and provides
    212   /// the offset into IdentifierTableData where the string data is
    213   /// stored.
    214   const uint32_t *IdentifierOffsets;
    215 
    216   /// \brief Base identifier ID for identifiers local to this module.
    217   serialization::IdentID BaseIdentifierID;
    218 
    219   /// \brief Remapping table for identifier IDs in this module.
    220   ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
    221 
    222   /// \brief Actual data for the on-disk hash table of identifiers.
    223   ///
    224   /// This pointer points into a memory buffer, where the on-disk hash
    225   /// table for identifiers actually lives.
    226   const char *IdentifierTableData;
    227 
    228   /// \brief A pointer to an on-disk hash table of opaque type
    229   /// IdentifierHashTable.
    230   void *IdentifierLookupTable;
    231 
    232   // === Macros ===
    233 
    234   /// \brief The cursor to the start of the preprocessor block, which stores
    235   /// all of the macro definitions.
    236   llvm::BitstreamCursor MacroCursor;
    237 
    238   /// \brief The number of macros in this AST file.
    239   unsigned LocalNumMacros;
    240 
    241   /// \brief Offsets of macros in the preprocessor block.
    242   ///
    243   /// This array is indexed by the macro ID (-1), and provides
    244   /// the offset into the preprocessor block where macro definitions are
    245   /// stored.
    246   const uint32_t *MacroOffsets;
    247 
    248   /// \brief Base macro ID for macros local to this module.
    249   serialization::MacroID BaseMacroID;
    250 
    251   /// \brief Remapping table for macro IDs in this module.
    252   ContinuousRangeMap<uint32_t, int, 2> MacroRemap;
    253 
    254   /// \brief The offset of the start of the set of defined macros.
    255   uint64_t MacroStartOffset;
    256 
    257   // === Detailed PreprocessingRecord ===
    258 
    259   /// \brief The cursor to the start of the (optional) detailed preprocessing
    260   /// record block.
    261   llvm::BitstreamCursor PreprocessorDetailCursor;
    262 
    263   /// \brief The offset of the start of the preprocessor detail cursor.
    264   uint64_t PreprocessorDetailStartOffset;
    265 
    266   /// \brief Base preprocessed entity ID for preprocessed entities local to
    267   /// this module.
    268   serialization::PreprocessedEntityID BasePreprocessedEntityID;
    269 
    270   /// \brief Remapping table for preprocessed entity IDs in this module.
    271   ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
    272 
    273   const PPEntityOffset *PreprocessedEntityOffsets;
    274   unsigned NumPreprocessedEntities;
    275 
    276   // === Header search information ===
    277 
    278   /// \brief The number of local HeaderFileInfo structures.
    279   unsigned LocalNumHeaderFileInfos;
    280 
    281   /// \brief Actual data for the on-disk hash table of header file
    282   /// information.
    283   ///
    284   /// This pointer points into a memory buffer, where the on-disk hash
    285   /// table for header file information actually lives.
    286   const char *HeaderFileInfoTableData;
    287 
    288   /// \brief The on-disk hash table that contains information about each of
    289   /// the header files.
    290   void *HeaderFileInfoTable;
    291 
    292   // === Submodule information ===
    293   /// \brief The number of submodules in this module.
    294   unsigned LocalNumSubmodules;
    295 
    296   /// \brief Base submodule ID for submodules local to this module.
    297   serialization::SubmoduleID BaseSubmoduleID;
    298 
    299   /// \brief Remapping table for submodule IDs in this module.
    300   ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
    301 
    302   // === Selectors ===
    303 
    304   /// \brief The number of selectors new to this file.
    305   ///
    306   /// This is the number of entries in SelectorOffsets.
    307   unsigned LocalNumSelectors;
    308 
    309   /// \brief Offsets into the selector lookup table's data array
    310   /// where each selector resides.
    311   const uint32_t *SelectorOffsets;
    312 
    313   /// \brief Base selector ID for selectors local to this module.
    314   serialization::SelectorID BaseSelectorID;
    315 
    316   /// \brief Remapping table for selector IDs in this module.
    317   ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
    318 
    319   /// \brief A pointer to the character data that comprises the selector table
    320   ///
    321   /// The SelectorOffsets table refers into this memory.
    322   const unsigned char *SelectorLookupTableData;
    323 
    324   /// \brief A pointer to an on-disk hash table of opaque type
    325   /// ASTSelectorLookupTable.
    326   ///
    327   /// This hash table provides the IDs of all selectors, and the associated
    328   /// instance and factory methods.
    329   void *SelectorLookupTable;
    330 
    331   // === Declarations ===
    332 
    333   /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
    334   /// has read all the abbreviations at the start of the block and is ready to
    335   /// jump around with these in context.
    336   llvm::BitstreamCursor DeclsCursor;
    337 
    338   /// \brief The number of declarations in this AST file.
    339   unsigned LocalNumDecls;
    340 
    341   /// \brief Offset of each declaration within the bitstream, indexed
    342   /// by the declaration ID (-1).
    343   const DeclOffset *DeclOffsets;
    344 
    345   /// \brief Base declaration ID for declarations local to this module.
    346   serialization::DeclID BaseDeclID;
    347 
    348   /// \brief Remapping table for declaration IDs in this module.
    349   ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
    350 
    351   /// \brief Mapping from the module files that this module file depends on
    352   /// to the base declaration ID for that module as it is understood within this
    353   /// module.
    354   ///
    355   /// This is effectively a reverse global-to-local mapping for declaration
    356   /// IDs, so that we can interpret a true global ID (for this translation unit)
    357   /// as a local ID (for this module file).
    358   llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs;
    359 
    360   /// \brief The number of C++ base specifier sets in this AST file.
    361   unsigned LocalNumCXXBaseSpecifiers;
    362 
    363   /// \brief Offset of each C++ base specifier set within the bitstream,
    364   /// indexed by the C++ base specifier set ID (-1).
    365   const uint32_t *CXXBaseSpecifiersOffsets;
    366 
    367   typedef llvm::DenseMap<const DeclContext *, DeclContextInfo>
    368   DeclContextInfosMap;
    369 
    370   /// \brief Information about the lexical and visible declarations
    371   /// for each DeclContext.
    372   DeclContextInfosMap DeclContextInfos;
    373 
    374   /// \brief Array of file-level DeclIDs sorted by file.
    375   const serialization::DeclID *FileSortedDecls;
    376   unsigned NumFileSortedDecls;
    377 
    378   /// \brief Array of redeclaration chain location information within this
    379   /// module file, sorted by the first declaration ID.
    380   const serialization::LocalRedeclarationsInfo *RedeclarationsMap;
    381 
    382   /// \brief The number of redeclaration info entries in RedeclarationsMap.
    383   unsigned LocalNumRedeclarationsInMap;
    384 
    385   /// \brief The redeclaration chains for declarations local to this
    386   /// module file.
    387   SmallVector<uint64_t, 1> RedeclarationChains;
    388 
    389   /// \brief Array of category list location information within this
    390   /// module file, sorted by the definition ID.
    391   const serialization::ObjCCategoriesInfo *ObjCCategoriesMap;
    392 
    393   /// \brief The number of redeclaration info entries in ObjCCategoriesMap.
    394   unsigned LocalNumObjCCategoriesInMap;
    395 
    396   /// \brief The Objective-C category lists for categories known to this
    397   /// module.
    398   SmallVector<uint64_t, 1> ObjCCategories;
    399 
    400   // === Types ===
    401 
    402   /// \brief The number of types in this AST file.
    403   unsigned LocalNumTypes;
    404 
    405   /// \brief Offset of each type within the bitstream, indexed by the
    406   /// type ID, or the representation of a Type*.
    407   const uint32_t *TypeOffsets;
    408 
    409   /// \brief Base type ID for types local to this module as represented in
    410   /// the global type ID space.
    411   serialization::TypeID BaseTypeIndex;
    412 
    413   /// \brief Remapping table for type IDs in this module.
    414   ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
    415 
    416   // === Miscellaneous ===
    417 
    418   /// \brief Diagnostic IDs and their mappings that the user changed.
    419   SmallVector<uint64_t, 8> PragmaDiagMappings;
    420 
    421   /// \brief List of modules which depend on this module
    422   llvm::SetVector<ModuleFile *> ImportedBy;
    423 
    424   /// \brief List of modules which this module depends on
    425   llvm::SetVector<ModuleFile *> Imports;
    426 
    427   /// \brief Determine whether this module was directly imported at
    428   /// any point during translation.
    429   bool isDirectlyImported() const { return DirectlyImported; }
    430 
    431   /// \brief Dump debugging output for this module.
    432   void dump();
    433 };
    434 
    435 } // end namespace serialization
    436 
    437 } // end namespace clang
    438 
    439 #endif
    440