clang API Documentation
00001 //===--- ASTReaderInternals.h - AST Reader Internals ------------*- 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 provides internal definitions used in the AST reader. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H 00014 #define LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H 00015 00016 #include "clang/AST/DeclarationName.h" 00017 #include "clang/Serialization/ASTBitCodes.h" 00018 #include "llvm/Support/Endian.h" 00019 #include "llvm/Support/OnDiskHashTable.h" 00020 #include <utility> 00021 00022 namespace clang { 00023 00024 class ASTReader; 00025 class HeaderSearch; 00026 struct HeaderFileInfo; 00027 class FileEntry; 00028 00029 namespace serialization { 00030 00031 class ModuleFile; 00032 00033 namespace reader { 00034 00035 /// \brief Class that performs name lookup into a DeclContext stored 00036 /// in an AST file. 00037 class ASTDeclContextNameLookupTrait { 00038 ASTReader &Reader; 00039 ModuleFile &F; 00040 00041 public: 00042 /// \brief Pair of begin/end iterators for DeclIDs. 00043 /// 00044 /// Note that these declaration IDs are local to the module that contains this 00045 /// particular lookup t 00046 typedef llvm::support::ulittle32_t LE32DeclID; 00047 typedef std::pair<LE32DeclID *, LE32DeclID *> data_type; 00048 typedef unsigned hash_value_type; 00049 typedef unsigned offset_type; 00050 00051 /// \brief Special internal key for declaration names. 00052 /// The hash table creates keys for comparison; we do not create 00053 /// a DeclarationName for the internal key to avoid deserializing types. 00054 struct DeclNameKey { 00055 DeclarationName::NameKind Kind; 00056 uint64_t Data; 00057 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { } 00058 }; 00059 00060 typedef DeclarationName external_key_type; 00061 typedef DeclNameKey internal_key_type; 00062 00063 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F) 00064 : Reader(Reader), F(F) { } 00065 00066 static bool EqualKey(const internal_key_type& a, 00067 const internal_key_type& b) { 00068 return a.Kind == b.Kind && a.Data == b.Data; 00069 } 00070 00071 hash_value_type ComputeHash(const DeclNameKey &Key) const; 00072 internal_key_type GetInternalKey(const external_key_type& Name) const; 00073 00074 static std::pair<unsigned, unsigned> 00075 ReadKeyDataLength(const unsigned char*& d); 00076 00077 internal_key_type ReadKey(const unsigned char* d, unsigned); 00078 00079 data_type ReadData(internal_key_type, const unsigned char* d, 00080 unsigned DataLen); 00081 }; 00082 00083 /// \brief Base class for the trait describing the on-disk hash table for the 00084 /// identifiers in an AST file. 00085 /// 00086 /// This class is not useful by itself; rather, it provides common 00087 /// functionality for accessing the on-disk hash table of identifiers 00088 /// in an AST file. Different subclasses customize that functionality 00089 /// based on what information they are interested in. Those subclasses 00090 /// must provide the \c data_type typedef and the ReadData operation, 00091 /// only. 00092 class ASTIdentifierLookupTraitBase { 00093 public: 00094 typedef StringRef external_key_type; 00095 typedef StringRef internal_key_type; 00096 typedef unsigned hash_value_type; 00097 typedef unsigned offset_type; 00098 00099 static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { 00100 return a == b; 00101 } 00102 00103 static hash_value_type ComputeHash(const internal_key_type& a); 00104 00105 static std::pair<unsigned, unsigned> 00106 ReadKeyDataLength(const unsigned char*& d); 00107 00108 // This hopefully will just get inlined and removed by the optimizer. 00109 static const internal_key_type& 00110 GetInternalKey(const external_key_type& x) { return x; } 00111 00112 // This hopefully will just get inlined and removed by the optimizer. 00113 static const external_key_type& 00114 GetExternalKey(const internal_key_type& x) { return x; } 00115 00116 static internal_key_type ReadKey(const unsigned char* d, unsigned n); 00117 }; 00118 00119 /// \brief Class that performs lookup for an identifier stored in an AST file. 00120 class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase { 00121 ASTReader &Reader; 00122 ModuleFile &F; 00123 00124 // If we know the IdentifierInfo in advance, it is here and we will 00125 // not build a new one. Used when deserializing information about an 00126 // identifier that was constructed before the AST file was read. 00127 IdentifierInfo *KnownII; 00128 00129 public: 00130 typedef IdentifierInfo * data_type; 00131 00132 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F, 00133 IdentifierInfo *II = nullptr) 00134 : Reader(Reader), F(F), KnownII(II) { } 00135 00136 data_type ReadData(const internal_key_type& k, 00137 const unsigned char* d, 00138 unsigned DataLen); 00139 00140 ASTReader &getReader() const { return Reader; } 00141 }; 00142 00143 /// \brief The on-disk hash table used to contain information about 00144 /// all of the identifiers in the program. 00145 typedef llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait> 00146 ASTIdentifierLookupTable; 00147 00148 /// \brief Class that performs lookup for a selector's entries in the global 00149 /// method pool stored in an AST file. 00150 class ASTSelectorLookupTrait { 00151 ASTReader &Reader; 00152 ModuleFile &F; 00153 00154 public: 00155 struct data_type { 00156 SelectorID ID; 00157 unsigned InstanceBits; 00158 unsigned FactoryBits; 00159 SmallVector<ObjCMethodDecl *, 2> Instance; 00160 SmallVector<ObjCMethodDecl *, 2> Factory; 00161 }; 00162 00163 typedef Selector external_key_type; 00164 typedef external_key_type internal_key_type; 00165 typedef unsigned hash_value_type; 00166 typedef unsigned offset_type; 00167 00168 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F) 00169 : Reader(Reader), F(F) { } 00170 00171 static bool EqualKey(const internal_key_type& a, 00172 const internal_key_type& b) { 00173 return a == b; 00174 } 00175 00176 static hash_value_type ComputeHash(Selector Sel); 00177 00178 static const internal_key_type& 00179 GetInternalKey(const external_key_type& x) { return x; } 00180 00181 static std::pair<unsigned, unsigned> 00182 ReadKeyDataLength(const unsigned char*& d); 00183 00184 internal_key_type ReadKey(const unsigned char* d, unsigned); 00185 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen); 00186 }; 00187 00188 /// \brief The on-disk hash table used for the global method pool. 00189 typedef llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait> 00190 ASTSelectorLookupTable; 00191 00192 /// \brief Trait class used to search the on-disk hash table containing all of 00193 /// the header search information. 00194 /// 00195 /// The on-disk hash table contains a mapping from each header path to 00196 /// information about that header (how many times it has been included, its 00197 /// controlling macro, etc.). Note that we actually hash based on the 00198 /// filename, and support "deep" comparisons of file names based on current 00199 /// inode numbers, so that the search can cope with non-normalized path names 00200 /// and symlinks. 00201 class HeaderFileInfoTrait { 00202 ASTReader &Reader; 00203 ModuleFile &M; 00204 HeaderSearch *HS; 00205 const char *FrameworkStrings; 00206 00207 public: 00208 typedef const FileEntry *external_key_type; 00209 00210 struct internal_key_type { 00211 off_t Size; 00212 time_t ModTime; 00213 const char *Filename; 00214 }; 00215 typedef const internal_key_type &internal_key_ref; 00216 00217 typedef HeaderFileInfo data_type; 00218 typedef unsigned hash_value_type; 00219 typedef unsigned offset_type; 00220 00221 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS, 00222 const char *FrameworkStrings) 00223 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { } 00224 00225 static hash_value_type ComputeHash(internal_key_ref ikey); 00226 static internal_key_type GetInternalKey(const FileEntry *FE); 00227 bool EqualKey(internal_key_ref a, internal_key_ref b); 00228 00229 static std::pair<unsigned, unsigned> 00230 ReadKeyDataLength(const unsigned char*& d); 00231 00232 static internal_key_type ReadKey(const unsigned char *d, unsigned); 00233 00234 data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen); 00235 }; 00236 00237 /// \brief The on-disk hash table used for known header files. 00238 typedef llvm::OnDiskChainedHashTable<HeaderFileInfoTrait> 00239 HeaderFileInfoLookupTable; 00240 00241 } // end namespace clang::serialization::reader 00242 } // end namespace clang::serialization 00243 } // end namespace clang 00244 00245 00246 #endif