1 //===--- ASTReaderInternals.h - AST Reader Internals ------------*- 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 provides internal definitions used in the AST reader. 11 // 12 //===----------------------------------------------------------------------===// 13 #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H 14 #define LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H 15 16 #include "MultiOnDiskHashTable.h" 17 #include "clang/AST/DeclarationName.h" 18 #include "clang/Serialization/ASTBitCodes.h" 19 #include "llvm/ADT/DenseSet.h" 20 #include "llvm/Support/Endian.h" 21 #include "llvm/Support/OnDiskHashTable.h" 22 #include <utility> 23 24 namespace clang { 25 26 class ASTReader; 27 class HeaderSearch; 28 struct HeaderFileInfo; 29 class FileEntry; 30 31 namespace serialization { 32 33 class ModuleFile; 34 35 namespace reader { 36 37 /// \brief Class that performs name lookup into a DeclContext stored 38 /// in an AST file. 39 class ASTDeclContextNameLookupTrait { 40 ASTReader &Reader; 41 ModuleFile &F; 42 43 public: 44 // Maximum number of lookup tables we allow before condensing the tables. 45 static const int MaxTables = 4; 46 47 /// The lookup result is a list of global declaration IDs. 48 typedef llvm::SmallVector<DeclID, 4> data_type; 49 struct data_type_builder { 50 data_type &Data; 51 llvm::DenseSet<DeclID> Found; 52 53 data_type_builder(data_type &D) : Data(D) {} 54 void insert(DeclID ID) { 55 // Just use a linear scan unless we have more than a few IDs. 56 if (Found.empty() && !Data.empty()) { 57 if (Data.size() <= 4) { 58 for (auto I : Found) 59 if (I == ID) 60 return; 61 Data.push_back(ID); 62 return; 63 } 64 65 // Switch to tracking found IDs in the set. 66 Found.insert(Data.begin(), Data.end()); 67 } 68 69 if (Found.insert(ID).second) 70 Data.push_back(ID); 71 } 72 }; 73 typedef unsigned hash_value_type; 74 typedef unsigned offset_type; 75 typedef ModuleFile *file_type; 76 77 typedef DeclarationName external_key_type; 78 typedef DeclarationNameKey internal_key_type; 79 80 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F) 81 : Reader(Reader), F(F) { } 82 83 static bool EqualKey(const internal_key_type &a, const internal_key_type &b) { 84 return a == b; 85 } 86 87 static hash_value_type ComputeHash(const internal_key_type &Key) { 88 return Key.getHash(); 89 } 90 static internal_key_type GetInternalKey(const external_key_type &Name) { 91 return Name; 92 } 93 94 static std::pair<unsigned, unsigned> 95 ReadKeyDataLength(const unsigned char *&d); 96 97 internal_key_type ReadKey(const unsigned char *d, unsigned); 98 99 void ReadDataInto(internal_key_type, const unsigned char *d, 100 unsigned DataLen, data_type_builder &Val); 101 102 static void MergeDataInto(const data_type &From, data_type_builder &To) { 103 To.Data.reserve(To.Data.size() + From.size()); 104 for (DeclID ID : From) 105 To.insert(ID); 106 } 107 108 file_type ReadFileRef(const unsigned char *&d); 109 }; 110 111 struct DeclContextLookupTable { 112 MultiOnDiskHashTable<ASTDeclContextNameLookupTrait> Table; 113 114 // These look redundant, but don't remove them -- they work around MSVC 2013's 115 // inability to synthesize move operations. Without them, the 116 // MultiOnDiskHashTable will be copied (despite being move-only!). 117 DeclContextLookupTable() : Table() {} 118 DeclContextLookupTable(DeclContextLookupTable &&O) 119 : Table(std::move(O.Table)) {} 120 DeclContextLookupTable &operator=(DeclContextLookupTable &&O) { 121 Table = std::move(O.Table); 122 return *this; 123 } 124 }; 125 126 /// \brief Base class for the trait describing the on-disk hash table for the 127 /// identifiers in an AST file. 128 /// 129 /// This class is not useful by itself; rather, it provides common 130 /// functionality for accessing the on-disk hash table of identifiers 131 /// in an AST file. Different subclasses customize that functionality 132 /// based on what information they are interested in. Those subclasses 133 /// must provide the \c data_type typedef and the ReadData operation, 134 /// only. 135 class ASTIdentifierLookupTraitBase { 136 public: 137 typedef StringRef external_key_type; 138 typedef StringRef internal_key_type; 139 typedef unsigned hash_value_type; 140 typedef unsigned offset_type; 141 142 static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { 143 return a == b; 144 } 145 146 static hash_value_type ComputeHash(const internal_key_type& a); 147 148 static std::pair<unsigned, unsigned> 149 ReadKeyDataLength(const unsigned char*& d); 150 151 // This hopefully will just get inlined and removed by the optimizer. 152 static const internal_key_type& 153 GetInternalKey(const external_key_type& x) { return x; } 154 155 // This hopefully will just get inlined and removed by the optimizer. 156 static const external_key_type& 157 GetExternalKey(const internal_key_type& x) { return x; } 158 159 static internal_key_type ReadKey(const unsigned char* d, unsigned n); 160 }; 161 162 /// \brief Class that performs lookup for an identifier stored in an AST file. 163 class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase { 164 ASTReader &Reader; 165 ModuleFile &F; 166 167 // If we know the IdentifierInfo in advance, it is here and we will 168 // not build a new one. Used when deserializing information about an 169 // identifier that was constructed before the AST file was read. 170 IdentifierInfo *KnownII; 171 172 public: 173 typedef IdentifierInfo * data_type; 174 175 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F, 176 IdentifierInfo *II = nullptr) 177 : Reader(Reader), F(F), KnownII(II) { } 178 179 data_type ReadData(const internal_key_type& k, 180 const unsigned char* d, 181 unsigned DataLen); 182 183 IdentID ReadIdentifierID(const unsigned char *d); 184 185 ASTReader &getReader() const { return Reader; } 186 }; 187 188 /// \brief The on-disk hash table used to contain information about 189 /// all of the identifiers in the program. 190 typedef llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait> 191 ASTIdentifierLookupTable; 192 193 /// \brief Class that performs lookup for a selector's entries in the global 194 /// method pool stored in an AST file. 195 class ASTSelectorLookupTrait { 196 ASTReader &Reader; 197 ModuleFile &F; 198 199 public: 200 struct data_type { 201 SelectorID ID; 202 unsigned InstanceBits; 203 unsigned FactoryBits; 204 bool InstanceHasMoreThanOneDecl; 205 bool FactoryHasMoreThanOneDecl; 206 SmallVector<ObjCMethodDecl *, 2> Instance; 207 SmallVector<ObjCMethodDecl *, 2> Factory; 208 }; 209 210 typedef Selector external_key_type; 211 typedef external_key_type internal_key_type; 212 typedef unsigned hash_value_type; 213 typedef unsigned offset_type; 214 215 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F) 216 : Reader(Reader), F(F) { } 217 218 static bool EqualKey(const internal_key_type& a, 219 const internal_key_type& b) { 220 return a == b; 221 } 222 223 static hash_value_type ComputeHash(Selector Sel); 224 225 static const internal_key_type& 226 GetInternalKey(const external_key_type& x) { return x; } 227 228 static std::pair<unsigned, unsigned> 229 ReadKeyDataLength(const unsigned char*& d); 230 231 internal_key_type ReadKey(const unsigned char* d, unsigned); 232 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen); 233 }; 234 235 /// \brief The on-disk hash table used for the global method pool. 236 typedef llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait> 237 ASTSelectorLookupTable; 238 239 /// \brief Trait class used to search the on-disk hash table containing all of 240 /// the header search information. 241 /// 242 /// The on-disk hash table contains a mapping from each header path to 243 /// information about that header (how many times it has been included, its 244 /// controlling macro, etc.). Note that we actually hash based on the size 245 /// and mtime, and support "deep" comparisons of file names based on current 246 /// inode numbers, so that the search can cope with non-normalized path names 247 /// and symlinks. 248 class HeaderFileInfoTrait { 249 ASTReader &Reader; 250 ModuleFile &M; 251 HeaderSearch *HS; 252 const char *FrameworkStrings; 253 254 public: 255 typedef const FileEntry *external_key_type; 256 257 struct internal_key_type { 258 off_t Size; 259 time_t ModTime; 260 const char *Filename; 261 bool Imported; 262 }; 263 typedef const internal_key_type &internal_key_ref; 264 265 typedef HeaderFileInfo data_type; 266 typedef unsigned hash_value_type; 267 typedef unsigned offset_type; 268 269 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS, 270 const char *FrameworkStrings) 271 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { } 272 273 static hash_value_type ComputeHash(internal_key_ref ikey); 274 internal_key_type GetInternalKey(const FileEntry *FE); 275 bool EqualKey(internal_key_ref a, internal_key_ref b); 276 277 static std::pair<unsigned, unsigned> 278 ReadKeyDataLength(const unsigned char*& d); 279 280 static internal_key_type ReadKey(const unsigned char *d, unsigned); 281 282 data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen); 283 }; 284 285 /// \brief The on-disk hash table used for known header files. 286 typedef llvm::OnDiskChainedHashTable<HeaderFileInfoTrait> 287 HeaderFileInfoLookupTable; 288 289 } // end namespace clang::serialization::reader 290 } // end namespace clang::serialization 291 } // end namespace clang 292 293 294 #endif 295