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.x
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H
15 #define LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H
21 template <class T> class SmallVectorImpl;
29 class DeclContextLookupResult;
30 class DeclarationName;
31 class ExternalSemaSource; // layering violation required for downcasting
36 /// \brief Abstract interface for external sources of AST nodes.
38 /// External AST sources provide AST nodes constructed from some
39 /// external source, such as a precompiled header. External AST
40 /// sources can resolve types and declarations from abstract IDs into
41 /// actual type and declaration nodes, and read parts of declaration
43 class ExternalASTSource {
44 /// \brief Whether this AST source also provides information for
45 /// semantic analysis.
48 friend class ExternalSemaSource;
51 ExternalASTSource() : SemaSource(false) { }
53 virtual ~ExternalASTSource();
55 /// \brief RAII class for safely pairing a StartedDeserializing call
56 /// with FinishedDeserializing.
58 ExternalASTSource *Source;
60 explicit Deserializing(ExternalASTSource *source) : Source(source) {
62 Source->StartedDeserializing();
65 Source->FinishedDeserializing();
69 /// \brief Resolve a declaration ID into a declaration, potentially
70 /// building a new declaration.
72 /// This method only needs to be implemented if the AST source ever
73 /// passes back decl sets as VisibleDeclaration objects.
74 virtual Decl *GetExternalDecl(uint32_t ID) = 0;
76 /// \brief Resolve a selector ID into a selector.
78 /// This operation only needs to be implemented if the AST source
79 /// returns non-zero for GetNumKnownSelectors().
80 virtual Selector GetExternalSelector(uint32_t ID) = 0;
82 /// \brief Returns the number of selectors known to the external AST
84 virtual uint32_t GetNumExternalSelectors() = 0;
86 /// \brief Resolve the offset of a statement in the decl stream into
89 /// This operation is meant to be used via a LazyOffsetPtr. It only
90 /// needs to be implemented if the AST source uses methods like
91 /// FunctionDecl::setLazyBody when building decls.
92 virtual Stmt *GetExternalDeclStmt(uint64_t Offset) = 0;
94 /// \brief Finds all declarations with the given name in the
97 /// Generally the final step of this method is either to call
98 /// SetExternalVisibleDeclsForName or to recursively call lookup on
99 /// the DeclContext after calling SetExternalVisibleDecls.
100 virtual DeclContextLookupResult
101 FindExternalVisibleDeclsByName(const DeclContext *DC,
102 DeclarationName Name) = 0;
104 /// \brief Deserialize all the visible declarations from external storage.
106 /// Name lookup deserializes visible declarations lazily, thus a DeclContext
107 /// may not have a complete name lookup table. This function deserializes
108 /// the rest of visible declarations from the external storage and completes
109 /// the name lookup table of the DeclContext.
110 virtual void MaterializeVisibleDecls(const DeclContext *DC) = 0;
112 /// \brief Finds all declarations lexically contained within the given
115 /// \return true if an error occurred
116 virtual bool FindExternalLexicalDecls(const DeclContext *DC,
117 llvm::SmallVectorImpl<Decl*> &Result) = 0;
119 /// \brief Notify ExternalASTSource that we started deserialization of
120 /// a decl or type so until FinishedDeserializing is called there may be
121 /// decls that are initializing. Must be paired with FinishedDeserializing.
123 /// The default implementation of this method is a no-op.
124 virtual void StartedDeserializing() { }
126 /// \brief Notify ExternalASTSource that we finished the deserialization of
127 /// a decl or type. Must be paired with StartedDeserializing.
129 /// The default implementation of this method is a no-op.
130 virtual void FinishedDeserializing() { }
132 /// \brief Function that will be invoked when we begin parsing a new
133 /// translation unit involving this external AST source.
135 /// The default implementation of this method is a no-op.
136 virtual void StartTranslationUnit(ASTConsumer *Consumer) { }
138 /// \brief Print any statistics that have been gathered regarding
139 /// the external AST source.
141 /// The default implementation of this method is a no-op.
142 virtual void PrintStats();
145 static DeclContextLookupResult
146 SetExternalVisibleDeclsForName(const DeclContext *DC,
147 DeclarationName Name,
148 llvm::SmallVectorImpl<NamedDecl*> &Decls);
150 static DeclContextLookupResult
151 SetNoExternalVisibleDeclsForName(const DeclContext *DC,
152 DeclarationName Name);
154 void MaterializeVisibleDeclsForName(const DeclContext *DC,
155 DeclarationName Name,
156 llvm::SmallVectorImpl<NamedDecl*> &Decls);
159 /// \brief A lazy pointer to an AST node (of base type T) that resides
160 /// within an external AST source.
162 /// The AST node is identified within the external AST source by a
163 /// 63-bit offset, and can be retrieved via an operation on the
164 /// external AST source itself.
165 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
166 struct LazyOffsetPtr {
167 /// \brief Either a pointer to an AST node or the offset within the
168 /// external AST source where the AST node can be found.
170 /// If the low bit is clear, a pointer to the AST node. If the low
171 /// bit is set, the upper 63 bits are the offset.
172 mutable uint64_t Ptr;
175 LazyOffsetPtr() : Ptr(0) { }
177 explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
178 explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
179 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
184 LazyOffsetPtr &operator=(T *Ptr) {
185 this->Ptr = reinterpret_cast<uint64_t>(Ptr);
189 LazyOffsetPtr &operator=(uint64_t Offset) {
190 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
194 Ptr = (Offset << 1) | 0x01;
199 /// \brief Whether this pointer is non-NULL.
201 /// This operation does not require the AST node to be deserialized.
202 operator bool() const { return Ptr != 0; }
204 /// \brief Whether this pointer is currently stored as an offset.
205 bool isOffset() const { return Ptr & 0x01; }
207 /// \brief Retrieve the pointer to the AST node that this lazy pointer
209 /// \param Source the external AST source.
211 /// \returns a pointer to the AST node.
212 T* get(ExternalASTSource *Source) const {
215 "Cannot deserialize a lazy pointer without an AST source");
216 Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
218 return reinterpret_cast<T*>(Ptr);
222 /// \brief A lazy pointer to a statement.
223 typedef LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>
226 /// \brief A lazy pointer to a declaration.
227 typedef LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>
230 } // end namespace clang
232 #endif // LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H