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