blob: 6cb4d662e33897e71424437d330a45032d9df8ca [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//===----------------------------------------------------------------------===//
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
14#define LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
Douglas Gregord44252e2011-08-25 20:47:51 +000015
Mehdi Amini9670f842016-07-18 19:02:11 +000016#include "MultiOnDiskHashTable.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000017#include "clang/AST/DeclarationName.h"
Douglas Gregorbfd73d72013-01-23 18:53:14 +000018#include "clang/Serialization/ASTBitCodes.h"
Richard Smithd88a7f12015-09-01 20:35:42 +000019#include "llvm/ADT/DenseSet.h"
Douglas Gregorde95ead2012-01-06 16:09:53 +000020#include "llvm/Support/Endian.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000021#include "llvm/Support/OnDiskHashTable.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include <utility>
Douglas Gregord44252e2011-08-25 20:47:51 +000023
24namespace clang {
25
26class ASTReader;
27class HeaderSearch;
28struct HeaderFileInfo;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000029class FileEntry;
Douglas Gregord44252e2011-08-25 20:47:51 +000030
31namespace serialization {
32
Douglas Gregorde3ef502011-11-30 23:21:26 +000033class ModuleFile;
Douglas Gregord44252e2011-08-25 20:47:51 +000034
35namespace reader {
36
37/// \brief Class that performs name lookup into a DeclContext stored
38/// in an AST file.
39class ASTDeclContextNameLookupTrait {
40 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +000041 ModuleFile &F;
Douglas Gregord44252e2011-08-25 20:47:51 +000042
43public:
Richard Smithd88a7f12015-09-01 20:35:42 +000044 // Maximum number of lookup tables we allow before condensing the tables.
45 static const int MaxTables = 4;
46
47 /// The lookup result is a list of global declaration IDs.
48 typedef llvm::SmallVector<DeclID, 4> data_type;
49 struct data_type_builder {
50 data_type &Data;
51 llvm::DenseSet<DeclID> Found;
52
53 data_type_builder(data_type &D) : Data(D) {}
54 void insert(DeclID ID) {
55 // Just use a linear scan unless we have more than a few IDs.
56 if (Found.empty() && !Data.empty()) {
57 if (Data.size() <= 4) {
58 for (auto I : Found)
59 if (I == ID)
60 return;
61 Data.push_back(ID);
62 return;
63 }
64
65 // Switch to tracking found IDs in the set.
66 Found.insert(Data.begin(), Data.end());
67 }
68
69 if (Found.insert(ID).second)
70 Data.push_back(ID);
71 }
72 };
Justin Bogner25463f12014-04-18 20:27:24 +000073 typedef unsigned hash_value_type;
74 typedef unsigned offset_type;
Richard Smithd88a7f12015-09-01 20:35:42 +000075 typedef ModuleFile *file_type;
Douglas Gregord44252e2011-08-25 20:47:51 +000076
Douglas Gregord44252e2011-08-25 20:47:51 +000077 typedef DeclarationName external_key_type;
Richard Smitha06c7e62015-08-26 23:55:49 +000078 typedef DeclarationNameKey internal_key_type;
Douglas Gregord44252e2011-08-25 20:47:51 +000079
Nick Lewycky2bd0ab22012-04-16 02:51:46 +000080 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregord44252e2011-08-25 20:47:51 +000081 : Reader(Reader), F(F) { }
82
Richard Smithd88a7f12015-09-01 20:35:42 +000083 static bool EqualKey(const internal_key_type &a, const internal_key_type &b) {
Richard Smitha06c7e62015-08-26 23:55:49 +000084 return a == b;
Douglas Gregord44252e2011-08-25 20:47:51 +000085 }
86
Richard Smitha06c7e62015-08-26 23:55:49 +000087 static hash_value_type ComputeHash(const internal_key_type &Key) {
88 return Key.getHash();
89 }
90 static internal_key_type GetInternalKey(const external_key_type &Name) {
91 return Name;
92 }
Douglas Gregord44252e2011-08-25 20:47:51 +000093
Nick Lewycky2bd0ab22012-04-16 02:51:46 +000094 static std::pair<unsigned, unsigned>
Richard Smitha06c7e62015-08-26 23:55:49 +000095 ReadKeyDataLength(const unsigned char *&d);
Douglas Gregord44252e2011-08-25 20:47:51 +000096
Richard Smitha06c7e62015-08-26 23:55:49 +000097 internal_key_type ReadKey(const unsigned char *d, unsigned);
Douglas Gregord44252e2011-08-25 20:47:51 +000098
Richard Smithd88a7f12015-09-01 20:35:42 +000099 void ReadDataInto(internal_key_type, const unsigned char *d,
100 unsigned DataLen, data_type_builder &Val);
101
102 static void MergeDataInto(const data_type &From, data_type_builder &To) {
103 To.Data.reserve(To.Data.size() + From.size());
104 for (DeclID ID : From)
105 To.insert(ID);
106 }
107
108 file_type ReadFileRef(const unsigned char *&d);
109};
110
111struct DeclContextLookupTable {
112 MultiOnDiskHashTable<ASTDeclContextNameLookupTrait> Table;
Douglas Gregord44252e2011-08-25 20:47:51 +0000113};
114
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000115/// \brief Base class for the trait describing the on-disk hash table for the
116/// identifiers in an AST file.
117///
118/// This class is not useful by itself; rather, it provides common
119/// functionality for accessing the on-disk hash table of identifiers
120/// in an AST file. Different subclasses customize that functionality
121/// based on what information they are interested in. Those subclasses
122/// must provide the \c data_type typedef and the ReadData operation,
123/// only.
124class ASTIdentifierLookupTraitBase {
125public:
126 typedef StringRef external_key_type;
127 typedef StringRef internal_key_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000128 typedef unsigned hash_value_type;
129 typedef unsigned offset_type;
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000130
131 static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
132 return a == b;
133 }
134
Justin Bogner25463f12014-04-18 20:27:24 +0000135 static hash_value_type ComputeHash(const internal_key_type& a);
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000136
137 static std::pair<unsigned, unsigned>
138 ReadKeyDataLength(const unsigned char*& d);
139
140 // This hopefully will just get inlined and removed by the optimizer.
141 static const internal_key_type&
142 GetInternalKey(const external_key_type& x) { return x; }
143
144 // This hopefully will just get inlined and removed by the optimizer.
145 static const external_key_type&
146 GetExternalKey(const internal_key_type& x) { return x; }
147
148 static internal_key_type ReadKey(const unsigned char* d, unsigned n);
149};
150
Douglas Gregord44252e2011-08-25 20:47:51 +0000151/// \brief Class that performs lookup for an identifier stored in an AST file.
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000152class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase {
Douglas Gregord44252e2011-08-25 20:47:51 +0000153 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000154 ModuleFile &F;
Douglas Gregord44252e2011-08-25 20:47:51 +0000155
156 // If we know the IdentifierInfo in advance, it is here and we will
157 // not build a new one. Used when deserializing information about an
158 // identifier that was constructed before the AST file was read.
159 IdentifierInfo *KnownII;
160
161public:
162 typedef IdentifierInfo * data_type;
Craig Toppera13603a2014-05-22 05:54:18 +0000163
Douglas Gregorde3ef502011-11-30 23:21:26 +0000164 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
Craig Toppera13603a2014-05-22 05:54:18 +0000165 IdentifierInfo *II = nullptr)
Douglas Gregord44252e2011-08-25 20:47:51 +0000166 : Reader(Reader), F(F), KnownII(II) { }
Craig Toppera13603a2014-05-22 05:54:18 +0000167
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000168 data_type ReadData(const internal_key_type& k,
169 const unsigned char* d,
170 unsigned DataLen);
Douglas Gregor247afcc2012-01-24 15:24:38 +0000171
Richard Smith79bf9202015-08-24 03:33:22 +0000172 IdentID ReadIdentifierID(const unsigned char *d);
173
Douglas Gregor247afcc2012-01-24 15:24:38 +0000174 ASTReader &getReader() const { return Reader; }
Douglas Gregord44252e2011-08-25 20:47:51 +0000175};
176
177/// \brief The on-disk hash table used to contain information about
178/// all of the identifiers in the program.
Justin Bognerbb094f02014-04-18 19:57:06 +0000179typedef llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait>
Douglas Gregord44252e2011-08-25 20:47:51 +0000180 ASTIdentifierLookupTable;
181
182/// \brief Class that performs lookup for a selector's entries in the global
183/// method pool stored in an AST file.
184class ASTSelectorLookupTrait {
185 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000186 ModuleFile &F;
Douglas Gregord44252e2011-08-25 20:47:51 +0000187
188public:
189 struct data_type {
190 SelectorID ID;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +0000191 unsigned InstanceBits;
192 unsigned FactoryBits;
Nico Weberff4b35e2014-12-27 22:14:15 +0000193 bool InstanceHasMoreThanOneDecl;
194 bool FactoryHasMoreThanOneDecl;
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000195 SmallVector<ObjCMethodDecl *, 2> Instance;
196 SmallVector<ObjCMethodDecl *, 2> Factory;
Douglas Gregord44252e2011-08-25 20:47:51 +0000197 };
198
199 typedef Selector external_key_type;
200 typedef external_key_type internal_key_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000201 typedef unsigned hash_value_type;
202 typedef unsigned offset_type;
Douglas Gregord44252e2011-08-25 20:47:51 +0000203
Douglas Gregorde3ef502011-11-30 23:21:26 +0000204 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregord44252e2011-08-25 20:47:51 +0000205 : Reader(Reader), F(F) { }
206
207 static bool EqualKey(const internal_key_type& a,
208 const internal_key_type& b) {
209 return a == b;
210 }
211
Justin Bogner25463f12014-04-18 20:27:24 +0000212 static hash_value_type ComputeHash(Selector Sel);
Douglas Gregord44252e2011-08-25 20:47:51 +0000213
214 static const internal_key_type&
Chris Lattnerd2cd41c2011-09-10 16:13:42 +0000215 GetInternalKey(const external_key_type& x) { return x; }
Douglas Gregord44252e2011-08-25 20:47:51 +0000216
217 static std::pair<unsigned, unsigned>
218 ReadKeyDataLength(const unsigned char*& d);
219
220 internal_key_type ReadKey(const unsigned char* d, unsigned);
221 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
222};
223
224/// \brief The on-disk hash table used for the global method pool.
Justin Bognerbb094f02014-04-18 19:57:06 +0000225typedef llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait>
Douglas Gregord44252e2011-08-25 20:47:51 +0000226 ASTSelectorLookupTable;
227
228/// \brief Trait class used to search the on-disk hash table containing all of
229/// the header search information.
230///
231/// The on-disk hash table contains a mapping from each header path to
232/// information about that header (how many times it has been included, its
Richard Smith7ed1bc92014-12-05 22:42:13 +0000233/// controlling macro, etc.). Note that we actually hash based on the size
234/// and mtime, and support "deep" comparisons of file names based on current
Douglas Gregord44252e2011-08-25 20:47:51 +0000235/// inode numbers, so that the search can cope with non-normalized path names
236/// and symlinks.
237class HeaderFileInfoTrait {
238 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000239 ModuleFile &M;
Douglas Gregord44252e2011-08-25 20:47:51 +0000240 HeaderSearch *HS;
241 const char *FrameworkStrings;
Ted Kremenek03cb1372013-02-05 06:21:59 +0000242
Douglas Gregord44252e2011-08-25 20:47:51 +0000243public:
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +0000244 typedef const FileEntry *external_key_type;
245
246 struct internal_key_type {
247 off_t Size;
248 time_t ModTime;
Mehdi Amini004b9c72016-10-10 22:52:47 +0000249 StringRef Filename;
Richard Smith7ed1bc92014-12-05 22:42:13 +0000250 bool Imported;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +0000251 };
252 typedef const internal_key_type &internal_key_ref;
Douglas Gregord44252e2011-08-25 20:47:51 +0000253
254 typedef HeaderFileInfo data_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000255 typedef unsigned hash_value_type;
256 typedef unsigned offset_type;
Douglas Gregord44252e2011-08-25 20:47:51 +0000257
Douglas Gregorde3ef502011-11-30 23:21:26 +0000258 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
Argyrios Kyrtzidisb42863e2013-03-06 18:12:41 +0000259 const char *FrameworkStrings)
260 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { }
Douglas Gregord44252e2011-08-25 20:47:51 +0000261
Justin Bogner25463f12014-04-18 20:27:24 +0000262 static hash_value_type ComputeHash(internal_key_ref ikey);
Richard Smithe75ee0f2015-08-17 07:13:32 +0000263 internal_key_type GetInternalKey(const FileEntry *FE);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +0000264 bool EqualKey(internal_key_ref a, internal_key_ref b);
Douglas Gregord44252e2011-08-25 20:47:51 +0000265
266 static std::pair<unsigned, unsigned>
267 ReadKeyDataLength(const unsigned char*& d);
268
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +0000269 static internal_key_type ReadKey(const unsigned char *d, unsigned);
Douglas Gregord44252e2011-08-25 20:47:51 +0000270
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +0000271 data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
Douglas Gregord44252e2011-08-25 20:47:51 +0000272};
273
274/// \brief The on-disk hash table used for known header files.
Justin Bognerbb094f02014-04-18 19:57:06 +0000275typedef llvm::OnDiskChainedHashTable<HeaderFileInfoTrait>
Douglas Gregord44252e2011-08-25 20:47:51 +0000276 HeaderFileInfoLookupTable;
277
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000278} // end namespace clang::serialization::reader
279} // end namespace clang::serialization
Douglas Gregord44252e2011-08-25 20:47:51 +0000280} // end namespace clang
281
282
283#endif