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;
34class Module;
35
36namespace serialization {
37
38class ModuleFile;
39
40namespace reader {
41
43protected:
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
83protected:
85 : Reader(Reader), F(F) {}
86
87public:
88 static std::pair<unsigned, unsigned>
89 ReadKeyDataLength(const unsigned char *&d);
90
91 void ReadDataIntoImpl(const unsigned char *d, unsigned DataLen,
92 data_type_builder &Val);
93
94 static void MergeDataInto(const data_type &From, data_type_builder &To) {
95 To.Data.reserve(To.Data.size() + From.size());
96 for (GlobalDeclID ID : From)
97 To.insert(ID);
98 }
99
100 file_type ReadFileRef(const unsigned char *&d);
101
102 DeclarationNameKey ReadKeyBase(const unsigned char *&d);
103};
104
105/// Class that performs name lookup into a DeclContext stored
106/// in an AST file.
108public:
111
114
115 static bool EqualKey(const internal_key_type &a, const internal_key_type &b) {
116 return a == b;
117 }
118
120 return Key.getHash();
121 }
122
124 return Name;
125 }
126
127 internal_key_type ReadKey(const unsigned char *d, unsigned);
128
129 void ReadDataInto(internal_key_type, const unsigned char *d,
130 unsigned DataLen, data_type_builder &Val);
131};
132
135};
136
138public:
141
142 using external_key_type = std::pair<DeclarationName, const Module *>;
143 using internal_key_type = std::pair<DeclarationNameKey, unsigned>;
144
145 static bool EqualKey(const internal_key_type &a, const internal_key_type &b) {
146 return a == b;
147 }
148
151
152 internal_key_type ReadKey(const unsigned char *d, unsigned);
153
154 void ReadDataInto(internal_key_type, const unsigned char *d, unsigned DataLen,
155 data_type_builder &Val);
156};
157
160};
161
163
164/// Class that performs lookup to specialized decls.
166 ASTReader &Reader;
167 ModuleFile &F;
168
169public:
170 // Maximum number of lookup tables we allow before condensing the tables.
171 static const int MaxTables = 4;
172
173 /// The lookup result is a list of global declaration IDs.
175
178 llvm::DenseSet<LazySpecializationInfo> Found;
179
181
183 // Just use a linear scan unless we have more than a few IDs.
184 if (Found.empty() && !Data.empty()) {
185 if (Data.size() <= 4) {
186 for (auto I : Found)
187 if (I == Info)
188 return;
189 Data.push_back(Info);
190 return;
191 }
192
193 // Switch to tracking found IDs in the set.
194 Found.insert(Data.begin(), Data.end());
195 }
196
197 if (Found.insert(Info).second)
198 Data.push_back(Info);
199 }
200 };
204
207
209 : Reader(Reader), F(F) {}
210
211 static bool EqualKey(const internal_key_type &a, const internal_key_type &b) {
212 return a == b;
213 }
214
216 return Key;
217 }
218
220 return Name;
221 }
222
223 static std::pair<unsigned, unsigned>
224 ReadKeyDataLength(const unsigned char *&d);
225
226 internal_key_type ReadKey(const unsigned char *d, unsigned);
227
228 void ReadDataInto(internal_key_type, const unsigned char *d, unsigned DataLen,
229 data_type_builder &Val);
230
231 static void MergeDataInto(const data_type &From, data_type_builder &To) {
232 To.Data.reserve(To.Data.size() + From.size());
233 for (LazySpecializationInfo Info : From)
234 To.insert(Info);
235 }
236
237 file_type ReadFileRef(const unsigned char *&d);
238};
239
242};
243
244/// Base class for the trait describing the on-disk hash table for the
245/// identifiers in an AST file.
246///
247/// This class is not useful by itself; rather, it provides common
248/// functionality for accessing the on-disk hash table of identifiers
249/// in an AST file. Different subclasses customize that functionality
250/// based on what information they are interested in. Those subclasses
251/// must provide the \c data_type type and the ReadData operation, only.
253public:
254 using external_key_type = StringRef;
255 using internal_key_type = StringRef;
258
259 static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
260 return a == b;
261 }
262
264
265 static std::pair<unsigned, unsigned>
266 ReadKeyDataLength(const unsigned char*& d);
267
268 // This hopefully will just get inlined and removed by the optimizer.
269 static const internal_key_type&
270 GetInternalKey(const external_key_type& x) { return x; }
271
272 // This hopefully will just get inlined and removed by the optimizer.
273 static const external_key_type&
274 GetExternalKey(const internal_key_type& x) { return x; }
275
276 static internal_key_type ReadKey(const unsigned char* d, unsigned n);
277};
278
279/// Class that performs lookup for an identifier stored in an AST file.
281 ASTReader &Reader;
282 ModuleFile &F;
283
284 // If we know the IdentifierInfo in advance, it is here and we will
285 // not build a new one. Used when deserializing information about an
286 // identifier that was constructed before the AST file was read.
287 IdentifierInfo *KnownII;
288
289public:
291
293 IdentifierInfo *II = nullptr)
294 : Reader(Reader), F(F), KnownII(II) {}
295
297 const unsigned char* d,
298 unsigned DataLen);
299
300 IdentifierID ReadIdentifierID(const unsigned char *d);
301
302 ASTReader &getReader() const { return Reader; }
303};
304
305/// The on-disk hash table used to contain information about
306/// all of the identifiers in the program.
308 llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait>;
309
310/// Class that performs lookup for a selector's entries in the global
311/// method pool stored in an AST file.
313 ASTReader &Reader;
314 ModuleFile &F;
315
316public:
317 struct data_type {
319 unsigned InstanceBits;
320 unsigned FactoryBits;
325 };
326
331
333 : Reader(Reader), F(F) {}
334
335 static bool EqualKey(const internal_key_type& a,
336 const internal_key_type& b) {
337 return a == b;
338 }
339
341
342 static const internal_key_type&
343 GetInternalKey(const external_key_type& x) { return x; }
344
345 static std::pair<unsigned, unsigned>
346 ReadKeyDataLength(const unsigned char*& d);
347
348 internal_key_type ReadKey(const unsigned char* d, unsigned);
349 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
350};
351
352/// The on-disk hash table used for the global method pool.
354 llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait>;
355
356/// Trait class used to search the on-disk hash table containing all of
357/// the header search information.
358///
359/// The on-disk hash table contains a mapping from each header path to
360/// information about that header (how many times it has been included, its
361/// controlling macro, etc.). Note that we actually hash based on the size
362/// and mtime, and support "deep" comparisons of file names based on current
363/// inode numbers, so that the search can cope with non-normalized path names
364/// and symlinks.
366 ASTReader &Reader;
367 ModuleFile &M;
368
369public:
371
373 off_t Size;
374 time_t ModTime;
375 StringRef Filename;
377 };
378
380
384
386 : Reader(Reader), M(M) {}
387
389 internal_key_type GetInternalKey(external_key_type ekey);
391
392 static std::pair<unsigned, unsigned>
393 ReadKeyDataLength(const unsigned char*& d);
394
395 static internal_key_type ReadKey(const unsigned char *d, unsigned);
396
397 data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
398
399private:
400 OptionalFileEntryRef getFile(const internal_key_type &Key);
401};
402
403/// The on-disk hash table used for known header files.
405 llvm::OnDiskChainedHashTable<HeaderFileInfoTrait>;
406
407} // namespace reader
408
409} // namespace serialization
410
411} // namespace clang
412
413#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:384
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:2115
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.
static void MergeDataInto(const data_type &From, data_type_builder &To)
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:1247
void ReadDataIntoImpl(const unsigned char *d, unsigned DataLen, data_type_builder &Val)
Definition: ASTReader.cpp:1291
DeclarationNameKey ReadKeyBase(const unsigned char *&d)
Definition: ASTReader.cpp:1252
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:1287
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:1302
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:2220
internal_key_type GetInternalKey(external_key_type ekey)
Definition: ASTReader.cpp:2199
bool EqualKey(internal_key_ref a, internal_key_ref b)
Definition: ASTReader.cpp:2206
static hash_value_type ComputeHash(internal_key_ref ikey)
Definition: ASTReader.cpp:2191
data_type ReadData(internal_key_ref, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:2238
static internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:2225
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:1363
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:1359
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:1353
ModuleLocalNameLookupTrait(ASTReader &Reader, ModuleFile &F)
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
std::pair< DeclarationName, const Module * > external_key_type
void ReadDataInto(internal_key_type, const unsigned char *d, unsigned DataLen, data_type_builder &Val)
Definition: ASTReader.cpp:1336
std::pair< DeclarationNameKey, unsigned > internal_key_type
internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:1329
static hash_value_type ComputeHash(const internal_key_type &Key)
Definition: ASTReader.cpp:1310
static internal_key_type GetInternalKey(const external_key_type &Key)
Definition: ASTReader.cpp:1318
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.
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
The preprocessor keeps track of this information for each file that is #included.
Definition: HeaderSearch.h:59
MultiOnDiskHashTable< ASTDeclContextNameLookupTrait > Table
MultiOnDiskHashTable< LazySpecializationInfoLookupTrait > Table
MultiOnDiskHashTable< ModuleLocalNameLookupTrait > Table