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); |
Douglas Gregor | 5d5051f | 2012-01-24 15:24:38 +0000 | [diff] [blame^] | 132 | |
| 133 | ASTReader &getReader() const { return Reader; } |
| 134 | |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /// \brief The on-disk hash table used to contain information about |
| 138 | /// all of the identifiers in the program. |
| 139 | typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait> |
| 140 | ASTIdentifierLookupTable; |
| 141 | |
| 142 | /// \brief Class that performs lookup for a selector's entries in the global |
| 143 | /// method pool stored in an AST file. |
| 144 | class ASTSelectorLookupTrait { |
| 145 | ASTReader &Reader; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 146 | ModuleFile &F; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 147 | |
| 148 | public: |
| 149 | struct data_type { |
| 150 | SelectorID ID; |
| 151 | llvm::SmallVector<ObjCMethodDecl *, 2> Instance; |
| 152 | llvm::SmallVector<ObjCMethodDecl *, 2> Factory; |
| 153 | }; |
| 154 | |
| 155 | typedef Selector external_key_type; |
| 156 | typedef external_key_type internal_key_type; |
| 157 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 158 | ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F) |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 159 | : Reader(Reader), F(F) { } |
| 160 | |
| 161 | static bool EqualKey(const internal_key_type& a, |
| 162 | const internal_key_type& b) { |
| 163 | return a == b; |
| 164 | } |
| 165 | |
| 166 | static unsigned ComputeHash(Selector Sel); |
| 167 | |
| 168 | static const internal_key_type& |
Chris Lattner | a2f4ae8 | 2011-09-10 16:13:42 +0000 | [diff] [blame] | 169 | GetInternalKey(const external_key_type& x) { return x; } |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 170 | |
| 171 | static std::pair<unsigned, unsigned> |
| 172 | ReadKeyDataLength(const unsigned char*& d); |
| 173 | |
| 174 | internal_key_type ReadKey(const unsigned char* d, unsigned); |
| 175 | data_type ReadData(Selector, const unsigned char* d, unsigned DataLen); |
| 176 | }; |
| 177 | |
| 178 | /// \brief The on-disk hash table used for the global method pool. |
| 179 | typedef OnDiskChainedHashTable<ASTSelectorLookupTrait> |
| 180 | ASTSelectorLookupTable; |
| 181 | |
| 182 | /// \brief Trait class used to search the on-disk hash table containing all of |
| 183 | /// the header search information. |
| 184 | /// |
| 185 | /// The on-disk hash table contains a mapping from each header path to |
| 186 | /// information about that header (how many times it has been included, its |
| 187 | /// controlling macro, etc.). Note that we actually hash based on the |
| 188 | /// filename, and support "deep" comparisons of file names based on current |
| 189 | /// inode numbers, so that the search can cope with non-normalized path names |
| 190 | /// and symlinks. |
| 191 | class HeaderFileInfoTrait { |
| 192 | ASTReader &Reader; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 193 | ModuleFile &M; |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 194 | HeaderSearch *HS; |
| 195 | const char *FrameworkStrings; |
| 196 | const char *SearchPath; |
| 197 | struct stat SearchPathStatBuf; |
| 198 | llvm::Optional<int> SearchPathStatResult; |
| 199 | |
| 200 | int StatSimpleCache(const char *Path, struct stat *StatBuf) { |
| 201 | if (Path == SearchPath) { |
| 202 | if (!SearchPathStatResult) |
| 203 | SearchPathStatResult = stat(Path, &SearchPathStatBuf); |
| 204 | |
| 205 | *StatBuf = SearchPathStatBuf; |
| 206 | return *SearchPathStatResult; |
| 207 | } |
| 208 | |
| 209 | return stat(Path, StatBuf); |
| 210 | } |
| 211 | |
| 212 | public: |
| 213 | typedef const char *external_key_type; |
| 214 | typedef const char *internal_key_type; |
| 215 | |
| 216 | typedef HeaderFileInfo data_type; |
| 217 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 218 | HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS, |
Douglas Gregor | 98339b9 | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 219 | const char *FrameworkStrings, |
| 220 | const char *SearchPath = 0) |
| 221 | : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings), |
| 222 | SearchPath(SearchPath) { } |
| 223 | |
| 224 | static unsigned ComputeHash(const char *path); |
| 225 | static internal_key_type GetInternalKey(const char *path); |
| 226 | bool EqualKey(internal_key_type a, internal_key_type b); |
| 227 | |
| 228 | static std::pair<unsigned, unsigned> |
| 229 | ReadKeyDataLength(const unsigned char*& d); |
| 230 | |
| 231 | static internal_key_type ReadKey(const unsigned char *d, unsigned) { |
| 232 | return (const char *)d; |
| 233 | } |
| 234 | |
| 235 | data_type ReadData(const internal_key_type, const unsigned char *d, |
| 236 | unsigned DataLen); |
| 237 | }; |
| 238 | |
| 239 | /// \brief The on-disk hash table used for known header files. |
| 240 | typedef OnDiskChainedHashTable<HeaderFileInfoTrait> |
| 241 | HeaderFileInfoLookupTable; |
| 242 | |
| 243 | } // end namespace clang::serialization::reader |
| 244 | } // end namespace clang::serialization |
| 245 | } // end namespace clang |
| 246 | |
| 247 | |
| 248 | #endif |