OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/accessors.h" | 7 #include "src/accessors.h" |
8 #include "src/bootstrapper.h" | 8 #include "src/bootstrapper.h" |
9 #include "src/messages.h" | 9 #include "src/messages.h" |
10 #include "src/parser.h" | 10 #include "src/parser.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 // ---------------------------------------------------------------------------- | 70 // ---------------------------------------------------------------------------- |
71 // Implementation of Scope | 71 // Implementation of Scope |
72 | 72 |
73 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, | 73 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, |
74 AstValueFactory* ast_value_factory, FunctionKind function_kind) | 74 AstValueFactory* ast_value_factory, FunctionKind function_kind) |
75 : inner_scopes_(4, zone), | 75 : inner_scopes_(4, zone), |
76 variables_(zone), | 76 variables_(zone), |
77 internals_(4, zone), | 77 internals_(4, zone), |
78 temps_(4, zone), | 78 temps_(4, zone), |
79 params_(4, zone), | 79 params_(4, zone), |
80 param_positions_(4, zone), | |
80 unresolved_(16, zone), | 81 unresolved_(16, zone), |
81 decls_(4, zone), | 82 decls_(4, zone), |
82 module_descriptor_( | 83 module_descriptor_( |
83 scope_type == MODULE_SCOPE ? ModuleDescriptor::New(zone) : NULL), | 84 scope_type == MODULE_SCOPE ? ModuleDescriptor::New(zone) : NULL), |
84 already_resolved_(false), | 85 already_resolved_(false), |
85 ast_value_factory_(ast_value_factory), | 86 ast_value_factory_(ast_value_factory), |
86 zone_(zone), | 87 zone_(zone), |
87 class_declaration_group_start_(-1) { | 88 class_declaration_group_start_(-1) { |
88 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null(), | 89 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null(), |
89 function_kind); | 90 function_kind); |
90 // The outermost scope must be a script scope. | 91 // The outermost scope must be a script scope. |
91 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL); | 92 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL); |
92 DCHECK(!HasIllegalRedeclaration()); | 93 DCHECK(!HasIllegalRedeclaration()); |
93 } | 94 } |
94 | 95 |
95 | 96 |
96 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type, | 97 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type, |
97 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory) | 98 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory) |
98 : inner_scopes_(4, zone), | 99 : inner_scopes_(4, zone), |
99 variables_(zone), | 100 variables_(zone), |
100 internals_(4, zone), | 101 internals_(4, zone), |
101 temps_(4, zone), | 102 temps_(4, zone), |
102 params_(4, zone), | 103 params_(4, zone), |
104 param_positions_(4, zone), | |
103 unresolved_(16, zone), | 105 unresolved_(16, zone), |
104 decls_(4, zone), | 106 decls_(4, zone), |
105 module_descriptor_(NULL), | 107 module_descriptor_(NULL), |
106 already_resolved_(true), | 108 already_resolved_(true), |
107 ast_value_factory_(value_factory), | 109 ast_value_factory_(value_factory), |
108 zone_(zone), | 110 zone_(zone), |
109 class_declaration_group_start_(-1) { | 111 class_declaration_group_start_(-1) { |
110 SetDefaults(scope_type, NULL, scope_info); | 112 SetDefaults(scope_type, NULL, scope_info); |
111 if (!scope_info.is_null()) { | 113 if (!scope_info.is_null()) { |
112 num_heap_slots_ = scope_info_->ContextLength(); | 114 num_heap_slots_ = scope_info_->ContextLength(); |
113 } | 115 } |
114 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. | 116 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. |
115 num_heap_slots_ = Max(num_heap_slots_, | 117 num_heap_slots_ = Max(num_heap_slots_, |
116 static_cast<int>(Context::MIN_CONTEXT_SLOTS)); | 118 static_cast<int>(Context::MIN_CONTEXT_SLOTS)); |
117 AddInnerScope(inner_scope); | 119 AddInnerScope(inner_scope); |
118 } | 120 } |
119 | 121 |
120 | 122 |
121 Scope::Scope(Zone* zone, Scope* inner_scope, | 123 Scope::Scope(Zone* zone, Scope* inner_scope, |
122 const AstRawString* catch_variable_name, | 124 const AstRawString* catch_variable_name, |
123 AstValueFactory* value_factory) | 125 AstValueFactory* value_factory) |
124 : inner_scopes_(1, zone), | 126 : inner_scopes_(1, zone), |
125 variables_(zone), | 127 variables_(zone), |
126 internals_(0, zone), | 128 internals_(0, zone), |
127 temps_(0, zone), | 129 temps_(0, zone), |
128 params_(0, zone), | 130 params_(0, zone), |
131 param_positions_(0, zone), | |
129 unresolved_(0, zone), | 132 unresolved_(0, zone), |
130 decls_(0, zone), | 133 decls_(0, zone), |
131 module_descriptor_(NULL), | 134 module_descriptor_(NULL), |
132 already_resolved_(true), | 135 already_resolved_(true), |
133 ast_value_factory_(value_factory), | 136 ast_value_factory_(value_factory), |
134 zone_(zone), | 137 zone_(zone), |
135 class_declaration_group_start_(-1) { | 138 class_declaration_group_start_(-1) { |
136 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); | 139 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); |
137 AddInnerScope(inner_scope); | 140 AddInnerScope(inner_scope); |
138 ++num_var_or_const_; | 141 ++num_var_or_const_; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 force_eager_compilation_ = false; | 179 force_eager_compilation_ = false; |
177 force_context_allocation_ = (outer_scope != NULL && !is_function_scope()) | 180 force_context_allocation_ = (outer_scope != NULL && !is_function_scope()) |
178 ? outer_scope->has_forced_context_allocation() : false; | 181 ? outer_scope->has_forced_context_allocation() : false; |
179 num_var_or_const_ = 0; | 182 num_var_or_const_ = 0; |
180 num_stack_slots_ = 0; | 183 num_stack_slots_ = 0; |
181 num_heap_slots_ = 0; | 184 num_heap_slots_ = 0; |
182 num_modules_ = 0; | 185 num_modules_ = 0; |
183 module_var_ = NULL, | 186 module_var_ = NULL, |
184 rest_parameter_ = NULL; | 187 rest_parameter_ = NULL; |
185 rest_index_ = -1; | 188 rest_index_ = -1; |
189 has_parameter_expressions_ = false; | |
186 scope_info_ = scope_info; | 190 scope_info_ = scope_info; |
187 start_position_ = RelocInfo::kNoPosition; | 191 start_position_ = RelocInfo::kNoPosition; |
188 end_position_ = RelocInfo::kNoPosition; | 192 end_position_ = RelocInfo::kNoPosition; |
189 if (!scope_info.is_null()) { | 193 if (!scope_info.is_null()) { |
190 scope_calls_eval_ = scope_info->CallsEval(); | 194 scope_calls_eval_ = scope_info->CallsEval(); |
191 language_mode_ = scope_info->language_mode(); | 195 language_mode_ = scope_info->language_mode(); |
192 block_scope_is_class_scope_ = scope_info->block_scope_is_class_scope(); | 196 block_scope_is_class_scope_ = scope_info->block_scope_is_class_scope(); |
193 function_kind_ = scope_info->function_kind(); | 197 function_kind_ = scope_info->function_kind(); |
194 } | 198 } |
195 } | 199 } |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
452 scope != NULL; | 456 scope != NULL; |
453 scope = scope->outer_scope()) { | 457 scope = scope->outer_scope()) { |
454 Variable* var = scope->LookupLocal(name); | 458 Variable* var = scope->LookupLocal(name); |
455 if (var != NULL) return var; | 459 if (var != NULL) return var; |
456 } | 460 } |
457 return NULL; | 461 return NULL; |
458 } | 462 } |
459 | 463 |
460 | 464 |
461 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, | 465 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, |
462 bool is_rest, bool* is_duplicate) { | 466 bool is_rest, bool* is_duplicate, int pos) { |
463 DCHECK(!already_resolved()); | 467 DCHECK(!already_resolved()); |
464 DCHECK(is_function_scope()); | 468 DCHECK(is_function_scope()); |
465 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, | 469 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, |
466 kCreatedInitialized); | 470 kCreatedInitialized); |
467 if (is_rest) { | 471 if (is_rest) { |
468 DCHECK_NULL(rest_parameter_); | 472 DCHECK_NULL(rest_parameter_); |
469 rest_parameter_ = var; | 473 rest_parameter_ = var; |
470 rest_index_ = num_parameters(); | 474 rest_index_ = num_parameters(); |
471 } | 475 } |
472 // TODO(wingo): Avoid O(n^2) check. | 476 // TODO(wingo): Avoid O(n^2) check. |
473 *is_duplicate = IsDeclaredParameter(name); | 477 *is_duplicate = IsDeclaredParameter(name); |
474 params_.Add(var, zone()); | 478 params_.Add(var, zone()); |
479 param_positions_.Add(pos, zone()); | |
475 return var; | 480 return var; |
476 } | 481 } |
477 | 482 |
478 | 483 |
479 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, | 484 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, |
480 InitializationFlag init_flag, Variable::Kind kind, | 485 InitializationFlag init_flag, Variable::Kind kind, |
481 MaybeAssignedFlag maybe_assigned_flag, | 486 MaybeAssignedFlag maybe_assigned_flag, |
482 int declaration_group_start) { | 487 int declaration_group_start) { |
483 DCHECK(!already_resolved()); | 488 DCHECK(!already_resolved()); |
484 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are | 489 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
535 temps_.Add(var, zone()); | 540 temps_.Add(var, zone()); |
536 return var; | 541 return var; |
537 } | 542 } |
538 | 543 |
539 | 544 |
540 void Scope::AddDeclaration(Declaration* declaration) { | 545 void Scope::AddDeclaration(Declaration* declaration) { |
541 decls_.Add(declaration, zone()); | 546 decls_.Add(declaration, zone()); |
542 } | 547 } |
543 | 548 |
544 | 549 |
550 void Scope::UndeclareParametersForExpressions() { | |
551 DCHECK(is_function_scope()); | |
552 DCHECK(!has_parameter_expressions_); | |
553 has_parameter_expressions_ = true; | |
554 for (int i = 0; i < num_parameters(); ++i) { | |
555 Variable* p = parameter(i); | |
556 const AstRawString* name = p->raw_name(); | |
557 variables_.Remove(const_cast<AstRawString*>(name), name->hash()); | |
558 } | |
559 } | |
560 | |
561 | |
545 void Scope::SetIllegalRedeclaration(Expression* expression) { | 562 void Scope::SetIllegalRedeclaration(Expression* expression) { |
546 // Record only the first illegal redeclaration. | 563 // Record only the first illegal redeclaration. |
547 if (!HasIllegalRedeclaration()) { | 564 if (!HasIllegalRedeclaration()) { |
548 illegal_redecl_ = expression; | 565 illegal_redecl_ = expression; |
549 } | 566 } |
550 DCHECK(HasIllegalRedeclaration()); | 567 DCHECK(HasIllegalRedeclaration()); |
551 } | 568 } |
552 | 569 |
553 | 570 |
554 void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) { | 571 void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) { |
(...skipping 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1412 var->ForceContextAllocation(); | 1429 var->ForceContextAllocation(); |
1413 } | 1430 } |
1414 AllocateParameter(var, i); | 1431 AllocateParameter(var, i); |
1415 } | 1432 } |
1416 } | 1433 } |
1417 | 1434 |
1418 | 1435 |
1419 void Scope::AllocateParameter(Variable* var, int index) { | 1436 void Scope::AllocateParameter(Variable* var, int index) { |
1420 if (MustAllocate(var)) { | 1437 if (MustAllocate(var)) { |
1421 if (MustAllocateInContext(var)) { | 1438 if (MustAllocateInContext(var)) { |
1439 if (has_parameter_expressions_) { | |
1440 // If hasParameterExpressions is true, context-allocate the redeclared | |
1441 // lexical variable, not the function parameter. | |
1442 var = variables_.Lookup(var->raw_name()); | |
caitp (gmail)
2015/05/26 17:14:07
This is a hack to make sure the re-declared variab
adamk
2015/05/26 20:03:20
Can we just skip the loop over params_ in Allocate
caitp (gmail)
2015/05/26 20:24:44
Maybe, but there are a few things that might go wr
caitp (gmail)
2015/05/26 20:28:40
Actually, it passes tests at the moment, so I gues
| |
1443 } | |
1422 DCHECK(var->IsUnallocated() || var->IsContextSlot()); | 1444 DCHECK(var->IsUnallocated() || var->IsContextSlot()); |
1423 if (var->IsUnallocated()) { | 1445 if (var->IsUnallocated()) { |
1424 AllocateHeapSlot(var); | 1446 AllocateHeapSlot(var); |
1425 } | 1447 } |
1426 } else { | 1448 } else { |
1427 DCHECK(var->IsUnallocated() || var->IsParameter()); | 1449 DCHECK(var->IsUnallocated() || var->IsParameter()); |
1428 if (var->IsUnallocated()) { | 1450 if (var->IsUnallocated()) { |
1429 var->AllocateTo(Variable::PARAMETER, index); | 1451 var->AllocateTo(Variable::PARAMETER, index); |
1430 } | 1452 } |
1431 } | 1453 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1556 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); | 1578 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); |
1557 } | 1579 } |
1558 | 1580 |
1559 | 1581 |
1560 int Scope::ContextLocalCount() const { | 1582 int Scope::ContextLocalCount() const { |
1561 if (num_heap_slots() == 0) return 0; | 1583 if (num_heap_slots() == 0) return 0; |
1562 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1584 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1563 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1585 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
1564 } | 1586 } |
1565 } } // namespace v8::internal | 1587 } } // namespace v8::internal |
OLD | NEW |