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"
22 class CXXBaseSpecifier;
23 class DeclarationName;
24 class ExternalSemaSource; // layering violation required for downcasting
30 /// \brief Enumeration describing the result of loading information from
31 /// an external source.
32 enum ExternalLoadResult {
33 /// \brief Loading the external information has succeeded.
36 /// \brief Loading the external information has failed.
39 /// \brief The external information has already been loaded, and therefore
40 /// no additional processing is required.
44 /// \brief Abstract interface for external sources of AST nodes.
46 /// External AST sources provide AST nodes constructed from some
47 /// external source, such as a precompiled header. External AST
48 /// sources can resolve types and declarations from abstract IDs into
49 /// actual type and declaration nodes, and read parts of declaration
51 class ExternalASTSource {
52 /// \brief Whether this AST source also provides information for
53 /// semantic analysis.
56 friend class ExternalSemaSource;
59 ExternalASTSource() : SemaSource(false) { }
61 virtual ~ExternalASTSource();
63 /// \brief RAII class for safely pairing a StartedDeserializing call
64 /// with FinishedDeserializing.
66 ExternalASTSource *Source;
68 explicit Deserializing(ExternalASTSource *source) : Source(source) {
70 Source->StartedDeserializing();
73 Source->FinishedDeserializing();
77 /// \brief Resolve a declaration ID into a declaration, potentially
78 /// building a new declaration.
80 /// This method only needs to be implemented if the AST source ever
81 /// passes back decl sets as VisibleDeclaration objects.
83 /// The default implementation of this method is a no-op.
84 virtual Decl *GetExternalDecl(uint32_t ID);
86 /// \brief Resolve a selector ID into a selector.
88 /// This operation only needs to be implemented if the AST source
89 /// returns non-zero for GetNumKnownSelectors().
91 /// The default implementation of this method is a no-op.
92 virtual Selector GetExternalSelector(uint32_t ID);
94 /// \brief Returns the number of selectors known to the external AST
97 /// The default implementation of this method is a no-op.
98 virtual uint32_t GetNumExternalSelectors();
100 /// \brief Resolve the offset of a statement in the decl stream into
103 /// This operation is meant to be used via a LazyOffsetPtr. It only
104 /// needs to be implemented if the AST source uses methods like
105 /// FunctionDecl::setLazyBody when building decls.
107 /// The default implementation of this method is a no-op.
108 virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
110 /// \brief Resolve the offset of a set of C++ base specifiers in the decl
111 /// stream into an array of specifiers.
113 /// The default implementation of this method is a no-op.
114 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
116 /// \brief Finds all declarations with the given name in the
119 /// Generally the final step of this method is either to call
120 /// SetExternalVisibleDeclsForName or to recursively call lookup on
121 /// the DeclContext after calling SetExternalVisibleDecls.
123 /// The default implementation of this method is a no-op.
124 virtual DeclContextLookupResult
125 FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
127 /// \brief Finds all declarations lexically contained within the given
128 /// DeclContext, after applying an optional filter predicate.
130 /// \param isKindWeWant a predicate function that returns true if the passed
131 /// declaration kind is one we are looking for. If NULL, all declarations
134 /// \return an indication of whether the load succeeded or failed.
136 /// The default implementation of this method is a no-op.
137 virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
138 bool (*isKindWeWant)(Decl::Kind),
139 SmallVectorImpl<Decl*> &Result);
141 /// \brief Finds all declarations lexically contained within the given
144 /// \return true if an error occurred
145 ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
146 SmallVectorImpl<Decl*> &Result) {
147 return FindExternalLexicalDecls(DC, 0, Result);
150 template <typename DeclTy>
151 ExternalLoadResult FindExternalLexicalDeclsBy(const DeclContext *DC,
152 SmallVectorImpl<Decl*> &Result) {
153 return FindExternalLexicalDecls(DC, DeclTy::classofKind, Result);
156 /// \brief Gives the external AST source an opportunity to complete
157 /// an incomplete type.
158 virtual void CompleteType(TagDecl *Tag) {}
160 /// \brief Gives the external AST source an opportunity to complete an
161 /// incomplete Objective-C class.
163 /// This routine will only be invoked if the "externally completed" bit is
164 /// set on the ObjCInterfaceDecl via the function
165 /// \c ObjCInterfaceDecl::setExternallyCompleted().
166 virtual void CompleteType(ObjCInterfaceDecl *Class) { }
168 /// \brief Notify ExternalASTSource that we started deserialization of
169 /// a decl or type so until FinishedDeserializing is called there may be
170 /// decls that are initializing. Must be paired with FinishedDeserializing.
172 /// The default implementation of this method is a no-op.
173 virtual void StartedDeserializing() { }
175 /// \brief Notify ExternalASTSource that we finished the deserialization of
176 /// a decl or type. Must be paired with StartedDeserializing.
178 /// The default implementation of this method is a no-op.
179 virtual void FinishedDeserializing() { }
181 /// \brief Function that will be invoked when we begin parsing a new
182 /// translation unit involving this external AST source.
184 /// The default implementation of this method is a no-op.
185 virtual void StartTranslationUnit(ASTConsumer *Consumer) { }
187 /// \brief Print any statistics that have been gathered regarding
188 /// the external AST source.
190 /// The default implementation of this method is a no-op.
191 virtual void PrintStats();
193 //===--------------------------------------------------------------------===//
194 // Queries for performance analysis.
195 //===--------------------------------------------------------------------===//
197 struct MemoryBufferSizes {
201 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
202 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
205 /// Return the amount of memory used by memory buffers, breaking down
206 /// by heap-backed versus mmap'ed memory.
207 MemoryBufferSizes getMemoryBufferSizes() const {
208 MemoryBufferSizes sizes(0, 0);
209 getMemoryBufferSizes(sizes);
213 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
216 static DeclContextLookupResult
217 SetExternalVisibleDeclsForName(const DeclContext *DC,
218 DeclarationName Name,
219 ArrayRef<NamedDecl*> Decls);
221 static DeclContextLookupResult
222 SetNoExternalVisibleDeclsForName(const DeclContext *DC,
223 DeclarationName Name);
226 /// \brief A lazy pointer to an AST node (of base type T) that resides
227 /// within an external AST source.
229 /// The AST node is identified within the external AST source by a
230 /// 63-bit offset, and can be retrieved via an operation on the
231 /// external AST source itself.
232 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
233 struct LazyOffsetPtr {
234 /// \brief Either a pointer to an AST node or the offset within the
235 /// external AST source where the AST node can be found.
237 /// If the low bit is clear, a pointer to the AST node. If the low
238 /// bit is set, the upper 63 bits are the offset.
239 mutable uint64_t Ptr;
242 LazyOffsetPtr() : Ptr(0) { }
244 explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
245 explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
246 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
251 LazyOffsetPtr &operator=(T *Ptr) {
252 this->Ptr = reinterpret_cast<uint64_t>(Ptr);
256 LazyOffsetPtr &operator=(uint64_t Offset) {
257 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
261 Ptr = (Offset << 1) | 0x01;
266 /// \brief Whether this pointer is non-NULL.
268 /// This operation does not require the AST node to be deserialized.
269 operator bool() const { return Ptr != 0; }
271 /// \brief Whether this pointer is currently stored as an offset.
272 bool isOffset() const { return Ptr & 0x01; }
274 /// \brief Retrieve the pointer to the AST node that this lazy pointer
276 /// \param Source the external AST source.
278 /// \returns a pointer to the AST node.
279 T* get(ExternalASTSource *Source) const {
282 "Cannot deserialize a lazy pointer without an AST source");
283 Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
285 return reinterpret_cast<T*>(Ptr);
289 /// \brief Represents a lazily-loaded vector of data.
291 /// The lazily-loaded vector of data contains data that is partially loaded
292 /// from an external source and partially added by local translation. The
293 /// items loaded from the external source are loaded lazily, when needed for
294 /// iteration over the complete vector.
295 template<typename T, typename Source,
296 void (Source::*Loader)(SmallVectorImpl<T>&),
297 unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
299 SmallVector<T, LoadedStorage> Loaded;
300 SmallVector<T, LocalStorage> Local;
303 // Iteration over the elements in the vector.
307 /// \brief Position within the vector..
309 /// In a complete iteration, the Position field walks the range [-M, N),
310 /// where negative values are used to indicate elements
311 /// loaded from the external source while non-negative values are used to
312 /// indicate elements added via \c push_back().
313 /// However, to provide iteration in source order (for, e.g., chained
314 /// precompiled headers), dereferencing the iterator flips the negative
315 /// values (corresponding to loaded entities), so that position -M
316 /// corresponds to element 0 in the loaded entities vector, position -M+1
317 /// corresponds to element 1 in the loaded entities vector, etc. This
318 /// gives us a reasonably efficient, source-order walk.
321 friend class LazyVector;
324 typedef T value_type;
325 typedef value_type& reference;
326 typedef value_type* pointer;
327 typedef std::random_access_iterator_tag iterator_category;
328 typedef int difference_type;
330 iterator() : Self(0), Position(0) { }
332 iterator(LazyVector *Self, int Position)
333 : Self(Self), Position(Position) { }
335 reference operator*() const {
337 return Self->Loaded.end()[Position];
338 return Self->Local[Position];
341 pointer operator->() const {
343 return &Self->Loaded.end()[Position];
345 return &Self->Local[Position];
348 reference operator[](difference_type D) {
352 iterator &operator++() {
357 iterator operator++(int) {
358 iterator Prev(*this);
363 iterator &operator--() {
368 iterator operator--(int) {
369 iterator Prev(*this);
374 friend bool operator==(const iterator &X, const iterator &Y) {
375 return X.Position == Y.Position;
378 friend bool operator!=(const iterator &X, const iterator &Y) {
379 return X.Position != Y.Position;
382 friend bool operator<(const iterator &X, const iterator &Y) {
383 return X.Position < Y.Position;
386 friend bool operator>(const iterator &X, const iterator &Y) {
387 return X.Position > Y.Position;
390 friend bool operator<=(const iterator &X, const iterator &Y) {
391 return X.Position < Y.Position;
394 friend bool operator>=(const iterator &X, const iterator &Y) {
395 return X.Position > Y.Position;
398 friend iterator& operator+=(iterator &X, difference_type D) {
403 friend iterator& operator-=(iterator &X, difference_type D) {
408 friend iterator operator+(iterator X, difference_type D) {
413 friend iterator operator+(difference_type D, iterator X) {
418 friend difference_type operator-(const iterator &X, const iterator &Y) {
419 return X.Position - Y.Position;
422 friend iterator operator-(iterator X, difference_type D) {
427 friend class iterator;
429 iterator begin(Source *source, bool LocalOnly = false) {
431 return iterator(this, 0);
434 (source->*Loader)(Loaded);
435 return iterator(this, -(int)Loaded.size());
439 return iterator(this, Local.size());
442 void push_back(const T& LocalValue) {
443 Local.push_back(LocalValue);
446 void erase(iterator From, iterator To) {
447 if (From.Position < 0 && To.Position < 0) {
448 Loaded.erase(Loaded.end() + From.Position, Loaded.end() + To.Position);
452 if (From.Position < 0) {
453 Loaded.erase(Loaded.end() + From.Position, Loaded.end());
454 From = begin(0, true);
457 Local.erase(Local.begin() + From.Position, Local.begin() + To.Position);
461 /// \brief A lazy pointer to a statement.
462 typedef LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>
465 /// \brief A lazy pointer to a declaration.
466 typedef LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>
469 /// \brief A lazy pointer to a set of CXXBaseSpecifiers.
470 typedef LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
471 &ExternalASTSource::GetExternalCXXBaseSpecifiers>
472 LazyCXXBaseSpecifiersPtr;
474 } // end namespace clang
476 #endif // LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H