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 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_EXTERNALASTSOURCE_H
15 #define LLVM_CLANG_AST_EXTERNALASTSOURCE_H
17 #include "clang/AST/CharUnits.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/Basic/Module.h"
20 #include "llvm/ADT/DenseMap.h"
25 class CXXBaseSpecifier;
26 class CXXCtorInitializer;
27 class DeclarationName;
28 class ExternalSemaSource; // layering violation required for downcasting
37 /// \brief Abstract interface for external sources of AST nodes.
39 /// External AST sources provide AST nodes constructed from some
40 /// external source, such as a precompiled header. External AST
41 /// sources can resolve types and declarations from abstract IDs into
42 /// actual type and declaration nodes, and read parts of declaration
44 class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
45 /// Generation number for this external AST source. Must be increased
46 /// whenever we might have added new redeclarations for existing decls.
47 uint32_t CurrentGeneration;
49 /// \brief Whether this AST source also provides information for
50 /// semantic analysis.
53 friend class ExternalSemaSource;
56 ExternalASTSource() : CurrentGeneration(0), SemaSource(false) { }
58 virtual ~ExternalASTSource();
60 /// \brief RAII class for safely pairing a StartedDeserializing call
61 /// with FinishedDeserializing.
63 ExternalASTSource *Source;
65 explicit Deserializing(ExternalASTSource *source) : Source(source) {
67 Source->StartedDeserializing();
70 Source->FinishedDeserializing();
74 /// \brief Get the current generation of this AST source. This number
75 /// is incremented each time the AST source lazily extends an existing
77 uint32_t getGeneration() const { return CurrentGeneration; }
79 /// \brief Resolve a declaration ID into a declaration, potentially
80 /// building a new declaration.
82 /// This method only needs to be implemented if the AST source ever
83 /// passes back decl sets as VisibleDeclaration objects.
85 /// The default implementation of this method is a no-op.
86 virtual Decl *GetExternalDecl(uint32_t ID);
88 /// \brief Resolve a selector ID into a selector.
90 /// This operation only needs to be implemented if the AST source
91 /// returns non-zero for GetNumKnownSelectors().
93 /// The default implementation of this method is a no-op.
94 virtual Selector GetExternalSelector(uint32_t ID);
96 /// \brief Returns the number of selectors known to the external AST
99 /// The default implementation of this method is a no-op.
100 virtual uint32_t GetNumExternalSelectors();
102 /// \brief Resolve the offset of a statement in the decl stream into
105 /// This operation is meant to be used via a LazyOffsetPtr. It only
106 /// needs to be implemented if the AST source uses methods like
107 /// FunctionDecl::setLazyBody when building decls.
109 /// The default implementation of this method is a no-op.
110 virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
112 /// \brief Resolve the offset of a set of C++ constructor initializers in
113 /// the decl stream into an array of initializers.
115 /// The default implementation of this method is a no-op.
116 virtual CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset);
118 /// \brief Resolve the offset of a set of C++ base specifiers in the decl
119 /// stream into an array of specifiers.
121 /// The default implementation of this method is a no-op.
122 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
124 /// \brief Update an out-of-date identifier.
125 virtual void updateOutOfDateIdentifier(IdentifierInfo &II) { }
127 /// \brief Find all declarations with the given name in the given context,
128 /// and add them to the context by calling SetExternalVisibleDeclsForName
129 /// or SetNoExternalVisibleDeclsForName.
130 /// \return \c true if any declarations might have been found, \c false if
131 /// we definitely have no declarations with tbis name.
133 /// The default implementation of this method is a no-op returning \c false.
135 FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
137 /// \brief Ensures that the table of all visible declarations inside this
138 /// context is up to date.
140 /// The default implementation of this function is a no-op.
141 virtual void completeVisibleDeclsMap(const DeclContext *DC);
143 /// \brief Retrieve the module that corresponds to the given module ID.
144 virtual Module *getModule(unsigned ID) { return nullptr; }
146 /// Abstracts clang modules and precompiled header files and holds
147 /// everything needed to generate debug info for an imported module
149 class ASTSourceDescriptor {
150 StringRef PCHModuleName;
153 ASTFileSignature Signature;
154 const Module *ClangModule = nullptr;
157 ASTSourceDescriptor(){};
158 ASTSourceDescriptor(StringRef Name, StringRef Path, StringRef ASTFile,
159 ASTFileSignature Signature)
160 : PCHModuleName(std::move(Name)), Path(std::move(Path)),
161 ASTFile(std::move(ASTFile)), Signature(Signature){};
162 ASTSourceDescriptor(const Module &M);
163 std::string getModuleName() const;
164 StringRef getPath() const { return Path; }
165 StringRef getASTFile() const { return ASTFile; }
166 ASTFileSignature getSignature() const { return Signature; }
167 const Module *getModuleOrNull() const { return ClangModule; }
170 /// Return a descriptor for the corresponding module, if one exists.
171 virtual llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID);
173 enum ExtKind { EK_Always, EK_Never, EK_ReplyHazy };
175 virtual ExtKind hasExternalDefinitions(const Decl *D);
177 /// \brief Finds all declarations lexically contained within the given
178 /// DeclContext, after applying an optional filter predicate.
180 /// \param IsKindWeWant a predicate function that returns true if the passed
181 /// declaration kind is one we are looking for.
183 /// The default implementation of this method is a no-op.
185 FindExternalLexicalDecls(const DeclContext *DC,
186 llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
187 SmallVectorImpl<Decl *> &Result);
189 /// \brief Finds all declarations lexically contained within the given
191 void FindExternalLexicalDecls(const DeclContext *DC,
192 SmallVectorImpl<Decl *> &Result) {
193 FindExternalLexicalDecls(DC, [](Decl::Kind) { return true; }, Result);
196 /// \brief Get the decls that are contained in a file in the Offset/Length
197 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
199 virtual void FindFileRegionDecls(FileID File, unsigned Offset,
201 SmallVectorImpl<Decl *> &Decls);
203 /// \brief Gives the external AST source an opportunity to complete
204 /// the redeclaration chain for a declaration. Called each time we
205 /// need the most recent declaration of a declaration after the
206 /// generation count is incremented.
207 virtual void CompleteRedeclChain(const Decl *D);
209 /// \brief Gives the external AST source an opportunity to complete
210 /// an incomplete type.
211 virtual void CompleteType(TagDecl *Tag);
213 /// \brief Gives the external AST source an opportunity to complete an
214 /// incomplete Objective-C class.
216 /// This routine will only be invoked if the "externally completed" bit is
217 /// set on the ObjCInterfaceDecl via the function
218 /// \c ObjCInterfaceDecl::setExternallyCompleted().
219 virtual void CompleteType(ObjCInterfaceDecl *Class);
221 /// \brief Loads comment ranges.
222 virtual void ReadComments();
224 /// \brief Notify ExternalASTSource that we started deserialization of
225 /// a decl or type so until FinishedDeserializing is called there may be
226 /// decls that are initializing. Must be paired with FinishedDeserializing.
228 /// The default implementation of this method is a no-op.
229 virtual void StartedDeserializing();
231 /// \brief Notify ExternalASTSource that we finished the deserialization of
232 /// a decl or type. Must be paired with StartedDeserializing.
234 /// The default implementation of this method is a no-op.
235 virtual void FinishedDeserializing();
237 /// \brief Function that will be invoked when we begin parsing a new
238 /// translation unit involving this external AST source.
240 /// The default implementation of this method is a no-op.
241 virtual void StartTranslationUnit(ASTConsumer *Consumer);
243 /// \brief Print any statistics that have been gathered regarding
244 /// the external AST source.
246 /// The default implementation of this method is a no-op.
247 virtual void PrintStats();
250 /// \brief Perform layout on the given record.
252 /// This routine allows the external AST source to provide an specific
253 /// layout for a record, overriding the layout that would normally be
254 /// constructed. It is intended for clients who receive specific layout
255 /// details rather than source code (such as LLDB). The client is expected
256 /// to fill in the field offsets, base offsets, virtual base offsets, and
257 /// complete object size.
259 /// \param Record The record whose layout is being requested.
261 /// \param Size The final size of the record, in bits.
263 /// \param Alignment The final alignment of the record, in bits.
265 /// \param FieldOffsets The offset of each of the fields within the record,
266 /// expressed in bits. All of the fields must be provided with offsets.
268 /// \param BaseOffsets The offset of each of the direct, non-virtual base
269 /// classes. If any bases are not given offsets, the bases will be laid
270 /// out according to the ABI.
272 /// \param VirtualBaseOffsets The offset of each of the virtual base classes
273 /// (either direct or not). If any bases are not given offsets, the bases will be laid
274 /// out according to the ABI.
276 /// \returns true if the record layout was provided, false otherwise.
277 virtual bool layoutRecordType(
278 const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
279 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
280 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
281 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets);
283 //===--------------------------------------------------------------------===//
284 // Queries for performance analysis.
285 //===--------------------------------------------------------------------===//
287 struct MemoryBufferSizes {
291 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
292 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
295 /// Return the amount of memory used by memory buffers, breaking down
296 /// by heap-backed versus mmap'ed memory.
297 MemoryBufferSizes getMemoryBufferSizes() const {
298 MemoryBufferSizes sizes(0, 0);
299 getMemoryBufferSizes(sizes);
303 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
306 static DeclContextLookupResult
307 SetExternalVisibleDeclsForName(const DeclContext *DC,
308 DeclarationName Name,
309 ArrayRef<NamedDecl*> Decls);
311 static DeclContextLookupResult
312 SetNoExternalVisibleDeclsForName(const DeclContext *DC,
313 DeclarationName Name);
315 /// \brief Increment the current generation.
316 uint32_t incrementGeneration(ASTContext &C);
319 /// \brief A lazy pointer to an AST node (of base type T) that resides
320 /// within an external AST source.
322 /// The AST node is identified within the external AST source by a
323 /// 63-bit offset, and can be retrieved via an operation on the
324 /// external AST source itself.
325 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
326 struct LazyOffsetPtr {
327 /// \brief Either a pointer to an AST node or the offset within the
328 /// external AST source where the AST node can be found.
330 /// If the low bit is clear, a pointer to the AST node. If the low
331 /// bit is set, the upper 63 bits are the offset.
332 mutable uint64_t Ptr;
335 LazyOffsetPtr() : Ptr(0) { }
337 explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
338 explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
339 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
344 LazyOffsetPtr &operator=(T *Ptr) {
345 this->Ptr = reinterpret_cast<uint64_t>(Ptr);
349 LazyOffsetPtr &operator=(uint64_t Offset) {
350 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
354 Ptr = (Offset << 1) | 0x01;
359 /// \brief Whether this pointer is non-NULL.
361 /// This operation does not require the AST node to be deserialized.
362 explicit operator bool() const { return Ptr != 0; }
364 /// \brief Whether this pointer is non-NULL.
366 /// This operation does not require the AST node to be deserialized.
367 bool isValid() const { return Ptr != 0; }
369 /// \brief Whether this pointer is currently stored as an offset.
370 bool isOffset() const { return Ptr & 0x01; }
372 /// \brief Retrieve the pointer to the AST node that this lazy pointer
374 /// \param Source the external AST source.
376 /// \returns a pointer to the AST node.
377 T* get(ExternalASTSource *Source) const {
380 "Cannot deserialize a lazy pointer without an AST source");
381 Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
383 return reinterpret_cast<T*>(Ptr);
387 /// \brief A lazy value (of type T) that is within an AST node of type Owner,
388 /// where the value might change in later generations of the external AST
390 template<typename Owner, typename T, void (ExternalASTSource::*Update)(Owner)>
391 struct LazyGenerationalUpdatePtr {
392 /// A cache of the value of this pointer, in the most recent generation in
393 /// which we queried it.
395 LazyData(ExternalASTSource *Source, T Value)
396 : ExternalSource(Source), LastGeneration(0), LastValue(Value) {}
397 ExternalASTSource *ExternalSource;
398 uint32_t LastGeneration;
402 // Our value is represented as simply T if there is no external AST source.
403 typedef llvm::PointerUnion<T, LazyData*> ValueType;
406 LazyGenerationalUpdatePtr(ValueType V) : Value(V) {}
408 // Defined in ASTContext.h
409 static ValueType makeValue(const ASTContext &Ctx, T Value);
412 explicit LazyGenerationalUpdatePtr(const ASTContext &Ctx, T Value = T())
413 : Value(makeValue(Ctx, Value)) {}
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())
421 /// Forcibly set this pointer (which must be lazy) as needing updates.
422 void markIncomplete() {
423 Value.template get<LazyData *>()->LastGeneration = 0;
426 /// Set the value of this pointer, in the current generation.
427 void set(T NewValue) {
428 if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>()) {
429 LazyVal->LastValue = NewValue;
435 /// Set the value of this pointer, for this and all future generations.
436 void setNotUpdated(T NewValue) { Value = NewValue; }
438 /// Get the value of this pointer, updating its owner if necessary.
440 if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>()) {
441 if (LazyVal->LastGeneration != LazyVal->ExternalSource->getGeneration()) {
442 LazyVal->LastGeneration = LazyVal->ExternalSource->getGeneration();
443 (LazyVal->ExternalSource->*Update)(O);
445 return LazyVal->LastValue;
447 return Value.template get<T>();
450 /// Get the most recently computed value of this pointer without updating it.
451 T getNotUpdated() const {
452 if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>())
453 return LazyVal->LastValue;
454 return Value.template get<T>();
457 void *getOpaqueValue() { return Value.getOpaqueValue(); }
458 static LazyGenerationalUpdatePtr getFromOpaqueValue(void *Ptr) {
459 return LazyGenerationalUpdatePtr(ValueType::getFromOpaqueValue(Ptr));
462 } // end namespace clang
464 /// Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be
465 /// placed into a PointerUnion.
467 template<typename Owner, typename T,
468 void (clang::ExternalASTSource::*Update)(Owner)>
469 struct PointerLikeTypeTraits<
470 clang::LazyGenerationalUpdatePtr<Owner, T, Update>> {
471 typedef clang::LazyGenerationalUpdatePtr<Owner, T, Update> Ptr;
472 static void *getAsVoidPointer(Ptr P) { return P.getOpaqueValue(); }
473 static Ptr getFromVoidPointer(void *P) { return Ptr::getFromOpaqueValue(P); }
475 NumLowBitsAvailable = PointerLikeTypeTraits<T>::NumLowBitsAvailable - 1
481 /// \brief Represents a lazily-loaded vector of data.
483 /// The lazily-loaded vector of data contains data that is partially loaded
484 /// from an external source and partially added by local translation. The
485 /// items loaded from the external source are loaded lazily, when needed for
486 /// iteration over the complete vector.
487 template<typename T, typename Source,
488 void (Source::*Loader)(SmallVectorImpl<T>&),
489 unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
491 SmallVector<T, LoadedStorage> Loaded;
492 SmallVector<T, LocalStorage> Local;
495 /// Iteration over the elements in the vector.
497 /// In a complete iteration, the iterator walks the range [-M, N),
498 /// where negative values are used to indicate elements
499 /// loaded from the external source while non-negative values are used to
500 /// indicate elements added via \c push_back().
501 /// However, to provide iteration in source order (for, e.g., chained
502 /// precompiled headers), dereferencing the iterator flips the negative
503 /// values (corresponding to loaded entities), so that position -M
504 /// corresponds to element 0 in the loaded entities vector, position -M+1
505 /// corresponds to element 1 in the loaded entities vector, etc. This
506 /// gives us a reasonably efficient, source-order walk.
508 /// We define this as a wrapping iterator around an int. The
509 /// iterator_adaptor_base class forwards the iterator methods to basic integer
512 : public llvm::iterator_adaptor_base<
513 iterator, int, std::random_access_iterator_tag, T, int, T *, T &> {
516 iterator(LazyVector *Self, int Position)
517 : iterator::iterator_adaptor_base(Position), Self(Self) {}
519 bool isLoaded() const { return this->I < 0; }
520 friend class LazyVector;
523 iterator() : iterator(nullptr, 0) {}
525 typename iterator::reference operator*() const {
527 return Self->Loaded.end()[this->I];
528 return Self->Local.begin()[this->I];
532 iterator begin(Source *source, bool LocalOnly = false) {
534 return iterator(this, 0);
537 (source->*Loader)(Loaded);
538 return iterator(this, -(int)Loaded.size());
542 return iterator(this, Local.size());
545 void push_back(const T& LocalValue) {
546 Local.push_back(LocalValue);
549 void erase(iterator From, iterator To) {
550 if (From.isLoaded() && To.isLoaded()) {
551 Loaded.erase(&*From, &*To);
555 if (From.isLoaded()) {
556 Loaded.erase(&*From, Loaded.end());
557 From = begin(nullptr, true);
560 Local.erase(&*From, &*To);
564 /// \brief A lazy pointer to a statement.
565 typedef LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>
568 /// \brief A lazy pointer to a declaration.
569 typedef LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>
572 /// \brief A lazy pointer to a set of CXXCtorInitializers.
573 typedef LazyOffsetPtr<CXXCtorInitializer *, uint64_t,
574 &ExternalASTSource::GetExternalCXXCtorInitializers>
575 LazyCXXCtorInitializersPtr;
577 /// \brief A lazy pointer to a set of CXXBaseSpecifiers.
578 typedef LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
579 &ExternalASTSource::GetExternalCXXBaseSpecifiers>
580 LazyCXXBaseSpecifiersPtr;
582 } // end namespace clang