1 //===--- ASTReaderInternals.h - AST Reader Internals ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides internal definitions used in the AST reader.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_INTERNALS_H
14 #define LLVM_CLANG_SERIALIZATION_ASTREADER_INTERNALS_H
16 #include "clang/Basic/OnDiskHashTable.h"
17 #include "clang/AST/DeclarationName.h"
18 #include "llvm/Support/Endian.h"
26 struct HeaderFileInfo;
28 namespace serialization {
34 /// \brief Class that performs name lookup into a DeclContext stored
36 class ASTDeclContextNameLookupTrait {
41 /// \brief Pair of begin/end iterators for DeclIDs.
43 /// Note that these declaration IDs are local to the module that contains this
44 /// particular lookup t
45 typedef llvm::support::ulittle32_t LE32DeclID;
46 typedef std::pair<LE32DeclID *, LE32DeclID *> data_type;
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.
52 DeclarationName::NameKind Kind;
54 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
57 typedef DeclarationName external_key_type;
58 typedef DeclNameKey internal_key_type;
60 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
61 : Reader(Reader), F(F) { }
63 static bool EqualKey(const internal_key_type& a,
64 const internal_key_type& b) {
65 return a.Kind == b.Kind && a.Data == b.Data;
68 unsigned ComputeHash(const DeclNameKey &Key) const;
69 internal_key_type GetInternalKey(const external_key_type& Name) const;
71 static std::pair<unsigned, unsigned>
72 ReadKeyDataLength(const unsigned char*& d);
74 internal_key_type ReadKey(const unsigned char* d, unsigned);
76 data_type ReadData(internal_key_type, const unsigned char* d,
80 /// \brief Class that performs lookup for an identifier stored in an AST file.
81 class ASTIdentifierLookupTrait {
85 // If we know the IdentifierInfo in advance, it is here and we will
86 // not build a new one. Used when deserializing information about an
87 // identifier that was constructed before the AST file was read.
88 IdentifierInfo *KnownII;
91 typedef IdentifierInfo * data_type;
93 typedef const std::pair<const char*, unsigned> external_key_type;
95 typedef external_key_type internal_key_type;
97 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
98 IdentifierInfo *II = 0)
99 : Reader(Reader), F(F), KnownII(II) { }
101 static bool EqualKey(const internal_key_type& a,
102 const internal_key_type& b) {
103 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
107 static unsigned ComputeHash(const internal_key_type& a);
109 // This hopefully will just get inlined and removed by the optimizer.
110 static const internal_key_type&
111 GetInternalKey(const external_key_type& x) { return x; }
113 // This hopefully will just get inlined and removed by the optimizer.
114 static const external_key_type&
115 GetExternalKey(const internal_key_type& x) { return x; }
117 static std::pair<unsigned, unsigned>
118 ReadKeyDataLength(const unsigned char*& d);
120 static std::pair<const char*, unsigned>
121 ReadKey(const unsigned char* d, unsigned n);
123 IdentifierInfo *ReadData(const internal_key_type& k,
124 const unsigned char* d,
127 ASTReader &getReader() const { return Reader; }
131 /// \brief The on-disk hash table used to contain information about
132 /// all of the identifiers in the program.
133 typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
134 ASTIdentifierLookupTable;
136 /// \brief Class that performs lookup for a selector's entries in the global
137 /// method pool stored in an AST file.
138 class ASTSelectorLookupTrait {
145 llvm::SmallVector<ObjCMethodDecl *, 2> Instance;
146 llvm::SmallVector<ObjCMethodDecl *, 2> Factory;
149 typedef Selector external_key_type;
150 typedef external_key_type internal_key_type;
152 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
153 : Reader(Reader), F(F) { }
155 static bool EqualKey(const internal_key_type& a,
156 const internal_key_type& b) {
160 static unsigned ComputeHash(Selector Sel);
162 static const internal_key_type&
163 GetInternalKey(const external_key_type& x) { return x; }
165 static std::pair<unsigned, unsigned>
166 ReadKeyDataLength(const unsigned char*& d);
168 internal_key_type ReadKey(const unsigned char* d, unsigned);
169 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
172 /// \brief The on-disk hash table used for the global method pool.
173 typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
174 ASTSelectorLookupTable;
176 /// \brief Trait class used to search the on-disk hash table containing all of
177 /// the header search information.
179 /// The on-disk hash table contains a mapping from each header path to
180 /// information about that header (how many times it has been included, its
181 /// controlling macro, etc.). Note that we actually hash based on the
182 /// filename, and support "deep" comparisons of file names based on current
183 /// inode numbers, so that the search can cope with non-normalized path names
185 class HeaderFileInfoTrait {
189 const char *FrameworkStrings;
190 const char *SearchPath;
191 struct stat SearchPathStatBuf;
192 llvm::Optional<int> SearchPathStatResult;
194 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
195 if (Path == SearchPath) {
196 if (!SearchPathStatResult)
197 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
199 *StatBuf = SearchPathStatBuf;
200 return *SearchPathStatResult;
203 return stat(Path, StatBuf);
207 typedef const char *external_key_type;
208 typedef const char *internal_key_type;
210 typedef HeaderFileInfo data_type;
212 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
213 const char *FrameworkStrings,
214 const char *SearchPath = 0)
215 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings),
216 SearchPath(SearchPath) { }
218 static unsigned ComputeHash(const char *path);
219 static internal_key_type GetInternalKey(const char *path);
220 bool EqualKey(internal_key_type a, internal_key_type b);
222 static std::pair<unsigned, unsigned>
223 ReadKeyDataLength(const unsigned char*& d);
225 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
226 return (const char *)d;
229 data_type ReadData(const internal_key_type, const unsigned char *d,
233 /// \brief The on-disk hash table used for known header files.
234 typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
235 HeaderFileInfoLookupTable;
237 } // end namespace clang::serialization::reader
238 } // end namespace clang::serialization
239 } // end namespace clang