1 //===--- ExternalSemaSource.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 the ExternalSemaSource interface.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_CLANG_SEMA_EXTERNALSEMASOURCE_H
13 #define LLVM_CLANG_SEMA_EXTERNALSEMASOURCE_H
14 
15 #include "clang/AST/ExternalASTSource.h"
16 #include "clang/AST/Type.h"
17 #include "clang/Sema/TypoCorrection.h"
18 #include "clang/Sema/Weak.h"
19 #include "llvm/ADT/MapVector.h"
20 #include <utility>
21 
22 namespace llvm {
23 template <class T, unsigned n> class SmallSetVector;
24 }
25 
26 namespace clang {
27 
28 class CXXConstructorDecl;
29 class CXXRecordDecl;
30 class DeclaratorDecl;
31 class LookupResult;
32 class Scope;
33 class Sema;
34 class TypedefNameDecl;
35 class ValueDecl;
36 class VarDecl;
37 struct LateParsedTemplate;
38 
39 /// A simple structure that captures a vtable use for the purposes of
40 /// the \c ExternalSemaSource.
41 struct ExternalVTableUse {
42   CXXRecordDecl *Record;
43   SourceLocation Location;
44   bool DefinitionRequired;
45 };
46 
47 /// An abstract interface that should be implemented by
48 /// external AST sources that also provide information for semantic
49 /// analysis.
50 class ExternalSemaSource : public ExternalASTSource {
51   /// LLVM-style RTTI.
52   static char ID;
53 
54 public:
55   ExternalSemaSource() = default;
56 
57   ~ExternalSemaSource() override;
58 
59   /// Initialize the semantic source with the Sema instance
60   /// being used to perform semantic analysis on the abstract syntax
61   /// tree.
62   virtual void InitializeSema(Sema &S) {}
63 
64   /// Inform the semantic consumer that Sema is no longer available.
65   virtual void ForgetSema() {}
66 
67   /// Load the contents of the global method pool for a given
68   /// selector.
69   virtual void ReadMethodPool(Selector Sel);
70 
71   /// Load the contents of the global method pool for a given
72   /// selector if necessary.
73   virtual void updateOutOfDateSelector(Selector Sel);
74 
75   /// Load the set of namespaces that are known to the external source,
76   /// which will be used during typo correction.
77   virtual void ReadKnownNamespaces(
78                            SmallVectorImpl<NamespaceDecl *> &Namespaces);
79 
80   /// Load the set of used but not defined functions or variables with
81   /// internal linkage, or used but not defined internal functions.
82   virtual void
83   ReadUndefinedButUsed(llvm::MapVector<NamedDecl *, SourceLocation> &Undefined);
84 
85   virtual void ReadMismatchingDeleteExpressions(llvm::MapVector<
86       FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &);
87 
88   /// Do last resort, unqualified lookup on a LookupResult that
89   /// Sema cannot find.
90   ///
91   /// \param R a LookupResult that is being recovered.
92   ///
93   /// \param S the Scope of the identifier occurrence.
94   ///
95   /// \return true to tell Sema to recover using the LookupResult.
96   virtual bool LookupUnqualified(LookupResult &R, Scope *S) { return false; }
97 
98   /// Read the set of tentative definitions known to the external Sema
99   /// source.
100   ///
101   /// The external source should append its own tentative definitions to the
102   /// given vector of tentative definitions. Note that this routine may be
103   /// invoked multiple times; the external source should take care not to
104   /// introduce the same declarations repeatedly.
105   virtual void ReadTentativeDefinitions(
106                                   SmallVectorImpl<VarDecl *> &TentativeDefs) {}
107 
108   /// Read the set of unused file-scope declarations known to the
109   /// external Sema source.
110   ///
111   /// The external source should append its own unused, filed-scope to the
112   /// given vector of declarations. Note that this routine may be
113   /// invoked multiple times; the external source should take care not to
114   /// introduce the same declarations repeatedly.
115   virtual void ReadUnusedFileScopedDecls(
116                  SmallVectorImpl<const DeclaratorDecl *> &Decls) {}
117 
118   /// Read the set of delegating constructors known to the
119   /// external Sema source.
120   ///
121   /// The external source should append its own delegating constructors to the
122   /// given vector of declarations. Note that this routine may be
123   /// invoked multiple times; the external source should take care not to
124   /// introduce the same declarations repeatedly.
125   virtual void ReadDelegatingConstructors(
126                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {}
127 
128   /// Read the set of ext_vector type declarations known to the
129   /// external Sema source.
130   ///
131   /// The external source should append its own ext_vector type declarations to
132   /// the given vector of declarations. Note that this routine may be
133   /// invoked multiple times; the external source should take care not to
134   /// introduce the same declarations repeatedly.
135   virtual void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {}
136 
137   /// Read the set of potentially unused typedefs known to the source.
138   ///
139   /// The external source should append its own potentially unused local
140   /// typedefs to the given vector of declarations. Note that this routine may
141   /// be invoked multiple times; the external source should take care not to
142   /// introduce the same declarations repeatedly.
143   virtual void ReadUnusedLocalTypedefNameCandidates(
144       llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {}
145 
146   /// Read the set of referenced selectors known to the
147   /// external Sema source.
148   ///
149   /// The external source should append its own referenced selectors to the
150   /// given vector of selectors. Note that this routine
151   /// may be invoked multiple times; the external source should take care not
152   /// to introduce the same selectors repeatedly.
153   virtual void ReadReferencedSelectors(
154                  SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {}
155 
156   /// Read the set of weak, undeclared identifiers known to the
157   /// external Sema source.
158   ///
159   /// The external source should append its own weak, undeclared identifiers to
160   /// the given vector. Note that this routine may be invoked multiple times;
161   /// the external source should take care not to introduce the same identifiers
162   /// repeatedly.
163   virtual void ReadWeakUndeclaredIdentifiers(
164                  SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI) {}
165 
166   /// Read the set of used vtables known to the external Sema source.
167   ///
168   /// The external source should append its own used vtables to the given
169   /// vector. Note that this routine may be invoked multiple times; the external
170   /// source should take care not to introduce the same vtables repeatedly.
171   virtual void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {}
172 
173   /// Read the set of pending instantiations known to the external
174   /// Sema source.
175   ///
176   /// The external source should append its own pending instantiations to the
177   /// given vector. Note that this routine may be invoked multiple times; the
178   /// external source should take care not to introduce the same instantiations
179   /// repeatedly.
180   virtual void ReadPendingInstantiations(
181                  SmallVectorImpl<std::pair<ValueDecl *,
182                                            SourceLocation> > &Pending) {}
183 
184   /// Read the set of late parsed template functions for this source.
185   ///
186   /// The external source should insert its own late parsed template functions
187   /// into the map. Note that this routine may be invoked multiple times; the
188   /// external source should take care not to introduce the same map entries
189   /// repeatedly.
190   virtual void ReadLateParsedTemplates(
191       llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
192           &LPTMap) {}
193 
194   /// Read the set of decls to be checked for deferred diags.
195   ///
196   /// The external source should append its own potentially emitted function
197   /// and variable decls which may cause deferred diags. Note that this routine
198   /// may be invoked multiple times; the external source should take care not to
199   /// introduce the same declarations repeatedly.
200   virtual void
201   ReadDeclsToCheckForDeferredDiags(llvm::SmallSetVector<Decl *, 4> &Decls) {}
202 
203   /// \copydoc Sema::CorrectTypo
204   /// \note LookupKind must correspond to a valid Sema::LookupNameKind
205   ///
206   /// ExternalSemaSource::CorrectTypo is always given the first chance to
207   /// correct a typo (really, to offer suggestions to repair a failed lookup).
208   /// It will even be called when SpellChecking is turned off or after a
209   /// fatal error has already been detected.
210   virtual TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo,
211                                      int LookupKind, Scope *S, CXXScopeSpec *SS,
212                                      CorrectionCandidateCallback &CCC,
213                                      DeclContext *MemberContext,
214                                      bool EnteringContext,
215                                      const ObjCObjectPointerType *OPT) {
216     return TypoCorrection();
217   }
218 
219   /// Produces a diagnostic note if the external source contains a
220   /// complete definition for \p T.
221   ///
222   /// \param Loc the location at which a complete type was required but not
223   /// provided
224   ///
225   /// \param T the \c QualType that should have been complete at \p Loc
226   ///
227   /// \return true if a diagnostic was produced, false otherwise.
228   virtual bool MaybeDiagnoseMissingCompleteType(SourceLocation Loc,
229                                                 QualType T) {
230     return false;
231   }
232 
233   /// LLVM-style RTTI.
234   /// \{
235   bool isA(const void *ClassID) const override {
236     return ClassID == &ID || ExternalASTSource::isA(ClassID);
237   }
238   static bool classof(const ExternalASTSource *S) { return S->isA(&ID); }
239   /// \}
240 };
241 
242 } // end namespace clang
243 
244 #endif
245