clang 22.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_range(Data);
73 }
74
75 if (Found.insert(ID).second)
76 Data.push_back(ID);
77 }
78 };
82
83protected:
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
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
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_range(Data);
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
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
289 bool hasMacroDefinitionInDependencies = false;
290
291public:
293
295 IdentifierInfo *II = nullptr)
296 : Reader(Reader), F(F), KnownII(II) {}
297
299 const unsigned char* d,
300 unsigned DataLen);
301
302 IdentifierID ReadIdentifierID(const unsigned char *d);
303
304 ASTReader &getReader() const { return Reader; }
305
307 return hasMacroDefinitionInDependencies;
308 }
309};
310
311/// The on-disk hash table used to contain information about
312/// all of the identifiers in the program.
314 llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait>;
315
316/// Class that performs lookup for a selector's entries in the global
317/// method pool stored in an AST file.
319 ASTReader &Reader;
320 ModuleFile &F;
321
322public:
332
337
339 : Reader(Reader), F(F) {}
340
341 static bool EqualKey(const internal_key_type& a,
342 const internal_key_type& b) {
343 return a == b;
344 }
345
347
348 static const internal_key_type&
349 GetInternalKey(const external_key_type& x) { return x; }
350
351 static std::pair<unsigned, unsigned>
352 ReadKeyDataLength(const unsigned char*& d);
353
354 internal_key_type ReadKey(const unsigned char* d, unsigned);
355 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
356};
357
358/// The on-disk hash table used for the global method pool.
360 llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait>;
361
362/// Trait class used to search the on-disk hash table containing all of
363/// the header search information.
364///
365/// The on-disk hash table contains a mapping from each header path to
366/// information about that header (how many times it has been included, its
367/// controlling macro, etc.). Note that we actually hash based on the size
368/// and mtime, and support "deep" comparisons of file names based on current
369/// inode numbers, so that the search can cope with non-normalized path names
370/// and symlinks.
372 ASTReader &Reader;
373 ModuleFile &M;
374
375public:
377
379 off_t Size;
380 time_t ModTime;
381 StringRef Filename;
383 };
384
386
390
392 : Reader(Reader), M(M) {}
393
395 internal_key_type GetInternalKey(external_key_type ekey);
397
398 static std::pair<unsigned, unsigned>
399 ReadKeyDataLength(const unsigned char*& d);
400
401 static internal_key_type ReadKey(const unsigned char *d, unsigned);
402
403 data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
404
405private:
406 OptionalFileEntryRef getFile(const internal_key_type &Key);
407};
408
409/// The on-disk hash table used for known header files.
411 llvm::OnDiskChainedHashTable<HeaderFileInfoTrait>;
412
413} // namespace reader
414
415} // namespace serialization
416
417} // namespace clang
418
419#endif // LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
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:430
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
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:306
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
One of these records is kept for each identifier that is lexed.
Describes a module or submodule.
Definition Module.h:144
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
Smart pointer class that efficiently represents Objective-C method names.
A key used when looking up entities by DeclarationName.
unsigned getHash() const
Compute a fingerprint of this key for use in on-disk hash table.
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.
SmallVector< GlobalDeclID, 4 > data_type
The lookup result is a list of global declaration IDs.
static void MergeDataInto(const data_type &From, data_type_builder &To)
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
void ReadDataIntoImpl(const unsigned char *d, unsigned DataLen, data_type_builder &Val)
DeclarationNameKey ReadKeyBase(const unsigned char *&d)
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
internal_key_type ReadKey(const unsigned char *d, unsigned)
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)
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)
static const internal_key_type & GetInternalKey(const external_key_type &x)
static hash_value_type ComputeHash(const internal_key_type &a)
static const external_key_type & GetExternalKey(const internal_key_type &x)
static internal_key_type ReadKey(const unsigned char *d, unsigned n)
ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F, IdentifierInfo *II=nullptr)
IdentifierID ReadIdentifierID(const unsigned char *d)
data_type ReadData(const internal_key_type &k, const unsigned char *d, unsigned DataLen)
internal_key_type ReadKey(const unsigned char *d, unsigned)
ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
data_type ReadData(Selector, const unsigned char *d, unsigned DataLen)
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
static hash_value_type ComputeHash(Selector Sel)
static const internal_key_type & GetInternalKey(const external_key_type &x)
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M)
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
internal_key_type GetInternalKey(external_key_type ekey)
bool EqualKey(internal_key_ref a, internal_key_ref b)
static hash_value_type ComputeHash(internal_key_ref ikey)
data_type ReadData(internal_key_ref, const unsigned char *d, unsigned DataLen)
static internal_key_type ReadKey(const unsigned char *d, unsigned)
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)
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
static void MergeDataInto(const data_type &From, data_type_builder &To)
SmallVector< LazySpecializationInfo, 4 > data_type
The lookup result is a list of global declaration IDs.
static hash_value_type ComputeHash(const internal_key_type &Key)
internal_key_type ReadKey(const unsigned char *d, unsigned)
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)
std::pair< DeclarationNameKey, unsigned > internal_key_type
internal_key_type ReadKey(const unsigned char *d, unsigned)
static hash_value_type ComputeHash(const internal_key_type &Key)
static internal_key_type GetInternalKey(const external_key_type &Key)
llvm::OnDiskChainedHashTable< ASTSelectorLookupTrait > ASTSelectorLookupTable
The on-disk hash table used for the global method pool.
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.
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
uint64_t IdentifierID
An ID number that refers to an identifier in an AST file.
Definition ASTBitCodes.h:63
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
The preprocessor keeps track of this information for each file that is #included.
MultiOnDiskHashTable< ASTDeclContextNameLookupTrait > Table
MultiOnDiskHashTable< LazySpecializationInfoLookupTrait > Table
MultiOnDiskHashTable< ModuleLocalNameLookupTrait > Table