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