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