ExternalASTSource.h revision c304d9058ad69ff1fcbc6d19ca0be91acfa243a2
1//===--- ExternalASTSource.h - Abstract External AST Interface --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the ExternalASTSource interface, which enables
11//  construction of AST nodes from some external source.
12//
13//===----------------------------------------------------------------------===//
14#ifndef LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H
15#define LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H
16
17#include "clang/AST/DeclBase.h"
18#include <cassert>
19
20namespace llvm {
21template <class T> class SmallVectorImpl;
22}
23
24namespace clang {
25
26class ASTConsumer;
27class CXXBaseSpecifier;
28class DeclarationName;
29class ExternalSemaSource; // layering violation required for downcasting
30class NamedDecl;
31class Selector;
32class Stmt;
33class TagDecl;
34
35/// \brief Abstract interface for external sources of AST nodes.
36///
37/// External AST sources provide AST nodes constructed from some
38/// external source, such as a precompiled header. External AST
39/// sources can resolve types and declarations from abstract IDs into
40/// actual type and declaration nodes, and read parts of declaration
41/// contexts.
42class ExternalASTSource {
43  /// \brief Whether this AST source also provides information for
44  /// semantic analysis.
45  bool SemaSource;
46
47  friend class ExternalSemaSource;
48
49public:
50  ExternalASTSource() : SemaSource(false) { }
51
52  virtual ~ExternalASTSource();
53
54  /// \brief RAII class for safely pairing a StartedDeserializing call
55  /// with FinishedDeserializing.
56  class Deserializing {
57    ExternalASTSource *Source;
58  public:
59    explicit Deserializing(ExternalASTSource *source) : Source(source) {
60      assert(Source);
61      Source->StartedDeserializing();
62    }
63    ~Deserializing() {
64      Source->FinishedDeserializing();
65    }
66  };
67
68  /// \brief Resolve a declaration ID into a declaration, potentially
69  /// building a new declaration.
70  ///
71  /// This method only needs to be implemented if the AST source ever
72  /// passes back decl sets as VisibleDeclaration objects.
73  ///
74  /// The default implementation of this method is a no-op.
75  virtual Decl *GetExternalDecl(uint32_t ID);
76
77  /// \brief Resolve a selector ID into a selector.
78  ///
79  /// This operation only needs to be implemented if the AST source
80  /// returns non-zero for GetNumKnownSelectors().
81  ///
82  /// The default implementation of this method is a no-op.
83  virtual Selector GetExternalSelector(uint32_t ID);
84
85  /// \brief Returns the number of selectors known to the external AST
86  /// source.
87  ///
88  /// The default implementation of this method is a no-op.
89  virtual uint32_t GetNumExternalSelectors();
90
91  /// \brief Resolve the offset of a statement in the decl stream into
92  /// a statement.
93  ///
94  /// This operation is meant to be used via a LazyOffsetPtr.  It only
95  /// needs to be implemented if the AST source uses methods like
96  /// FunctionDecl::setLazyBody when building decls.
97  ///
98  /// The default implementation of this method is a no-op.
99  virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
100
101  /// \brief Resolve the offset of a set of C++ base specifiers in the decl
102  /// stream into an array of specifiers.
103  ///
104  /// The default implementation of this method is a no-op.
105  virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
106
107  /// \brief Finds all declarations with the given name in the
108  /// given context.
109  ///
110  /// Generally the final step of this method is either to call
111  /// SetExternalVisibleDeclsForName or to recursively call lookup on
112  /// the DeclContext after calling SetExternalVisibleDecls.
113  ///
114  /// The default implementation of this method is a no-op.
115  virtual DeclContextLookupResult
116  FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
117
118  /// \brief Deserialize all the visible declarations from external storage.
119  ///
120  /// Name lookup deserializes visible declarations lazily, thus a DeclContext
121  /// may not have a complete name lookup table. This function deserializes
122  /// the rest of visible declarations from the external storage and completes
123  /// the name lookup table of the DeclContext.
124  ///
125  /// The default implementation of this method is a no-op.
126  virtual void MaterializeVisibleDecls(const DeclContext *DC);
127
128  /// \brief Finds all declarations lexically contained within the given
129  /// DeclContext, after applying an optional filter predicate.
130  ///
131  /// \param isKindWeWant a predicate function that returns true if the passed
132  /// declaration kind is one we are looking for. If NULL, all declarations
133  /// are returned.
134  ///
135  /// \return true if an error occurred
136  ///
137  /// The default implementation of this method is a no-op.
138  virtual bool FindExternalLexicalDecls(const DeclContext *DC,
139                                        bool (*isKindWeWant)(Decl::Kind),
140                                        llvm::SmallVectorImpl<Decl*> &Result);
141
142  /// \brief Finds all declarations lexically contained within the given
143  /// DeclContext.
144  ///
145  /// \return true if an error occurred
146  bool FindExternalLexicalDecls(const DeclContext *DC,
147                                llvm::SmallVectorImpl<Decl*> &Result) {
148    return FindExternalLexicalDecls(DC, 0, Result);
149  }
150
151  template <typename DeclTy>
152  bool FindExternalLexicalDeclsBy(const DeclContext *DC,
153                                llvm::SmallVectorImpl<Decl*> &Result) {
154    return FindExternalLexicalDecls(DC, DeclTy::classofKind, Result);
155  }
156
157  /// \brief Gives the external AST source an opportunity to complete
158  /// an incomplete type.
159  virtual void CompleteType(TagDecl *Tag) {}
160
161  /// \brief Gives the external AST source an opportunity to complete an
162  /// incomplete Objective-C class.
163  ///
164  /// This routine will only be invoked if the "externally completed" bit is
165  /// set on the ObjCInterfaceDecl via the function
166  /// \c ObjCInterfaceDecl::setExternallyCompleted().
167  virtual void CompleteType(ObjCInterfaceDecl *Class) { }
168
169  /// \brief Notify ExternalASTSource that we started deserialization of
170  /// a decl or type so until FinishedDeserializing is called there may be
171  /// decls that are initializing. Must be paired with FinishedDeserializing.
172  ///
173  /// The default implementation of this method is a no-op.
174  virtual void StartedDeserializing() { }
175
176  /// \brief Notify ExternalASTSource that we finished the deserialization of
177  /// a decl or type. Must be paired with StartedDeserializing.
178  ///
179  /// The default implementation of this method is a no-op.
180  virtual void FinishedDeserializing() { }
181
182  /// \brief Function that will be invoked when we begin parsing a new
183  /// translation unit involving this external AST source.
184  ///
185  /// The default implementation of this method is a no-op.
186  virtual void StartTranslationUnit(ASTConsumer *Consumer) { }
187
188  /// \brief Print any statistics that have been gathered regarding
189  /// the external AST source.
190  ///
191  /// The default implementation of this method is a no-op.
192  virtual void PrintStats();
193
194protected:
195  static DeclContextLookupResult
196  SetExternalVisibleDeclsForName(const DeclContext *DC,
197                                 DeclarationName Name,
198                                 llvm::SmallVectorImpl<NamedDecl*> &Decls);
199
200  static DeclContextLookupResult
201  SetNoExternalVisibleDeclsForName(const DeclContext *DC,
202                                   DeclarationName Name);
203
204  void MaterializeVisibleDeclsForName(const DeclContext *DC,
205                                      DeclarationName Name,
206                                 llvm::SmallVectorImpl<NamedDecl*> &Decls);
207};
208
209/// \brief A lazy pointer to an AST node (of base type T) that resides
210/// within an external AST source.
211///
212/// The AST node is identified within the external AST source by a
213/// 63-bit offset, and can be retrieved via an operation on the
214/// external AST source itself.
215template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
216struct LazyOffsetPtr {
217  /// \brief Either a pointer to an AST node or the offset within the
218  /// external AST source where the AST node can be found.
219  ///
220  /// If the low bit is clear, a pointer to the AST node. If the low
221  /// bit is set, the upper 63 bits are the offset.
222  mutable uint64_t Ptr;
223
224public:
225  LazyOffsetPtr() : Ptr(0) { }
226
227  explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
228  explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
229    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
230    if (Offset == 0)
231      Ptr = 0;
232  }
233
234  LazyOffsetPtr &operator=(T *Ptr) {
235    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
236    return *this;
237  }
238
239  LazyOffsetPtr &operator=(uint64_t Offset) {
240    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
241    if (Offset == 0)
242      Ptr = 0;
243    else
244      Ptr = (Offset << 1) | 0x01;
245
246    return *this;
247  }
248
249  /// \brief Whether this pointer is non-NULL.
250  ///
251  /// This operation does not require the AST node to be deserialized.
252  operator bool() const { return Ptr != 0; }
253
254  /// \brief Whether this pointer is currently stored as an offset.
255  bool isOffset() const { return Ptr & 0x01; }
256
257  /// \brief Retrieve the pointer to the AST node that this lazy pointer
258  ///
259  /// \param Source the external AST source.
260  ///
261  /// \returns a pointer to the AST node.
262  T* get(ExternalASTSource *Source) const {
263    if (isOffset()) {
264      assert(Source &&
265             "Cannot deserialize a lazy pointer without an AST source");
266      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
267    }
268    return reinterpret_cast<T*>(Ptr);
269  }
270};
271
272/// \brief A lazy pointer to a statement.
273typedef LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>
274  LazyDeclStmtPtr;
275
276/// \brief A lazy pointer to a declaration.
277typedef LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>
278  LazyDeclPtr;
279
280/// \brief A lazy pointer to a set of CXXBaseSpecifiers.
281typedef LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
282                      &ExternalASTSource::GetExternalCXXBaseSpecifiers>
283  LazyCXXBaseSpecifiersPtr;
284
285} // end namespace clang
286
287#endif // LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H
288