• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 private:
129   /// The parent scope for this scope.  This is null for the translation-unit
130   /// scope.
131   Scope *AnyParent;
132 
133   /// Flags - This contains a set of ScopeFlags, which indicates how the scope
134   /// interrelates with other control flow statements.
135   unsigned Flags;
136 
137   /// Depth - This is the depth of this scope.  The translation-unit scope has
138   /// depth 0.
139   unsigned short Depth;
140 
141   /// \brief Declarations with static linkage are mangled with the number of
142   /// scopes seen as a component.
143   unsigned short MSLastManglingNumber;
144 
145   unsigned short MSCurManglingNumber;
146 
147   /// PrototypeDepth - This is the number of function prototype scopes
148   /// enclosing this scope, including this scope.
149   unsigned short PrototypeDepth;
150 
151   /// PrototypeIndex - This is the number of parameters currently
152   /// declared in this scope.
153   unsigned short PrototypeIndex;
154 
155   /// FnParent - If this scope has a parent scope that is a function body, this
156   /// pointer is non-null and points to it.  This is used for label processing.
157   Scope *FnParent;
158   Scope *MSLastManglingParent;
159 
160   /// BreakParent/ContinueParent - This is a direct link to the innermost
161   /// BreakScope/ContinueScope which contains the contents of this scope
162   /// for control flow purposes (and might be this scope itself), or null
163   /// if there is no such scope.
164   Scope *BreakParent, *ContinueParent;
165 
166   /// BlockParent - This is a direct link to the immediately containing
167   /// BlockScope if this scope is not one, or null if there is none.
168   Scope *BlockParent;
169 
170   /// TemplateParamParent - This is a direct link to the
171   /// immediately containing template parameter scope. In the
172   /// case of nested templates, template parameter scopes can have
173   /// other template parameter scopes as parents.
174   Scope *TemplateParamParent;
175 
176   /// DeclsInScope - This keeps track of all declarations in this scope.  When
177   /// the declaration is added to the scope, it is set as the current
178   /// declaration for the identifier in the IdentifierTable.  When the scope is
179   /// popped, these declarations are removed from the IdentifierTable's notion
180   /// of current declaration.  It is up to the current Action implementation to
181   /// implement these semantics.
182   typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy;
183   DeclSetTy DeclsInScope;
184 
185   /// The DeclContext with which this scope is associated. For
186   /// example, the entity of a class scope is the class itself, the
187   /// entity of a function scope is a function, etc.
188   DeclContext *Entity;
189 
190   typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
191   UsingDirectivesTy UsingDirectives;
192 
193   /// \brief Used to determine if errors occurred in this scope.
194   DiagnosticErrorTrap ErrorTrap;
195 
196   /// A lattice consisting of undefined, a single NRVO candidate variable in
197   /// this scope, or over-defined. The bit is true when over-defined.
198   llvm::PointerIntPair<VarDecl *, 1, bool> NRVO;
199 
200   void setFlags(Scope *Parent, unsigned F);
201 
202 public:
Scope(Scope * Parent,unsigned ScopeFlags,DiagnosticsEngine & Diag)203   Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
204     : ErrorTrap(Diag) {
205     Init(Parent, ScopeFlags);
206   }
207 
208   /// getFlags - Return the flags for this scope.
209   ///
getFlags()210   unsigned getFlags() const { return Flags; }
setFlags(unsigned F)211   void setFlags(unsigned F) { setFlags(getParent(), F); }
212 
213   /// isBlockScope - Return true if this scope correspond to a closure.
isBlockScope()214   bool isBlockScope() const { return Flags & BlockScope; }
215 
216   /// getParent - Return the scope that this is nested in.
217   ///
getParent()218   const Scope *getParent() const { return AnyParent; }
getParent()219   Scope *getParent() { return AnyParent; }
220 
221   /// getFnParent - Return the closest scope that is a function body.
222   ///
getFnParent()223   const Scope *getFnParent() const { return FnParent; }
getFnParent()224   Scope *getFnParent() { return FnParent; }
225 
getMSLastManglingParent()226   const Scope *getMSLastManglingParent() const {
227     return MSLastManglingParent;
228   }
getMSLastManglingParent()229   Scope *getMSLastManglingParent() { return MSLastManglingParent; }
230 
231   /// getContinueParent - Return the closest scope that a continue statement
232   /// would be affected by.
getContinueParent()233   Scope *getContinueParent() {
234     return ContinueParent;
235   }
236 
getContinueParent()237   const Scope *getContinueParent() const {
238     return const_cast<Scope*>(this)->getContinueParent();
239   }
240 
241   /// getBreakParent - Return the closest scope that a break statement
242   /// would be affected by.
getBreakParent()243   Scope *getBreakParent() {
244     return BreakParent;
245   }
getBreakParent()246   const Scope *getBreakParent() const {
247     return const_cast<Scope*>(this)->getBreakParent();
248   }
249 
getBlockParent()250   Scope *getBlockParent() { return BlockParent; }
getBlockParent()251   const Scope *getBlockParent() const { return BlockParent; }
252 
getTemplateParamParent()253   Scope *getTemplateParamParent() { return TemplateParamParent; }
getTemplateParamParent()254   const Scope *getTemplateParamParent() const { return TemplateParamParent; }
255 
256   /// Returns the number of function prototype scopes in this scope
257   /// chain.
getFunctionPrototypeDepth()258   unsigned getFunctionPrototypeDepth() const {
259     return PrototypeDepth;
260   }
261 
262   /// Return the number of parameters declared in this function
263   /// prototype, increasing it by one for the next call.
getNextFunctionPrototypeIndex()264   unsigned getNextFunctionPrototypeIndex() {
265     assert(isFunctionPrototypeScope());
266     return PrototypeIndex++;
267   }
268 
269   typedef llvm::iterator_range<DeclSetTy::iterator> decl_range;
decls()270   decl_range decls() const {
271     return decl_range(DeclsInScope.begin(), DeclsInScope.end());
272   }
decl_empty()273   bool decl_empty() const { return DeclsInScope.empty(); }
274 
AddDecl(Decl * D)275   void AddDecl(Decl *D) {
276     DeclsInScope.insert(D);
277   }
278 
RemoveDecl(Decl * D)279   void RemoveDecl(Decl *D) {
280     DeclsInScope.erase(D);
281   }
282 
incrementMSManglingNumber()283   void incrementMSManglingNumber() {
284     if (Scope *MSLMP = getMSLastManglingParent()) {
285       MSLMP->MSLastManglingNumber += 1;
286       MSCurManglingNumber += 1;
287     }
288   }
289 
decrementMSManglingNumber()290   void decrementMSManglingNumber() {
291     if (Scope *MSLMP = getMSLastManglingParent()) {
292       MSLMP->MSLastManglingNumber -= 1;
293       MSCurManglingNumber -= 1;
294     }
295   }
296 
getMSLastManglingNumber()297   unsigned getMSLastManglingNumber() const {
298     if (const Scope *MSLMP = getMSLastManglingParent())
299       return MSLMP->MSLastManglingNumber;
300     return 1;
301   }
302 
getMSCurManglingNumber()303   unsigned getMSCurManglingNumber() const {
304     return MSCurManglingNumber;
305   }
306 
307   /// isDeclScope - Return true if this is the scope that the specified decl is
308   /// declared in.
isDeclScope(Decl * D)309   bool isDeclScope(Decl *D) {
310     return DeclsInScope.count(D) != 0;
311   }
312 
getEntity()313   DeclContext *getEntity() const { return Entity; }
setEntity(DeclContext * E)314   void setEntity(DeclContext *E) { Entity = E; }
315 
hasErrorOccurred()316   bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
317 
hasUnrecoverableErrorOccurred()318   bool hasUnrecoverableErrorOccurred() const {
319     return ErrorTrap.hasUnrecoverableErrorOccurred();
320   }
321 
322   /// isFunctionScope() - Return true if this scope is a function scope.
isFunctionScope()323   bool isFunctionScope() const { return (getFlags() & Scope::FnScope); }
324 
325   /// isClassScope - Return true if this scope is a class/struct/union scope.
isClassScope()326   bool isClassScope() const {
327     return (getFlags() & Scope::ClassScope);
328   }
329 
330   /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
331   /// method scope or is inside one.
isInCXXInlineMethodScope()332   bool isInCXXInlineMethodScope() const {
333     if (const Scope *FnS = getFnParent()) {
334       assert(FnS->getParent() && "TUScope not created?");
335       return FnS->getParent()->isClassScope();
336     }
337     return false;
338   }
339 
340   /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
341   /// Objective-C method body.  Note that this method is not constant time.
isInObjcMethodScope()342   bool isInObjcMethodScope() const {
343     for (const Scope *S = this; S; S = S->getParent()) {
344       // If this scope is an objc method scope, then we succeed.
345       if (S->getFlags() & ObjCMethodScope)
346         return true;
347     }
348     return false;
349   }
350 
351   /// isInObjcMethodOuterScope - Return true if this scope is an
352   /// Objective-C method outer most body.
isInObjcMethodOuterScope()353   bool isInObjcMethodOuterScope() const {
354     if (const Scope *S = this) {
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 
363   /// isTemplateParamScope - Return true if this scope is a C++
364   /// template parameter scope.
isTemplateParamScope()365   bool isTemplateParamScope() const {
366     return getFlags() & Scope::TemplateParamScope;
367   }
368 
369   /// isFunctionPrototypeScope - Return true if this scope is a
370   /// function prototype scope.
isFunctionPrototypeScope()371   bool isFunctionPrototypeScope() const {
372     return getFlags() & Scope::FunctionPrototypeScope;
373   }
374 
375   /// isAtCatchScope - Return true if this scope is \@catch.
isAtCatchScope()376   bool isAtCatchScope() const {
377     return getFlags() & Scope::AtCatchScope;
378   }
379 
380   /// isSwitchScope - Return true if this scope is a switch scope.
isSwitchScope()381   bool isSwitchScope() const {
382     for (const Scope *S = this; S; S = S->getParent()) {
383       if (S->getFlags() & Scope::SwitchScope)
384         return true;
385       else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
386                                 Scope::BlockScope | Scope::TemplateParamScope |
387                                 Scope::FunctionPrototypeScope |
388                                 Scope::AtCatchScope | Scope::ObjCMethodScope))
389         return false;
390     }
391     return false;
392   }
393 
394   /// \brief Determines whether this scope is the OpenMP directive scope
isOpenMPDirectiveScope()395   bool isOpenMPDirectiveScope() const {
396     return (getFlags() & Scope::OpenMPDirectiveScope);
397   }
398 
399   /// \brief Determine whether this scope is some OpenMP loop directive scope
400   /// (for example, 'omp for', 'omp simd').
isOpenMPLoopDirectiveScope()401   bool isOpenMPLoopDirectiveScope() const {
402     if (getFlags() & Scope::OpenMPLoopDirectiveScope) {
403       assert(isOpenMPDirectiveScope() &&
404              "OpenMP loop directive scope is not a directive scope");
405       return true;
406     }
407     return false;
408   }
409 
410   /// \brief Determine whether this scope is (or is nested into) some OpenMP
411   /// loop simd directive scope (for example, 'omp simd', 'omp for simd').
isOpenMPSimdDirectiveScope()412   bool isOpenMPSimdDirectiveScope() const {
413     return getFlags() & Scope::OpenMPSimdDirectiveScope;
414   }
415 
416   /// \brief Determine whether this scope is a loop having OpenMP loop
417   /// directive attached.
isOpenMPLoopScope()418   bool isOpenMPLoopScope() const {
419     const Scope *P = getParent();
420     return P && P->isOpenMPLoopDirectiveScope();
421   }
422 
423   /// \brief Determine whether this scope is a C++ 'try' block.
isTryScope()424   bool isTryScope() const { return getFlags() & Scope::TryScope; }
425 
426   /// \brief Determine whether this scope is a SEH '__try' block.
isSEHTryScope()427   bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
428 
429   /// \brief Determine whether this scope is a SEH '__except' block.
isSEHExceptScope()430   bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; }
431 
432   /// \brief Returns if rhs has a higher scope depth than this.
433   ///
434   /// The caller is responsible for calling this only if one of the two scopes
435   /// is an ancestor of the other.
Contains(const Scope & rhs)436   bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; }
437 
438   /// containedInPrototypeScope - Return true if this or a parent scope
439   /// is a FunctionPrototypeScope.
440   bool containedInPrototypeScope() const;
441 
PushUsingDirective(UsingDirectiveDecl * UDir)442   void PushUsingDirective(UsingDirectiveDecl *UDir) {
443     UsingDirectives.push_back(UDir);
444   }
445 
446   typedef llvm::iterator_range<UsingDirectivesTy::iterator>
447     using_directives_range;
448 
using_directives()449   using_directives_range using_directives() {
450     return using_directives_range(UsingDirectives.begin(),
451                                   UsingDirectives.end());
452   }
453 
addNRVOCandidate(VarDecl * VD)454   void addNRVOCandidate(VarDecl *VD) {
455     if (NRVO.getInt())
456       return;
457     if (NRVO.getPointer() == nullptr) {
458       NRVO.setPointer(VD);
459       return;
460     }
461     if (NRVO.getPointer() != VD)
462       setNoNRVO();
463   }
464 
setNoNRVO()465   void setNoNRVO() {
466     NRVO.setInt(1);
467     NRVO.setPointer(nullptr);
468   }
469 
470   void mergeNRVOIntoParent();
471 
472   /// Init - This is used by the parser to implement scope caching.
473   ///
474   void Init(Scope *parent, unsigned flags);
475 
476   /// \brief Sets up the specified scope flags and adjusts the scope state
477   /// variables accordingly.
478   ///
479   void AddFlags(unsigned Flags);
480 
481   void dumpImpl(raw_ostream &OS) const;
482   void dump() const;
483 };
484 
485 }  // end namespace clang
486 
487 #endif
488