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"
25 struct HeaderFileInfo;
27 namespace serialization {
33 /// \brief Class that performs name lookup into a DeclContext stored
35 class ASTDeclContextNameLookupTrait {
40 /// \brief Pair of begin/end iterators for DeclIDs.
42 /// Note that these declaration IDs are local to the module that contains this
43 /// particular lookup t
44 typedef std::pair<DeclID *, DeclID *> data_type;
46 /// \brief Special internal key for declaration names.
47 /// The hash table creates keys for comparison; we do not create
48 /// a DeclarationName for the internal key to avoid deserializing types.
50 DeclarationName::NameKind Kind;
52 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
55 typedef DeclarationName external_key_type;
56 typedef DeclNameKey internal_key_type;
58 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader,
60 : Reader(Reader), F(F) { }
62 static bool EqualKey(const internal_key_type& a,
63 const internal_key_type& b) {
64 return a.Kind == b.Kind && a.Data == b.Data;
67 unsigned ComputeHash(const DeclNameKey &Key) const;
68 internal_key_type GetInternalKey(const external_key_type& Name) const;
69 external_key_type GetExternalKey(const internal_key_type& Key) 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 The on-disk hash table used for the DeclContext's Name lookup table.
81 typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
82 ASTDeclContextNameLookupTable;
84 /// \brief Class that performs lookup for an identifier stored in an AST file.
85 class ASTIdentifierLookupTrait {
89 // If we know the IdentifierInfo in advance, it is here and we will
90 // not build a new one. Used when deserializing information about an
91 // identifier that was constructed before the AST file was read.
92 IdentifierInfo *KnownII;
95 typedef IdentifierInfo * data_type;
97 typedef const std::pair<const char*, unsigned> external_key_type;
99 typedef external_key_type internal_key_type;
101 ASTIdentifierLookupTrait(ASTReader &Reader, Module &F,
102 IdentifierInfo *II = 0)
103 : Reader(Reader), F(F), KnownII(II) { }
105 static bool EqualKey(const internal_key_type& a,
106 const internal_key_type& b) {
107 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
111 static unsigned ComputeHash(const internal_key_type& a);
113 // This hopefully will just get inlined and removed by the optimizer.
114 static const internal_key_type&
115 GetInternalKey(const external_key_type& x) { return x; }
117 // This hopefully will just get inlined and removed by the optimizer.
118 static const external_key_type&
119 GetExternalKey(const internal_key_type& x) { return x; }
121 static std::pair<unsigned, unsigned>
122 ReadKeyDataLength(const unsigned char*& d);
124 static std::pair<const char*, unsigned>
125 ReadKey(const unsigned char* d, unsigned n);
127 IdentifierInfo *ReadData(const internal_key_type& k,
128 const unsigned char* d,
132 /// \brief The on-disk hash table used to contain information about
133 /// all of the identifiers in the program.
134 typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
135 ASTIdentifierLookupTable;
137 /// \brief Class that performs lookup for a selector's entries in the global
138 /// method pool stored in an AST file.
139 class ASTSelectorLookupTrait {
146 llvm::SmallVector<ObjCMethodDecl *, 2> Instance;
147 llvm::SmallVector<ObjCMethodDecl *, 2> Factory;
150 typedef Selector external_key_type;
151 typedef external_key_type internal_key_type;
153 ASTSelectorLookupTrait(ASTReader &Reader, Module &F)
154 : Reader(Reader), F(F) { }
156 static bool EqualKey(const internal_key_type& a,
157 const internal_key_type& b) {
161 static unsigned ComputeHash(Selector Sel);
163 static const internal_key_type&
164 GetInternalKey(const external_key_type& x) { return x; }
166 static std::pair<unsigned, unsigned>
167 ReadKeyDataLength(const unsigned char*& d);
169 internal_key_type ReadKey(const unsigned char* d, unsigned);
170 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
173 /// \brief The on-disk hash table used for the global method pool.
174 typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
175 ASTSelectorLookupTable;
177 /// \brief Trait class used to search the on-disk hash table containing all of
178 /// the header search information.
180 /// The on-disk hash table contains a mapping from each header path to
181 /// information about that header (how many times it has been included, its
182 /// controlling macro, etc.). Note that we actually hash based on the
183 /// filename, and support "deep" comparisons of file names based on current
184 /// inode numbers, so that the search can cope with non-normalized path names
186 class HeaderFileInfoTrait {
190 const char *FrameworkStrings;
191 const char *SearchPath;
192 struct stat SearchPathStatBuf;
193 llvm::Optional<int> SearchPathStatResult;
195 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
196 if (Path == SearchPath) {
197 if (!SearchPathStatResult)
198 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
200 *StatBuf = SearchPathStatBuf;
201 return *SearchPathStatResult;
204 return stat(Path, StatBuf);
208 typedef const char *external_key_type;
209 typedef const char *internal_key_type;
211 typedef HeaderFileInfo data_type;
213 HeaderFileInfoTrait(ASTReader &Reader, Module &M, HeaderSearch *HS,
214 const char *FrameworkStrings,
215 const char *SearchPath = 0)
216 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings),
217 SearchPath(SearchPath) { }
219 static unsigned ComputeHash(const char *path);
220 static internal_key_type GetInternalKey(const char *path);
221 bool EqualKey(internal_key_type a, internal_key_type b);
223 static std::pair<unsigned, unsigned>
224 ReadKeyDataLength(const unsigned char*& d);
226 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
227 return (const char *)d;
230 data_type ReadData(const internal_key_type, const unsigned char *d,
234 /// \brief The on-disk hash table used for known header files.
235 typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
236 HeaderFileInfoLookupTable;
238 } // end namespace clang::serialization::reader
239 } // end namespace clang::serialization
240 } // end namespace clang