blob: a63e362eb64f03c05f589a1ff53b7821d7f516a2 [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
Douglas Gregord44252e2011-08-25 20:47:51 +000016#include "clang/AST/DeclarationName.h"
Douglas Gregorbfd73d72013-01-23 18:53:14 +000017#include "clang/Serialization/ASTBitCodes.h"
Douglas Gregorde95ead2012-01-06 16:09:53 +000018#include "llvm/Support/Endian.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000019#include "llvm/Support/OnDiskHashTable.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include <utility>
Douglas Gregord44252e2011-08-25 20:47:51 +000021
22namespace clang {
23
24class ASTReader;
25class HeaderSearch;
26struct HeaderFileInfo;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000027class FileEntry;
Douglas Gregord44252e2011-08-25 20:47:51 +000028
29namespace serialization {
30
Douglas Gregorde3ef502011-11-30 23:21:26 +000031class ModuleFile;
Douglas Gregord44252e2011-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 Gregorde3ef502011-11-30 23:21:26 +000039 ModuleFile &F;
Douglas Gregord44252e2011-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 Gregorde95ead2012-01-06 16:09:53 +000046 typedef llvm::support::ulittle32_t LE32DeclID;
47 typedef std::pair<LE32DeclID *, LE32DeclID *> data_type;
Justin Bogner25463f12014-04-18 20:27:24 +000048 typedef unsigned hash_value_type;
49 typedef unsigned offset_type;
Douglas Gregord44252e2011-08-25 20:47:51 +000050
51 /// \brief Special internal key for declaration names.
52 /// The hash table creates keys for comparison; we do not create
53 /// a DeclarationName for the internal key to avoid deserializing types.
54 struct DeclNameKey {
55 DeclarationName::NameKind Kind;
56 uint64_t Data;
57 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
58 };
59
60 typedef DeclarationName external_key_type;
61 typedef DeclNameKey internal_key_type;
62
Nick Lewycky2bd0ab22012-04-16 02:51:46 +000063 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregord44252e2011-08-25 20:47:51 +000064 : Reader(Reader), F(F) { }
65
66 static bool EqualKey(const internal_key_type& a,
67 const internal_key_type& b) {
68 return a.Kind == b.Kind && a.Data == b.Data;
69 }
70
Justin Bogner25463f12014-04-18 20:27:24 +000071 hash_value_type ComputeHash(const DeclNameKey &Key) const;
Douglas Gregord44252e2011-08-25 20:47:51 +000072 internal_key_type GetInternalKey(const external_key_type& Name) const;
Douglas Gregord44252e2011-08-25 20:47:51 +000073
Nick Lewycky2bd0ab22012-04-16 02:51:46 +000074 static std::pair<unsigned, unsigned>
Douglas Gregord44252e2011-08-25 20:47:51 +000075 ReadKeyDataLength(const unsigned char*& d);
76
77 internal_key_type ReadKey(const unsigned char* d, unsigned);
78
79 data_type ReadData(internal_key_type, const unsigned char* d,
80 unsigned DataLen);
81};
82
Douglas Gregorbfd73d72013-01-23 18:53:14 +000083/// \brief Base class for the trait describing the on-disk hash table for the
84/// identifiers in an AST file.
85///
86/// This class is not useful by itself; rather, it provides common
87/// functionality for accessing the on-disk hash table of identifiers
88/// in an AST file. Different subclasses customize that functionality
89/// based on what information they are interested in. Those subclasses
90/// must provide the \c data_type typedef and the ReadData operation,
91/// only.
92class ASTIdentifierLookupTraitBase {
93public:
94 typedef StringRef external_key_type;
95 typedef StringRef internal_key_type;
Justin Bogner25463f12014-04-18 20:27:24 +000096 typedef unsigned hash_value_type;
97 typedef unsigned offset_type;
Douglas Gregorbfd73d72013-01-23 18:53:14 +000098
99 static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
100 return a == b;
101 }
102
Justin Bogner25463f12014-04-18 20:27:24 +0000103 static hash_value_type ComputeHash(const internal_key_type& a);
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000104
105 static std::pair<unsigned, unsigned>
106 ReadKeyDataLength(const unsigned char*& d);
107
108 // This hopefully will just get inlined and removed by the optimizer.
109 static const internal_key_type&
110 GetInternalKey(const external_key_type& x) { return x; }
111
112 // This hopefully will just get inlined and removed by the optimizer.
113 static const external_key_type&
114 GetExternalKey(const internal_key_type& x) { return x; }
115
116 static internal_key_type ReadKey(const unsigned char* d, unsigned n);
117};
118
Douglas Gregord44252e2011-08-25 20:47:51 +0000119/// \brief Class that performs lookup for an identifier stored in an AST file.
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000120class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase {
Douglas Gregord44252e2011-08-25 20:47:51 +0000121 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000122 ModuleFile &F;
Douglas Gregord44252e2011-08-25 20:47:51 +0000123
124 // If we know the IdentifierInfo in advance, it is here and we will
125 // not build a new one. Used when deserializing information about an
126 // identifier that was constructed before the AST file was read.
127 IdentifierInfo *KnownII;
128
129public:
130 typedef IdentifierInfo * data_type;
Craig Toppera13603a2014-05-22 05:54:18 +0000131
Douglas Gregorde3ef502011-11-30 23:21:26 +0000132 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
Craig Toppera13603a2014-05-22 05:54:18 +0000133 IdentifierInfo *II = nullptr)
Douglas Gregord44252e2011-08-25 20:47:51 +0000134 : Reader(Reader), F(F), KnownII(II) { }
Craig Toppera13603a2014-05-22 05:54:18 +0000135
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000136 data_type ReadData(const internal_key_type& k,
137 const unsigned char* d,
138 unsigned DataLen);
Douglas Gregor247afcc2012-01-24 15:24:38 +0000139
140 ASTReader &getReader() const { return Reader; }
Douglas Gregord44252e2011-08-25 20:47:51 +0000141};
142
143/// \brief The on-disk hash table used to contain information about
144/// all of the identifiers in the program.
Justin Bognerbb094f02014-04-18 19:57:06 +0000145typedef llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait>
Douglas Gregord44252e2011-08-25 20:47:51 +0000146 ASTIdentifierLookupTable;
147
148/// \brief Class that performs lookup for a selector's entries in the global
149/// method pool stored in an AST file.
150class ASTSelectorLookupTrait {
151 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000152 ModuleFile &F;
Douglas Gregord44252e2011-08-25 20:47:51 +0000153
154public:
155 struct data_type {
156 SelectorID ID;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +0000157 unsigned InstanceBits;
158 unsigned FactoryBits;
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000159 SmallVector<ObjCMethodDecl *, 2> Instance;
160 SmallVector<ObjCMethodDecl *, 2> Factory;
Douglas Gregord44252e2011-08-25 20:47:51 +0000161 };
162
163 typedef Selector external_key_type;
164 typedef external_key_type internal_key_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000165 typedef unsigned hash_value_type;
166 typedef unsigned offset_type;
Douglas Gregord44252e2011-08-25 20:47:51 +0000167
Douglas Gregorde3ef502011-11-30 23:21:26 +0000168 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregord44252e2011-08-25 20:47:51 +0000169 : Reader(Reader), F(F) { }
170
171 static bool EqualKey(const internal_key_type& a,
172 const internal_key_type& b) {
173 return a == b;
174 }
175
Justin Bogner25463f12014-04-18 20:27:24 +0000176 static hash_value_type ComputeHash(Selector Sel);
Douglas Gregord44252e2011-08-25 20:47:51 +0000177
178 static const internal_key_type&
Chris Lattnerd2cd41c2011-09-10 16:13:42 +0000179 GetInternalKey(const external_key_type& x) { return x; }
Douglas Gregord44252e2011-08-25 20:47:51 +0000180
181 static std::pair<unsigned, unsigned>
182 ReadKeyDataLength(const unsigned char*& d);
183
184 internal_key_type ReadKey(const unsigned char* d, unsigned);
185 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
186};
187
188/// \brief The on-disk hash table used for the global method pool.
Justin Bognerbb094f02014-04-18 19:57:06 +0000189typedef llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait>
Douglas Gregord44252e2011-08-25 20:47:51 +0000190 ASTSelectorLookupTable;
191
192/// \brief Trait class used to search the on-disk hash table containing all of
193/// the header search information.
194///
195/// The on-disk hash table contains a mapping from each header path to
196/// information about that header (how many times it has been included, its
197/// controlling macro, etc.). Note that we actually hash based on the
198/// filename, and support "deep" comparisons of file names based on current
199/// inode numbers, so that the search can cope with non-normalized path names
200/// and symlinks.
201class HeaderFileInfoTrait {
202 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000203 ModuleFile &M;
Douglas Gregord44252e2011-08-25 20:47:51 +0000204 HeaderSearch *HS;
205 const char *FrameworkStrings;
Ted Kremenek03cb1372013-02-05 06:21:59 +0000206
Douglas Gregord44252e2011-08-25 20:47:51 +0000207public:
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +0000208 typedef const FileEntry *external_key_type;
209
210 struct internal_key_type {
211 off_t Size;
212 time_t ModTime;
213 const char *Filename;
214 };
215 typedef const internal_key_type &internal_key_ref;
Douglas Gregord44252e2011-08-25 20:47:51 +0000216
217 typedef HeaderFileInfo data_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000218 typedef unsigned hash_value_type;
219 typedef unsigned offset_type;
Douglas Gregord44252e2011-08-25 20:47:51 +0000220
Douglas Gregorde3ef502011-11-30 23:21:26 +0000221 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
Argyrios Kyrtzidisb42863e2013-03-06 18:12:41 +0000222 const char *FrameworkStrings)
223 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { }
Douglas Gregord44252e2011-08-25 20:47:51 +0000224
Justin Bogner25463f12014-04-18 20:27:24 +0000225 static hash_value_type ComputeHash(internal_key_ref ikey);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +0000226 static internal_key_type GetInternalKey(const FileEntry *FE);
227 bool EqualKey(internal_key_ref a, internal_key_ref b);
Douglas Gregord44252e2011-08-25 20:47:51 +0000228
229 static std::pair<unsigned, unsigned>
230 ReadKeyDataLength(const unsigned char*& d);
231
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +0000232 static internal_key_type ReadKey(const unsigned char *d, unsigned);
Douglas Gregord44252e2011-08-25 20:47:51 +0000233
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +0000234 data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
Douglas Gregord44252e2011-08-25 20:47:51 +0000235};
236
237/// \brief The on-disk hash table used for known header files.
Justin Bognerbb094f02014-04-18 19:57:06 +0000238typedef llvm::OnDiskChainedHashTable<HeaderFileInfoTrait>
Douglas Gregord44252e2011-08-25 20:47:51 +0000239 HeaderFileInfoLookupTable;
240
241} // end namespace clang::serialization::reader
242} // end namespace clang::serialization
243} // end namespace clang
244
245
246#endif