clang  15.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 
60  void insert(DeclID ID) {
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 (DeclID 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  IdentID 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:
250  using external_key_type = const FileEntry *;
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(const FileEntry *FE);
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 
281 /// The on-disk hash table used for known header files.
283  llvm::OnDiskChainedHashTable<HeaderFileInfoTrait>;
284 
285 } // namespace reader
286 
287 } // namespace serialization
288 
289 } // namespace clang
290 
291 #endif // LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
clang::serialization::reader::ASTDeclContextNameLookupTrait::data_type_builder::Data
data_type & Data
Definition: ASTReaderInternals.h:55
clang::serialization::reader::ASTSelectorLookupTrait::EqualKey
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
Definition: ASTReaderInternals.h:213
clang::serialization::reader::ASTIdentifierLookupTrait::ASTIdentifierLookupTrait
ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F, IdentifierInfo *II=nullptr)
Definition: ASTReaderInternals.h:170
clang::serialization::reader::ASTSelectorLookupTrait::data_type::ID
SelectorID ID
Definition: ASTReaderInternals.h:196
clang::serialization::reader::ASTSelectorLookupTrait::data_type::InstanceHasMoreThanOneDecl
bool InstanceHasMoreThanOneDecl
Definition: ASTReaderInternals.h:199
clang::serialization::reader::ASTIdentifierLookupTrait::getReader
ASTReader & getReader() const
Definition: ASTReaderInternals.h:180
clang::serialization::reader::ASTIdentifierLookupTrait
Class that performs lookup for an identifier stored in an AST file.
Definition: ASTReaderInternals.h:158
llvm::SmallVector
Definition: LLVM.h:38
clang::serialization::IdentID
uint32_t IdentID
An ID number that refers to an identifier in an AST file.
Definition: ASTBitCodes.h:134
clang::serialization::reader::ASTIdentifierLookupTraitBase::ComputeHash
static hash_value_type ComputeHash(const internal_key_type &a)
Definition: ASTReader.cpp:915
clang::serialization::reader::ASTDeclContextNameLookupTrait::MaxTables
static const int MaxTables
Definition: ASTReaderInternals.h:49
clang::serialization::reader::ASTSelectorLookupTrait::internal_key_type
external_key_type internal_key_type
Definition: ASTReaderInternals.h:206
clang::serialization::DeclarationNameKey
A key used when looking up entities by DeclarationName.
Definition: ASTBitCodes.h:2089
clang::serialization::reader::HeaderFileInfoTrait::hash_value_type
unsigned hash_value_type
Definition: ASTReaderInternals.h:262
clang::serialization::reader::HeaderFileInfoLookupTable
llvm::OnDiskChainedHashTable< HeaderFileInfoTrait > HeaderFileInfoLookupTable
The on-disk hash table used for known header files.
Definition: ASTReaderInternals.h:283
clang::serialization::reader::HeaderFileInfoTrait::internal_key_type
Definition: ASTReaderInternals.h:252
clang::serialization::reader::ASTDeclContextNameLookupTrait::GetInternalKey
static internal_key_type GetInternalKey(const external_key_type &Name)
Definition: ASTReaderInternals.h:97
clang::serialization::reader::ASTDeclContextNameLookupTrait::ComputeHash
static hash_value_type ComputeHash(const internal_key_type &Key)
Definition: ASTReaderInternals.h:93
clang::serialization::reader::ASTSelectorLookupTrait::data_type::InstanceBits
unsigned InstanceBits
Definition: ASTReaderInternals.h:197
clang::DeclarationName
The name of a declaration.
Definition: DeclarationName.h:144
b
__device__ __2f16 b
Definition: __clang_hip_libdevice_declares.h:314
clang::serialization::reader::ASTIdentifierLookupTable
llvm::OnDiskIterableChainedHashTable< ASTIdentifierLookupTrait > ASTIdentifierLookupTable
The on-disk hash table used to contain information about all of the identifiers in the program.
Definition: ASTReaderInternals.h:186
clang::serialization::reader::ASTSelectorLookupTrait::data_type::Factory
SmallVector< ObjCMethodDecl *, 2 > Factory
Definition: ASTReaderInternals.h:202
clang::serialization::reader::ASTDeclContextNameLookupTrait::ReadKeyDataLength
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:1108
clang::serialization::reader::ASTSelectorLookupTrait::ReadKeyDataLength
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:854
clang::HeaderSearch
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
Definition: HeaderSearch.h:223
clang::serialization::reader::ASTDeclContextNameLookupTrait::ReadDataInto
void ReadDataInto(internal_key_type, const unsigned char *d, unsigned DataLen, data_type_builder &Val)
Definition: ASTReader.cpp:1147
clang::serialization::reader::HeaderFileInfoTrait::data_type
HeaderFileInfo data_type
Definition: ASTReaderInternals.h:261
clang::serialization::reader::HeaderFileInfoTrait::ReadKey
static internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:1874
clang::serialization::reader::ASTIdentifierLookupTraitBase::external_key_type
StringRef external_key_type
Definition: ASTReaderInternals.h:132
clang::serialization::reader::ASTIdentifierLookupTraitBase::ReadKey
static internal_key_type ReadKey(const unsigned char *d, unsigned n)
Definition: ASTReader.cpp:925
clang::serialization::reader::ASTDeclContextNameLookupTrait::file_type
ModuleFile * file_type
Definition: ASTReaderInternals.h:81
clang::serialization::reader::ASTSelectorLookupTrait::data_type
Definition: ASTReaderInternals.h:195
clang::serialization::reader::ASTIdentifierLookupTrait::data_type
IdentifierInfo * data_type
Definition: ASTReaderInternals.h:168
clang::serialization::reader::ASTDeclContextNameLookupTrait::MergeDataInto
static void MergeDataInto(const data_type &From, data_type_builder &To)
Definition: ASTReaderInternals.h:109
clang::serialization::reader::HeaderFileInfoTrait::internal_key_type::Imported
bool Imported
Definition: ASTReaderInternals.h:256
clang::serialization::reader::ASTSelectorLookupTrait::ASTSelectorLookupTrait
ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Definition: ASTReaderInternals.h:210
clang::serialization::reader::HeaderFileInfoTrait::HeaderFileInfoTrait
HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS, const char *FrameworkStrings)
Definition: ASTReaderInternals.h:265
clang::serialization::reader::ASTIdentifierLookupTraitBase
Base class for the trait describing the on-disk hash table for the identifiers in an AST file.
Definition: ASTReaderInternals.h:130
clang::serialization::reader::HeaderFileInfoTrait::internal_key_type::ModTime
time_t ModTime
Definition: ASTReaderInternals.h:254
clang::serialization::reader::ASTSelectorLookupTrait::data_type::FactoryBits
unsigned FactoryBits
Definition: ASTReaderInternals.h:198
clang::serialization::reader::ASTIdentifierLookupTraitBase::GetExternalKey
static const external_key_type & GetExternalKey(const internal_key_type &x)
Definition: ASTReaderInternals.h:152
clang::serialization::reader::ASTIdentifierLookupTraitBase::GetInternalKey
static const internal_key_type & GetInternalKey(const external_key_type &x)
Definition: ASTReaderInternals.h:148
llvm::DenseSet< DeclID >
clang::serialization::reader::ASTSelectorLookupTrait::GetInternalKey
static const internal_key_type & GetInternalKey(const external_key_type &x)
Definition: ASTReaderInternals.h:221
clang::serialization::reader::ASTDeclContextNameLookupTrait::data_type_builder::data_type_builder
data_type_builder(data_type &D)
Definition: ASTReaderInternals.h:58
clang::serialization::reader::ASTSelectorLookupTrait::ComputeHash
static hash_value_type ComputeHash(Selector Sel)
Definition: ASTReader.cpp:849
clang::serialization::reader::ASTDeclContextNameLookupTrait::data_type_builder::Found
llvm::DenseSet< DeclID > Found
Definition: ASTReaderInternals.h:56
clang::serialization::reader::ASTIdentifierLookupTrait::ReadData
data_type ReadData(const internal_key_type &k, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:962
x
IRgen optimization opportunities The common pattern of short x
Definition: README.txt:7
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:330
clang::serialization::reader::ASTIdentifierLookupTraitBase::hash_value_type
unsigned hash_value_type
Definition: ASTReaderInternals.h:134
clang::serialization::MultiOnDiskHashTable
A collection of on-disk hash tables, merged when relevant for performance.
Definition: MultiOnDiskHashTable.h:40
clang::serialization::reader::HeaderFileInfoTrait::ComputeHash
static hash_value_type ComputeHash(internal_key_ref ikey)
Definition: ASTReader.cpp:1828
ASTBitCodes.h
clang::serialization::reader::ASTDeclContextNameLookupTrait::ReadFileRef
file_type ReadFileRef(const unsigned char *&d)
Definition: ASTReader.cpp:1100
clang::serialization::reader::HeaderFileInfoTrait::internal_key_type::Filename
StringRef Filename
Definition: ASTReaderInternals.h:255
clang::serialization::reader::ASTSelectorLookupTrait
Class that performs lookup for a selector's entries in the global method pool stored in an AST file.
Definition: ASTReaderInternals.h:190
MultiOnDiskHashTable.h
LLVM.h
clang::serialization::reader::HeaderFileInfoTrait::internal_key_ref
const internal_key_type & internal_key_ref
Definition: ASTReaderInternals.h:259
clang::serialization::reader::HeaderFileInfoTrait::ReadKeyDataLength
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:1869
clang::serialization::reader::ASTSelectorLookupTrait::hash_value_type
unsigned hash_value_type
Definition: ASTReaderInternals.h:207
clang::HeaderFileInfo
The preprocessor keeps track of this information for each file that is #included.
Definition: HeaderSearch.h:58
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:84
clang::serialization::reader::ASTDeclContextNameLookupTrait
Class that performs name lookup into a DeclContext stored in an AST file.
Definition: ASTReaderInternals.h:43
clang::serialization::reader::ASTSelectorLookupTrait::external_key_type
Selector external_key_type
Definition: ASTReaderInternals.h:205
clang::serialization::reader::ASTSelectorLookupTrait::ReadData
data_type ReadData(Selector, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:881
clang::ASTReader
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:340
clang::serialization::reader::HeaderFileInfoTrait::ReadData
data_type ReadData(internal_key_ref, const unsigned char *d, unsigned DataLen)
Definition: ASTReader.cpp:1886
clang::serialization::reader::HeaderFileInfoTrait
Trait class used to search the on-disk hash table containing all of the header search information.
Definition: ASTReaderInternals.h:243
DeclarationName.h
clang::serialization::reader::ASTSelectorLookupTable
llvm::OnDiskChainedHashTable< ASTSelectorLookupTrait > ASTSelectorLookupTable
The on-disk hash table used for the global method pool.
Definition: ASTReaderInternals.h:232
clang::serialization::reader::ASTDeclContextNameLookupTrait::ReadKey
internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:1113
clang::Builtin::ID
ID
Definition: Builtins.h:49
clang::serialization::reader::DeclContextLookupTable
Definition: ASTReaderInternals.h:118
clang
Definition: CalledOnceCheck.h:17
clang::Selector
Smart pointer class that efficiently represents Objective-C method names.
Definition: IdentifierTable.h:752
clang::serialization::reader::HeaderFileInfoTrait::EqualKey
bool EqualKey(internal_key_ref a, internal_key_ref b)
Definition: ASTReader.cpp:1840
clang::serialization::reader::ASTSelectorLookupTrait::ReadKey
internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:859
unsigned
clang::serialization::reader::ASTDeclContextNameLookupTrait::EqualKey
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
Definition: ASTReaderInternals.h:89
clang::serialization::reader::HeaderFileInfoTrait::internal_key_type::Size
off_t Size
Definition: ASTReaderInternals.h:253
clang::serialization::reader::ASTDeclContextNameLookupTrait::ASTDeclContextNameLookupTrait
ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
Definition: ASTReaderInternals.h:86
clang::serialization::reader::DeclContextLookupTable::Table
MultiOnDiskHashTable< ASTDeclContextNameLookupTrait > Table
Definition: ASTReaderInternals.h:119
clang::serialization::reader::HeaderFileInfoTrait::GetInternalKey
internal_key_type GetInternalKey(const FileEntry *FE)
Definition: ASTReader.cpp:1833
clang::serialization::reader::ASTIdentifierLookupTraitBase::internal_key_type
StringRef internal_key_type
Definition: ASTReaderInternals.h:133
clang::serialization::DeclID
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: ASTBitCodes.h:68
clang::serialization::reader::ASTIdentifierLookupTrait::ReadIdentifierID
IdentID ReadIdentifierID(const unsigned char *d)
Definition: ASTReader.cpp:946
clang::serialization::reader::ASTSelectorLookupTrait::data_type::FactoryHasMoreThanOneDecl
bool FactoryHasMoreThanOneDecl
Definition: ASTReaderInternals.h:200
clang::serialization::reader::ASTSelectorLookupTrait::data_type::Instance
SmallVector< ObjCMethodDecl *, 2 > Instance
Definition: ASTReaderInternals.h:201
clang::serialization::SelectorID
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
Definition: ASTBitCodes.h:153
clang::serialization::reader::ASTDeclContextNameLookupTrait::internal_key_type
DeclarationNameKey internal_key_type
Definition: ASTReaderInternals.h:84
clang::serialization::reader::ASTDeclContextNameLookupTrait::data_type_builder::insert
void insert(DeclID ID)
Definition: ASTReaderInternals.h:60
clang::serialization::reader::ASTDeclContextNameLookupTrait::data_type_builder
Definition: ASTReaderInternals.h:54
clang::serialization::ModuleFile
Information about a module that has been loaded by the ASTReader.
Definition: ModuleFile.h:111
clang::serialization::reader::ASTIdentifierLookupTraitBase::ReadKeyDataLength
static std::pair< unsigned, unsigned > ReadKeyDataLength(const unsigned char *&d)
Definition: ASTReader.cpp:920
clang::serialization::DeclarationNameKey::getHash
unsigned getHash() const
Compute a fingerprint of this key for use in on-disk hash table.
Definition: ASTReader.cpp:1071
clang::serialization::reader::ASTIdentifierLookupTraitBase::EqualKey
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
Definition: ASTReaderInternals.h:137