1 //===- Scope.h - Scope interface --------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the Scope interface. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_SEMA_SCOPE_H 14 #define LLVM_CLANG_SEMA_SCOPE_H 15 16 #include "clang/AST/Decl.h" 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 #include "llvm/ADT/iterator_range.h" 22 #include <cassert> 23 #include <optional> 24 25 namespace llvm { 26 27 class raw_ostream; 28 29 } // namespace llvm 30 31 namespace clang { 32 33 class Decl; 34 class DeclContext; 35 class UsingDirectiveDecl; 36 class VarDecl; 37 38 /// Scope - A scope is a transient data structure that is used while parsing the 39 /// program. It assists with resolving identifiers to the appropriate 40 /// declaration. 41 class Scope { 42 public: 43 /// ScopeFlags - These are bitfields that are or'd together when creating a 44 /// scope, which defines the sorts of things the scope contains. 45 enum ScopeFlags { 46 // A bitfield value representing no scopes. 47 NoScope = 0, 48 49 /// This indicates that the scope corresponds to a function, which 50 /// means that labels are set here. 51 FnScope = 0x01, 52 53 /// This is a while, do, switch, for, etc that can have break 54 /// statements embedded into it. 55 BreakScope = 0x02, 56 57 /// This is a while, do, for, which can have continue statements 58 /// embedded into it. 59 ContinueScope = 0x04, 60 61 /// This is a scope that can contain a declaration. Some scopes 62 /// just contain loop constructs but don't contain decls. 63 DeclScope = 0x08, 64 65 /// The controlling scope in a if/switch/while/for statement. 66 ControlScope = 0x10, 67 68 /// The scope of a struct/union/class definition. 69 ClassScope = 0x20, 70 71 /// This is a scope that corresponds to a block/closure object. 72 /// Blocks serve as top-level scopes for some objects like labels, they 73 /// also prevent things like break and continue. BlockScopes always have 74 /// the FnScope and DeclScope flags set as well. 75 BlockScope = 0x40, 76 77 /// This is a scope that corresponds to the 78 /// template parameters of a C++ template. Template parameter 79 /// scope starts at the 'template' keyword and ends when the 80 /// template declaration ends. 81 TemplateParamScope = 0x80, 82 83 /// This is a scope that corresponds to the 84 /// parameters within a function prototype. 85 FunctionPrototypeScope = 0x100, 86 87 /// This is a scope that corresponds to the parameters within 88 /// a function prototype for a function declaration (as opposed to any 89 /// other kind of function declarator). Always has FunctionPrototypeScope 90 /// set as well. 91 FunctionDeclarationScope = 0x200, 92 93 /// This is a scope that corresponds to the Objective-C 94 /// \@catch statement. 95 AtCatchScope = 0x400, 96 97 /// This scope corresponds to an Objective-C method body. 98 /// It always has FnScope and DeclScope set as well. 99 ObjCMethodScope = 0x800, 100 101 /// This is a scope that corresponds to a switch statement. 102 SwitchScope = 0x1000, 103 104 /// This is the scope of a C++ try statement. 105 TryScope = 0x2000, 106 107 /// This is the scope for a function-level C++ try or catch scope. 108 FnTryCatchScope = 0x4000, 109 110 /// This is the scope of OpenMP executable directive. 111 OpenMPDirectiveScope = 0x8000, 112 113 /// This is the scope of some OpenMP loop directive. 114 OpenMPLoopDirectiveScope = 0x10000, 115 116 /// This is the scope of some OpenMP simd directive. 117 /// For example, it is used for 'omp simd', 'omp for simd'. 118 /// This flag is propagated to children scopes. 119 OpenMPSimdDirectiveScope = 0x20000, 120 121 /// This scope corresponds to an enum. 122 EnumScope = 0x40000, 123 124 /// This scope corresponds to an SEH try. 125 SEHTryScope = 0x80000, 126 127 /// This scope corresponds to an SEH except. 128 SEHExceptScope = 0x100000, 129 130 /// We are currently in the filter expression of an SEH except block. 131 SEHFilterScope = 0x200000, 132 133 /// This is a compound statement scope. 134 CompoundStmtScope = 0x400000, 135 136 /// We are between inheritance colon and the real class/struct definition 137 /// scope. 138 ClassInheritanceScope = 0x800000, 139 140 /// This is the scope of a C++ catch statement. 141 CatchScope = 0x1000000, 142 143 /// This is a scope in which a condition variable is currently being 144 /// parsed. If such a scope is a ContinueScope, it's invalid to jump to the 145 /// continue block from here. 146 ConditionVarScope = 0x2000000, 147 148 /// This is a scope of some OpenMP directive with 149 /// order clause which specifies concurrent 150 OpenMPOrderClauseScope = 0x4000000, 151 /// This is the scope for a lambda, after the lambda introducer. 152 /// Lambdas need two FunctionPrototypeScope scopes (because there is a 153 /// template scope in between), the outer scope does not increase the 154 /// depth of recursion. 155 LambdaScope = 0x8000000, 156 /// This is the scope of an OpenACC Compute Construct, which restricts 157 /// jumping into/out of it. We also use this to represent 'combined' 158 /// constructs, since they have the same behavior. 159 OpenACCComputeConstructScope = 0x10000000, 160 161 /// This is a scope of type alias declaration. 162 TypeAliasScope = 0x20000000, 163 164 /// This is a scope of friend declaration. 165 FriendScope = 0x40000000, 166 }; 167 168 private: 169 /// The parent scope for this scope. This is null for the translation-unit 170 /// scope. 171 Scope *AnyParent; 172 173 /// Flags - This contains a set of ScopeFlags, which indicates how the scope 174 /// interrelates with other control flow statements. 175 unsigned Flags; 176 177 /// Depth - This is the depth of this scope. The translation-unit scope has 178 /// depth 0. 179 unsigned short Depth; 180 181 /// Declarations with static linkage are mangled with the number of 182 /// scopes seen as a component. 183 unsigned short MSLastManglingNumber; 184 185 unsigned short MSCurManglingNumber; 186 187 /// PrototypeDepth - This is the number of function prototype scopes 188 /// enclosing this scope, including this scope. 189 unsigned short PrototypeDepth; 190 191 /// PrototypeIndex - This is the number of parameters currently 192 /// declared in this scope. 193 unsigned short PrototypeIndex; 194 195 /// FnParent - If this scope has a parent scope that is a function body, this 196 /// pointer is non-null and points to it. This is used for label processing. 197 Scope *FnParent; 198 Scope *MSLastManglingParent; 199 200 /// BreakParent/ContinueParent - This is a direct link to the innermost 201 /// BreakScope/ContinueScope which contains the contents of this scope 202 /// for control flow purposes (and might be this scope itself), or null 203 /// if there is no such scope. 204 Scope *BreakParent, *ContinueParent; 205 206 /// BlockParent - This is a direct link to the immediately containing 207 /// BlockScope if this scope is not one, or null if there is none. 208 Scope *BlockParent; 209 210 /// TemplateParamParent - This is a direct link to the 211 /// immediately containing template parameter scope. In the 212 /// case of nested templates, template parameter scopes can have 213 /// other template parameter scopes as parents. 214 Scope *TemplateParamParent; 215 216 /// DeclScopeParent - This is a direct link to the immediately containing 217 /// DeclScope, i.e. scope which can contain declarations. 218 Scope *DeclParent; 219 220 /// DeclsInScope - This keeps track of all declarations in this scope. When 221 /// the declaration is added to the scope, it is set as the current 222 /// declaration for the identifier in the IdentifierTable. When the scope is 223 /// popped, these declarations are removed from the IdentifierTable's notion 224 /// of current declaration. It is up to the current Action implementation to 225 /// implement these semantics. 226 using DeclSetTy = llvm::SmallPtrSet<Decl *, 32>; 227 DeclSetTy DeclsInScope; 228 229 /// The DeclContext with which this scope is associated. For 230 /// example, the entity of a class scope is the class itself, the 231 /// entity of a function scope is a function, etc. 232 DeclContext *Entity; 233 234 using UsingDirectivesTy = SmallVector<UsingDirectiveDecl *, 2>; 235 UsingDirectivesTy UsingDirectives; 236 237 /// Used to determine if errors occurred in this scope. 238 DiagnosticErrorTrap ErrorTrap; 239 240 /// A single NRVO candidate variable in this scope. 241 /// There are three possible values: 242 /// 1) pointer to VarDecl that denotes NRVO candidate itself. 243 /// 2) nullptr value means that NRVO is not allowed in this scope 244 /// (e.g. return a function parameter). 245 /// 3) std::nullopt value means that there is no NRVO candidate in this scope 246 /// (i.e. there are no return statements in this scope). 247 std::optional<VarDecl *> NRVO; 248 249 /// Represents return slots for NRVO candidates in the current scope. 250 /// If a variable is present in this set, it means that a return slot is 251 /// available for this variable in the current scope. 252 llvm::SmallPtrSet<VarDecl *, 8> ReturnSlots; 253 254 void setFlags(Scope *Parent, unsigned F); 255 256 public: 257 Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag) 258 : ErrorTrap(Diag) { 259 Init(Parent, ScopeFlags); 260 } 261 262 /// getFlags - Return the flags for this scope. 263 unsigned getFlags() const { return Flags; } 264 265 void setFlags(unsigned F) { setFlags(getParent(), F); } 266 267 /// isBlockScope - Return true if this scope correspond to a closure. 268 bool isBlockScope() const { return Flags & BlockScope; } 269 270 /// getParent - Return the scope that this is nested in. 271 const Scope *getParent() const { return AnyParent; } 272 Scope *getParent() { return AnyParent; } 273 274 /// getFnParent - Return the closest scope that is a function body. 275 const Scope *getFnParent() const { return FnParent; } 276 Scope *getFnParent() { return FnParent; } 277 278 const Scope *getMSLastManglingParent() const { 279 return MSLastManglingParent; 280 } 281 Scope *getMSLastManglingParent() { return MSLastManglingParent; } 282 283 /// getContinueParent - Return the closest scope that a continue statement 284 /// would be affected by. 285 Scope *getContinueParent() { 286 return ContinueParent; 287 } 288 289 const Scope *getContinueParent() const { 290 return const_cast<Scope*>(this)->getContinueParent(); 291 } 292 293 // Set whether we're in the scope of a condition variable, where 'continue' 294 // is disallowed despite being a continue scope. 295 void setIsConditionVarScope(bool InConditionVarScope) { 296 Flags = (Flags & ~ConditionVarScope) | 297 (InConditionVarScope ? ConditionVarScope : 0); 298 } 299 300 bool isConditionVarScope() const { 301 return Flags & ConditionVarScope; 302 } 303 304 /// getBreakParent - Return the closest scope that a break statement 305 /// would be affected by. 306 Scope *getBreakParent() { 307 return BreakParent; 308 } 309 const Scope *getBreakParent() const { 310 return const_cast<Scope*>(this)->getBreakParent(); 311 } 312 313 Scope *getBlockParent() { return BlockParent; } 314 const Scope *getBlockParent() const { return BlockParent; } 315 316 Scope *getTemplateParamParent() { return TemplateParamParent; } 317 const Scope *getTemplateParamParent() const { return TemplateParamParent; } 318 319 Scope *getDeclParent() { return DeclParent; } 320 const Scope *getDeclParent() const { return DeclParent; } 321 322 /// Returns the depth of this scope. The translation-unit has scope depth 0. 323 unsigned getDepth() const { return Depth; } 324 325 /// Returns the number of function prototype scopes in this scope 326 /// chain. 327 unsigned getFunctionPrototypeDepth() const { 328 return PrototypeDepth; 329 } 330 331 /// Return the number of parameters declared in this function 332 /// prototype, increasing it by one for the next call. 333 unsigned getNextFunctionPrototypeIndex() { 334 assert(isFunctionPrototypeScope()); 335 return PrototypeIndex++; 336 } 337 338 using decl_range = llvm::iterator_range<DeclSetTy::iterator>; 339 340 decl_range decls() const { 341 return decl_range(DeclsInScope.begin(), DeclsInScope.end()); 342 } 343 344 bool decl_empty() const { return DeclsInScope.empty(); } 345 346 void AddDecl(Decl *D) { 347 if (auto *VD = dyn_cast<VarDecl>(D)) 348 if (!isa<ParmVarDecl>(VD)) 349 ReturnSlots.insert(VD); 350 351 DeclsInScope.insert(D); 352 } 353 354 void RemoveDecl(Decl *D) { DeclsInScope.erase(D); } 355 356 void incrementMSManglingNumber() { 357 if (Scope *MSLMP = getMSLastManglingParent()) { 358 MSLMP->MSLastManglingNumber += 1; 359 MSCurManglingNumber += 1; 360 } 361 } 362 363 void decrementMSManglingNumber() { 364 if (Scope *MSLMP = getMSLastManglingParent()) { 365 MSLMP->MSLastManglingNumber -= 1; 366 MSCurManglingNumber -= 1; 367 } 368 } 369 370 unsigned getMSLastManglingNumber() const { 371 if (const Scope *MSLMP = getMSLastManglingParent()) 372 return MSLMP->MSLastManglingNumber; 373 return 1; 374 } 375 376 unsigned getMSCurManglingNumber() const { 377 return MSCurManglingNumber; 378 } 379 380 /// isDeclScope - Return true if this is the scope that the specified decl is 381 /// declared in. 382 bool isDeclScope(const Decl *D) const { return DeclsInScope.contains(D); } 383 384 /// Get the entity corresponding to this scope. 385 DeclContext *getEntity() const { 386 return isTemplateParamScope() ? nullptr : Entity; 387 } 388 389 /// Get the DeclContext in which to continue unqualified lookup after a 390 /// lookup in this scope. 391 DeclContext *getLookupEntity() const { return Entity; } 392 393 void setEntity(DeclContext *E) { 394 assert(!isTemplateParamScope() && 395 "entity associated with template param scope"); 396 Entity = E; 397 } 398 void setLookupEntity(DeclContext *E) { Entity = E; } 399 400 /// Determine whether any unrecoverable errors have occurred within this 401 /// scope. Note that this may return false even if the scope contains invalid 402 /// declarations or statements, if the errors for those invalid constructs 403 /// were suppressed because some prior invalid construct was referenced. 404 bool hasUnrecoverableErrorOccurred() const { 405 return ErrorTrap.hasUnrecoverableErrorOccurred(); 406 } 407 408 /// isFunctionScope() - Return true if this scope is a function scope. 409 bool isFunctionScope() const { return getFlags() & Scope::FnScope; } 410 411 /// isClassScope - Return true if this scope is a class/struct/union scope. 412 bool isClassScope() const { return getFlags() & Scope::ClassScope; } 413 414 /// Determines whether this scope is between inheritance colon and the real 415 /// class/struct definition. 416 bool isClassInheritanceScope() const { 417 return getFlags() & Scope::ClassInheritanceScope; 418 } 419 420 /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline 421 /// method scope or is inside one. 422 bool isInCXXInlineMethodScope() const { 423 if (const Scope *FnS = getFnParent()) { 424 assert(FnS->getParent() && "TUScope not created?"); 425 return FnS->getParent()->isClassScope(); 426 } 427 return false; 428 } 429 430 /// isInObjcMethodScope - Return true if this scope is, or is contained in, an 431 /// Objective-C method body. Note that this method is not constant time. 432 bool isInObjcMethodScope() const { 433 for (const Scope *S = this; S; S = S->getParent()) { 434 // If this scope is an objc method scope, then we succeed. 435 if (S->getFlags() & ObjCMethodScope) 436 return true; 437 } 438 return false; 439 } 440 441 /// isInObjcMethodOuterScope - Return true if this scope is an 442 /// Objective-C method outer most body. 443 bool isInObjcMethodOuterScope() const { 444 if (const Scope *S = this) { 445 // If this scope is an objc method scope, then we succeed. 446 if (S->getFlags() & ObjCMethodScope) 447 return true; 448 } 449 return false; 450 } 451 452 /// isTemplateParamScope - Return true if this scope is a C++ 453 /// template parameter scope. 454 bool isTemplateParamScope() const { 455 return getFlags() & Scope::TemplateParamScope; 456 } 457 458 /// isFunctionPrototypeScope - Return true if this scope is a 459 /// function prototype scope. 460 bool isFunctionPrototypeScope() const { 461 return getFlags() & Scope::FunctionPrototypeScope; 462 } 463 464 /// isFunctionDeclarationScope - Return true if this scope is a 465 /// function prototype scope. 466 bool isFunctionDeclarationScope() const { 467 return getFlags() & Scope::FunctionDeclarationScope; 468 } 469 470 /// isAtCatchScope - Return true if this scope is \@catch. 471 bool isAtCatchScope() const { 472 return getFlags() & Scope::AtCatchScope; 473 } 474 475 /// isCatchScope - Return true if this scope is a C++ catch statement. 476 bool isCatchScope() const { return getFlags() & Scope::CatchScope; } 477 478 /// isSwitchScope - Return true if this scope is a switch scope. 479 bool isSwitchScope() const { 480 for (const Scope *S = this; S; S = S->getParent()) { 481 if (S->getFlags() & Scope::SwitchScope) 482 return true; 483 else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope | 484 Scope::BlockScope | Scope::TemplateParamScope | 485 Scope::FunctionPrototypeScope | 486 Scope::AtCatchScope | Scope::ObjCMethodScope)) 487 return false; 488 } 489 return false; 490 } 491 492 /// Return true if this scope is a loop. 493 bool isLoopScope() const { 494 // 'switch' is the only loop that is not a 'break' scope as well, so we can 495 // just check BreakScope and not SwitchScope. 496 return (getFlags() & Scope::BreakScope) && 497 !(getFlags() & Scope::SwitchScope); 498 } 499 500 /// Determines whether this scope is the OpenMP directive scope 501 bool isOpenMPDirectiveScope() const { 502 return (getFlags() & Scope::OpenMPDirectiveScope); 503 } 504 505 /// Determine whether this scope is some OpenMP loop directive scope 506 /// (for example, 'omp for', 'omp simd'). 507 bool isOpenMPLoopDirectiveScope() const { 508 if (getFlags() & Scope::OpenMPLoopDirectiveScope) { 509 assert(isOpenMPDirectiveScope() && 510 "OpenMP loop directive scope is not a directive scope"); 511 return true; 512 } 513 return false; 514 } 515 516 /// Determine whether this scope is (or is nested into) some OpenMP 517 /// loop simd directive scope (for example, 'omp simd', 'omp for simd'). 518 bool isOpenMPSimdDirectiveScope() const { 519 return getFlags() & Scope::OpenMPSimdDirectiveScope; 520 } 521 522 /// Determine whether this scope is a loop having OpenMP loop 523 /// directive attached. 524 bool isOpenMPLoopScope() const { 525 const Scope *P = getParent(); 526 return P && P->isOpenMPLoopDirectiveScope(); 527 } 528 529 /// Determine whether this scope is some OpenMP directive with 530 /// order clause which specifies concurrent scope. 531 bool isOpenMPOrderClauseScope() const { 532 return getFlags() & Scope::OpenMPOrderClauseScope; 533 } 534 535 /// Determine whether this scope is the statement associated with an OpenACC 536 /// Compute construct directive. 537 bool isOpenACCComputeConstructScope() const { 538 return getFlags() & Scope::OpenACCComputeConstructScope; 539 } 540 541 /// Determine if this scope (or its parents) are a compute construct. If the 542 /// argument is provided, the search will stop at any of the specified scopes. 543 /// Otherwise, it will stop only at the normal 'no longer search' scopes. 544 bool isInOpenACCComputeConstructScope(ScopeFlags Flags = NoScope) const { 545 for (const Scope *S = this; S; S = S->getParent()) { 546 if (S->isOpenACCComputeConstructScope()) 547 return true; 548 549 if (S->getFlags() & Flags) 550 return false; 551 552 else if (S->getFlags() & 553 (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | 554 Scope::TemplateParamScope | Scope::FunctionPrototypeScope | 555 Scope::AtCatchScope | Scope::ObjCMethodScope)) 556 return false; 557 } 558 return false; 559 } 560 561 /// Determine whether this scope is a while/do/for statement, which can have 562 /// continue statements embedded into it. 563 bool isContinueScope() const { 564 return getFlags() & ScopeFlags::ContinueScope; 565 } 566 567 /// Determine whether this scope is a C++ 'try' block. 568 bool isTryScope() const { return getFlags() & Scope::TryScope; } 569 570 /// Determine whether this scope is a function-level C++ try or catch scope. 571 bool isFnTryCatchScope() const { 572 return getFlags() & ScopeFlags::FnTryCatchScope; 573 } 574 575 /// Determine whether this scope is a SEH '__try' block. 576 bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; } 577 578 /// Determine whether this scope is a SEH '__except' block. 579 bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; } 580 581 /// Determine whether this scope is a compound statement scope. 582 bool isCompoundStmtScope() const { 583 return getFlags() & Scope::CompoundStmtScope; 584 } 585 586 /// Determine whether this scope is a controlling scope in a 587 /// if/switch/while/for statement. 588 bool isControlScope() const { return getFlags() & Scope::ControlScope; } 589 590 /// Determine whether this scope is a type alias scope. 591 bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; } 592 593 /// Determine whether this scope is a friend scope. 594 bool isFriendScope() const { return getFlags() & Scope::FriendScope; } 595 596 /// Returns if rhs has a higher scope depth than this. 597 /// 598 /// The caller is responsible for calling this only if one of the two scopes 599 /// is an ancestor of the other. 600 bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; } 601 602 /// containedInPrototypeScope - Return true if this or a parent scope 603 /// is a FunctionPrototypeScope. 604 bool containedInPrototypeScope() const; 605 606 void PushUsingDirective(UsingDirectiveDecl *UDir) { 607 UsingDirectives.push_back(UDir); 608 } 609 610 using using_directives_range = 611 llvm::iterator_range<UsingDirectivesTy::iterator>; 612 613 using_directives_range using_directives() { 614 return using_directives_range(UsingDirectives.begin(), 615 UsingDirectives.end()); 616 } 617 618 void updateNRVOCandidate(VarDecl *VD); 619 620 void applyNRVO(); 621 622 /// Init - This is used by the parser to implement scope caching. 623 void Init(Scope *parent, unsigned flags); 624 625 /// Sets up the specified scope flags and adjusts the scope state 626 /// variables accordingly. 627 void AddFlags(unsigned Flags); 628 629 void dumpImpl(raw_ostream &OS) const; 630 void dump() const; 631 }; 632 633 } // namespace clang 634 635 #endif // LLVM_CLANG_SEMA_SCOPE_H 636