2007-07-11 21:01:13 +04:00
|
|
|
//===--- Scope.h - Scope interface ------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the Scope interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-20 22:27:03 +04:00
|
|
|
#ifndef LLVM_CLANG_SEMA_SCOPE_H
|
|
|
|
#define LLVM_CLANG_SEMA_SCOPE_H
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2010-11-19 03:19:12 +03:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2011-05-02 02:35:37 +04:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
class Decl;
|
|
|
|
class UsingDirectiveDecl;
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// Scope - A scope is a transient data structure that is used while parsing the
|
|
|
|
/// program. It assists with resolving identifiers to the appropriate
|
|
|
|
/// declaration.
|
|
|
|
///
|
|
|
|
class Scope {
|
|
|
|
public:
|
|
|
|
/// ScopeFlags - These are bitfields that are or'd together when creating a
|
|
|
|
/// scope, which defines the sorts of things the scope contains.
|
|
|
|
enum ScopeFlags {
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This indicates that the scope corresponds to a function, which
|
2007-07-11 21:01:13 +04:00
|
|
|
/// means that labels are set here.
|
|
|
|
FnScope = 0x01,
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is a while, do, switch, for, etc that can have break
|
|
|
|
/// statements embedded into it.
|
2007-07-11 21:01:13 +04:00
|
|
|
BreakScope = 0x02,
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is a while, do, for, which can have continue statements
|
|
|
|
/// embedded into it.
|
2007-08-26 10:24:45 +04:00
|
|
|
ContinueScope = 0x04,
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is a scope that can contain a declaration. Some scopes
|
2007-08-26 10:24:45 +04:00
|
|
|
/// just contain loop constructs but don't contain decls.
|
2008-06-25 02:12:16 +04:00
|
|
|
DeclScope = 0x08,
|
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief The controlling scope in a if/switch/while/for statement.
|
2008-09-10 00:38:47 +04:00
|
|
|
ControlScope = 0x10,
|
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief The scope of a struct/union/class definition.
|
2009-01-10 01:42:13 +03:00
|
|
|
ClassScope = 0x20,
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is a scope that corresponds to a block/closure object.
|
2008-08-28 23:20:44 +04:00
|
|
|
/// Blocks serve as top-level scopes for some objects like labels, they
|
2010-04-12 09:38:43 +04:00
|
|
|
/// also prevent things like break and continue. BlockScopes always have
|
2012-02-17 05:35:32 +04:00
|
|
|
/// the FnScope and DeclScope flags set as well.
|
2008-12-02 03:41:28 +03:00
|
|
|
BlockScope = 0x40,
|
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is a scope that corresponds to the
|
2008-12-02 03:41:28 +03:00
|
|
|
/// template parameters of a C++ template. Template parameter
|
|
|
|
/// scope starts at the 'template' keyword and ends when the
|
|
|
|
/// template declaration ends.
|
2009-01-10 01:42:13 +03:00
|
|
|
TemplateParamScope = 0x80,
|
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is a scope that corresponds to the
|
2009-01-10 01:42:13 +03:00
|
|
|
/// parameters within a function prototype.
|
2009-02-11 23:05:44 +03:00
|
|
|
FunctionPrototypeScope = 0x100,
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is a scope that corresponds to the parameters within
|
|
|
|
/// a function prototype for a function declaration (as opposed to any
|
|
|
|
/// other kind of function declarator). Always has FunctionPrototypeScope
|
|
|
|
/// set as well.
|
|
|
|
FunctionDeclarationScope = 0x200,
|
|
|
|
|
|
|
|
/// \brief This is a scope that corresponds to the Objective-C
|
2012-06-14 02:07:09 +04:00
|
|
|
/// \@catch statement.
|
2013-01-29 02:42:45 +04:00
|
|
|
AtCatchScope = 0x400,
|
2010-04-12 09:38:43 +04:00
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This scope corresponds to an Objective-C method body.
|
2010-04-12 09:38:43 +04:00
|
|
|
/// It always has FnScope and DeclScope set as well.
|
2013-01-29 02:42:45 +04:00
|
|
|
ObjCMethodScope = 0x800,
|
2011-04-22 01:44:26 +04:00
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is a scope that corresponds to a switch statement.
|
|
|
|
SwitchScope = 0x1000,
|
2011-06-11 21:19:42 +04:00
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is the scope of a C++ try statement.
|
|
|
|
TryScope = 0x2000,
|
2012-11-10 05:04:23 +04:00
|
|
|
|
2013-01-29 02:42:45 +04:00
|
|
|
/// \brief This is the scope for a function-level C++ try or catch scope.
|
|
|
|
FnTryCatchScope = 0x4000
|
2007-07-11 21:01:13 +04:00
|
|
|
};
|
|
|
|
private:
|
|
|
|
/// The parent scope for this scope. This is null for the translation-unit
|
|
|
|
/// scope.
|
|
|
|
Scope *AnyParent;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// Depth - This is the depth of this scope. The translation-unit scope has
|
|
|
|
/// depth 0.
|
2010-04-12 10:12:50 +04:00
|
|
|
unsigned short Depth;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// Flags - This contains a set of ScopeFlags, which indicates how the scope
|
|
|
|
/// interrelates with other control flow statements.
|
2010-04-12 10:12:50 +04:00
|
|
|
unsigned short Flags;
|
2008-12-11 02:01:14 +03:00
|
|
|
|
2011-05-02 02:35:37 +04:00
|
|
|
/// PrototypeDepth - This is the number of function prototype scopes
|
|
|
|
/// enclosing this scope, including this scope.
|
|
|
|
unsigned short PrototypeDepth;
|
|
|
|
|
|
|
|
/// PrototypeIndex - This is the number of parameters currently
|
|
|
|
/// declared in this scope.
|
|
|
|
unsigned short PrototypeIndex;
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// FnParent - If this scope has a parent scope that is a function body, this
|
|
|
|
/// pointer is non-null and points to it. This is used for label processing.
|
|
|
|
Scope *FnParent;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2012-02-17 05:35:32 +04:00
|
|
|
/// BreakParent/ContinueParent - This is a direct link to the innermost
|
|
|
|
/// BreakScope/ContinueScope which contains the contents of this scope
|
|
|
|
/// for control flow purposes (and might be this scope itself), or null
|
|
|
|
/// if there is no such scope.
|
2007-07-11 21:01:13 +04:00
|
|
|
Scope *BreakParent, *ContinueParent;
|
2008-09-10 23:17:48 +04:00
|
|
|
|
|
|
|
/// BlockParent - This is a direct link to the immediately containing
|
|
|
|
/// BlockScope if this scope is not one, or null if there is none.
|
|
|
|
Scope *BlockParent;
|
2008-12-05 21:15:24 +03:00
|
|
|
|
|
|
|
/// TemplateParamParent - This is a direct link to the
|
|
|
|
/// immediately containing template parameter scope. In the
|
|
|
|
/// case of nested templates, template parameter scopes can have
|
|
|
|
/// other template parameter scopes as parents.
|
|
|
|
Scope *TemplateParamParent;
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// DeclsInScope - This keeps track of all declarations in this scope. When
|
|
|
|
/// the declaration is added to the scope, it is set as the current
|
|
|
|
/// declaration for the identifier in the IdentifierTable. When the scope is
|
|
|
|
/// popped, these declarations are removed from the IdentifierTable's notion
|
|
|
|
/// of current declaration. It is up to the current Action implementation to
|
|
|
|
/// implement these semantics.
|
2010-08-21 13:40:31 +04:00
|
|
|
typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy;
|
2007-07-11 21:01:13 +04:00
|
|
|
DeclSetTy DeclsInScope;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-12-11 19:49:14 +03:00
|
|
|
/// Entity - The entity with which this scope is associated. For
|
|
|
|
/// example, the entity of a class scope is the class itself, the
|
|
|
|
/// entity of a function scope is a function, etc. This field is
|
|
|
|
/// maintained by the Action implementation.
|
|
|
|
void *Entity;
|
|
|
|
|
2011-07-20 10:58:45 +04:00
|
|
|
typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
|
2009-02-03 22:21:40 +03:00
|
|
|
UsingDirectivesTy UsingDirectives;
|
|
|
|
|
2010-11-19 03:19:12 +03:00
|
|
|
/// \brief Used to determine if errors occurred in this scope.
|
|
|
|
DiagnosticErrorTrap ErrorTrap;
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97518 91177308-0d34-0410-b5e6-96231b3b80d8
2010-03-02 02:15:13 +03:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
public:
|
2011-09-26 03:23:43 +04:00
|
|
|
Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
|
2010-11-19 03:19:12 +03:00
|
|
|
: ErrorTrap(Diag) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Init(Parent, ScopeFlags);
|
|
|
|
}
|
2007-08-26 10:24:45 +04:00
|
|
|
|
|
|
|
/// getFlags - Return the flags for this scope.
|
|
|
|
///
|
|
|
|
unsigned getFlags() const { return Flags; }
|
2010-04-12 10:12:50 +04:00
|
|
|
void setFlags(unsigned F) { Flags = F; }
|
2007-08-26 10:24:45 +04:00
|
|
|
|
2011-02-18 04:27:55 +03:00
|
|
|
/// isBlockScope - Return true if this scope correspond to a closure.
|
2008-09-10 23:17:48 +04:00
|
|
|
bool isBlockScope() const { return Flags & BlockScope; }
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// getParent - Return the scope that this is nested in.
|
|
|
|
///
|
2007-08-26 10:24:45 +04:00
|
|
|
const Scope *getParent() const { return AnyParent; }
|
|
|
|
Scope *getParent() { return AnyParent; }
|
|
|
|
|
|
|
|
/// getFnParent - Return the closest scope that is a function body.
|
|
|
|
///
|
|
|
|
const Scope *getFnParent() const { return FnParent; }
|
|
|
|
Scope *getFnParent() { return FnParent; }
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// getContinueParent - Return the closest scope that a continue statement
|
2012-02-17 05:35:32 +04:00
|
|
|
/// would be affected by.
|
2008-09-10 23:17:48 +04:00
|
|
|
Scope *getContinueParent() {
|
2012-02-17 05:35:32 +04:00
|
|
|
return ContinueParent;
|
2008-09-10 23:17:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const Scope *getContinueParent() const {
|
|
|
|
return const_cast<Scope*>(this)->getContinueParent();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// getBreakParent - Return the closest scope that a break statement
|
2012-02-17 05:35:32 +04:00
|
|
|
/// would be affected by.
|
2008-09-10 23:17:48 +04:00
|
|
|
Scope *getBreakParent() {
|
2012-02-17 05:35:32 +04:00
|
|
|
return BreakParent;
|
2008-09-10 23:17:48 +04:00
|
|
|
}
|
|
|
|
const Scope *getBreakParent() const {
|
|
|
|
return const_cast<Scope*>(this)->getBreakParent();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-09-10 23:17:48 +04:00
|
|
|
Scope *getBlockParent() { return BlockParent; }
|
2009-09-09 19:08:12 +04:00
|
|
|
const Scope *getBlockParent() const { return BlockParent; }
|
2008-12-05 21:15:24 +03:00
|
|
|
|
|
|
|
Scope *getTemplateParamParent() { return TemplateParamParent; }
|
2009-09-09 19:08:12 +04:00
|
|
|
const Scope *getTemplateParamParent() const { return TemplateParamParent; }
|
|
|
|
|
2011-05-02 02:35:37 +04:00
|
|
|
/// Returns the number of function prototype scopes in this scope
|
|
|
|
/// chain.
|
|
|
|
unsigned getFunctionPrototypeDepth() const {
|
|
|
|
return PrototypeDepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the number of parameters declared in this function
|
|
|
|
/// prototype, increasing it by one for the next call.
|
|
|
|
unsigned getNextFunctionPrototypeIndex() {
|
|
|
|
assert(isFunctionPrototypeScope());
|
|
|
|
return PrototypeIndex++;
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
typedef DeclSetTy::iterator decl_iterator;
|
|
|
|
decl_iterator decl_begin() const { return DeclsInScope.begin(); }
|
|
|
|
decl_iterator decl_end() const { return DeclsInScope.end(); }
|
2007-08-26 10:24:45 +04:00
|
|
|
bool decl_empty() const { return DeclsInScope.empty(); }
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
void AddDecl(Decl *D) {
|
2007-07-11 21:01:13 +04:00
|
|
|
DeclsInScope.insert(D);
|
|
|
|
}
|
2008-04-21 06:02:58 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
void RemoveDecl(Decl *D) {
|
2008-12-11 19:49:14 +03:00
|
|
|
DeclsInScope.erase(D);
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// isDeclScope - Return true if this is the scope that the specified decl is
|
|
|
|
/// declared in.
|
2010-08-21 13:40:31 +04:00
|
|
|
bool isDeclScope(Decl *D) {
|
2007-07-11 21:01:13 +04:00
|
|
|
return DeclsInScope.count(D) != 0;
|
|
|
|
}
|
2008-06-25 02:12:16 +04:00
|
|
|
|
2008-12-11 19:49:14 +03:00
|
|
|
void* getEntity() const { return Entity; }
|
|
|
|
void setEntity(void *E) { Entity = E; }
|
|
|
|
|
2010-11-19 03:19:12 +03:00
|
|
|
bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
|
2013-03-29 01:46:45 +04:00
|
|
|
|
|
|
|
bool hasUnrecoverableErrorOccurred() const {
|
|
|
|
return ErrorTrap.hasUnrecoverableErrorOccurred();
|
|
|
|
}
|
|
|
|
|
2009-01-10 01:42:13 +03:00
|
|
|
/// isClassScope - Return true if this scope is a class/struct/union scope.
|
|
|
|
bool isClassScope() const {
|
|
|
|
return (getFlags() & Scope::ClassScope);
|
2008-06-25 02:12:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
|
|
|
|
/// method scope or is inside one.
|
|
|
|
bool isInCXXInlineMethodScope() const {
|
|
|
|
if (const Scope *FnS = getFnParent()) {
|
|
|
|
assert(FnS->getParent() && "TUScope not created?");
|
2009-01-10 01:42:13 +03:00
|
|
|
return FnS->getParent()->isClassScope();
|
2008-06-25 02:12:16 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-12 09:38:43 +04:00
|
|
|
|
|
|
|
/// isInObjcMethodScope - Return true if this scope is, or is contained in, an
|
|
|
|
/// Objective-C method body. Note that this method is not constant time.
|
|
|
|
bool isInObjcMethodScope() const {
|
|
|
|
for (const Scope *S = this; S; S = S->getParent()) {
|
|
|
|
// If this scope is an objc method scope, then we succeed.
|
|
|
|
if (S->getFlags() & ObjCMethodScope)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-12-02 03:41:28 +03:00
|
|
|
/// isTemplateParamScope - Return true if this scope is a C++
|
|
|
|
/// template parameter scope.
|
|
|
|
bool isTemplateParamScope() const {
|
|
|
|
return getFlags() & Scope::TemplateParamScope;
|
|
|
|
}
|
|
|
|
|
2009-01-10 01:42:13 +03:00
|
|
|
/// isFunctionPrototypeScope - Return true if this scope is a
|
|
|
|
/// function prototype scope.
|
|
|
|
bool isFunctionPrototypeScope() const {
|
|
|
|
return getFlags() & Scope::FunctionPrototypeScope;
|
|
|
|
}
|
|
|
|
|
2012-06-14 02:07:09 +04:00
|
|
|
/// isAtCatchScope - Return true if this scope is \@catch.
|
2009-02-11 23:05:44 +03:00
|
|
|
bool isAtCatchScope() const {
|
|
|
|
return getFlags() & Scope::AtCatchScope;
|
|
|
|
}
|
|
|
|
|
2011-04-22 01:44:26 +04:00
|
|
|
/// isSwitchScope - Return true if this scope is a switch scope.
|
|
|
|
bool isSwitchScope() const {
|
|
|
|
for (const Scope *S = this; S; S = S->getParent()) {
|
|
|
|
if (S->getFlags() & Scope::SwitchScope)
|
|
|
|
return true;
|
|
|
|
else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
|
|
|
|
Scope::BlockScope | Scope::TemplateParamScope |
|
|
|
|
Scope::FunctionPrototypeScope |
|
|
|
|
Scope::AtCatchScope | Scope::ObjCMethodScope))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-07 02:04:06 +04:00
|
|
|
|
|
|
|
/// \brief Determine whether this scope is a C++ 'try' block.
|
|
|
|
bool isTryScope() const { return getFlags() & Scope::TryScope; }
|
2011-04-22 01:44:26 +04:00
|
|
|
|
2012-02-29 14:24:19 +04:00
|
|
|
/// containedInPrototypeScope - Return true if this or a parent scope
|
|
|
|
/// is a FunctionPrototypeScope.
|
|
|
|
bool containedInPrototypeScope() const;
|
|
|
|
|
2009-02-03 22:21:40 +03:00
|
|
|
typedef UsingDirectivesTy::iterator udir_iterator;
|
|
|
|
typedef UsingDirectivesTy::const_iterator const_udir_iterator;
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
void PushUsingDirective(UsingDirectiveDecl *UDir) {
|
2009-02-03 22:21:40 +03:00
|
|
|
UsingDirectives.push_back(UDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
udir_iterator using_directives_begin() {
|
|
|
|
return UsingDirectives.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
udir_iterator using_directives_end() {
|
|
|
|
return UsingDirectives.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_udir_iterator using_directives_begin() const {
|
|
|
|
return UsingDirectives.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_udir_iterator using_directives_end() const {
|
|
|
|
return UsingDirectives.end();
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// Init - This is used by the parser to implement scope caching.
|
|
|
|
///
|
2011-05-02 02:35:37 +04:00
|
|
|
void Init(Scope *parent, unsigned flags);
|
2007-07-11 21:01:13 +04:00
|
|
|
};
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|