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