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. \arg Length can be 0 to indicate a point at \arg 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 Notify ExternalASTSource that we started deserialization of
183 /// a decl or type so until FinishedDeserializing is called there may be
184 /// decls that are initializing. Must be paired with FinishedDeserializing.
186 /// The default implementation of this method is a no-op.
187 virtual void StartedDeserializing() { }
189 /// \brief Notify ExternalASTSource that we finished the deserialization of
190 /// a decl or type. Must be paired with StartedDeserializing.
192 /// The default implementation of this method is a no-op.
193 virtual void FinishedDeserializing() { }
195 /// \brief Function that will be invoked when we begin parsing a new
196 /// translation unit involving this external AST source.
198 /// The default implementation of this method is a no-op.
199 virtual void StartTranslationUnit(ASTConsumer *Consumer) { }
201 /// \brief Print any statistics that have been gathered regarding
202 /// the external AST source.
204 /// The default implementation of this method is a no-op.
205 virtual void PrintStats();
208 /// \brief Perform layout on the given record.
210 /// This routine allows the external AST source to provide an specific
211 /// layout for a record, overriding the layout that would normally be
212 /// constructed. It is intended for clients who receive specific layout
213 /// details rather than source code (such as LLDB). The client is expected
214 /// to fill in the field offsets, base offsets, virtual base offsets, and
215 /// complete object size.
217 /// \param Record The record whose layout is being requested.
219 /// \param Size The final size of the record, in bits.
221 /// \param Alignment The final alignment of the record, in bits.
223 /// \param FieldOffsets The offset of each of the fields within the record,
224 /// expressed in bits. All of the fields must be provided with offsets.
226 /// \param BaseOffsets The offset of each of the direct, non-virtual base
227 /// classes. If any bases are not given offsets, the bases will be laid
228 /// out according to the ABI.
230 /// \param VirtualBaseOffsets The offset of each of the virtual base classes
231 /// (either direct or not). If any bases are not given offsets, the bases will be laid
232 /// out according to the ABI.
234 /// \returns true if the record layout was provided, false otherwise.
236 layoutRecordType(const RecordDecl *Record,
237 uint64_t &Size, uint64_t &Alignment,
238 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
239 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
240 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
245 //===--------------------------------------------------------------------===//
246 // Queries for performance analysis.
247 //===--------------------------------------------------------------------===//
249 struct MemoryBufferSizes {
253 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
254 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
257 /// Return the amount of memory used by memory buffers, breaking down
258 /// by heap-backed versus mmap'ed memory.
259 MemoryBufferSizes getMemoryBufferSizes() const {
260 MemoryBufferSizes sizes(0, 0);
261 getMemoryBufferSizes(sizes);
265 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
268 static DeclContextLookupResult
269 SetExternalVisibleDeclsForName(const DeclContext *DC,
270 DeclarationName Name,
271 ArrayRef<NamedDecl*> Decls);
273 static DeclContextLookupResult
274 SetNoExternalVisibleDeclsForName(const DeclContext *DC,
275 DeclarationName Name);
278 /// \brief A lazy pointer to an AST node (of base type T) that resides
279 /// within an external AST source.
281 /// The AST node is identified within the external AST source by a
282 /// 63-bit offset, and can be retrieved via an operation on the
283 /// external AST source itself.
284 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
285 struct LazyOffsetPtr {
286 /// \brief Either a pointer to an AST node or the offset within the
287 /// external AST source where the AST node can be found.
289 /// If the low bit is clear, a pointer to the AST node. If the low
290 /// bit is set, the upper 63 bits are the offset.
291 mutable uint64_t Ptr;
294 LazyOffsetPtr() : Ptr(0) { }
296 explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
297 explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
298 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
303 LazyOffsetPtr &operator=(T *Ptr) {
304 this->Ptr = reinterpret_cast<uint64_t>(Ptr);
308 LazyOffsetPtr &operator=(uint64_t Offset) {
309 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
313 Ptr = (Offset << 1) | 0x01;
318 /// \brief Whether this pointer is non-NULL.
320 /// This operation does not require the AST node to be deserialized.
321 operator bool() const { return Ptr != 0; }
323 /// \brief Whether this pointer is currently stored as an offset.
324 bool isOffset() const { return Ptr & 0x01; }
326 /// \brief Retrieve the pointer to the AST node that this lazy pointer
328 /// \param Source the external AST source.
330 /// \returns a pointer to the AST node.
331 T* get(ExternalASTSource *Source) const {
334 "Cannot deserialize a lazy pointer without an AST source");
335 Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
337 return reinterpret_cast<T*>(Ptr);
341 /// \brief Represents a lazily-loaded vector of data.
343 /// The lazily-loaded vector of data contains data that is partially loaded
344 /// from an external source and partially added by local translation. The
345 /// items loaded from the external source are loaded lazily, when needed for
346 /// iteration over the complete vector.
347 template<typename T, typename Source,
348 void (Source::*Loader)(SmallVectorImpl<T>&),
349 unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
351 SmallVector<T, LoadedStorage> Loaded;
352 SmallVector<T, LocalStorage> Local;
355 // Iteration over the elements in the vector.
359 /// \brief Position within the vector..
361 /// In a complete iteration, the Position field walks the range [-M, N),
362 /// where negative values are used to indicate elements
363 /// loaded from the external source while non-negative values are used to
364 /// indicate elements added via \c push_back().
365 /// However, to provide iteration in source order (for, e.g., chained
366 /// precompiled headers), dereferencing the iterator flips the negative
367 /// values (corresponding to loaded entities), so that position -M
368 /// corresponds to element 0 in the loaded entities vector, position -M+1
369 /// corresponds to element 1 in the loaded entities vector, etc. This
370 /// gives us a reasonably efficient, source-order walk.
373 friend class LazyVector;
376 typedef T value_type;
377 typedef value_type& reference;
378 typedef value_type* pointer;
379 typedef std::random_access_iterator_tag iterator_category;
380 typedef int difference_type;
382 iterator() : Self(0), Position(0) { }
384 iterator(LazyVector *Self, int Position)
385 : Self(Self), Position(Position) { }
387 reference operator*() const {
389 return Self->Loaded.end()[Position];
390 return Self->Local[Position];
393 pointer operator->() const {
395 return &Self->Loaded.end()[Position];
397 return &Self->Local[Position];
400 reference operator[](difference_type D) {
404 iterator &operator++() {
409 iterator operator++(int) {
410 iterator Prev(*this);
415 iterator &operator--() {
420 iterator operator--(int) {
421 iterator Prev(*this);
426 friend bool operator==(const iterator &X, const iterator &Y) {
427 return X.Position == Y.Position;
430 friend bool operator!=(const iterator &X, const iterator &Y) {
431 return X.Position != Y.Position;
434 friend bool operator<(const iterator &X, const iterator &Y) {
435 return X.Position < Y.Position;
438 friend bool operator>(const iterator &X, const iterator &Y) {
439 return X.Position > Y.Position;
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 iterator& operator+=(iterator &X, difference_type D) {
455 friend iterator& operator-=(iterator &X, difference_type D) {
460 friend iterator operator+(iterator X, difference_type D) {
465 friend iterator operator+(difference_type D, iterator X) {
470 friend difference_type operator-(const iterator &X, const iterator &Y) {
471 return X.Position - Y.Position;
474 friend iterator operator-(iterator X, difference_type D) {
479 friend class iterator;
481 iterator begin(Source *source, bool LocalOnly = false) {
483 return iterator(this, 0);
486 (source->*Loader)(Loaded);
487 return iterator(this, -(int)Loaded.size());
491 return iterator(this, Local.size());
494 void push_back(const T& LocalValue) {
495 Local.push_back(LocalValue);
498 void erase(iterator From, iterator To) {
499 if (From.Position < 0 && To.Position < 0) {
500 Loaded.erase(Loaded.end() + From.Position, Loaded.end() + To.Position);
504 if (From.Position < 0) {
505 Loaded.erase(Loaded.end() + From.Position, Loaded.end());
506 From = begin(0, true);
509 Local.erase(Local.begin() + From.Position, Local.begin() + To.Position);
513 /// \brief A lazy pointer to a statement.
514 typedef LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>
517 /// \brief A lazy pointer to a declaration.
518 typedef LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>
521 /// \brief A lazy pointer to a set of CXXBaseSpecifiers.
522 typedef LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
523 &ExternalASTSource::GetExternalCXXBaseSpecifiers>
524 LazyCXXBaseSpecifiersPtr;
526 } // end namespace clang
528 #endif // LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H