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_EXTERNAL_AST_SOURCE_H
15 #define LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_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 /// \brief Whether this AST source also provides information for
58 /// semantic analysis.
61 friend class ExternalSemaSource;
64 ExternalASTSource() : SemaSource(false) { }
66 virtual ~ExternalASTSource();
68 /// \brief RAII class for safely pairing a StartedDeserializing call
69 /// with FinishedDeserializing.
71 ExternalASTSource *Source;
73 explicit Deserializing(ExternalASTSource *source) : Source(source) {
75 Source->StartedDeserializing();
78 Source->FinishedDeserializing();
82 /// \brief Resolve a declaration ID into a declaration, potentially
83 /// building a new declaration.
85 /// This method only needs to be implemented if the AST source ever
86 /// passes back decl sets as VisibleDeclaration objects.
88 /// The default implementation of this method is a no-op.
89 virtual Decl *GetExternalDecl(uint32_t ID);
91 /// \brief Resolve a selector ID into a selector.
93 /// This operation only needs to be implemented if the AST source
94 /// returns non-zero for GetNumKnownSelectors().
96 /// The default implementation of this method is a no-op.
97 virtual Selector GetExternalSelector(uint32_t ID);
99 /// \brief Returns the number of selectors known to the external AST
102 /// The default implementation of this method is a no-op.
103 virtual uint32_t GetNumExternalSelectors();
105 /// \brief Resolve the offset of a statement in the decl stream into
108 /// This operation is meant to be used via a LazyOffsetPtr. It only
109 /// needs to be implemented if the AST source uses methods like
110 /// FunctionDecl::setLazyBody when building decls.
112 /// The default implementation of this method is a no-op.
113 virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
115 /// \brief Resolve the offset of a set of C++ base specifiers in the decl
116 /// stream into an array of specifiers.
118 /// The default implementation of this method is a no-op.
119 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
121 /// \brief Update an out-of-date identifier.
122 virtual void updateOutOfDateIdentifier(IdentifierInfo &II) { }
124 /// \brief Find all declarations with the given name in the given context,
125 /// and add them to the context by calling SetExternalVisibleDeclsForName
126 /// or SetNoExternalVisibleDeclsForName.
127 /// \return \c true if any declarations might have been found, \c false if
128 /// we definitely have no declarations with tbis name.
130 /// The default implementation of this method is a no-op returning \c false.
132 FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
134 /// \brief Ensures that the table of all visible declarations inside this
135 /// context is up to date.
137 /// The default implementation of this function is a no-op.
138 virtual void completeVisibleDeclsMap(const DeclContext *DC);
140 /// \brief Retrieve the module that corresponds to the given module ID.
141 virtual Module *getModule(unsigned ID) { return nullptr; }
143 /// \brief Finds all declarations lexically contained within the given
144 /// DeclContext, after applying an optional filter predicate.
146 /// \param isKindWeWant a predicate function that returns true if the passed
147 /// declaration kind is one we are looking for. If NULL, all declarations
150 /// \return an indication of whether the load succeeded or failed.
152 /// The default implementation of this method is a no-op.
153 virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
154 bool (*isKindWeWant)(Decl::Kind),
155 SmallVectorImpl<Decl*> &Result);
157 /// \brief Finds all declarations lexically contained within the given
160 /// \return true if an error occurred
161 ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
162 SmallVectorImpl<Decl*> &Result) {
163 return FindExternalLexicalDecls(DC, nullptr, Result);
166 template <typename DeclTy>
167 ExternalLoadResult FindExternalLexicalDeclsBy(const DeclContext *DC,
168 SmallVectorImpl<Decl*> &Result) {
169 return FindExternalLexicalDecls(DC, DeclTy::classofKind, Result);
172 /// \brief Get the decls that are contained in a file in the Offset/Length
173 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
175 virtual void FindFileRegionDecls(FileID File, unsigned Offset,unsigned Length,
176 SmallVectorImpl<Decl *> &Decls) {}
178 /// \brief Gives the external AST source an opportunity to complete
179 /// an incomplete type.
180 virtual void CompleteType(TagDecl *Tag) {}
182 /// \brief Gives the external AST source an opportunity to complete an
183 /// incomplete Objective-C class.
185 /// This routine will only be invoked if the "externally completed" bit is
186 /// set on the ObjCInterfaceDecl via the function
187 /// \c ObjCInterfaceDecl::setExternallyCompleted().
188 virtual void CompleteType(ObjCInterfaceDecl *Class) { }
190 /// \brief Loads comment ranges.
191 virtual void ReadComments() { }
193 /// \brief Notify ExternalASTSource that we started deserialization of
194 /// a decl or type so until FinishedDeserializing is called there may be
195 /// decls that are initializing. Must be paired with FinishedDeserializing.
197 /// The default implementation of this method is a no-op.
198 virtual void StartedDeserializing() { }
200 /// \brief Notify ExternalASTSource that we finished the deserialization of
201 /// a decl or type. Must be paired with StartedDeserializing.
203 /// The default implementation of this method is a no-op.
204 virtual void FinishedDeserializing() { }
206 /// \brief Function that will be invoked when we begin parsing a new
207 /// translation unit involving this external AST source.
209 /// The default implementation of this method is a no-op.
210 virtual void StartTranslationUnit(ASTConsumer *Consumer) { }
212 /// \brief Print any statistics that have been gathered regarding
213 /// the external AST source.
215 /// The default implementation of this method is a no-op.
216 virtual void PrintStats();
219 /// \brief Perform layout on the given record.
221 /// This routine allows the external AST source to provide an specific
222 /// layout for a record, overriding the layout that would normally be
223 /// constructed. It is intended for clients who receive specific layout
224 /// details rather than source code (such as LLDB). The client is expected
225 /// to fill in the field offsets, base offsets, virtual base offsets, and
226 /// complete object size.
228 /// \param Record The record whose layout is being requested.
230 /// \param Size The final size of the record, in bits.
232 /// \param Alignment The final alignment of the record, in bits.
234 /// \param FieldOffsets The offset of each of the fields within the record,
235 /// expressed in bits. All of the fields must be provided with offsets.
237 /// \param BaseOffsets The offset of each of the direct, non-virtual base
238 /// classes. If any bases are not given offsets, the bases will be laid
239 /// out according to the ABI.
241 /// \param VirtualBaseOffsets The offset of each of the virtual base classes
242 /// (either direct or not). If any bases are not given offsets, the bases will be laid
243 /// out according to the ABI.
245 /// \returns true if the record layout was provided, false otherwise.
247 layoutRecordType(const RecordDecl *Record,
248 uint64_t &Size, uint64_t &Alignment,
249 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
250 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
251 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
256 //===--------------------------------------------------------------------===//
257 // Queries for performance analysis.
258 //===--------------------------------------------------------------------===//
260 struct MemoryBufferSizes {
264 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
265 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
268 /// Return the amount of memory used by memory buffers, breaking down
269 /// by heap-backed versus mmap'ed memory.
270 MemoryBufferSizes getMemoryBufferSizes() const {
271 MemoryBufferSizes sizes(0, 0);
272 getMemoryBufferSizes(sizes);
276 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
279 static DeclContextLookupResult
280 SetExternalVisibleDeclsForName(const DeclContext *DC,
281 DeclarationName Name,
282 ArrayRef<NamedDecl*> Decls);
284 static DeclContextLookupResult
285 SetNoExternalVisibleDeclsForName(const DeclContext *DC,
286 DeclarationName Name);
289 /// \brief A lazy pointer to an AST node (of base type T) that resides
290 /// within an external AST source.
292 /// The AST node is identified within the external AST source by a
293 /// 63-bit offset, and can be retrieved via an operation on the
294 /// external AST source itself.
295 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
296 struct LazyOffsetPtr {
297 /// \brief Either a pointer to an AST node or the offset within the
298 /// external AST source where the AST node can be found.
300 /// If the low bit is clear, a pointer to the AST node. If the low
301 /// bit is set, the upper 63 bits are the offset.
302 mutable uint64_t Ptr;
305 LazyOffsetPtr() : Ptr(0) { }
307 explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
308 explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
309 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
314 LazyOffsetPtr &operator=(T *Ptr) {
315 this->Ptr = reinterpret_cast<uint64_t>(Ptr);
319 LazyOffsetPtr &operator=(uint64_t Offset) {
320 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
324 Ptr = (Offset << 1) | 0x01;
329 /// \brief Whether this pointer is non-NULL.
331 /// This operation does not require the AST node to be deserialized.
332 LLVM_EXPLICIT operator bool() const { return Ptr != 0; }
334 /// \brief Whether this pointer is non-NULL.
336 /// This operation does not require the AST node to be deserialized.
337 bool isValid() const { return Ptr != 0; }
339 /// \brief Whether this pointer is currently stored as an offset.
340 bool isOffset() const { return Ptr & 0x01; }
342 /// \brief Retrieve the pointer to the AST node that this lazy pointer
344 /// \param Source the external AST source.
346 /// \returns a pointer to the AST node.
347 T* get(ExternalASTSource *Source) const {
350 "Cannot deserialize a lazy pointer without an AST source");
351 Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
353 return reinterpret_cast<T*>(Ptr);
357 /// \brief Represents a lazily-loaded vector of data.
359 /// The lazily-loaded vector of data contains data that is partially loaded
360 /// from an external source and partially added by local translation. The
361 /// items loaded from the external source are loaded lazily, when needed for
362 /// iteration over the complete vector.
363 template<typename T, typename Source,
364 void (Source::*Loader)(SmallVectorImpl<T>&),
365 unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
367 SmallVector<T, LoadedStorage> Loaded;
368 SmallVector<T, LocalStorage> Local;
371 // Iteration over the elements in the vector.
375 /// \brief Position within the vector..
377 /// In a complete iteration, the Position field walks the range [-M, N),
378 /// where negative values are used to indicate elements
379 /// loaded from the external source while non-negative values are used to
380 /// indicate elements added via \c push_back().
381 /// However, to provide iteration in source order (for, e.g., chained
382 /// precompiled headers), dereferencing the iterator flips the negative
383 /// values (corresponding to loaded entities), so that position -M
384 /// corresponds to element 0 in the loaded entities vector, position -M+1
385 /// corresponds to element 1 in the loaded entities vector, etc. This
386 /// gives us a reasonably efficient, source-order walk.
389 friend class LazyVector;
392 typedef T value_type;
393 typedef value_type& reference;
394 typedef value_type* pointer;
395 typedef std::random_access_iterator_tag iterator_category;
396 typedef int difference_type;
398 iterator() : Self(0), Position(0) { }
400 iterator(LazyVector *Self, int Position)
401 : Self(Self), Position(Position) { }
403 reference operator*() const {
405 return Self->Loaded.end()[Position];
406 return Self->Local[Position];
409 pointer operator->() const {
411 return &Self->Loaded.end()[Position];
413 return &Self->Local[Position];
416 reference operator[](difference_type D) {
420 iterator &operator++() {
425 iterator operator++(int) {
426 iterator Prev(*this);
431 iterator &operator--() {
436 iterator operator--(int) {
437 iterator Prev(*this);
442 friend bool operator==(const iterator &X, const iterator &Y) {
443 return X.Position == Y.Position;
446 friend bool operator!=(const iterator &X, const iterator &Y) {
447 return X.Position != Y.Position;
450 friend bool operator<(const iterator &X, const iterator &Y) {
451 return X.Position < Y.Position;
454 friend bool operator>(const iterator &X, const iterator &Y) {
455 return X.Position > Y.Position;
458 friend bool operator<=(const iterator &X, const iterator &Y) {
459 return X.Position < Y.Position;
462 friend bool operator>=(const iterator &X, const iterator &Y) {
463 return X.Position > Y.Position;
466 friend iterator& operator+=(iterator &X, difference_type D) {
471 friend iterator& operator-=(iterator &X, difference_type D) {
476 friend iterator operator+(iterator X, difference_type D) {
481 friend iterator operator+(difference_type D, iterator X) {
486 friend difference_type operator-(const iterator &X, const iterator &Y) {
487 return X.Position - Y.Position;
490 friend iterator operator-(iterator X, difference_type D) {
495 friend class iterator;
497 iterator begin(Source *source, bool LocalOnly = false) {
499 return iterator(this, 0);
502 (source->*Loader)(Loaded);
503 return iterator(this, -(int)Loaded.size());
507 return iterator(this, Local.size());
510 void push_back(const T& LocalValue) {
511 Local.push_back(LocalValue);
514 void erase(iterator From, iterator To) {
515 if (From.Position < 0 && To.Position < 0) {
516 Loaded.erase(Loaded.end() + From.Position, Loaded.end() + To.Position);
520 if (From.Position < 0) {
521 Loaded.erase(Loaded.end() + From.Position, Loaded.end());
522 From = begin(nullptr, true);
525 Local.erase(Local.begin() + From.Position, Local.begin() + To.Position);
529 /// \brief A lazy pointer to a statement.
530 typedef LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>
533 /// \brief A lazy pointer to a declaration.
534 typedef LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>
537 /// \brief A lazy pointer to a set of CXXBaseSpecifiers.
538 typedef LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
539 &ExternalASTSource::GetExternalCXXBaseSpecifiers>
540 LazyCXXBaseSpecifiersPtr;
542 } // end namespace clang
544 #endif // LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H