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