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 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 scope != NULL; | 467 scope != NULL; |
464 scope = scope->outer_scope()) { | 468 scope = scope->outer_scope()) { |
465 Variable* var = scope->LookupLocal(name); | 469 Variable* var = scope->LookupLocal(name); |
466 if (var != NULL) return var; | 470 if (var != NULL) return var; |
467 } | 471 } |
468 return NULL; | 472 return NULL; |
469 } | 473 } |
470 | 474 |
471 | 475 |
472 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, | 476 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, |
473 bool is_rest, bool* is_duplicate) { | 477 bool is_rest, bool* is_duplicate, int pos) { |
474 DCHECK(!already_resolved()); | 478 DCHECK(!already_resolved()); |
475 DCHECK(is_function_scope()); | 479 DCHECK(is_function_scope()); |
476 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, | 480 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, |
477 kCreatedInitialized); | 481 kCreatedInitialized); |
478 if (is_rest) { | 482 if (is_rest) { |
479 DCHECK_NULL(rest_parameter_); | 483 DCHECK_NULL(rest_parameter_); |
480 rest_parameter_ = var; | 484 rest_parameter_ = var; |
481 rest_index_ = num_parameters(); | 485 rest_index_ = num_parameters(); |
482 } | 486 } |
483 // TODO(wingo): Avoid O(n^2) check. | 487 // TODO(wingo): Avoid O(n^2) check. |
484 *is_duplicate = IsDeclaredParameter(name); | 488 *is_duplicate = IsDeclaredParameter(name); |
485 params_.Add(var, zone()); | 489 params_.Add(var, zone()); |
| 490 param_positions_.Add(pos, zone()); |
486 return var; | 491 return var; |
487 } | 492 } |
488 | 493 |
489 | 494 |
490 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, | 495 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, |
491 InitializationFlag init_flag, Variable::Kind kind, | 496 InitializationFlag init_flag, Variable::Kind kind, |
492 MaybeAssignedFlag maybe_assigned_flag, | 497 MaybeAssignedFlag maybe_assigned_flag, |
493 int declaration_group_start) { | 498 int declaration_group_start) { |
494 DCHECK(!already_resolved()); | 499 DCHECK(!already_resolved()); |
495 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are | 500 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 temps_.Add(var, zone()); | 551 temps_.Add(var, zone()); |
547 return var; | 552 return var; |
548 } | 553 } |
549 | 554 |
550 | 555 |
551 void Scope::AddDeclaration(Declaration* declaration) { | 556 void Scope::AddDeclaration(Declaration* declaration) { |
552 decls_.Add(declaration, zone()); | 557 decls_.Add(declaration, zone()); |
553 } | 558 } |
554 | 559 |
555 | 560 |
| 561 void Scope::UndeclareParametersForExpressions() { |
| 562 DCHECK(is_function_scope()); |
| 563 DCHECK(!has_parameter_expressions_); |
| 564 has_parameter_expressions_ = true; |
| 565 for (int i = 0; i < num_parameters(); ++i) { |
| 566 Variable* p = parameter(i); |
| 567 const AstRawString* name = p->raw_name(); |
| 568 variables_.Remove(const_cast<AstRawString*>(name), name->hash()); |
| 569 } |
| 570 } |
| 571 |
| 572 |
556 void Scope::SetIllegalRedeclaration(Expression* expression) { | 573 void Scope::SetIllegalRedeclaration(Expression* expression) { |
557 // Record only the first illegal redeclaration. | 574 // Record only the first illegal redeclaration. |
558 if (!HasIllegalRedeclaration()) { | 575 if (!HasIllegalRedeclaration()) { |
559 illegal_redecl_ = expression; | 576 illegal_redecl_ = expression; |
560 } | 577 } |
561 DCHECK(HasIllegalRedeclaration()); | 578 DCHECK(HasIllegalRedeclaration()); |
562 } | 579 } |
563 | 580 |
564 | 581 |
565 void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) { | 582 void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) { |
(...skipping 846 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1412 // home_object_var. | 1429 // home_object_var. |
1413 // Consider splitting the AST node into 2 different nodes since the | 1430 // Consider splitting the AST node into 2 different nodes since the |
1414 // semantics is just so different. | 1431 // semantics is just so different. |
1415 home_object_ = home_object_var; | 1432 home_object_ = home_object_var; |
1416 } | 1433 } |
1417 | 1434 |
1418 // The same parameter may occur multiple times in the parameters_ list. | 1435 // The same parameter may occur multiple times in the parameters_ list. |
1419 // If it does, and if it is not copied into the context object, it must | 1436 // If it does, and if it is not copied into the context object, it must |
1420 // receive the highest parameter index for that parameter; thus iteration | 1437 // receive the highest parameter index for that parameter; thus iteration |
1421 // order is relevant! | 1438 // order is relevant! |
1422 for (int i = params_.length() - 1; i >= 0; --i) { | 1439 // |
1423 Variable* var = params_[i]; | 1440 // If hasParameterExpressions is true, parameters are redeclared during |
1424 if (var == rest_parameter_) continue; | 1441 // desugaring, and must not be allocated here. |
| 1442 if (!has_parameter_expressions_) { |
| 1443 for (int i = params_.length() - 1; i >= 0; --i) { |
| 1444 Variable* var = params_[i]; |
| 1445 if (var == rest_parameter_) continue; |
1425 | 1446 |
1426 DCHECK(var->scope() == this); | 1447 DCHECK(var->scope() == this); |
1427 if (uses_sloppy_arguments || has_forced_context_allocation()) { | 1448 if (uses_sloppy_arguments || has_forced_context_allocation()) { |
1428 // Force context allocation of the parameter. | 1449 // Force context allocation of the parameter. |
1429 var->ForceContextAllocation(); | 1450 var->ForceContextAllocation(); |
| 1451 } |
| 1452 AllocateParameter(var, i); |
1430 } | 1453 } |
1431 AllocateParameter(var, i); | |
1432 } | 1454 } |
1433 } | 1455 } |
1434 | 1456 |
1435 | 1457 |
1436 void Scope::AllocateParameter(Variable* var, int index) { | 1458 void Scope::AllocateParameter(Variable* var, int index) { |
1437 if (MustAllocate(var)) { | 1459 if (MustAllocate(var)) { |
1438 if (MustAllocateInContext(var)) { | 1460 if (MustAllocateInContext(var)) { |
1439 DCHECK(var->IsUnallocated() || var->IsContextSlot()); | 1461 DCHECK(var->IsUnallocated() || var->IsContextSlot()); |
1440 if (var->IsUnallocated()) { | 1462 if (var->IsUnallocated()) { |
1441 AllocateHeapSlot(var); | 1463 AllocateHeapSlot(var); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1573 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); | 1595 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); |
1574 } | 1596 } |
1575 | 1597 |
1576 | 1598 |
1577 int Scope::ContextLocalCount() const { | 1599 int Scope::ContextLocalCount() const { |
1578 if (num_heap_slots() == 0) return 0; | 1600 if (num_heap_slots() == 0) return 0; |
1579 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1601 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1580 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1602 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
1581 } | 1603 } |
1582 } } // namespace v8::internal | 1604 } } // namespace v8::internal |
OLD | NEW |