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