Scope.h revision e5afdcfd6a80efc20b0a2e5bde806c08c3bda887
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/SmallPtrSet.h"
19#include "llvm/ADT/SmallVector.h"
20
21namespace clang {
22
23class Decl;
24class UsingDirectiveDecl;
25
26/// Scope - A scope is a transient data structure that is used while parsing the
27/// program.  It assists with resolving identifiers to the appropriate
28/// declaration.
29///
30class Scope {
31public:
32  /// ScopeFlags - These are bitfields that are or'd together when creating a
33  /// scope, which defines the sorts of things the scope contains.
34  enum ScopeFlags {
35    /// FnScope - This indicates that the scope corresponds to a function, which
36    /// means that labels are set here.
37    FnScope       = 0x01,
38
39    /// BreakScope - This is a while,do,switch,for, etc that can have break
40    /// stmts embedded into it.
41    BreakScope    = 0x02,
42
43    /// ContinueScope - This is a while,do,for, which can have continue
44    /// stmt embedded into it.
45    ContinueScope = 0x04,
46
47    /// DeclScope - This is a scope that can contain a declaration.  Some scopes
48    /// just contain loop constructs but don't contain decls.
49    DeclScope = 0x08,
50
51    /// ControlScope - The controlling scope in a if/switch/while/for statement.
52    ControlScope = 0x10,
53
54    /// ClassScope - The scope of a struct/union/class definition.
55    ClassScope = 0x20,
56
57    /// BlockScope - This is a scope that corresponds to a block/closure object.
58    /// Blocks serve as top-level scopes for some objects like labels, they
59    /// also prevent things like break and continue.  BlockScopes always have
60    /// the FnScope and DeclScope flags set as well.
61    BlockScope = 0x40,
62
63    /// TemplateParamScope - This is a scope that corresponds to the
64    /// template parameters of a C++ template. Template parameter
65    /// scope starts at the 'template' keyword and ends when the
66    /// template declaration ends.
67    TemplateParamScope = 0x80,
68
69    /// FunctionPrototypeScope - This is a scope that corresponds to the
70    /// parameters within a function prototype.
71    FunctionPrototypeScope = 0x100,
72
73    /// AtCatchScope - This is a scope that corresponds to the Objective-C
74    /// \@catch statement.
75    AtCatchScope = 0x200,
76
77    /// ObjCMethodScope - This scope corresponds to an Objective-C method body.
78    /// It always has FnScope and DeclScope set as well.
79    ObjCMethodScope = 0x400,
80
81    /// SwitchScope - This is a scope that corresponds to a switch statement.
82    SwitchScope = 0x800,
83
84    /// TryScope - This is the scope of a C++ try statement.
85    TryScope = 0x1000,
86
87    /// FnTryCatchScope - This is the scope for a function-level C++ try or
88    /// catch scope.
89    FnTryCatchScope = 0x2000
90  };
91private:
92  /// The parent scope for this scope.  This is null for the translation-unit
93  /// scope.
94  Scope *AnyParent;
95
96  /// Depth - This is the depth of this scope.  The translation-unit scope has
97  /// depth 0.
98  unsigned short Depth;
99
100  /// Flags - This contains a set of ScopeFlags, which indicates how the scope
101  /// interrelates with other control flow statements.
102  unsigned short Flags;
103
104  /// PrototypeDepth - This is the number of function prototype scopes
105  /// enclosing this scope, including this scope.
106  unsigned short PrototypeDepth;
107
108  /// PrototypeIndex - This is the number of parameters currently
109  /// declared in this scope.
110  unsigned short PrototypeIndex;
111
112  /// FnParent - If this scope has a parent scope that is a function body, this
113  /// pointer is non-null and points to it.  This is used for label processing.
114  Scope *FnParent;
115
116  /// BreakParent/ContinueParent - This is a direct link to the innermost
117  /// BreakScope/ContinueScope which contains the contents of this scope
118  /// for control flow purposes (and might be this scope itself), or null
119  /// if there is no such scope.
120  Scope *BreakParent, *ContinueParent;
121
122  /// BlockParent - This is a direct link to the immediately containing
123  /// BlockScope if this scope is not one, or null if there is none.
124  Scope *BlockParent;
125
126  /// TemplateParamParent - This is a direct link to the
127  /// immediately containing template parameter scope. In the
128  /// case of nested templates, template parameter scopes can have
129  /// other template parameter scopes as parents.
130  Scope *TemplateParamParent;
131
132  /// DeclsInScope - This keeps track of all declarations in this scope.  When
133  /// the declaration is added to the scope, it is set as the current
134  /// declaration for the identifier in the IdentifierTable.  When the scope is
135  /// popped, these declarations are removed from the IdentifierTable's notion
136  /// of current declaration.  It is up to the current Action implementation to
137  /// implement these semantics.
138  typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy;
139  DeclSetTy DeclsInScope;
140
141  /// Entity - The entity with which this scope is associated. For
142  /// example, the entity of a class scope is the class itself, the
143  /// entity of a function scope is a function, etc. This field is
144  /// maintained by the Action implementation.
145  void *Entity;
146
147  typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
148  UsingDirectivesTy UsingDirectives;
149
150  /// \brief Used to determine if errors occurred in this scope.
151  DiagnosticErrorTrap ErrorTrap;
152
153public:
154  Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
155    : ErrorTrap(Diag) {
156    Init(Parent, ScopeFlags);
157  }
158
159  /// getFlags - Return the flags for this scope.
160  ///
161  unsigned getFlags() const { return Flags; }
162  void setFlags(unsigned F) { Flags = F; }
163
164  /// isBlockScope - Return true if this scope correspond to a closure.
165  bool isBlockScope() const { return Flags & BlockScope; }
166
167  /// getParent - Return the scope that this is nested in.
168  ///
169  const Scope *getParent() const { return AnyParent; }
170  Scope *getParent() { return AnyParent; }
171
172  /// getFnParent - Return the closest scope that is a function body.
173  ///
174  const Scope *getFnParent() const { return FnParent; }
175  Scope *getFnParent() { return FnParent; }
176
177  /// getContinueParent - Return the closest scope that a continue statement
178  /// would be affected by.
179  Scope *getContinueParent() {
180    return ContinueParent;
181  }
182
183  const Scope *getContinueParent() const {
184    return const_cast<Scope*>(this)->getContinueParent();
185  }
186
187  /// getBreakParent - Return the closest scope that a break statement
188  /// would be affected by.
189  Scope *getBreakParent() {
190    return BreakParent;
191  }
192  const Scope *getBreakParent() const {
193    return const_cast<Scope*>(this)->getBreakParent();
194  }
195
196  Scope *getBlockParent() { return BlockParent; }
197  const Scope *getBlockParent() const { return BlockParent; }
198
199  Scope *getTemplateParamParent() { return TemplateParamParent; }
200  const Scope *getTemplateParamParent() const { return TemplateParamParent; }
201
202  /// Returns the number of function prototype scopes in this scope
203  /// chain.
204  unsigned getFunctionPrototypeDepth() const {
205    return PrototypeDepth;
206  }
207
208  /// Return the number of parameters declared in this function
209  /// prototype, increasing it by one for the next call.
210  unsigned getNextFunctionPrototypeIndex() {
211    assert(isFunctionPrototypeScope());
212    return PrototypeIndex++;
213  }
214
215  typedef DeclSetTy::iterator decl_iterator;
216  decl_iterator decl_begin() const { return DeclsInScope.begin(); }
217  decl_iterator decl_end()   const { return DeclsInScope.end(); }
218  bool decl_empty()          const { return DeclsInScope.empty(); }
219
220  void AddDecl(Decl *D) {
221    DeclsInScope.insert(D);
222  }
223
224  void RemoveDecl(Decl *D) {
225    DeclsInScope.erase(D);
226  }
227
228  /// isDeclScope - Return true if this is the scope that the specified decl is
229  /// declared in.
230  bool isDeclScope(Decl *D) {
231    return DeclsInScope.count(D) != 0;
232  }
233
234  void* getEntity() const { return Entity; }
235  void setEntity(void *E) { Entity = E; }
236
237  bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
238
239  /// isClassScope - Return true if this scope is a class/struct/union scope.
240  bool isClassScope() const {
241    return (getFlags() & Scope::ClassScope);
242  }
243
244  /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
245  /// method scope or is inside one.
246  bool isInCXXInlineMethodScope() const {
247    if (const Scope *FnS = getFnParent()) {
248      assert(FnS->getParent() && "TUScope not created?");
249      return FnS->getParent()->isClassScope();
250    }
251    return false;
252  }
253
254  /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
255  /// Objective-C method body.  Note that this method is not constant time.
256  bool isInObjcMethodScope() const {
257    for (const Scope *S = this; S; S = S->getParent()) {
258      // If this scope is an objc method scope, then we succeed.
259      if (S->getFlags() & ObjCMethodScope)
260        return true;
261    }
262    return false;
263  }
264
265  /// isTemplateParamScope - Return true if this scope is a C++
266  /// template parameter scope.
267  bool isTemplateParamScope() const {
268    return getFlags() & Scope::TemplateParamScope;
269  }
270
271  /// isFunctionPrototypeScope - Return true if this scope is a
272  /// function prototype scope.
273  bool isFunctionPrototypeScope() const {
274    return getFlags() & Scope::FunctionPrototypeScope;
275  }
276
277  /// isAtCatchScope - Return true if this scope is \@catch.
278  bool isAtCatchScope() const {
279    return getFlags() & Scope::AtCatchScope;
280  }
281
282  /// isSwitchScope - Return true if this scope is a switch scope.
283  bool isSwitchScope() const {
284    for (const Scope *S = this; S; S = S->getParent()) {
285      if (S->getFlags() & Scope::SwitchScope)
286        return true;
287      else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
288                                Scope::BlockScope | Scope::TemplateParamScope |
289                                Scope::FunctionPrototypeScope |
290                                Scope::AtCatchScope | Scope::ObjCMethodScope))
291        return false;
292    }
293    return false;
294  }
295
296  /// \brief Determine whether this scope is a C++ 'try' block.
297  bool isTryScope() const { return getFlags() & Scope::TryScope; }
298
299  /// containedInPrototypeScope - Return true if this or a parent scope
300  /// is a FunctionPrototypeScope.
301  bool containedInPrototypeScope() const;
302
303  typedef UsingDirectivesTy::iterator udir_iterator;
304  typedef UsingDirectivesTy::const_iterator const_udir_iterator;
305
306  void PushUsingDirective(UsingDirectiveDecl *UDir) {
307    UsingDirectives.push_back(UDir);
308  }
309
310  udir_iterator using_directives_begin() {
311    return UsingDirectives.begin();
312  }
313
314  udir_iterator using_directives_end() {
315    return UsingDirectives.end();
316  }
317
318  const_udir_iterator using_directives_begin() const {
319    return UsingDirectives.begin();
320  }
321
322  const_udir_iterator using_directives_end() const {
323    return UsingDirectives.end();
324  }
325
326  /// Init - This is used by the parser to implement scope caching.
327  ///
328  void Init(Scope *parent, unsigned flags);
329};
330
331}  // end namespace clang
332
333#endif
334