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