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/PointerIntPair.h" 19 #include "llvm/ADT/SmallPtrSet.h" 20 #include "llvm/ADT/SmallVector.h" 21 22 namespace llvm { 23 24 class raw_ostream; 25 26 } 27 28 namespace clang { 29 30 class Decl; 31 class UsingDirectiveDecl; 32 class VarDecl; 33 34 /// Scope - A scope is a transient data structure that is used while parsing the 35 /// program. It assists with resolving identifiers to the appropriate 36 /// declaration. 37 /// 38 class Scope { 39 public: 40 /// ScopeFlags - These are bitfields that are or'd together when creating a 41 /// scope, which defines the sorts of things the scope contains. 42 enum ScopeFlags { 43 /// \brief This indicates that the scope corresponds to a function, which 44 /// means that labels are set here. 45 FnScope = 0x01, 46 47 /// \brief This is a while, do, switch, for, etc that can have break 48 /// statements embedded into it. 49 BreakScope = 0x02, 50 51 /// \brief This is a while, do, for, which can have continue statements 52 /// embedded into it. 53 ContinueScope = 0x04, 54 55 /// \brief This is a scope that can contain a declaration. Some scopes 56 /// just contain loop constructs but don't contain decls. 57 DeclScope = 0x08, 58 59 /// \brief The controlling scope in a if/switch/while/for statement. 60 ControlScope = 0x10, 61 62 /// \brief The scope of a struct/union/class definition. 63 ClassScope = 0x20, 64 65 /// \brief This is a scope that corresponds to a block/closure object. 66 /// Blocks serve as top-level scopes for some objects like labels, they 67 /// also prevent things like break and continue. BlockScopes always have 68 /// the FnScope and DeclScope flags set as well. 69 BlockScope = 0x40, 70 71 /// \brief This is a scope that corresponds to the 72 /// template parameters of a C++ template. Template parameter 73 /// scope starts at the 'template' keyword and ends when the 74 /// template declaration ends. 75 TemplateParamScope = 0x80, 76 77 /// \brief This is a scope that corresponds to the 78 /// parameters within a function prototype. 79 FunctionPrototypeScope = 0x100, 80 81 /// \brief This is a scope that corresponds to the parameters within 82 /// a function prototype for a function declaration (as opposed to any 83 /// other kind of function declarator). Always has FunctionPrototypeScope 84 /// set as well. 85 FunctionDeclarationScope = 0x200, 86 87 /// \brief This is a scope that corresponds to the Objective-C 88 /// \@catch statement. 89 AtCatchScope = 0x400, 90 91 /// \brief This scope corresponds to an Objective-C method body. 92 /// It always has FnScope and DeclScope set as well. 93 ObjCMethodScope = 0x800, 94 95 /// \brief This is a scope that corresponds to a switch statement. 96 SwitchScope = 0x1000, 97 98 /// \brief This is the scope of a C++ try statement. 99 TryScope = 0x2000, 100 101 /// \brief This is the scope for a function-level C++ try or catch scope. 102 FnTryCatchScope = 0x4000, 103 104 /// \brief This is the scope of OpenMP executable directive. 105 OpenMPDirectiveScope = 0x8000, 106 107 /// \brief This is the scope of some OpenMP loop directive. 108 OpenMPLoopDirectiveScope = 0x10000, 109 110 /// \brief This is the scope of some OpenMP simd directive. 111 /// For example, it is used for 'omp simd', 'omp for simd'. 112 /// This flag is propagated to children scopes. 113 OpenMPSimdDirectiveScope = 0x20000, 114 115 /// This scope corresponds to an enum. 116 EnumScope = 0x40000, 117 118 /// This scope corresponds to a SEH try. 119 SEHTryScope = 0x80000, 120 }; 121 private: 122 /// The parent scope for this scope. This is null for the translation-unit 123 /// scope. 124 Scope *AnyParent; 125 126 /// Flags - This contains a set of ScopeFlags, which indicates how the scope 127 /// interrelates with other control flow statements. 128 unsigned Flags; 129 130 /// Depth - This is the depth of this scope. The translation-unit scope has 131 /// depth 0. 132 unsigned short Depth; 133 134 /// \brief Declarations with static linkage are mangled with the number of 135 /// scopes seen as a component. 136 unsigned short MSLocalManglingNumber; 137 138 /// PrototypeDepth - This is the number of function prototype scopes 139 /// enclosing this scope, including this scope. 140 unsigned short PrototypeDepth; 141 142 /// PrototypeIndex - This is the number of parameters currently 143 /// declared in this scope. 144 unsigned short PrototypeIndex; 145 146 /// FnParent - If this scope has a parent scope that is a function body, this 147 /// pointer is non-null and points to it. This is used for label processing. 148 Scope *FnParent; 149 Scope *MSLocalManglingParent; 150 151 /// BreakParent/ContinueParent - This is a direct link to the innermost 152 /// BreakScope/ContinueScope which contains the contents of this scope 153 /// for control flow purposes (and might be this scope itself), or null 154 /// if there is no such scope. 155 Scope *BreakParent, *ContinueParent; 156 157 /// BlockParent - This is a direct link to the immediately containing 158 /// BlockScope if this scope is not one, or null if there is none. 159 Scope *BlockParent; 160 161 /// TemplateParamParent - This is a direct link to the 162 /// immediately containing template parameter scope. In the 163 /// case of nested templates, template parameter scopes can have 164 /// other template parameter scopes as parents. 165 Scope *TemplateParamParent; 166 167 /// DeclsInScope - This keeps track of all declarations in this scope. When 168 /// the declaration is added to the scope, it is set as the current 169 /// declaration for the identifier in the IdentifierTable. When the scope is 170 /// popped, these declarations are removed from the IdentifierTable's notion 171 /// of current declaration. It is up to the current Action implementation to 172 /// implement these semantics. 173 typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy; 174 DeclSetTy DeclsInScope; 175 176 /// The DeclContext with which this scope is associated. For 177 /// example, the entity of a class scope is the class itself, the 178 /// entity of a function scope is a function, etc. 179 DeclContext *Entity; 180 181 typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy; 182 UsingDirectivesTy UsingDirectives; 183 184 /// \brief Used to determine if errors occurred in this scope. 185 DiagnosticErrorTrap ErrorTrap; 186 187 /// A lattice consisting of undefined, a single NRVO candidate variable in 188 /// this scope, or over-defined. The bit is true when over-defined. 189 llvm::PointerIntPair<VarDecl *, 1, bool> NRVO; 190 191 public: Scope(Scope * Parent,unsigned ScopeFlags,DiagnosticsEngine & Diag)192 Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag) 193 : ErrorTrap(Diag) { 194 Init(Parent, ScopeFlags); 195 } 196 197 /// getFlags - Return the flags for this scope. 198 /// getFlags()199 unsigned getFlags() const { return Flags; } setFlags(unsigned F)200 void setFlags(unsigned F) { Flags = F; } 201 202 /// isBlockScope - Return true if this scope correspond to a closure. isBlockScope()203 bool isBlockScope() const { return Flags & BlockScope; } 204 205 /// getParent - Return the scope that this is nested in. 206 /// getParent()207 const Scope *getParent() const { return AnyParent; } getParent()208 Scope *getParent() { return AnyParent; } 209 210 /// getFnParent - Return the closest scope that is a function body. 211 /// getFnParent()212 const Scope *getFnParent() const { return FnParent; } getFnParent()213 Scope *getFnParent() { return FnParent; } 214 getMSLocalManglingParent()215 const Scope *getMSLocalManglingParent() const { 216 return MSLocalManglingParent; 217 } getMSLocalManglingParent()218 Scope *getMSLocalManglingParent() { return MSLocalManglingParent; } 219 220 /// getContinueParent - Return the closest scope that a continue statement 221 /// would be affected by. getContinueParent()222 Scope *getContinueParent() { 223 return ContinueParent; 224 } 225 getContinueParent()226 const Scope *getContinueParent() const { 227 return const_cast<Scope*>(this)->getContinueParent(); 228 } 229 230 /// getBreakParent - Return the closest scope that a break statement 231 /// would be affected by. getBreakParent()232 Scope *getBreakParent() { 233 return BreakParent; 234 } getBreakParent()235 const Scope *getBreakParent() const { 236 return const_cast<Scope*>(this)->getBreakParent(); 237 } 238 getBlockParent()239 Scope *getBlockParent() { return BlockParent; } getBlockParent()240 const Scope *getBlockParent() const { return BlockParent; } 241 getTemplateParamParent()242 Scope *getTemplateParamParent() { return TemplateParamParent; } getTemplateParamParent()243 const Scope *getTemplateParamParent() const { return TemplateParamParent; } 244 245 /// Returns the number of function prototype scopes in this scope 246 /// chain. getFunctionPrototypeDepth()247 unsigned getFunctionPrototypeDepth() const { 248 return PrototypeDepth; 249 } 250 251 /// Return the number of parameters declared in this function 252 /// prototype, increasing it by one for the next call. getNextFunctionPrototypeIndex()253 unsigned getNextFunctionPrototypeIndex() { 254 assert(isFunctionPrototypeScope()); 255 return PrototypeIndex++; 256 } 257 258 typedef llvm::iterator_range<DeclSetTy::iterator> decl_range; decls()259 decl_range decls() const { 260 return decl_range(DeclsInScope.begin(), DeclsInScope.end()); 261 } decl_empty()262 bool decl_empty() const { return DeclsInScope.empty(); } 263 AddDecl(Decl * D)264 void AddDecl(Decl *D) { 265 DeclsInScope.insert(D); 266 } 267 RemoveDecl(Decl * D)268 void RemoveDecl(Decl *D) { 269 DeclsInScope.erase(D); 270 } 271 incrementMSLocalManglingNumber()272 void incrementMSLocalManglingNumber() { 273 if (Scope *MSLMP = getMSLocalManglingParent()) 274 MSLMP->MSLocalManglingNumber += 1; 275 } 276 decrementMSLocalManglingNumber()277 void decrementMSLocalManglingNumber() { 278 if (Scope *MSLMP = getMSLocalManglingParent()) 279 MSLMP->MSLocalManglingNumber -= 1; 280 } 281 getMSLocalManglingNumber()282 unsigned getMSLocalManglingNumber() const { 283 if (const Scope *MSLMP = getMSLocalManglingParent()) 284 return MSLMP->MSLocalManglingNumber; 285 return 1; 286 } 287 288 /// isDeclScope - Return true if this is the scope that the specified decl is 289 /// declared in. isDeclScope(Decl * D)290 bool isDeclScope(Decl *D) { 291 return DeclsInScope.count(D) != 0; 292 } 293 getEntity()294 DeclContext *getEntity() const { return Entity; } setEntity(DeclContext * E)295 void setEntity(DeclContext *E) { Entity = E; } 296 hasErrorOccurred()297 bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); } 298 hasUnrecoverableErrorOccurred()299 bool hasUnrecoverableErrorOccurred() const { 300 return ErrorTrap.hasUnrecoverableErrorOccurred(); 301 } 302 303 /// isClassScope - Return true if this scope is a class/struct/union scope. isClassScope()304 bool isClassScope() const { 305 return (getFlags() & Scope::ClassScope); 306 } 307 308 /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline 309 /// method scope or is inside one. isInCXXInlineMethodScope()310 bool isInCXXInlineMethodScope() const { 311 if (const Scope *FnS = getFnParent()) { 312 assert(FnS->getParent() && "TUScope not created?"); 313 return FnS->getParent()->isClassScope(); 314 } 315 return false; 316 } 317 318 /// isInObjcMethodScope - Return true if this scope is, or is contained in, an 319 /// Objective-C method body. Note that this method is not constant time. isInObjcMethodScope()320 bool isInObjcMethodScope() const { 321 for (const Scope *S = this; S; S = S->getParent()) { 322 // If this scope is an objc method scope, then we succeed. 323 if (S->getFlags() & ObjCMethodScope) 324 return true; 325 } 326 return false; 327 } 328 329 /// isInObjcMethodOuterScope - Return true if this scope is an 330 /// Objective-C method outer most body. isInObjcMethodOuterScope()331 bool isInObjcMethodOuterScope() const { 332 if (const Scope *S = this) { 333 // If this scope is an objc method scope, then we succeed. 334 if (S->getFlags() & ObjCMethodScope) 335 return true; 336 } 337 return false; 338 } 339 340 341 /// isTemplateParamScope - Return true if this scope is a C++ 342 /// template parameter scope. isTemplateParamScope()343 bool isTemplateParamScope() const { 344 return getFlags() & Scope::TemplateParamScope; 345 } 346 347 /// isFunctionPrototypeScope - Return true if this scope is a 348 /// function prototype scope. isFunctionPrototypeScope()349 bool isFunctionPrototypeScope() const { 350 return getFlags() & Scope::FunctionPrototypeScope; 351 } 352 353 /// isAtCatchScope - Return true if this scope is \@catch. isAtCatchScope()354 bool isAtCatchScope() const { 355 return getFlags() & Scope::AtCatchScope; 356 } 357 358 /// isSwitchScope - Return true if this scope is a switch scope. isSwitchScope()359 bool isSwitchScope() const { 360 for (const Scope *S = this; S; S = S->getParent()) { 361 if (S->getFlags() & Scope::SwitchScope) 362 return true; 363 else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope | 364 Scope::BlockScope | Scope::TemplateParamScope | 365 Scope::FunctionPrototypeScope | 366 Scope::AtCatchScope | Scope::ObjCMethodScope)) 367 return false; 368 } 369 return false; 370 } 371 372 /// \brief Determines whether this scope is the OpenMP directive scope isOpenMPDirectiveScope()373 bool isOpenMPDirectiveScope() const { 374 return (getFlags() & Scope::OpenMPDirectiveScope); 375 } 376 377 /// \brief Determine whether this scope is some OpenMP loop directive scope 378 /// (for example, 'omp for', 'omp simd'). isOpenMPLoopDirectiveScope()379 bool isOpenMPLoopDirectiveScope() const { 380 if (getFlags() & Scope::OpenMPLoopDirectiveScope) { 381 assert(isOpenMPDirectiveScope() && 382 "OpenMP loop directive scope is not a directive scope"); 383 return true; 384 } 385 return false; 386 } 387 388 /// \brief Determine whether this scope is (or is nested into) some OpenMP 389 /// loop simd directive scope (for example, 'omp simd', 'omp for simd'). isOpenMPSimdDirectiveScope()390 bool isOpenMPSimdDirectiveScope() const { 391 return getFlags() & Scope::OpenMPSimdDirectiveScope; 392 } 393 394 /// \brief Determine whether this scope is a loop having OpenMP loop 395 /// directive attached. isOpenMPLoopScope()396 bool isOpenMPLoopScope() const { 397 const Scope *P = getParent(); 398 return P && P->isOpenMPLoopDirectiveScope(); 399 } 400 401 /// \brief Determine whether this scope is a C++ 'try' block. isTryScope()402 bool isTryScope() const { return getFlags() & Scope::TryScope; } 403 404 /// \brief Determine whether this scope is a SEH '__try' block. isSEHTryScope()405 bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; } 406 407 /// containedInPrototypeScope - Return true if this or a parent scope 408 /// is a FunctionPrototypeScope. 409 bool containedInPrototypeScope() const; 410 PushUsingDirective(UsingDirectiveDecl * UDir)411 void PushUsingDirective(UsingDirectiveDecl *UDir) { 412 UsingDirectives.push_back(UDir); 413 } 414 415 typedef llvm::iterator_range<UsingDirectivesTy::iterator> 416 using_directives_range; 417 using_directives()418 using_directives_range using_directives() { 419 return using_directives_range(UsingDirectives.begin(), 420 UsingDirectives.end()); 421 } 422 addNRVOCandidate(VarDecl * VD)423 void addNRVOCandidate(VarDecl *VD) { 424 if (NRVO.getInt()) 425 return; 426 if (NRVO.getPointer() == nullptr) { 427 NRVO.setPointer(VD); 428 return; 429 } 430 if (NRVO.getPointer() != VD) 431 setNoNRVO(); 432 } 433 setNoNRVO()434 void setNoNRVO() { 435 NRVO.setInt(1); 436 NRVO.setPointer(nullptr); 437 } 438 439 void mergeNRVOIntoParent(); 440 441 /// Init - This is used by the parser to implement scope caching. 442 /// 443 void Init(Scope *parent, unsigned flags); 444 445 /// \brief Sets up the specified scope flags and adjusts the scope state 446 /// variables accordingly. 447 /// 448 void AddFlags(unsigned Flags); 449 450 void dumpImpl(raw_ostream &OS) const; 451 void dump() const; 452 }; 453 454 } // end namespace clang 455 456 #endif 457