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