blob: da90c3400a25d6be2e8c1f773d19f19b765a2d24 [file] [log] [blame]
Douglas Gregord44252e2011-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 Gregorde95ead2012-01-06 16:09:53 +000018#include "llvm/Support/Endian.h"
Douglas Gregord44252e2011-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 Gregorde3ef502011-11-30 23:21:26 +000030class ModuleFile;
Douglas Gregord44252e2011-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 Gregorde3ef502011-11-30 23:21:26 +000038 ModuleFile &F;
Douglas Gregord44252e2011-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 Gregorde95ead2012-01-06 16:09:53 +000045 typedef llvm::support::ulittle32_t LE32DeclID;
46 typedef std::pair<LE32DeclID *, LE32DeclID *> data_type;
Douglas Gregord44252e2011-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 Gregorde3ef502011-11-30 23:21:26 +000061 ModuleFile &F)
Douglas Gregord44252e2011-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
Douglas Gregord44252e2011-08-25 20:47:51 +000082/// \brief Class that performs lookup for an identifier stored in an AST file.
83class ASTIdentifierLookupTrait {
84 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +000085 ModuleFile &F;
Douglas Gregord44252e2011-08-25 20:47:51 +000086
87 // If we know the IdentifierInfo in advance, it is here and we will
88 // not build a new one. Used when deserializing information about an
89 // identifier that was constructed before the AST file was read.
90 IdentifierInfo *KnownII;
91
92public:
93 typedef IdentifierInfo * data_type;
94
95 typedef const std::pair<const char*, unsigned> external_key_type;
96
97 typedef external_key_type internal_key_type;
98
Douglas Gregorde3ef502011-11-30 23:21:26 +000099 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
Douglas Gregord44252e2011-08-25 20:47:51 +0000100 IdentifierInfo *II = 0)
101 : Reader(Reader), F(F), KnownII(II) { }
102
103 static bool EqualKey(const internal_key_type& a,
104 const internal_key_type& b) {
105 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
106 : false;
107 }
108
109 static unsigned ComputeHash(const internal_key_type& a);
110
111 // This hopefully will just get inlined and removed by the optimizer.
112 static const internal_key_type&
113 GetInternalKey(const external_key_type& x) { return x; }
114
115 // This hopefully will just get inlined and removed by the optimizer.
116 static const external_key_type&
117 GetExternalKey(const internal_key_type& x) { return x; }
118
119 static std::pair<unsigned, unsigned>
120 ReadKeyDataLength(const unsigned char*& d);
121
122 static std::pair<const char*, unsigned>
123 ReadKey(const unsigned char* d, unsigned n);
124
125 IdentifierInfo *ReadData(const internal_key_type& k,
126 const unsigned char* d,
127 unsigned DataLen);
Douglas Gregor247afcc2012-01-24 15:24:38 +0000128
129 ASTReader &getReader() const { return Reader; }
130
Douglas Gregord44252e2011-08-25 20:47:51 +0000131};
132
133/// \brief The on-disk hash table used to contain information about
134/// all of the identifiers in the program.
135typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
136 ASTIdentifierLookupTable;
137
138/// \brief Class that performs lookup for a selector's entries in the global
139/// method pool stored in an AST file.
140class ASTSelectorLookupTrait {
141 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000142 ModuleFile &F;
Douglas Gregord44252e2011-08-25 20:47:51 +0000143
144public:
145 struct data_type {
146 SelectorID ID;
147 llvm::SmallVector<ObjCMethodDecl *, 2> Instance;
148 llvm::SmallVector<ObjCMethodDecl *, 2> Factory;
149 };
150
151 typedef Selector external_key_type;
152 typedef external_key_type internal_key_type;
153
Douglas Gregorde3ef502011-11-30 23:21:26 +0000154 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregord44252e2011-08-25 20:47:51 +0000155 : Reader(Reader), F(F) { }
156
157 static bool EqualKey(const internal_key_type& a,
158 const internal_key_type& b) {
159 return a == b;
160 }
161
162 static unsigned ComputeHash(Selector Sel);
163
164 static const internal_key_type&
Chris Lattnerd2cd41c2011-09-10 16:13:42 +0000165 GetInternalKey(const external_key_type& x) { return x; }
Douglas Gregord44252e2011-08-25 20:47:51 +0000166
167 static std::pair<unsigned, unsigned>
168 ReadKeyDataLength(const unsigned char*& d);
169
170 internal_key_type ReadKey(const unsigned char* d, unsigned);
171 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
172};
173
174/// \brief The on-disk hash table used for the global method pool.
175typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
176 ASTSelectorLookupTable;
177
178/// \brief Trait class used to search the on-disk hash table containing all of
179/// the header search information.
180///
181/// The on-disk hash table contains a mapping from each header path to
182/// information about that header (how many times it has been included, its
183/// controlling macro, etc.). Note that we actually hash based on the
184/// filename, and support "deep" comparisons of file names based on current
185/// inode numbers, so that the search can cope with non-normalized path names
186/// and symlinks.
187class HeaderFileInfoTrait {
188 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000189 ModuleFile &M;
Douglas Gregord44252e2011-08-25 20:47:51 +0000190 HeaderSearch *HS;
191 const char *FrameworkStrings;
192 const char *SearchPath;
193 struct stat SearchPathStatBuf;
194 llvm::Optional<int> SearchPathStatResult;
195
196 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
197 if (Path == SearchPath) {
198 if (!SearchPathStatResult)
199 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
200
201 *StatBuf = SearchPathStatBuf;
202 return *SearchPathStatResult;
203 }
204
205 return stat(Path, StatBuf);
206 }
207
208public:
209 typedef const char *external_key_type;
210 typedef const char *internal_key_type;
211
212 typedef HeaderFileInfo data_type;
213
Douglas Gregorde3ef502011-11-30 23:21:26 +0000214 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
Douglas Gregord44252e2011-08-25 20:47:51 +0000215 const char *FrameworkStrings,
216 const char *SearchPath = 0)
217 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings),
218 SearchPath(SearchPath) { }
219
220 static unsigned ComputeHash(const char *path);
221 static internal_key_type GetInternalKey(const char *path);
222 bool EqualKey(internal_key_type a, internal_key_type b);
223
224 static std::pair<unsigned, unsigned>
225 ReadKeyDataLength(const unsigned char*& d);
226
227 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
228 return (const char *)d;
229 }
230
231 data_type ReadData(const internal_key_type, const unsigned char *d,
232 unsigned DataLen);
233};
234
235/// \brief The on-disk hash table used for known header files.
236typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
237 HeaderFileInfoLookupTable;
238
239} // end namespace clang::serialization::reader
240} // end namespace clang::serialization
241} // end namespace clang
242
243
244#endif