]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Sema/Scope.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Sema / Scope.h
1 //===- Scope.h - Scope interface --------------------------------*- 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 defines the Scope interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_SEMA_SCOPE_H
15 #define LLVM_CLANG_SEMA_SCOPE_H
16
17 #include "clang/Basic/Diagnostic.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include <cassert>
23
24 namespace llvm {
25
26 class raw_ostream;
27
28 } // namespace llvm
29
30 namespace clang {
31
32 class Decl;
33 class DeclContext;
34 class UsingDirectiveDecl;
35 class VarDecl;
36
37 /// Scope - A scope is a transient data structure that is used while parsing the
38 /// program.  It assists with resolving identifiers to the appropriate
39 /// declaration.
40 class Scope {
41 public:
42   /// ScopeFlags - These are bitfields that are or'd together when creating a
43   /// scope, which defines the sorts of things the scope contains.
44   enum ScopeFlags {
45     /// This indicates that the scope corresponds to a function, which
46     /// means that labels are set here.
47     FnScope       = 0x01,
48
49     /// This is a while, do, switch, for, etc that can have break
50     /// statements embedded into it.
51     BreakScope    = 0x02,
52
53     /// This is a while, do, for, which can have continue statements
54     /// embedded into it.
55     ContinueScope = 0x04,
56
57     /// This is a scope that can contain a declaration.  Some scopes
58     /// just contain loop constructs but don't contain decls.
59     DeclScope = 0x08,
60
61     /// The controlling scope in a if/switch/while/for statement.
62     ControlScope = 0x10,
63
64     /// The scope of a struct/union/class definition.
65     ClassScope = 0x20,
66
67     /// This is a scope that corresponds to a block/closure object.
68     /// Blocks serve as top-level scopes for some objects like labels, they
69     /// also prevent things like break and continue.  BlockScopes always have
70     /// the FnScope and DeclScope flags set as well.
71     BlockScope = 0x40,
72
73     /// This is a scope that corresponds to the
74     /// template parameters of a C++ template. Template parameter
75     /// scope starts at the 'template' keyword and ends when the
76     /// template declaration ends.
77     TemplateParamScope = 0x80,
78
79     /// This is a scope that corresponds to the
80     /// parameters within a function prototype.
81     FunctionPrototypeScope = 0x100,
82
83     /// This is a scope that corresponds to the parameters within
84     /// a function prototype for a function declaration (as opposed to any
85     /// other kind of function declarator). Always has FunctionPrototypeScope
86     /// set as well.
87     FunctionDeclarationScope = 0x200,
88
89     /// This is a scope that corresponds to the Objective-C
90     /// \@catch statement.
91     AtCatchScope = 0x400,
92
93     /// This scope corresponds to an Objective-C method body.
94     /// It always has FnScope and DeclScope set as well.
95     ObjCMethodScope = 0x800,
96
97     /// This is a scope that corresponds to a switch statement.
98     SwitchScope = 0x1000,
99
100     /// This is the scope of a C++ try statement.
101     TryScope = 0x2000,
102
103     /// This is the scope for a function-level C++ try or catch scope.
104     FnTryCatchScope = 0x4000,
105
106     /// This is the scope of OpenMP executable directive.
107     OpenMPDirectiveScope = 0x8000,
108
109     /// This is the scope of some OpenMP loop directive.
110     OpenMPLoopDirectiveScope = 0x10000,
111
112     /// This is the scope of some OpenMP simd directive.
113     /// For example, it is used for 'omp simd', 'omp for simd'.
114     /// This flag is propagated to children scopes.
115     OpenMPSimdDirectiveScope = 0x20000,
116
117     /// This scope corresponds to an enum.
118     EnumScope = 0x40000,
119
120     /// This scope corresponds to an SEH try.
121     SEHTryScope = 0x80000,
122
123     /// This scope corresponds to an SEH except.
124     SEHExceptScope = 0x100000,
125
126     /// We are currently in the filter expression of an SEH except block.
127     SEHFilterScope = 0x200000,
128
129     /// This is a compound statement scope.
130     CompoundStmtScope = 0x400000,
131
132     /// We are between inheritance colon and the real class/struct definition scope.
133     ClassInheritanceScope = 0x800000,
134   };
135
136 private:
137   /// The parent scope for this scope.  This is null for the translation-unit
138   /// scope.
139   Scope *AnyParent;
140
141   /// Flags - This contains a set of ScopeFlags, which indicates how the scope
142   /// interrelates with other control flow statements.
143   unsigned Flags;
144
145   /// Depth - This is the depth of this scope.  The translation-unit scope has
146   /// depth 0.
147   unsigned short Depth;
148
149   /// Declarations with static linkage are mangled with the number of
150   /// scopes seen as a component.
151   unsigned short MSLastManglingNumber;
152
153   unsigned short MSCurManglingNumber;
154
155   /// PrototypeDepth - This is the number of function prototype scopes
156   /// enclosing this scope, including this scope.
157   unsigned short PrototypeDepth;
158
159   /// PrototypeIndex - This is the number of parameters currently
160   /// declared in this scope.
161   unsigned short PrototypeIndex;
162
163   /// FnParent - If this scope has a parent scope that is a function body, this
164   /// pointer is non-null and points to it.  This is used for label processing.
165   Scope *FnParent;
166   Scope *MSLastManglingParent;
167
168   /// BreakParent/ContinueParent - This is a direct link to the innermost
169   /// BreakScope/ContinueScope which contains the contents of this scope
170   /// for control flow purposes (and might be this scope itself), or null
171   /// if there is no such scope.
172   Scope *BreakParent, *ContinueParent;
173
174   /// BlockParent - This is a direct link to the immediately containing
175   /// BlockScope if this scope is not one, or null if there is none.
176   Scope *BlockParent;
177
178   /// TemplateParamParent - This is a direct link to the
179   /// immediately containing template parameter scope. In the
180   /// case of nested templates, template parameter scopes can have
181   /// other template parameter scopes as parents.
182   Scope *TemplateParamParent;
183
184   /// DeclsInScope - This keeps track of all declarations in this scope.  When
185   /// the declaration is added to the scope, it is set as the current
186   /// declaration for the identifier in the IdentifierTable.  When the scope is
187   /// popped, these declarations are removed from the IdentifierTable's notion
188   /// of current declaration.  It is up to the current Action implementation to
189   /// implement these semantics.
190   using DeclSetTy = llvm::SmallPtrSet<Decl *, 32>;
191   DeclSetTy DeclsInScope;
192
193   /// The DeclContext with which this scope is associated. For
194   /// example, the entity of a class scope is the class itself, the
195   /// entity of a function scope is a function, etc.
196   DeclContext *Entity;
197
198   using UsingDirectivesTy = SmallVector<UsingDirectiveDecl *, 2>;
199   UsingDirectivesTy UsingDirectives;
200
201   /// Used to determine if errors occurred in this scope.
202   DiagnosticErrorTrap ErrorTrap;
203
204   /// A lattice consisting of undefined, a single NRVO candidate variable in
205   /// this scope, or over-defined. The bit is true when over-defined.
206   llvm::PointerIntPair<VarDecl *, 1, bool> NRVO;
207
208   void setFlags(Scope *Parent, unsigned F);
209
210 public:
211   Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
212       : ErrorTrap(Diag) {
213     Init(Parent, ScopeFlags);
214   }
215
216   /// getFlags - Return the flags for this scope.
217   unsigned getFlags() const { return Flags; }
218
219   void setFlags(unsigned F) { setFlags(getParent(), F); }
220
221   /// isBlockScope - Return true if this scope correspond to a closure.
222   bool isBlockScope() const { return Flags & BlockScope; }
223
224   /// getParent - Return the scope that this is nested in.
225   const Scope *getParent() const { return AnyParent; }
226   Scope *getParent() { return AnyParent; }
227
228   /// getFnParent - Return the closest scope that is a function body.
229   const Scope *getFnParent() const { return FnParent; }
230   Scope *getFnParent() { return FnParent; }
231
232   const Scope *getMSLastManglingParent() const {
233     return MSLastManglingParent;
234   }
235   Scope *getMSLastManglingParent() { return MSLastManglingParent; }
236
237   /// getContinueParent - Return the closest scope that a continue statement
238   /// would be affected by.
239   Scope *getContinueParent() {
240     return ContinueParent;
241   }
242
243   const Scope *getContinueParent() const {
244     return const_cast<Scope*>(this)->getContinueParent();
245   }
246
247   /// getBreakParent - Return the closest scope that a break statement
248   /// would be affected by.
249   Scope *getBreakParent() {
250     return BreakParent;
251   }
252   const Scope *getBreakParent() const {
253     return const_cast<Scope*>(this)->getBreakParent();
254   }
255
256   Scope *getBlockParent() { return BlockParent; }
257   const Scope *getBlockParent() const { return BlockParent; }
258
259   Scope *getTemplateParamParent() { return TemplateParamParent; }
260   const Scope *getTemplateParamParent() const { return TemplateParamParent; }
261
262   /// Returns the depth of this scope. The translation-unit has scope depth 0.
263   unsigned getDepth() const { return Depth; }
264
265   /// Returns the number of function prototype scopes in this scope
266   /// chain.
267   unsigned getFunctionPrototypeDepth() const {
268     return PrototypeDepth;
269   }
270
271   /// Return the number of parameters declared in this function
272   /// prototype, increasing it by one for the next call.
273   unsigned getNextFunctionPrototypeIndex() {
274     assert(isFunctionPrototypeScope());
275     return PrototypeIndex++;
276   }
277
278   using decl_range = llvm::iterator_range<DeclSetTy::iterator>;
279
280   decl_range decls() const {
281     return decl_range(DeclsInScope.begin(), DeclsInScope.end());
282   }
283
284   bool decl_empty() const { return DeclsInScope.empty(); }
285
286   void AddDecl(Decl *D) {
287     DeclsInScope.insert(D);
288   }
289
290   void RemoveDecl(Decl *D) {
291     DeclsInScope.erase(D);
292   }
293
294   void incrementMSManglingNumber() {
295     if (Scope *MSLMP = getMSLastManglingParent()) {
296       MSLMP->MSLastManglingNumber += 1;
297       MSCurManglingNumber += 1;
298     }
299   }
300
301   void decrementMSManglingNumber() {
302     if (Scope *MSLMP = getMSLastManglingParent()) {
303       MSLMP->MSLastManglingNumber -= 1;
304       MSCurManglingNumber -= 1;
305     }
306   }
307
308   unsigned getMSLastManglingNumber() const {
309     if (const Scope *MSLMP = getMSLastManglingParent())
310       return MSLMP->MSLastManglingNumber;
311     return 1;
312   }
313
314   unsigned getMSCurManglingNumber() const {
315     return MSCurManglingNumber;
316   }
317
318   /// isDeclScope - Return true if this is the scope that the specified decl is
319   /// declared in.
320   bool isDeclScope(Decl *D) {
321     return DeclsInScope.count(D) != 0;
322   }
323
324   DeclContext *getEntity() const { return Entity; }
325   void setEntity(DeclContext *E) { Entity = E; }
326
327   bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
328
329   bool hasUnrecoverableErrorOccurred() const {
330     return ErrorTrap.hasUnrecoverableErrorOccurred();
331   }
332
333   /// isFunctionScope() - Return true if this scope is a function scope.
334   bool isFunctionScope() const { return (getFlags() & Scope::FnScope); }
335
336   /// isClassScope - Return true if this scope is a class/struct/union scope.
337   bool isClassScope() const {
338     return (getFlags() & Scope::ClassScope);
339   }
340
341   /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
342   /// method scope or is inside one.
343   bool isInCXXInlineMethodScope() const {
344     if (const Scope *FnS = getFnParent()) {
345       assert(FnS->getParent() && "TUScope not created?");
346       return FnS->getParent()->isClassScope();
347     }
348     return false;
349   }
350
351   /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
352   /// Objective-C method body.  Note that this method is not constant time.
353   bool isInObjcMethodScope() const {
354     for (const Scope *S = this; S; S = S->getParent()) {
355       // If this scope is an objc method scope, then we succeed.
356       if (S->getFlags() & ObjCMethodScope)
357         return true;
358     }
359     return false;
360   }
361
362   /// isInObjcMethodOuterScope - Return true if this scope is an
363   /// Objective-C method outer most body.
364   bool isInObjcMethodOuterScope() const {
365     if (const Scope *S = this) {
366       // If this scope is an objc method scope, then we succeed.
367       if (S->getFlags() & ObjCMethodScope)
368         return true;
369     }
370     return false;
371   }
372
373   /// isTemplateParamScope - Return true if this scope is a C++
374   /// template parameter scope.
375   bool isTemplateParamScope() const {
376     return getFlags() & Scope::TemplateParamScope;
377   }
378
379   /// isFunctionPrototypeScope - Return true if this scope is a
380   /// function prototype scope.
381   bool isFunctionPrototypeScope() const {
382     return getFlags() & Scope::FunctionPrototypeScope;
383   }
384
385   /// isAtCatchScope - Return true if this scope is \@catch.
386   bool isAtCatchScope() const {
387     return getFlags() & Scope::AtCatchScope;
388   }
389
390   /// isSwitchScope - Return true if this scope is a switch scope.
391   bool isSwitchScope() const {
392     for (const Scope *S = this; S; S = S->getParent()) {
393       if (S->getFlags() & Scope::SwitchScope)
394         return true;
395       else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
396                                 Scope::BlockScope | Scope::TemplateParamScope |
397                                 Scope::FunctionPrototypeScope |
398                                 Scope::AtCatchScope | Scope::ObjCMethodScope))
399         return false;
400     }
401     return false;
402   }
403
404   /// Determines whether this scope is the OpenMP directive scope
405   bool isOpenMPDirectiveScope() const {
406     return (getFlags() & Scope::OpenMPDirectiveScope);
407   }
408
409   /// Determine whether this scope is some OpenMP loop directive scope
410   /// (for example, 'omp for', 'omp simd').
411   bool isOpenMPLoopDirectiveScope() const {
412     if (getFlags() & Scope::OpenMPLoopDirectiveScope) {
413       assert(isOpenMPDirectiveScope() &&
414              "OpenMP loop directive scope is not a directive scope");
415       return true;
416     }
417     return false;
418   }
419
420   /// Determine whether this scope is (or is nested into) some OpenMP
421   /// loop simd directive scope (for example, 'omp simd', 'omp for simd').
422   bool isOpenMPSimdDirectiveScope() const {
423     return getFlags() & Scope::OpenMPSimdDirectiveScope;
424   }
425
426   /// Determine whether this scope is a loop having OpenMP loop
427   /// directive attached.
428   bool isOpenMPLoopScope() const {
429     const Scope *P = getParent();
430     return P && P->isOpenMPLoopDirectiveScope();
431   }
432
433   /// Determine whether this scope is a C++ 'try' block.
434   bool isTryScope() const { return getFlags() & Scope::TryScope; }
435
436   /// Determine whether this scope is a SEH '__try' block.
437   bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
438
439   /// Determine whether this scope is a SEH '__except' block.
440   bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; }
441
442   /// Determine whether this scope is a compound statement scope.
443   bool isCompoundStmtScope() const {
444     return getFlags() & Scope::CompoundStmtScope;
445   }
446
447   /// Returns if rhs has a higher scope depth than this.
448   ///
449   /// The caller is responsible for calling this only if one of the two scopes
450   /// is an ancestor of the other.
451   bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; }
452
453   /// containedInPrototypeScope - Return true if this or a parent scope
454   /// is a FunctionPrototypeScope.
455   bool containedInPrototypeScope() const;
456
457   void PushUsingDirective(UsingDirectiveDecl *UDir) {
458     UsingDirectives.push_back(UDir);
459   }
460
461   using using_directives_range =
462       llvm::iterator_range<UsingDirectivesTy::iterator>;
463
464   using_directives_range using_directives() {
465     return using_directives_range(UsingDirectives.begin(),
466                                   UsingDirectives.end());
467   }
468
469   void addNRVOCandidate(VarDecl *VD) {
470     if (NRVO.getInt())
471       return;
472     if (NRVO.getPointer() == nullptr) {
473       NRVO.setPointer(VD);
474       return;
475     }
476     if (NRVO.getPointer() != VD)
477       setNoNRVO();
478   }
479
480   void setNoNRVO() {
481     NRVO.setInt(true);
482     NRVO.setPointer(nullptr);
483   }
484
485   void mergeNRVOIntoParent();
486
487   /// Init - This is used by the parser to implement scope caching.
488   void Init(Scope *parent, unsigned flags);
489
490   /// Sets up the specified scope flags and adjusts the scope state
491   /// variables accordingly.
492   void AddFlags(unsigned Flags);
493
494   void dumpImpl(raw_ostream &OS) const;
495   void dump() const;
496 };
497
498 } // namespace clang
499
500 #endif // LLVM_CLANG_SEMA_SCOPE_H