blob: dc6ccb72d29e7ab22afe98041f53077ca2820582 [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
Douglas Gregor98339b92011-08-25 20:47:51 +000016#include "clang/AST/DeclarationName.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor479633c2013-01-23 18:53:14 +000018#include "clang/Serialization/ASTBitCodes.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 Gregor479633c2013-01-23 18:53:14 +000081/// \brief Base class for the trait describing the on-disk hash table for the
82/// identifiers in an AST file.
83///
84/// This class is not useful by itself; rather, it provides common
85/// functionality for accessing the on-disk hash table of identifiers
86/// in an AST file. Different subclasses customize that functionality
87/// based on what information they are interested in. Those subclasses
88/// must provide the \c data_type typedef and the ReadData operation,
89/// only.
90class ASTIdentifierLookupTraitBase {
91public:
92 typedef StringRef external_key_type;
93 typedef StringRef internal_key_type;
94
95
96 static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
97 return a == b;
98 }
99
100 static unsigned ComputeHash(const internal_key_type& a);
101
102 static std::pair<unsigned, unsigned>
103 ReadKeyDataLength(const unsigned char*& d);
104
105 // This hopefully will just get inlined and removed by the optimizer.
106 static const internal_key_type&
107 GetInternalKey(const external_key_type& x) { return x; }
108
109 // This hopefully will just get inlined and removed by the optimizer.
110 static const external_key_type&
111 GetExternalKey(const internal_key_type& x) { return x; }
112
113 static internal_key_type ReadKey(const unsigned char* d, unsigned n);
114};
115
Douglas Gregor98339b92011-08-25 20:47:51 +0000116/// \brief Class that performs lookup for an identifier stored in an AST file.
Douglas Gregor479633c2013-01-23 18:53:14 +0000117class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase {
Douglas Gregor98339b92011-08-25 20:47:51 +0000118 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000119 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +0000120
121 // If we know the IdentifierInfo in advance, it is here and we will
122 // not build a new one. Used when deserializing information about an
123 // identifier that was constructed before the AST file was read.
124 IdentifierInfo *KnownII;
125
126public:
127 typedef IdentifierInfo * data_type;
128
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000129 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
Douglas Gregor98339b92011-08-25 20:47:51 +0000130 IdentifierInfo *II = 0)
131 : Reader(Reader), F(F), KnownII(II) { }
Douglas Gregor479633c2013-01-23 18:53:14 +0000132
133 data_type ReadData(const internal_key_type& k,
134 const unsigned char* d,
135 unsigned DataLen);
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000136
137 ASTReader &getReader() const { return Reader; }
Douglas Gregor98339b92011-08-25 20:47:51 +0000138};
139
140/// \brief The on-disk hash table used to contain information about
141/// all of the identifiers in the program.
142typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
143 ASTIdentifierLookupTable;
144
145/// \brief Class that performs lookup for a selector's entries in the global
146/// method pool stored in an AST file.
147class ASTSelectorLookupTrait {
148 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000149 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +0000150
151public:
152 struct data_type {
153 SelectorID ID;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000154 SmallVector<ObjCMethodDecl *, 2> Instance;
155 SmallVector<ObjCMethodDecl *, 2> Factory;
Douglas Gregor98339b92011-08-25 20:47:51 +0000156 };
157
158 typedef Selector external_key_type;
159 typedef external_key_type internal_key_type;
160
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000161 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregor98339b92011-08-25 20:47:51 +0000162 : Reader(Reader), F(F) { }
163
164 static bool EqualKey(const internal_key_type& a,
165 const internal_key_type& b) {
166 return a == b;
167 }
168
169 static unsigned ComputeHash(Selector Sel);
170
171 static const internal_key_type&
Chris Lattnera2f4ae82011-09-10 16:13:42 +0000172 GetInternalKey(const external_key_type& x) { return x; }
Douglas Gregor98339b92011-08-25 20:47:51 +0000173
174 static std::pair<unsigned, unsigned>
175 ReadKeyDataLength(const unsigned char*& d);
176
177 internal_key_type ReadKey(const unsigned char* d, unsigned);
178 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
179};
180
181/// \brief The on-disk hash table used for the global method pool.
182typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
183 ASTSelectorLookupTable;
184
185/// \brief Trait class used to search the on-disk hash table containing all of
186/// the header search information.
187///
188/// The on-disk hash table contains a mapping from each header path to
189/// information about that header (how many times it has been included, its
190/// controlling macro, etc.). Note that we actually hash based on the
191/// filename, and support "deep" comparisons of file names based on current
192/// inode numbers, so that the search can cope with non-normalized path names
193/// and symlinks.
194class HeaderFileInfoTrait {
195 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000196 ModuleFile &M;
Douglas Gregor98339b92011-08-25 20:47:51 +0000197 HeaderSearch *HS;
198 const char *FrameworkStrings;
199 const char *SearchPath;
200 struct stat SearchPathStatBuf;
201 llvm::Optional<int> SearchPathStatResult;
202
203 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
204 if (Path == SearchPath) {
205 if (!SearchPathStatResult)
206 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
207
208 *StatBuf = SearchPathStatBuf;
209 return *SearchPathStatResult;
210 }
211
212 return stat(Path, StatBuf);
213 }
214
215public:
216 typedef const char *external_key_type;
217 typedef const char *internal_key_type;
218
219 typedef HeaderFileInfo data_type;
220
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000221 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
Douglas Gregor98339b92011-08-25 20:47:51 +0000222 const char *FrameworkStrings,
223 const char *SearchPath = 0)
224 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings),
225 SearchPath(SearchPath) { }
226
227 static unsigned ComputeHash(const char *path);
228 static internal_key_type GetInternalKey(const char *path);
229 bool EqualKey(internal_key_type a, internal_key_type b);
230
231 static std::pair<unsigned, unsigned>
232 ReadKeyDataLength(const unsigned char*& d);
233
234 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
235 return (const char *)d;
236 }
237
238 data_type ReadData(const internal_key_type, const unsigned char *d,
239 unsigned DataLen);
240};
241
242/// \brief The on-disk hash table used for known header files.
243typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
244 HeaderFileInfoLookupTable;
245
246} // end namespace clang::serialization::reader
247} // end namespace clang::serialization
248} // end namespace clang
249
250
251#endif