clang API Documentation
00001 //===--- Module.h - Module description --------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the Module class, which describes a module that has 00011 // been loaded from an AST file. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CLANG_SERIALIZATION_MODULE_H 00016 #define LLVM_CLANG_SERIALIZATION_MODULE_H 00017 00018 #include "clang/Basic/SourceLocation.h" 00019 #include "clang/Serialization/ASTBitCodes.h" 00020 #include "clang/Serialization/ContinuousRangeMap.h" 00021 #include "llvm/ADT/SetVector.h" 00022 #include "llvm/Bitcode/BitstreamReader.h" 00023 #include <memory> 00024 #include <string> 00025 00026 namespace llvm { 00027 template <typename Info> class OnDiskChainedHashTable; 00028 template <typename Info> class OnDiskIterableChainedHashTable; 00029 } 00030 00031 namespace clang { 00032 00033 class FileEntry; 00034 class DeclContext; 00035 class Module; 00036 00037 namespace serialization { 00038 00039 namespace reader { 00040 class ASTDeclContextNameLookupTrait; 00041 } 00042 00043 /// \brief Specifies the kind of module that has been loaded. 00044 enum ModuleKind { 00045 MK_ImplicitModule, ///< File is an implicitly-loaded module. 00046 MK_ExplicitModule, ///< File is an explicitly-loaded module. 00047 MK_PCH, ///< File is a PCH file treated as such. 00048 MK_Preamble, ///< File is a PCH file treated as the preamble. 00049 MK_MainFile ///< File is a PCH file treated as the actual main file. 00050 }; 00051 00052 /// \brief Information about the contents of a DeclContext. 00053 struct DeclContextInfo { 00054 DeclContextInfo() 00055 : NameLookupTableData(), LexicalDecls(), NumLexicalDecls() {} 00056 00057 llvm::OnDiskIterableChainedHashTable<reader::ASTDeclContextNameLookupTrait> 00058 *NameLookupTableData; // an ASTDeclContextNameLookupTable. 00059 const KindDeclIDPair *LexicalDecls; 00060 unsigned NumLexicalDecls; 00061 }; 00062 00063 /// \brief The input file that has been loaded from this AST file, along with 00064 /// bools indicating whether this was an overridden buffer or if it was 00065 /// out-of-date or not-found. 00066 class InputFile { 00067 enum { 00068 Overridden = 1, 00069 OutOfDate = 2, 00070 NotFound = 3 00071 }; 00072 llvm::PointerIntPair<const FileEntry *, 2, unsigned> Val; 00073 00074 public: 00075 InputFile() {} 00076 InputFile(const FileEntry *File, 00077 bool isOverridden = false, bool isOutOfDate = false) { 00078 assert(!(isOverridden && isOutOfDate) && 00079 "an overridden cannot be out-of-date"); 00080 unsigned intVal = 0; 00081 if (isOverridden) 00082 intVal = Overridden; 00083 else if (isOutOfDate) 00084 intVal = OutOfDate; 00085 Val.setPointerAndInt(File, intVal); 00086 } 00087 00088 static InputFile getNotFound() { 00089 InputFile File; 00090 File.Val.setInt(NotFound); 00091 return File; 00092 } 00093 00094 const FileEntry *getFile() const { return Val.getPointer(); } 00095 bool isOverridden() const { return Val.getInt() == Overridden; } 00096 bool isOutOfDate() const { return Val.getInt() == OutOfDate; } 00097 bool isNotFound() const { return Val.getInt() == NotFound; } 00098 }; 00099 00100 typedef unsigned ASTFileSignature; 00101 00102 /// \brief Information about a module that has been loaded by the ASTReader. 00103 /// 00104 /// Each instance of the Module class corresponds to a single AST file, which 00105 /// may be a precompiled header, precompiled preamble, a module, or an AST file 00106 /// of some sort loaded as the main file, all of which are specific formulations 00107 /// of the general notion of a "module". A module may depend on any number of 00108 /// other modules. 00109 class ModuleFile { 00110 public: 00111 ModuleFile(ModuleKind Kind, unsigned Generation); 00112 ~ModuleFile(); 00113 00114 // === General information === 00115 00116 /// \brief The index of this module in the list of modules. 00117 unsigned Index; 00118 00119 /// \brief The type of this module. 00120 ModuleKind Kind; 00121 00122 /// \brief The file name of the module file. 00123 std::string FileName; 00124 00125 /// \brief The name of the module. 00126 std::string ModuleName; 00127 00128 std::string getTimestampFilename() const { 00129 return FileName + ".timestamp"; 00130 } 00131 00132 /// \brief The original source file name that was used to build the 00133 /// primary AST file, which may have been modified for 00134 /// relocatable-pch support. 00135 std::string OriginalSourceFileName; 00136 00137 /// \brief The actual original source file name that was used to 00138 /// build this AST file. 00139 std::string ActualOriginalSourceFileName; 00140 00141 /// \brief The file ID for the original source file that was used to 00142 /// build this AST file. 00143 FileID OriginalSourceFileID; 00144 00145 /// \brief The directory that the PCH was originally created in. Used to 00146 /// allow resolving headers even after headers+PCH was moved to a new path. 00147 std::string OriginalDir; 00148 00149 std::string ModuleMapPath; 00150 00151 /// \brief Whether this precompiled header is a relocatable PCH file. 00152 bool RelocatablePCH; 00153 00154 /// \brief The file entry for the module file. 00155 const FileEntry *File; 00156 00157 /// \brief The signature of the module file, which may be used along with size 00158 /// and modification time to identify this particular file. 00159 ASTFileSignature Signature; 00160 00161 /// \brief Whether this module has been directly imported by the 00162 /// user. 00163 bool DirectlyImported; 00164 00165 /// \brief The generation of which this module file is a part. 00166 unsigned Generation; 00167 00168 /// \brief The memory buffer that stores the data associated with 00169 /// this AST file. 00170 std::unique_ptr<llvm::MemoryBuffer> Buffer; 00171 00172 /// \brief The size of this file, in bits. 00173 uint64_t SizeInBits; 00174 00175 /// \brief The global bit offset (or base) of this module 00176 uint64_t GlobalBitOffset; 00177 00178 /// \brief The bitstream reader from which we'll read the AST file. 00179 llvm::BitstreamReader StreamFile; 00180 00181 /// \brief The main bitstream cursor for the main block. 00182 llvm::BitstreamCursor Stream; 00183 00184 /// \brief The source location where the module was explicitly or implicitly 00185 /// imported in the local translation unit. 00186 /// 00187 /// If module A depends on and imports module B, both modules will have the 00188 /// same DirectImportLoc, but different ImportLoc (B's ImportLoc will be a 00189 /// source location inside module A). 00190 /// 00191 /// WARNING: This is largely useless. It doesn't tell you when a module was 00192 /// made visible, just when the first submodule of that module was imported. 00193 SourceLocation DirectImportLoc; 00194 00195 /// \brief The source location where this module was first imported. 00196 SourceLocation ImportLoc; 00197 00198 /// \brief The first source location in this module. 00199 SourceLocation FirstLoc; 00200 00201 // === Input Files === 00202 /// \brief The cursor to the start of the input-files block. 00203 llvm::BitstreamCursor InputFilesCursor; 00204 00205 /// \brief Offsets for all of the input file entries in the AST file. 00206 const uint32_t *InputFileOffsets; 00207 00208 /// \brief The input files that have been loaded from this AST file. 00209 std::vector<InputFile> InputFilesLoaded; 00210 00211 /// \brief If non-zero, specifies the time when we last validated input 00212 /// files. Zero means we never validated them. 00213 /// 00214 /// The time is specified in seconds since the start of the Epoch. 00215 uint64_t InputFilesValidationTimestamp; 00216 00217 // === Source Locations === 00218 00219 /// \brief Cursor used to read source location entries. 00220 llvm::BitstreamCursor SLocEntryCursor; 00221 00222 /// \brief The number of source location entries in this AST file. 00223 unsigned LocalNumSLocEntries; 00224 00225 /// \brief The base ID in the source manager's view of this module. 00226 int SLocEntryBaseID; 00227 00228 /// \brief The base offset in the source manager's view of this module. 00229 unsigned SLocEntryBaseOffset; 00230 00231 /// \brief Offsets for all of the source location entries in the 00232 /// AST file. 00233 const uint32_t *SLocEntryOffsets; 00234 00235 /// \brief SLocEntries that we're going to preload. 00236 SmallVector<uint64_t, 4> PreloadSLocEntries; 00237 00238 /// \brief Remapping table for source locations in this module. 00239 ContinuousRangeMap<uint32_t, int, 2> SLocRemap; 00240 00241 // === Identifiers === 00242 00243 /// \brief The number of identifiers in this AST file. 00244 unsigned LocalNumIdentifiers; 00245 00246 /// \brief Offsets into the identifier table data. 00247 /// 00248 /// This array is indexed by the identifier ID (-1), and provides 00249 /// the offset into IdentifierTableData where the string data is 00250 /// stored. 00251 const uint32_t *IdentifierOffsets; 00252 00253 /// \brief Base identifier ID for identifiers local to this module. 00254 serialization::IdentID BaseIdentifierID; 00255 00256 /// \brief Remapping table for identifier IDs in this module. 00257 ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap; 00258 00259 /// \brief Actual data for the on-disk hash table of identifiers. 00260 /// 00261 /// This pointer points into a memory buffer, where the on-disk hash 00262 /// table for identifiers actually lives. 00263 const char *IdentifierTableData; 00264 00265 /// \brief A pointer to an on-disk hash table of opaque type 00266 /// IdentifierHashTable. 00267 void *IdentifierLookupTable; 00268 00269 // === Macros === 00270 00271 /// \brief The cursor to the start of the preprocessor block, which stores 00272 /// all of the macro definitions. 00273 llvm::BitstreamCursor MacroCursor; 00274 00275 /// \brief The number of macros in this AST file. 00276 unsigned LocalNumMacros; 00277 00278 /// \brief Offsets of macros in the preprocessor block. 00279 /// 00280 /// This array is indexed by the macro ID (-1), and provides 00281 /// the offset into the preprocessor block where macro definitions are 00282 /// stored. 00283 const uint32_t *MacroOffsets; 00284 00285 /// \brief Base macro ID for macros local to this module. 00286 serialization::MacroID BaseMacroID; 00287 00288 /// \brief Remapping table for macro IDs in this module. 00289 ContinuousRangeMap<uint32_t, int, 2> MacroRemap; 00290 00291 /// \brief The offset of the start of the set of defined macros. 00292 uint64_t MacroStartOffset; 00293 00294 // === Detailed PreprocessingRecord === 00295 00296 /// \brief The cursor to the start of the (optional) detailed preprocessing 00297 /// record block. 00298 llvm::BitstreamCursor PreprocessorDetailCursor; 00299 00300 /// \brief The offset of the start of the preprocessor detail cursor. 00301 uint64_t PreprocessorDetailStartOffset; 00302 00303 /// \brief Base preprocessed entity ID for preprocessed entities local to 00304 /// this module. 00305 serialization::PreprocessedEntityID BasePreprocessedEntityID; 00306 00307 /// \brief Remapping table for preprocessed entity IDs in this module. 00308 ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap; 00309 00310 const PPEntityOffset *PreprocessedEntityOffsets; 00311 unsigned NumPreprocessedEntities; 00312 00313 // === Header search information === 00314 00315 /// \brief The number of local HeaderFileInfo structures. 00316 unsigned LocalNumHeaderFileInfos; 00317 00318 /// \brief Actual data for the on-disk hash table of header file 00319 /// information. 00320 /// 00321 /// This pointer points into a memory buffer, where the on-disk hash 00322 /// table for header file information actually lives. 00323 const char *HeaderFileInfoTableData; 00324 00325 /// \brief The on-disk hash table that contains information about each of 00326 /// the header files. 00327 void *HeaderFileInfoTable; 00328 00329 // === Submodule information === 00330 /// \brief The number of submodules in this module. 00331 unsigned LocalNumSubmodules; 00332 00333 /// \brief Base submodule ID for submodules local to this module. 00334 serialization::SubmoduleID BaseSubmoduleID; 00335 00336 /// \brief Remapping table for submodule IDs in this module. 00337 ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap; 00338 00339 // === Selectors === 00340 00341 /// \brief The number of selectors new to this file. 00342 /// 00343 /// This is the number of entries in SelectorOffsets. 00344 unsigned LocalNumSelectors; 00345 00346 /// \brief Offsets into the selector lookup table's data array 00347 /// where each selector resides. 00348 const uint32_t *SelectorOffsets; 00349 00350 /// \brief Base selector ID for selectors local to this module. 00351 serialization::SelectorID BaseSelectorID; 00352 00353 /// \brief Remapping table for selector IDs in this module. 00354 ContinuousRangeMap<uint32_t, int, 2> SelectorRemap; 00355 00356 /// \brief A pointer to the character data that comprises the selector table 00357 /// 00358 /// The SelectorOffsets table refers into this memory. 00359 const unsigned char *SelectorLookupTableData; 00360 00361 /// \brief A pointer to an on-disk hash table of opaque type 00362 /// ASTSelectorLookupTable. 00363 /// 00364 /// This hash table provides the IDs of all selectors, and the associated 00365 /// instance and factory methods. 00366 void *SelectorLookupTable; 00367 00368 // === Declarations === 00369 00370 /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It 00371 /// has read all the abbreviations at the start of the block and is ready to 00372 /// jump around with these in context. 00373 llvm::BitstreamCursor DeclsCursor; 00374 00375 /// \brief The number of declarations in this AST file. 00376 unsigned LocalNumDecls; 00377 00378 /// \brief Offset of each declaration within the bitstream, indexed 00379 /// by the declaration ID (-1). 00380 const DeclOffset *DeclOffsets; 00381 00382 /// \brief Base declaration ID for declarations local to this module. 00383 serialization::DeclID BaseDeclID; 00384 00385 /// \brief Remapping table for declaration IDs in this module. 00386 ContinuousRangeMap<uint32_t, int, 2> DeclRemap; 00387 00388 /// \brief Mapping from the module files that this module file depends on 00389 /// to the base declaration ID for that module as it is understood within this 00390 /// module. 00391 /// 00392 /// This is effectively a reverse global-to-local mapping for declaration 00393 /// IDs, so that we can interpret a true global ID (for this translation unit) 00394 /// as a local ID (for this module file). 00395 llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs; 00396 00397 /// \brief The number of C++ base specifier sets in this AST file. 00398 unsigned LocalNumCXXBaseSpecifiers; 00399 00400 /// \brief Offset of each C++ base specifier set within the bitstream, 00401 /// indexed by the C++ base specifier set ID (-1). 00402 const uint32_t *CXXBaseSpecifiersOffsets; 00403 00404 typedef llvm::DenseMap<const DeclContext *, DeclContextInfo> 00405 DeclContextInfosMap; 00406 00407 /// \brief Information about the lexical and visible declarations 00408 /// for each DeclContext. 00409 DeclContextInfosMap DeclContextInfos; 00410 00411 /// \brief Array of file-level DeclIDs sorted by file. 00412 const serialization::DeclID *FileSortedDecls; 00413 unsigned NumFileSortedDecls; 00414 00415 /// \brief Array of redeclaration chain location information within this 00416 /// module file, sorted by the first declaration ID. 00417 const serialization::LocalRedeclarationsInfo *RedeclarationsMap; 00418 00419 /// \brief The number of redeclaration info entries in RedeclarationsMap. 00420 unsigned LocalNumRedeclarationsInMap; 00421 00422 /// \brief The redeclaration chains for declarations local to this 00423 /// module file. 00424 SmallVector<uint64_t, 1> RedeclarationChains; 00425 00426 /// \brief Array of category list location information within this 00427 /// module file, sorted by the definition ID. 00428 const serialization::ObjCCategoriesInfo *ObjCCategoriesMap; 00429 00430 /// \brief The number of redeclaration info entries in ObjCCategoriesMap. 00431 unsigned LocalNumObjCCategoriesInMap; 00432 00433 /// \brief The Objective-C category lists for categories known to this 00434 /// module. 00435 SmallVector<uint64_t, 1> ObjCCategories; 00436 00437 // === Types === 00438 00439 /// \brief The number of types in this AST file. 00440 unsigned LocalNumTypes; 00441 00442 /// \brief Offset of each type within the bitstream, indexed by the 00443 /// type ID, or the representation of a Type*. 00444 const uint32_t *TypeOffsets; 00445 00446 /// \brief Base type ID for types local to this module as represented in 00447 /// the global type ID space. 00448 serialization::TypeID BaseTypeIndex; 00449 00450 /// \brief Remapping table for type IDs in this module. 00451 ContinuousRangeMap<uint32_t, int, 2> TypeRemap; 00452 00453 // === Miscellaneous === 00454 00455 /// \brief Diagnostic IDs and their mappings that the user changed. 00456 SmallVector<uint64_t, 8> PragmaDiagMappings; 00457 00458 /// \brief List of modules which depend on this module 00459 llvm::SetVector<ModuleFile *> ImportedBy; 00460 00461 /// \brief List of modules which this module depends on 00462 llvm::SetVector<ModuleFile *> Imports; 00463 00464 /// \brief Determine whether this module was directly imported at 00465 /// any point during translation. 00466 bool isDirectlyImported() const { return DirectlyImported; } 00467 00468 /// \brief Dump debugging output for this module. 00469 void dump(); 00470 }; 00471 00472 } // end namespace serialization 00473 00474 } // end namespace clang 00475 00476 #endif