Commit 7eaeb5ae authored by marja's avatar marja Committed by Commit bot

Scopes: simplify scope creation.

With scopes: Don't call the ctor which wants a ScopeInfo if we
don't want to pass it, instead call a ctor which doesn't need it.

In addition, remove inner_scope from ctors and adjust it
explicitly afterwards. It's confusing that some ctors get passed
inner scopes and some outer scopes.

BUG=v8:5209

Review-Url: https://codereview.chromium.org/2270743002
Cr-Commit-Position: refs/heads/master@{#38859}
parent 6e4d678d
......@@ -77,14 +77,20 @@ void SloppyBlockFunctionMap::Declare(Zone* zone, const AstRawString* name,
// ----------------------------------------------------------------------------
// Implementation of Scope
Scope::Scope(Zone* zone)
Scope::Scope(Zone* zone, ScopeType scope_type)
: zone_(zone),
outer_scope_(nullptr),
variables_(zone),
ordered_variables_(4, zone),
decls_(4, zone),
scope_type_(SCRIPT_SCOPE) {
scope_type_(scope_type) {
DCHECK(scope_type == SCRIPT_SCOPE || scope_type == WITH_SCOPE);
SetDefaults();
#ifdef DEBUG
if (scope_type == WITH_SCOPE) {
already_resolved_ = true;
}
#endif
}
Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type)
......@@ -137,8 +143,7 @@ ModuleScope::ModuleScope(Zone* zone, DeclarationScope* script_scope,
DeclareThis(ast_value_factory);
}
Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type,
Handle<ScopeInfo> scope_info)
Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info)
: zone_(zone),
outer_scope_(nullptr),
variables_(zone),
......@@ -146,26 +151,20 @@ Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type,
decls_(0, zone),
scope_info_(scope_info),
scope_type_(scope_type) {
DCHECK(!scope_info.is_null());
SetDefaults();
#ifdef DEBUG
already_resolved_ = true;
#endif
if (scope_type == WITH_SCOPE) {
DCHECK(scope_info.is_null());
} else {
if (scope_info->CallsEval()) RecordEvalCall();
set_language_mode(scope_info->language_mode());
num_heap_slots_ = scope_info->ContextLength();
}
if (scope_info->CallsEval()) RecordEvalCall();
set_language_mode(scope_info->language_mode());
num_heap_slots_ = scope_info->ContextLength();
DCHECK_LE(Context::MIN_CONTEXT_SLOTS, num_heap_slots_);
if (inner_scope != nullptr) AddInnerScope(inner_scope);
}
DeclarationScope::DeclarationScope(Zone* zone, Scope* inner_scope,
ScopeType scope_type,
DeclarationScope::DeclarationScope(Zone* zone, ScopeType scope_type,
Handle<ScopeInfo> scope_info)
: Scope(zone, inner_scope, scope_type, scope_info),
: Scope(zone, scope_type, scope_info),
function_kind_(scope_info->function_kind()),
temps_(0, zone),
params_(0, zone),
......@@ -173,8 +172,7 @@ DeclarationScope::DeclarationScope(Zone* zone, Scope* inner_scope,
SetDefaults();
}
Scope::Scope(Zone* zone, Scope* inner_scope,
const AstRawString* catch_variable_name)
Scope::Scope(Zone* zone, const AstRawString* catch_variable_name)
: zone_(zone),
outer_scope_(nullptr),
variables_(zone),
......@@ -185,7 +183,6 @@ Scope::Scope(Zone* zone, Scope* inner_scope,
#ifdef DEBUG
already_resolved_ = true;
#endif
if (inner_scope != nullptr) AddInnerScope(inner_scope);
Variable* variable =
variables_.Declare(zone, this, catch_variable_name, VAR, Variable::NORMAL,
kCreatedInitialized);
......@@ -259,17 +256,17 @@ Scope* Scope::DeserializeScopeChain(Isolate* isolate, Zone* zone,
// Reconstruct the outer scope chain from a closure's context chain.
Scope* current_scope = nullptr;
Scope* innermost_scope = nullptr;
Scope* outer_scope = nullptr;
while (!context->IsNativeContext()) {
if (context->IsWithContext() || context->IsDebugEvaluateContext()) {
// For scope analysis, debug-evaluate is equivalent to a with scope.
Scope* with_scope = new (zone)
Scope(zone, current_scope, WITH_SCOPE, Handle<ScopeInfo>());
outer_scope = new (zone) Scope(zone, WITH_SCOPE);
// TODO(yangguo): Remove once debug-evaluate properly keeps track of the
// function scope in which we are evaluating.
if (context->IsDebugEvaluateContext()) {
with_scope->set_is_debug_evaluate_scope();
outer_scope->set_is_debug_evaluate_scope();
}
current_scope = with_scope;
} else if (context->IsScriptContext()) {
// If we reach a script context, it's the outermost context with scope
// info. The next context will be the native context. Install the scope
......@@ -287,28 +284,31 @@ Scope* Scope::DeserializeScopeChain(Isolate* isolate, Zone* zone,
// https://bugs.chromium.org/p/v8/issues/detail?id=5295
DCHECK(scope_info->scope_type() == FUNCTION_SCOPE ||
scope_info->scope_type() == EVAL_SCOPE);
DeclarationScope* function_scope = new (zone)
DeclarationScope(zone, current_scope, FUNCTION_SCOPE, scope_info);
if (scope_info->IsAsmFunction()) function_scope->set_asm_function();
if (scope_info->IsAsmModule()) function_scope->set_asm_module();
current_scope = function_scope;
outer_scope =
new (zone) DeclarationScope(zone, FUNCTION_SCOPE, scope_info);
if (scope_info->IsAsmFunction())
outer_scope->AsDeclarationScope()->set_asm_function();
if (scope_info->IsAsmModule())
outer_scope->AsDeclarationScope()->set_asm_module();
} else if (context->IsBlockContext()) {
Handle<ScopeInfo> scope_info(context->scope_info(), isolate);
DCHECK_EQ(scope_info->scope_type(), BLOCK_SCOPE);
if (scope_info->is_declaration_scope()) {
current_scope = new (zone)
DeclarationScope(zone, current_scope, BLOCK_SCOPE, scope_info);
outer_scope =
new (zone) DeclarationScope(zone, BLOCK_SCOPE, scope_info);
} else {
current_scope =
new (zone) Scope(zone, current_scope, BLOCK_SCOPE, scope_info);
outer_scope = new (zone) Scope(zone, BLOCK_SCOPE, scope_info);
}
} else {
DCHECK(context->IsCatchContext());
String* name = context->catch_name();
current_scope =
new (zone) Scope(zone, current_scope,
ast_value_factory->GetString(handle(name, isolate)));
outer_scope = new (zone)
Scope(zone, ast_value_factory->GetString(handle(name, isolate)));
}
if (current_scope != nullptr) {
outer_scope->AddInnerScope(current_scope);
}
current_scope = outer_scope;
if (deserialization_mode == DeserializationMode::kDeserializeOffHeap) {
current_scope->DeserializeScopeInfo(isolate, ast_value_factory);
}
......
......@@ -416,8 +416,7 @@ class Scope: public ZoneObject {
void set_is_debug_evaluate_scope() { is_debug_evaluate_scope_ = true; }
protected:
// Creates a script scope.
explicit Scope(Zone* zone);
explicit Scope(Zone* zone, ScopeType scope_type = SCRIPT_SCOPE);
void set_language_mode(LanguageMode language_mode) {
is_strict_ = is_strict(language_mode);
......@@ -542,12 +541,10 @@ class Scope: public ZoneObject {
void AllocateVariablesRecursively();
// Construct a scope based on the scope info.
Scope(Zone* zone, Scope* inner_scope, ScopeType type,
Handle<ScopeInfo> scope_info);
Scope(Zone* zone, ScopeType type, Handle<ScopeInfo> scope_info);
// Construct a catch scope with a binding for the name.
Scope(Zone* zone, Scope* inner_scope,
const AstRawString* catch_variable_name);
Scope(Zone* zone, const AstRawString* catch_variable_name);
void AddInnerScope(Scope* inner_scope) {
inner_scope->sibling_ = inner_scope_;
......@@ -582,7 +579,7 @@ class DeclarationScope : public Scope {
public:
DeclarationScope(Zone* zone, Scope* outer_scope, ScopeType scope_type,
FunctionKind function_kind = kNormalFunction);
DeclarationScope(Zone* zone, Scope* inner_scope, ScopeType scope_type,
DeclarationScope(Zone* zone, ScopeType scope_type,
Handle<ScopeInfo> scope_info);
// Creates a script scope.
explicit DeclarationScope(Zone* zone);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment