Home | History | Annotate | Download | only in Sema
      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     /// \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 private:
     97   /// The parent scope for this scope.  This is null for the translation-unit
     98   /// scope.
     99   Scope *AnyParent;
    100 
    101   /// Depth - This is the depth of this scope.  The translation-unit scope has
    102   /// depth 0.
    103   unsigned short Depth;
    104 
    105   /// Flags - This contains a set of ScopeFlags, which indicates how the scope
    106   /// interrelates with other control flow statements.
    107   unsigned short Flags;
    108 
    109   /// PrototypeDepth - This is the number of function prototype scopes
    110   /// enclosing this scope, including this scope.
    111   unsigned short PrototypeDepth;
    112 
    113   /// PrototypeIndex - This is the number of parameters currently
    114   /// declared in this scope.
    115   unsigned short PrototypeIndex;
    116 
    117   /// FnParent - If this scope has a parent scope that is a function body, this
    118   /// pointer is non-null and points to it.  This is used for label processing.
    119   Scope *FnParent;
    120 
    121   /// BreakParent/ContinueParent - This is a direct link to the innermost
    122   /// BreakScope/ContinueScope which contains the contents of this scope
    123   /// for control flow purposes (and might be this scope itself), or null
    124   /// if there is no such scope.
    125   Scope *BreakParent, *ContinueParent;
    126 
    127   /// BlockParent - This is a direct link to the immediately containing
    128   /// BlockScope if this scope is not one, or null if there is none.
    129   Scope *BlockParent;
    130 
    131   /// TemplateParamParent - This is a direct link to the
    132   /// immediately containing template parameter scope. In the
    133   /// case of nested templates, template parameter scopes can have
    134   /// other template parameter scopes as parents.
    135   Scope *TemplateParamParent;
    136 
    137   /// DeclsInScope - This keeps track of all declarations in this scope.  When
    138   /// the declaration is added to the scope, it is set as the current
    139   /// declaration for the identifier in the IdentifierTable.  When the scope is
    140   /// popped, these declarations are removed from the IdentifierTable's notion
    141   /// of current declaration.  It is up to the current Action implementation to
    142   /// implement these semantics.
    143   typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy;
    144   DeclSetTy DeclsInScope;
    145 
    146   /// Entity - The entity with which this scope is associated. For
    147   /// example, the entity of a class scope is the class itself, the
    148   /// entity of a function scope is a function, etc. This field is
    149   /// maintained by the Action implementation.
    150   void *Entity;
    151 
    152   typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
    153   UsingDirectivesTy UsingDirectives;
    154 
    155   /// \brief Used to determine if errors occurred in this scope.
    156   DiagnosticErrorTrap ErrorTrap;
    157 
    158 public:
    159   Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
    160     : ErrorTrap(Diag) {
    161     Init(Parent, ScopeFlags);
    162   }
    163 
    164   /// getFlags - Return the flags for this scope.
    165   ///
    166   unsigned getFlags() const { return Flags; }
    167   void setFlags(unsigned F) { Flags = F; }
    168 
    169   /// isBlockScope - Return true if this scope correspond to a closure.
    170   bool isBlockScope() const { return Flags & BlockScope; }
    171 
    172   /// getParent - Return the scope that this is nested in.
    173   ///
    174   const Scope *getParent() const { return AnyParent; }
    175   Scope *getParent() { return AnyParent; }
    176 
    177   /// getFnParent - Return the closest scope that is a function body.
    178   ///
    179   const Scope *getFnParent() const { return FnParent; }
    180   Scope *getFnParent() { return FnParent; }
    181 
    182   /// getContinueParent - Return the closest scope that a continue statement
    183   /// would be affected by.
    184   Scope *getContinueParent() {
    185     return ContinueParent;
    186   }
    187 
    188   const Scope *getContinueParent() const {
    189     return const_cast<Scope*>(this)->getContinueParent();
    190   }
    191 
    192   /// getBreakParent - Return the closest scope that a break statement
    193   /// would be affected by.
    194   Scope *getBreakParent() {
    195     return BreakParent;
    196   }
    197   const Scope *getBreakParent() const {
    198     return const_cast<Scope*>(this)->getBreakParent();
    199   }
    200 
    201   Scope *getBlockParent() { return BlockParent; }
    202   const Scope *getBlockParent() const { return BlockParent; }
    203 
    204   Scope *getTemplateParamParent() { return TemplateParamParent; }
    205   const Scope *getTemplateParamParent() const { return TemplateParamParent; }
    206 
    207   /// Returns the number of function prototype scopes in this scope
    208   /// chain.
    209   unsigned getFunctionPrototypeDepth() const {
    210     return PrototypeDepth;
    211   }
    212 
    213   /// Return the number of parameters declared in this function
    214   /// prototype, increasing it by one for the next call.
    215   unsigned getNextFunctionPrototypeIndex() {
    216     assert(isFunctionPrototypeScope());
    217     return PrototypeIndex++;
    218   }
    219 
    220   typedef DeclSetTy::iterator decl_iterator;
    221   decl_iterator decl_begin() const { return DeclsInScope.begin(); }
    222   decl_iterator decl_end()   const { return DeclsInScope.end(); }
    223   bool decl_empty()          const { return DeclsInScope.empty(); }
    224 
    225   void AddDecl(Decl *D) {
    226     DeclsInScope.insert(D);
    227   }
    228 
    229   void RemoveDecl(Decl *D) {
    230     DeclsInScope.erase(D);
    231   }
    232 
    233   /// isDeclScope - Return true if this is the scope that the specified decl is
    234   /// declared in.
    235   bool isDeclScope(Decl *D) {
    236     return DeclsInScope.count(D) != 0;
    237   }
    238 
    239   void* getEntity() const { return Entity; }
    240   void setEntity(void *E) { Entity = E; }
    241 
    242   bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
    243 
    244   /// isClassScope - Return true if this scope is a class/struct/union scope.
    245   bool isClassScope() const {
    246     return (getFlags() & Scope::ClassScope);
    247   }
    248 
    249   /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
    250   /// method scope or is inside one.
    251   bool isInCXXInlineMethodScope() const {
    252     if (const Scope *FnS = getFnParent()) {
    253       assert(FnS->getParent() && "TUScope not created?");
    254       return FnS->getParent()->isClassScope();
    255     }
    256     return false;
    257   }
    258 
    259   /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
    260   /// Objective-C method body.  Note that this method is not constant time.
    261   bool isInObjcMethodScope() const {
    262     for (const Scope *S = this; S; S = S->getParent()) {
    263       // If this scope is an objc method scope, then we succeed.
    264       if (S->getFlags() & ObjCMethodScope)
    265         return true;
    266     }
    267     return false;
    268   }
    269 
    270   /// isTemplateParamScope - Return true if this scope is a C++
    271   /// template parameter scope.
    272   bool isTemplateParamScope() const {
    273     return getFlags() & Scope::TemplateParamScope;
    274   }
    275 
    276   /// isFunctionPrototypeScope - Return true if this scope is a
    277   /// function prototype scope.
    278   bool isFunctionPrototypeScope() const {
    279     return getFlags() & Scope::FunctionPrototypeScope;
    280   }
    281 
    282   /// isAtCatchScope - Return true if this scope is \@catch.
    283   bool isAtCatchScope() const {
    284     return getFlags() & Scope::AtCatchScope;
    285   }
    286 
    287   /// isSwitchScope - Return true if this scope is a switch scope.
    288   bool isSwitchScope() const {
    289     for (const Scope *S = this; S; S = S->getParent()) {
    290       if (S->getFlags() & Scope::SwitchScope)
    291         return true;
    292       else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
    293                                 Scope::BlockScope | Scope::TemplateParamScope |
    294                                 Scope::FunctionPrototypeScope |
    295                                 Scope::AtCatchScope | Scope::ObjCMethodScope))
    296         return false;
    297     }
    298     return false;
    299   }
    300 
    301   /// \brief Determine whether this scope is a C++ 'try' block.
    302   bool isTryScope() const { return getFlags() & Scope::TryScope; }
    303 
    304   /// containedInPrototypeScope - Return true if this or a parent scope
    305   /// is a FunctionPrototypeScope.
    306   bool containedInPrototypeScope() const;
    307 
    308   typedef UsingDirectivesTy::iterator udir_iterator;
    309   typedef UsingDirectivesTy::const_iterator const_udir_iterator;
    310 
    311   void PushUsingDirective(UsingDirectiveDecl *UDir) {
    312     UsingDirectives.push_back(UDir);
    313   }
    314 
    315   udir_iterator using_directives_begin() {
    316     return UsingDirectives.begin();
    317   }
    318 
    319   udir_iterator using_directives_end() {
    320     return UsingDirectives.end();
    321   }
    322 
    323   const_udir_iterator using_directives_begin() const {
    324     return UsingDirectives.begin();
    325   }
    326 
    327   const_udir_iterator using_directives_end() const {
    328     return UsingDirectives.end();
    329   }
    330 
    331   /// Init - This is used by the parser to implement scope caching.
    332   ///
    333   void Init(Scope *parent, unsigned flags);
    334 };
    335 
    336 }  // end namespace clang
    337 
    338 #endif
    339