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/ast/ast.h" | 10 #include "src/ast/ast.h" |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 variables_(zone), | 270 variables_(zone), |
271 scope_info_(scope_info), | 271 scope_info_(scope_info), |
272 scope_type_(CATCH_SCOPE) { | 272 scope_type_(CATCH_SCOPE) { |
273 SetDefaults(); | 273 SetDefaults(); |
274 #ifdef DEBUG | 274 #ifdef DEBUG |
275 already_resolved_ = true; | 275 already_resolved_ = true; |
276 #endif | 276 #endif |
277 // Cache the catch variable, even though it's also available via the | 277 // Cache the catch variable, even though it's also available via the |
278 // scope_info, as the parser expects that a catch scope always has the catch | 278 // scope_info, as the parser expects that a catch scope always has the catch |
279 // variable as first and only variable. | 279 // variable as first and only variable. |
280 Variable* variable = Declare(zone, catch_variable_name, VAR, NORMAL_VARIABLE, | 280 Variable* variable = Declare(zone, catch_variable_name, VAR); |
281 kCreatedInitialized); | |
282 AllocateHeapSlot(variable); | 281 AllocateHeapSlot(variable); |
283 } | 282 } |
284 | 283 |
285 void DeclarationScope::SetDefaults() { | 284 void DeclarationScope::SetDefaults() { |
286 is_declaration_scope_ = true; | 285 is_declaration_scope_ = true; |
287 has_simple_parameters_ = true; | 286 has_simple_parameters_ = true; |
288 asm_module_ = false; | 287 asm_module_ = false; |
289 asm_function_ = false; | 288 asm_function_ = false; |
290 force_eager_compilation_ = false; | 289 force_eager_compilation_ = false; |
291 has_arguments_parameter_ = false; | 290 has_arguments_parameter_ = false; |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 | 647 |
649 void DeclarationScope::DeclareArguments(AstValueFactory* ast_value_factory) { | 648 void DeclarationScope::DeclareArguments(AstValueFactory* ast_value_factory) { |
650 DCHECK(is_function_scope()); | 649 DCHECK(is_function_scope()); |
651 DCHECK(!is_arrow_scope()); | 650 DCHECK(!is_arrow_scope()); |
652 | 651 |
653 arguments_ = LookupLocal(ast_value_factory->arguments_string()); | 652 arguments_ = LookupLocal(ast_value_factory->arguments_string()); |
654 if (arguments_ == nullptr) { | 653 if (arguments_ == nullptr) { |
655 // Declare 'arguments' variable which exists in all non arrow functions. | 654 // Declare 'arguments' variable which exists in all non arrow functions. |
656 // Note that it might never be accessed, in which case it won't be | 655 // Note that it might never be accessed, in which case it won't be |
657 // allocated during variable allocation. | 656 // allocated during variable allocation. |
658 arguments_ = Declare(zone(), ast_value_factory->arguments_string(), VAR, | 657 arguments_ = Declare(zone(), ast_value_factory->arguments_string(), VAR); |
659 NORMAL_VARIABLE, kCreatedInitialized); | |
660 } else if (IsLexicalVariableMode(arguments_->mode())) { | 658 } else if (IsLexicalVariableMode(arguments_->mode())) { |
661 // Check if there's lexically declared variable named arguments to avoid | 659 // Check if there's lexically declared variable named arguments to avoid |
662 // redeclaration. See ES#sec-functiondeclarationinstantiation, step 20. | 660 // redeclaration. See ES#sec-functiondeclarationinstantiation, step 20. |
663 arguments_ = nullptr; | 661 arguments_ = nullptr; |
664 } | 662 } |
665 } | 663 } |
666 | 664 |
667 void DeclarationScope::DeclareDefaultFunctionVariables( | 665 void DeclarationScope::DeclareDefaultFunctionVariables( |
668 AstValueFactory* ast_value_factory) { | 666 AstValueFactory* ast_value_factory) { |
669 DCHECK(is_function_scope()); | 667 DCHECK(is_function_scope()); |
670 DCHECK(!is_arrow_scope()); | 668 DCHECK(!is_arrow_scope()); |
671 | 669 |
672 DeclareThis(ast_value_factory); | 670 DeclareThis(ast_value_factory); |
673 new_target_ = Declare(zone(), ast_value_factory->new_target_string(), CONST, | 671 new_target_ = Declare(zone(), ast_value_factory->new_target_string(), CONST); |
674 NORMAL_VARIABLE, kCreatedInitialized); | |
675 | 672 |
676 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) || | 673 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) || |
677 IsAccessorFunction(function_kind_)) { | 674 IsAccessorFunction(function_kind_)) { |
678 this_function_ = Declare(zone(), ast_value_factory->this_function_string(), | 675 this_function_ = |
679 CONST, NORMAL_VARIABLE, kCreatedInitialized); | 676 Declare(zone(), ast_value_factory->this_function_string(), CONST); |
680 } | 677 } |
681 } | 678 } |
682 | 679 |
683 Variable* DeclarationScope::DeclareFunctionVar(const AstRawString* name) { | 680 Variable* DeclarationScope::DeclareFunctionVar(const AstRawString* name) { |
684 DCHECK(is_function_scope()); | 681 DCHECK(is_function_scope()); |
685 DCHECK_NULL(function_); | 682 DCHECK_NULL(function_); |
686 DCHECK_NULL(variables_.Lookup(name)); | 683 DCHECK_NULL(variables_.Lookup(name)); |
687 VariableKind kind = is_sloppy(language_mode()) ? SLOPPY_FUNCTION_NAME_VARIABLE | 684 VariableKind kind = is_sloppy(language_mode()) ? SLOPPY_FUNCTION_NAME_VARIABLE |
688 : NORMAL_VARIABLE; | 685 : NORMAL_VARIABLE; |
689 function_ = | 686 function_ = |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
924 DCHECK(!already_resolved_); | 921 DCHECK(!already_resolved_); |
925 DCHECK(is_function_scope() || is_module_scope()); | 922 DCHECK(is_function_scope() || is_module_scope()); |
926 DCHECK(!has_rest_); | 923 DCHECK(!has_rest_); |
927 DCHECK(!is_optional || !is_rest); | 924 DCHECK(!is_optional || !is_rest); |
928 DCHECK(!is_being_lazily_parsed_); | 925 DCHECK(!is_being_lazily_parsed_); |
929 DCHECK(!was_lazily_parsed_); | 926 DCHECK(!was_lazily_parsed_); |
930 Variable* var; | 927 Variable* var; |
931 if (mode == TEMPORARY) { | 928 if (mode == TEMPORARY) { |
932 var = NewTemporary(name); | 929 var = NewTemporary(name); |
933 } else { | 930 } else { |
934 var = Declare(zone(), name, mode, NORMAL_VARIABLE, kCreatedInitialized); | 931 var = Declare(zone(), name, mode); |
935 // TODO(wingo): Avoid O(n^2) check. | 932 // TODO(wingo): Avoid O(n^2) check. |
936 *is_duplicate = IsDeclaredParameter(name); | 933 *is_duplicate = IsDeclaredParameter(name); |
937 } | 934 } |
938 has_rest_ = is_rest; | 935 has_rest_ = is_rest; |
939 params_.Add(var, zone()); | 936 params_.Add(var, zone()); |
940 if (name == ast_value_factory->arguments_string()) { | 937 if (name == ast_value_factory->arguments_string()) { |
941 has_arguments_parameter_ = true; | 938 has_arguments_parameter_ = true; |
942 } | 939 } |
943 return var; | 940 return var; |
944 } | 941 } |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1101 void Scope::AddUnresolved(VariableProxy* proxy) { | 1098 void Scope::AddUnresolved(VariableProxy* proxy) { |
1102 DCHECK(!already_resolved_); | 1099 DCHECK(!already_resolved_); |
1103 DCHECK(!proxy->is_resolved()); | 1100 DCHECK(!proxy->is_resolved()); |
1104 proxy->set_next_unresolved(unresolved_); | 1101 proxy->set_next_unresolved(unresolved_); |
1105 unresolved_ = proxy; | 1102 unresolved_ = proxy; |
1106 } | 1103 } |
1107 | 1104 |
1108 Variable* DeclarationScope::DeclareDynamicGlobal(const AstRawString* name, | 1105 Variable* DeclarationScope::DeclareDynamicGlobal(const AstRawString* name, |
1109 VariableKind kind) { | 1106 VariableKind kind) { |
1110 DCHECK(is_script_scope()); | 1107 DCHECK(is_script_scope()); |
1111 return variables_.Declare(zone(), this, name, DYNAMIC_GLOBAL, kind, | 1108 return variables_.Declare(zone(), this, name, DYNAMIC_GLOBAL, kind); |
1112 kCreatedInitialized); | |
1113 } | 1109 } |
1114 | 1110 |
1115 | 1111 |
1116 bool Scope::RemoveUnresolved(VariableProxy* var) { | 1112 bool Scope::RemoveUnresolved(VariableProxy* var) { |
1117 if (unresolved_ == var) { | 1113 if (unresolved_ == var) { |
1118 unresolved_ = var->next_unresolved(); | 1114 unresolved_ = var->next_unresolved(); |
1119 var->set_next_unresolved(nullptr); | 1115 var->set_next_unresolved(nullptr); |
1120 return true; | 1116 return true; |
1121 } | 1117 } |
1122 VariableProxy* current = unresolved_; | 1118 VariableProxy* current = unresolved_; |
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1630 } | 1626 } |
1631 CHECK_EQ(scope->zone(), zone()); | 1627 CHECK_EQ(scope->zone(), zone()); |
1632 scope->CheckZones(); | 1628 scope->CheckZones(); |
1633 } | 1629 } |
1634 } | 1630 } |
1635 #endif // DEBUG | 1631 #endif // DEBUG |
1636 | 1632 |
1637 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) { | 1633 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) { |
1638 // Declare a new non-local. | 1634 // Declare a new non-local. |
1639 DCHECK(IsDynamicVariableMode(mode)); | 1635 DCHECK(IsDynamicVariableMode(mode)); |
1640 Variable* var = variables_.Declare(zone(), NULL, name, mode, NORMAL_VARIABLE, | 1636 Variable* var = variables_.Declare(zone(), nullptr, name, mode); |
1641 kCreatedInitialized); | |
1642 // Allocate it by giving it a dynamic lookup. | 1637 // Allocate it by giving it a dynamic lookup. |
1643 var->AllocateTo(VariableLocation::LOOKUP, -1); | 1638 var->AllocateTo(VariableLocation::LOOKUP, -1); |
1644 return var; | 1639 return var; |
1645 } | 1640 } |
1646 | 1641 |
1647 Variable* Scope::LookupRecursive(VariableProxy* proxy, Scope* outer_scope_end) { | 1642 Variable* Scope::LookupRecursive(VariableProxy* proxy, Scope* outer_scope_end) { |
1648 DCHECK_NE(outer_scope_end, this); | 1643 DCHECK_NE(outer_scope_end, this); |
1649 // Short-cut: whenever we find a debug-evaluate scope, just look everything up | 1644 // Short-cut: whenever we find a debug-evaluate scope, just look everything up |
1650 // dynamically. Debug-evaluate doesn't properly create scope info for the | 1645 // dynamically. Debug-evaluate doesn't properly create scope info for the |
1651 // lookups it does. It may not have a valid 'this' declaration, and anything | 1646 // lookups it does. It may not have a valid 'this' declaration, and anything |
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2168 Variable* function = | 2163 Variable* function = |
2169 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 2164 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
2170 bool is_function_var_in_context = | 2165 bool is_function_var_in_context = |
2171 function != nullptr && function->IsContextSlot(); | 2166 function != nullptr && function->IsContextSlot(); |
2172 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 2167 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
2173 (is_function_var_in_context ? 1 : 0); | 2168 (is_function_var_in_context ? 1 : 0); |
2174 } | 2169 } |
2175 | 2170 |
2176 } // namespace internal | 2171 } // namespace internal |
2177 } // namespace v8 | 2172 } // namespace v8 |
OLD | NEW |