1 //===--- Scope.h - Scope interface ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Scope interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SEMA_SCOPE_H
15 #define LLVM_CLANG_SEMA_SCOPE_H
17 #include "clang/Basic/Diagnostic.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
24 class UsingDirectiveDecl;
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
32 /// ScopeFlags - These are bitfields that are or'd together when creating a
33 /// scope, which defines the sorts of things the scope contains.
35 /// FnScope - This indicates that the scope corresponds to a function, which
36 /// means that labels are set here.
39 /// BreakScope - This is a while,do,switch,for, etc that can have break
40 /// stmts embedded into it.
43 /// ContinueScope - This is a while,do,for, which can have continue
44 /// stmt embedded into it.
47 /// DeclScope - This is a scope that can contain a declaration. Some scopes
48 /// just contain loop constructs but don't contain decls.
51 /// ControlScope - The controlling scope in a if/switch/while/for statement.
54 /// ClassScope - The scope of a struct/union/class definition.
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.
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,
69 /// FunctionPrototypeScope - This is a scope that corresponds to the
70 /// parameters within a function prototype.
71 FunctionPrototypeScope = 0x100,
73 /// AtCatchScope - This is a scope that corresponds to the Objective-C
77 /// ObjCMethodScope - This scope corresponds to an Objective-C method body.
78 /// It always has FnScope and DeclScope set as well.
79 ObjCMethodScope = 0x400,
81 /// SwitchScope - This is a scope that corresponds to a switch statement.
84 /// TryScope - This is the scope of a C++ try statement.
88 /// The parent scope for this scope. This is null for the translation-unit
92 /// Depth - This is the depth of this scope. The translation-unit scope has
96 /// Flags - This contains a set of ScopeFlags, which indicates how the scope
97 /// interrelates with other control flow statements.
100 /// PrototypeDepth - This is the number of function prototype scopes
101 /// enclosing this scope, including this scope.
102 unsigned short PrototypeDepth;
104 /// PrototypeIndex - This is the number of parameters currently
105 /// declared in this scope.
106 unsigned short PrototypeIndex;
108 /// FnParent - If this scope has a parent scope that is a function body, this
109 /// pointer is non-null and points to it. This is used for label processing.
112 /// BreakParent/ContinueParent - This is a direct link to the innermost
113 /// BreakScope/ContinueScope which contains the contents of this scope
114 /// for control flow purposes (and might be this scope itself), or null
115 /// if there is no such scope.
116 Scope *BreakParent, *ContinueParent;
118 /// BlockParent - This is a direct link to the immediately containing
119 /// BlockScope if this scope is not one, or null if there is none.
122 /// TemplateParamParent - This is a direct link to the
123 /// immediately containing template parameter scope. In the
124 /// case of nested templates, template parameter scopes can have
125 /// other template parameter scopes as parents.
126 Scope *TemplateParamParent;
128 /// DeclsInScope - This keeps track of all declarations in this scope. When
129 /// the declaration is added to the scope, it is set as the current
130 /// declaration for the identifier in the IdentifierTable. When the scope is
131 /// popped, these declarations are removed from the IdentifierTable's notion
132 /// of current declaration. It is up to the current Action implementation to
133 /// implement these semantics.
134 typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy;
135 DeclSetTy DeclsInScope;
137 /// Entity - The entity with which this scope is associated. For
138 /// example, the entity of a class scope is the class itself, the
139 /// entity of a function scope is a function, etc. This field is
140 /// maintained by the Action implementation.
143 typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
144 UsingDirectivesTy UsingDirectives;
146 /// \brief Used to determine if errors occurred in this scope.
147 DiagnosticErrorTrap ErrorTrap;
150 Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
152 Init(Parent, ScopeFlags);
155 /// getFlags - Return the flags for this scope.
157 unsigned getFlags() const { return Flags; }
158 void setFlags(unsigned F) { Flags = F; }
160 /// isBlockScope - Return true if this scope correspond to a closure.
161 bool isBlockScope() const { return Flags & BlockScope; }
163 /// getParent - Return the scope that this is nested in.
165 const Scope *getParent() const { return AnyParent; }
166 Scope *getParent() { return AnyParent; }
168 /// getFnParent - Return the closest scope that is a function body.
170 const Scope *getFnParent() const { return FnParent; }
171 Scope *getFnParent() { return FnParent; }
173 /// getContinueParent - Return the closest scope that a continue statement
174 /// would be affected by.
175 Scope *getContinueParent() {
176 return ContinueParent;
179 const Scope *getContinueParent() const {
180 return const_cast<Scope*>(this)->getContinueParent();
183 /// getBreakParent - Return the closest scope that a break statement
184 /// would be affected by.
185 Scope *getBreakParent() {
188 const Scope *getBreakParent() const {
189 return const_cast<Scope*>(this)->getBreakParent();
192 Scope *getBlockParent() { return BlockParent; }
193 const Scope *getBlockParent() const { return BlockParent; }
195 Scope *getTemplateParamParent() { return TemplateParamParent; }
196 const Scope *getTemplateParamParent() const { return TemplateParamParent; }
198 /// Returns the number of function prototype scopes in this scope
200 unsigned getFunctionPrototypeDepth() const {
201 return PrototypeDepth;
204 /// Return the number of parameters declared in this function
205 /// prototype, increasing it by one for the next call.
206 unsigned getNextFunctionPrototypeIndex() {
207 assert(isFunctionPrototypeScope());
208 return PrototypeIndex++;
211 typedef DeclSetTy::iterator decl_iterator;
212 decl_iterator decl_begin() const { return DeclsInScope.begin(); }
213 decl_iterator decl_end() const { return DeclsInScope.end(); }
214 bool decl_empty() const { return DeclsInScope.empty(); }
216 void AddDecl(Decl *D) {
217 DeclsInScope.insert(D);
220 void RemoveDecl(Decl *D) {
221 DeclsInScope.erase(D);
224 /// isDeclScope - Return true if this is the scope that the specified decl is
226 bool isDeclScope(Decl *D) {
227 return DeclsInScope.count(D) != 0;
230 void* getEntity() const { return Entity; }
231 void setEntity(void *E) { Entity = E; }
233 bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
235 /// isClassScope - Return true if this scope is a class/struct/union scope.
236 bool isClassScope() const {
237 return (getFlags() & Scope::ClassScope);
240 /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
241 /// method scope or is inside one.
242 bool isInCXXInlineMethodScope() const {
243 if (const Scope *FnS = getFnParent()) {
244 assert(FnS->getParent() && "TUScope not created?");
245 return FnS->getParent()->isClassScope();
250 /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
251 /// Objective-C method body. Note that this method is not constant time.
252 bool isInObjcMethodScope() const {
253 for (const Scope *S = this; S; S = S->getParent()) {
254 // If this scope is an objc method scope, then we succeed.
255 if (S->getFlags() & ObjCMethodScope)
261 /// isTemplateParamScope - Return true if this scope is a C++
262 /// template parameter scope.
263 bool isTemplateParamScope() const {
264 return getFlags() & Scope::TemplateParamScope;
267 /// isFunctionPrototypeScope - Return true if this scope is a
268 /// function prototype scope.
269 bool isFunctionPrototypeScope() const {
270 return getFlags() & Scope::FunctionPrototypeScope;
273 /// isAtCatchScope - Return true if this scope is @catch.
274 bool isAtCatchScope() const {
275 return getFlags() & Scope::AtCatchScope;
278 /// isSwitchScope - Return true if this scope is a switch scope.
279 bool isSwitchScope() const {
280 for (const Scope *S = this; S; S = S->getParent()) {
281 if (S->getFlags() & Scope::SwitchScope)
283 else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
284 Scope::BlockScope | Scope::TemplateParamScope |
285 Scope::FunctionPrototypeScope |
286 Scope::AtCatchScope | Scope::ObjCMethodScope))
292 /// \brief Determine whether this scope is a C++ 'try' block.
293 bool isTryScope() const { return getFlags() & Scope::TryScope; }
295 /// containedInPrototypeScope - Return true if this or a parent scope
296 /// is a FunctionPrototypeScope.
297 bool containedInPrototypeScope() const;
299 typedef UsingDirectivesTy::iterator udir_iterator;
300 typedef UsingDirectivesTy::const_iterator const_udir_iterator;
302 void PushUsingDirective(UsingDirectiveDecl *UDir) {
303 UsingDirectives.push_back(UDir);
306 udir_iterator using_directives_begin() {
307 return UsingDirectives.begin();
310 udir_iterator using_directives_end() {
311 return UsingDirectives.end();
314 const_udir_iterator using_directives_begin() const {
315 return UsingDirectives.begin();
318 const_udir_iterator using_directives_end() const {
319 return UsingDirectives.end();
322 /// Init - This is used by the parser to implement scope caching.
324 void Init(Scope *parent, unsigned flags);
327 } // end namespace clang