Scope.h revision d661d50118716e9695af5a893a2df45e87a6b3c8
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 /// \brief This indicates that the scope corresponds to a function, which 36 /// means that labels are set here. 37 FnScope = 0x01, 38 39 /// \brief This is a while, do, switch, for, etc that can have break 40 /// statements embedded into it. 41 BreakScope = 0x02, 42 43 /// \brief This is a while, do, for, which can have continue statements 44 /// embedded into it. 45 ContinueScope = 0x04, 46 47 /// \brief 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 /// \brief The controlling scope in a if/switch/while/for statement. 52 ControlScope = 0x10, 53 54 /// \brief The scope of a struct/union/class definition. 55 ClassScope = 0x20, 56 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. 61 BlockScope = 0x40, 62 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, 68 69 /// \brief This is a scope that corresponds to the 70 /// parameters within a function prototype. 71 FunctionPrototypeScope = 0x100, 72 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 76 /// set as well. 77 FunctionDeclarationScope = 0x200, 78 79 /// \brief This is a scope that corresponds to the Objective-C 80 /// \@catch statement. 81 AtCatchScope = 0x400, 82 83 /// \brief This scope corresponds to an Objective-C method body. 84 /// It always has FnScope and DeclScope set as well. 85 ObjCMethodScope = 0x800, 86 87 /// \brief This is a scope that corresponds to a switch statement. 88 SwitchScope = 0x1000, 89 90 /// \brief This is the scope of a C++ try statement. 91 TryScope = 0x2000, 92 93 /// \brief This is the scope for a function-level C++ try or catch scope. 94 FnTryCatchScope = 0x4000, 95 96 /// \brief This is the scope of OpenMP executable directive 97 OpenMPDirectiveScope = 0x8000 98 }; 99private: 100 /// The parent scope for this scope. This is null for the translation-unit 101 /// scope. 102 Scope *AnyParent; 103 104 /// Depth - This is the depth of this scope. The translation-unit scope has 105 /// depth 0. 106 unsigned short Depth; 107 108 /// Flags - This contains a set of ScopeFlags, which indicates how the scope 109 /// interrelates with other control flow statements. 110 unsigned short Flags; 111 112 /// PrototypeDepth - This is the number of function prototype scopes 113 /// enclosing this scope, including this scope. 114 unsigned short PrototypeDepth; 115 116 /// PrototypeIndex - This is the number of parameters currently 117 /// declared in this scope. 118 unsigned short PrototypeIndex; 119 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. 122 Scope *FnParent; 123 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; 129 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. 132 Scope *BlockParent; 133 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; 139 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; 148 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. 152 DeclContext *Entity; 153 154 typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy; 155 UsingDirectivesTy UsingDirectives; 156 157 /// \brief Used to determine if errors occurred in this scope. 158 DiagnosticErrorTrap ErrorTrap; 159 160public: 161 Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag) 162 : ErrorTrap(Diag) { 163 Init(Parent, ScopeFlags); 164 } 165 166 /// getFlags - Return the flags for this scope. 167 /// 168 unsigned getFlags() const { return Flags; } 169 void setFlags(unsigned F) { Flags = F; } 170 171 /// isBlockScope - Return true if this scope correspond to a closure. 172 bool isBlockScope() const { return Flags & BlockScope; } 173 174 /// getParent - Return the scope that this is nested in. 175 /// 176 const Scope *getParent() const { return AnyParent; } 177 Scope *getParent() { return AnyParent; } 178 179 /// getFnParent - Return the closest scope that is a function body. 180 /// 181 const Scope *getFnParent() const { return FnParent; } 182 Scope *getFnParent() { return FnParent; } 183 184 /// getContinueParent - Return the closest scope that a continue statement 185 /// would be affected by. 186 Scope *getContinueParent() { 187 return ContinueParent; 188 } 189 190 const Scope *getContinueParent() const { 191 return const_cast<Scope*>(this)->getContinueParent(); 192 } 193 194 /// getBreakParent - Return the closest scope that a break statement 195 /// would be affected by. 196 Scope *getBreakParent() { 197 return BreakParent; 198 } 199 const Scope *getBreakParent() const { 200 return const_cast<Scope*>(this)->getBreakParent(); 201 } 202 203 Scope *getBlockParent() { return BlockParent; } 204 const Scope *getBlockParent() const { return BlockParent; } 205 206 Scope *getTemplateParamParent() { return TemplateParamParent; } 207 const Scope *getTemplateParamParent() const { return TemplateParamParent; } 208 209 /// Returns the number of function prototype scopes in this scope 210 /// chain. 211 unsigned getFunctionPrototypeDepth() const { 212 return PrototypeDepth; 213 } 214 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++; 220 } 221 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(); } 226 227 void AddDecl(Decl *D) { 228 DeclsInScope.insert(D); 229 } 230 231 void RemoveDecl(Decl *D) { 232 DeclsInScope.erase(D); 233 } 234 235 /// isDeclScope - Return true if this is the scope that the specified decl is 236 /// declared in. 237 bool isDeclScope(Decl *D) { 238 return DeclsInScope.count(D) != 0; 239 } 240 241 DeclContext *getEntity() const { return Entity; } 242 void setEntity(DeclContext *E) { Entity = E; } 243 244 bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); } 245 246 bool hasUnrecoverableErrorOccurred() const { 247 return ErrorTrap.hasUnrecoverableErrorOccurred(); 248 } 249 250 /// isClassScope - Return true if this scope is a class/struct/union scope. 251 bool isClassScope() const { 252 return (getFlags() & Scope::ClassScope); 253 } 254 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(); 261 } 262 return false; 263 } 264 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) 271 return true; 272 } 273 return false; 274 } 275 276 /// isTemplateParamScope - Return true if this scope is a C++ 277 /// template parameter scope. 278 bool isTemplateParamScope() const { 279 return getFlags() & Scope::TemplateParamScope; 280 } 281 282 /// isFunctionPrototypeScope - Return true if this scope is a 283 /// function prototype scope. 284 bool isFunctionPrototypeScope() const { 285 return getFlags() & Scope::FunctionPrototypeScope; 286 } 287 288 /// isAtCatchScope - Return true if this scope is \@catch. 289 bool isAtCatchScope() const { 290 return getFlags() & Scope::AtCatchScope; 291 } 292 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) 297 return true; 298 else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope | 299 Scope::BlockScope | Scope::TemplateParamScope | 300 Scope::FunctionPrototypeScope | 301 Scope::AtCatchScope | Scope::ObjCMethodScope)) 302 return false; 303 } 304 return false; 305 } 306 307 /// \brief Determines whether this scope is the OpenMP directive scope 308 bool isOpenMPDirectiveScope() const { 309 return (getFlags() & Scope::OpenMPDirectiveScope); 310 } 311 312 /// \brief Determine whether this scope is a C++ 'try' block. 313 bool isTryScope() const { return getFlags() & Scope::TryScope; } 314 315 /// containedInPrototypeScope - Return true if this or a parent scope 316 /// is a FunctionPrototypeScope. 317 bool containedInPrototypeScope() const; 318 319 typedef UsingDirectivesTy::iterator udir_iterator; 320 typedef UsingDirectivesTy::const_iterator const_udir_iterator; 321 322 void PushUsingDirective(UsingDirectiveDecl *UDir) { 323 UsingDirectives.push_back(UDir); 324 } 325 326 udir_iterator using_directives_begin() { 327 return UsingDirectives.begin(); 328 } 329 330 udir_iterator using_directives_end() { 331 return UsingDirectives.end(); 332 } 333 334 const_udir_iterator using_directives_begin() const { 335 return UsingDirectives.begin(); 336 } 337 338 const_udir_iterator using_directives_end() const { 339 return UsingDirectives.end(); 340 } 341 342 /// Init - This is used by the parser to implement scope caching. 343 /// 344 void Init(Scope *parent, unsigned flags); 345}; 346 347} // end namespace clang 348 349#endif 350