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