blob: e5159e952635da6f7ac7059caa1e7bed3035671d [file] [log] [blame]
Douglas Gregor98339b92011-08-25 20:47:51 +00001//===--- 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 Gregor9b8b20f2012-01-06 16:09:53 +000018#include "llvm/Support/Endian.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000019#include <utility>
20#include <sys/stat.h>
21
22namespace clang {
23
24class ASTReader;
25class HeaderSearch;
26struct HeaderFileInfo;
27
28namespace serialization {
29
Douglas Gregor1a4761e2011-11-30 23:21:26 +000030class ModuleFile;
Douglas Gregor98339b92011-08-25 20:47:51 +000031
32namespace reader {
33
34/// \brief Class that performs name lookup into a DeclContext stored
35/// in an AST file.
36class ASTDeclContextNameLookupTrait {
37 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000038 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +000039
40public:
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 Gregor9b8b20f2012-01-06 16:09:53 +000045 typedef llvm::support::ulittle32_t LE32DeclID;
46 typedef std::pair<LE32DeclID *, LE32DeclID *> data_type;
Douglas Gregor98339b92011-08-25 20:47:51 +000047
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
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000060 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregor98339b92011-08-25 20:47:51 +000061 : Reader(Reader), F(F) { }
62
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;
66 }
67
68 unsigned ComputeHash(const DeclNameKey &Key) const;
69 internal_key_type GetInternalKey(const external_key_type& Name) const;
Douglas Gregor98339b92011-08-25 20:47:51 +000070
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000071 static std::pair<unsigned, unsigned>
Douglas Gregor98339b92011-08-25 20:47:51 +000072 ReadKeyDataLength(const unsigned char*& d);
73
74 internal_key_type ReadKey(const unsigned char* d, unsigned);
75
76 data_type ReadData(internal_key_type, const unsigned char* d,
77 unsigned DataLen);
78};
79
Douglas Gregor98339b92011-08-25 20:47:51 +000080/// \brief Class that performs lookup for an identifier stored in an AST file.
81class ASTIdentifierLookupTrait {
82 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000083 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +000084
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;
89
90public:
91 typedef IdentifierInfo * data_type;
92
93 typedef const std::pair<const char*, unsigned> external_key_type;
94
95 typedef external_key_type internal_key_type;
96
Douglas Gregor1a4761e2011-11-30 23:21:26 +000097 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
Douglas Gregor98339b92011-08-25 20:47:51 +000098 IdentifierInfo *II = 0)
99 : Reader(Reader), F(F), KnownII(II) { }
100
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
104 : false;
105 }
106
107 static unsigned ComputeHash(const internal_key_type& a);
108
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; }
112
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; }
116
117 static std::pair<unsigned, unsigned>
118 ReadKeyDataLength(const unsigned char*& d);
119
120 static std::pair<const char*, unsigned>
121 ReadKey(const unsigned char* d, unsigned n);
122
123 IdentifierInfo *ReadData(const internal_key_type& k,
124 const unsigned char* d,
125 unsigned DataLen);
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000126
127 ASTReader &getReader() const { return Reader; }
128
Douglas Gregor98339b92011-08-25 20:47:51 +0000129};
130
131/// \brief The on-disk hash table used to contain information about
132/// all of the identifiers in the program.
133typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
134 ASTIdentifierLookupTable;
135
136/// \brief Class that performs lookup for a selector's entries in the global
137/// method pool stored in an AST file.
138class ASTSelectorLookupTrait {
139 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000140 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +0000141
142public:
143 struct data_type {
144 SelectorID ID;
145 llvm::SmallVector<ObjCMethodDecl *, 2> Instance;
146 llvm::SmallVector<ObjCMethodDecl *, 2> Factory;
147 };
148
149 typedef Selector external_key_type;
150 typedef external_key_type internal_key_type;
151
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000152 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregor98339b92011-08-25 20:47:51 +0000153 : Reader(Reader), F(F) { }
154
155 static bool EqualKey(const internal_key_type& a,
156 const internal_key_type& b) {
157 return a == b;
158 }
159
160 static unsigned ComputeHash(Selector Sel);
161
162 static const internal_key_type&
Chris Lattnera2f4ae82011-09-10 16:13:42 +0000163 GetInternalKey(const external_key_type& x) { return x; }
Douglas Gregor98339b92011-08-25 20:47:51 +0000164
165 static std::pair<unsigned, unsigned>
166 ReadKeyDataLength(const unsigned char*& d);
167
168 internal_key_type ReadKey(const unsigned char* d, unsigned);
169 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
170};
171
172/// \brief The on-disk hash table used for the global method pool.
173typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
174 ASTSelectorLookupTable;
175
176/// \brief Trait class used to search the on-disk hash table containing all of
177/// the header search information.
178///
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
184/// and symlinks.
185class HeaderFileInfoTrait {
186 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000187 ModuleFile &M;
Douglas Gregor98339b92011-08-25 20:47:51 +0000188 HeaderSearch *HS;
189 const char *FrameworkStrings;
190 const char *SearchPath;
191 struct stat SearchPathStatBuf;
192 llvm::Optional<int> SearchPathStatResult;
193
194 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
195 if (Path == SearchPath) {
196 if (!SearchPathStatResult)
197 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
198
199 *StatBuf = SearchPathStatBuf;
200 return *SearchPathStatResult;
201 }
202
203 return stat(Path, StatBuf);
204 }
205
206public:
207 typedef const char *external_key_type;
208 typedef const char *internal_key_type;
209
210 typedef HeaderFileInfo data_type;
211
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000212 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
Douglas Gregor98339b92011-08-25 20:47:51 +0000213 const char *FrameworkStrings,
214 const char *SearchPath = 0)
215 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings),
216 SearchPath(SearchPath) { }
217
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);
221
222 static std::pair<unsigned, unsigned>
223 ReadKeyDataLength(const unsigned char*& d);
224
225 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
226 return (const char *)d;
227 }
228
229 data_type ReadData(const internal_key_type, const unsigned char *d,
230 unsigned DataLen);
231};
232
233/// \brief The on-disk hash table used for known header files.
234typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
235 HeaderFileInfoLookupTable;
236
237} // end namespace clang::serialization::reader
238} // end namespace clang::serialization
239} // end namespace clang
240
241
242#endif