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 21 namespace clang { 22 23 class Decl; 24 class 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 /// 30 class Scope { 31 public: 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 private: 88 /// The parent scope for this scope. This is null for the translation-unit 89 /// scope. 90 Scope *AnyParent; 91 92 /// Depth - This is the depth of this scope. The translation-unit scope has 93 /// depth 0. 94 unsigned short Depth; 95 96 /// Flags - This contains a set of ScopeFlags, which indicates how the scope 97 /// interrelates with other control flow statements. 98 unsigned short Flags; 99 100 /// PrototypeDepth - This is the number of function prototype scopes 101 /// enclosing this scope, including this scope. 102 unsigned short PrototypeDepth; 103 104 /// PrototypeIndex - This is the number of parameters currently 105 /// declared in this scope. 106 unsigned short PrototypeIndex; 107 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. 110 Scope *FnParent; 111 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; 117 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. 120 Scope *BlockParent; 121 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; 127 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; 136 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. 141 void *Entity; 142 143 typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy; 144 UsingDirectivesTy UsingDirectives; 145 146 /// \brief Used to determine if errors occurred in this scope. 147 DiagnosticErrorTrap ErrorTrap; 148 149 public: Scope(Scope * Parent,unsigned ScopeFlags,DiagnosticsEngine & Diag)150 Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag) 151 : ErrorTrap(Diag) { 152 Init(Parent, ScopeFlags); 153 } 154 155 /// getFlags - Return the flags for this scope. 156 /// getFlags()157 unsigned getFlags() const { return Flags; } setFlags(unsigned F)158 void setFlags(unsigned F) { Flags = F; } 159 160 /// isBlockScope - Return true if this scope correspond to a closure. isBlockScope()161 bool isBlockScope() const { return Flags & BlockScope; } 162 163 /// getParent - Return the scope that this is nested in. 164 /// getParent()165 const Scope *getParent() const { return AnyParent; } getParent()166 Scope *getParent() { return AnyParent; } 167 168 /// getFnParent - Return the closest scope that is a function body. 169 /// getFnParent()170 const Scope *getFnParent() const { return FnParent; } getFnParent()171 Scope *getFnParent() { return FnParent; } 172 173 /// getContinueParent - Return the closest scope that a continue statement 174 /// would be affected by. getContinueParent()175 Scope *getContinueParent() { 176 return ContinueParent; 177 } 178 getContinueParent()179 const Scope *getContinueParent() const { 180 return const_cast<Scope*>(this)->getContinueParent(); 181 } 182 183 /// getBreakParent - Return the closest scope that a break statement 184 /// would be affected by. getBreakParent()185 Scope *getBreakParent() { 186 return BreakParent; 187 } getBreakParent()188 const Scope *getBreakParent() const { 189 return const_cast<Scope*>(this)->getBreakParent(); 190 } 191 getBlockParent()192 Scope *getBlockParent() { return BlockParent; } getBlockParent()193 const Scope *getBlockParent() const { return BlockParent; } 194 getTemplateParamParent()195 Scope *getTemplateParamParent() { return TemplateParamParent; } getTemplateParamParent()196 const Scope *getTemplateParamParent() const { return TemplateParamParent; } 197 198 /// Returns the number of function prototype scopes in this scope 199 /// chain. getFunctionPrototypeDepth()200 unsigned getFunctionPrototypeDepth() const { 201 return PrototypeDepth; 202 } 203 204 /// Return the number of parameters declared in this function 205 /// prototype, increasing it by one for the next call. getNextFunctionPrototypeIndex()206 unsigned getNextFunctionPrototypeIndex() { 207 assert(isFunctionPrototypeScope()); 208 return PrototypeIndex++; 209 } 210 211 typedef DeclSetTy::iterator decl_iterator; decl_begin()212 decl_iterator decl_begin() const { return DeclsInScope.begin(); } decl_end()213 decl_iterator decl_end() const { return DeclsInScope.end(); } decl_empty()214 bool decl_empty() const { return DeclsInScope.empty(); } 215 AddDecl(Decl * D)216 void AddDecl(Decl *D) { 217 DeclsInScope.insert(D); 218 } 219 RemoveDecl(Decl * D)220 void RemoveDecl(Decl *D) { 221 DeclsInScope.erase(D); 222 } 223 224 /// isDeclScope - Return true if this is the scope that the specified decl is 225 /// declared in. isDeclScope(Decl * D)226 bool isDeclScope(Decl *D) { 227 return DeclsInScope.count(D) != 0; 228 } 229 getEntity()230 void* getEntity() const { return Entity; } setEntity(void * E)231 void setEntity(void *E) { Entity = E; } 232 hasErrorOccurred()233 bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); } 234 235 /// isClassScope - Return true if this scope is a class/struct/union scope. isClassScope()236 bool isClassScope() const { 237 return (getFlags() & Scope::ClassScope); 238 } 239 240 /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline 241 /// method scope or is inside one. isInCXXInlineMethodScope()242 bool isInCXXInlineMethodScope() const { 243 if (const Scope *FnS = getFnParent()) { 244 assert(FnS->getParent() && "TUScope not created?"); 245 return FnS->getParent()->isClassScope(); 246 } 247 return false; 248 } 249 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. isInObjcMethodScope()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) 256 return true; 257 } 258 return false; 259 } 260 261 /// isTemplateParamScope - Return true if this scope is a C++ 262 /// template parameter scope. isTemplateParamScope()263 bool isTemplateParamScope() const { 264 return getFlags() & Scope::TemplateParamScope; 265 } 266 267 /// isFunctionPrototypeScope - Return true if this scope is a 268 /// function prototype scope. isFunctionPrototypeScope()269 bool isFunctionPrototypeScope() const { 270 return getFlags() & Scope::FunctionPrototypeScope; 271 } 272 273 /// isAtCatchScope - Return true if this scope is \@catch. isAtCatchScope()274 bool isAtCatchScope() const { 275 return getFlags() & Scope::AtCatchScope; 276 } 277 278 /// isSwitchScope - Return true if this scope is a switch scope. isSwitchScope()279 bool isSwitchScope() const { 280 for (const Scope *S = this; S; S = S->getParent()) { 281 if (S->getFlags() & Scope::SwitchScope) 282 return true; 283 else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope | 284 Scope::BlockScope | Scope::TemplateParamScope | 285 Scope::FunctionPrototypeScope | 286 Scope::AtCatchScope | Scope::ObjCMethodScope)) 287 return false; 288 } 289 return false; 290 } 291 292 /// \brief Determine whether this scope is a C++ 'try' block. isTryScope()293 bool isTryScope() const { return getFlags() & Scope::TryScope; } 294 295 /// containedInPrototypeScope - Return true if this or a parent scope 296 /// is a FunctionPrototypeScope. 297 bool containedInPrototypeScope() const; 298 299 typedef UsingDirectivesTy::iterator udir_iterator; 300 typedef UsingDirectivesTy::const_iterator const_udir_iterator; 301 PushUsingDirective(UsingDirectiveDecl * UDir)302 void PushUsingDirective(UsingDirectiveDecl *UDir) { 303 UsingDirectives.push_back(UDir); 304 } 305 using_directives_begin()306 udir_iterator using_directives_begin() { 307 return UsingDirectives.begin(); 308 } 309 using_directives_end()310 udir_iterator using_directives_end() { 311 return UsingDirectives.end(); 312 } 313 using_directives_begin()314 const_udir_iterator using_directives_begin() const { 315 return UsingDirectives.begin(); 316 } 317 using_directives_end()318 const_udir_iterator using_directives_end() const { 319 return UsingDirectives.end(); 320 } 321 322 /// Init - This is used by the parser to implement scope caching. 323 /// 324 void Init(Scope *parent, unsigned flags); 325 }; 326 327 } // end namespace clang 328 329 #endif 330