blob: 635f95fe594e04585964b651e41ab7fd551e071f [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);
132};
133
134/// \brief The on-disk hash table used to contain information about
135/// all of the identifiers in the program.
136typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
137 ASTIdentifierLookupTable;
138
139/// \brief Class that performs lookup for a selector's entries in the global
140/// method pool stored in an AST file.
141class ASTSelectorLookupTrait {
142 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000143 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +0000144
145public:
146 struct data_type {
147 SelectorID ID;
148 llvm::SmallVector<ObjCMethodDecl *, 2> Instance;
149 llvm::SmallVector<ObjCMethodDecl *, 2> Factory;
150 };
151
152 typedef Selector external_key_type;
153 typedef external_key_type internal_key_type;
154
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000155 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregor98339b92011-08-25 20:47:51 +0000156 : Reader(Reader), F(F) { }
157
158 static bool EqualKey(const internal_key_type& a,
159 const internal_key_type& b) {
160 return a == b;
161 }
162
163 static unsigned ComputeHash(Selector Sel);
164
165 static const internal_key_type&
Chris Lattnera2f4ae82011-09-10 16:13:42 +0000166 GetInternalKey(const external_key_type& x) { return x; }
Douglas Gregor98339b92011-08-25 20:47:51 +0000167
168 static std::pair<unsigned, unsigned>
169 ReadKeyDataLength(const unsigned char*& d);
170
171 internal_key_type ReadKey(const unsigned char* d, unsigned);
172 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
173};
174
175/// \brief The on-disk hash table used for the global method pool.
176typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
177 ASTSelectorLookupTable;
178
179/// \brief Trait class used to search the on-disk hash table containing all of
180/// the header search information.
181///
182/// The on-disk hash table contains a mapping from each header path to
183/// information about that header (how many times it has been included, its
184/// controlling macro, etc.). Note that we actually hash based on the
185/// filename, and support "deep" comparisons of file names based on current
186/// inode numbers, so that the search can cope with non-normalized path names
187/// and symlinks.
188class HeaderFileInfoTrait {
189 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000190 ModuleFile &M;
Douglas Gregor98339b92011-08-25 20:47:51 +0000191 HeaderSearch *HS;
192 const char *FrameworkStrings;
193 const char *SearchPath;
194 struct stat SearchPathStatBuf;
195 llvm::Optional<int> SearchPathStatResult;
196
197 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
198 if (Path == SearchPath) {
199 if (!SearchPathStatResult)
200 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
201
202 *StatBuf = SearchPathStatBuf;
203 return *SearchPathStatResult;
204 }
205
206 return stat(Path, StatBuf);
207 }
208
209public:
210 typedef const char *external_key_type;
211 typedef const char *internal_key_type;
212
213 typedef HeaderFileInfo data_type;
214
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000215 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
Douglas Gregor98339b92011-08-25 20:47:51 +0000216 const char *FrameworkStrings,
217 const char *SearchPath = 0)
218 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings),
219 SearchPath(SearchPath) { }
220
221 static unsigned ComputeHash(const char *path);
222 static internal_key_type GetInternalKey(const char *path);
223 bool EqualKey(internal_key_type a, internal_key_type b);
224
225 static std::pair<unsigned, unsigned>
226 ReadKeyDataLength(const unsigned char*& d);
227
228 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
229 return (const char *)d;
230 }
231
232 data_type ReadData(const internal_key_type, const unsigned char *d,
233 unsigned DataLen);
234};
235
236/// \brief The on-disk hash table used for known header files.
237typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
238 HeaderFileInfoLookupTable;
239
240} // end namespace clang::serialization::reader
241} // end namespace clang::serialization
242} // end namespace clang
243
244
245#endif