1212795Sdim//===--- Scope.h - Scope interface ------------------------------*- C++ -*-===// 2212795Sdim// 3212795Sdim// The LLVM Compiler Infrastructure 4212795Sdim// 5212795Sdim// This file is distributed under the University of Illinois Open Source 6212795Sdim// License. See LICENSE.TXT for details. 7212795Sdim// 8212795Sdim//===----------------------------------------------------------------------===// 9212795Sdim// 10212795Sdim// This file defines the Scope interface. 11212795Sdim// 12212795Sdim//===----------------------------------------------------------------------===// 13212795Sdim 14212795Sdim#ifndef LLVM_CLANG_SEMA_SCOPE_H 15212795Sdim#define LLVM_CLANG_SEMA_SCOPE_H 16212795Sdim 17218893Sdim#include "clang/Basic/Diagnostic.h" 18212795Sdim#include "llvm/ADT/SmallPtrSet.h" 19221345Sdim#include "llvm/ADT/SmallVector.h" 20212795Sdim 21212795Sdimnamespace clang { 22212795Sdim 23212795Sdimclass Decl; 24212795Sdimclass UsingDirectiveDecl; 25212795Sdim 26212795Sdim/// Scope - A scope is a transient data structure that is used while parsing the 27212795Sdim/// program. It assists with resolving identifiers to the appropriate 28212795Sdim/// declaration. 29212795Sdim/// 30212795Sdimclass Scope { 31212795Sdimpublic: 32212795Sdim /// ScopeFlags - These are bitfields that are or'd together when creating a 33212795Sdim /// scope, which defines the sorts of things the scope contains. 34212795Sdim enum ScopeFlags { 35249423Sdim /// \brief This indicates that the scope corresponds to a function, which 36212795Sdim /// means that labels are set here. 37212795Sdim FnScope = 0x01, 38212795Sdim 39249423Sdim /// \brief This is a while, do, switch, for, etc that can have break 40249423Sdim /// statements embedded into it. 41212795Sdim BreakScope = 0x02, 42212795Sdim 43249423Sdim /// \brief This is a while, do, for, which can have continue statements 44249423Sdim /// embedded into it. 45212795Sdim ContinueScope = 0x04, 46212795Sdim 47249423Sdim /// \brief This is a scope that can contain a declaration. Some scopes 48212795Sdim /// just contain loop constructs but don't contain decls. 49212795Sdim DeclScope = 0x08, 50212795Sdim 51249423Sdim /// \brief The controlling scope in a if/switch/while/for statement. 52212795Sdim ControlScope = 0x10, 53212795Sdim 54249423Sdim /// \brief The scope of a struct/union/class definition. 55212795Sdim ClassScope = 0x20, 56212795Sdim 57249423Sdim /// \brief This is a scope that corresponds to a block/closure object. 58212795Sdim /// Blocks serve as top-level scopes for some objects like labels, they 59212795Sdim /// also prevent things like break and continue. BlockScopes always have 60234353Sdim /// the FnScope and DeclScope flags set as well. 61212795Sdim BlockScope = 0x40, 62212795Sdim 63249423Sdim /// \brief This is a scope that corresponds to the 64212795Sdim /// template parameters of a C++ template. Template parameter 65212795Sdim /// scope starts at the 'template' keyword and ends when the 66212795Sdim /// template declaration ends. 67212795Sdim TemplateParamScope = 0x80, 68212795Sdim 69249423Sdim /// \brief This is a scope that corresponds to the 70212795Sdim /// parameters within a function prototype. 71212795Sdim FunctionPrototypeScope = 0x100, 72212795Sdim 73249423Sdim /// \brief This is a scope that corresponds to the parameters within 74249423Sdim /// a function prototype for a function declaration (as opposed to any 75249423Sdim /// other kind of function declarator). Always has FunctionPrototypeScope 76249423Sdim /// set as well. 77249423Sdim FunctionDeclarationScope = 0x200, 78249423Sdim 79249423Sdim /// \brief This is a scope that corresponds to the Objective-C 80239462Sdim /// \@catch statement. 81249423Sdim AtCatchScope = 0x400, 82212795Sdim 83249423Sdim /// \brief This scope corresponds to an Objective-C method body. 84212795Sdim /// It always has FnScope and DeclScope set as well. 85249423Sdim ObjCMethodScope = 0x800, 86221345Sdim 87249423Sdim /// \brief This is a scope that corresponds to a switch statement. 88249423Sdim SwitchScope = 0x1000, 89223017Sdim 90249423Sdim /// \brief This is the scope of a C++ try statement. 91249423Sdim TryScope = 0x2000, 92243830Sdim 93249423Sdim /// \brief This is the scope for a function-level C++ try or catch scope. 94263508Sdim FnTryCatchScope = 0x4000, 95263508Sdim 96263508Sdim /// \brief This is the scope of OpenMP executable directive 97263508Sdim OpenMPDirectiveScope = 0x8000 98212795Sdim }; 99212795Sdimprivate: 100212795Sdim /// The parent scope for this scope. This is null for the translation-unit 101212795Sdim /// scope. 102212795Sdim Scope *AnyParent; 103212795Sdim 104212795Sdim /// Depth - This is the depth of this scope. The translation-unit scope has 105212795Sdim /// depth 0. 106212795Sdim unsigned short Depth; 107212795Sdim 108212795Sdim /// Flags - This contains a set of ScopeFlags, which indicates how the scope 109212795Sdim /// interrelates with other control flow statements. 110212795Sdim unsigned short Flags; 111212795Sdim 112221345Sdim /// PrototypeDepth - This is the number of function prototype scopes 113221345Sdim /// enclosing this scope, including this scope. 114221345Sdim unsigned short PrototypeDepth; 115221345Sdim 116221345Sdim /// PrototypeIndex - This is the number of parameters currently 117221345Sdim /// declared in this scope. 118221345Sdim unsigned short PrototypeIndex; 119221345Sdim 120212795Sdim /// FnParent - If this scope has a parent scope that is a function body, this 121212795Sdim /// pointer is non-null and points to it. This is used for label processing. 122212795Sdim Scope *FnParent; 123212795Sdim 124234353Sdim /// BreakParent/ContinueParent - This is a direct link to the innermost 125234353Sdim /// BreakScope/ContinueScope which contains the contents of this scope 126234353Sdim /// for control flow purposes (and might be this scope itself), or null 127234353Sdim /// if there is no such scope. 128212795Sdim Scope *BreakParent, *ContinueParent; 129212795Sdim 130212795Sdim /// BlockParent - This is a direct link to the immediately containing 131212795Sdim /// BlockScope if this scope is not one, or null if there is none. 132212795Sdim Scope *BlockParent; 133212795Sdim 134212795Sdim /// TemplateParamParent - This is a direct link to the 135212795Sdim /// immediately containing template parameter scope. In the 136212795Sdim /// case of nested templates, template parameter scopes can have 137212795Sdim /// other template parameter scopes as parents. 138212795Sdim Scope *TemplateParamParent; 139212795Sdim 140212795Sdim /// DeclsInScope - This keeps track of all declarations in this scope. When 141212795Sdim /// the declaration is added to the scope, it is set as the current 142212795Sdim /// declaration for the identifier in the IdentifierTable. When the scope is 143212795Sdim /// popped, these declarations are removed from the IdentifierTable's notion 144212795Sdim /// of current declaration. It is up to the current Action implementation to 145212795Sdim /// implement these semantics. 146212795Sdim typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy; 147212795Sdim DeclSetTy DeclsInScope; 148212795Sdim 149263508Sdim /// The DeclContext with which this scope is associated. For 150212795Sdim /// example, the entity of a class scope is the class itself, the 151263508Sdim /// entity of a function scope is a function, etc. 152263508Sdim DeclContext *Entity; 153212795Sdim 154226633Sdim typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy; 155212795Sdim UsingDirectivesTy UsingDirectives; 156212795Sdim 157218893Sdim /// \brief Used to determine if errors occurred in this scope. 158218893Sdim DiagnosticErrorTrap ErrorTrap; 159212795Sdim 160212795Sdimpublic: 161226633Sdim Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag) 162218893Sdim : ErrorTrap(Diag) { 163212795Sdim Init(Parent, ScopeFlags); 164212795Sdim } 165212795Sdim 166212795Sdim /// getFlags - Return the flags for this scope. 167212795Sdim /// 168212795Sdim unsigned getFlags() const { return Flags; } 169212795Sdim void setFlags(unsigned F) { Flags = F; } 170212795Sdim 171218893Sdim /// isBlockScope - Return true if this scope correspond to a closure. 172212795Sdim bool isBlockScope() const { return Flags & BlockScope; } 173212795Sdim 174212795Sdim /// getParent - Return the scope that this is nested in. 175212795Sdim /// 176212795Sdim const Scope *getParent() const { return AnyParent; } 177212795Sdim Scope *getParent() { return AnyParent; } 178212795Sdim 179212795Sdim /// getFnParent - Return the closest scope that is a function body. 180212795Sdim /// 181212795Sdim const Scope *getFnParent() const { return FnParent; } 182212795Sdim Scope *getFnParent() { return FnParent; } 183212795Sdim 184212795Sdim /// getContinueParent - Return the closest scope that a continue statement 185234353Sdim /// would be affected by. 186212795Sdim Scope *getContinueParent() { 187234353Sdim return ContinueParent; 188212795Sdim } 189212795Sdim 190212795Sdim const Scope *getContinueParent() const { 191212795Sdim return const_cast<Scope*>(this)->getContinueParent(); 192212795Sdim } 193212795Sdim 194212795Sdim /// getBreakParent - Return the closest scope that a break statement 195234353Sdim /// would be affected by. 196212795Sdim Scope *getBreakParent() { 197234353Sdim return BreakParent; 198212795Sdim } 199212795Sdim const Scope *getBreakParent() const { 200212795Sdim return const_cast<Scope*>(this)->getBreakParent(); 201212795Sdim } 202212795Sdim 203212795Sdim Scope *getBlockParent() { return BlockParent; } 204212795Sdim const Scope *getBlockParent() const { return BlockParent; } 205212795Sdim 206212795Sdim Scope *getTemplateParamParent() { return TemplateParamParent; } 207212795Sdim const Scope *getTemplateParamParent() const { return TemplateParamParent; } 208212795Sdim 209221345Sdim /// Returns the number of function prototype scopes in this scope 210221345Sdim /// chain. 211221345Sdim unsigned getFunctionPrototypeDepth() const { 212221345Sdim return PrototypeDepth; 213221345Sdim } 214221345Sdim 215221345Sdim /// Return the number of parameters declared in this function 216221345Sdim /// prototype, increasing it by one for the next call. 217221345Sdim unsigned getNextFunctionPrototypeIndex() { 218221345Sdim assert(isFunctionPrototypeScope()); 219221345Sdim return PrototypeIndex++; 220221345Sdim } 221221345Sdim 222212795Sdim typedef DeclSetTy::iterator decl_iterator; 223212795Sdim decl_iterator decl_begin() const { return DeclsInScope.begin(); } 224212795Sdim decl_iterator decl_end() const { return DeclsInScope.end(); } 225212795Sdim bool decl_empty() const { return DeclsInScope.empty(); } 226212795Sdim 227212795Sdim void AddDecl(Decl *D) { 228212795Sdim DeclsInScope.insert(D); 229212795Sdim } 230212795Sdim 231212795Sdim void RemoveDecl(Decl *D) { 232212795Sdim DeclsInScope.erase(D); 233212795Sdim } 234212795Sdim 235212795Sdim /// isDeclScope - Return true if this is the scope that the specified decl is 236212795Sdim /// declared in. 237212795Sdim bool isDeclScope(Decl *D) { 238212795Sdim return DeclsInScope.count(D) != 0; 239212795Sdim } 240212795Sdim 241263508Sdim DeclContext *getEntity() const { return Entity; } 242263508Sdim void setEntity(DeclContext *E) { Entity = E; } 243212795Sdim 244218893Sdim bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); } 245249423Sdim 246249423Sdim bool hasUnrecoverableErrorOccurred() const { 247249423Sdim return ErrorTrap.hasUnrecoverableErrorOccurred(); 248249423Sdim } 249249423Sdim 250212795Sdim /// isClassScope - Return true if this scope is a class/struct/union scope. 251212795Sdim bool isClassScope() const { 252212795Sdim return (getFlags() & Scope::ClassScope); 253212795Sdim } 254212795Sdim 255212795Sdim /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline 256212795Sdim /// method scope or is inside one. 257212795Sdim bool isInCXXInlineMethodScope() const { 258212795Sdim if (const Scope *FnS = getFnParent()) { 259212795Sdim assert(FnS->getParent() && "TUScope not created?"); 260212795Sdim return FnS->getParent()->isClassScope(); 261212795Sdim } 262212795Sdim return false; 263212795Sdim } 264212795Sdim 265212795Sdim /// isInObjcMethodScope - Return true if this scope is, or is contained in, an 266212795Sdim /// Objective-C method body. Note that this method is not constant time. 267212795Sdim bool isInObjcMethodScope() const { 268212795Sdim for (const Scope *S = this; S; S = S->getParent()) { 269212795Sdim // If this scope is an objc method scope, then we succeed. 270212795Sdim if (S->getFlags() & ObjCMethodScope) 271212795Sdim return true; 272212795Sdim } 273212795Sdim return false; 274212795Sdim } 275212795Sdim 276212795Sdim /// isTemplateParamScope - Return true if this scope is a C++ 277212795Sdim /// template parameter scope. 278212795Sdim bool isTemplateParamScope() const { 279212795Sdim return getFlags() & Scope::TemplateParamScope; 280212795Sdim } 281212795Sdim 282212795Sdim /// isFunctionPrototypeScope - Return true if this scope is a 283212795Sdim /// function prototype scope. 284212795Sdim bool isFunctionPrototypeScope() const { 285212795Sdim return getFlags() & Scope::FunctionPrototypeScope; 286212795Sdim } 287212795Sdim 288239462Sdim /// isAtCatchScope - Return true if this scope is \@catch. 289212795Sdim bool isAtCatchScope() const { 290212795Sdim return getFlags() & Scope::AtCatchScope; 291212795Sdim } 292212795Sdim 293221345Sdim /// isSwitchScope - Return true if this scope is a switch scope. 294221345Sdim bool isSwitchScope() const { 295221345Sdim for (const Scope *S = this; S; S = S->getParent()) { 296221345Sdim if (S->getFlags() & Scope::SwitchScope) 297221345Sdim return true; 298221345Sdim else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope | 299221345Sdim Scope::BlockScope | Scope::TemplateParamScope | 300221345Sdim Scope::FunctionPrototypeScope | 301221345Sdim Scope::AtCatchScope | Scope::ObjCMethodScope)) 302221345Sdim return false; 303221345Sdim } 304221345Sdim return false; 305221345Sdim } 306263508Sdim 307263508Sdim /// \brief Determines whether this scope is the OpenMP directive scope 308263508Sdim bool isOpenMPDirectiveScope() const { 309263508Sdim return (getFlags() & Scope::OpenMPDirectiveScope); 310263508Sdim } 311263508Sdim 312224145Sdim /// \brief Determine whether this scope is a C++ 'try' block. 313224145Sdim bool isTryScope() const { return getFlags() & Scope::TryScope; } 314221345Sdim 315234353Sdim /// containedInPrototypeScope - Return true if this or a parent scope 316234353Sdim /// is a FunctionPrototypeScope. 317234353Sdim bool containedInPrototypeScope() const; 318234353Sdim 319212795Sdim typedef UsingDirectivesTy::iterator udir_iterator; 320212795Sdim typedef UsingDirectivesTy::const_iterator const_udir_iterator; 321212795Sdim 322212795Sdim void PushUsingDirective(UsingDirectiveDecl *UDir) { 323212795Sdim UsingDirectives.push_back(UDir); 324212795Sdim } 325212795Sdim 326212795Sdim udir_iterator using_directives_begin() { 327212795Sdim return UsingDirectives.begin(); 328212795Sdim } 329212795Sdim 330212795Sdim udir_iterator using_directives_end() { 331212795Sdim return UsingDirectives.end(); 332212795Sdim } 333212795Sdim 334212795Sdim const_udir_iterator using_directives_begin() const { 335212795Sdim return UsingDirectives.begin(); 336212795Sdim } 337212795Sdim 338212795Sdim const_udir_iterator using_directives_end() const { 339212795Sdim return UsingDirectives.end(); 340212795Sdim } 341212795Sdim 342212795Sdim /// Init - This is used by the parser to implement scope caching. 343212795Sdim /// 344221345Sdim void Init(Scope *parent, unsigned flags); 345212795Sdim}; 346212795Sdim 347212795Sdim} // end namespace clang 348212795Sdim 349212795Sdim#endif 350