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