Coverage Report

Created: 2020-09-15 12:33

/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
48.8k
  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
20.8M
    explicit Deserializing(ExternalASTSource *source) : Source(source) {
83
20.8M
      assert(Source);
84
20.8M
      Source->StartedDeserializing();
85
20.8M
    }
86
87
20.8M
    ~Deserializing() {
88
20.8M
      Source->FinishedDeserializing();
89
20.8M
    }
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
84.9M
  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
51.4k
                                SmallVectorImpl<Decl *> &Result) {
187
370k
    FindExternalLexicalDecls(DC, [](Decl::Kind) { return true; }, Result);
188
51.4k
  }
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
429k
  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
27.9M
  LazyOffsetPtr() = default;
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::LazyOffsetPtr()
Line
Count
Source
334
18.1M
  LazyOffsetPtr() = default;
clang::LazyOffsetPtr<clang::CXXBaseSpecifier, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXBaseSpecifiers(unsigned long long))>::LazyOffsetPtr()
Line
Count
Source
334
5.70M
  LazyOffsetPtr() = default;
clang::LazyOffsetPtr<clang::CXXCtorInitializer*, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXCtorInitializers(unsigned long long))>::LazyOffsetPtr()
Line
Count
Source
334
781k
  LazyOffsetPtr() = default;
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::LazyOffsetPtr()
Line
Count
Source
334
3.30M
  LazyOffsetPtr() = default;
335
3.38M
  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.38M
  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.25k
  explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) {}
336
337
210k
  explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
338
210k
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
339
210k
    if (Offset == 0)
340
0
      Ptr = 0;
341
210k
  }
342
343
752k
  LazyOffsetPtr &operator=(T *Ptr) {
344
752k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
752k
    return *this;
346
752k
  }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::operator=(clang::Decl*)
Line
Count
Source
343
96.8k
  LazyOffsetPtr &operator=(T *Ptr) {
344
96.8k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
96.8k
    return *this;
346
96.8k
  }
clang::LazyOffsetPtr<clang::CXXCtorInitializer*, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXCtorInitializers(unsigned long long))>::operator=(clang::CXXCtorInitializer**)
Line
Count
Source
343
201k
  LazyOffsetPtr &operator=(T *Ptr) {
344
201k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
201k
    return *this;
346
201k
  }
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::operator=(clang::Stmt*)
Line
Count
Source
343
7.33k
  LazyOffsetPtr &operator=(T *Ptr) {
344
7.33k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
7.33k
    return *this;
346
7.33k
  }
clang::LazyOffsetPtr<clang::CXXBaseSpecifier, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXBaseSpecifiers(unsigned long long))>::operator=(clang::CXXBaseSpecifier*)
Line
Count
Source
343
447k
  LazyOffsetPtr &operator=(T *Ptr) {
344
447k
    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
345
447k
    return *this;
346
447k
  }
347
348
1.17M
  LazyOffsetPtr &operator=(uint64_t Offset) {
349
1.17M
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
350
1.17M
    if (Offset == 0)
351
806k
      Ptr = 0;
352
365k
    else
353
365k
      Ptr = (Offset << 1) | 0x01;
354
1.17M
355
1.17M
    return *this;
356
1.17M
  }
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
16
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
861k
  LazyOffsetPtr &operator=(uint64_t Offset) {
349
861k
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
350
861k
    if (Offset == 0)
351
806k
      Ptr = 0;
352
54.1k
    else
353
54.1k
      Ptr = (Offset << 1) | 0x01;
354
861k
355
861k
    return *this;
356
861k
  }
clang::LazyOffsetPtr<clang::CXXCtorInitializer*, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXCtorInitializers(unsigned long long))>::operator=(unsigned long long)
Line
Count
Source
348
21.3k
  LazyOffsetPtr &operator=(uint64_t Offset) {
349
21.3k
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
350
21.3k
    if (Offset == 0)
351
0
      Ptr = 0;
352
21.3k
    else
353
21.3k
      Ptr = (Offset << 1) | 0x01;
354
21.3k
355
21.3k
    return *this;
356
21.3k
  }
clang::LazyOffsetPtr<clang::CXXBaseSpecifier, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXBaseSpecifiers(unsigned long long))>::operator=(unsigned long long)
Line
Count
Source
348
289k
  LazyOffsetPtr &operator=(uint64_t Offset) {
349
289k
    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
350
289k
    if (Offset == 0)
351
0
      Ptr = 0;
352
289k
    else
353
289k
      Ptr = (Offset << 1) | 0x01;
354
289k
355
289k
    return *this;
356
289k
  }
357
358
  /// Whether this pointer is non-NULL.
359
  ///
360
  /// This operation does not require the AST node to be deserialized.
361
60.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
59.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
842k
  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
214k
  bool isValid() const { return Ptr != 0; }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::isValid() const
Line
Count
Source
366
153k
  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
92.6M
  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
83.1M
  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.81M
  bool isOffset() const { return Ptr & 0x01; }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::isOffset() const
Line
Count
Source
369
1.11M
  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
513k
  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
50.5M
  T* get(ExternalASTSource *Source) const {
377
50.5M
    if (isOffset()) {
378
56.8k
      assert(Source &&
379
56.8k
             "Cannot deserialize a lazy pointer without an AST source");
380
56.8k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
56.8k
    }
382
50.5M
    return reinterpret_cast<T*>(Ptr);
383
50.5M
  }
clang::LazyOffsetPtr<clang::CXXBaseSpecifier, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXBaseSpecifiers(unsigned long long))>::get(clang::ExternalASTSource*) const
Line
Count
Source
376
41.3M
  T* get(ExternalASTSource *Source) const {
377
41.3M
    if (isOffset()) {
378
14.3k
      assert(Source &&
379
14.3k
             "Cannot deserialize a lazy pointer without an AST source");
380
14.3k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
14.3k
    }
382
41.3M
    return reinterpret_cast<T*>(Ptr);
383
41.3M
  }
clang::LazyOffsetPtr<clang::Stmt, unsigned long long, &(clang::ExternalASTSource::GetExternalDeclStmt(unsigned long long))>::get(clang::ExternalASTSource*) const
Line
Count
Source
376
7.81M
  T* get(ExternalASTSource *Source) const {
377
7.81M
    if (isOffset()) {
378
36.2k
      assert(Source &&
379
36.2k
             "Cannot deserialize a lazy pointer without an AST source");
380
36.2k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
36.2k
    }
382
7.81M
    return reinterpret_cast<T*>(Ptr);
383
7.81M
  }
clang::LazyOffsetPtr<clang::Decl, unsigned int, &(clang::ExternalASTSource::GetExternalDecl(unsigned int))>::get(clang::ExternalASTSource*) const
Line
Count
Source
376
874k
  T* get(ExternalASTSource *Source) const {
377
874k
    if (isOffset()) {
378
1.91k
      assert(Source &&
379
1.91k
             "Cannot deserialize a lazy pointer without an AST source");
380
1.91k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
1.91k
    }
382
874k
    return reinterpret_cast<T*>(Ptr);
383
874k
  }
clang::LazyOffsetPtr<clang::CXXCtorInitializer*, unsigned long long, &(clang::ExternalASTSource::GetExternalCXXCtorInitializers(unsigned long long))>::get(clang::ExternalASTSource*) const
Line
Count
Source
376
513k
  T* get(ExternalASTSource *Source) const {
377
513k
    if (isOffset()) {
378
4.29k
      assert(Source &&
379
4.29k
             "Cannot deserialize a lazy pointer without an AST source");
380
4.29k
      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
381
4.29k
    }
382
513k
    return reinterpret_cast<T*>(Ptr);
383
513k
  }
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
6.20M
        : 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
470M
  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
41.4M
      : 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
212k
  void markIncomplete() {
423
212k
    Value.template get<LazyData *>()->LastGeneration = 0;
424
212k
  }
425
426
  /// Set the value of this pointer, in the current generation.
427
2.40M
  void set(T NewValue) {
428
2.40M
    if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) {
429
1.14M
      LazyVal->LastValue = NewValue;
430
1.14M
      return;
431
1.14M
    }
432
1.25M
    Value = NewValue;
433
1.25M
  }
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
468M
  T get(Owner O) {
440
468M
    if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) {
441
76.7M
      if (LazyVal->LastGeneration != LazyVal->ExternalSource->getGeneration()) {
442
2.64M
        LazyVal->LastGeneration = LazyVal->ExternalSource->getGeneration();
443
2.64M
        (LazyVal->ExternalSource->*Update)(O);
444
2.64M
      }
445
76.7M
      return LazyVal->LastValue;
446
76.7M
    }
447
391M
    return Value.template get<T>();
448
391M
  }
449
450
  /// Get the most recently computed value of this pointer without updating it.
451
221k
  T getNotUpdated() const {
452
221k
    if (auto *LazyVal = Value.template dyn_cast<LazyData *>())
453
221k
      return LazyVal->LastValue;
454
0
    return Value.template get<T>();
455
0
  }
456
457
43.8M
  void *getOpaqueValue() { return Value.getOpaqueValue(); }
458
470M
  static LazyGenerationalUpdatePtr getFromOpaqueValue(void *Ptr) {
459
470M
    return LazyGenerationalUpdatePtr(ValueType::getFromOpaqueValue(Ptr));
460
470M
  }
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
43.8M
  static void *getAsVoidPointer(Ptr P) { return P.getOpaqueValue(); }
476
470M
  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
602k
        : 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
97.8k
        : 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.3k
        : 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
344k
        : 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
149k
        : iterator::iterator_adaptor_base(Position), Self(Self) {}
525
526
421k
    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
361
    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
732
    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
410k
    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.4k
    bool isLoaded() const { return this->I < 0; }
527
528
  public:
529
    iterator() : iterator(nullptr, 0) {}
530
531
275k
    typename iterator::reference operator*() const {
532
275k
      if (isLoaded())
533
1.50k
        return Self->Loaded.end()[this->I];
534
274k
      return Self->Local.begin()[this->I];
535
274k
    }
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::iterator::operator*() const
Line
Count
Source
531
361
    typename iterator::reference operator*() const {
532
361
      if (isLoaded())
533
3
        return Self->Loaded.end()[this->I];
534
358
      return Self->Local.begin()[this->I];
535
358
    }
clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>::iterator::operator*() const
Line
Count
Source
531
732
    typename iterator::reference operator*() const {
532
732
      if (isLoaded())
533
4
        return Self->Loaded.end()[this->I];
534
728
      return Self->Local.begin()[this->I];
535
728
    }
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::iterator::operator*() const
Line
Count
Source
531
10.4k
    typename iterator::reference operator*() const {
532
10.4k
      if (isLoaded())
533
1.10k
        return Self->Loaded.end()[this->I];
534
9.29k
      return Self->Local.begin()[this->I];
535
9.29k
    }
clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>::iterator::operator*() const
Line
Count
Source
531
264k
    typename iterator::reference operator*() const {
532
264k
      if (isLoaded())
533
392
        return Self->Loaded.end()[this->I];
534
263k
      return Self->Local.begin()[this->I];
535
263k
    }
536
  };
537
538
264k
  iterator begin(Source *source, bool LocalOnly = false) {
539
264k
    if (LocalOnly)
540
89.5k
      return iterator(this, 0);
541
175k
542
175k
    if (source)
543
26.7k
      (source->*Loader)(Loaded);
544
175k
    return iterator(this, -(int)Loaded.size());
545
175k
  }
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::begin(clang::ExternalSemaSource*, bool)
Line
Count
Source
538
48.9k
  iterator begin(Source *source, bool LocalOnly = false) {
539
48.9k
    if (LocalOnly)
540
3.17k
      return iterator(this, 0);
541
45.7k
542
45.7k
    if (source)
543
8.40k
      (source->*Loader)(Loaded);
544
45.7k
    return iterator(this, -(int)Loaded.size());
545
45.7k
  }
clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>::begin(clang::ExternalSemaSource*, bool)
Line
Count
Source
538
5.18k
  iterator begin(Source *source, bool LocalOnly = false) {
539
5.18k
    if (LocalOnly)
540
5.04k
      return iterator(this, 0);
541
142
542
142
    if (source)
543
2
      (source->*Loader)(Loaded);
544
142
    return iterator(this, -(int)Loaded.size());
545
142
  }
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
135k
  iterator begin(Source *source, bool LocalOnly = false) {
539
135k
    if (LocalOnly)
540
76.3k
      return iterator(this, 0);
541
59.2k
542
59.2k
    if (source)
543
7.90k
      (source->*Loader)(Loaded);
544
59.2k
    return iterator(this, -(int)Loaded.size());
545
59.2k
  }
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::begin(clang::ExternalSemaSource*, bool)
Line
Count
Source
538
74.9k
  iterator begin(Source *source, bool LocalOnly = false) {
539
74.9k
    if (LocalOnly)
540
5.04k
      return iterator(this, 0);
541
69.9k
542
69.9k
    if (source)
543
10.4k
      (source->*Loader)(Loaded);
544
69.9k
    return iterator(this, -(int)Loaded.size());
545
69.9k
  }
546
547
337k
  iterator end() {
548
337k
    return iterator(this, Local.size());
549
337k
  }
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::end()
Line
Count
Source
547
48.9k
  iterator end() {
548
48.9k
    return iterator(this, Local.size());
549
48.9k
  }
clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>::end()
Line
Count
Source
547
5.18k
  iterator end() {
548
5.18k
    return iterator(this, Local.size());
549
5.18k
  }
clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>::end()
Line
Count
Source
547
208k
  iterator end() {
548
208k
    return iterator(this, Local.size());
549
208k
  }
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::end()
Line
Count
Source
547
74.9k
  iterator end() {
548
74.9k
    return iterator(this, Local.size());
549
74.9k
  }
550
551
46.0k
  void push_back(const T& LocalValue) {
552
46.0k
    Local.push_back(LocalValue);
553
46.0k
  }
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
34.1k
  void push_back(const T& LocalValue) {
552
34.1k
    Local.push_back(LocalValue);
553
34.1k
  }
clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>::push_back(clang::VarDecl* const&)
Line
Count
Source
551
8.99k
  void push_back(const T& LocalValue) {
552
8.99k
    Local.push_back(LocalValue);
553
8.99k
  }
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.55k
  void push_back(const T& LocalValue) {
552
2.55k
    Local.push_back(LocalValue);
553
2.55k
  }
clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>::push_back(clang::CXXConstructorDecl* const&)
Line
Count
Source
551
358
  void push_back(const T& LocalValue) {
552
358
    Local.push_back(LocalValue);
553
358
  }
554
555
73.1k
  void erase(iterator From, iterator To) {
556
73.1k
    if (From.isLoaded() && 
To.isLoaded()0
) {
557
0
      Loaded.erase(&*From, &*To);
558
0
      return;
559
0
    }
560
73.1k
561
73.1k
    if (From.isLoaded()) {
562
0
      Loaded.erase(&*From, Loaded.end());
563
0
      From = begin(nullptr, true);
564
0
    }
565
73.1k
566
73.1k
    Local.erase(&*From, &*To);
567
73.1k
  }
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