Douglas Gregor | 98339b9 | 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 | //===----------------------------------------------------------------------===// |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13 | #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H |
| 14 | #define LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 15 | |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclarationName.h" |
Douglas Gregor | 479633c | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 17 | #include "clang/Serialization/ASTBitCodes.h" |
Douglas Gregor | 9b8b20f | 2012-01-06 16:09:53 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Endian.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 19 | #include "llvm/Support/OnDiskHashTable.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include <utility> |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 21 | |
| 22 | namespace clang { |
| 23 | |
| 24 | class ASTReader; |
| 25 | class HeaderSearch; |
| 26 | struct HeaderFileInfo; |
Argyrios Kyrtzidis | ed3802e | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 27 | class FileEntry; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 28 | |
| 29 | namespace serialization { |
| 30 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 31 | class ModuleFile; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 32 | |
| 33 | namespace reader { |
| 34 | |
| 35 | /// \brief Class that performs name lookup into a DeclContext stored |
| 36 | /// in an AST file. |
| 37 | class ASTDeclContextNameLookupTrait { |
| 38 | ASTReader &Reader; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 39 | ModuleFile &F; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 40 | |
| 41 | public: |
| 42 | /// \brief Pair of begin/end iterators for DeclIDs. |
| 43 | /// |
| 44 | /// Note that these declaration IDs are local to the module that contains this |
| 45 | /// particular lookup t |
Douglas Gregor | 9b8b20f | 2012-01-06 16:09:53 +0000 | [diff] [blame] | 46 | typedef llvm::support::ulittle32_t LE32DeclID; |
| 47 | typedef std::pair<LE32DeclID *, LE32DeclID *> data_type; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 48 | typedef unsigned hash_value_type; |
| 49 | typedef unsigned offset_type; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 50 | |
| 51 | /// \brief Special internal key for declaration names. |
| 52 | /// The hash table creates keys for comparison; we do not create |
| 53 | /// a DeclarationName for the internal key to avoid deserializing types. |
| 54 | struct DeclNameKey { |
| 55 | DeclarationName::NameKind Kind; |
| 56 | uint64_t Data; |
| 57 | DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { } |
| 58 | }; |
| 59 | |
| 60 | typedef DeclarationName external_key_type; |
| 61 | typedef DeclNameKey internal_key_type; |
| 62 | |
Nick Lewycky | b346d2f | 2012-04-16 02:51:46 +0000 | [diff] [blame] | 63 | explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F) |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 64 | : Reader(Reader), F(F) { } |
| 65 | |
| 66 | static bool EqualKey(const internal_key_type& a, |
| 67 | const internal_key_type& b) { |
| 68 | return a.Kind == b.Kind && a.Data == b.Data; |
| 69 | } |
| 70 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 71 | hash_value_type ComputeHash(const DeclNameKey &Key) const; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 72 | internal_key_type GetInternalKey(const external_key_type& Name) const; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 73 | |
Nick Lewycky | b346d2f | 2012-04-16 02:51:46 +0000 | [diff] [blame] | 74 | static std::pair<unsigned, unsigned> |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 75 | ReadKeyDataLength(const unsigned char*& d); |
| 76 | |
| 77 | internal_key_type ReadKey(const unsigned char* d, unsigned); |
| 78 | |
| 79 | data_type ReadData(internal_key_type, const unsigned char* d, |
| 80 | unsigned DataLen); |
| 81 | }; |
| 82 | |
Douglas Gregor | 479633c | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 83 | /// \brief Base class for the trait describing the on-disk hash table for the |
| 84 | /// identifiers in an AST file. |
| 85 | /// |
| 86 | /// This class is not useful by itself; rather, it provides common |
| 87 | /// functionality for accessing the on-disk hash table of identifiers |
| 88 | /// in an AST file. Different subclasses customize that functionality |
| 89 | /// based on what information they are interested in. Those subclasses |
| 90 | /// must provide the \c data_type typedef and the ReadData operation, |
| 91 | /// only. |
| 92 | class ASTIdentifierLookupTraitBase { |
| 93 | public: |
| 94 | typedef StringRef external_key_type; |
| 95 | typedef StringRef internal_key_type; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 96 | typedef unsigned hash_value_type; |
| 97 | typedef unsigned offset_type; |
Douglas Gregor | 479633c | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 98 | |
| 99 | static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { |
| 100 | return a == b; |
| 101 | } |
| 102 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 103 | static hash_value_type ComputeHash(const internal_key_type& a); |
Douglas Gregor | 479633c | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 104 | |
| 105 | static std::pair<unsigned, unsigned> |
| 106 | ReadKeyDataLength(const unsigned char*& d); |
| 107 | |
| 108 | // This hopefully will just get inlined and removed by the optimizer. |
| 109 | static const internal_key_type& |
| 110 | GetInternalKey(const external_key_type& x) { return x; } |
| 111 | |
| 112 | // This hopefully will just get inlined and removed by the optimizer. |
| 113 | static const external_key_type& |
| 114 | GetExternalKey(const internal_key_type& x) { return x; } |
| 115 | |
| 116 | static internal_key_type ReadKey(const unsigned char* d, unsigned n); |
| 117 | }; |
| 118 | |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 119 | /// \brief Class that performs lookup for an identifier stored in an AST file. |
Douglas Gregor | 479633c | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 120 | class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase { |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 121 | ASTReader &Reader; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 122 | ModuleFile &F; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 123 | |
| 124 | // If we know the IdentifierInfo in advance, it is here and we will |
| 125 | // not build a new one. Used when deserializing information about an |
| 126 | // identifier that was constructed before the AST file was read. |
| 127 | IdentifierInfo *KnownII; |
| 128 | |
| 129 | public: |
| 130 | typedef IdentifierInfo * data_type; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 131 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 132 | ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 133 | IdentifierInfo *II = nullptr) |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 134 | : Reader(Reader), F(F), KnownII(II) { } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 135 | |
Douglas Gregor | 479633c | 2013-01-23 18:53:14 +0000 | [diff] [blame] | 136 | data_type ReadData(const internal_key_type& k, |
| 137 | const unsigned char* d, |
| 138 | unsigned DataLen); |
Douglas Gregor | 5d5051f | 2012-01-24 15:24:38 +0000 | [diff] [blame] | 139 | |
| 140 | ASTReader &getReader() const { return Reader; } |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /// \brief The on-disk hash table used to contain information about |
| 144 | /// all of the identifiers in the program. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 145 | typedef llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait> |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 146 | ASTIdentifierLookupTable; |
| 147 | |
| 148 | /// \brief Class that performs lookup for a selector's entries in the global |
| 149 | /// method pool stored in an AST file. |
| 150 | class ASTSelectorLookupTrait { |
| 151 | ASTReader &Reader; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 152 | ModuleFile &F; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 153 | |
| 154 | public: |
| 155 | struct data_type { |
| 156 | SelectorID ID; |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 157 | unsigned InstanceBits; |
| 158 | unsigned FactoryBits; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 159 | bool InstanceHasMoreThanOneDecl; |
| 160 | bool FactoryHasMoreThanOneDecl; |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 161 | SmallVector<ObjCMethodDecl *, 2> Instance; |
| 162 | SmallVector<ObjCMethodDecl *, 2> Factory; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | typedef Selector external_key_type; |
| 166 | typedef external_key_type internal_key_type; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 167 | typedef unsigned hash_value_type; |
| 168 | typedef unsigned offset_type; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 170 | ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F) |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 171 | : Reader(Reader), F(F) { } |
| 172 | |
| 173 | static bool EqualKey(const internal_key_type& a, |
| 174 | const internal_key_type& b) { |
| 175 | return a == b; |
| 176 | } |
| 177 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 178 | static hash_value_type ComputeHash(Selector Sel); |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 179 | |
| 180 | static const internal_key_type& |
Chris Lattner | a2f4ae8 | 2011-09-10 16:13:42 +0000 | [diff] [blame] | 181 | GetInternalKey(const external_key_type& x) { return x; } |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 182 | |
| 183 | static std::pair<unsigned, unsigned> |
| 184 | ReadKeyDataLength(const unsigned char*& d); |
| 185 | |
| 186 | internal_key_type ReadKey(const unsigned char* d, unsigned); |
| 187 | data_type ReadData(Selector, const unsigned char* d, unsigned DataLen); |
| 188 | }; |
| 189 | |
| 190 | /// \brief The on-disk hash table used for the global method pool. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 191 | typedef llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait> |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 192 | ASTSelectorLookupTable; |
| 193 | |
| 194 | /// \brief Trait class used to search the on-disk hash table containing all of |
| 195 | /// the header search information. |
| 196 | /// |
| 197 | /// The on-disk hash table contains a mapping from each header path to |
| 198 | /// information about that header (how many times it has been included, its |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 199 | /// controlling macro, etc.). Note that we actually hash based on the size |
| 200 | /// and mtime, and support "deep" comparisons of file names based on current |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 201 | /// inode numbers, so that the search can cope with non-normalized path names |
| 202 | /// and symlinks. |
| 203 | class HeaderFileInfoTrait { |
| 204 | ASTReader &Reader; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 205 | ModuleFile &M; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 206 | HeaderSearch *HS; |
| 207 | const char *FrameworkStrings; |
Ted Kremenek | 4fd83a3 | 2013-02-05 06:21:59 +0000 | [diff] [blame] | 208 | |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 209 | public: |
Argyrios Kyrtzidis | ed3802e | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 210 | typedef const FileEntry *external_key_type; |
| 211 | |
| 212 | struct internal_key_type { |
| 213 | off_t Size; |
| 214 | time_t ModTime; |
| 215 | const char *Filename; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 216 | bool Imported; |
Argyrios Kyrtzidis | ed3802e | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 217 | }; |
| 218 | typedef const internal_key_type &internal_key_ref; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 219 | |
| 220 | typedef HeaderFileInfo data_type; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 221 | typedef unsigned hash_value_type; |
| 222 | typedef unsigned offset_type; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 223 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 224 | HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS, |
Argyrios Kyrtzidis | 8bd50b1 | 2013-03-06 18:12:41 +0000 | [diff] [blame] | 225 | const char *FrameworkStrings) |
| 226 | : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { } |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 227 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 228 | static hash_value_type ComputeHash(internal_key_ref ikey); |
Argyrios Kyrtzidis | ed3802e | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 229 | static internal_key_type GetInternalKey(const FileEntry *FE); |
| 230 | bool EqualKey(internal_key_ref a, internal_key_ref b); |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 231 | |
| 232 | static std::pair<unsigned, unsigned> |
| 233 | ReadKeyDataLength(const unsigned char*& d); |
| 234 | |
Argyrios Kyrtzidis | ed3802e | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 235 | static internal_key_type ReadKey(const unsigned char *d, unsigned); |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 236 | |
Argyrios Kyrtzidis | ed3802e | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 237 | data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen); |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | /// \brief The on-disk hash table used for known header files. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 241 | typedef llvm::OnDiskChainedHashTable<HeaderFileInfoTrait> |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 242 | HeaderFileInfoLookupTable; |
| 243 | |
| 244 | } // end namespace clang::serialization::reader |
| 245 | } // end namespace clang::serialization |
| 246 | } // end namespace clang |
| 247 | |
| 248 | |
| 249 | #endif |