Coverage Report

Created: 2020-11-24 06:42

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/AST/ExternalASTSource.h
Line
Count
Source (jump to first uncovered line)
1
//===- ExternalASTSource.h - Abstract External AST Interface ----*- 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 defines the ExternalASTSource interface, which enables
10
//  construction of AST nodes from some external source.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_AST_EXTERNALASTSOURCE_H
15
#define LLVM_CLANG_AST_EXTERNALASTSOURCE_H
16
17
#include "clang/AST/CharUnits.h"
18
#include "clang/AST/DeclBase.h"
19
#include "clang/Basic/LLVM.h"
20
#include "llvm/ADT/ArrayRef.h"
21
#include "llvm/ADT/DenseMap.h"
22
#include "llvm/ADT/IntrusiveRefCntPtr.h"
23
#include "llvm/ADT/Optional.h"
24
#include "llvm/ADT/PointerUnion.h"
25
#include "llvm/ADT/STLExtras.h"
26
#include "llvm/ADT/SmallVector.h"
27
#include "llvm/ADT/StringRef.h"
28
#include "llvm/ADT/iterator.h"
29
#include "llvm/Support/PointerLikeTypeTraits.h"
30
#include <cassert>
31
#include <cstddef>
32
#include <cstdint>
33
#include <iterator>
34
#include <string>
35
#include <utility>
36
37
namespace clang {
38
39
class ASTConsumer;
40
class ASTContext;
41
class ASTSourceDescriptor;
42
class CXXBaseSpecifier;
43
class CXXCtorInitializer;
44
class CXXRecordDecl;
45
class DeclarationName;
46
class FieldDecl;
47
class IdentifierInfo;
48
class NamedDecl;
49
class ObjCInterfaceDecl;
50
class RecordDecl;
51
class Selector;
52
class Stmt;
53
class TagDecl;
54
55
/// Abstract interface for external sources of AST nodes.
56
///
57
/// External AST sources provide AST nodes constructed from some
58
/// external source, such as a precompiled header. External AST
59
/// sources can resolve types and declarations from abstract IDs into
60
/// actual type and declaration nodes, and read parts of declaration
61
/// contexts.
62
class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
63
  friend class ExternalSemaSource;
64
65
  /// Generation number for this external AST source. Must be increased
66
  /// whenever we might have added new redeclarations for existing decls.
67
  uint32_t CurrentGeneration = 0;
68
69
  /// LLVM-style RTTI.
70
  static char ID;
71
72
public:
73
51.4k
  ExternalASTSource() = default;
74
  virtual ~ExternalASTSource();
75
76
  /// RAII class for safely pairing a StartedDeserializing call
77
  /// with FinishedDeserializing.
78
  class Deserializing {
79
    ExternalASTSource *Source;
80
81
  public:
82
29.1M
    explicit Deserializing(ExternalASTSource *source) : Source(source) {
83
29.1M
      assert(Source);
84
29.1M
      Source->StartedDeserializing();
85
29.1M
    }
86
87
29.1M
    ~Deserializing() {
88
29.1M
      Source->FinishedDeserializing();
89
29.1M
    }
90
  };
91
92
  /// Get the current generation of this AST source. This number
93
  /// is incremented each time the AST source lazily extends an existing
94
  /// entity.
95
113M
  uint32_t getGeneration() const { return CurrentGeneration; }
96
97
  /// Resolve a declaration ID into a declaration, potentially
98
  /// building a new declaration.
99
  ///
100
  /// This method only needs to be implemented if the AST source ever
101
  /// passes back decl sets as VisibleDeclaration objects.
102
  ///
103
  /// The default implementation of this method is a no-op.
104
  virtual Decl *GetExternalDecl(uint32_t ID);
105
106
  /// Resolve a selector ID into a selector.
107
  ///
108
  /// This operation only needs to be implemented if the AST source
109
  /// returns non-zero for GetNumKnownSelectors().
110
  ///
111
  /// The default implementation of this method is a no-op.
112
  virtual Selector GetExternalSelector(uint32_t ID);
113
114
  /// Returns the number of selectors known to the external AST
115
  /// source.
116
  ///
117
  /// The default implementation of this method is a no-op.
118
  virtual uint32_t GetNumExternalSelectors();
119
120
  /// Resolve the offset of a statement in the decl stream into
121
  /// a statement.
122
  ///
123
  /// This operation is meant to be used via a LazyOffsetPtr.  It only
124
  /// needs to be implemented if the AST source uses methods like
125
  /// FunctionDecl::setLazyBody when building decls.
126
  ///
127
  /// The default implementation of this method is a no-op.
128
  virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
129
130
  /// Resolve the offset of a set of C++ constructor initializers in
131
  /// the decl stream into an array of initializers.
132
  ///
133
  /// The default implementation of this method is a no-op.
134
  virtual CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset);
135
136
  /// Resolve the offset of a set of C++ base specifiers in the decl
137
  /// stream into an array of specifiers.
138
  ///
139
  /// The default implementation of this method is a no-op.
140
  virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
141
142
  /// Update an out-of-date identifier.
143
0
  virtual void updateOutOfDateIdentifier(IdentifierInfo &II) {}
144
145
  /// Find all declarations with the given name in the given context,
146
  /// and add them to the context by calling SetExternalVisibleDeclsForName
147
  /// or SetNoExternalVisibleDeclsForName.
148
  /// \return \c true if any declarations might have been found, \c false if
149
  /// we definitely have no declarations with tbis name.
150
  ///
151
  /// The default implementation of this method is a no-op returning \c false.
152
  virtual bool
153
  FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
154
155
  /// Ensures that the table of all visible declarations inside this
156
  /// context is up to date.
157
  ///
158
  /// The default implementation of this function is a no-op.
159
  virtual void completeVisibleDeclsMap(const DeclContext *DC);
160
161
  /// Retrieve the module that corresponds to the given module ID.
162
0
  virtual Module *getModule(unsigned ID) { return nullptr; }
163
164
  /// Return a descriptor for the corresponding module, if one exists.
165
  virtual llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID);
166
167
  enum ExtKind { EK_Always, EK_Never, EK_ReplyHazy };
168
169
  virtual ExtKind hasExternalDefinitions(const Decl *D);
170
171
  /// Finds all declarations lexically contained within the given
172
  /// DeclContext, after applying an optional filter predicate.
173
  ///
174
  /// \param IsKindWeWant a predicate function that returns true if the passed
175
  /// declaration kind is one we are looking for.
176
  ///
177
  /// The default implementation of this method is a no-op.
178
  virtual void
179
  FindExternalLexicalDecls(const DeclContext *DC,
180
                           llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
181
                           SmallVectorImpl<Decl *> &Result);
182
183
  /// Finds all declarations lexically contained within the given
184
  /// DeclContext.
185
  void FindExternalLexicalDecls(const DeclContext *DC,
186
173k
                                SmallVectorImpl<Decl *> &Result) {
187
1.63M
    FindExternalLexicalDecls(DC, [](Decl::Kind) { return true; }, Result);
188
173k
  }
189
190
  /// Get the decls that are contained in a file in the Offset/Length
191
  /// range. \p Length can be 0 to indicate a point at \p Offset instead of
192
  /// a range.
193
  virtual void FindFileRegionDecls(FileID File, unsigned Offset,
194
                                   unsigned Length,
195
                                   SmallVectorImpl<Decl *> &Decls);
196
197
  /// Gives the external AST source an opportunity to complete
198
  /// the redeclaration chain for a declaration. Called each time we
199
  /// need the most recent declaration of a declaration after the
200
  /// generation count is incremented.
201
  virtual void CompleteRedeclChain(const Decl *D);
202
203
  /// Gives the external AST source an opportunity to complete
204
  /// an incomplete type.
205
  virtual void CompleteType(TagDecl *Tag);
206
207
  /// Gives the external AST source an opportunity to complete an
208
  /// incomplete Objective-C class.
209
  ///
210
  /// This routine will only be invoked if the "externally completed" bit is
211
  /// set on the ObjCInterfaceDecl via the function
212
  /// \c ObjCInterfaceDecl::setExternallyCompleted().
213
  virtual void CompleteType(ObjCInterfaceDecl *Class);
214
215
  /// Loads comment ranges.
216
  virtual void ReadComments();
217
218
  /// Notify ExternalASTSource that we started deserialization of
219
  /// a decl or type so until FinishedDeserializing is called there may be
220
  /// decls that are initializing. Must be paired with FinishedDeserializing.
221
  ///
222
  /// The default implementation of this method is a no-op.
223
  virtual void StartedDeserializing();
224
225
  /// Notify ExternalASTSource that we finished the deserialization of
226
  /// a decl or type. Must be paired with StartedDeserializing.
227
  ///
228
  /// The default implementation of this method is a no-op.
229
  virtual void FinishedDeserializing();
230
231
  /// Function that will be invoked when we begin parsing a new
232
  /// translation unit involving this external AST source.
233
  ///
234
  /// The default implementation of this method is a no-op.
235
  virtual void StartTranslationUnit(ASTConsumer *Consumer);
236
237
  /// Print any statistics that have been gathered regarding
238
  /// the external AST source.
239
  ///
240
  /// The default implementation of this method is a no-op.
241
  virtual void PrintStats();
242
243
  /// Perform layout on the given record.
244
  ///
245
  /// This routine allows the external AST source to provide an specific
246
  /// layout for a record, overriding the layout that would normally be
247
  /// constructed. It is intended for clients who receive specific layout
248
  /// details rather than source code (such as LLDB). The client is expected
249
  /// to fill in the field offsets, base offsets, virtual base offsets, and
250
  /// complete object size.
251
  ///
252
  /// \param Record The record whose layout is being requested.
253
  ///
254
  /// \param Size The final size of the record, in bits.
255
  ///
256
  /// \param Alignment The final alignment of the record, in bits.
257
  ///
258
  /// \param FieldOffsets The offset of each of the fields within the record,
259
  /// expressed in bits. All of the fields must be provided with offsets.
260
  ///
261
  /// \param BaseOffsets The offset of each of the direct, non-virtual base
262
  /// classes. If any bases are not given offsets, the bases will be laid
263
  /// out according to the ABI.
264
  ///
265
  /// \param VirtualBaseOffsets The offset of each of the virtual base classes
266
  /// (either direct or not). If any bases are not given offsets, the bases will be laid
267
  /// out according to the ABI.
268
  ///
269
  /// \returns true if the record layout was provided, false otherwise.
270
  virtual bool layoutRecordType(
271
      const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
272
      llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
273
      llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
274
      llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets);
275
276
  //===--------------------------------------------------------------------===//
277
  // Queries for performance analysis.
278
  //===--------------------------------------------------------------------===//
279
280
  struct MemoryBufferSizes {
281
    size_t malloc_bytes;
282
    size_t mmap_bytes;
283
284
    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
285
0
        : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
286
  };
287
288
  /// Return the amount of memory used by memory buffers, breaking down
289
  /// by heap-backed versus mmap'ed memory.
290
0
  MemoryBufferSizes getMemoryBufferSizes() const {
291
0
    MemoryBufferSizes sizes(0, 0);
292
0
    getMemoryBufferSizes(sizes);
293
0
    return sizes;
294
0
  }
295
296
  virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
297
298
  /// LLVM-style RTTI.
299
  /// \{
300
4.96M
  virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
301
0
  static bool classof(const ExternalASTSource *S) { return S->isA(&ID); }
302
  /// \}
303
304
protected:
305
  static DeclContextLookupResult
306
  SetExternalVisibleDeclsForName(const DeclContext *DC,
307
                                 DeclarationName Name,
308
                                 ArrayRef<NamedDecl*> Decls);
309
310
  static DeclContextLookupResult
311
  SetNoExternalVisibleDeclsForName(const DeclContext *DC,
312
                                   DeclarationName Name);
313
314
  /// Increment the current generation.
315
  uint32_t incrementGeneration(ASTContext &C);
316
};
317
318
/// A lazy pointer to an AST node (of base type T) that resides
319
/// within an external AST source.
320
///
321
/// The AST node is identified within the external AST source by a
322
/// 63-bit offset, and can be retrieved via an operation on the
323
/// external AST source itself.
324
template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
325
struct LazyOffsetPtr {
326
  /// Either a pointer to an AST node or the offset within the
327
  /// external AST source where the AST node can be found.
328
  ///
329
  /// If the low bit is clear, a pointer to the AST node. If the low
330
  /// bit is set, the upper 63 bits are the offset.
331
  mutable uint64_t Ptr = 0;
332
333
public:
334
30.9M
  LazyOffsetPtr() = default;
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::LazyOffsetPtr()
Line
Count
Source
334
19.3M
  LazyOffsetPtr() = default;
clang::LazyOffsetPtr<clang::CXXBaseSpecifier, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXBaseSpecifiers(unsigned long long))>::LazyOffsetPtr()
Line
Count
Source
334
6.70M
  LazyOffsetPtr() = default;
clang::LazyOffsetPtr<clang::CXXCtorInitializer*, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXCtorInitializers(unsigned long long))>::LazyOffsetPtr()
Line
Count
Source
334
982k
  LazyOffsetPtr() = default;
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::LazyOffsetPtr()
Line
Count
Source
334
3.87M
  LazyOffsetPtr() = default;
335
3.65M
  explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) {}
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::LazyOffsetPtr(clang::Stmt*)
Line
Count
Source
335
3.65M
  explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) {}
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::LazyOffsetPtr(clang::Decl*)
Line
Count
Source
335
1.28k
  explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) {}
336
337
484k
  explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
338
484k
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
339
484k
    if (Offset == 0)
340
0
      Ptr = 0;
341
484k
  }
342
343
840k
  LazyOffsetPtr &operator=(T *Ptr) {
344
840k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
840k
    return *this;
346
840k
  }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::operator=(clang::Decl*)
Line
Count
Source
343
121k
  LazyOffsetPtr &operator=(T *Ptr) {
344
121k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
121k
    return *this;
346
121k
  }
clang::LazyOffsetPtr<clang::CXXCtorInitializer*, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXCtorInitializers(unsigned long long))>::operator=(clang::CXXCtorInitializer**)
Line
Count
Source
343
231k
  LazyOffsetPtr &operator=(T *Ptr) {
344
231k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
231k
    return *this;
346
231k
  }
clang::LazyOffsetPtr<clang::CXXBaseSpecifier, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXBaseSpecifiers(unsigned long long))>::operator=(clang::CXXBaseSpecifier*)
Line
Count
Source
343
479k
  LazyOffsetPtr &operator=(T *Ptr) {
344
479k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
479k
    return *this;
346
479k
  }
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::operator=(clang::Stmt*)
Line
Count
Source
343
7.34k
  LazyOffsetPtr &operator=(T *Ptr) {
344
7.34k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
7.34k
    return *this;
346
7.34k
  }
347
348
1.76M
  LazyOffsetPtr &operator=(uint64_t Offset) {
349
1.76M
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
350
1.76M
    if (Offset == 0)
351
1.18M
      Ptr = 0;
352
575k
    else
353
575k
      Ptr = (Offset << 1) | 0x01;
354
355
1.76M
    return *this;
356
1.76M
  }
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::operator=(unsigned long long)
Line
Count
Source
348
16
  LazyOffsetPtr &operator=(uint64_t Offset) {
349
16
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
350
16
    if (Offset == 0)
351
0
      Ptr = 0;
352
16
    else
353
16
      Ptr = (Offset << 1) | 0x01;
354
355
16
    return *this;
356
16
  }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::operator=(unsigned long long)
Line
Count
Source
348
1.28M
  LazyOffsetPtr &operator=(uint64_t Offset) {
349
1.28M
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
350
1.28M
    if (Offset == 0)
351
1.18M
      Ptr = 0;
352
102k
    else
353
102k
      Ptr = (Offset << 1) | 0x01;
354
355
1.28M
    return *this;
356
1.28M
  }
clang::LazyOffsetPtr<clang::CXXCtorInitializer*, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXCtorInitializers(unsigned long long))>::operator=(unsigned long long)
Line
Count
Source
348
49.5k
  LazyOffsetPtr &operator=(uint64_t Offset) {
349
49.5k
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
350
49.5k
    if (Offset == 0)
351
0
      Ptr = 0;
352
49.5k
    else
353
49.5k
      Ptr = (Offset << 1) | 0x01;
354
355
49.5k
    return *this;
356
49.5k
  }
clang::LazyOffsetPtr<clang::CXXBaseSpecifier, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXBaseSpecifiers(unsigned long long))>::operator=(unsigned long long)
Line
Count
Source
348
422k
  LazyOffsetPtr &operator=(uint64_t Offset) {
349
422k
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
350
422k
    if (Offset == 0)
351
0
      Ptr = 0;
352
422k
    else
353
422k
      Ptr = (Offset << 1) | 0x01;
354
355
422k
    return *this;
356
422k
  }
357
358
  /// Whether this pointer is non-NULL.
359
  ///
360
  /// This operation does not require the AST node to be deserialized.
361
68.0M
  explicit operator bool() const { return Ptr != 0; }
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::operator bool() const
Line
Count
Source
361
67.1M
  explicit operator bool() const { return Ptr != 0; }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::operator bool() const
Line
Count
Source
361
881k
  explicit operator bool() const { return Ptr != 0; }
362
363
  /// Whether this pointer is non-NULL.
364
  ///
365
  /// This operation does not require the AST node to be deserialized.
366
217k
  bool isValid() const { return Ptr != 0; }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::isValid() const
Line
Count
Source
366
156k
  bool isValid() const { return Ptr != 0; }
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::isValid() const
Line
Count
Source
366
60.6k
  bool isValid() const { return Ptr != 0; }
367
368
  /// Whether this pointer is currently stored as an offset.
369
100M
  bool isOffset() const { return Ptr & 0x01; }
clang::LazyOffsetPtr<clang::CXXBaseSpecifier, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXBaseSpecifiers(unsigned long long))>::isOffset() const
Line
Count
Source
369
88.2M
  bool isOffset() const { return Ptr & 0x01; }
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::isOffset() const
Line
Count
Source
369
7.96M
  bool isOffset() const { return Ptr & 0x01; }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::isOffset() const
Line
Count
Source
369
4.14M
  bool isOffset() const { return Ptr & 0x01; }
clang::LazyOffsetPtr<clang::CXXCtorInitializer*, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXCtorInitializers(unsigned long long))>::isOffset() const
Line
Count
Source
369
503k
  bool isOffset() const { return Ptr & 0x01; }
370
371
  /// Retrieve the pointer to the AST node that this lazy pointer points to.
372
  ///
373
  /// \param Source the external AST source.
374
  ///
375
  /// \returns a pointer to the AST node.
376
54.9M
  T* get(ExternalASTSource *Source) const {
377
54.9M
    if (isOffset()) {
378
327k
      assert(Source &&
379
327k
             "Cannot deserialize a lazy pointer without an AST source");
380
327k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
327k
    }
382
54.9M
    return reinterpret_cast<T*>(Ptr);
383
54.9M
  }
clang::LazyOffsetPtr<clang::CXXBaseSpecifier, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXBaseSpecifiers(unsigned long long))>::get(clang::ExternalASTSource*) const
Line
Count
Source
376
43.8M
  T* get(ExternalASTSource *Source) const {
377
43.8M
    if (isOffset()) {
378
36.5k
      assert(Source &&
379
36.5k
             "Cannot deserialize a lazy pointer without an AST source");
380
36.5k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
36.5k
    }
382
43.8M
    return reinterpret_cast<T*>(Ptr);
383
43.8M
  }
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::get(clang::ExternalASTSource*) const
Line
Count
Source
376
7.96M
  T* get(ExternalASTSource *Source) const {
377
7.96M
    if (isOffset()) {
378
227k
      assert(Source &&
379
227k
             "Cannot deserialize a lazy pointer without an AST source");
380
227k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
227k
    }
382
7.96M
    return reinterpret_cast<T*>(Ptr);
383
7.96M
  }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::get(clang::ExternalASTSource*) const
Line
Count
Source
376
2.62M
  T* get(ExternalASTSource *Source) const {
377
2.62M
    if (isOffset()) {
378
32.5k
      assert(Source &&
379
32.5k
             "Cannot deserialize a lazy pointer without an AST source");
380
32.5k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
32.5k
    }
382
2.62M
    return reinterpret_cast<T*>(Ptr);
383
2.62M
  }
clang::LazyOffsetPtr<clang::CXXCtorInitializer*, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXCtorInitializers(unsigned long long))>::get(clang::ExternalASTSource*) const
Line
Count
Source
376
503k
  T* get(ExternalASTSource *Source) const {
377
503k
    if (isOffset()) {
378
30.9k
      assert(Source &&
379
30.9k
             "Cannot deserialize a lazy pointer without an AST source");
380
30.9k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
30.9k
    }
382
503k
    return reinterpret_cast<T*>(Ptr);
383
503k
  }
384
};
385
386
/// A lazy value (of type T) that is within an AST node of type Owner,
387
/// where the value might change in later generations of the external AST
388
/// source.
389
template<typename Owner, typename T, void (ExternalASTSource::*Update)(Owner)>
390
struct LazyGenerationalUpdatePtr {
391
  /// A cache of the value of this pointer, in the most recent generation in
392
  /// which we queried it.
393
  struct LazyData {
394
    ExternalASTSource *ExternalSource;
395
    uint32_t LastGeneration = 0;
396
    T LastValue;
397
398
    LazyData(ExternalASTSource *Source, T Value)
399
9.52M
        : ExternalSource(Source), LastValue(Value) {}
400
  };
401
402
  // Our value is represented as simply T if there is no external AST source.
403
  using ValueType = llvm::PointerUnion<T, LazyData*>;
404
  ValueType Value;
405
406
526M
  LazyGenerationalUpdatePtr(ValueType V) : Value(V) {}
407
408
  // Defined in ASTContext.h
409
  static ValueType makeValue(const ASTContext &Ctx, T Value);
410
411
public:
412
  explicit LazyGenerationalUpdatePtr(const ASTContext &Ctx, T Value = T())
413
45.5M
      : Value(makeValue(Ctx, Value)) {}
414
415
  /// Create a pointer that is not potentially updated by later generations of
416
  /// the external AST source.
417
  enum NotUpdatedTag { NotUpdated };
418
  LazyGenerationalUpdatePtr(NotUpdatedTag, T Value = T())
419
      : Value(Value) {}
420
421
  /// Forcibly set this pointer (which must be lazy) as needing updates.
422
490k
  void markIncomplete() {
423
490k
    Value.template get<LazyData *>()->LastGeneration = 0;
424
490k
  }
425
426
  /// Set the value of this pointer, in the current generation.
427
2.98M
  void set(T NewValue) {
428
2.98M
    if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) {
429
1.71M
      LazyVal->LastValue = NewValue;
430
1.71M
      return;
431
1.71M
    }
432
1.27M
    Value = NewValue;
433
1.27M
  }
434
435
  /// Set the value of this pointer, for this and all future generations.
436
  void setNotUpdated(T NewValue) { Value = NewValue; }
437
438
  /// Get the value of this pointer, updating its owner if necessary.
439
522M
  T get(Owner O) {
440
522M
    if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) {
441
103M
      if (LazyVal->LastGeneration != LazyVal->ExternalSource->getGeneration()) {
442
4.47M
        LazyVal->LastGeneration = LazyVal->ExternalSource->getGeneration();
443
4.47M
        (LazyVal->ExternalSource->*Update)(O);
444
4.47M
      }
445
103M
      return LazyVal->LastValue;
446
103M
    }
447
418M
    return Value.template get<T>();
448
418M
  }
449
450
  /// Get the most recently computed value of this pointer without updating it.
451
438k
  T getNotUpdated() const {
452
438k
    if (auto *LazyVal = Value.template dyn_cast<LazyData *>())
453
438k
      return LazyVal->LastValue;
454
0
    return Value.template get<T>();
455
0
  }
456
457
48.5M
  void *getOpaqueValue() { return Value.getOpaqueValue(); }
458
526M
  static LazyGenerationalUpdatePtr getFromOpaqueValue(void *Ptr) {
459
526M
    return LazyGenerationalUpdatePtr(ValueType::getFromOpaqueValue(Ptr));
460
526M
  }
461
};
462
463
} // namespace clang
464
465
/// Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be
466
/// placed into a PointerUnion.
467
namespace llvm {
468
469
template<typename Owner, typename T,
470
         void (clang::ExternalASTSource::*Update)(Owner)>
471
struct PointerLikeTypeTraits<
472
    clang::LazyGenerationalUpdatePtr<Owner, T, Update>> {
473
  using Ptr = clang::LazyGenerationalUpdatePtr<Owner, T, Update>;
474
475
48.5M
  static void *getAsVoidPointer(Ptr P) { return P.getOpaqueValue(); }
476
526M
  static Ptr getFromVoidPointer(void *P) { return Ptr::getFromOpaqueValue(P); }
477
478
  static constexpr int NumLowBitsAvailable =
479
      PointerLikeTypeTraits<T>::NumLowBitsAvailable - 1;
480
};
481
482
} // namespace llvm
483
484
namespace clang {
485
486
/// Represents a lazily-loaded vector of data.
487
///
488
/// The lazily-loaded vector of data contains data that is partially loaded
489
/// from an external source and partially added by local translation. The
490
/// items loaded from the external source are loaded lazily, when needed for
491
/// iteration over the complete vector.
492
template<typename T, typename Source,
493
         void (Source::*Loader)(SmallVectorImpl<T>&),
494
         unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
495
class LazyVector {
496
  SmallVector<T, LoadedStorage> Loaded;
497
  SmallVector<T, LocalStorage> Local;
498
499
public:
500
  /// Iteration over the elements in the vector.
501
  ///
502
  /// In a complete iteration, the iterator walks the range [-M, N),
503
  /// where negative values are used to indicate elements
504
  /// loaded from the external source while non-negative values are used to
505
  /// indicate elements added via \c push_back().
506
  /// However, to provide iteration in source order (for, e.g., chained
507
  /// precompiled headers), dereferencing the iterator flips the negative
508
  /// values (corresponding to loaded entities), so that position -M
509
  /// corresponds to element 0 in the loaded entities vector, position -M+1
510
  /// corresponds to element 1 in the loaded entities vector, etc. This
511
  /// gives us a reasonably efficient, source-order walk.
512
  ///
513
  /// We define this as a wrapping iterator around an int. The
514
  /// iterator_adaptor_base class forwards the iterator methods to basic integer
515
  /// arithmetic.
516
  class iterator
517
      : public llvm::iterator_adaptor_base<
518
            iterator, int, std::random_access_iterator_tag, T, int, T *, T &> {
519
    friend class LazyVector;
520
521
    LazyVector *Self;
522
523
    iterator(LazyVector *Self, int Position)
524
619k
        : iterator::iterator_adaptor_base(Position), Self(Self) {}
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::iterator::iterator(clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>*, int)
Line
Count
Source
524
100k
        : iterator::iterator_adaptor_base(Position), Self(Self) {}
clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>::iterator::iterator(clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>*, int)
Line
Count
Source
524
10.5k
        : iterator::iterator_adaptor_base(Position), Self(Self) {}
clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>::iterator::iterator(clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>*, int)
Line
Count
Source
524
354k
        : iterator::iterator_adaptor_base(Position), Self(Self) {}
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::iterator::iterator(clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>*, int)
Line
Count
Source
524
154k
        : iterator::iterator_adaptor_base(Position), Self(Self) {}
525
526
445k
    bool isLoaded() const { return this->I < 0; }
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::iterator::isLoaded() const
Line
Count
Source
526
362
    bool isLoaded() const { return this->I < 0; }
clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>::iterator::isLoaded() const
Line
Count
Source
526
734
    bool isLoaded() const { return this->I < 0; }
clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>::iterator::isLoaded() const
Line
Count
Source
526
433k
    bool isLoaded() const { return this->I < 0; }
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::iterator::isLoaded() const
Line
Count
Source
526
10.8k
    bool isLoaded() const { return this->I < 0; }
527
528
  public:
529
    iterator() : iterator(nullptr, 0) {}
530
531
294k
    typename iterator::reference operator*() const {
532
294k
      if (isLoaded())
533
1.50k
        return Self->Loaded.end()[this->I];
534
293k
      return Self->Local.begin()[this->I];
535
293k
    }
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::iterator::operator*() const
Line
Count
Source
531
362
    typename iterator::reference operator*() const {
532
362
      if (isLoaded())
533
3
        return Self->Loaded.end()[this->I];
534
359
      return Self->Local.begin()[this->I];
535
359
    }
clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>::iterator::operator*() const
Line
Count
Source
531
734
    typename iterator::reference operator*() const {
532
734
      if (isLoaded())
533
4
        return Self->Loaded.end()[this->I];
534
730
      return Self->Local.begin()[this->I];
535
730
    }
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::iterator::operator*() const
Line
Count
Source
531
10.8k
    typename iterator::reference operator*() const {
532
10.8k
      if (isLoaded())
533
1.10k
        return Self->Loaded.end()[this->I];
534
9.70k
      return Self->Local.begin()[this->I];
535
9.70k
    }
clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>::iterator::operator*() const
Line
Count
Source
531
282k
    typename iterator::reference operator*() const {
532
282k
      if (isLoaded())
533
392
        return Self->Loaded.end()[this->I];
534
282k
      return Self->Local.begin()[this->I];
535
282k
    }
536
  };
537
538
272k
  iterator begin(Source *source, bool LocalOnly = false) {
539
272k
    if (LocalOnly)
540
92.1k
      return iterator(this, 0);
541
542
180k
    if (source)
543
27.7k
      (source->*Loader)(Loaded);
544
180k
    return iterator(this, -(int)Loaded.size());
545
180k
  }
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::begin(clang::ExternalSemaSource*, bool)
Line
Count
Source
538
50.3k
  iterator begin(Source *source, bool LocalOnly = false) {
539
50.3k
    if (LocalOnly)
540
3.26k
      return iterator(this, 0);
541
542
47.0k
    if (source)
543
8.71k
      (source->*Loader)(Loaded);
544
47.0k
    return iterator(this, -(int)Loaded.size());
545
47.0k
  }
clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>::begin(clang::ExternalSemaSource*, bool)
Line
Count
Source
538
5.28k
  iterator begin(Source *source, bool LocalOnly = false) {
539
5.28k
    if (LocalOnly)
540
5.14k
      return iterator(this, 0);
541
542
144
    if (source)
543
2
      (source->*Loader)(Loaded);
544
144
    return iterator(this, -(int)Loaded.size());
545
144
  }
clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>::begin(clang::ExternalSemaSource*, bool)
Line
Count
Source
538
139k
  iterator begin(Source *source, bool LocalOnly = false) {
539
139k
    if (LocalOnly)
540
78.5k
      return iterator(this, 0);
541
542
60.8k
    if (source)
543
8.20k
      (source->*Loader)(Loaded);
544
60.8k
    return iterator(this, -(int)Loaded.size());
545
60.8k
  }
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::begin(clang::ExternalSemaSource*, bool)
Line
Count
Source
538
77.1k
  iterator begin(Source *source, bool LocalOnly = false) {
539
77.1k
    if (LocalOnly)
540
5.14k
      return iterator(this, 0);
541
542
71.9k
    if (source)
543
10.7k
      (source->*Loader)(Loaded);
544
71.9k
    return iterator(this, -(int)Loaded.size());
545
71.9k
  }
546
547
347k
  iterator end() {
548
347k
    return iterator(this, Local.size());
549
347k
  }
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::end()
Line
Count
Source
547
50.3k
  iterator end() {
548
50.3k
    return iterator(this, Local.size());
549
50.3k
  }
clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>::end()
Line
Count
Source
547
5.28k
  iterator end() {
548
5.28k
    return iterator(this, Local.size());
549
5.28k
  }
clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>::end()
Line
Count
Source
547
214k
  iterator end() {
548
214k
    return iterator(this, Local.size());
549
214k
  }
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::end()
Line
Count
Source
547
77.1k
  iterator end() {
548
77.1k
    return iterator(this, Local.size());
549
77.1k
  }
550
551
49.7k
  void push_back(const T& LocalValue) {
552
49.7k
    Local.push_back(LocalValue);
553
49.7k
  }
clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>::push_back(clang::DeclaratorDecl const* const&)
Line
Count
Source
551
37.3k
  void push_back(const T& LocalValue) {
552
37.3k
    Local.push_back(LocalValue);
553
37.3k
  }
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::push_back(clang::VarDecl* const&)
Line
Count
Source
551
9.39k
  void push_back(const T& LocalValue) {
552
9.39k
    Local.push_back(LocalValue);
553
9.39k
  }
clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>::push_back(clang::TypedefNameDecl* const&)
Line
Count
Source
551
2.61k
  void push_back(const T& LocalValue) {
552
2.61k
    Local.push_back(LocalValue);
553
2.61k
  }
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::push_back(clang::CXXConstructorDecl* const&)
Line
Count
Source
551
359
  void push_back(const T& LocalValue) {
552
359
    Local.push_back(LocalValue);
553
359
  }
554
555
75.3k
  void erase(iterator From, iterator To) {
556
75.3k
    if (From.isLoaded() && 
To.isLoaded()0
) {
557
0
      Loaded.erase(&*From, &*To);
558
0
      return;
559
0
    }
560
561
75.3k
    if (From.isLoaded()) {
562
0
      Loaded.erase(&*From, Loaded.end());
563
0
      From = begin(nullptr, true);
564
0
    }
565
566
75.3k
    Local.erase(&*From, &*To);
567
75.3k
  }
568
};
569
570
/// A lazy pointer to a statement.
571
using LazyDeclStmtPtr =
572
    LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>;
573
574
/// A lazy pointer to a declaration.
575
using LazyDeclPtr =
576
    LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>;
577
578
/// A lazy pointer to a set of CXXCtorInitializers.
579
using LazyCXXCtorInitializersPtr =
580
    LazyOffsetPtr<CXXCtorInitializer *, uint64_t,
581
                  &ExternalASTSource::GetExternalCXXCtorInitializers>;
582
583
/// A lazy pointer to a set of CXXBaseSpecifiers.
584
using LazyCXXBaseSpecifiersPtr =
585
    LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
586
                  &ExternalASTSource::GetExternalCXXBaseSpecifiers>;
587
588
} // namespace clang
589
590
#endif // LLVM_CLANG_AST_EXTERNALASTSOURCE_H