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,
|
|
|
|
|
|
|
|
/// BreakScope - This is a while,do,switch,for, etc that can have break
|
|
|
|
/// stmts embedded into it.
|
|
|
|
BreakScope = 0x02,
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
|
2008-06-24 22:12:16 +00:00
|
|
|
/// CXXClassScope - The scope of a C++ struct/union/class definition.
|
2008-09-09 20:38:47 +00:00
|
|
|
CXXClassScope = 0x20,
|
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
|
|
|
|
/// also prevent things like break and continue. BlockScopes have the
|
|
|
|
/// other 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.
|
|
|
|
TemplateParamScope = 0x80
|
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;
|
2006-08-05 22:46:42 +00:00
|
|
|
|
|
|
|
/// Depth - This is the depth of this scope. The translation-unit scope has
|
|
|
|
/// depth 0.
|
2006-11-05 23:47:55 +00:00
|
|
|
unsigned Depth : 16;
|
|
|
|
|
|
|
|
/// Flags - This contains a set of ScopeFlags, which indicates how the scope
|
|
|
|
/// interrelates with other control flow statements.
|
|
|
|
unsigned Flags : 8;
|
|
|
|
|
2008-12-10 23:01:14 +00:00
|
|
|
/// WithinElse - Whether this scope is part of the "else" branch in
|
|
|
|
/// its parent ControlScope.
|
|
|
|
bool WithinElse : 1;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/// 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.
|
2007-06-15 23:05:46 +00:00
|
|
|
typedef llvm::SmallPtrSet<Action::DeclTy*, 32> DeclSetTy;
|
2007-01-27 07:16:01 +00:00
|
|
|
DeclSetTy DeclsInScope;
|
2007-07-18 02:28:47 +00:00
|
|
|
|
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; }
|
|
|
|
|
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; }
|
2006-11-05 23:47:55 +00:00
|
|
|
|
|
|
|
/// getContinueParent - Return the closest scope that a continue statement
|
2008-09-10 19:17:48 +00:00
|
|
|
/// would be affected by. If the closest scope is a closure scope, we know
|
|
|
|
/// 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();
|
|
|
|
}
|
2006-11-05 23:47:55 +00:00
|
|
|
|
|
|
|
/// getBreakParent - Return the closest scope that a break statement
|
2008-09-10 19:17:48 +00:00
|
|
|
/// would be affected by. If the closest scope is a block scope, we know
|
|
|
|
/// 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();
|
|
|
|
}
|
|
|
|
|
2008-12-10 23:01:14 +00:00
|
|
|
Scope *getControlParent() { return ControlParent; }
|
|
|
|
const Scope *getControlParent() const { return ControlParent; }
|
|
|
|
|
2008-09-10 19:17:48 +00:00
|
|
|
Scope *getBlockParent() { return BlockParent; }
|
|
|
|
const Scope *getBlockParent() const { return BlockParent; }
|
2008-12-05 18:15:24 +00:00
|
|
|
|
|
|
|
Scope *getTemplateParamParent() { return TemplateParamParent; }
|
|
|
|
const Scope *getTemplateParamParent() const { return TemplateParamParent; }
|
2007-08-26 06:24:45 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
void AddDecl(Action::DeclTy *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
|
|
|
|
2007-01-21 22:37:37 +00:00
|
|
|
/// isDeclScope - Return true if this is the scope that the specified decl is
|
|
|
|
/// declared in.
|
|
|
|
bool isDeclScope(Action::DeclTy *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
|
|
|
|
|
|
|
/// isCXXClassScope - Return true if this scope is a C++ class scope.
|
|
|
|
bool isCXXClassScope() const {
|
|
|
|
return (getFlags() & Scope::CXXClassScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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?");
|
|
|
|
return FnS->getParent()->isCXXClassScope();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-11-06 00:22:42 +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;
|
|
|
|
}
|
|
|
|
|
2008-12-10 23:01:14 +00:00
|
|
|
/// isWithinElse - Whether we are within the "else" of the
|
|
|
|
/// ControlParent (if any).
|
|
|
|
bool isWithinElse() const { return WithinElse; }
|
|
|
|
|
|
|
|
void setWithinElse(bool WE) { WithinElse = WE; }
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
2008-12-10 23:01:14 +00:00
|
|
|
WithinElse = AnyParent->WithinElse;
|
|
|
|
|
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;
|
2008-12-10 23:01:14 +00:00
|
|
|
WithinElse = false;
|
2006-11-06 00:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this scope is a function or contains breaks/continues, remember it.
|
2008-12-05 18:15:24 +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-05 18:15:24 +00:00
|
|
|
if (Flags & BlockScope) BlockParent = this;
|
|
|
|
if (Flags & TemplateParamScope) TemplateParamParent = this;
|
2006-11-06 00:22:42 +00:00
|
|
|
DeclsInScope.clear();
|
2008-12-05 18:15:24 +00:00
|
|
|
}
|
2006-08-05 22:46:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|