]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Sema/SemaInternal.h
Update the device tree source files to a Linux 4.7-RC.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Sema / SemaInternal.h
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
25 inline PartialDiagnostic Sema::PDiag(unsigned DiagID) {
26   return PartialDiagnostic(DiagID, Context.getDiagAllocator());
27 }
28
29 inline bool
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
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.
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.
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.  
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() &&
77     !(Var->isStaticDataMember() && Var->hasInit())) {
78       SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()];
79       if (old.isInvalid()) old = Loc;
80   }
81   QualType CaptureType, DeclRefType;
82   SemaRef.tryCaptureVariable(Var, Loc, Sema::TryCapture_Implicit, 
83     /*EllipsisLoc*/ SourceLocation(),
84     /*BuildAndDiagnose*/ true, 
85     CaptureType, DeclRefType, 
86     FunctionScopeIndexToStopAt);
87
88   Var->markUsed(SemaRef.Context);
89 }
90
91 /// Return a DLL attribute from the declaration.
92 inline InheritableAttr *getDLLAttr(Decl *D) {
93   assert(!(D->hasAttr<DLLImportAttr>() && D->hasAttr<DLLExportAttr>()) &&
94          "A declaration cannot be both dllimport and dllexport.");
95   if (auto *Import = D->getAttr<DLLImportAttr>())
96     return Import;
97   if (auto *Export = D->getAttr<DLLExportAttr>())
98     return Export;
99   return nullptr;
100 }
101
102 class TypoCorrectionConsumer : public VisibleDeclConsumer {
103   typedef SmallVector<TypoCorrection, 1> TypoResultList;
104   typedef llvm::StringMap<TypoResultList> TypoResultsMap;
105   typedef std::map<unsigned, TypoResultsMap> TypoEditDistanceMap;
106
107 public:
108   TypoCorrectionConsumer(Sema &SemaRef,
109                          const DeclarationNameInfo &TypoName,
110                          Sema::LookupNameKind LookupKind,
111                          Scope *S, CXXScopeSpec *SS,
112                          std::unique_ptr<CorrectionCandidateCallback> CCC,
113                          DeclContext *MemberContext,
114                          bool EnteringContext)
115       : Typo(TypoName.getName().getAsIdentifierInfo()), CurrentTCIndex(0),
116         SavedTCIndex(0), SemaRef(SemaRef), S(S),
117         SS(SS ? llvm::make_unique<CXXScopeSpec>(*SS) : nullptr),
118         CorrectionValidator(std::move(CCC)), MemberContext(MemberContext),
119         Result(SemaRef, TypoName, LookupKind),
120         Namespaces(SemaRef.Context, SemaRef.CurContext, SS),
121         EnteringContext(EnteringContext), SearchNamespaces(false) {
122     Result.suppressDiagnostics();
123     // Arrange for ValidatedCorrections[0] to always be an empty correction.
124     ValidatedCorrections.push_back(TypoCorrection());
125   }
126
127   bool includeHiddenDecls() const override { return true; }
128
129   // Methods for adding potential corrections to the consumer.
130   void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
131                  bool InBaseClass) override;
132   void FoundName(StringRef Name);
133   void addKeywordResult(StringRef Keyword);
134   void addCorrection(TypoCorrection Correction);
135
136   bool empty() const {
137     return CorrectionResults.empty() && ValidatedCorrections.size() == 1;
138   }
139
140   /// \brief Return the list of TypoCorrections for the given identifier from
141   /// the set of corrections that have the closest edit distance, if any.
142   TypoResultList &operator[](StringRef Name) {
143     return CorrectionResults.begin()->second[Name];
144   }
145
146   /// \brief Return the edit distance of the corrections that have the
147   /// closest/best edit distance from the original typop.
148   unsigned getBestEditDistance(bool Normalized) {
149     if (CorrectionResults.empty())
150       return (std::numeric_limits<unsigned>::max)();
151
152     unsigned BestED = CorrectionResults.begin()->first;
153     return Normalized ? TypoCorrection::NormalizeEditDistance(BestED) : BestED;
154   }
155
156   /// \brief Set-up method to add to the consumer the set of namespaces to use
157   /// in performing corrections to nested name specifiers. This method also
158   /// implicitly adds all of the known classes in the current AST context to the
159   /// to the consumer for correcting nested name specifiers.
160   void
161   addNamespaces(const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces);
162
163   /// \brief Return the next typo correction that passes all internal filters
164   /// and is deemed valid by the consumer's CorrectionCandidateCallback,
165   /// starting with the corrections that have the closest edit distance. An
166   /// empty TypoCorrection is returned once no more viable corrections remain
167   /// in the consumer.
168   const TypoCorrection &getNextCorrection();
169
170   /// \brief Get the last correction returned by getNextCorrection().
171   const TypoCorrection &getCurrentCorrection() {
172     return CurrentTCIndex < ValidatedCorrections.size()
173                ? ValidatedCorrections[CurrentTCIndex]
174                : ValidatedCorrections[0];  // The empty correction.
175   }
176
177   /// \brief Return the next typo correction like getNextCorrection, but keep
178   /// the internal state pointed to the current correction (i.e. the next time
179   /// getNextCorrection is called, it will return the same correction returned
180   /// by peekNextcorrection).
181   const TypoCorrection &peekNextCorrection() {
182     auto Current = CurrentTCIndex;
183     const TypoCorrection &TC = getNextCorrection();
184     CurrentTCIndex = Current;
185     return TC;
186   }
187
188   /// \brief Reset the consumer's position in the stream of viable corrections
189   /// (i.e. getNextCorrection() will return each of the previously returned
190   /// corrections in order before returning any new corrections).
191   void resetCorrectionStream() {
192     CurrentTCIndex = 0;
193   }
194
195   /// \brief Return whether the end of the stream of corrections has been
196   /// reached.
197   bool finished() {
198     return CorrectionResults.empty() &&
199            CurrentTCIndex >= ValidatedCorrections.size();
200   }
201
202   /// \brief Save the current position in the correction stream (overwriting any
203   /// previously saved position).
204   void saveCurrentPosition() {
205     SavedTCIndex = CurrentTCIndex;
206   }
207
208   /// \brief Restore the saved position in the correction stream.
209   void restoreSavedPosition() {
210     CurrentTCIndex = SavedTCIndex;
211   }
212
213   ASTContext &getContext() const { return SemaRef.Context; }
214   const LookupResult &getLookupResult() const { return Result; }
215
216   bool isAddressOfOperand() const { return CorrectionValidator->IsAddressOfOperand; }
217   const CXXScopeSpec *getSS() const { return SS.get(); }
218   Scope *getScope() const { return S; }
219
220 private:
221   class NamespaceSpecifierSet {
222     struct SpecifierInfo {
223       DeclContext* DeclCtx;
224       NestedNameSpecifier* NameSpecifier;
225       unsigned EditDistance;
226     };
227
228     typedef SmallVector<DeclContext*, 4> DeclContextList;
229     typedef SmallVector<SpecifierInfo, 16> SpecifierInfoList;
230
231     ASTContext &Context;
232     DeclContextList CurContextChain;
233     std::string CurNameSpecifier;
234     SmallVector<const IdentifierInfo*, 4> CurContextIdentifiers;
235     SmallVector<const IdentifierInfo*, 4> CurNameSpecifierIdentifiers;
236
237     std::map<unsigned, SpecifierInfoList> DistanceMap;
238
239     /// \brief Helper for building the list of DeclContexts between the current
240     /// context and the top of the translation unit
241     static DeclContextList buildContextChain(DeclContext *Start);
242
243     unsigned buildNestedNameSpecifier(DeclContextList &DeclChain,
244                                       NestedNameSpecifier *&NNS);
245
246    public:
247     NamespaceSpecifierSet(ASTContext &Context, DeclContext *CurContext,
248                           CXXScopeSpec *CurScopeSpec);
249
250     /// \brief Add the DeclContext (a namespace or record) to the set, computing
251     /// the corresponding NestedNameSpecifier and its distance in the process.
252     void addNameSpecifier(DeclContext *Ctx);
253
254     /// \brief Provides flat iteration over specifiers, sorted by distance.
255     class iterator
256         : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag,
257                                             SpecifierInfo> {
258       /// Always points to the last element in the distance map.
259       const std::map<unsigned, SpecifierInfoList>::iterator OuterBack;
260       /// Iterator on the distance map.
261       std::map<unsigned, SpecifierInfoList>::iterator Outer;
262       /// Iterator on an element in the distance map.
263       SpecifierInfoList::iterator Inner;
264
265     public:
266       iterator(NamespaceSpecifierSet &Set, bool IsAtEnd)
267           : OuterBack(std::prev(Set.DistanceMap.end())),
268             Outer(Set.DistanceMap.begin()),
269             Inner(!IsAtEnd ? Outer->second.begin() : OuterBack->second.end()) {
270         assert(!Set.DistanceMap.empty());
271       }
272
273       iterator &operator++() {
274         ++Inner;
275         if (Inner == Outer->second.end() && Outer != OuterBack) {
276           ++Outer;
277           Inner = Outer->second.begin();
278         }
279         return *this;
280       }
281
282       SpecifierInfo &operator*() { return *Inner; }
283       bool operator==(const iterator &RHS) const { return Inner == RHS.Inner; }
284     };
285
286     iterator begin() { return iterator(*this, /*IsAtEnd=*/false); }
287     iterator end() { return iterator(*this, /*IsAtEnd=*/true); }
288   };
289
290   void addName(StringRef Name, NamedDecl *ND,
291                NestedNameSpecifier *NNS = nullptr, bool isKeyword = false);
292
293   /// \brief Find any visible decls for the given typo correction candidate.
294   /// If none are found, it to the set of candidates for which qualified lookups
295   /// will be performed to find possible nested name specifier changes.
296   bool resolveCorrection(TypoCorrection &Candidate);
297
298   /// \brief Perform qualified lookups on the queued set of typo correction
299   /// candidates and add the nested name specifier changes to each candidate if
300   /// a lookup succeeds (at which point the candidate will be returned to the
301   /// main pool of potential corrections).
302   void performQualifiedLookups();
303
304   /// \brief The name written that is a typo in the source.
305   IdentifierInfo *Typo;
306
307   /// \brief The results found that have the smallest edit distance
308   /// found (so far) with the typo name.
309   ///
310   /// The pointer value being set to the current DeclContext indicates
311   /// whether there is a keyword with this name.
312   TypoEditDistanceMap CorrectionResults;
313
314   SmallVector<TypoCorrection, 4> ValidatedCorrections;
315   size_t CurrentTCIndex;
316   size_t SavedTCIndex;
317
318   Sema &SemaRef;
319   Scope *S;
320   std::unique_ptr<CXXScopeSpec> SS;
321   std::unique_ptr<CorrectionCandidateCallback> CorrectionValidator;
322   DeclContext *MemberContext;
323   LookupResult Result;
324   NamespaceSpecifierSet Namespaces;
325   SmallVector<TypoCorrection, 2> QualifiedResults;
326   bool EnteringContext;
327   bool SearchNamespaces;
328 };
329
330 inline Sema::TypoExprState::TypoExprState() {}
331
332 inline Sema::TypoExprState::TypoExprState(TypoExprState &&other) LLVM_NOEXCEPT {
333   *this = std::move(other);
334 }
335
336 inline Sema::TypoExprState &Sema::TypoExprState::operator=(
337     Sema::TypoExprState &&other) LLVM_NOEXCEPT {
338   Consumer = std::move(other.Consumer);
339   DiagHandler = std::move(other.DiagHandler);
340   RecoveryHandler = std::move(other.RecoveryHandler);
341   return *this;
342 }
343
344 } // end namespace clang
345
346 #endif