blob: 3a1dfcf4b74a62438c382837226fa59cc40742da [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
60 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader,
Douglas Gregor1a4761e2011-11-30 23:21:26 +000061 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;
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.
83typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
84 ASTDeclContextNameLookupTable;
85
86/// \brief Class that performs lookup for an identifier stored in an AST file.
87class ASTIdentifierLookupTrait {
88 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000089 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +000090
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
96public:
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 Gregor1a4761e2011-11-30 23:21:26 +0000103 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
Douglas Gregor98339b92011-08-25 20:47:51 +0000104 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 Gregor5d5051f2012-01-24 15:24:38 +0000132
133 ASTReader &getReader() const { return Reader; }
134
Douglas Gregor98339b92011-08-25 20:47:51 +0000135};
136
137/// \brief The on-disk hash table used to contain information about
138/// all of the identifiers in the program.
139typedef 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.
144class ASTSelectorLookupTrait {
145 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000146 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +0000147
148public:
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 Gregor1a4761e2011-11-30 23:21:26 +0000158 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregor98339b92011-08-25 20:47:51 +0000159 : 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 Lattnera2f4ae82011-09-10 16:13:42 +0000169 GetInternalKey(const external_key_type& x) { return x; }
Douglas Gregor98339b92011-08-25 20:47:51 +0000170
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.
179typedef 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.
191class HeaderFileInfoTrait {
192 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000193 ModuleFile &M;
Douglas Gregor98339b92011-08-25 20:47:51 +0000194 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
212public:
213 typedef const char *external_key_type;
214 typedef const char *internal_key_type;
215
216 typedef HeaderFileInfo data_type;
217
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000218 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
Douglas Gregor98339b92011-08-25 20:47:51 +0000219 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.
240typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
241 HeaderFileInfoLookupTable;
242
243} // end namespace clang::serialization::reader
244} // end namespace clang::serialization
245} // end namespace clang
246
247
248#endif