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