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