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 <set> | 7 #include <set> |
8 | 8 |
9 #include "src/accessors.h" | 9 #include "src/accessors.h" |
10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 outer_scope_(outer_scope), | 80 outer_scope_(outer_scope), |
81 variables_(zone), | 81 variables_(zone), |
82 decls_(4, zone), | 82 decls_(4, zone), |
83 scope_type_(scope_type) { | 83 scope_type_(scope_type) { |
84 SetDefaults(); | 84 SetDefaults(); |
85 if (outer_scope == nullptr) { | 85 if (outer_scope == nullptr) { |
86 // If the outer scope is null, this cannot be a with scope. The outermost | 86 // If the outer scope is null, this cannot be a with scope. The outermost |
87 // scope must be a script scope. | 87 // scope must be a script scope. |
88 DCHECK_EQ(SCRIPT_SCOPE, scope_type); | 88 DCHECK_EQ(SCRIPT_SCOPE, scope_type); |
89 } else { | 89 } else { |
90 asm_function_ = outer_scope_->asm_module_; | |
91 set_language_mode(outer_scope->language_mode()); | 90 set_language_mode(outer_scope->language_mode()); |
92 force_context_allocation_ = | 91 force_context_allocation_ = |
93 !is_function_scope() && outer_scope->has_forced_context_allocation(); | 92 !is_function_scope() && outer_scope->has_forced_context_allocation(); |
94 outer_scope_->AddInnerScope(this); | 93 outer_scope_->AddInnerScope(this); |
95 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); | 94 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); |
96 } | 95 } |
97 } | 96 } |
98 | 97 |
99 Scope::Snapshot::Snapshot(Scope* scope) | 98 Scope::Snapshot::Snapshot(Scope* scope) |
100 : outer_scope_(scope), | 99 : outer_scope_(scope), |
101 top_inner_scope_(scope->inner_scope_), | 100 top_inner_scope_(scope->inner_scope_), |
102 top_unresolved_(scope->unresolved_), | 101 top_unresolved_(scope->unresolved_), |
103 top_temp_(scope->GetClosureScope()->temps()->length()) {} | 102 top_temp_(scope->GetClosureScope()->temps()->length()) {} |
104 | 103 |
105 DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope, | 104 DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope, |
106 ScopeType scope_type, | 105 ScopeType scope_type, |
107 FunctionKind function_kind) | 106 FunctionKind function_kind) |
108 : Scope(zone, outer_scope, scope_type), | 107 : Scope(zone, outer_scope, scope_type), |
109 function_kind_(function_kind), | 108 function_kind_(function_kind), |
110 temps_(4, zone), | 109 temps_(4, zone), |
111 params_(4, zone), | 110 params_(4, zone), |
112 sloppy_block_function_map_(zone) { | 111 sloppy_block_function_map_(zone) { |
113 SetDefaults(); | 112 SetDefaults(); |
| 113 if (outer_scope != nullptr) asm_function_ = outer_scope_->IsAsmModule(); |
114 } | 114 } |
115 | 115 |
116 ModuleScope::ModuleScope(Zone* zone, DeclarationScope* script_scope, | 116 ModuleScope::ModuleScope(Zone* zone, DeclarationScope* script_scope, |
117 AstValueFactory* ast_value_factory) | 117 AstValueFactory* ast_value_factory) |
118 : DeclarationScope(zone, script_scope, MODULE_SCOPE) { | 118 : DeclarationScope(zone, script_scope, MODULE_SCOPE) { |
119 module_descriptor_ = new (zone) ModuleDescriptor(zone); | 119 module_descriptor_ = new (zone) ModuleDescriptor(zone); |
120 set_language_mode(STRICT); | 120 set_language_mode(STRICT); |
121 DeclareThis(ast_value_factory); | 121 DeclareThis(ast_value_factory); |
122 } | 122 } |
123 | 123 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 if (inner_scope != nullptr) AddInnerScope(inner_scope); | 170 if (inner_scope != nullptr) AddInnerScope(inner_scope); |
171 Variable* variable = | 171 Variable* variable = |
172 variables_.Declare(zone, this, catch_variable_name, VAR, Variable::NORMAL, | 172 variables_.Declare(zone, this, catch_variable_name, VAR, Variable::NORMAL, |
173 kCreatedInitialized); | 173 kCreatedInitialized); |
174 AllocateHeapSlot(variable); | 174 AllocateHeapSlot(variable); |
175 } | 175 } |
176 | 176 |
177 void DeclarationScope::SetDefaults() { | 177 void DeclarationScope::SetDefaults() { |
178 is_declaration_scope_ = true; | 178 is_declaration_scope_ = true; |
179 has_simple_parameters_ = true; | 179 has_simple_parameters_ = true; |
| 180 asm_module_ = false; |
| 181 asm_function_ = false; |
180 receiver_ = nullptr; | 182 receiver_ = nullptr; |
181 new_target_ = nullptr; | 183 new_target_ = nullptr; |
182 function_ = nullptr; | 184 function_ = nullptr; |
183 arguments_ = nullptr; | 185 arguments_ = nullptr; |
184 this_function_ = nullptr; | 186 this_function_ = nullptr; |
185 arity_ = 0; | 187 arity_ = 0; |
186 rest_parameter_ = nullptr; | 188 rest_parameter_ = nullptr; |
187 rest_index_ = -1; | 189 rest_index_ = -1; |
188 } | 190 } |
189 | 191 |
(...skipping 13 matching lines...) Expand all Loading... |
203 num_stack_slots_ = 0; | 205 num_stack_slots_ = 0; |
204 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; | 206 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
205 num_global_slots_ = 0; | 207 num_global_slots_ = 0; |
206 | 208 |
207 set_language_mode(SLOPPY); | 209 set_language_mode(SLOPPY); |
208 | 210 |
209 scope_inside_with_ = false; | 211 scope_inside_with_ = false; |
210 scope_calls_eval_ = false; | 212 scope_calls_eval_ = false; |
211 scope_uses_super_property_ = false; | 213 scope_uses_super_property_ = false; |
212 has_arguments_parameter_ = false; | 214 has_arguments_parameter_ = false; |
213 asm_module_ = false; | |
214 asm_function_ = false; | |
215 scope_nonlinear_ = false; | 215 scope_nonlinear_ = false; |
216 is_hidden_ = false; | 216 is_hidden_ = false; |
217 is_debug_evaluate_scope_ = false; | 217 is_debug_evaluate_scope_ = false; |
218 | 218 |
219 outer_scope_calls_sloppy_eval_ = false; | 219 outer_scope_calls_sloppy_eval_ = false; |
220 inner_scope_calls_eval_ = false; | 220 inner_scope_calls_eval_ = false; |
221 force_eager_compilation_ = false; | 221 force_eager_compilation_ = false; |
222 force_context_allocation_ = false; | 222 force_context_allocation_ = false; |
223 | 223 |
224 is_declaration_scope_ = false; | 224 is_declaration_scope_ = false; |
225 } | 225 } |
226 | 226 |
227 bool Scope::HasSimpleParameters() { | 227 bool Scope::HasSimpleParameters() { |
228 DeclarationScope* scope = GetClosureScope(); | 228 DeclarationScope* scope = GetClosureScope(); |
229 return !scope->is_function_scope() || scope->has_simple_parameters(); | 229 return !scope->is_function_scope() || scope->has_simple_parameters(); |
230 } | 230 } |
231 | 231 |
| 232 bool Scope::IsAsmModule() const { |
| 233 return is_function_scope() && AsDeclarationScope()->asm_module(); |
| 234 } |
| 235 |
| 236 bool Scope::IsAsmFunction() const { |
| 237 return is_function_scope() && AsDeclarationScope()->asm_function(); |
| 238 } |
| 239 |
232 Scope* Scope::DeserializeScopeChain(Isolate* isolate, Zone* zone, | 240 Scope* Scope::DeserializeScopeChain(Isolate* isolate, Zone* zone, |
233 Context* context, | 241 Context* context, |
234 DeclarationScope* script_scope, | 242 DeclarationScope* script_scope, |
235 AstValueFactory* ast_value_factory, | 243 AstValueFactory* ast_value_factory, |
236 DeserializationMode deserialization_mode) { | 244 DeserializationMode deserialization_mode) { |
237 // Reconstruct the outer scope chain from a closure's context chain. | 245 // Reconstruct the outer scope chain from a closure's context chain. |
238 Scope* current_scope = nullptr; | 246 Scope* current_scope = nullptr; |
239 Scope* innermost_scope = nullptr; | 247 Scope* innermost_scope = nullptr; |
240 while (!context->IsNativeContext()) { | 248 while (!context->IsNativeContext()) { |
241 if (context->IsWithContext() || context->IsDebugEvaluateContext()) { | 249 if (context->IsWithContext() || context->IsDebugEvaluateContext()) { |
(...skipping 10 matching lines...) Expand all Loading... |
252 for (Scope* s = innermost_scope; s != nullptr; s = s->outer_scope()) { | 260 for (Scope* s = innermost_scope; s != nullptr; s = s->outer_scope()) { |
253 s->scope_inside_with_ = true; | 261 s->scope_inside_with_ = true; |
254 } | 262 } |
255 } else if (context->IsScriptContext()) { | 263 } else if (context->IsScriptContext()) { |
256 Handle<ScopeInfo> scope_info(context->scope_info(), isolate); | 264 Handle<ScopeInfo> scope_info(context->scope_info(), isolate); |
257 current_scope = new (zone) | 265 current_scope = new (zone) |
258 DeclarationScope(zone, current_scope, SCRIPT_SCOPE, scope_info); | 266 DeclarationScope(zone, current_scope, SCRIPT_SCOPE, scope_info); |
259 } else if (context->IsFunctionContext()) { | 267 } else if (context->IsFunctionContext()) { |
260 Handle<ScopeInfo> scope_info(context->closure()->shared()->scope_info(), | 268 Handle<ScopeInfo> scope_info(context->closure()->shared()->scope_info(), |
261 isolate); | 269 isolate); |
262 current_scope = new (zone) | 270 DeclarationScope* function_scope = new (zone) |
263 DeclarationScope(zone, current_scope, FUNCTION_SCOPE, scope_info); | 271 DeclarationScope(zone, current_scope, FUNCTION_SCOPE, scope_info); |
264 if (scope_info->IsAsmFunction()) current_scope->asm_function_ = true; | 272 if (scope_info->IsAsmFunction()) function_scope->set_asm_function(); |
265 if (scope_info->IsAsmModule()) current_scope->asm_module_ = true; | 273 if (scope_info->IsAsmModule()) function_scope->set_asm_module(); |
| 274 current_scope = function_scope; |
266 } else if (context->IsBlockContext()) { | 275 } else if (context->IsBlockContext()) { |
267 Handle<ScopeInfo> scope_info(context->scope_info(), isolate); | 276 Handle<ScopeInfo> scope_info(context->scope_info(), isolate); |
268 if (scope_info->is_declaration_scope()) { | 277 if (scope_info->is_declaration_scope()) { |
269 current_scope = new (zone) | 278 current_scope = new (zone) |
270 DeclarationScope(zone, current_scope, BLOCK_SCOPE, scope_info); | 279 DeclarationScope(zone, current_scope, BLOCK_SCOPE, scope_info); |
271 } else { | 280 } else { |
272 current_scope = | 281 current_scope = |
273 new (zone) Scope(zone, current_scope, BLOCK_SCOPE, scope_info); | 282 new (zone) Scope(zone, current_scope, BLOCK_SCOPE, scope_info); |
274 } | 283 } |
275 } else { | 284 } else { |
(...skipping 877 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1153 PrintF("\n"); | 1162 PrintF("\n"); |
1154 } | 1163 } |
1155 | 1164 |
1156 // Scope info. | 1165 // Scope info. |
1157 if (HasTrivialOuterContext()) { | 1166 if (HasTrivialOuterContext()) { |
1158 Indent(n1, "// scope has trivial outer context\n"); | 1167 Indent(n1, "// scope has trivial outer context\n"); |
1159 } | 1168 } |
1160 if (is_strict(language_mode())) { | 1169 if (is_strict(language_mode())) { |
1161 Indent(n1, "// strict mode scope\n"); | 1170 Indent(n1, "// strict mode scope\n"); |
1162 } | 1171 } |
1163 if (asm_module_) Indent(n1, "// scope is an asm module\n"); | 1172 if (IsAsmModule()) Indent(n1, "// scope is an asm module\n"); |
1164 if (asm_function_) Indent(n1, "// scope is an asm function\n"); | 1173 if (IsAsmFunction()) Indent(n1, "// scope is an asm function\n"); |
1165 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); | 1174 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); |
1166 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); | 1175 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); |
1167 if (scope_uses_super_property_) | 1176 if (scope_uses_super_property_) |
1168 Indent(n1, "// scope uses 'super' property\n"); | 1177 Indent(n1, "// scope uses 'super' property\n"); |
1169 if (outer_scope_calls_sloppy_eval_) { | 1178 if (outer_scope_calls_sloppy_eval_) { |
1170 Indent(n1, "// outer scope calls 'eval' in sloppy context\n"); | 1179 Indent(n1, "// outer scope calls 'eval' in sloppy context\n"); |
1171 } | 1180 } |
1172 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); | 1181 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); |
1173 if (num_stack_slots_ > 0) { | 1182 if (num_stack_slots_ > 0) { |
1174 Indent(n1, "// "); | 1183 Indent(n1, "// "); |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1484 bool calls_sloppy_eval = | 1493 bool calls_sloppy_eval = |
1485 this->calls_sloppy_eval() || outer_scope_calls_sloppy_eval_; | 1494 this->calls_sloppy_eval() || outer_scope_calls_sloppy_eval_; |
1486 for (Scope* inner = inner_scope_; inner != nullptr; inner = inner->sibling_) { | 1495 for (Scope* inner = inner_scope_; inner != nullptr; inner = inner->sibling_) { |
1487 inner->PropagateScopeInfo(calls_sloppy_eval); | 1496 inner->PropagateScopeInfo(calls_sloppy_eval); |
1488 if (inner->scope_calls_eval_ || inner->inner_scope_calls_eval_) { | 1497 if (inner->scope_calls_eval_ || inner->inner_scope_calls_eval_) { |
1489 inner_scope_calls_eval_ = true; | 1498 inner_scope_calls_eval_ = true; |
1490 } | 1499 } |
1491 if (inner->force_eager_compilation_) { | 1500 if (inner->force_eager_compilation_) { |
1492 force_eager_compilation_ = true; | 1501 force_eager_compilation_ = true; |
1493 } | 1502 } |
1494 if (asm_module_ && inner->scope_type() == FUNCTION_SCOPE) { | 1503 if (IsAsmModule() && inner->is_function_scope()) { |
1495 inner->asm_function_ = true; | 1504 inner->AsDeclarationScope()->set_asm_function(); |
1496 } | 1505 } |
1497 } | 1506 } |
1498 } | 1507 } |
1499 | 1508 |
1500 | 1509 |
1501 bool Scope::MustAllocate(Variable* var) { | 1510 bool Scope::MustAllocate(Variable* var) { |
1502 DCHECK(var->location() != VariableLocation::MODULE); | 1511 DCHECK(var->location() != VariableLocation::MODULE); |
1503 // Give var a read/write use if there is a chance it might be accessed | 1512 // Give var a read/write use if there is a chance it might be accessed |
1504 // via an eval() call. This is only possible if the variable has a | 1513 // via an eval() call. This is only possible if the variable has a |
1505 // visible name. | 1514 // visible name. |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1778 function != nullptr && function->IsContextSlot(); | 1787 function != nullptr && function->IsContextSlot(); |
1779 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - | 1788 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - |
1780 (is_function_var_in_context ? 1 : 0); | 1789 (is_function_var_in_context ? 1 : 0); |
1781 } | 1790 } |
1782 | 1791 |
1783 | 1792 |
1784 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1793 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
1785 | 1794 |
1786 } // namespace internal | 1795 } // namespace internal |
1787 } // namespace v8 | 1796 } // namespace v8 |
OLD | NEW |