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/DeclBase.h"
18 #include "clang/AST/CharUnits.h"
19 #include "llvm/ADT/DenseMap.h"
24 class CXXBaseSpecifier;
25 class DeclarationName;
26 class ExternalSemaSource; // layering violation required for downcasting
32 /// \brief Enumeration describing the result of loading information from
33 /// an external source.
34 enum ExternalLoadResult {
35 /// \brief Loading the external information has succeeded.
38 /// \brief Loading the external information has failed.
41 /// \brief The external information has already been loaded, and therefore
42 /// no additional processing is required.
46 /// \brief Abstract interface for external sources of AST nodes.
48 /// External AST sources provide AST nodes constructed from some
49 /// external source, such as a precompiled header. External AST
50 /// sources can resolve types and declarations from abstract IDs into
51 /// actual type and declaration nodes, and read parts of declaration
53 class ExternalASTSource {
54 /// \brief Whether this AST source also provides information for
55 /// semantic analysis.
58 friend class ExternalSemaSource;
61 ExternalASTSource() : SemaSource(false) { }
63 virtual ~ExternalASTSource();
65 /// \brief RAII class for safely pairing a StartedDeserializing call
66 /// with FinishedDeserializing.
68 ExternalASTSource *Source;
70 explicit Deserializing(ExternalASTSource *source) : Source(source) {
72 Source->StartedDeserializing();
75 Source->FinishedDeserializing();
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++ base specifiers in the decl
113 /// stream into an array of specifiers.
115 /// The default implementation of this method is a no-op.
116 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
118 /// \brief Finds all declarations with the given name in the
121 /// Generally the final step of this method is either to call
122 /// SetExternalVisibleDeclsForName or to recursively call lookup on
123 /// the DeclContext after calling SetExternalVisibleDecls.
125 /// The default implementation of this method is a no-op.
126 virtual DeclContextLookupResult
127 FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
129 /// \brief Ensures that the table of all visible declarations inside this
130 /// context is up to date.
132 /// The default implementation of this functino is a no-op.
133 virtual void completeVisibleDeclsMap(const DeclContext *DC);
135 /// \brief Finds all declarations lexically contained within the given
136 /// DeclContext, after applying an optional filter predicate.
138 /// \param isKindWeWant a predicate function that returns true if the passed
139 /// declaration kind is one we are looking for. If NULL, all declarations
142 /// \return an indication of whether the load succeeded or failed.
144 /// The default implementation of this method is a no-op.
145 virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
146 bool (*isKindWeWant)(Decl::Kind),
147 SmallVectorImpl<Decl*> &Result);
149 /// \brief Finds all declarations lexically contained within the given
152 /// \return true if an error occurred
153 ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
154 SmallVectorImpl<Decl*> &Result) {
155 return FindExternalLexicalDecls(DC, 0, Result);
158 template <typename DeclTy>
159 ExternalLoadResult FindExternalLexicalDeclsBy(const DeclContext *DC,
160 SmallVectorImpl<Decl*> &Result) {
161 return FindExternalLexicalDecls(DC, DeclTy::classofKind, Result);
164 /// \brief Get the decls that are contained in a file in the Offset/Length
165 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
167 virtual void FindFileRegionDecls(FileID File, unsigned Offset,unsigned Length,
168 SmallVectorImpl<Decl *> &Decls) {}
170 /// \brief Gives the external AST source an opportunity to complete
171 /// an incomplete type.
172 virtual void CompleteType(TagDecl *Tag) {}
174 /// \brief Gives the external AST source an opportunity to complete an
175 /// incomplete Objective-C class.
177 /// This routine will only be invoked if the "externally completed" bit is
178 /// set on the ObjCInterfaceDecl via the function
179 /// \c ObjCInterfaceDecl::setExternallyCompleted().
180 virtual void CompleteType(ObjCInterfaceDecl *Class) { }
182 /// \brief Loads comment ranges.
183 virtual void ReadComments() { }
185 /// \brief Notify ExternalASTSource that we started deserialization of
186 /// a decl or type so until FinishedDeserializing is called there may be
187 /// decls that are initializing. Must be paired with FinishedDeserializing.
189 /// The default implementation of this method is a no-op.
190 virtual void StartedDeserializing() { }
192 /// \brief Notify ExternalASTSource that we finished the deserialization of
193 /// a decl or type. Must be paired with StartedDeserializing.
195 /// The default implementation of this method is a no-op.
196 virtual void FinishedDeserializing() { }
198 /// \brief Function that will be invoked when we begin parsing a new
199 /// translation unit involving this external AST source.
201 /// The default implementation of this method is a no-op.
202 virtual void StartTranslationUnit(ASTConsumer *Consumer) { }
204 /// \brief Print any statistics that have been gathered regarding
205 /// the external AST source.
207 /// The default implementation of this method is a no-op.
208 virtual void PrintStats();
211 /// \brief Perform layout on the given record.
213 /// This routine allows the external AST source to provide an specific
214 /// layout for a record, overriding the layout that would normally be
215 /// constructed. It is intended for clients who receive specific layout
216 /// details rather than source code (such as LLDB). The client is expected
217 /// to fill in the field offsets, base offsets, virtual base offsets, and
218 /// complete object size.
220 /// \param Record The record whose layout is being requested.
222 /// \param Size The final size of the record, in bits.
224 /// \param Alignment The final alignment of the record, in bits.
226 /// \param FieldOffsets The offset of each of the fields within the record,
227 /// expressed in bits. All of the fields must be provided with offsets.
229 /// \param BaseOffsets The offset of each of the direct, non-virtual base
230 /// classes. If any bases are not given offsets, the bases will be laid
231 /// out according to the ABI.
233 /// \param VirtualBaseOffsets The offset of each of the virtual base classes
234 /// (either direct or not). If any bases are not given offsets, the bases will be laid
235 /// out according to the ABI.
237 /// \returns true if the record layout was provided, false otherwise.
239 layoutRecordType(const RecordDecl *Record,
240 uint64_t &Size, uint64_t &Alignment,
241 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
242 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
243 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
248 //===--------------------------------------------------------------------===//
249 // Queries for performance analysis.
250 //===--------------------------------------------------------------------===//
252 struct MemoryBufferSizes {
256 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
257 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
260 /// Return the amount of memory used by memory buffers, breaking down
261 /// by heap-backed versus mmap'ed memory.
262 MemoryBufferSizes getMemoryBufferSizes() const {
263 MemoryBufferSizes sizes(0, 0);
264 getMemoryBufferSizes(sizes);
268 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
271 static DeclContextLookupResult
272 SetExternalVisibleDeclsForName(const DeclContext *DC,
273 DeclarationName Name,
274 ArrayRef<NamedDecl*> Decls);
276 static DeclContextLookupResult
277 SetNoExternalVisibleDeclsForName(const DeclContext *DC,
278 DeclarationName Name);
281 /// \brief A lazy pointer to an AST node (of base type T) that resides
282 /// within an external AST source.
284 /// The AST node is identified within the external AST source by a
285 /// 63-bit offset, and can be retrieved via an operation on the
286 /// external AST source itself.
287 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
288 struct LazyOffsetPtr {
289 /// \brief Either a pointer to an AST node or the offset within the
290 /// external AST source where the AST node can be found.
292 /// If the low bit is clear, a pointer to the AST node. If the low
293 /// bit is set, the upper 63 bits are the offset.
294 mutable uint64_t Ptr;
297 LazyOffsetPtr() : Ptr(0) { }
299 explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
300 explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
301 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
306 LazyOffsetPtr &operator=(T *Ptr) {
307 this->Ptr = reinterpret_cast<uint64_t>(Ptr);
311 LazyOffsetPtr &operator=(uint64_t Offset) {
312 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
316 Ptr = (Offset << 1) | 0x01;
321 /// \brief Whether this pointer is non-NULL.
323 /// This operation does not require the AST node to be deserialized.
324 operator bool() const { return Ptr != 0; }
326 /// \brief Whether this pointer is currently stored as an offset.
327 bool isOffset() const { return Ptr & 0x01; }
329 /// \brief Retrieve the pointer to the AST node that this lazy pointer
331 /// \param Source the external AST source.
333 /// \returns a pointer to the AST node.
334 T* get(ExternalASTSource *Source) const {
337 "Cannot deserialize a lazy pointer without an AST source");
338 Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
340 return reinterpret_cast<T*>(Ptr);
344 /// \brief Represents a lazily-loaded vector of data.
346 /// The lazily-loaded vector of data contains data that is partially loaded
347 /// from an external source and partially added by local translation. The
348 /// items loaded from the external source are loaded lazily, when needed for
349 /// iteration over the complete vector.
350 template<typename T, typename Source,
351 void (Source::*Loader)(SmallVectorImpl<T>&),
352 unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
354 SmallVector<T, LoadedStorage> Loaded;
355 SmallVector<T, LocalStorage> Local;
358 // Iteration over the elements in the vector.
362 /// \brief Position within the vector..
364 /// In a complete iteration, the Position field walks the range [-M, N),
365 /// where negative values are used to indicate elements
366 /// loaded from the external source while non-negative values are used to
367 /// indicate elements added via \c push_back().
368 /// However, to provide iteration in source order (for, e.g., chained
369 /// precompiled headers), dereferencing the iterator flips the negative
370 /// values (corresponding to loaded entities), so that position -M
371 /// corresponds to element 0 in the loaded entities vector, position -M+1
372 /// corresponds to element 1 in the loaded entities vector, etc. This
373 /// gives us a reasonably efficient, source-order walk.
376 friend class LazyVector;
379 typedef T value_type;
380 typedef value_type& reference;
381 typedef value_type* pointer;
382 typedef std::random_access_iterator_tag iterator_category;
383 typedef int difference_type;
385 iterator() : Self(0), Position(0) { }
387 iterator(LazyVector *Self, int Position)
388 : Self(Self), Position(Position) { }
390 reference operator*() const {
392 return Self->Loaded.end()[Position];
393 return Self->Local[Position];
396 pointer operator->() const {
398 return &Self->Loaded.end()[Position];
400 return &Self->Local[Position];
403 reference operator[](difference_type D) {
407 iterator &operator++() {
412 iterator operator++(int) {
413 iterator Prev(*this);
418 iterator &operator--() {
423 iterator operator--(int) {
424 iterator Prev(*this);
429 friend bool operator==(const iterator &X, const iterator &Y) {
430 return X.Position == Y.Position;
433 friend bool operator!=(const iterator &X, const iterator &Y) {
434 return X.Position != Y.Position;
437 friend bool operator<(const iterator &X, const iterator &Y) {
438 return X.Position < Y.Position;
441 friend bool operator>(const iterator &X, const iterator &Y) {
442 return X.Position > Y.Position;
445 friend bool operator<=(const iterator &X, const iterator &Y) {
446 return X.Position < Y.Position;
449 friend bool operator>=(const iterator &X, const iterator &Y) {
450 return X.Position > Y.Position;
453 friend iterator& operator+=(iterator &X, difference_type D) {
458 friend iterator& operator-=(iterator &X, difference_type D) {
463 friend iterator operator+(iterator X, difference_type D) {
468 friend iterator operator+(difference_type D, iterator X) {
473 friend difference_type operator-(const iterator &X, const iterator &Y) {
474 return X.Position - Y.Position;
477 friend iterator operator-(iterator X, difference_type D) {
482 friend class iterator;
484 iterator begin(Source *source, bool LocalOnly = false) {
486 return iterator(this, 0);
489 (source->*Loader)(Loaded);
490 return iterator(this, -(int)Loaded.size());
494 return iterator(this, Local.size());
497 void push_back(const T& LocalValue) {
498 Local.push_back(LocalValue);
501 void erase(iterator From, iterator To) {
502 if (From.Position < 0 && To.Position < 0) {
503 Loaded.erase(Loaded.end() + From.Position, Loaded.end() + To.Position);
507 if (From.Position < 0) {
508 Loaded.erase(Loaded.end() + From.Position, Loaded.end());
509 From = begin(0, true);
512 Local.erase(Local.begin() + From.Position, Local.begin() + To.Position);
516 /// \brief A lazy pointer to a statement.
517 typedef LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>
520 /// \brief A lazy pointer to a declaration.
521 typedef LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>
524 /// \brief A lazy pointer to a set of CXXBaseSpecifiers.
525 typedef LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
526 &ExternalASTSource::GetExternalCXXBaseSpecifiers>
527 LazyCXXBaseSpecifiersPtr;
529 } // end namespace clang
531 #endif // LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H