Index: src/ast/scopes.cc |
diff --git a/src/ast/scopes.cc b/src/ast/scopes.cc |
index 2a41c7d47a2c1a458422e4f1225b24bd080b8567..59b0bd9034aa1a75cedac51e672930af0a57455a 100644 |
--- a/src/ast/scopes.cc |
+++ b/src/ast/scopes.cc |
@@ -41,13 +41,27 @@ Variable* VariableMap::Declare(Zone* zone, Scope* scope, |
if (added) *added = p->value == nullptr; |
if (p->value == nullptr) { |
// The variable has not been declared yet -> insert it. |
- DCHECK(p->key == name); |
+ DCHECK_EQ(name, p->key); |
p->value = new (zone) Variable(scope, name, mode, kind, initialization_flag, |
maybe_assigned_flag); |
} |
return reinterpret_cast<Variable*>(p->value); |
} |
+void VariableMap::Remove(Variable* var) { |
+ const AstRawString* name = var->raw_name(); |
+ ZoneHashMap::Remove(const_cast<AstRawString*>(name), name->hash()); |
+} |
+ |
+void VariableMap::Add(Zone* zone, Variable* var) { |
+ const AstRawString* name = var->raw_name(); |
+ Entry* p = |
+ ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), name->hash(), |
+ ZoneAllocationPolicy(zone)); |
+ DCHECK_NULL(p->value); |
+ DCHECK_EQ(name, p->key); |
+ p->value = var; |
+} |
Variable* VariableMap::Lookup(const AstRawString* name) { |
Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash()); |
@@ -81,7 +95,7 @@ Scope::Scope(Zone* zone, ScopeType scope_type) |
: zone_(zone), |
outer_scope_(nullptr), |
variables_(zone), |
- ordered_variables_(4, zone), |
+ locals_(4, zone), |
decls_(4, zone), |
scope_type_(scope_type) { |
DCHECK(scope_type == SCRIPT_SCOPE || scope_type == WITH_SCOPE); |
@@ -97,7 +111,7 @@ Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type) |
: zone_(zone), |
outer_scope_(outer_scope), |
variables_(zone), |
- ordered_variables_(4, zone), |
+ locals_(4, zone), |
decls_(4, zone), |
scope_type_(scope_type) { |
DCHECK_NE(SCRIPT_SCOPE, scope_type); |
@@ -112,12 +126,12 @@ Scope::Snapshot::Snapshot(Scope* scope) |
: outer_scope_(scope), |
top_inner_scope_(scope->inner_scope_), |
top_unresolved_(scope->unresolved_), |
- top_temp_(scope->GetClosureScope()->temps()->length()) {} |
+ top_local_(scope->GetClosureScope()->locals_.length()), |
+ top_decl_(scope->GetClosureScope()->decls_.length()) {} |
DeclarationScope::DeclarationScope(Zone* zone) |
: Scope(zone), |
function_kind_(kNormalFunction), |
- temps_(4, zone), |
params_(4, zone), |
sloppy_block_function_map_(zone) { |
SetDefaults(); |
@@ -128,7 +142,6 @@ DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope, |
FunctionKind function_kind) |
: Scope(zone, outer_scope, scope_type), |
function_kind_(function_kind), |
- temps_(4, zone), |
params_(4, zone), |
sloppy_block_function_map_(zone) { |
SetDefaults(); |
@@ -147,7 +160,7 @@ Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info) |
: zone_(zone), |
outer_scope_(nullptr), |
variables_(zone), |
- ordered_variables_(0, zone), |
+ locals_(0, zone), |
decls_(0, zone), |
scope_info_(scope_info), |
scope_type_(scope_type) { |
@@ -166,7 +179,6 @@ DeclarationScope::DeclarationScope(Zone* zone, ScopeType scope_type, |
Handle<ScopeInfo> scope_info) |
: Scope(zone, scope_type, scope_info), |
function_kind_(scope_info->function_kind()), |
- temps_(0, zone), |
params_(0, zone), |
sloppy_block_function_map_(zone) { |
SetDefaults(); |
@@ -176,7 +188,7 @@ Scope::Scope(Zone* zone, const AstRawString* catch_variable_name) |
: zone_(zone), |
outer_scope_(nullptr), |
variables_(zone), |
- ordered_variables_(0, zone), |
+ locals_(0, zone), |
decls_(0, zone), |
scope_type_(CATCH_SCOPE) { |
SetDefaults(); |
@@ -530,7 +542,7 @@ void Scope::Snapshot::Reparent(DeclarationScope* new_parent) const { |
DCHECK_EQ(new_parent, new_parent->GetClosureScope()); |
DCHECK_NULL(new_parent->inner_scope_); |
DCHECK_NULL(new_parent->unresolved_); |
- DCHECK_EQ(0, new_parent->temps()->length()); |
+ DCHECK_EQ(0, new_parent->locals_.length()); |
Scope* inner_scope = new_parent->sibling_; |
if (inner_scope != top_inner_scope_) { |
for (; inner_scope->sibling() != top_inner_scope_; |
@@ -557,17 +569,25 @@ void Scope::Snapshot::Reparent(DeclarationScope* new_parent) const { |
outer_scope_->unresolved_ = top_unresolved_; |
} |
- if (outer_scope_->GetClosureScope()->temps()->length() != top_temp_) { |
- ZoneList<Variable*>* temps = outer_scope_->GetClosureScope()->temps(); |
- for (int i = top_temp_; i < temps->length(); i++) { |
- Variable* temp = temps->at(i); |
- DCHECK_EQ(temp->scope(), temp->scope()->GetClosureScope()); |
- DCHECK_NE(temp->scope(), new_parent); |
- temp->set_scope(new_parent); |
- new_parent->AddTemporary(temp); |
+ // TODO(verwaest): This currently only moves do-expression declared variables |
+ // in default arguments that weren't already previously declared with the same |
+ // name in the closure-scope. See |
+ // test/mjsunit/harmony/default-parameter-do-expression.js. |
+ DeclarationScope* outer_closure = outer_scope_->GetClosureScope(); |
+ for (int i = top_local_; i < outer_closure->locals_.length(); i++) { |
+ Variable* local = outer_closure->locals_.at(i); |
+ DCHECK(local->mode() == TEMPORARY || local->mode() == VAR); |
+ DCHECK_EQ(local->scope(), local->scope()->GetClosureScope()); |
+ DCHECK_NE(local->scope(), new_parent); |
+ local->set_scope(new_parent); |
+ new_parent->AddLocal(local); |
+ if (local->mode() == VAR) { |
+ outer_closure->variables_.Remove(local); |
+ new_parent->variables_.Add(new_parent->zone(), local); |
} |
- temps->Rewind(top_temp_); |
} |
+ outer_closure->locals_.Rewind(top_local_); |
+ outer_closure->decls_.Rewind(top_decl_); |
} |
void Scope::ReplaceOuterScope(Scope* outer) { |
@@ -748,7 +768,7 @@ Variable* Scope::NewTemporary(const AstRawString* name) { |
TEMPORARY, |
Variable::NORMAL, |
kCreatedInitialized); |
- scope->AddTemporary(var); |
+ scope->AddLocal(var); |
adamk
2016/08/25 17:37:10
This means that it's now possible to close over te
adamk
2016/08/25 17:40:12
Okay, I was confused, these still aren't lookup-ab
|
return var; |
} |
@@ -810,31 +830,13 @@ Declaration* Scope::CheckLexDeclarationsConflictingWith( |
void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, |
ZoneList<Variable*>* context_locals, |
ZoneList<Variable*>* context_globals) { |
- DCHECK(stack_locals != NULL); |
- DCHECK(context_locals != NULL); |
- DCHECK(context_globals != NULL); |
+ // TODO(verwaest): Just pass out locals_ directly and walk it? |
+ DCHECK_NOT_NULL(stack_locals); |
+ DCHECK_NOT_NULL(context_locals); |
+ DCHECK_NOT_NULL(context_globals); |
- // Collect temporaries which are always allocated on the stack, unless the |
- // context as a whole has forced context allocation. |
- if (is_declaration_scope()) { |
- ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); |
- for (int i = 0; i < temps->length(); i++) { |
- Variable* var = (*temps)[i]; |
- if (var->is_used()) { |
- if (var->IsContextSlot()) { |
- DCHECK(has_forced_context_allocation()); |
- context_locals->Add(var, zone()); |
- } else if (var->IsStackLocal()) { |
- stack_locals->Add(var, zone()); |
- } else { |
- DCHECK(var->IsParameter()); |
- } |
- } |
- } |
- } |
- |
- for (int i = 0; i < ordered_variables_.length(); i++) { |
- Variable* var = ordered_variables_[i]; |
+ for (int i = 0; i < locals_.length(); i++) { |
+ Variable* var = locals_[i]; |
if (var->IsStackLocal()) { |
stack_locals->Add(var, zone()); |
} else if (var->IsContextSlot()) { |
@@ -1161,18 +1163,6 @@ void Scope::Print(int n) { |
PrintVar(n1, function); |
} |
- if (is_declaration_scope()) { |
- bool printed_header = false; |
- ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); |
- for (int i = 0; i < temps->length(); i++) { |
- if (!printed_header) { |
- printed_header = true; |
- Indent(n1, "// temporary vars:\n"); |
- } |
- PrintVar(n1, (*temps)[i]); |
- } |
- } |
- |
if (variables_.Start() != NULL) { |
Indent(n1, "// local vars:\n"); |
PrintMap(n1, &variables_, true); |
@@ -1554,21 +1544,13 @@ void Scope::AllocateDeclaredGlobal(Variable* var) { |
} |
void Scope::AllocateNonParameterLocalsAndDeclaredGlobals() { |
- // All variables that have no rewrite yet are non-parameter locals. |
- if (is_declaration_scope()) { |
- ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); |
- for (int i = 0; i < temps->length(); i++) { |
- AllocateNonParameterLocal((*temps)[i]); |
- } |
- } |
- |
- for (int i = 0; i < ordered_variables_.length(); i++) { |
- AllocateNonParameterLocal(ordered_variables_[i]); |
+ for (int i = 0; i < locals_.length(); i++) { |
+ AllocateNonParameterLocal(locals_[i]); |
} |
if (FLAG_global_var_shortcuts) { |
- for (int i = 0; i < ordered_variables_.length(); i++) { |
- AllocateDeclaredGlobal(ordered_variables_[i]); |
+ for (int i = 0; i < locals_.length(); i++) { |
+ AllocateDeclaredGlobal(locals_[i]); |
} |
} |