1 //===--- ASTReaderInternals.h - AST Reader Internals ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides internal definitions used in the AST reader.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
14 #define LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
16 #include "MultiOnDiskHashTable.h"
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/Serialization/ASTBitCodes.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/OnDiskHashTable.h"
28 struct HeaderFileInfo;
31 namespace serialization {
37 /// \brief Class that performs name lookup into a DeclContext stored
39 class ASTDeclContextNameLookupTrait {
44 // Maximum number of lookup tables we allow before condensing the tables.
45 static const int MaxTables = 4;
47 /// The lookup result is a list of global declaration IDs.
48 typedef llvm::SmallVector<DeclID, 4> data_type;
49 struct data_type_builder {
51 llvm::DenseSet<DeclID> Found;
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) {
65 // Switch to tracking found IDs in the set.
66 Found.insert(Data.begin(), Data.end());
69 if (Found.insert(ID).second)
73 typedef unsigned hash_value_type;
74 typedef unsigned offset_type;
75 typedef ModuleFile *file_type;
77 typedef DeclarationName external_key_type;
78 typedef DeclarationNameKey internal_key_type;
80 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
81 : Reader(Reader), F(F) { }
83 static bool EqualKey(const internal_key_type &a, const internal_key_type &b) {
87 static hash_value_type ComputeHash(const internal_key_type &Key) {
90 static internal_key_type GetInternalKey(const external_key_type &Name) {
94 static std::pair<unsigned, unsigned>
95 ReadKeyDataLength(const unsigned char *&d);
97 internal_key_type ReadKey(const unsigned char *d, unsigned);
99 void ReadDataInto(internal_key_type, const unsigned char *d,
100 unsigned DataLen, data_type_builder &Val);
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)
108 file_type ReadFileRef(const unsigned char *&d);
111 struct DeclContextLookupTable {
112 MultiOnDiskHashTable<ASTDeclContextNameLookupTrait> Table;
115 /// \brief Base class for the trait describing the on-disk hash table for the
116 /// identifiers in an AST file.
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,
124 class ASTIdentifierLookupTraitBase {
126 typedef StringRef external_key_type;
127 typedef StringRef internal_key_type;
128 typedef unsigned hash_value_type;
129 typedef unsigned offset_type;
131 static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
135 static hash_value_type ComputeHash(const internal_key_type& a);
137 static std::pair<unsigned, unsigned>
138 ReadKeyDataLength(const unsigned char*& d);
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; }
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; }
148 static internal_key_type ReadKey(const unsigned char* d, unsigned n);
151 /// \brief Class that performs lookup for an identifier stored in an AST file.
152 class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase {
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;
162 typedef IdentifierInfo * data_type;
164 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
165 IdentifierInfo *II = nullptr)
166 : Reader(Reader), F(F), KnownII(II) { }
168 data_type ReadData(const internal_key_type& k,
169 const unsigned char* d,
172 IdentID ReadIdentifierID(const unsigned char *d);
174 ASTReader &getReader() const { return Reader; }
177 /// \brief The on-disk hash table used to contain information about
178 /// all of the identifiers in the program.
179 typedef llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait>
180 ASTIdentifierLookupTable;
182 /// \brief Class that performs lookup for a selector's entries in the global
183 /// method pool stored in an AST file.
184 class ASTSelectorLookupTrait {
191 unsigned InstanceBits;
192 unsigned FactoryBits;
193 bool InstanceHasMoreThanOneDecl;
194 bool FactoryHasMoreThanOneDecl;
195 SmallVector<ObjCMethodDecl *, 2> Instance;
196 SmallVector<ObjCMethodDecl *, 2> Factory;
199 typedef Selector external_key_type;
200 typedef external_key_type internal_key_type;
201 typedef unsigned hash_value_type;
202 typedef unsigned offset_type;
204 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
205 : Reader(Reader), F(F) { }
207 static bool EqualKey(const internal_key_type& a,
208 const internal_key_type& b) {
212 static hash_value_type ComputeHash(Selector Sel);
214 static const internal_key_type&
215 GetInternalKey(const external_key_type& x) { return x; }
217 static std::pair<unsigned, unsigned>
218 ReadKeyDataLength(const unsigned char*& d);
220 internal_key_type ReadKey(const unsigned char* d, unsigned);
221 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
224 /// \brief The on-disk hash table used for the global method pool.
225 typedef llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait>
226 ASTSelectorLookupTable;
228 /// \brief Trait class used to search the on-disk hash table containing all of
229 /// the header search information.
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
233 /// 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
235 /// inode numbers, so that the search can cope with non-normalized path names
237 class HeaderFileInfoTrait {
241 const char *FrameworkStrings;
244 typedef const FileEntry *external_key_type;
246 struct internal_key_type {
252 typedef const internal_key_type &internal_key_ref;
254 typedef HeaderFileInfo data_type;
255 typedef unsigned hash_value_type;
256 typedef unsigned offset_type;
258 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
259 const char *FrameworkStrings)
260 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { }
262 static hash_value_type ComputeHash(internal_key_ref ikey);
263 internal_key_type GetInternalKey(const FileEntry *FE);
264 bool EqualKey(internal_key_ref a, internal_key_ref b);
266 static std::pair<unsigned, unsigned>
267 ReadKeyDataLength(const unsigned char*& d);
269 static internal_key_type ReadKey(const unsigned char *d, unsigned);
271 data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
274 /// \brief The on-disk hash table used for known header files.
275 typedef llvm::OnDiskChainedHashTable<HeaderFileInfoTrait>
276 HeaderFileInfoLookupTable;
278 } // end namespace clang::serialization::reader
279 } // end namespace clang::serialization
280 } // end namespace clang