| 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/ast/scopes.h" | 5 #include "src/ast/scopes.h" |
| 6 | 6 |
| 7 #include "src/accessors.h" | 7 #include "src/accessors.h" |
| 8 #include "src/ast/scopeinfo.h" | 8 #include "src/ast/scopeinfo.h" |
| 9 #include "src/bootstrapper.h" | 9 #include "src/bootstrapper.h" |
| 10 #include "src/messages.h" | 10 #include "src/messages.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 delegates->push_back(stmt); | 78 delegates->push_back(stmt); |
| 79 } | 79 } |
| 80 | 80 |
| 81 | 81 |
| 82 // ---------------------------------------------------------------------------- | 82 // ---------------------------------------------------------------------------- |
| 83 // Implementation of Scope | 83 // Implementation of Scope |
| 84 | 84 |
| 85 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, | 85 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, |
| 86 FunctionKind function_kind) | 86 FunctionKind function_kind) |
| 87 : outer_scope_(outer_scope), | 87 : outer_scope_(outer_scope), |
| 88 scope_type_(scope_type), | |
| 89 function_kind_(function_kind), | |
| 90 variables_(zone), | 88 variables_(zone), |
| 91 temps_(4, zone), | 89 temps_(4, zone), |
| 92 params_(4, zone), | 90 params_(4, zone), |
| 93 decls_(4, zone), | 91 decls_(4, zone), |
| 94 module_descriptor_(scope_type == MODULE_SCOPE ? new (zone) | 92 module_descriptor_(scope_type == MODULE_SCOPE ? new (zone) |
| 95 ModuleDescriptor(zone) | 93 ModuleDescriptor(zone) |
| 96 : NULL), | 94 : NULL), |
| 97 sloppy_block_function_map_(zone), | 95 sloppy_block_function_map_(zone), |
| 96 scope_type_(scope_type), |
| 97 function_kind_(function_kind), |
| 98 already_resolved_(false) { | 98 already_resolved_(false) { |
| 99 SetDefaults(); | 99 SetDefaults(); |
| 100 if (outer_scope == nullptr) { | 100 if (outer_scope == nullptr) { |
| 101 // If the outer scope is null, this cannot be a with scope. The outermost | 101 // If the outer scope is null, this cannot be a with scope. The outermost |
| 102 // scope must be a script scope. | 102 // scope must be a script scope. |
| 103 DCHECK_EQ(SCRIPT_SCOPE, scope_type); | 103 DCHECK_EQ(SCRIPT_SCOPE, scope_type); |
| 104 } else { | 104 } else { |
| 105 asm_function_ = outer_scope_->asm_module_; | 105 asm_function_ = outer_scope_->asm_module_; |
| 106 // Inherit the language mode from the parent scope unless we're a module | 106 // Inherit the language mode from the parent scope unless we're a module |
| 107 // scope. | 107 // scope. |
| 108 if (!is_module_scope()) language_mode_ = outer_scope->language_mode_; | 108 if (!is_module_scope()) language_mode_ = outer_scope->language_mode_; |
| 109 force_context_allocation_ = | 109 force_context_allocation_ = |
| 110 !is_function_scope() && outer_scope->has_forced_context_allocation(); | 110 !is_function_scope() && outer_scope->has_forced_context_allocation(); |
| 111 outer_scope_->AddInnerScope(this); | 111 outer_scope_->AddInnerScope(this); |
| 112 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); | 112 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 | 115 |
| 116 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type, | 116 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type, |
| 117 Handle<ScopeInfo> scope_info) | 117 Handle<ScopeInfo> scope_info) |
| 118 : outer_scope_(nullptr), | 118 : outer_scope_(nullptr), |
| 119 scope_type_(scope_type), | |
| 120 function_kind_(scope_info.is_null() ? kNormalFunction | |
| 121 : scope_info->function_kind()), | |
| 122 variables_(zone), | 119 variables_(zone), |
| 123 temps_(4, zone), | 120 temps_(4, zone), |
| 124 params_(4, zone), | 121 params_(4, zone), |
| 125 decls_(4, zone), | 122 decls_(4, zone), |
| 126 module_descriptor_(nullptr), | 123 module_descriptor_(nullptr), |
| 127 sloppy_block_function_map_(zone), | 124 sloppy_block_function_map_(zone), |
| 125 scope_type_(scope_type), |
| 126 function_kind_(scope_info.is_null() ? kNormalFunction |
| 127 : scope_info->function_kind()), |
| 128 already_resolved_(true), | 128 already_resolved_(true), |
| 129 scope_info_(scope_info) { | 129 scope_info_(scope_info) { |
| 130 SetDefaults(); | 130 SetDefaults(); |
| 131 if (!scope_info.is_null()) { | 131 if (!scope_info.is_null()) { |
| 132 scope_calls_eval_ = scope_info->CallsEval(); | 132 scope_calls_eval_ = scope_info->CallsEval(); |
| 133 language_mode_ = scope_info->language_mode(); | 133 language_mode_ = scope_info->language_mode(); |
| 134 is_declaration_scope_ = scope_info->is_declaration_scope(); | 134 is_declaration_scope_ = scope_info->is_declaration_scope(); |
| 135 num_heap_slots_ = scope_info_->ContextLength(); | 135 num_heap_slots_ = scope_info_->ContextLength(); |
| 136 } | 136 } |
| 137 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. | 137 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. |
| 138 num_heap_slots_ = Max(num_heap_slots_, | 138 num_heap_slots_ = Max(num_heap_slots_, |
| 139 static_cast<int>(Context::MIN_CONTEXT_SLOTS)); | 139 static_cast<int>(Context::MIN_CONTEXT_SLOTS)); |
| 140 AddInnerScope(inner_scope); | 140 AddInnerScope(inner_scope); |
| 141 } | 141 } |
| 142 | 142 |
| 143 Scope::Scope(Zone* zone, Scope* inner_scope, | 143 Scope::Scope(Zone* zone, Scope* inner_scope, |
| 144 const AstRawString* catch_variable_name) | 144 const AstRawString* catch_variable_name) |
| 145 : outer_scope_(nullptr), | 145 : outer_scope_(nullptr), |
| 146 scope_type_(CATCH_SCOPE), | |
| 147 function_kind_(kNormalFunction), | |
| 148 variables_(zone), | 146 variables_(zone), |
| 149 temps_(0, zone), | 147 temps_(0, zone), |
| 150 params_(0, zone), | 148 params_(0, zone), |
| 151 decls_(0, zone), | 149 decls_(0, zone), |
| 152 module_descriptor_(nullptr), | 150 module_descriptor_(nullptr), |
| 153 sloppy_block_function_map_(zone), | 151 sloppy_block_function_map_(zone), |
| 152 scope_type_(CATCH_SCOPE), |
| 153 function_kind_(kNormalFunction), |
| 154 already_resolved_(true) { | 154 already_resolved_(true) { |
| 155 SetDefaults(); | 155 SetDefaults(); |
| 156 AddInnerScope(inner_scope); | 156 AddInnerScope(inner_scope); |
| 157 ++num_var_; | |
| 158 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; | 157 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
| 159 Variable* variable = variables_.Declare(this, | 158 Variable* variable = variables_.Declare(this, |
| 160 catch_variable_name, | 159 catch_variable_name, |
| 161 VAR, | 160 VAR, |
| 162 Variable::NORMAL, | 161 Variable::NORMAL, |
| 163 kCreatedInitialized); | 162 kCreatedInitialized); |
| 164 AllocateHeapSlot(variable); | 163 AllocateHeapSlot(variable); |
| 165 } | 164 } |
| 166 | 165 |
| 167 void Scope::SetDefaults() { | 166 void Scope::SetDefaults() { |
| 168 is_declaration_scope_ = | 167 is_declaration_scope_ = |
| 169 is_eval_scope() || is_function_scope() || | 168 is_eval_scope() || is_function_scope() || |
| 170 is_module_scope() || is_script_scope(); | 169 is_module_scope() || is_script_scope(); |
| 171 inner_scope_ = nullptr; | 170 inner_scope_ = nullptr; |
| 172 sibling_ = nullptr; | 171 sibling_ = nullptr; |
| 173 unresolved_ = nullptr; | 172 unresolved_ = nullptr; |
| 173 #ifdef DEBUG |
| 174 scope_name_ = nullptr; | 174 scope_name_ = nullptr; |
| 175 #endif |
| 175 dynamics_ = nullptr; | 176 dynamics_ = nullptr; |
| 176 receiver_ = nullptr; | 177 receiver_ = nullptr; |
| 177 new_target_ = nullptr; | 178 new_target_ = nullptr; |
| 178 function_ = nullptr; | 179 function_ = nullptr; |
| 179 arguments_ = nullptr; | 180 arguments_ = nullptr; |
| 180 this_function_ = nullptr; | 181 this_function_ = nullptr; |
| 181 scope_inside_with_ = false; | 182 scope_inside_with_ = false; |
| 182 scope_calls_eval_ = false; | 183 scope_calls_eval_ = false; |
| 183 has_arguments_parameter_ = false; | 184 has_arguments_parameter_ = false; |
| 184 scope_uses_super_property_ = false; | 185 scope_uses_super_property_ = false; |
| 185 asm_module_ = false; | 186 asm_module_ = false; |
| 186 asm_function_ = false; | 187 asm_function_ = false; |
| 187 language_mode_ = is_module_scope() ? STRICT : SLOPPY; | 188 language_mode_ = is_module_scope() ? STRICT : SLOPPY; |
| 188 outer_scope_calls_sloppy_eval_ = false; | 189 outer_scope_calls_sloppy_eval_ = false; |
| 189 inner_scope_calls_eval_ = false; | 190 inner_scope_calls_eval_ = false; |
| 190 scope_nonlinear_ = false; | 191 scope_nonlinear_ = false; |
| 191 force_eager_compilation_ = false; | 192 force_eager_compilation_ = false; |
| 192 force_context_allocation_ = false; | 193 force_context_allocation_ = false; |
| 193 num_var_ = 0; | |
| 194 num_stack_slots_ = 0; | 194 num_stack_slots_ = 0; |
| 195 num_heap_slots_ = 0; | 195 num_heap_slots_ = 0; |
| 196 num_global_slots_ = 0; | 196 num_global_slots_ = 0; |
| 197 arity_ = 0; | 197 arity_ = 0; |
| 198 has_simple_parameters_ = true; | 198 has_simple_parameters_ = true; |
| 199 rest_parameter_ = NULL; | 199 rest_parameter_ = NULL; |
| 200 rest_index_ = -1; | 200 rest_index_ = -1; |
| 201 start_position_ = kNoSourcePosition; | 201 start_position_ = kNoSourcePosition; |
| 202 end_position_ = kNoSourcePosition; | 202 end_position_ = kNoSourcePosition; |
| 203 is_hidden_ = false; | 203 is_hidden_ = false; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 CONST, Variable::NORMAL, kCreatedInitialized); | 321 CONST, Variable::NORMAL, kCreatedInitialized); |
| 322 } | 322 } |
| 323 } | 323 } |
| 324 | 324 |
| 325 | 325 |
| 326 Scope* Scope::FinalizeBlockScope() { | 326 Scope* Scope::FinalizeBlockScope() { |
| 327 DCHECK(is_block_scope()); | 327 DCHECK(is_block_scope()); |
| 328 DCHECK(temps_.is_empty()); | 328 DCHECK(temps_.is_empty()); |
| 329 DCHECK(params_.is_empty()); | 329 DCHECK(params_.is_empty()); |
| 330 | 330 |
| 331 if (num_var() > 0 || (is_declaration_scope() && calls_sloppy_eval())) { | 331 if (variables_.occupancy() > 0 || |
| 332 (is_declaration_scope() && calls_sloppy_eval())) { |
| 332 return this; | 333 return this; |
| 333 } | 334 } |
| 334 | 335 |
| 335 // Remove this scope from outer scope. | 336 // Remove this scope from outer scope. |
| 336 outer_scope()->RemoveInnerScope(this); | 337 outer_scope()->RemoveInnerScope(this); |
| 337 | 338 |
| 338 // Reparent inner scopes. | 339 // Reparent inner scopes. |
| 339 if (inner_scope_ != nullptr) { | 340 if (inner_scope_ != nullptr) { |
| 340 Scope* scope = inner_scope_; | 341 Scope* scope = inner_scope_; |
| 341 scope->outer_scope_ = outer_scope(); | 342 scope->outer_scope_ = outer_scope(); |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 } | 557 } |
| 557 | 558 |
| 558 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, | 559 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, |
| 559 InitializationFlag init_flag, Variable::Kind kind, | 560 InitializationFlag init_flag, Variable::Kind kind, |
| 560 MaybeAssignedFlag maybe_assigned_flag) { | 561 MaybeAssignedFlag maybe_assigned_flag) { |
| 561 DCHECK(!already_resolved()); | 562 DCHECK(!already_resolved()); |
| 562 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are | 563 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are |
| 563 // introduced during variable allocation, and TEMPORARY variables are | 564 // introduced during variable allocation, and TEMPORARY variables are |
| 564 // allocated via NewTemporary(). | 565 // allocated via NewTemporary(). |
| 565 DCHECK(IsDeclaredVariableMode(mode)); | 566 DCHECK(IsDeclaredVariableMode(mode)); |
| 566 ++num_var_; | |
| 567 return variables_.Declare(this, name, mode, kind, init_flag, | 567 return variables_.Declare(this, name, mode, kind, init_flag, |
| 568 maybe_assigned_flag); | 568 maybe_assigned_flag); |
| 569 } | 569 } |
| 570 | 570 |
| 571 | 571 |
| 572 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { | 572 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { |
| 573 DCHECK(is_script_scope()); | 573 DCHECK(is_script_scope()); |
| 574 return variables_.Declare(this, | 574 return variables_.Declare(this, |
| 575 name, | 575 name, |
| 576 DYNAMIC_GLOBAL, | 576 DYNAMIC_GLOBAL, |
| (...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1577 function_ != NULL && function_->proxy()->var()->IsContextSlot(); | 1577 function_ != NULL && function_->proxy()->var()->IsContextSlot(); |
| 1578 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - | 1578 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - |
| 1579 (is_function_var_in_context ? 1 : 0); | 1579 (is_function_var_in_context ? 1 : 0); |
| 1580 } | 1580 } |
| 1581 | 1581 |
| 1582 | 1582 |
| 1583 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1583 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
| 1584 | 1584 |
| 1585 } // namespace internal | 1585 } // namespace internal |
| 1586 } // namespace v8 | 1586 } // namespace v8 |
| OLD | NEW |