clang  6.0.0svn
ASTReaderInternals.h
Go to the documentation of this file.
1 //===- ASTReaderInternals.h - AST Reader Internals --------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides internal definitions used in the AST reader.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
15 #define LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
16 
17 #include "MultiOnDiskHashTable.h"
19 #include "clang/Basic/LLVM.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/OnDiskHashTable.h"
25 #include <ctime>
26 #include <utility>
27 
28 namespace clang {
29 
30 class ASTReader;
31 class FileEntry;
32 struct HeaderFileInfo;
33 class HeaderSearch;
34 class IdentifierTable;
35 class ObjCMethodDecl;
36 
37 namespace serialization {
38 
39 class ModuleFile;
40 
41 namespace reader {
42 
43 /// \brief Class that performs name lookup into a DeclContext stored
44 /// in an AST file.
46  ASTReader &Reader;
47  ModuleFile &F;
48 
49 public:
50  // Maximum number of lookup tables we allow before condensing the tables.
51  static const int MaxTables = 4;
52 
53  /// The lookup result is a list of global declaration IDs.
55 
59 
60  data_type_builder(data_type &D) : Data(D) {}
61 
62  void insert(DeclID ID) {
63  // Just use a linear scan unless we have more than a few IDs.
64  if (Found.empty() && !Data.empty()) {
65  if (Data.size() <= 4) {
66  for (auto I : Found)
67  if (I == ID)
68  return;
69  Data.push_back(ID);
70  return;
71  }
72 
73  // Switch to tracking found IDs in the set.
74  Found.insert(Data.begin(), Data.end());
75  }
76 
77  if (Found.insert(ID).second)
78  Data.push_back(ID);
79  }
80  };
81  using hash_value_type = unsigned;
82  using offset_type = unsigned;
83  using file_type = ModuleFile *;
84 
87 
89  : Reader(Reader), F(F) {}
90 
91  static bool EqualKey(const internal_key_type &a, const internal_key_type &b) {
92  return a == b;
93  }
94 
96  return Key.getHash();
97  }
98 
100  return Name;
101  }
102 
103  static std::pair<unsigned, unsigned>
104  ReadKeyDataLength(const unsigned char *&d);
105 
106  internal_key_type ReadKey(const unsigned char *d, unsigned);
107 
108  void ReadDataInto(internal_key_type, const unsigned char *d,
109  unsigned DataLen, data_type_builder &Val);
110 
111  static void MergeDataInto(const data_type &From, data_type_builder &To) {
112  To.Data.reserve(To.Data.size() + From.size());
113  for (DeclID ID : From)
114  To.insert(ID);
115  }
116 
117  file_type ReadFileRef(const unsigned char *&d);
118 };
119 
122 };
123 
124 /// \brief Base class for the trait describing the on-disk hash table for the
125 /// identifiers in an AST file.
126 ///
127 /// This class is not useful by itself; rather, it provides common
128 /// functionality for accessing the on-disk hash table of identifiers
129 /// in an AST file. Different subclasses customize that functionality
130 /// based on what information they are interested in. Those subclasses
131 /// must provide the \c data_type type and the ReadData operation, only.
133 public:
134  using external_key_type = StringRef;
135  using internal_key_type = StringRef;
136  using hash_value_type = unsigned;
137  using offset_type = unsigned;
138 
139  static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
140  return a == b;
141  }
142 
144 
145  static std::pair<unsigned, unsigned>
146  ReadKeyDataLength(const unsigned char*& d);
147 
148  // This hopefully will just get inlined and removed by the optimizer.
149  static const internal_key_type&
150  GetInternalKey(const external_key_type& x) { return x; }
151 
152  // This hopefully will just get inlined and removed by the optimizer.
153  static const external_key_type&
154  GetExternalKey(const internal_key_type& x) { return x; }
155 
156  static internal_key_type ReadKey(const unsigned char* d, unsigned n);
157 };
158 
159 /// \brief Class that performs lookup for an identifier stored in an AST file.
161  ASTReader &Reader;
162  ModuleFile &F;
163 
164  // If we know the IdentifierInfo in advance, it is here and we will
165  // not build a new one. Used when deserializing information about an
166  // identifier that was constructed before the AST file was read.
167  IdentifierInfo *KnownII;
168 
169 public:
171 
173  IdentifierInfo *II = nullptr)
174  : Reader(Reader), F(F), KnownII(II) {}
175 
176  data_type ReadData(const internal_key_type& k,
177  const unsigned char* d,
178  unsigned DataLen);
179 
180  IdentID ReadIdentifierID(const unsigned char *d);
181 
182  ASTReader &getReader() const { return Reader; }
183 };
184 
185 /// \brief The on-disk hash table used to contain information about
186 /// all of the identifiers in the program.
189 
190 /// \brief Class that performs lookup for a selector's entries in the global
191 /// method pool stored in an AST file.
193  ASTReader &Reader;
194  ModuleFile &F;
195 
196 public:
197  struct data_type {
199  unsigned InstanceBits;
200  unsigned FactoryBits;
205  };
206 
209  using hash_value_type = unsigned;
210  using offset_type = unsigned;
211 
213  : Reader(Reader), F(F) {}
214 
215  static bool EqualKey(const internal_key_type& a,
216  const internal_key_type& b) {
217  return a == b;
218  }
219 
221 
222  static const internal_key_type&
223  GetInternalKey(const external_key_type& x) { return x; }
224 
225  static std::pair<unsigned, unsigned>
226  ReadKeyDataLength(const unsigned char*& d);
227 
228  internal_key_type ReadKey(const unsigned char* d, unsigned);
229  data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
230 };
231 
232 /// \brief The on-disk hash table used for the global method pool.
235 
236 /// \brief Trait class used to search the on-disk hash table containing all of
237 /// the header search information.
238 ///
239 /// The on-disk hash table contains a mapping from each header path to
240 /// information about that header (how many times it has been included, its
241 /// controlling macro, etc.). Note that we actually hash based on the size
242 /// and mtime, and support "deep" comparisons of file names based on current
243 /// inode numbers, so that the search can cope with non-normalized path names
244 /// and symlinks.
246  ASTReader &Reader;
247  ModuleFile &M;
248  HeaderSearch *HS;
249  const char *FrameworkStrings;
250 
251 public:
252  using external_key_type = const FileEntry *;
253 
255  off_t Size;
256  time_t ModTime;
257  StringRef Filename;
258  bool Imported;
259  };
260 
262 
264  using hash_value_type = unsigned;
265  using offset_type = unsigned;
266 
268  const char *FrameworkStrings)
269  : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) {}
270 
274 
275  static std::pair<unsigned, unsigned>
276  ReadKeyDataLength(const unsigned char*& d);
277 
278  static internal_key_type ReadKey(const unsigned char *d, unsigned);
279 
280  data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
281 };
282 
283 /// \brief The on-disk hash table used for known header files.
286 
287 } // namespace reader
288 
289 } // namespace serialization
290 
291 } // namespace clang
292 
293 #endif // LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H
Smart pointer class that efficiently represents Objective-C method names.
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:1077
static internal_key_type GetInternalKey(const external_key_type &Name)
Class that performs name lookup into a DeclContext stored in an AST file.
static void MergeDataInto(const data_type &From, data_type_builder &To)
static const external_key_type & GetExternalKey(const internal_key_type &x)
static const internal_key_type & GetInternalKey(const external_key_type &x)
One of these records is kept for each identifier that is lexed.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
unsigned getHash() const
Compute a fingerprint of this key for use in on-disk hash table.
Definition: ASTReader.cpp:1040
static hash_value_type ComputeHash(const internal_key_type &Key)
A collection of on-disk hash tables, merged when relevant for performance.
The preprocessor keeps track of this information for each file that is #included. ...
Definition: HeaderSearch.h:51
Encapsulates the information needed to find the file referenced by a #include or #include_next, (sub-)framework lookup, etc.
Definition: HeaderSearch.h:148
Base class for the trait describing the on-disk hash table for the identifiers in an AST file...
Information about a module that has been loaded by the ASTReader.
Definition: Module.h:100
Trait class used to search the on-disk hash table containing all of the header search information...
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
static bool EqualKey(const internal_key_type &a, const internal_key_type &b)
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
Class that performs lookup for a selector&#39;s entries in the global method pool stored in an AST file...
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
Definition: ASTBitCodes.h:154
Dataflow Directional Tag Classes.
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:358
DeclarationName - The name of a declaration.
Class that performs lookup for an identifier stored in an AST file.
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: ASTBitCodes.h:69
void ReadDataInto(internal_key_type, const unsigned char *d, unsigned DataLen, data_type_builder &Val)
Definition: ASTReader.cpp:1120
uint32_t IdentID
An ID number that refers to an identifier in an AST file.
Definition: ASTBitCodes.h:135
A key used when looking up entities by DeclarationName.
Definition: ASTBitCodes.h:1957
ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F, IdentifierInfo *II=nullptr)
MultiOnDiskHashTable< ASTDeclContextNameLookupTrait > Table
internal_key_type ReadKey(const unsigned char *d, unsigned)
Definition: ASTReader.cpp:1086
static const internal_key_type & GetInternalKey(const external_key_type &x)
HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS, const char *FrameworkStrings)