Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 13 | #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H |
| 14 | #define LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 15 | |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 16 | #include "MultiOnDiskHashTable.h" |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclarationName.h" |
Douglas Gregor | bfd73d7 | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 18 | #include "clang/Serialization/ASTBitCodes.h" |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | de95ead | 2012-01-06 16:09:53 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Endian.h" |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 21 | #include "llvm/Support/OnDiskHashTable.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include <utility> |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 23 | |
| 24 | namespace clang { |
| 25 | |
| 26 | class ASTReader; |
| 27 | class HeaderSearch; |
| 28 | struct HeaderFileInfo; |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 29 | class FileEntry; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 30 | |
| 31 | namespace serialization { |
| 32 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 33 | class ModuleFile; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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; |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 41 | ModuleFile &F; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 42 | |
| 43 | public: |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 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 | }; |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 73 | typedef unsigned hash_value_type; |
| 74 | typedef unsigned offset_type; |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 75 | typedef ModuleFile *file_type; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 76 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 77 | typedef DeclarationName external_key_type; |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 78 | typedef DeclarationNameKey internal_key_type; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 79 | |
Nick Lewycky | 2bd0ab2 | 2012-04-16 02:51:46 +0000 | [diff] [blame] | 80 | explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F) |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 81 | : Reader(Reader), F(F) { } |
| 82 | |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 83 | static bool EqualKey(const internal_key_type &a, const internal_key_type &b) { |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 84 | return a == b; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 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 | } |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 93 | |
Nick Lewycky | 2bd0ab2 | 2012-04-16 02:51:46 +0000 | [diff] [blame] | 94 | static std::pair<unsigned, unsigned> |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 95 | ReadKeyDataLength(const unsigned char *&d); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 96 | |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 97 | internal_key_type ReadKey(const unsigned char *d, unsigned); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 98 | |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 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; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Douglas Gregor | bfd73d7 | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 115 | /// \brief Base class for the trait describing the on-disk hash table for the |
| 116 | /// identifiers in an AST file. |
| 117 | /// |
| 118 | /// This class is not useful by itself; rather, it provides common |
| 119 | /// functionality for accessing the on-disk hash table of identifiers |
| 120 | /// in an AST file. Different subclasses customize that functionality |
| 121 | /// based on what information they are interested in. Those subclasses |
| 122 | /// must provide the \c data_type typedef and the ReadData operation, |
| 123 | /// only. |
| 124 | class ASTIdentifierLookupTraitBase { |
| 125 | public: |
| 126 | typedef StringRef external_key_type; |
| 127 | typedef StringRef internal_key_type; |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 128 | typedef unsigned hash_value_type; |
| 129 | typedef unsigned offset_type; |
Douglas Gregor | bfd73d7 | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 130 | |
| 131 | static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { |
| 132 | return a == b; |
| 133 | } |
| 134 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 135 | static hash_value_type ComputeHash(const internal_key_type& a); |
Douglas Gregor | bfd73d7 | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 136 | |
| 137 | static std::pair<unsigned, unsigned> |
| 138 | ReadKeyDataLength(const unsigned char*& d); |
| 139 | |
| 140 | // This hopefully will just get inlined and removed by the optimizer. |
| 141 | static const internal_key_type& |
| 142 | GetInternalKey(const external_key_type& x) { return x; } |
| 143 | |
| 144 | // This hopefully will just get inlined and removed by the optimizer. |
| 145 | static const external_key_type& |
| 146 | GetExternalKey(const internal_key_type& x) { return x; } |
| 147 | |
| 148 | static internal_key_type ReadKey(const unsigned char* d, unsigned n); |
| 149 | }; |
| 150 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 151 | /// \brief Class that performs lookup for an identifier stored in an AST file. |
Douglas Gregor | bfd73d7 | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 152 | class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase { |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 153 | ASTReader &Reader; |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 154 | ModuleFile &F; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 155 | |
| 156 | // If we know the IdentifierInfo in advance, it is here and we will |
| 157 | // not build a new one. Used when deserializing information about an |
| 158 | // identifier that was constructed before the AST file was read. |
| 159 | IdentifierInfo *KnownII; |
| 160 | |
| 161 | public: |
| 162 | typedef IdentifierInfo * data_type; |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 163 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 164 | ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F, |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 165 | IdentifierInfo *II = nullptr) |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 166 | : Reader(Reader), F(F), KnownII(II) { } |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 167 | |
Douglas Gregor | bfd73d7 | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 168 | data_type ReadData(const internal_key_type& k, |
| 169 | const unsigned char* d, |
| 170 | unsigned DataLen); |
Douglas Gregor | 247afcc | 2012-01-24 15:24:38 +0000 | [diff] [blame] | 171 | |
Richard Smith | 79bf920 | 2015-08-24 03:33:22 +0000 | [diff] [blame] | 172 | IdentID ReadIdentifierID(const unsigned char *d); |
| 173 | |
Douglas Gregor | 247afcc | 2012-01-24 15:24:38 +0000 | [diff] [blame] | 174 | ASTReader &getReader() const { return Reader; } |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | /// \brief The on-disk hash table used to contain information about |
| 178 | /// all of the identifiers in the program. |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 179 | typedef llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait> |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 180 | ASTIdentifierLookupTable; |
| 181 | |
| 182 | /// \brief Class that performs lookup for a selector's entries in the global |
| 183 | /// method pool stored in an AST file. |
| 184 | class ASTSelectorLookupTrait { |
| 185 | ASTReader &Reader; |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 186 | ModuleFile &F; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 187 | |
| 188 | public: |
| 189 | struct data_type { |
| 190 | SelectorID ID; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 191 | unsigned InstanceBits; |
| 192 | unsigned FactoryBits; |
Nico Weber | ff4b35e | 2014-12-27 22:14:15 +0000 | [diff] [blame] | 193 | bool InstanceHasMoreThanOneDecl; |
| 194 | bool FactoryHasMoreThanOneDecl; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 195 | SmallVector<ObjCMethodDecl *, 2> Instance; |
| 196 | SmallVector<ObjCMethodDecl *, 2> Factory; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | typedef Selector external_key_type; |
| 200 | typedef external_key_type internal_key_type; |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 201 | typedef unsigned hash_value_type; |
| 202 | typedef unsigned offset_type; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 204 | ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F) |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 205 | : Reader(Reader), F(F) { } |
| 206 | |
| 207 | static bool EqualKey(const internal_key_type& a, |
| 208 | const internal_key_type& b) { |
| 209 | return a == b; |
| 210 | } |
| 211 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 212 | static hash_value_type ComputeHash(Selector Sel); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 213 | |
| 214 | static const internal_key_type& |
Chris Lattner | d2cd41c | 2011-09-10 16:13:42 +0000 | [diff] [blame] | 215 | GetInternalKey(const external_key_type& x) { return x; } |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 216 | |
| 217 | static std::pair<unsigned, unsigned> |
| 218 | ReadKeyDataLength(const unsigned char*& d); |
| 219 | |
| 220 | internal_key_type ReadKey(const unsigned char* d, unsigned); |
| 221 | data_type ReadData(Selector, const unsigned char* d, unsigned DataLen); |
| 222 | }; |
| 223 | |
| 224 | /// \brief The on-disk hash table used for the global method pool. |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 225 | typedef llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait> |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 226 | ASTSelectorLookupTable; |
| 227 | |
| 228 | /// \brief Trait class used to search the on-disk hash table containing all of |
| 229 | /// the header search information. |
| 230 | /// |
| 231 | /// The on-disk hash table contains a mapping from each header path to |
| 232 | /// information about that header (how many times it has been included, its |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 233 | /// controlling macro, etc.). Note that we actually hash based on the size |
| 234 | /// and mtime, and support "deep" comparisons of file names based on current |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 235 | /// inode numbers, so that the search can cope with non-normalized path names |
| 236 | /// and symlinks. |
| 237 | class HeaderFileInfoTrait { |
| 238 | ASTReader &Reader; |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 239 | ModuleFile &M; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 240 | HeaderSearch *HS; |
| 241 | const char *FrameworkStrings; |
Ted Kremenek | 03cb137 | 2013-02-05 06:21:59 +0000 | [diff] [blame] | 242 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 243 | public: |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 244 | typedef const FileEntry *external_key_type; |
| 245 | |
| 246 | struct internal_key_type { |
| 247 | off_t Size; |
| 248 | time_t ModTime; |
Mehdi Amini | 004b9c7 | 2016-10-10 22:52:47 +0000 | [diff] [blame] | 249 | StringRef Filename; |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 250 | bool Imported; |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 251 | }; |
| 252 | typedef const internal_key_type &internal_key_ref; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 253 | |
| 254 | typedef HeaderFileInfo data_type; |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 255 | typedef unsigned hash_value_type; |
| 256 | typedef unsigned offset_type; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 257 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 258 | HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS, |
Argyrios Kyrtzidis | b42863e | 2013-03-06 18:12:41 +0000 | [diff] [blame] | 259 | const char *FrameworkStrings) |
| 260 | : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { } |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 261 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 262 | static hash_value_type ComputeHash(internal_key_ref ikey); |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 263 | internal_key_type GetInternalKey(const FileEntry *FE); |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 264 | bool EqualKey(internal_key_ref a, internal_key_ref b); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 265 | |
| 266 | static std::pair<unsigned, unsigned> |
| 267 | ReadKeyDataLength(const unsigned char*& d); |
| 268 | |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 269 | static internal_key_type ReadKey(const unsigned char *d, unsigned); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 270 | |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 271 | data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | /// \brief The on-disk hash table used for known header files. |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 275 | typedef llvm::OnDiskChainedHashTable<HeaderFileInfoTrait> |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 276 | HeaderFileInfoLookupTable; |
| 277 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 278 | } // end namespace clang::serialization::reader |
| 279 | } // end namespace clang::serialization |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 280 | } // end namespace clang |
| 281 | |
| 282 | |
| 283 | #endif |