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