clang  19.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 
16 #include "MultiOnDiskHashTable.h"
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 
27 namespace clang {
28 
29 class ASTReader;
30 class FileEntry;
31 struct HeaderFileInfo;
32 class HeaderSearch;
33 class ObjCMethodDecl;
34 
35 namespace serialization {
36 
37 class ModuleFile;
38 
39 namespace reader {
40 
41 /// Class that performs name lookup into a DeclContext stored
42 /// in an AST file.
44  ASTReader &Reader;
45  ModuleFile &F;
46 
47 public:
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 
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  };
81  using file_type = ModuleFile *;
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 
122 /// Base class for the trait describing the on-disk hash table for the
123 /// identifiers in an AST file.
124 ///
125 /// This class is not useful by itself; rather, it provides common
126 /// functionality for accessing the on-disk hash table of identifiers
127 /// in an AST file. Different subclasses customize that functionality
128 /// based on what information they are interested in. Those subclasses
129 /// must provide the \c data_type type and the ReadData operation, only.
131 public:
132  using external_key_type = StringRef;
133  using internal_key_type = StringRef;
136 
137  static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
138  return a == b;
139  }
140 
142 
143  static std::pair<unsigned, unsigned>
144  ReadKeyDataLength(const unsigned char*& d);
145 
146  // This hopefully will just get inlined and removed by the optimizer.
147  static const internal_key_type&
148  GetInternalKey(const external_key_type& x) { return x; }
149 
150  // This hopefully will just get inlined and removed by the optimizer.
151  static const external_key_type&
152  GetExternalKey(const internal_key_type& x) { return x; }
153 
154  static internal_key_type ReadKey(const unsigned char* d, unsigned n);
155 };
156 
157 /// Class that performs lookup for an identifier stored in an AST file.
159  ASTReader &Reader;
160  ModuleFile &F;
161 
162  // If we know the IdentifierInfo in advance, it is here and we will
163  // not build a new one. Used when deserializing information about an
164  // identifier that was constructed before the AST file was read.
165  IdentifierInfo *KnownII;
166 
167 public:
169 
171  IdentifierInfo *II = nullptr)
172  : Reader(Reader), F(F), KnownII(II) {}
173 
175  const unsigned char* d,
176  unsigned DataLen);
177 
178  IdentifierID ReadIdentifierID(const unsigned char *d);
179 
180  ASTReader &getReader() const { return Reader; }
181 };
182 
183 /// The on-disk hash table used to contain information about
184 /// all of the identifiers in the program.
186  llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait>;
187 
188 /// Class that performs lookup for a selector's entries in the global
189 /// method pool stored in an AST file.
191  ASTReader &Reader;
192  ModuleFile &F;
193 
194 public:
195  struct data_type {
197  unsigned InstanceBits;
198  unsigned FactoryBits;
203  };
204 
209 
211  : Reader(Reader), F(F) {}
212 
213  static bool EqualKey(const internal_key_type& a,
214  const internal_key_type& b) {
215  return a == b;
216  }
217 
219 
220  static const internal_key_type&
221  GetInternalKey(const external_key_type& x) { return x; }
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  data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
228 };
229 
230 /// The on-disk hash table used for the global method pool.
232  llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait>;
233 
234 /// Trait class used to search the on-disk hash table containing all of
235 /// the header search information.
236 ///
237 /// The on-disk hash table contains a mapping from each header path to
238 /// information about that header (how many times it has been included, its
239 /// controlling macro, etc.). Note that we actually hash based on the size
240 /// and mtime, and support "deep" comparisons of file names based on current
241 /// inode numbers, so that the search can cope with non-normalized path names
242 /// and symlinks.
244  ASTReader &Reader;
245  ModuleFile &M;
246  HeaderSearch *HS;
247  const char *FrameworkStrings;
248 
249 public:
251 
253  off_t Size;
254  time_t ModTime;
255  StringRef Filename;
256  bool Imported;
257  };
258 
260 
264 
266  const char *FrameworkStrings)
267  : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) {}
268 
270  internal_key_type GetInternalKey(external_key_type ekey);
272 
273  static std::pair<unsigned, unsigned>
274  ReadKeyDataLength(const unsigned char*& d);
275 
276  static internal_key_type ReadKey(const unsigned char *d, unsigned);
277 
278  data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
279 
280 private:
281  const FileEntry *getFile(const internal_key_type &Key);
282 };
283 
284 /// The on-disk hash table used for known header files.
286  llvm::OnDiskChainedHashTable<HeaderFileInfoTrait>;
287 
288 } // namespace reader
289 
290 } // namespace serialization
291 
292 } // namespace clang
293 
294 #endif // LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
static char ID
Definition: Arena.cpp:183
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:366
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:300
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
Definition: HeaderSearch.h:253
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:2025
unsigned getHash() const
Compute a fingerprint of this key for use in on-disk hash table.
Definition: ASTReader.cpp:1136
Information about a module that has been loaded by the ASTReader.
Definition: ModuleFile.h:124
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:1179
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:1174
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:1213
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:980
static hash_value_type ComputeHash(const internal_key_type &a)
Definition: ASTReader.cpp:975
static const internal_key_type & GetInternalKey(const external_key_type &x)
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:985
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:1009
data_type ReadData(const internal_key_type &k, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:1025
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:915
ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
data_type ReadData(Selector, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:937
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:910
static const internal_key_type & GetInternalKey(const external_key_type &x)
static hash_value_type ComputeHash(Selector Sel)
Definition: ASTReader.cpp:905
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.
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:2017
internal_key_type GetInternalKey(external_key_type ekey)
Definition: ASTReader.cpp:1996
bool EqualKey(internal_key_ref a, internal_key_ref b)
Definition: ASTReader.cpp:2003
static hash_value_type ComputeHash(internal_key_ref ikey)
Definition: ASTReader.cpp:1991
data_type ReadData(internal_key_ref, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:2035
HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS, const char *FrameworkStrings)
static internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:2022
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:143
uint32_t IdentifierID
An ID number that refers to an identifier in an AST file.
Definition: ASTBitCodes.h:62
@ 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:58
MultiOnDiskHashTable< ASTDeclContextNameLookupTrait > Table