blob: 9f3f1a4c1091f9ddfc23e45286835c78c7fadbfa [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
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "ASTReaderInternals.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000017#include "clang/AST/DeclarationName.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor9b8b20f2012-01-06 16:09:53 +000019#include "llvm/Support/Endian.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000020#include <sys/stat.h>
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include <utility>
Douglas Gregor98339b92011-08-25 20:47:51 +000022
23namespace clang {
24
25class ASTReader;
26class HeaderSearch;
27struct HeaderFileInfo;
28
29namespace serialization {
30
Douglas Gregor1a4761e2011-11-30 23:21:26 +000031class ModuleFile;
Douglas Gregor98339b92011-08-25 20:47:51 +000032
33namespace reader {
34
35/// \brief Class that performs name lookup into a DeclContext stored
36/// in an AST file.
37class ASTDeclContextNameLookupTrait {
38 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000039 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +000040
41public:
42 /// \brief Pair of begin/end iterators for DeclIDs.
43 ///
44 /// Note that these declaration IDs are local to the module that contains this
45 /// particular lookup t
Douglas Gregor9b8b20f2012-01-06 16:09:53 +000046 typedef llvm::support::ulittle32_t LE32DeclID;
47 typedef std::pair<LE32DeclID *, LE32DeclID *> data_type;
Douglas Gregor98339b92011-08-25 20:47:51 +000048
49 /// \brief Special internal key for declaration names.
50 /// The hash table creates keys for comparison; we do not create
51 /// a DeclarationName for the internal key to avoid deserializing types.
52 struct DeclNameKey {
53 DeclarationName::NameKind Kind;
54 uint64_t Data;
55 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
56 };
57
58 typedef DeclarationName external_key_type;
59 typedef DeclNameKey internal_key_type;
60
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000061 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregor98339b92011-08-25 20:47:51 +000062 : 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;
Douglas Gregor98339b92011-08-25 20:47:51 +000071
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000072 static std::pair<unsigned, unsigned>
Douglas Gregor98339b92011-08-25 20:47:51 +000073 ReadKeyDataLength(const unsigned char*& d);
74
75 internal_key_type ReadKey(const unsigned char* d, unsigned);
76
77 data_type ReadData(internal_key_type, const unsigned char* d,
78 unsigned DataLen);
79};
80
Douglas Gregor98339b92011-08-25 20:47:51 +000081/// \brief Class that performs lookup for an identifier stored in an AST file.
82class ASTIdentifierLookupTrait {
83 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000084 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +000085
86 // If we know the IdentifierInfo in advance, it is here and we will
87 // not build a new one. Used when deserializing information about an
88 // identifier that was constructed before the AST file was read.
89 IdentifierInfo *KnownII;
90
91public:
92 typedef IdentifierInfo * data_type;
93
94 typedef const std::pair<const char*, unsigned> external_key_type;
95
96 typedef external_key_type internal_key_type;
97
Douglas Gregor1a4761e2011-11-30 23:21:26 +000098 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
Douglas Gregor98339b92011-08-25 20:47:51 +000099 IdentifierInfo *II = 0)
100 : Reader(Reader), F(F), KnownII(II) { }
101
102 static bool EqualKey(const internal_key_type& a,
103 const internal_key_type& b) {
104 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
105 : false;
106 }
107
108 static unsigned ComputeHash(const internal_key_type& a);
109
110 // This hopefully will just get inlined and removed by the optimizer.
111 static const internal_key_type&
112 GetInternalKey(const external_key_type& x) { return x; }
113
114 // This hopefully will just get inlined and removed by the optimizer.
115 static const external_key_type&
116 GetExternalKey(const internal_key_type& x) { return x; }
117
118 static std::pair<unsigned, unsigned>
119 ReadKeyDataLength(const unsigned char*& d);
120
121 static std::pair<const char*, unsigned>
122 ReadKey(const unsigned char* d, unsigned n);
123
124 IdentifierInfo *ReadData(const internal_key_type& k,
125 const unsigned char* d,
126 unsigned DataLen);
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000127
128 ASTReader &getReader() const { return Reader; }
129
Douglas Gregor98339b92011-08-25 20:47:51 +0000130};
131
132/// \brief The on-disk hash table used to contain information about
133/// all of the identifiers in the program.
134typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
135 ASTIdentifierLookupTable;
136
137/// \brief Class that performs lookup for a selector's entries in the global
138/// method pool stored in an AST file.
139class ASTSelectorLookupTrait {
140 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000141 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +0000142
143public:
144 struct data_type {
145 SelectorID ID;
146 llvm::SmallVector<ObjCMethodDecl *, 2> Instance;
147 llvm::SmallVector<ObjCMethodDecl *, 2> Factory;
148 };
149
150 typedef Selector external_key_type;
151 typedef external_key_type internal_key_type;
152
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000153 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregor98339b92011-08-25 20:47:51 +0000154 : Reader(Reader), F(F) { }
155
156 static bool EqualKey(const internal_key_type& a,
157 const internal_key_type& b) {
158 return a == b;
159 }
160
161 static unsigned ComputeHash(Selector Sel);
162
163 static const internal_key_type&
Chris Lattnera2f4ae82011-09-10 16:13:42 +0000164 GetInternalKey(const external_key_type& x) { return x; }
Douglas Gregor98339b92011-08-25 20:47:51 +0000165
166 static std::pair<unsigned, unsigned>
167 ReadKeyDataLength(const unsigned char*& d);
168
169 internal_key_type ReadKey(const unsigned char* d, unsigned);
170 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
171};
172
173/// \brief The on-disk hash table used for the global method pool.
174typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
175 ASTSelectorLookupTable;
176
177/// \brief Trait class used to search the on-disk hash table containing all of
178/// the header search information.
179///
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
185/// and symlinks.
186class HeaderFileInfoTrait {
187 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000188 ModuleFile &M;
Douglas Gregor98339b92011-08-25 20:47:51 +0000189 HeaderSearch *HS;
190 const char *FrameworkStrings;
191 const char *SearchPath;
192 struct stat SearchPathStatBuf;
193 llvm::Optional<int> SearchPathStatResult;
194
195 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
196 if (Path == SearchPath) {
197 if (!SearchPathStatResult)
198 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
199
200 *StatBuf = SearchPathStatBuf;
201 return *SearchPathStatResult;
202 }
203
204 return stat(Path, StatBuf);
205 }
206
207public:
208 typedef const char *external_key_type;
209 typedef const char *internal_key_type;
210
211 typedef HeaderFileInfo data_type;
212
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000213 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
Douglas Gregor98339b92011-08-25 20:47:51 +0000214 const char *FrameworkStrings,
215 const char *SearchPath = 0)
216 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings),
217 SearchPath(SearchPath) { }
218
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);
222
223 static std::pair<unsigned, unsigned>
224 ReadKeyDataLength(const unsigned char*& d);
225
226 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
227 return (const char *)d;
228 }
229
230 data_type ReadData(const internal_key_type, const unsigned char *d,
231 unsigned DataLen);
232};
233
234/// \brief The on-disk hash table used for known header files.
235typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
236 HeaderFileInfoLookupTable;
237
238} // end namespace clang::serialization::reader
239} // end namespace clang::serialization
240} // end namespace clang
241
242
243#endif