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   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     explicit Deserializing(ExternalASTSource *source) : Source(source) {
83       assert(Source);
84       Source->StartedDeserializing();
85     }
86 
87     ~Deserializing() {
88       Source->FinishedDeserializing();
89     }
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   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   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   virtual Module *getModule(unsigned ID) { return nullptr; }
163 
164   /// Determine whether D comes from a PCH which was built with a corresponding
165   /// object file.
166   virtual bool DeclIsFromPCHWithObjectFile(const Decl *D) { return false; }
167 
168   /// Return a descriptor for the corresponding module, if one exists.
169   virtual llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID);
170 
171   enum ExtKind { EK_Always, EK_Never, EK_ReplyHazy };
172 
173   virtual ExtKind hasExternalDefinitions(const Decl *D);
174 
175   /// Finds all declarations lexically contained within the given
176   /// DeclContext, after applying an optional filter predicate.
177   ///
178   /// \param IsKindWeWant a predicate function that returns true if the passed
179   /// declaration kind is one we are looking for.
180   ///
181   /// The default implementation of this method is a no-op.
182   virtual void
183   FindExternalLexicalDecls(const DeclContext *DC,
184                            llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
185                            SmallVectorImpl<Decl *> &Result);
186 
187   /// Finds all declarations lexically contained within the given
188   /// DeclContext.
189   void FindExternalLexicalDecls(const DeclContext *DC,
190                                 SmallVectorImpl<Decl *> &Result) {
191     FindExternalLexicalDecls(DC, [](Decl::Kind) { return true; }, Result);
192   }
193 
194   /// Get the decls that are contained in a file in the Offset/Length
195   /// range. \p Length can be 0 to indicate a point at \p Offset instead of
196   /// a range.
197   virtual void FindFileRegionDecls(FileID File, unsigned Offset,
198                                    unsigned Length,
199                                    SmallVectorImpl<Decl *> &Decls);
200 
201   /// Gives the external AST source an opportunity to complete
202   /// the redeclaration chain for a declaration. Called each time we
203   /// need the most recent declaration of a declaration after the
204   /// generation count is incremented.
205   virtual void CompleteRedeclChain(const Decl *D);
206 
207   /// Gives the external AST source an opportunity to complete
208   /// an incomplete type.
209   virtual void CompleteType(TagDecl *Tag);
210 
211   /// Gives the external AST source an opportunity to complete an
212   /// incomplete Objective-C class.
213   ///
214   /// This routine will only be invoked if the "externally completed" bit is
215   /// set on the ObjCInterfaceDecl via the function
216   /// \c ObjCInterfaceDecl::setExternallyCompleted().
217   virtual void CompleteType(ObjCInterfaceDecl *Class);
218 
219   /// Loads comment ranges.
220   virtual void ReadComments();
221 
222   /// Notify ExternalASTSource that we started deserialization of
223   /// a decl or type so until FinishedDeserializing is called there may be
224   /// decls that are initializing. Must be paired with FinishedDeserializing.
225   ///
226   /// The default implementation of this method is a no-op.
227   virtual void StartedDeserializing();
228 
229   /// Notify ExternalASTSource that we finished the deserialization of
230   /// a decl or type. Must be paired with StartedDeserializing.
231   ///
232   /// The default implementation of this method is a no-op.
233   virtual void FinishedDeserializing();
234 
235   /// Function that will be invoked when we begin parsing a new
236   /// translation unit involving this external AST source.
237   ///
238   /// The default implementation of this method is a no-op.
239   virtual void StartTranslationUnit(ASTConsumer *Consumer);
240 
241   /// Print any statistics that have been gathered regarding
242   /// the external AST source.
243   ///
244   /// The default implementation of this method is a no-op.
245   virtual void PrintStats();
246 
247   /// Perform layout on the given record.
248   ///
249   /// This routine allows the external AST source to provide an specific
250   /// layout for a record, overriding the layout that would normally be
251   /// constructed. It is intended for clients who receive specific layout
252   /// details rather than source code (such as LLDB). The client is expected
253   /// to fill in the field offsets, base offsets, virtual base offsets, and
254   /// complete object size.
255   ///
256   /// \param Record The record whose layout is being requested.
257   ///
258   /// \param Size The final size of the record, in bits.
259   ///
260   /// \param Alignment The final alignment of the record, in bits.
261   ///
262   /// \param FieldOffsets The offset of each of the fields within the record,
263   /// expressed in bits. All of the fields must be provided with offsets.
264   ///
265   /// \param BaseOffsets The offset of each of the direct, non-virtual base
266   /// classes. If any bases are not given offsets, the bases will be laid
267   /// out according to the ABI.
268   ///
269   /// \param VirtualBaseOffsets The offset of each of the virtual base classes
270   /// (either direct or not). If any bases are not given offsets, the bases will be laid
271   /// out according to the ABI.
272   ///
273   /// \returns true if the record layout was provided, false otherwise.
274   virtual bool layoutRecordType(
275       const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
276       llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
277       llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
278       llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets);
279 
280   //===--------------------------------------------------------------------===//
281   // Queries for performance analysis.
282   //===--------------------------------------------------------------------===//
283 
284   struct MemoryBufferSizes {
285     size_t malloc_bytes;
286     size_t mmap_bytes;
287 
288     MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
289         : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
290   };
291 
292   /// Return the amount of memory used by memory buffers, breaking down
293   /// by heap-backed versus mmap'ed memory.
294   MemoryBufferSizes getMemoryBufferSizes() const {
295     MemoryBufferSizes sizes(0, 0);
296     getMemoryBufferSizes(sizes);
297     return sizes;
298   }
299 
300   virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
301 
302   /// LLVM-style RTTI.
303   /// \{
304   virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
305   static bool classof(const ExternalASTSource *S) { return S->isA(&ID); }
306   /// \}
307 
308 protected:
309   static DeclContextLookupResult
310   SetExternalVisibleDeclsForName(const DeclContext *DC,
311                                  DeclarationName Name,
312                                  ArrayRef<NamedDecl*> Decls);
313 
314   static DeclContextLookupResult
315   SetNoExternalVisibleDeclsForName(const DeclContext *DC,
316                                    DeclarationName Name);
317 
318   /// Increment the current generation.
319   uint32_t incrementGeneration(ASTContext &C);
320 };
321 
322 /// A lazy pointer to an AST node (of base type T) that resides
323 /// within an external AST source.
324 ///
325 /// The AST node is identified within the external AST source by a
326 /// 63-bit offset, and can be retrieved via an operation on the
327 /// external AST source itself.
328 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
329 struct LazyOffsetPtr {
330   /// Either a pointer to an AST node or the offset within the
331   /// external AST source where the AST node can be found.
332   ///
333   /// If the low bit is clear, a pointer to the AST node. If the low
334   /// bit is set, the upper 63 bits are the offset.
335   mutable uint64_t Ptr = 0;
336 
337 public:
338   LazyOffsetPtr() = default;
339   explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) {}
340 
341   explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
342     assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
343     if (Offset == 0)
344       Ptr = 0;
345   }
346 
347   LazyOffsetPtr &operator=(T *Ptr) {
348     this->Ptr = reinterpret_cast<uint64_t>(Ptr);
349     return *this;
350   }
351 
352   LazyOffsetPtr &operator=(uint64_t Offset) {
353     assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
354     if (Offset == 0)
355       Ptr = 0;
356     else
357       Ptr = (Offset << 1) | 0x01;
358 
359     return *this;
360   }
361 
362   /// Whether this pointer is non-NULL.
363   ///
364   /// This operation does not require the AST node to be deserialized.
365   explicit operator bool() const { return Ptr != 0; }
366 
367   /// Whether this pointer is non-NULL.
368   ///
369   /// This operation does not require the AST node to be deserialized.
370   bool isValid() const { return Ptr != 0; }
371 
372   /// Whether this pointer is currently stored as an offset.
373   bool isOffset() const { return Ptr & 0x01; }
374 
375   /// Retrieve the pointer to the AST node that this lazy pointer points to.
376   ///
377   /// \param Source the external AST source.
378   ///
379   /// \returns a pointer to the AST node.
380   T* get(ExternalASTSource *Source) const {
381     if (isOffset()) {
382       assert(Source &&
383              "Cannot deserialize a lazy pointer without an AST source");
384       Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
385     }
386     return reinterpret_cast<T*>(Ptr);
387   }
388 };
389 
390 /// A lazy value (of type T) that is within an AST node of type Owner,
391 /// where the value might change in later generations of the external AST
392 /// source.
393 template<typename Owner, typename T, void (ExternalASTSource::*Update)(Owner)>
394 struct LazyGenerationalUpdatePtr {
395   /// A cache of the value of this pointer, in the most recent generation in
396   /// which we queried it.
397   struct LazyData {
398     ExternalASTSource *ExternalSource;
399     uint32_t LastGeneration = 0;
400     T LastValue;
401 
402     LazyData(ExternalASTSource *Source, T Value)
403         : ExternalSource(Source), LastValue(Value) {}
404   };
405 
406   // Our value is represented as simply T if there is no external AST source.
407   using ValueType = llvm::PointerUnion<T, LazyData*>;
408   ValueType Value;
409 
410   LazyGenerationalUpdatePtr(ValueType V) : Value(V) {}
411 
412   // Defined in ASTContext.h
413   static ValueType makeValue(const ASTContext &Ctx, T Value);
414 
415 public:
416   explicit LazyGenerationalUpdatePtr(const ASTContext &Ctx, T Value = T())
417       : Value(makeValue(Ctx, Value)) {}
418 
419   /// Create a pointer that is not potentially updated by later generations of
420   /// the external AST source.
421   enum NotUpdatedTag { NotUpdated };
422   LazyGenerationalUpdatePtr(NotUpdatedTag, T Value = T())
423       : Value(Value) {}
424 
425   /// Forcibly set this pointer (which must be lazy) as needing updates.
426   void markIncomplete() {
427     Value.template get<LazyData *>()->LastGeneration = 0;
428   }
429 
430   /// Set the value of this pointer, in the current generation.
431   void set(T NewValue) {
432     if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) {
433       LazyVal->LastValue = NewValue;
434       return;
435     }
436     Value = NewValue;
437   }
438 
439   /// Set the value of this pointer, for this and all future generations.
440   void setNotUpdated(T NewValue) { Value = NewValue; }
441 
442   /// Get the value of this pointer, updating its owner if necessary.
443   T get(Owner O) {
444     if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) {
445       if (LazyVal->LastGeneration != LazyVal->ExternalSource->getGeneration()) {
446         LazyVal->LastGeneration = LazyVal->ExternalSource->getGeneration();
447         (LazyVal->ExternalSource->*Update)(O);
448       }
449       return LazyVal->LastValue;
450     }
451     return Value.template get<T>();
452   }
453 
454   /// Get the most recently computed value of this pointer without updating it.
455   T getNotUpdated() const {
456     if (auto *LazyVal = Value.template dyn_cast<LazyData *>())
457       return LazyVal->LastValue;
458     return Value.template get<T>();
459   }
460 
461   void *getOpaqueValue() { return Value.getOpaqueValue(); }
462   static LazyGenerationalUpdatePtr getFromOpaqueValue(void *Ptr) {
463     return LazyGenerationalUpdatePtr(ValueType::getFromOpaqueValue(Ptr));
464   }
465 };
466 
467 } // namespace clang
468 
469 /// Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be
470 /// placed into a PointerUnion.
471 namespace llvm {
472 
473 template<typename Owner, typename T,
474          void (clang::ExternalASTSource::*Update)(Owner)>
475 struct PointerLikeTypeTraits<
476     clang::LazyGenerationalUpdatePtr<Owner, T, Update>> {
477   using Ptr = clang::LazyGenerationalUpdatePtr<Owner, T, Update>;
478 
479   static void *getAsVoidPointer(Ptr P) { return P.getOpaqueValue(); }
480   static Ptr getFromVoidPointer(void *P) { return Ptr::getFromOpaqueValue(P); }
481 
482   static constexpr int NumLowBitsAvailable =
483       PointerLikeTypeTraits<T>::NumLowBitsAvailable - 1;
484 };
485 
486 } // namespace llvm
487 
488 namespace clang {
489 
490 /// Represents a lazily-loaded vector of data.
491 ///
492 /// The lazily-loaded vector of data contains data that is partially loaded
493 /// from an external source and partially added by local translation. The
494 /// items loaded from the external source are loaded lazily, when needed for
495 /// iteration over the complete vector.
496 template<typename T, typename Source,
497          void (Source::*Loader)(SmallVectorImpl<T>&),
498          unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
499 class LazyVector {
500   SmallVector<T, LoadedStorage> Loaded;
501   SmallVector<T, LocalStorage> Local;
502 
503 public:
504   /// Iteration over the elements in the vector.
505   ///
506   /// In a complete iteration, the iterator walks the range [-M, N),
507   /// where negative values are used to indicate elements
508   /// loaded from the external source while non-negative values are used to
509   /// indicate elements added via \c push_back().
510   /// However, to provide iteration in source order (for, e.g., chained
511   /// precompiled headers), dereferencing the iterator flips the negative
512   /// values (corresponding to loaded entities), so that position -M
513   /// corresponds to element 0 in the loaded entities vector, position -M+1
514   /// corresponds to element 1 in the loaded entities vector, etc. This
515   /// gives us a reasonably efficient, source-order walk.
516   ///
517   /// We define this as a wrapping iterator around an int. The
518   /// iterator_adaptor_base class forwards the iterator methods to basic integer
519   /// arithmetic.
520   class iterator
521       : public llvm::iterator_adaptor_base<
522             iterator, int, std::random_access_iterator_tag, T, int, T *, T &> {
523     friend class LazyVector;
524 
525     LazyVector *Self;
526 
527     iterator(LazyVector *Self, int Position)
528         : iterator::iterator_adaptor_base(Position), Self(Self) {}
529 
530     bool isLoaded() const { return this->I < 0; }
531 
532   public:
533     iterator() : iterator(nullptr, 0) {}
534 
535     typename iterator::reference operator*() const {
536       if (isLoaded())
537         return Self->Loaded.end()[this->I];
538       return Self->Local.begin()[this->I];
539     }
540   };
541 
542   iterator begin(Source *source, bool LocalOnly = false) {
543     if (LocalOnly)
544       return iterator(this, 0);
545 
546     if (source)
547       (source->*Loader)(Loaded);
548     return iterator(this, -(int)Loaded.size());
549   }
550 
551   iterator end() {
552     return iterator(this, Local.size());
553   }
554 
555   void push_back(const T& LocalValue) {
556     Local.push_back(LocalValue);
557   }
558 
559   void erase(iterator From, iterator To) {
560     if (From.isLoaded() && To.isLoaded()) {
561       Loaded.erase(&*From, &*To);
562       return;
563     }
564 
565     if (From.isLoaded()) {
566       Loaded.erase(&*From, Loaded.end());
567       From = begin(nullptr, true);
568     }
569 
570     Local.erase(&*From, &*To);
571   }
572 };
573 
574 /// A lazy pointer to a statement.
575 using LazyDeclStmtPtr =
576     LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>;
577 
578 /// A lazy pointer to a declaration.
579 using LazyDeclPtr =
580     LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>;
581 
582 /// A lazy pointer to a set of CXXCtorInitializers.
583 using LazyCXXCtorInitializersPtr =
584     LazyOffsetPtr<CXXCtorInitializer *, uint64_t,
585                   &ExternalASTSource::GetExternalCXXCtorInitializers>;
586 
587 /// A lazy pointer to a set of CXXBaseSpecifiers.
588 using LazyCXXBaseSpecifiersPtr =
589     LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
590                   &ExternalASTSource::GetExternalCXXBaseSpecifiers>;
591 
592 } // namespace clang
593 
594 #endif // LLVM_CLANG_AST_EXTERNALASTSOURCE_H
595