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 "llvm/ADT/DenseMap.h"
24 class CXXBaseSpecifier;
25 class DeclarationName;
26 class ExternalSemaSource; // layering violation required for downcasting
35 /// \brief Enumeration describing the result of loading information from
36 /// an external source.
37 enum ExternalLoadResult {
38 /// \brief Loading the external information has succeeded.
41 /// \brief Loading the external information has failed.
44 /// \brief The external information has already been loaded, and therefore
45 /// no additional processing is required.
49 /// \brief Abstract interface for external sources of AST nodes.
51 /// External AST sources provide AST nodes constructed from some
52 /// external source, such as a precompiled header. External AST
53 /// sources can resolve types and declarations from abstract IDs into
54 /// actual type and declaration nodes, and read parts of declaration
56 class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
57 /// Generation number for this external AST source. Must be increased
58 /// whenever we might have added new redeclarations for existing decls.
59 uint32_t CurrentGeneration;
61 /// \brief Whether this AST source also provides information for
62 /// semantic analysis.
65 friend class ExternalSemaSource;
68 ExternalASTSource() : CurrentGeneration(0), SemaSource(false) { }
70 virtual ~ExternalASTSource();
72 /// \brief RAII class for safely pairing a StartedDeserializing call
73 /// with FinishedDeserializing.
75 ExternalASTSource *Source;
77 explicit Deserializing(ExternalASTSource *source) : Source(source) {
79 Source->StartedDeserializing();
82 Source->FinishedDeserializing();
86 /// \brief Get the current generation of this AST source. This number
87 /// is incremented each time the AST source lazily extends an existing
89 uint32_t getGeneration() const { return CurrentGeneration; }
91 /// \brief Resolve a declaration ID into a declaration, potentially
92 /// building a new declaration.
94 /// This method only needs to be implemented if the AST source ever
95 /// passes back decl sets as VisibleDeclaration objects.
97 /// The default implementation of this method is a no-op.
98 virtual Decl *GetExternalDecl(uint32_t ID);
100 /// \brief Resolve a selector ID into a selector.
102 /// This operation only needs to be implemented if the AST source
103 /// returns non-zero for GetNumKnownSelectors().
105 /// The default implementation of this method is a no-op.
106 virtual Selector GetExternalSelector(uint32_t ID);
108 /// \brief Returns the number of selectors known to the external AST
111 /// The default implementation of this method is a no-op.
112 virtual uint32_t GetNumExternalSelectors();
114 /// \brief Resolve the offset of a statement in the decl stream into
117 /// This operation is meant to be used via a LazyOffsetPtr. It only
118 /// needs to be implemented if the AST source uses methods like
119 /// FunctionDecl::setLazyBody when building decls.
121 /// The default implementation of this method is a no-op.
122 virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
124 /// \brief Resolve the offset of a set of C++ base specifiers in the decl
125 /// stream into an array of specifiers.
127 /// The default implementation of this method is a no-op.
128 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
130 /// \brief Update an out-of-date identifier.
131 virtual void updateOutOfDateIdentifier(IdentifierInfo &II) { }
133 /// \brief Find all declarations with the given name in the given context,
134 /// and add them to the context by calling SetExternalVisibleDeclsForName
135 /// or SetNoExternalVisibleDeclsForName.
136 /// \return \c true if any declarations might have been found, \c false if
137 /// we definitely have no declarations with tbis name.
139 /// The default implementation of this method is a no-op returning \c false.
141 FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
143 /// \brief Ensures that the table of all visible declarations inside this
144 /// context is up to date.
146 /// The default implementation of this function is a no-op.
147 virtual void completeVisibleDeclsMap(const DeclContext *DC);
149 /// \brief Retrieve the module that corresponds to the given module ID.
150 virtual Module *getModule(unsigned ID) { return nullptr; }
152 /// \brief Finds all declarations lexically contained within the given
153 /// DeclContext, after applying an optional filter predicate.
155 /// \param isKindWeWant a predicate function that returns true if the passed
156 /// declaration kind is one we are looking for. If NULL, all declarations
159 /// \return an indication of whether the load succeeded or failed.
161 /// The default implementation of this method is a no-op.
162 virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
163 bool (*isKindWeWant)(Decl::Kind),
164 SmallVectorImpl<Decl*> &Result);
166 /// \brief Finds all declarations lexically contained within the given
169 /// \return true if an error occurred
170 ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
171 SmallVectorImpl<Decl*> &Result) {
172 return FindExternalLexicalDecls(DC, nullptr, Result);
175 template <typename DeclTy>
176 ExternalLoadResult FindExternalLexicalDeclsBy(const DeclContext *DC,
177 SmallVectorImpl<Decl*> &Result) {
178 return FindExternalLexicalDecls(DC, DeclTy::classofKind, Result);
181 /// \brief Get the decls that are contained in a file in the Offset/Length
182 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
184 virtual void FindFileRegionDecls(FileID File, unsigned Offset,
186 SmallVectorImpl<Decl *> &Decls);
188 /// \brief Gives the external AST source an opportunity to complete
189 /// the redeclaration chain for a declaration. Called each time we
190 /// need the most recent declaration of a declaration after the
191 /// generation count is incremented.
192 virtual void CompleteRedeclChain(const Decl *D);
194 /// \brief Gives the external AST source an opportunity to complete
195 /// an incomplete type.
196 virtual void CompleteType(TagDecl *Tag);
198 /// \brief Gives the external AST source an opportunity to complete an
199 /// incomplete Objective-C class.
201 /// This routine will only be invoked if the "externally completed" bit is
202 /// set on the ObjCInterfaceDecl via the function
203 /// \c ObjCInterfaceDecl::setExternallyCompleted().
204 virtual void CompleteType(ObjCInterfaceDecl *Class);
206 /// \brief Loads comment ranges.
207 virtual void ReadComments();
209 /// \brief Notify ExternalASTSource that we started deserialization of
210 /// a decl or type so until FinishedDeserializing is called there may be
211 /// decls that are initializing. Must be paired with FinishedDeserializing.
213 /// The default implementation of this method is a no-op.
214 virtual void StartedDeserializing();
216 /// \brief Notify ExternalASTSource that we finished the deserialization of
217 /// a decl or type. Must be paired with StartedDeserializing.
219 /// The default implementation of this method is a no-op.
220 virtual void FinishedDeserializing();
222 /// \brief Function that will be invoked when we begin parsing a new
223 /// translation unit involving this external AST source.
225 /// The default implementation of this method is a no-op.
226 virtual void StartTranslationUnit(ASTConsumer *Consumer);
228 /// \brief Print any statistics that have been gathered regarding
229 /// the external AST source.
231 /// The default implementation of this method is a no-op.
232 virtual void PrintStats();
235 /// \brief Perform layout on the given record.
237 /// This routine allows the external AST source to provide an specific
238 /// layout for a record, overriding the layout that would normally be
239 /// constructed. It is intended for clients who receive specific layout
240 /// details rather than source code (such as LLDB). The client is expected
241 /// to fill in the field offsets, base offsets, virtual base offsets, and
242 /// complete object size.
244 /// \param Record The record whose layout is being requested.
246 /// \param Size The final size of the record, in bits.
248 /// \param Alignment The final alignment of the record, in bits.
250 /// \param FieldOffsets The offset of each of the fields within the record,
251 /// expressed in bits. All of the fields must be provided with offsets.
253 /// \param BaseOffsets The offset of each of the direct, non-virtual base
254 /// classes. If any bases are not given offsets, the bases will be laid
255 /// out according to the ABI.
257 /// \param VirtualBaseOffsets The offset of each of the virtual base classes
258 /// (either direct or not). If any bases are not given offsets, the bases will be laid
259 /// out according to the ABI.
261 /// \returns true if the record layout was provided, false otherwise.
262 virtual bool layoutRecordType(
263 const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
264 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
265 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
266 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets);
268 //===--------------------------------------------------------------------===//
269 // Queries for performance analysis.
270 //===--------------------------------------------------------------------===//
272 struct MemoryBufferSizes {
276 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
277 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
280 /// Return the amount of memory used by memory buffers, breaking down
281 /// by heap-backed versus mmap'ed memory.
282 MemoryBufferSizes getMemoryBufferSizes() const {
283 MemoryBufferSizes sizes(0, 0);
284 getMemoryBufferSizes(sizes);
288 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
291 static DeclContextLookupResult
292 SetExternalVisibleDeclsForName(const DeclContext *DC,
293 DeclarationName Name,
294 ArrayRef<NamedDecl*> Decls);
296 static DeclContextLookupResult
297 SetNoExternalVisibleDeclsForName(const DeclContext *DC,
298 DeclarationName Name);
300 /// \brief Increment the current generation.
301 uint32_t incrementGeneration(ASTContext &C);
304 /// \brief A lazy pointer to an AST node (of base type T) that resides
305 /// within an external AST source.
307 /// The AST node is identified within the external AST source by a
308 /// 63-bit offset, and can be retrieved via an operation on the
309 /// external AST source itself.
310 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
311 struct LazyOffsetPtr {
312 /// \brief Either a pointer to an AST node or the offset within the
313 /// external AST source where the AST node can be found.
315 /// If the low bit is clear, a pointer to the AST node. If the low
316 /// bit is set, the upper 63 bits are the offset.
317 mutable uint64_t Ptr;
320 LazyOffsetPtr() : Ptr(0) { }
322 explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
323 explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
324 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
329 LazyOffsetPtr &operator=(T *Ptr) {
330 this->Ptr = reinterpret_cast<uint64_t>(Ptr);
334 LazyOffsetPtr &operator=(uint64_t Offset) {
335 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
339 Ptr = (Offset << 1) | 0x01;
344 /// \brief Whether this pointer is non-NULL.
346 /// This operation does not require the AST node to be deserialized.
347 LLVM_EXPLICIT operator bool() const { return Ptr != 0; }
349 /// \brief Whether this pointer is non-NULL.
351 /// This operation does not require the AST node to be deserialized.
352 bool isValid() const { return Ptr != 0; }
354 /// \brief Whether this pointer is currently stored as an offset.
355 bool isOffset() const { return Ptr & 0x01; }
357 /// \brief Retrieve the pointer to the AST node that this lazy pointer
359 /// \param Source the external AST source.
361 /// \returns a pointer to the AST node.
362 T* get(ExternalASTSource *Source) const {
365 "Cannot deserialize a lazy pointer without an AST source");
366 Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
368 return reinterpret_cast<T*>(Ptr);
372 /// \brief A lazy value (of type T) that is within an AST node of type Owner,
373 /// where the value might change in later generations of the external AST
375 template<typename Owner, typename T, void (ExternalASTSource::*Update)(Owner)>
376 struct LazyGenerationalUpdatePtr {
377 /// A cache of the value of this pointer, in the most recent generation in
378 /// which we queried it.
380 LazyData(ExternalASTSource *Source, T Value)
381 : ExternalSource(Source), LastGeneration(0), LastValue(Value) {}
382 ExternalASTSource *ExternalSource;
383 uint32_t LastGeneration;
387 // Our value is represented as simply T if there is no external AST source.
388 typedef llvm::PointerUnion<T, LazyData*> ValueType;
391 LazyGenerationalUpdatePtr(ValueType V) : Value(V) {}
393 // Defined in ASTContext.h
394 static ValueType makeValue(const ASTContext &Ctx, T Value);
397 explicit LazyGenerationalUpdatePtr(const ASTContext &Ctx, T Value = T())
398 : Value(makeValue(Ctx, Value)) {}
400 /// Create a pointer that is not potentially updated by later generations of
401 /// the external AST source.
402 enum NotUpdatedTag { NotUpdated };
403 LazyGenerationalUpdatePtr(NotUpdatedTag, T Value = T())
406 /// Forcibly set this pointer (which must be lazy) as needing updates.
407 void markIncomplete() {
408 Value.template get<LazyData *>()->LastGeneration = 0;
411 /// Set the value of this pointer, in the current generation.
412 void set(T NewValue) {
413 if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>()) {
414 LazyVal->LastValue = NewValue;
420 /// Set the value of this pointer, for this and all future generations.
421 void setNotUpdated(T NewValue) { Value = NewValue; }
423 /// Get the value of this pointer, updating its owner if necessary.
425 if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>()) {
426 if (LazyVal->LastGeneration != LazyVal->ExternalSource->getGeneration()) {
427 LazyVal->LastGeneration = LazyVal->ExternalSource->getGeneration();
428 (LazyVal->ExternalSource->*Update)(O);
430 return LazyVal->LastValue;
432 return Value.template get<T>();
435 /// Get the most recently computed value of this pointer without updating it.
436 T getNotUpdated() const {
437 if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>())
438 return LazyVal->LastValue;
439 return Value.template get<T>();
442 void *getOpaqueValue() { return Value.getOpaqueValue(); }
443 static LazyGenerationalUpdatePtr getFromOpaqueValue(void *Ptr) {
444 return LazyGenerationalUpdatePtr(ValueType::getFromOpaqueValue(Ptr));
447 } // end namespace clang
449 /// Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be
450 /// placed into a PointerUnion.
452 template<typename Owner, typename T,
453 void (clang::ExternalASTSource::*Update)(Owner)>
454 struct PointerLikeTypeTraits<
455 clang::LazyGenerationalUpdatePtr<Owner, T, Update>> {
456 typedef clang::LazyGenerationalUpdatePtr<Owner, T, Update> Ptr;
457 static void *getAsVoidPointer(Ptr P) { return P.getOpaqueValue(); }
458 static Ptr getFromVoidPointer(void *P) { return Ptr::getFromOpaqueValue(P); }
460 NumLowBitsAvailable = PointerLikeTypeTraits<T>::NumLowBitsAvailable - 1
466 /// \brief Represents a lazily-loaded vector of data.
468 /// The lazily-loaded vector of data contains data that is partially loaded
469 /// from an external source and partially added by local translation. The
470 /// items loaded from the external source are loaded lazily, when needed for
471 /// iteration over the complete vector.
472 template<typename T, typename Source,
473 void (Source::*Loader)(SmallVectorImpl<T>&),
474 unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
476 SmallVector<T, LoadedStorage> Loaded;
477 SmallVector<T, LocalStorage> Local;
480 // Iteration over the elements in the vector.
484 /// \brief Position within the vector..
486 /// In a complete iteration, the Position field walks the range [-M, N),
487 /// where negative values are used to indicate elements
488 /// loaded from the external source while non-negative values are used to
489 /// indicate elements added via \c push_back().
490 /// However, to provide iteration in source order (for, e.g., chained
491 /// precompiled headers), dereferencing the iterator flips the negative
492 /// values (corresponding to loaded entities), so that position -M
493 /// corresponds to element 0 in the loaded entities vector, position -M+1
494 /// corresponds to element 1 in the loaded entities vector, etc. This
495 /// gives us a reasonably efficient, source-order walk.
498 friend class LazyVector;
501 typedef T value_type;
502 typedef value_type& reference;
503 typedef value_type* pointer;
504 typedef std::random_access_iterator_tag iterator_category;
505 typedef int difference_type;
507 iterator() : Self(0), Position(0) { }
509 iterator(LazyVector *Self, int Position)
510 : Self(Self), Position(Position) { }
512 reference operator*() const {
514 return Self->Loaded.end()[Position];
515 return Self->Local[Position];
518 pointer operator->() const {
520 return &Self->Loaded.end()[Position];
522 return &Self->Local[Position];
525 reference operator[](difference_type D) {
529 iterator &operator++() {
534 iterator operator++(int) {
535 iterator Prev(*this);
540 iterator &operator--() {
545 iterator operator--(int) {
546 iterator Prev(*this);
551 friend bool operator==(const iterator &X, const iterator &Y) {
552 return X.Position == Y.Position;
555 friend bool operator!=(const iterator &X, const iterator &Y) {
556 return X.Position != Y.Position;
559 friend bool operator<(const iterator &X, const iterator &Y) {
560 return X.Position < Y.Position;
563 friend bool operator>(const iterator &X, const iterator &Y) {
564 return X.Position > Y.Position;
567 friend bool operator<=(const iterator &X, const iterator &Y) {
568 return X.Position < Y.Position;
571 friend bool operator>=(const iterator &X, const iterator &Y) {
572 return X.Position > Y.Position;
575 friend iterator& operator+=(iterator &X, difference_type D) {
580 friend iterator& operator-=(iterator &X, difference_type D) {
585 friend iterator operator+(iterator X, difference_type D) {
590 friend iterator operator+(difference_type D, iterator X) {
595 friend difference_type operator-(const iterator &X, const iterator &Y) {
596 return X.Position - Y.Position;
599 friend iterator operator-(iterator X, difference_type D) {
604 friend class iterator;
606 iterator begin(Source *source, bool LocalOnly = false) {
608 return iterator(this, 0);
611 (source->*Loader)(Loaded);
612 return iterator(this, -(int)Loaded.size());
616 return iterator(this, Local.size());
619 void push_back(const T& LocalValue) {
620 Local.push_back(LocalValue);
623 void erase(iterator From, iterator To) {
624 if (From.Position < 0 && To.Position < 0) {
625 Loaded.erase(Loaded.end() + From.Position, Loaded.end() + To.Position);
629 if (From.Position < 0) {
630 Loaded.erase(Loaded.end() + From.Position, Loaded.end());
631 From = begin(nullptr, true);
634 Local.erase(Local.begin() + From.Position, Local.begin() + To.Position);
638 /// \brief A lazy pointer to a statement.
639 typedef LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>
642 /// \brief A lazy pointer to a declaration.
643 typedef LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>
646 /// \brief A lazy pointer to a set of CXXBaseSpecifiers.
647 typedef LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
648 &ExternalASTSource::GetExternalCXXBaseSpecifiers>
649 LazyCXXBaseSpecifiersPtr;
651 } // end namespace clang