xref: /llvm-project/clang/include/clang/Sema/MultiplexExternalSemaSource.h (revision 263fed7ce9d2c155af44829018673caa67fa4f47)
1 //===--- MultiplexExternalSemaSource.h - External Sema Interface-*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines ExternalSemaSource interface, dispatching to all clients
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_CLANG_SEMA_MULTIPLEXEXTERNALSEMASOURCE_H
13 #define LLVM_CLANG_SEMA_MULTIPLEXEXTERNALSEMASOURCE_H
14 
15 #include "clang/Sema/ExternalSemaSource.h"
16 #include "clang/Sema/Weak.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include <utility>
19 
20 namespace clang {
21 
22   class CXXConstructorDecl;
23   class CXXRecordDecl;
24   class DeclaratorDecl;
25   struct ExternalVTableUse;
26   class LookupResult;
27   class NamespaceDecl;
28   class Scope;
29   class Sema;
30   class TypedefNameDecl;
31   class ValueDecl;
32   class VarDecl;
33 
34 
35 /// An abstract interface that should be implemented by
36 /// external AST sources that also provide information for semantic
37 /// analysis.
38 class MultiplexExternalSemaSource : public ExternalSemaSource {
39   /// LLVM-style RTTI.
40   static char ID;
41 
42 private:
43   SmallVector<ExternalSemaSource *, 2> Sources;
44 
45 public:
46   /// 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() override;
55 
56   /// 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   /// Resolve a declaration ID into a declaration, potentially
67   /// building a new declaration.
68   Decl *GetExternalDecl(GlobalDeclID ID) override;
69 
70   /// Complete the redeclaration chain if it's been extended since the
71   /// previous generation of the AST source.
72   void CompleteRedeclChain(const Decl *D) override;
73 
74   /// Resolve a selector ID into a selector.
75   Selector GetExternalSelector(uint32_t ID) override;
76 
77   /// Returns the number of selectors known to the external AST
78   /// source.
79   uint32_t GetNumExternalSelectors() override;
80 
81   /// Resolve the offset of a statement in the decl stream into
82   /// a statement.
83   Stmt *GetExternalDeclStmt(uint64_t Offset) override;
84 
85   /// Resolve the offset of a set of C++ base specifiers in the decl
86   /// stream into an array of specifiers.
87   CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override;
88 
89   /// Resolve a handle to a list of ctor initializers into the list of
90   /// initializers themselves.
91   CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset) override;
92 
93   ExtKind hasExternalDefinitions(const Decl *D) override;
94 
95   /// Find all declarations with the given name in the
96   /// given context.
97   bool FindExternalVisibleDeclsByName(const DeclContext *DC,
98                                       DeclarationName Name,
99                                       const DeclContext *OriginalDC) override;
100 
101   bool LoadExternalSpecializations(const Decl *D, bool OnlyPartial) override;
102 
103   bool
104   LoadExternalSpecializations(const Decl *D,
105                               ArrayRef<TemplateArgument> TemplateArgs) override;
106 
107   /// Ensures that the table of all visible declarations inside this
108   /// context is up to date.
109   void completeVisibleDeclsMap(const DeclContext *DC) override;
110 
111   /// Finds all declarations lexically contained within the given
112   /// DeclContext, after applying an optional filter predicate.
113   ///
114   /// \param IsKindWeWant a predicate function that returns true if the passed
115   /// declaration kind is one we are looking for.
116   void
117   FindExternalLexicalDecls(const DeclContext *DC,
118                            llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
119                            SmallVectorImpl<Decl *> &Result) override;
120 
121   /// Get the decls that are contained in a file in the Offset/Length
122   /// range. \p Length can be 0 to indicate a point at \p Offset instead of
123   /// a range.
124   void FindFileRegionDecls(FileID File, unsigned Offset,unsigned Length,
125                            SmallVectorImpl<Decl *> &Decls) override;
126 
127   /// Gives the external AST source an opportunity to complete
128   /// an incomplete type.
129   void CompleteType(TagDecl *Tag) override;
130 
131   /// Gives the external AST source an opportunity to complete an
132   /// incomplete Objective-C class.
133   ///
134   /// This routine will only be invoked if the "externally completed" bit is
135   /// set on the ObjCInterfaceDecl via the function
136   /// \c ObjCInterfaceDecl::setExternallyCompleted().
137   void CompleteType(ObjCInterfaceDecl *Class) override;
138 
139   /// Loads comment ranges.
140   void ReadComments() override;
141 
142   /// Notify ExternalASTSource that we started deserialization of
143   /// a decl or type so until FinishedDeserializing is called there may be
144   /// decls that are initializing. Must be paired with FinishedDeserializing.
145   void StartedDeserializing() override;
146 
147   /// Notify ExternalASTSource that we finished the deserialization of
148   /// a decl or type. Must be paired with StartedDeserializing.
149   void FinishedDeserializing() override;
150 
151   /// Function that will be invoked when we begin parsing a new
152   /// translation unit involving this external AST source.
153   void StartTranslationUnit(ASTConsumer *Consumer) override;
154 
155   /// Print any statistics that have been gathered regarding
156   /// the external AST source.
157   void PrintStats() override;
158 
159   /// Retrieve the module that corresponds to the given module ID.
160   Module *getModule(unsigned ID) override;
161 
162   /// Perform layout on the given record.
163   ///
164   /// This routine allows the external AST source to provide an specific
165   /// layout for a record, overriding the layout that would normally be
166   /// constructed. It is intended for clients who receive specific layout
167   /// details rather than source code (such as LLDB). The client is expected
168   /// to fill in the field offsets, base offsets, virtual base offsets, and
169   /// complete object size.
170   ///
171   /// \param Record The record whose layout is being requested.
172   ///
173   /// \param Size The final size of the record, in bits.
174   ///
175   /// \param Alignment The final alignment of the record, in bits.
176   ///
177   /// \param FieldOffsets The offset of each of the fields within the record,
178   /// expressed in bits. All of the fields must be provided with offsets.
179   ///
180   /// \param BaseOffsets The offset of each of the direct, non-virtual base
181   /// classes. If any bases are not given offsets, the bases will be laid
182   /// out according to the ABI.
183   ///
184   /// \param VirtualBaseOffsets The offset of each of the virtual base classes
185   /// (either direct or not). If any bases are not given offsets, the bases will
186   /// be laid out according to the ABI.
187   ///
188   /// \returns true if the record layout was provided, false otherwise.
189   bool
190   layoutRecordType(const RecordDecl *Record,
191                    uint64_t &Size, uint64_t &Alignment,
192                    llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
193                  llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
194                  llvm::DenseMap<const CXXRecordDecl *,
195                                 CharUnits> &VirtualBaseOffsets) override;
196 
197   /// Return the amount of memory used by memory buffers, breaking down
198   /// by heap-backed versus mmap'ed memory.
199   void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override;
200 
201   //===--------------------------------------------------------------------===//
202   // ExternalSemaSource.
203   //===--------------------------------------------------------------------===//
204 
205   /// Initialize the semantic source with the Sema instance
206   /// being used to perform semantic analysis on the abstract syntax
207   /// tree.
208   void InitializeSema(Sema &S) override;
209 
210   /// Inform the semantic consumer that Sema is no longer available.
211   void ForgetSema() override;
212 
213   /// Load the contents of the global method pool for a given
214   /// selector.
215   void ReadMethodPool(Selector Sel) override;
216 
217   /// Load the contents of the global method pool for a given
218   /// selector if necessary.
219   void updateOutOfDateSelector(Selector Sel) override;
220 
221   /// Load the set of namespaces that are known to the external source,
222   /// which will be used during typo correction.
223   void
224   ReadKnownNamespaces(SmallVectorImpl<NamespaceDecl*> &Namespaces) override;
225 
226   /// Load the set of used but not defined functions or variables with
227   /// internal linkage, or used but not defined inline functions.
228   void ReadUndefinedButUsed(
229       llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) override;
230 
231   void ReadMismatchingDeleteExpressions(llvm::MapVector<
232       FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
233                                             Exprs) override;
234 
235   /// Do last resort, unqualified lookup on a LookupResult that
236   /// Sema cannot find.
237   ///
238   /// \param R a LookupResult that is being recovered.
239   ///
240   /// \param S the Scope of the identifier occurrence.
241   ///
242   /// \return true to tell Sema to recover using the LookupResult.
243   bool LookupUnqualified(LookupResult &R, Scope *S) override;
244 
245   /// Read the set of tentative definitions known to the external Sema
246   /// source.
247   ///
248   /// The external source should append its own tentative definitions to the
249   /// given vector of tentative definitions. Note that this routine may be
250   /// invoked multiple times; the external source should take care not to
251   /// introduce the same declarations repeatedly.
252   void ReadTentativeDefinitions(SmallVectorImpl<VarDecl*> &Defs) override;
253 
254   /// Read the set of unused file-scope declarations known to the
255   /// external Sema source.
256   ///
257   /// The external source should append its own unused, filed-scope to the
258   /// given vector of declarations. Note that this routine may be
259   /// invoked multiple times; the external source should take care not to
260   /// introduce the same declarations repeatedly.
261   void ReadUnusedFileScopedDecls(
262                         SmallVectorImpl<const DeclaratorDecl*> &Decls) override;
263 
264   /// Read the set of delegating constructors known to the
265   /// external Sema source.
266   ///
267   /// The external source should append its own delegating constructors to the
268   /// given vector of declarations. Note that this routine may be
269   /// invoked multiple times; the external source should take care not to
270   /// introduce the same declarations repeatedly.
271   void ReadDelegatingConstructors(
272                           SmallVectorImpl<CXXConstructorDecl*> &Decls) override;
273 
274   /// Read the set of ext_vector type declarations known to the
275   /// external Sema source.
276   ///
277   /// The external source should append its own ext_vector type declarations to
278   /// the given vector of declarations. Note that this routine may be
279   /// invoked multiple times; the external source should take care not to
280   /// introduce the same declarations repeatedly.
281   void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl*> &Decls) override;
282 
283   /// Read the set of potentially unused typedefs known to the source.
284   ///
285   /// The external source should append its own potentially unused local
286   /// typedefs to the given vector of declarations. Note that this routine may
287   /// be invoked multiple times; the external source should take care not to
288   /// introduce the same declarations repeatedly.
289   void ReadUnusedLocalTypedefNameCandidates(
290       llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) override;
291 
292   /// Read the set of referenced selectors known to the
293   /// external Sema source.
294   ///
295   /// The external source should append its own referenced selectors to the
296   /// given vector of selectors. Note that this routine
297   /// may be invoked multiple times; the external source should take care not
298   /// to introduce the same selectors repeatedly.
299   void ReadReferencedSelectors(SmallVectorImpl<std::pair<Selector,
300                                               SourceLocation> > &Sels) override;
301 
302   /// Read the set of weak, undeclared identifiers known to the
303   /// external Sema source.
304   ///
305   /// The external source should append its own weak, undeclared identifiers to
306   /// the given vector. Note that this routine may be invoked multiple times;
307   /// the external source should take care not to introduce the same identifiers
308   /// repeatedly.
309   void ReadWeakUndeclaredIdentifiers(
310            SmallVectorImpl<std::pair<IdentifierInfo*, WeakInfo> > &WI) override;
311 
312   /// Read the set of used vtables known to the external Sema source.
313   ///
314   /// The external source should append its own used vtables to the given
315   /// vector. Note that this routine may be invoked multiple times; the external
316   /// source should take care not to introduce the same vtables repeatedly.
317   void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) override;
318 
319   /// Read the set of pending instantiations known to the external
320   /// Sema source.
321   ///
322   /// The external source should append its own pending instantiations to the
323   /// given vector. Note that this routine may be invoked multiple times; the
324   /// external source should take care not to introduce the same instantiations
325   /// repeatedly.
326   void ReadPendingInstantiations(
327      SmallVectorImpl<std::pair<ValueDecl*, SourceLocation> >& Pending) override;
328 
329   /// Read the set of late parsed template functions for this source.
330   ///
331   /// The external source should insert its own late parsed template functions
332   /// into the map. Note that this routine may be invoked multiple times; the
333   /// external source should take care not to introduce the same map entries
334   /// repeatedly.
335   void ReadLateParsedTemplates(
336       llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
337           &LPTMap) override;
338 
339   /// Read the set of decls to be checked for deferred diags.
340   ///
341   /// The external source should append its own potentially emitted function
342   /// and variable decls which may cause deferred diags. Note that this routine
343   /// may be invoked multiple times; the external source should take care not to
344   /// introduce the same declarations repeatedly.
345   void ReadDeclsToCheckForDeferredDiags(
346       llvm::SmallSetVector<Decl *, 4> &Decls) override;
347 
348   /// \copydoc ExternalSemaSource::CorrectTypo
349   /// \note Returns the first nonempty correction.
350   TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo,
351                              int LookupKind, Scope *S, CXXScopeSpec *SS,
352                              CorrectionCandidateCallback &CCC,
353                              DeclContext *MemberContext,
354                              bool EnteringContext,
355                              const ObjCObjectPointerType *OPT) override;
356 
357   /// Produces a diagnostic note if one of the attached sources
358   /// contains a complete definition for \p T. Queries the sources in list
359   /// order until the first one claims that a diagnostic was produced.
360   ///
361   /// \param Loc the location at which a complete type was required but not
362   /// provided
363   ///
364   /// \param T the \c QualType that should have been complete at \p Loc
365   ///
366   /// \return true if a diagnostic was produced, false otherwise.
367   bool MaybeDiagnoseMissingCompleteType(SourceLocation Loc,
368                                         QualType T) override;
369 
370   // Inform all attached sources that a mangling number was assigned.
371   void AssignedLambdaNumbering(CXXRecordDecl *Lambda) override;
372 
373   /// LLVM-style RTTI.
374   /// \{
375   bool isA(const void *ClassID) const override {
376     return ClassID == &ID || ExternalSemaSource::isA(ClassID);
377   }
378   static bool classof(const ExternalASTSource *S) { return S->isA(&ID); }
379   /// \}
380 };
381 
382 } // end namespace clang
383 
384 #endif
385