1 //===- ExternalASTSource.h - Abstract External AST Interface ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ExternalASTSource interface, which enables
11 // construction of AST nodes from some external source.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_EXTERNALASTSOURCE_H
16 #define LLVM_CLANG_AST_EXTERNALASTSOURCE_H
18 #include "clang/AST/CharUnits.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/Basic/Module.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/IntrusiveRefCntPtr.h"
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/ADT/PointerUnion.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/ADT/iterator.h"
32 #include "llvm/Support/PointerLikeTypeTraits.h"
44 class CXXBaseSpecifier;
45 class CXXCtorInitializer;
47 class DeclarationName;
51 class ObjCInterfaceDecl;
57 /// \brief Abstract interface for external sources of AST nodes.
59 /// External AST sources provide AST nodes constructed from some
60 /// external source, such as a precompiled header. External AST
61 /// sources can resolve types and declarations from abstract IDs into
62 /// actual type and declaration nodes, and read parts of declaration
64 class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
65 friend class ExternalSemaSource;
67 /// Generation number for this external AST source. Must be increased
68 /// whenever we might have added new redeclarations for existing decls.
69 uint32_t CurrentGeneration = 0;
71 /// \brief Whether this AST source also provides information for
72 /// semantic analysis.
73 bool SemaSource = false;
76 ExternalASTSource() = default;
77 virtual ~ExternalASTSource();
79 /// \brief RAII class for safely pairing a StartedDeserializing call
80 /// with FinishedDeserializing.
82 ExternalASTSource *Source;
85 explicit Deserializing(ExternalASTSource *source) : Source(source) {
87 Source->StartedDeserializing();
91 Source->FinishedDeserializing();
95 /// \brief Get the current generation of this AST source. This number
96 /// is incremented each time the AST source lazily extends an existing
98 uint32_t getGeneration() const { return CurrentGeneration; }
100 /// \brief Resolve a declaration ID into a declaration, potentially
101 /// building a new declaration.
103 /// This method only needs to be implemented if the AST source ever
104 /// passes back decl sets as VisibleDeclaration objects.
106 /// The default implementation of this method is a no-op.
107 virtual Decl *GetExternalDecl(uint32_t ID);
109 /// \brief Resolve a selector ID into a selector.
111 /// This operation only needs to be implemented if the AST source
112 /// returns non-zero for GetNumKnownSelectors().
114 /// The default implementation of this method is a no-op.
115 virtual Selector GetExternalSelector(uint32_t ID);
117 /// \brief Returns the number of selectors known to the external AST
120 /// The default implementation of this method is a no-op.
121 virtual uint32_t GetNumExternalSelectors();
123 /// \brief Resolve the offset of a statement in the decl stream into
126 /// This operation is meant to be used via a LazyOffsetPtr. It only
127 /// needs to be implemented if the AST source uses methods like
128 /// FunctionDecl::setLazyBody when building decls.
130 /// The default implementation of this method is a no-op.
131 virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
133 /// \brief Resolve the offset of a set of C++ constructor initializers in
134 /// the decl stream into an array of initializers.
136 /// The default implementation of this method is a no-op.
137 virtual CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset);
139 /// \brief Resolve the offset of a set of C++ base specifiers in the decl
140 /// stream into an array of specifiers.
142 /// The default implementation of this method is a no-op.
143 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
145 /// \brief Update an out-of-date identifier.
146 virtual void updateOutOfDateIdentifier(IdentifierInfo &II) {}
148 /// \brief Find all declarations with the given name in the given context,
149 /// and add them to the context by calling SetExternalVisibleDeclsForName
150 /// or SetNoExternalVisibleDeclsForName.
151 /// \return \c true if any declarations might have been found, \c false if
152 /// we definitely have no declarations with tbis name.
154 /// The default implementation of this method is a no-op returning \c false.
156 FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
158 /// \brief Ensures that the table of all visible declarations inside this
159 /// context is up to date.
161 /// The default implementation of this function is a no-op.
162 virtual void completeVisibleDeclsMap(const DeclContext *DC);
164 /// \brief Retrieve the module that corresponds to the given module ID.
165 virtual Module *getModule(unsigned ID) { return nullptr; }
167 /// Abstracts clang modules and precompiled header files and holds
168 /// everything needed to generate debug info for an imported module
170 class ASTSourceDescriptor {
171 StringRef PCHModuleName;
174 ASTFileSignature Signature;
175 const Module *ClangModule = nullptr;
178 ASTSourceDescriptor() = default;
179 ASTSourceDescriptor(StringRef Name, StringRef Path, StringRef ASTFile,
180 ASTFileSignature Signature)
181 : PCHModuleName(std::move(Name)), Path(std::move(Path)),
182 ASTFile(std::move(ASTFile)), Signature(Signature) {}
183 ASTSourceDescriptor(const Module &M);
185 std::string getModuleName() const;
186 StringRef getPath() const { return Path; }
187 StringRef getASTFile() const { return ASTFile; }
188 ASTFileSignature getSignature() const { return Signature; }
189 const Module *getModuleOrNull() const { return ClangModule; }
192 /// Return a descriptor for the corresponding module, if one exists.
193 virtual llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID);
195 enum ExtKind { EK_Always, EK_Never, EK_ReplyHazy };
197 virtual ExtKind hasExternalDefinitions(const Decl *D);
199 /// \brief Finds all declarations lexically contained within the given
200 /// DeclContext, after applying an optional filter predicate.
202 /// \param IsKindWeWant a predicate function that returns true if the passed
203 /// declaration kind is one we are looking for.
205 /// The default implementation of this method is a no-op.
207 FindExternalLexicalDecls(const DeclContext *DC,
208 llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
209 SmallVectorImpl<Decl *> &Result);
211 /// \brief Finds all declarations lexically contained within the given
213 void FindExternalLexicalDecls(const DeclContext *DC,
214 SmallVectorImpl<Decl *> &Result) {
215 FindExternalLexicalDecls(DC, [](Decl::Kind) { return true; }, Result);
218 /// \brief Get the decls that are contained in a file in the Offset/Length
219 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
221 virtual void FindFileRegionDecls(FileID File, unsigned Offset,
223 SmallVectorImpl<Decl *> &Decls);
225 /// \brief Gives the external AST source an opportunity to complete
226 /// the redeclaration chain for a declaration. Called each time we
227 /// need the most recent declaration of a declaration after the
228 /// generation count is incremented.
229 virtual void CompleteRedeclChain(const Decl *D);
231 /// \brief Gives the external AST source an opportunity to complete
232 /// an incomplete type.
233 virtual void CompleteType(TagDecl *Tag);
235 /// \brief Gives the external AST source an opportunity to complete an
236 /// incomplete Objective-C class.
238 /// This routine will only be invoked if the "externally completed" bit is
239 /// set on the ObjCInterfaceDecl via the function
240 /// \c ObjCInterfaceDecl::setExternallyCompleted().
241 virtual void CompleteType(ObjCInterfaceDecl *Class);
243 /// \brief Loads comment ranges.
244 virtual void ReadComments();
246 /// \brief Notify ExternalASTSource that we started deserialization of
247 /// a decl or type so until FinishedDeserializing is called there may be
248 /// decls that are initializing. Must be paired with FinishedDeserializing.
250 /// The default implementation of this method is a no-op.
251 virtual void StartedDeserializing();
253 /// \brief Notify ExternalASTSource that we finished the deserialization of
254 /// a decl or type. Must be paired with StartedDeserializing.
256 /// The default implementation of this method is a no-op.
257 virtual void FinishedDeserializing();
259 /// \brief Function that will be invoked when we begin parsing a new
260 /// translation unit involving this external AST source.
262 /// The default implementation of this method is a no-op.
263 virtual void StartTranslationUnit(ASTConsumer *Consumer);
265 /// \brief Print any statistics that have been gathered regarding
266 /// the external AST source.
268 /// The default implementation of this method is a no-op.
269 virtual void PrintStats();
271 /// \brief Perform layout on the given record.
273 /// This routine allows the external AST source to provide an specific
274 /// layout for a record, overriding the layout that would normally be
275 /// constructed. It is intended for clients who receive specific layout
276 /// details rather than source code (such as LLDB). The client is expected
277 /// to fill in the field offsets, base offsets, virtual base offsets, and
278 /// complete object size.
280 /// \param Record The record whose layout is being requested.
282 /// \param Size The final size of the record, in bits.
284 /// \param Alignment The final alignment of the record, in bits.
286 /// \param FieldOffsets The offset of each of the fields within the record,
287 /// expressed in bits. All of the fields must be provided with offsets.
289 /// \param BaseOffsets The offset of each of the direct, non-virtual base
290 /// classes. If any bases are not given offsets, the bases will be laid
291 /// out according to the ABI.
293 /// \param VirtualBaseOffsets The offset of each of the virtual base classes
294 /// (either direct or not). If any bases are not given offsets, the bases will be laid
295 /// out according to the ABI.
297 /// \returns true if the record layout was provided, false otherwise.
298 virtual bool layoutRecordType(
299 const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
300 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
301 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
302 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets);
304 //===--------------------------------------------------------------------===//
305 // Queries for performance analysis.
306 //===--------------------------------------------------------------------===//
308 struct MemoryBufferSizes {
312 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
313 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
316 /// Return the amount of memory used by memory buffers, breaking down
317 /// by heap-backed versus mmap'ed memory.
318 MemoryBufferSizes getMemoryBufferSizes() const {
319 MemoryBufferSizes sizes(0, 0);
320 getMemoryBufferSizes(sizes);
324 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
327 static DeclContextLookupResult
328 SetExternalVisibleDeclsForName(const DeclContext *DC,
329 DeclarationName Name,
330 ArrayRef<NamedDecl*> Decls);
332 static DeclContextLookupResult
333 SetNoExternalVisibleDeclsForName(const DeclContext *DC,
334 DeclarationName Name);
336 /// \brief Increment the current generation.
337 uint32_t incrementGeneration(ASTContext &C);
340 /// \brief A lazy pointer to an AST node (of base type T) that resides
341 /// within an external AST source.
343 /// The AST node is identified within the external AST source by a
344 /// 63-bit offset, and can be retrieved via an operation on the
345 /// external AST source itself.
346 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
347 struct LazyOffsetPtr {
348 /// \brief Either a pointer to an AST node or the offset within the
349 /// external AST source where the AST node can be found.
351 /// If the low bit is clear, a pointer to the AST node. If the low
352 /// bit is set, the upper 63 bits are the offset.
353 mutable uint64_t Ptr = 0;
356 LazyOffsetPtr() = default;
357 explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) {}
359 explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
360 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
365 LazyOffsetPtr &operator=(T *Ptr) {
366 this->Ptr = reinterpret_cast<uint64_t>(Ptr);
370 LazyOffsetPtr &operator=(uint64_t Offset) {
371 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
375 Ptr = (Offset << 1) | 0x01;
380 /// \brief Whether this pointer is non-NULL.
382 /// This operation does not require the AST node to be deserialized.
383 explicit operator bool() const { return Ptr != 0; }
385 /// \brief Whether this pointer is non-NULL.
387 /// This operation does not require the AST node to be deserialized.
388 bool isValid() const { return Ptr != 0; }
390 /// \brief Whether this pointer is currently stored as an offset.
391 bool isOffset() const { return Ptr & 0x01; }
393 /// \brief Retrieve the pointer to the AST node that this lazy pointer
395 /// \param Source the external AST source.
397 /// \returns a pointer to the AST node.
398 T* get(ExternalASTSource *Source) const {
401 "Cannot deserialize a lazy pointer without an AST source");
402 Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
404 return reinterpret_cast<T*>(Ptr);
408 /// \brief A lazy value (of type T) that is within an AST node of type Owner,
409 /// where the value might change in later generations of the external AST
411 template<typename Owner, typename T, void (ExternalASTSource::*Update)(Owner)>
412 struct LazyGenerationalUpdatePtr {
413 /// A cache of the value of this pointer, in the most recent generation in
414 /// which we queried it.
416 ExternalASTSource *ExternalSource;
417 uint32_t LastGeneration = 0;
420 LazyData(ExternalASTSource *Source, T Value)
421 : ExternalSource(Source), LastValue(Value) {}
424 // Our value is represented as simply T if there is no external AST source.
425 using ValueType = llvm::PointerUnion<T, LazyData*>;
428 LazyGenerationalUpdatePtr(ValueType V) : Value(V) {}
430 // Defined in ASTContext.h
431 static ValueType makeValue(const ASTContext &Ctx, T Value);
434 explicit LazyGenerationalUpdatePtr(const ASTContext &Ctx, T Value = T())
435 : Value(makeValue(Ctx, Value)) {}
437 /// Create a pointer that is not potentially updated by later generations of
438 /// the external AST source.
439 enum NotUpdatedTag { NotUpdated };
440 LazyGenerationalUpdatePtr(NotUpdatedTag, T Value = T())
443 /// Forcibly set this pointer (which must be lazy) as needing updates.
444 void markIncomplete() {
445 Value.template get<LazyData *>()->LastGeneration = 0;
448 /// Set the value of this pointer, in the current generation.
449 void set(T NewValue) {
450 if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>()) {
451 LazyVal->LastValue = NewValue;
457 /// Set the value of this pointer, for this and all future generations.
458 void setNotUpdated(T NewValue) { Value = NewValue; }
460 /// Get the value of this pointer, updating its owner if necessary.
462 if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>()) {
463 if (LazyVal->LastGeneration != LazyVal->ExternalSource->getGeneration()) {
464 LazyVal->LastGeneration = LazyVal->ExternalSource->getGeneration();
465 (LazyVal->ExternalSource->*Update)(O);
467 return LazyVal->LastValue;
469 return Value.template get<T>();
472 /// Get the most recently computed value of this pointer without updating it.
473 T getNotUpdated() const {
474 if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>())
475 return LazyVal->LastValue;
476 return Value.template get<T>();
479 void *getOpaqueValue() { return Value.getOpaqueValue(); }
480 static LazyGenerationalUpdatePtr getFromOpaqueValue(void *Ptr) {
481 return LazyGenerationalUpdatePtr(ValueType::getFromOpaqueValue(Ptr));
487 /// Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be
488 /// placed into a PointerUnion.
491 template<typename Owner, typename T,
492 void (clang::ExternalASTSource::*Update)(Owner)>
493 struct PointerLikeTypeTraits<
494 clang::LazyGenerationalUpdatePtr<Owner, T, Update>> {
495 using Ptr = clang::LazyGenerationalUpdatePtr<Owner, T, Update>;
497 static void *getAsVoidPointer(Ptr P) { return P.getOpaqueValue(); }
498 static Ptr getFromVoidPointer(void *P) { return Ptr::getFromOpaqueValue(P); }
501 NumLowBitsAvailable = PointerLikeTypeTraits<T>::NumLowBitsAvailable - 1
509 /// \brief Represents a lazily-loaded vector of data.
511 /// The lazily-loaded vector of data contains data that is partially loaded
512 /// from an external source and partially added by local translation. The
513 /// items loaded from the external source are loaded lazily, when needed for
514 /// iteration over the complete vector.
515 template<typename T, typename Source,
516 void (Source::*Loader)(SmallVectorImpl<T>&),
517 unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
519 SmallVector<T, LoadedStorage> Loaded;
520 SmallVector<T, LocalStorage> Local;
523 /// Iteration over the elements in the vector.
525 /// In a complete iteration, the iterator walks the range [-M, N),
526 /// where negative values are used to indicate elements
527 /// loaded from the external source while non-negative values are used to
528 /// indicate elements added via \c push_back().
529 /// However, to provide iteration in source order (for, e.g., chained
530 /// precompiled headers), dereferencing the iterator flips the negative
531 /// values (corresponding to loaded entities), so that position -M
532 /// corresponds to element 0 in the loaded entities vector, position -M+1
533 /// corresponds to element 1 in the loaded entities vector, etc. This
534 /// gives us a reasonably efficient, source-order walk.
536 /// We define this as a wrapping iterator around an int. The
537 /// iterator_adaptor_base class forwards the iterator methods to basic integer
540 : public llvm::iterator_adaptor_base<
541 iterator, int, std::random_access_iterator_tag, T, int, T *, T &> {
542 friend class LazyVector;
546 iterator(LazyVector *Self, int Position)
547 : iterator::iterator_adaptor_base(Position), Self(Self) {}
549 bool isLoaded() const { return this->I < 0; }
552 iterator() : iterator(nullptr, 0) {}
554 typename iterator::reference operator*() const {
556 return Self->Loaded.end()[this->I];
557 return Self->Local.begin()[this->I];
561 iterator begin(Source *source, bool LocalOnly = false) {
563 return iterator(this, 0);
566 (source->*Loader)(Loaded);
567 return iterator(this, -(int)Loaded.size());
571 return iterator(this, Local.size());
574 void push_back(const T& LocalValue) {
575 Local.push_back(LocalValue);
578 void erase(iterator From, iterator To) {
579 if (From.isLoaded() && To.isLoaded()) {
580 Loaded.erase(&*From, &*To);
584 if (From.isLoaded()) {
585 Loaded.erase(&*From, Loaded.end());
586 From = begin(nullptr, true);
589 Local.erase(&*From, &*To);
593 /// \brief A lazy pointer to a statement.
594 using LazyDeclStmtPtr =
595 LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>;
597 /// \brief A lazy pointer to a declaration.
599 LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>;
601 /// \brief A lazy pointer to a set of CXXCtorInitializers.
602 using LazyCXXCtorInitializersPtr =
603 LazyOffsetPtr<CXXCtorInitializer *, uint64_t,
604 &ExternalASTSource::GetExternalCXXCtorInitializers>;
606 /// \brief A lazy pointer to a set of CXXBaseSpecifiers.
607 using LazyCXXBaseSpecifiersPtr =
608 LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
609 &ExternalASTSource::GetExternalCXXBaseSpecifiers>;
613 #endif // LLVM_CLANG_AST_EXTERNALASTSOURCE_H