1 //===--- SemaInternal.h - Internal Sema Interfaces --------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides common API and #includes for the internal
11 // implementation of Sema.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_SEMA_SEMAINTERNAL_H
16 #define LLVM_CLANG_SEMA_SEMAINTERNAL_H
17
18 #include "clang/AST/ASTContext.h"
19 #include "clang/Sema/Lookup.h"
20 #include "clang/Sema/Sema.h"
21 #include "clang/Sema/SemaDiagnostic.h"
22
23 namespace clang {
24
PDiag(unsigned DiagID)25 inline PartialDiagnostic Sema::PDiag(unsigned DiagID) {
26 return PartialDiagnostic(DiagID, Context.getDiagAllocator());
27 }
28
29 inline bool
FTIHasSingleVoidParameter(const DeclaratorChunk::FunctionTypeInfo & FTI)30 FTIHasSingleVoidParameter(const DeclaratorChunk::FunctionTypeInfo &FTI) {
31 return FTI.NumParams == 1 && !FTI.isVariadic &&
32 FTI.Params[0].Ident == nullptr && FTI.Params[0].Param &&
33 cast<ParmVarDecl>(FTI.Params[0].Param)->getType()->isVoidType();
34 }
35
36 inline bool
FTIHasNonVoidParameters(const DeclaratorChunk::FunctionTypeInfo & FTI)37 FTIHasNonVoidParameters(const DeclaratorChunk::FunctionTypeInfo &FTI) {
38 // Assume FTI is well-formed.
39 return FTI.NumParams && !FTIHasSingleVoidParameter(FTI);
40 }
41
42 // This requires the variable to be non-dependent and the initializer
43 // to not be value dependent.
IsVariableAConstantExpression(VarDecl * Var,ASTContext & Context)44 inline bool IsVariableAConstantExpression(VarDecl *Var, ASTContext &Context) {
45 const VarDecl *DefVD = nullptr;
46 return !isa<ParmVarDecl>(Var) &&
47 Var->isUsableInConstantExpressions(Context) &&
48 Var->getAnyInitializer(DefVD) && DefVD->checkInitIsICE();
49 }
50
51 // Helper function to check whether D's attributes match current CUDA mode.
52 // Decls with mismatched attributes and related diagnostics may have to be
53 // ignored during this CUDA compilation pass.
DeclAttrsMatchCUDAMode(const LangOptions & LangOpts,Decl * D)54 inline bool DeclAttrsMatchCUDAMode(const LangOptions &LangOpts, Decl *D) {
55 if (!LangOpts.CUDA || !D)
56 return true;
57 bool isDeviceSideDecl = D->hasAttr<CUDADeviceAttr>() ||
58 D->hasAttr<CUDASharedAttr>() ||
59 D->hasAttr<CUDAGlobalAttr>();
60 return isDeviceSideDecl == LangOpts.CUDAIsDevice;
61 }
62
63 // Directly mark a variable odr-used. Given a choice, prefer to use
64 // MarkVariableReferenced since it does additional checks and then
65 // calls MarkVarDeclODRUsed.
66 // If the variable must be captured:
67 // - if FunctionScopeIndexToStopAt is null, capture it in the CurContext
68 // - else capture it in the DeclContext that maps to the
69 // *FunctionScopeIndexToStopAt on the FunctionScopeInfo stack.
MarkVarDeclODRUsed(VarDecl * Var,SourceLocation Loc,Sema & SemaRef,const unsigned * const FunctionScopeIndexToStopAt)70 inline void MarkVarDeclODRUsed(VarDecl *Var,
71 SourceLocation Loc, Sema &SemaRef,
72 const unsigned *const FunctionScopeIndexToStopAt) {
73 // Keep track of used but undefined variables.
74 // FIXME: We shouldn't suppress this warning for static data members.
75 if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly &&
76 (!Var->isExternallyVisible() || Var->isInline()) &&
77 !(Var->isStaticDataMember() && Var->hasInit())) {
78 SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()];
79 if (old.isInvalid())
80 old = Loc;
81 }
82 QualType CaptureType, DeclRefType;
83 SemaRef.tryCaptureVariable(Var, Loc, Sema::TryCapture_Implicit,
84 /*EllipsisLoc*/ SourceLocation(),
85 /*BuildAndDiagnose*/ true,
86 CaptureType, DeclRefType,
87 FunctionScopeIndexToStopAt);
88
89 Var->markUsed(SemaRef.Context);
90 }
91
92 /// Return a DLL attribute from the declaration.
getDLLAttr(Decl * D)93 inline InheritableAttr *getDLLAttr(Decl *D) {
94 assert(!(D->hasAttr<DLLImportAttr>() && D->hasAttr<DLLExportAttr>()) &&
95 "A declaration cannot be both dllimport and dllexport.");
96 if (auto *Import = D->getAttr<DLLImportAttr>())
97 return Import;
98 if (auto *Export = D->getAttr<DLLExportAttr>())
99 return Export;
100 return nullptr;
101 }
102
103 class TypoCorrectionConsumer : public VisibleDeclConsumer {
104 typedef SmallVector<TypoCorrection, 1> TypoResultList;
105 typedef llvm::StringMap<TypoResultList> TypoResultsMap;
106 typedef std::map<unsigned, TypoResultsMap> TypoEditDistanceMap;
107
108 public:
TypoCorrectionConsumer(Sema & SemaRef,const DeclarationNameInfo & TypoName,Sema::LookupNameKind LookupKind,Scope * S,CXXScopeSpec * SS,std::unique_ptr<CorrectionCandidateCallback> CCC,DeclContext * MemberContext,bool EnteringContext)109 TypoCorrectionConsumer(Sema &SemaRef,
110 const DeclarationNameInfo &TypoName,
111 Sema::LookupNameKind LookupKind,
112 Scope *S, CXXScopeSpec *SS,
113 std::unique_ptr<CorrectionCandidateCallback> CCC,
114 DeclContext *MemberContext,
115 bool EnteringContext)
116 : Typo(TypoName.getName().getAsIdentifierInfo()), CurrentTCIndex(0),
117 SavedTCIndex(0), SemaRef(SemaRef), S(S),
118 SS(SS ? llvm::make_unique<CXXScopeSpec>(*SS) : nullptr),
119 CorrectionValidator(std::move(CCC)), MemberContext(MemberContext),
120 Result(SemaRef, TypoName, LookupKind),
121 Namespaces(SemaRef.Context, SemaRef.CurContext, SS),
122 EnteringContext(EnteringContext), SearchNamespaces(false) {
123 Result.suppressDiagnostics();
124 // Arrange for ValidatedCorrections[0] to always be an empty correction.
125 ValidatedCorrections.push_back(TypoCorrection());
126 }
127
includeHiddenDecls()128 bool includeHiddenDecls() const override { return true; }
129
130 // Methods for adding potential corrections to the consumer.
131 void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
132 bool InBaseClass) override;
133 void FoundName(StringRef Name);
134 void addKeywordResult(StringRef Keyword);
135 void addCorrection(TypoCorrection Correction);
136
empty()137 bool empty() const {
138 return CorrectionResults.empty() && ValidatedCorrections.size() == 1;
139 }
140
141 /// \brief Return the list of TypoCorrections for the given identifier from
142 /// the set of corrections that have the closest edit distance, if any.
143 TypoResultList &operator[](StringRef Name) {
144 return CorrectionResults.begin()->second[Name];
145 }
146
147 /// \brief Return the edit distance of the corrections that have the
148 /// closest/best edit distance from the original typop.
getBestEditDistance(bool Normalized)149 unsigned getBestEditDistance(bool Normalized) {
150 if (CorrectionResults.empty())
151 return (std::numeric_limits<unsigned>::max)();
152
153 unsigned BestED = CorrectionResults.begin()->first;
154 return Normalized ? TypoCorrection::NormalizeEditDistance(BestED) : BestED;
155 }
156
157 /// \brief Set-up method to add to the consumer the set of namespaces to use
158 /// in performing corrections to nested name specifiers. This method also
159 /// implicitly adds all of the known classes in the current AST context to the
160 /// to the consumer for correcting nested name specifiers.
161 void
162 addNamespaces(const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces);
163
164 /// \brief Return the next typo correction that passes all internal filters
165 /// and is deemed valid by the consumer's CorrectionCandidateCallback,
166 /// starting with the corrections that have the closest edit distance. An
167 /// empty TypoCorrection is returned once no more viable corrections remain
168 /// in the consumer.
169 const TypoCorrection &getNextCorrection();
170
171 /// \brief Get the last correction returned by getNextCorrection().
getCurrentCorrection()172 const TypoCorrection &getCurrentCorrection() {
173 return CurrentTCIndex < ValidatedCorrections.size()
174 ? ValidatedCorrections[CurrentTCIndex]
175 : ValidatedCorrections[0]; // The empty correction.
176 }
177
178 /// \brief Return the next typo correction like getNextCorrection, but keep
179 /// the internal state pointed to the current correction (i.e. the next time
180 /// getNextCorrection is called, it will return the same correction returned
181 /// by peekNextcorrection).
peekNextCorrection()182 const TypoCorrection &peekNextCorrection() {
183 auto Current = CurrentTCIndex;
184 const TypoCorrection &TC = getNextCorrection();
185 CurrentTCIndex = Current;
186 return TC;
187 }
188
189 /// \brief Reset the consumer's position in the stream of viable corrections
190 /// (i.e. getNextCorrection() will return each of the previously returned
191 /// corrections in order before returning any new corrections).
resetCorrectionStream()192 void resetCorrectionStream() {
193 CurrentTCIndex = 0;
194 }
195
196 /// \brief Return whether the end of the stream of corrections has been
197 /// reached.
finished()198 bool finished() {
199 return CorrectionResults.empty() &&
200 CurrentTCIndex >= ValidatedCorrections.size();
201 }
202
203 /// \brief Save the current position in the correction stream (overwriting any
204 /// previously saved position).
saveCurrentPosition()205 void saveCurrentPosition() {
206 SavedTCIndex = CurrentTCIndex;
207 }
208
209 /// \brief Restore the saved position in the correction stream.
restoreSavedPosition()210 void restoreSavedPosition() {
211 CurrentTCIndex = SavedTCIndex;
212 }
213
getContext()214 ASTContext &getContext() const { return SemaRef.Context; }
getLookupResult()215 const LookupResult &getLookupResult() const { return Result; }
216
isAddressOfOperand()217 bool isAddressOfOperand() const { return CorrectionValidator->IsAddressOfOperand; }
getSS()218 const CXXScopeSpec *getSS() const { return SS.get(); }
getScope()219 Scope *getScope() const { return S; }
getCorrectionValidator()220 CorrectionCandidateCallback *getCorrectionValidator() const {
221 return CorrectionValidator.get();
222 }
223
224 private:
225 class NamespaceSpecifierSet {
226 struct SpecifierInfo {
227 DeclContext* DeclCtx;
228 NestedNameSpecifier* NameSpecifier;
229 unsigned EditDistance;
230 };
231
232 typedef SmallVector<DeclContext*, 4> DeclContextList;
233 typedef SmallVector<SpecifierInfo, 16> SpecifierInfoList;
234
235 ASTContext &Context;
236 DeclContextList CurContextChain;
237 std::string CurNameSpecifier;
238 SmallVector<const IdentifierInfo*, 4> CurContextIdentifiers;
239 SmallVector<const IdentifierInfo*, 4> CurNameSpecifierIdentifiers;
240
241 std::map<unsigned, SpecifierInfoList> DistanceMap;
242
243 /// \brief Helper for building the list of DeclContexts between the current
244 /// context and the top of the translation unit
245 static DeclContextList buildContextChain(DeclContext *Start);
246
247 unsigned buildNestedNameSpecifier(DeclContextList &DeclChain,
248 NestedNameSpecifier *&NNS);
249
250 public:
251 NamespaceSpecifierSet(ASTContext &Context, DeclContext *CurContext,
252 CXXScopeSpec *CurScopeSpec);
253
254 /// \brief Add the DeclContext (a namespace or record) to the set, computing
255 /// the corresponding NestedNameSpecifier and its distance in the process.
256 void addNameSpecifier(DeclContext *Ctx);
257
258 /// \brief Provides flat iteration over specifiers, sorted by distance.
259 class iterator
260 : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag,
261 SpecifierInfo> {
262 /// Always points to the last element in the distance map.
263 const std::map<unsigned, SpecifierInfoList>::iterator OuterBack;
264 /// Iterator on the distance map.
265 std::map<unsigned, SpecifierInfoList>::iterator Outer;
266 /// Iterator on an element in the distance map.
267 SpecifierInfoList::iterator Inner;
268
269 public:
iterator(NamespaceSpecifierSet & Set,bool IsAtEnd)270 iterator(NamespaceSpecifierSet &Set, bool IsAtEnd)
271 : OuterBack(std::prev(Set.DistanceMap.end())),
272 Outer(Set.DistanceMap.begin()),
273 Inner(!IsAtEnd ? Outer->second.begin() : OuterBack->second.end()) {
274 assert(!Set.DistanceMap.empty());
275 }
276
277 iterator &operator++() {
278 ++Inner;
279 if (Inner == Outer->second.end() && Outer != OuterBack) {
280 ++Outer;
281 Inner = Outer->second.begin();
282 }
283 return *this;
284 }
285
286 SpecifierInfo &operator*() { return *Inner; }
287 bool operator==(const iterator &RHS) const { return Inner == RHS.Inner; }
288 };
289
begin()290 iterator begin() { return iterator(*this, /*IsAtEnd=*/false); }
end()291 iterator end() { return iterator(*this, /*IsAtEnd=*/true); }
292 };
293
294 void addName(StringRef Name, NamedDecl *ND,
295 NestedNameSpecifier *NNS = nullptr, bool isKeyword = false);
296
297 /// \brief Find any visible decls for the given typo correction candidate.
298 /// If none are found, it to the set of candidates for which qualified lookups
299 /// will be performed to find possible nested name specifier changes.
300 bool resolveCorrection(TypoCorrection &Candidate);
301
302 /// \brief Perform qualified lookups on the queued set of typo correction
303 /// candidates and add the nested name specifier changes to each candidate if
304 /// a lookup succeeds (at which point the candidate will be returned to the
305 /// main pool of potential corrections).
306 void performQualifiedLookups();
307
308 /// \brief The name written that is a typo in the source.
309 IdentifierInfo *Typo;
310
311 /// \brief The results found that have the smallest edit distance
312 /// found (so far) with the typo name.
313 ///
314 /// The pointer value being set to the current DeclContext indicates
315 /// whether there is a keyword with this name.
316 TypoEditDistanceMap CorrectionResults;
317
318 SmallVector<TypoCorrection, 4> ValidatedCorrections;
319 size_t CurrentTCIndex;
320 size_t SavedTCIndex;
321
322 Sema &SemaRef;
323 Scope *S;
324 std::unique_ptr<CXXScopeSpec> SS;
325 std::unique_ptr<CorrectionCandidateCallback> CorrectionValidator;
326 DeclContext *MemberContext;
327 LookupResult Result;
328 NamespaceSpecifierSet Namespaces;
329 SmallVector<TypoCorrection, 2> QualifiedResults;
330 bool EnteringContext;
331 bool SearchNamespaces;
332 };
333
TypoExprState()334 inline Sema::TypoExprState::TypoExprState() {}
335
TypoExprState(TypoExprState && other)336 inline Sema::TypoExprState::TypoExprState(TypoExprState &&other) LLVM_NOEXCEPT {
337 *this = std::move(other);
338 }
339
340 inline Sema::TypoExprState &Sema::TypoExprState::operator=(
341 Sema::TypoExprState &&other) LLVM_NOEXCEPT {
342 Consumer = std::move(other.Consumer);
343 DiagHandler = std::move(other.DiagHandler);
344 RecoveryHandler = std::move(other.RecoveryHandler);
345 return *this;
346 }
347
348 } // end namespace clang
349
350 #endif
351