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 /// \brief This indicates that the scope corresponds to a function, which
36 /// means that labels are set here.
39 /// \brief This is a while, do, switch, for, etc that can have break
40 /// statements embedded into it.
43 /// \brief This is a while, do, for, which can have continue statements
47 /// \brief This is a scope that can contain a declaration. Some scopes
48 /// just contain loop constructs but don't contain decls.
51 /// \brief The controlling scope in a if/switch/while/for statement.
54 /// \brief The scope of a struct/union/class definition.
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.
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,
69 /// \brief This is a scope that corresponds to the
70 /// parameters within a function prototype.
71 FunctionPrototypeScope = 0x100,
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
77 FunctionDeclarationScope = 0x200,
79 /// \brief This is a scope that corresponds to the Objective-C
80 /// \@catch statement.
83 /// \brief This scope corresponds to an Objective-C method body.
84 /// It always has FnScope and DeclScope set as well.
85 ObjCMethodScope = 0x800,
87 /// \brief This is a scope that corresponds to a switch statement.
90 /// \brief This is the scope of a C++ try statement.
93 /// \brief This is the scope for a function-level C++ try or catch scope.
94 FnTryCatchScope = 0x4000,
96 /// \brief This is the scope of OpenMP executable directive
97 OpenMPDirectiveScope = 0x8000
100 /// The parent scope for this scope. This is null for the translation-unit
104 /// Depth - This is the depth of this scope. The translation-unit scope has
106 unsigned short Depth;
108 /// Flags - This contains a set of ScopeFlags, which indicates how the scope
109 /// interrelates with other control flow statements.
110 unsigned short Flags;
112 /// PrototypeDepth - This is the number of function prototype scopes
113 /// enclosing this scope, including this scope.
114 unsigned short PrototypeDepth;
116 /// PrototypeIndex - This is the number of parameters currently
117 /// declared in this scope.
118 unsigned short PrototypeIndex;
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.
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;
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.
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;
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;
149 /// The DeclContext 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.
154 typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
155 UsingDirectivesTy UsingDirectives;
157 /// \brief Used to determine if errors occurred in this scope.
158 DiagnosticErrorTrap ErrorTrap;
161 Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
163 Init(Parent, ScopeFlags);
166 /// getFlags - Return the flags for this scope.
168 unsigned getFlags() const { return Flags; }
169 void setFlags(unsigned F) { Flags = F; }
171 /// isBlockScope - Return true if this scope correspond to a closure.
172 bool isBlockScope() const { return Flags & BlockScope; }
174 /// getParent - Return the scope that this is nested in.
176 const Scope *getParent() const { return AnyParent; }
177 Scope *getParent() { return AnyParent; }
179 /// getFnParent - Return the closest scope that is a function body.
181 const Scope *getFnParent() const { return FnParent; }
182 Scope *getFnParent() { return FnParent; }
184 /// getContinueParent - Return the closest scope that a continue statement
185 /// would be affected by.
186 Scope *getContinueParent() {
187 return ContinueParent;
190 const Scope *getContinueParent() const {
191 return const_cast<Scope*>(this)->getContinueParent();
194 /// getBreakParent - Return the closest scope that a break statement
195 /// would be affected by.
196 Scope *getBreakParent() {
199 const Scope *getBreakParent() const {
200 return const_cast<Scope*>(this)->getBreakParent();
203 Scope *getBlockParent() { return BlockParent; }
204 const Scope *getBlockParent() const { return BlockParent; }
206 Scope *getTemplateParamParent() { return TemplateParamParent; }
207 const Scope *getTemplateParamParent() const { return TemplateParamParent; }
209 /// Returns the number of function prototype scopes in this scope
211 unsigned getFunctionPrototypeDepth() const {
212 return PrototypeDepth;
215 /// Return the number of parameters declared in this function
216 /// prototype, increasing it by one for the next call.
217 unsigned getNextFunctionPrototypeIndex() {
218 assert(isFunctionPrototypeScope());
219 return PrototypeIndex++;
222 typedef DeclSetTy::iterator decl_iterator;
223 decl_iterator decl_begin() const { return DeclsInScope.begin(); }
224 decl_iterator decl_end() const { return DeclsInScope.end(); }
225 bool decl_empty() const { return DeclsInScope.empty(); }
227 void AddDecl(Decl *D) {
228 DeclsInScope.insert(D);
231 void RemoveDecl(Decl *D) {
232 DeclsInScope.erase(D);
235 /// isDeclScope - Return true if this is the scope that the specified decl is
237 bool isDeclScope(Decl *D) {
238 return DeclsInScope.count(D) != 0;
241 DeclContext *getEntity() const { return Entity; }
242 void setEntity(DeclContext *E) { Entity = E; }
244 bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
246 bool hasUnrecoverableErrorOccurred() const {
247 return ErrorTrap.hasUnrecoverableErrorOccurred();
250 /// isClassScope - Return true if this scope is a class/struct/union scope.
251 bool isClassScope() const {
252 return (getFlags() & Scope::ClassScope);
255 /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
256 /// method scope or is inside one.
257 bool isInCXXInlineMethodScope() const {
258 if (const Scope *FnS = getFnParent()) {
259 assert(FnS->getParent() && "TUScope not created?");
260 return FnS->getParent()->isClassScope();
265 /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
266 /// Objective-C method body. Note that this method is not constant time.
267 bool isInObjcMethodScope() const {
268 for (const Scope *S = this; S; S = S->getParent()) {
269 // If this scope is an objc method scope, then we succeed.
270 if (S->getFlags() & ObjCMethodScope)
276 /// isTemplateParamScope - Return true if this scope is a C++
277 /// template parameter scope.
278 bool isTemplateParamScope() const {
279 return getFlags() & Scope::TemplateParamScope;
282 /// isFunctionPrototypeScope - Return true if this scope is a
283 /// function prototype scope.
284 bool isFunctionPrototypeScope() const {
285 return getFlags() & Scope::FunctionPrototypeScope;
288 /// isAtCatchScope - Return true if this scope is \@catch.
289 bool isAtCatchScope() const {
290 return getFlags() & Scope::AtCatchScope;
293 /// isSwitchScope - Return true if this scope is a switch scope.
294 bool isSwitchScope() const {
295 for (const Scope *S = this; S; S = S->getParent()) {
296 if (S->getFlags() & Scope::SwitchScope)
298 else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
299 Scope::BlockScope | Scope::TemplateParamScope |
300 Scope::FunctionPrototypeScope |
301 Scope::AtCatchScope | Scope::ObjCMethodScope))
307 /// \brief Determines whether this scope is the OpenMP directive scope
308 bool isOpenMPDirectiveScope() const {
309 return (getFlags() & Scope::OpenMPDirectiveScope);
312 /// \brief Determine whether this scope is a C++ 'try' block.
313 bool isTryScope() const { return getFlags() & Scope::TryScope; }
315 /// containedInPrototypeScope - Return true if this or a parent scope
316 /// is a FunctionPrototypeScope.
317 bool containedInPrototypeScope() const;
319 typedef UsingDirectivesTy::iterator udir_iterator;
320 typedef UsingDirectivesTy::const_iterator const_udir_iterator;
322 void PushUsingDirective(UsingDirectiveDecl *UDir) {
323 UsingDirectives.push_back(UDir);
326 udir_iterator using_directives_begin() {
327 return UsingDirectives.begin();
330 udir_iterator using_directives_end() {
331 return UsingDirectives.end();
334 const_udir_iterator using_directives_begin() const {
335 return UsingDirectives.begin();
338 const_udir_iterator using_directives_end() const {
339 return UsingDirectives.end();
342 /// Init - This is used by the parser to implement scope caching.
344 void Init(Scope *parent, unsigned flags);
347 } // end namespace clang