2006-08-05 22:46:42 +00:00
|
|
|
//===--- Scope.h - Scope interface ------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:25 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-05 22:46:42 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the Scope interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_PARSE_SCOPE_H
|
|
|
|
#define LLVM_CLANG_PARSE_SCOPE_H
|
|
|
|
|
2006-08-14 00:57:12 +00:00
|
|
|
#include "clang/Parse/Action.h"
|
2007-01-27 07:16:01 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2006-08-05 22:46:42 +00:00
|
|
|
|
|
|
|
namespace clang {
|
2006-11-05 23:47:55 +00:00
|
|
|
|
2006-08-05 22:46:42 +00: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 {
|
2006-11-05 23:47:55 +00:00
|
|
|
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 {
|
|
|
|
/// FnScope - This indicates that the scope corresponds to a function, which
|
|
|
|
/// means that labels are set here.
|
|
|
|
FnScope = 0x01,
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2006-11-05 23:47:55 +00:00
|
|
|
/// BreakScope - This is a while,do,switch,for, etc that can have break
|
|
|
|
/// stmts embedded into it.
|
|
|
|
BreakScope = 0x02,
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2006-11-05 23:47:55 +00:00
|
|
|
/// ContinueScope - This is a while,do,for, which can have continue
|
|
|
|
/// stmt embedded into it.
|
2007-08-26 06:24:45 +00:00
|
|
|
ContinueScope = 0x04,
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2007-08-26 06:24:45 +00:00
|
|
|
/// DeclScope - This is a scope that can contain a declaration. Some scopes
|
|
|
|
/// just contain loop constructs but don't contain decls.
|
2008-06-24 22:12:16 +00:00
|
|
|
DeclScope = 0x08,
|
|
|
|
|
2008-09-09 20:38:47 +00:00
|
|
|
/// ControlScope - The controlling scope in a if/switch/while/for statement.
|
|
|
|
ControlScope = 0x10,
|
|
|
|
|
2009-01-09 22:42:13 +00:00
|
|
|
/// ClassScope - The scope of a struct/union/class definition.
|
|
|
|
ClassScope = 0x20,
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2008-08-28 19:20:44 +00:00
|
|
|
/// BlockScope - This is a scope that corresponds to a block object.
|
|
|
|
/// Blocks serve as top-level scopes for some objects like labels, they
|
2010-04-12 05:38:43 +00:00
|
|
|
/// also prevent things like break and continue. BlockScopes always have
|
|
|
|
/// the FnScope, BreakScope, ContinueScope, and DeclScope flags set as well.
|
2008-12-02 00:41:28 +00:00
|
|
|
BlockScope = 0x40,
|
|
|
|
|
|
|
|
/// TemplateParamScope - This is a scope that corresponds to the
|
|
|
|
/// template parameters of a C++ template. Template parameter
|
|
|
|
/// scope starts at the 'template' keyword and ends when the
|
|
|
|
/// template declaration ends.
|
2009-01-09 22:42:13 +00:00
|
|
|
TemplateParamScope = 0x80,
|
|
|
|
|
|
|
|
/// FunctionPrototypeScope - This is a scope that corresponds to the
|
|
|
|
/// parameters within a function prototype.
|
2009-02-11 20:05:44 +00:00
|
|
|
FunctionPrototypeScope = 0x100,
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-02-11 20:05:44 +00:00
|
|
|
/// AtCatchScope - This is a scope that corresponds to the Objective-C
|
|
|
|
/// @catch statement.
|
2010-04-12 05:38:43 +00:00
|
|
|
AtCatchScope = 0x200,
|
|
|
|
|
|
|
|
/// ObjCMethodScope - This scope corresponds to an Objective-C method body.
|
|
|
|
/// It always has FnScope and DeclScope set as well.
|
2010-08-06 06:50:17 +00:00
|
|
|
ObjCMethodScope = 0x400
|
2006-11-05 23:47:55 +00:00
|
|
|
};
|
|
|
|
private:
|
2006-08-05 22:46:42 +00:00
|
|
|
/// The parent scope for this scope. This is null for the translation-unit
|
|
|
|
/// scope.
|
2006-11-05 23:47:55 +00:00
|
|
|
Scope *AnyParent;
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2006-08-05 22:46:42 +00:00
|
|
|
/// Depth - This is the depth of this scope. The translation-unit scope has
|
|
|
|
/// depth 0.
|
2010-04-12 06:12:50 +00:00
|
|
|
unsigned short Depth;
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2006-11-05 23:47:55 +00:00
|
|
|
/// Flags - This contains a set of ScopeFlags, which indicates how the scope
|
|
|
|
/// interrelates with other control flow statements.
|
2010-04-12 06:12:50 +00:00
|
|
|
unsigned short Flags;
|
2008-12-10 23:01:14 +00:00
|
|
|
|
2006-11-05 23:47:55 +00: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 15:08:12 +00:00
|
|
|
|
2006-11-05 23:47:55 +00:00
|
|
|
/// BreakParent/ContinueParent - This is a direct link to the immediately
|
|
|
|
/// preceeding BreakParent/ContinueParent if this scope is not one, or null if
|
|
|
|
/// there is no containing break/continue scope.
|
|
|
|
Scope *BreakParent, *ContinueParent;
|
2008-09-10 19:17:48 +00:00
|
|
|
|
2008-12-10 23:01:14 +00:00
|
|
|
/// ControlParent - This is a direct link to the immediately
|
|
|
|
/// preceeding ControlParent if this scope is not one, or null if
|
|
|
|
/// there is no containing control scope.
|
|
|
|
Scope *ControlParent;
|
|
|
|
|
2008-09-10 19:17:48 +00: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 18:15:24 +00: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;
|
|
|
|
|
2006-08-05 22:46:42 +00: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
|
2006-08-14 00:57:12 +00:00
|
|
|
/// of current declaration. It is up to the current Action implementation to
|
|
|
|
/// implement these semantics.
|
2009-03-28 19:18:32 +00:00
|
|
|
typedef llvm::SmallPtrSet<Action::DeclPtrTy, 32> DeclSetTy;
|
2007-01-27 07:16:01 +00:00
|
|
|
DeclSetTy DeclsInScope;
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2008-12-11 16:49:14 +00: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;
|
|
|
|
|
2009-03-28 19:18:32 +00:00
|
|
|
typedef llvm::SmallVector<Action::DeclPtrTy, 2> UsingDirectivesTy;
|
2009-02-03 19:21:40 +00:00
|
|
|
UsingDirectivesTy UsingDirectives;
|
|
|
|
|
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.
llvm-svn: 97518
2010-03-01 23:15:13 +00:00
|
|
|
/// \brief The number of errors at the start of the given scope.
|
|
|
|
unsigned NumErrorsAtStart;
|
|
|
|
|
2006-08-05 22:46:42 +00:00
|
|
|
public:
|
2006-11-06 00:22:42 +00:00
|
|
|
Scope(Scope *Parent, unsigned ScopeFlags) {
|
|
|
|
Init(Parent, ScopeFlags);
|
2006-08-05 22:46:42 +00:00
|
|
|
}
|
2007-08-26 06:24:45 +00:00
|
|
|
|
|
|
|
/// getFlags - Return the flags for this scope.
|
|
|
|
///
|
|
|
|
unsigned getFlags() const { return Flags; }
|
2010-04-12 06:12:50 +00:00
|
|
|
void setFlags(unsigned F) { Flags = F; }
|
2007-08-26 06:24:45 +00:00
|
|
|
|
2008-09-10 19:17:48 +00:00
|
|
|
/// isBlockScope - Return true if this scope does not correspond to a
|
|
|
|
/// closure.
|
|
|
|
bool isBlockScope() const { return Flags & BlockScope; }
|
|
|
|
|
2006-08-14 00:13:44 +00:00
|
|
|
/// getParent - Return the scope that this is nested in.
|
|
|
|
///
|
2007-08-26 06:24:45 +00: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 15:08:12 +00:00
|
|
|
|
2006-11-05 23:47:55 +00:00
|
|
|
/// getContinueParent - Return the closest scope that a continue statement
|
2009-09-09 15:08:12 +00:00
|
|
|
/// would be affected by. If the closest scope is a closure scope, we know
|
2008-09-10 19:17:48 +00:00
|
|
|
/// that there is no loop *inside* the closure.
|
|
|
|
Scope *getContinueParent() {
|
|
|
|
if (ContinueParent && !ContinueParent->isBlockScope())
|
|
|
|
return ContinueParent;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Scope *getContinueParent() const {
|
|
|
|
return const_cast<Scope*>(this)->getContinueParent();
|
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2006-11-05 23:47:55 +00:00
|
|
|
/// getBreakParent - Return the closest scope that a break statement
|
2009-09-09 15:08:12 +00:00
|
|
|
/// would be affected by. If the closest scope is a block scope, we know
|
2008-09-10 19:17:48 +00:00
|
|
|
/// that there is no loop *inside* the block.
|
|
|
|
Scope *getBreakParent() {
|
|
|
|
if (BreakParent && !BreakParent->isBlockScope())
|
|
|
|
return BreakParent;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const Scope *getBreakParent() const {
|
|
|
|
return const_cast<Scope*>(this)->getBreakParent();
|
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2008-12-10 23:01:14 +00:00
|
|
|
Scope *getControlParent() { return ControlParent; }
|
|
|
|
const Scope *getControlParent() const { return ControlParent; }
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2008-09-10 19:17:48 +00:00
|
|
|
Scope *getBlockParent() { return BlockParent; }
|
2009-09-09 15:08:12 +00:00
|
|
|
const Scope *getBlockParent() const { return BlockParent; }
|
2008-12-05 18:15:24 +00:00
|
|
|
|
|
|
|
Scope *getTemplateParamParent() { return TemplateParamParent; }
|
2009-09-09 15:08:12 +00:00
|
|
|
const Scope *getTemplateParamParent() const { return TemplateParamParent; }
|
|
|
|
|
2007-01-27 07:16:01 +00:00
|
|
|
typedef DeclSetTy::iterator decl_iterator;
|
2007-01-23 03:43:34 +00:00
|
|
|
decl_iterator decl_begin() const { return DeclsInScope.begin(); }
|
|
|
|
decl_iterator decl_end() const { return DeclsInScope.end(); }
|
2007-08-26 06:24:45 +00:00
|
|
|
bool decl_empty() const { return DeclsInScope.empty(); }
|
2006-08-14 00:57:12 +00:00
|
|
|
|
2009-03-28 19:18:32 +00:00
|
|
|
void AddDecl(Action::DeclPtrTy D) {
|
2007-01-23 01:35:19 +00:00
|
|
|
DeclsInScope.insert(D);
|
2006-08-14 00:57:12 +00:00
|
|
|
}
|
2008-04-21 02:02:58 +00:00
|
|
|
|
2009-03-28 19:18:32 +00:00
|
|
|
void RemoveDecl(Action::DeclPtrTy D) {
|
2008-12-11 16:49:14 +00:00
|
|
|
DeclsInScope.erase(D);
|
|
|
|
}
|
|
|
|
|
2007-01-21 22:37:37 +00:00
|
|
|
/// isDeclScope - Return true if this is the scope that the specified decl is
|
|
|
|
/// declared in.
|
2009-03-28 19:18:32 +00:00
|
|
|
bool isDeclScope(Action::DeclPtrTy D) {
|
2007-01-23 01:35:19 +00:00
|
|
|
return DeclsInScope.count(D) != 0;
|
2007-01-21 22:37:37 +00:00
|
|
|
}
|
2008-06-24 22:12:16 +00:00
|
|
|
|
2008-12-11 16:49:14 +00:00
|
|
|
void* getEntity() const { return Entity; }
|
|
|
|
void setEntity(void *E) { Entity = E; }
|
|
|
|
|
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.
llvm-svn: 97518
2010-03-01 23:15:13 +00:00
|
|
|
/// \brief Retrieve the number of errors that had been emitted when we
|
|
|
|
/// entered this scope.
|
|
|
|
unsigned getNumErrorsAtStart() const { return NumErrorsAtStart; }
|
|
|
|
|
2010-03-01 23:31:19 +00:00
|
|
|
void setNumErrorsAtStart(unsigned NumErrors) {
|
|
|
|
NumErrorsAtStart = NumErrors;
|
|
|
|
}
|
|
|
|
|
2009-01-09 22:42:13 +00:00
|
|
|
/// isClassScope - Return true if this scope is a class/struct/union scope.
|
|
|
|
bool isClassScope() const {
|
|
|
|
return (getFlags() & Scope::ClassScope);
|
2008-06-24 22:12:16 +00: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-09 22:42:13 +00:00
|
|
|
return FnS->getParent()->isClassScope();
|
2008-06-24 22:12:16 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-12 05:38:43 +00: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 15:08:12 +00:00
|
|
|
|
2008-12-02 00:41:28 +00:00
|
|
|
/// isTemplateParamScope - Return true if this scope is a C++
|
|
|
|
/// template parameter scope.
|
|
|
|
bool isTemplateParamScope() const {
|
|
|
|
return getFlags() & Scope::TemplateParamScope;
|
|
|
|
}
|
|
|
|
|
2009-01-09 22:42:13 +00:00
|
|
|
/// isFunctionPrototypeScope - Return true if this scope is a
|
|
|
|
/// function prototype scope.
|
|
|
|
bool isFunctionPrototypeScope() const {
|
|
|
|
return getFlags() & Scope::FunctionPrototypeScope;
|
|
|
|
}
|
|
|
|
|
2009-02-11 20:05:44 +00:00
|
|
|
/// isAtCatchScope - Return true if this scope is @catch.
|
|
|
|
bool isAtCatchScope() const {
|
|
|
|
return getFlags() & Scope::AtCatchScope;
|
|
|
|
}
|
|
|
|
|
2009-02-03 19:21:40 +00:00
|
|
|
typedef UsingDirectivesTy::iterator udir_iterator;
|
|
|
|
typedef UsingDirectivesTy::const_iterator const_udir_iterator;
|
|
|
|
|
2009-03-28 19:18:32 +00:00
|
|
|
void PushUsingDirective(Action::DeclPtrTy UDir) {
|
2009-02-03 19:21:40 +00: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();
|
|
|
|
}
|
|
|
|
|
2006-11-06 00:22:42 +00:00
|
|
|
/// Init - This is used by the parser to implement scope caching.
|
|
|
|
///
|
|
|
|
void Init(Scope *Parent, unsigned ScopeFlags) {
|
|
|
|
AnyParent = Parent;
|
|
|
|
Depth = AnyParent ? AnyParent->Depth+1 : 0;
|
|
|
|
Flags = ScopeFlags;
|
2009-11-05 17:49:26 +00:00
|
|
|
|
2006-11-06 00:22:42 +00:00
|
|
|
if (AnyParent) {
|
|
|
|
FnParent = AnyParent->FnParent;
|
|
|
|
BreakParent = AnyParent->BreakParent;
|
|
|
|
ContinueParent = AnyParent->ContinueParent;
|
2008-12-10 23:01:14 +00:00
|
|
|
ControlParent = AnyParent->ControlParent;
|
2008-09-10 19:17:48 +00:00
|
|
|
BlockParent = AnyParent->BlockParent;
|
2008-12-05 18:15:24 +00:00
|
|
|
TemplateParamParent = AnyParent->TemplateParamParent;
|
2006-11-06 00:22:42 +00:00
|
|
|
} else {
|
2008-09-10 19:17:48 +00:00
|
|
|
FnParent = BreakParent = ContinueParent = BlockParent = 0;
|
2008-12-10 23:01:14 +00:00
|
|
|
ControlParent = 0;
|
2008-12-05 18:15:24 +00:00
|
|
|
TemplateParamParent = 0;
|
2006-11-06 00:22:42 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2006-11-06 00:22:42 +00:00
|
|
|
// If this scope is a function or contains breaks/continues, remember it.
|
2008-12-16 19:57:09 +00:00
|
|
|
if (Flags & FnScope) FnParent = this;
|
|
|
|
if (Flags & BreakScope) BreakParent = this;
|
|
|
|
if (Flags & ContinueScope) ContinueParent = this;
|
2008-12-10 23:01:14 +00:00
|
|
|
if (Flags & ControlScope) ControlParent = this;
|
2008-12-16 19:57:09 +00:00
|
|
|
if (Flags & BlockScope) BlockParent = this;
|
2008-12-05 18:15:24 +00:00
|
|
|
if (Flags & TemplateParamScope) TemplateParamParent = this;
|
2006-11-06 00:22:42 +00:00
|
|
|
DeclsInScope.clear();
|
2009-02-03 19:21:40 +00:00
|
|
|
UsingDirectives.clear();
|
2008-12-11 16:49:14 +00:00
|
|
|
Entity = 0;
|
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.
llvm-svn: 97518
2010-03-01 23:15:13 +00:00
|
|
|
NumErrorsAtStart = 0;
|
2008-12-05 18:15:24 +00:00
|
|
|
}
|
2006-08-05 22:46:42 +00:00
|
|
|
};
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2006-08-05 22:46:42 +00:00
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|