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