MultiplexExternalSemaSource.h revision cfa88f893915ceb8ae4ce2f17c46c24a4d67502f
1//===--- MultiplexExternalSemaSource.h - External Sema 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 ExternalSemaSource interface, dispatching to all clients
11//
12//===----------------------------------------------------------------------===//
13#ifndef LLVM_CLANG_SEMA_MULTIPLEX_EXTERNAL_SEMA_SOURCE_H
14#define LLVM_CLANG_SEMA_MULTIPLEX_EXTERNAL_SEMA_SOURCE_H
15
16#include "clang/Sema/ExternalSemaSource.h"
17#include "clang/Sema/Weak.h"
18#include "llvm/ADT/SmallVector.h"
19#include <utility>
20
21namespace clang {
22
23  class CXXConstructorDecl;
24  class CXXRecordDecl;
25  class DeclaratorDecl;
26  struct ExternalVTableUse;
27  class LookupResult;
28  class NamespaceDecl;
29  class Scope;
30  class Sema;
31  class TypedefNameDecl;
32  class ValueDecl;
33  class VarDecl;
34
35
36/// \brief An abstract interface that should be implemented by
37/// external AST sources that also provide information for semantic
38/// analysis.
39class MultiplexExternalSemaSource : public ExternalSemaSource {
40
41private:
42  SmallVector<ExternalSemaSource *, 2> Sources; // doesn't own them.
43
44public:
45
46  ///\brief Constructs a new multiplexing external sema source and appends the
47  /// given element to it.
48  ///
49  ///\param[in] s1 - A non-null (old) ExternalSemaSource.
50  ///\param[in] s2 - A non-null (new) ExternalSemaSource.
51  ///
52  MultiplexExternalSemaSource(ExternalSemaSource& s1, ExternalSemaSource& s2);
53
54  ~MultiplexExternalSemaSource();
55
56  ///\brief Appends new source to the source list.
57  ///
58  ///\param[in] source - An ExternalSemaSource.
59  ///
60  void addSource(ExternalSemaSource &source);
61
62  //===--------------------------------------------------------------------===//
63  // ExternalASTSource.
64  //===--------------------------------------------------------------------===//
65
66  /// \brief Resolve a declaration ID into a declaration, potentially
67  /// building a new declaration.
68  ///
69  /// This method only needs to be implemented if the AST source ever
70  /// passes back decl sets as VisibleDeclaration objects.
71  ///
72  /// The default implementation of this method is a no-op.
73  virtual Decl *GetExternalDecl(uint32_t ID);
74
75  /// \brief Resolve a selector ID into a selector.
76  ///
77  /// This operation only needs to be implemented if the AST source
78  /// returns non-zero for GetNumKnownSelectors().
79  ///
80  /// The default implementation of this method is a no-op.
81  virtual Selector GetExternalSelector(uint32_t ID);
82
83  /// \brief Returns the number of selectors known to the external AST
84  /// source.
85  ///
86  /// The default implementation of this method is a no-op.
87  virtual uint32_t GetNumExternalSelectors();
88
89  /// \brief Resolve the offset of a statement in the decl stream into
90  /// a statement.
91  ///
92  /// This operation is meant to be used via a LazyOffsetPtr.  It only
93  /// needs to be implemented if the AST source uses methods like
94  /// FunctionDecl::setLazyBody when building decls.
95  ///
96  /// The default implementation of this method is a no-op.
97  virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
98
99  /// \brief Resolve the offset of a set of C++ base specifiers in the decl
100  /// stream into an array of specifiers.
101  ///
102  /// The default implementation of this method is a no-op.
103  virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
104
105  /// \brief Finds all declarations with the given name in the
106  /// given context.
107  ///
108  /// Generally the final step of this method is either to call
109  /// SetExternalVisibleDeclsForName or to recursively call lookup on
110  /// the DeclContext after calling SetExternalVisibleDecls.
111  ///
112  /// The default implementation of this method is a no-op.
113  virtual DeclContextLookupResult
114  FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
115
116  /// \brief Ensures that the table of all visible declarations inside this
117  /// context is up to date.
118  ///
119  /// The default implementation of this functino is a no-op.
120  virtual void completeVisibleDeclsMap(const DeclContext *DC);
121
122  /// \brief Finds all declarations lexically contained within the given
123  /// DeclContext, after applying an optional filter predicate.
124  ///
125  /// \param isKindWeWant a predicate function that returns true if the passed
126  /// declaration kind is one we are looking for. If NULL, all declarations
127  /// are returned.
128  ///
129  /// \return an indication of whether the load succeeded or failed.
130  ///
131  /// The default implementation of this method is a no-op.
132  virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
133                                        bool (*isKindWeWant)(Decl::Kind),
134                                        SmallVectorImpl<Decl*> &Result);
135
136  /// \brief Finds all declarations lexically contained within the given
137  /// DeclContext.
138  ///
139  /// \return true if an error occurred
140  ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
141                                SmallVectorImpl<Decl*> &Result) {
142    return FindExternalLexicalDecls(DC, 0, Result);
143  }
144
145  template <typename DeclTy>
146  ExternalLoadResult FindExternalLexicalDeclsBy(const DeclContext *DC,
147                                  SmallVectorImpl<Decl*> &Result) {
148    return FindExternalLexicalDecls(DC, DeclTy::classofKind, Result);
149  }
150
151  /// \brief Get the decls that are contained in a file in the Offset/Length
152  /// range. \p Length can be 0 to indicate a point at \p Offset instead of
153  /// a range.
154  virtual void FindFileRegionDecls(FileID File, unsigned Offset,unsigned Length,
155                                   SmallVectorImpl<Decl *> &Decls);
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 Loads comment ranges.
170  virtual void ReadComments();
171
172  /// \brief Notify ExternalASTSource that we started deserialization of
173  /// a decl or type so until FinishedDeserializing is called there may be
174  /// decls that are initializing. Must be paired with FinishedDeserializing.
175  ///
176  /// The default implementation of this method is a no-op.
177  virtual void StartedDeserializing();
178
179  /// \brief Notify ExternalASTSource that we finished the deserialization of
180  /// a decl or type. Must be paired with StartedDeserializing.
181  ///
182  /// The default implementation of this method is a no-op.
183  virtual void FinishedDeserializing();
184
185  /// \brief Function that will be invoked when we begin parsing a new
186  /// translation unit involving this external AST source.
187  ///
188  /// The default implementation of this method is a no-op.
189  virtual void StartTranslationUnit(ASTConsumer *Consumer);
190
191  /// \brief Print any statistics that have been gathered regarding
192  /// the external AST source.
193  ///
194  /// The default implementation of this method is a no-op.
195  virtual void PrintStats();
196
197
198  /// \brief Perform layout on the given record.
199  ///
200  /// This routine allows the external AST source to provide an specific
201  /// layout for a record, overriding the layout that would normally be
202  /// constructed. It is intended for clients who receive specific layout
203  /// details rather than source code (such as LLDB). The client is expected
204  /// to fill in the field offsets, base offsets, virtual base offsets, and
205  /// complete object size.
206  ///
207  /// \param Record The record whose layout is being requested.
208  ///
209  /// \param Size The final size of the record, in bits.
210  ///
211  /// \param Alignment The final alignment of the record, in bits.
212  ///
213  /// \param FieldOffsets The offset of each of the fields within the record,
214  /// expressed in bits. All of the fields must be provided with offsets.
215  ///
216  /// \param BaseOffsets The offset of each of the direct, non-virtual base
217  /// classes. If any bases are not given offsets, the bases will be laid
218  /// out according to the ABI.
219  ///
220  /// \param VirtualBaseOffsets The offset of each of the virtual base classes
221  /// (either direct or not). If any bases are not given offsets, the bases will
222  /// be laid out according to the ABI.
223  ///
224  /// \returns true if the record layout was provided, false otherwise.
225  virtual bool
226  layoutRecordType(const RecordDecl *Record,
227                   uint64_t &Size, uint64_t &Alignment,
228                   llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
229                 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
230          llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets);
231
232  /// Return the amount of memory used by memory buffers, breaking down
233  /// by heap-backed versus mmap'ed memory.
234  virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
235
236  //===--------------------------------------------------------------------===//
237  // ExternalSemaSource.
238  //===--------------------------------------------------------------------===//
239
240  /// \brief Initialize the semantic source with the Sema instance
241  /// being used to perform semantic analysis on the abstract syntax
242  /// tree.
243  virtual void InitializeSema(Sema &S);
244
245  /// \brief Inform the semantic consumer that Sema is no longer available.
246  virtual void ForgetSema();
247
248  /// \brief Load the contents of the global method pool for a given
249  /// selector.
250  virtual void ReadMethodPool(Selector Sel);
251
252  /// \brief Load the set of namespaces that are known to the external source,
253  /// which will be used during typo correction.
254  virtual void ReadKnownNamespaces(SmallVectorImpl<NamespaceDecl*> &Namespaces);
255
256  /// \brief Do last resort, unqualified lookup on a LookupResult that
257  /// Sema cannot find.
258  ///
259  /// \param R a LookupResult that is being recovered.
260  ///
261  /// \param S the Scope of the identifier occurrence.
262  ///
263  /// \return true to tell Sema to recover using the LookupResult.
264  virtual bool LookupUnqualified(LookupResult &R, Scope *S);
265
266  /// \brief Read the set of tentative definitions known to the external Sema
267  /// source.
268  ///
269  /// The external source should append its own tentative definitions to the
270  /// given vector of tentative definitions. Note that this routine may be
271  /// invoked multiple times; the external source should take care not to
272  /// introduce the same declarations repeatedly.
273  virtual void ReadTentativeDefinitions(SmallVectorImpl<VarDecl*> &Defs);
274
275  /// \brief Read the set of unused file-scope declarations known to the
276  /// external Sema source.
277  ///
278  /// The external source should append its own unused, filed-scope to the
279  /// given vector of declarations. Note that this routine may be
280  /// invoked multiple times; the external source should take care not to
281  /// introduce the same declarations repeatedly.
282  virtual void ReadUnusedFileScopedDecls(
283                                 SmallVectorImpl<const DeclaratorDecl*> &Decls);
284
285  /// \brief Read the set of delegating constructors known to the
286  /// external Sema source.
287  ///
288  /// The external source should append its own delegating constructors to the
289  /// given vector of declarations. Note that this routine may be
290  /// invoked multiple times; the external source should take care not to
291  /// introduce the same declarations repeatedly.
292  virtual void ReadDelegatingConstructors(
293                                   SmallVectorImpl<CXXConstructorDecl*> &Decls);
294
295  /// \brief Read the set of ext_vector type declarations known to the
296  /// external Sema source.
297  ///
298  /// The external source should append its own ext_vector type declarations to
299  /// the given vector of declarations. Note that this routine may be
300  /// invoked multiple times; the external source should take care not to
301  /// introduce the same declarations repeatedly.
302  virtual void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl*> &Decls);
303
304  /// \brief Read the set of dynamic classes known to the external Sema source.
305  ///
306  /// The external source should append its own dynamic classes to
307  /// the given vector of declarations. Note that this routine may be
308  /// invoked multiple times; the external source should take care not to
309  /// introduce the same declarations repeatedly.
310  virtual void ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl*> &Decls);
311
312  /// \brief Read the set of locally-scoped extern "C" declarations known to the
313  /// external Sema source.
314  ///
315  /// The external source should append its own locally-scoped external
316  /// declarations to the given vector of declarations. Note that this routine
317  /// may be invoked multiple times; the external source should take care not
318  /// to introduce the same declarations repeatedly.
319  virtual void ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl*>&Decls);
320
321  /// \brief Read the set of referenced selectors known to the
322  /// external Sema source.
323  ///
324  /// The external source should append its own referenced selectors to the
325  /// given vector of selectors. Note that this routine
326  /// may be invoked multiple times; the external source should take care not
327  /// to introduce the same selectors repeatedly.
328  virtual void ReadReferencedSelectors(SmallVectorImpl<std::pair<Selector,
329                                                       SourceLocation> > &Sels);
330
331  /// \brief Read the set of weak, undeclared identifiers known to the
332  /// external Sema source.
333  ///
334  /// The external source should append its own weak, undeclared identifiers to
335  /// the given vector. Note that this routine may be invoked multiple times;
336  /// the external source should take care not to introduce the same identifiers
337  /// repeatedly.
338  virtual void ReadWeakUndeclaredIdentifiers(
339                    SmallVectorImpl<std::pair<IdentifierInfo*, WeakInfo> > &WI);
340
341  /// \brief Read the set of used vtables known to the external Sema source.
342  ///
343  /// The external source should append its own used vtables to the given
344  /// vector. Note that this routine may be invoked multiple times; the external
345  /// source should take care not to introduce the same vtables repeatedly.
346  virtual void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables);
347
348  /// \brief Read the set of pending instantiations known to the external
349  /// Sema source.
350  ///
351  /// The external source should append its own pending instantiations to the
352  /// given vector. Note that this routine may be invoked multiple times; the
353  /// external source should take care not to introduce the same instantiations
354  /// repeatedly.
355  virtual void ReadPendingInstantiations(
356              SmallVectorImpl<std::pair<ValueDecl*, SourceLocation> >& Pending);
357
358  // isa/cast/dyn_cast support
359  static bool classof(const MultiplexExternalSemaSource*) { return true; }
360  //static bool classof(const ExternalSemaSource*) { return true; }
361};
362
363} // end namespace clang
364
365#endif // LLVM_CLANG_SEMA_MULTIPLEX_EXTERNAL_SEMA_SOURCE_H
366