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