clang 20.0.0git
ASTReaderInternals.h
Go to the documentation of this file.
1//===- ASTReaderInternals.h - AST Reader Internals --------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides internal definitions used in the AST reader.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
14#define LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
15
18#include "clang/Basic/LLVM.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/OnDiskHashTable.h"
24#include <ctime>
25#include <utility>
26
27namespace clang {
28
29class ASTReader;
30class FileEntry;
31struct HeaderFileInfo;
32class HeaderSearch;
33class ObjCMethodDecl;
34
35namespace serialization {
36
37class ModuleFile;
38
39namespace reader {
40
41/// Class that performs name lookup into a DeclContext stored
42/// in an AST file.
44 ASTReader &Reader;
45 ModuleFile &F;
46
47public:
48 // Maximum number of lookup tables we allow before condensing the tables.
49 static const int MaxTables = 4;
50
51 /// The lookup result is a list of global declaration IDs.
53
56 llvm::DenseSet<GlobalDeclID> Found;
57
59
61 // Just use a linear scan unless we have more than a few IDs.
62 if (Found.empty() && !Data.empty()) {
63 if (Data.size() <= 4) {
64 for (auto I : Found)
65 if (I == ID)
66 return;
67 Data.push_back(ID);
68 return;
69 }
70
71 // Switch to tracking found IDs in the set.
72 Found.insert(Data.begin(), Data.end());
73 }
74
75 if (Found.insert(ID).second)
76 Data.push_back(ID);
77 }
78 };
82
85
87 : Reader(Reader), F(F) {}
88
89 static bool EqualKey(const internal_key_type &a, const internal_key_type &b) {
90 return a == b;
91 }
92
94 return Key.getHash();
95 }
96
98 return Name;
99 }
100
101 static std::pair<unsigned, unsigned>
102 ReadKeyDataLength(const unsigned char *&d);
103
104 internal_key_type ReadKey(const unsigned char *d, unsigned);
105
106 void ReadDataInto(internal_key_type, const unsigned char *d,
107 unsigned DataLen, data_type_builder &Val);
108
109 static void MergeDataInto(const data_type &From, data_type_builder &To) {
110 To.Data.reserve(To.Data.size() + From.size());
111 for (GlobalDeclID ID : From)
112 To.insert(ID);
113 }
114
115 file_type ReadFileRef(const unsigned char *&d);
116};
117
120};
121
123
124/// Class that performs lookup to specialized decls.
126 ASTReader &Reader;
127 ModuleFile &F;
128
129public:
130 // Maximum number of lookup tables we allow before condensing the tables.
131 static const int MaxTables = 4;
132
133 /// The lookup result is a list of global declaration IDs.
135
138 llvm::DenseSet<LazySpecializationInfo> Found;
139
141
143 // Just use a linear scan unless we have more than a few IDs.
144 if (Found.empty() && !Data.empty()) {
145 if (Data.size() <= 4) {
146 for (auto I : Found)
147 if (I == Info)
148 return;
149 Data.push_back(Info);
150 return;
151 }
152
153 // Switch to tracking found IDs in the set.
154 Found.insert(Data.begin(), Data.end());
155 }
156
157 if (Found.insert(Info).second)
158 Data.push_back(Info);
159 }
160 };
164
167
169 : Reader(Reader), F(F) {}
170
171 static bool EqualKey(const internal_key_type &a, const internal_key_type &b) {
172 return a == b;
173 }
174
176 return Key;
177 }
178
180 return Name;
181 }
182
183 static std::pair<unsigned, unsigned>
184 ReadKeyDataLength(const unsigned char *&d);
185
186 internal_key_type ReadKey(const unsigned char *d, unsigned);
187
188 void ReadDataInto(internal_key_type, const unsigned char *d, unsigned DataLen,
189 data_type_builder &Val);
190
191 static void MergeDataInto(const data_type &From, data_type_builder &To) {
192 To.Data.reserve(To.Data.size() + From.size());
193 for (LazySpecializationInfo Info : From)
194 To.insert(Info);
195 }
196
197 file_type ReadFileRef(const unsigned char *&d);
198};
199
202};
203
204/// Base class for the trait describing the on-disk hash table for the
205/// identifiers in an AST file.
206///
207/// This class is not useful by itself; rather, it provides common
208/// functionality for accessing the on-disk hash table of identifiers
209/// in an AST file. Different subclasses customize that functionality
210/// based on what information they are interested in. Those subclasses
211/// must provide the \c data_type type and the ReadData operation, only.
213public:
214 using external_key_type = StringRef;
215 using internal_key_type = StringRef;
218
219 static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
220 return a == b;
221 }
222
224
225 static std::pair<unsigned, unsigned>
226 ReadKeyDataLength(const unsigned char*& d);
227
228 // This hopefully will just get inlined and removed by the optimizer.
229 static const internal_key_type&
230 GetInternalKey(const external_key_type& x) { return x; }
231
232 // This hopefully will just get inlined and removed by the optimizer.
233 static const external_key_type&
234 GetExternalKey(const internal_key_type& x) { return x; }
235
236 static internal_key_type ReadKey(const unsigned char* d, unsigned n);
237};
238
239/// Class that performs lookup for an identifier stored in an AST file.
241 ASTReader &Reader;
242 ModuleFile &F;
243
244 // If we know the IdentifierInfo in advance, it is here and we will
245 // not build a new one. Used when deserializing information about an
246 // identifier that was constructed before the AST file was read.
247 IdentifierInfo *KnownII;
248
249public:
251
253 IdentifierInfo *II = nullptr)
254 : Reader(Reader), F(F), KnownII(II) {}
255
257 const unsigned char* d,
258 unsigned DataLen);
259
260 IdentifierID ReadIdentifierID(const unsigned char *d);
261
262 ASTReader &getReader() const { return Reader; }
263};
264
265/// The on-disk hash table used to contain information about
266/// all of the identifiers in the program.
268 llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait>;
269
270/// Class that performs lookup for a selector's entries in the global
271/// method pool stored in an AST file.
273 ASTReader &Reader;
274 ModuleFile &F;
275
276public:
277 struct data_type {
279 unsigned InstanceBits;
280 unsigned FactoryBits;
285 };
286
291
293 : Reader(Reader), F(F) {}
294
295 static bool EqualKey(const internal_key_type& a,
296 const internal_key_type& b) {
297 return a == b;
298 }
299
301
302 static const internal_key_type&
303 GetInternalKey(const external_key_type& x) { return x; }
304
305 static std::pair<unsigned, unsigned>
306 ReadKeyDataLength(const unsigned char*& d);
307
308 internal_key_type ReadKey(const unsigned char* d, unsigned);
309 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
310};
311
312/// The on-disk hash table used for the global method pool.
314 llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait>;
315
316/// Trait class used to search the on-disk hash table containing all of
317/// the header search information.
318///
319/// The on-disk hash table contains a mapping from each header path to
320/// information about that header (how many times it has been included, its
321/// controlling macro, etc.). Note that we actually hash based on the size
322/// and mtime, and support "deep" comparisons of file names based on current
323/// inode numbers, so that the search can cope with non-normalized path names
324/// and symlinks.
326 ASTReader &Reader;
327 ModuleFile &M;
328
329public:
331
333 off_t Size;
334 time_t ModTime;
335 StringRef Filename;
337 };
338
340
344
346 : Reader(Reader), M(M) {}
347
349 internal_key_type GetInternalKey(external_key_type ekey);
351
352 static std::pair<unsigned, unsigned>
353 ReadKeyDataLength(const unsigned char*& d);
354
355 static internal_key_type ReadKey(const unsigned char *d, unsigned);
356
357 data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
358
359private:
360 OptionalFileEntryRef getFile(const internal_key_type &Key);
361};
362
363/// The on-disk hash table used for known header files.
365 llvm::OnDiskChainedHashTable<HeaderFileInfoTrait>;
366
367} // namespace reader
368
369} // namespace serialization
370
371} // namespace clang
372
373#endif // LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
static char ID
Definition: Arena.cpp:183
const Decl * D
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
__device__ __2f16 b
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:383
The name of a declaration.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
One of these records is kept for each identifier that is lexed.
Smart pointer class that efficiently represents Objective-C method names.
A key used when looking up entities by DeclarationName.
Definition: ASTBitCodes.h:2107
unsigned getHash() const
Compute a fingerprint of this key for use in on-disk hash table.
Definition: ASTReader.cpp:1209
Information about a module that has been loaded by the ASTReader.
Definition: ModuleFile.h:130
A collection of on-disk hash tables, merged when relevant for performance.
Class that performs name lookup into a DeclContext stored in an AST file.
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:1252
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:1247
static internal_key_type GetInternalKey(const external_key_type &Name)
static void MergeDataInto(const data_type &From, data_type_builder &To)
void ReadDataInto(internal_key_type, const unsigned char *d, unsigned DataLen, data_type_builder &Val)
Definition: ASTReader.cpp:1286
static hash_value_type ComputeHash(const internal_key_type &Key)
Base class for the trait describing the on-disk hash table for the identifiers in an AST file.
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:1047
static const internal_key_type & GetInternalKey(const external_key_type &x)
static hash_value_type ComputeHash(const internal_key_type &a)
Definition: ASTReader.cpp:1042
static const external_key_type & GetExternalKey(const internal_key_type &x)
static internal_key_type ReadKey(const unsigned char *d, unsigned n)
Definition: ASTReader.cpp:1052
Class that performs lookup for an identifier stored in an AST file.
ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F, IdentifierInfo *II=nullptr)
IdentifierID ReadIdentifierID(const unsigned char *d)
Definition: ASTReader.cpp:1076
data_type ReadData(const internal_key_type &k, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:1093
Class that performs lookup for a selector's entries in the global method pool stored in an AST file.
internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:980
ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
data_type ReadData(Selector, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:1002
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:975
static hash_value_type ComputeHash(Selector Sel)
Definition: ASTReader.cpp:937
static const internal_key_type & GetInternalKey(const external_key_type &x)
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
Trait class used to search the on-disk hash table containing all of the header search information.
HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M)
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:2169
internal_key_type GetInternalKey(external_key_type ekey)
Definition: ASTReader.cpp:2148
bool EqualKey(internal_key_ref a, internal_key_ref b)
Definition: ASTReader.cpp:2155
static hash_value_type ComputeHash(internal_key_ref ikey)
Definition: ASTReader.cpp:2140
data_type ReadData(internal_key_ref, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:2187
static internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:2174
Class that performs lookup to specialized decls.
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
static internal_key_type GetInternalKey(const external_key_type &Name)
void ReadDataInto(internal_key_type, const unsigned char *d, unsigned DataLen, data_type_builder &Val)
Definition: ASTReader.cpp:1319
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:1315
static void MergeDataInto(const data_type &From, data_type_builder &To)
static hash_value_type ComputeHash(const internal_key_type &Key)
internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:1309
llvm::OnDiskChainedHashTable< HeaderFileInfoTrait > HeaderFileInfoLookupTable
The on-disk hash table used for known header files.
llvm::OnDiskIterableChainedHashTable< ASTIdentifierLookupTrait > ASTIdentifierLookupTable
The on-disk hash table used to contain information about all of the identifiers in the program.
llvm::OnDiskChainedHashTable< ASTSelectorLookupTrait > ASTSelectorLookupTable
The on-disk hash table used for the global method pool.
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
Definition: ASTBitCodes.h:167
uint64_t IdentifierID
An ID number that refers to an identifier in an AST file.
Definition: ASTBitCodes.h:63
@ ModuleFile
The module file (.pcm). Required.
@ HeaderSearch
Remove unused header search paths including header maps.
The JSON file list parser is used to communicate input to InstallAPI.
The preprocessor keeps track of this information for each file that is #included.
Definition: HeaderSearch.h:59
MultiOnDiskHashTable< ASTDeclContextNameLookupTrait > Table
MultiOnDiskHashTable< LazySpecializationInfoLookupTrait > Table