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 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 void DeclarationScope::DeclareArguments(AstValueFactory* ast_value_factory) { | 650 void DeclarationScope::DeclareArguments(AstValueFactory* ast_value_factory) { |
651 DCHECK(is_function_scope()); | 651 DCHECK(is_function_scope()); |
652 DCHECK(!is_arrow_scope()); | 652 DCHECK(!is_arrow_scope()); |
653 | 653 |
654 arguments_ = LookupLocal(ast_value_factory->arguments_string()); | 654 arguments_ = LookupLocal(ast_value_factory->arguments_string()); |
655 if (arguments_ == nullptr) { | 655 if (arguments_ == nullptr) { |
656 // Declare 'arguments' variable which exists in all non arrow functions. | 656 // Declare 'arguments' variable which exists in all non arrow functions. |
657 // Note that it might never be accessed, in which case it won't be | 657 // Note that it might never be accessed, in which case it won't be |
658 // allocated during variable allocation. | 658 // allocated during variable allocation. |
659 arguments_ = Declare(zone(), ast_value_factory->arguments_string(), VAR); | 659 arguments_ = Declare(zone(), ast_value_factory->arguments_string(), VAR); |
660 } else if (IsLexicalVariableMode(arguments_->mode())) { | 660 } else if (IsLexical(arguments_)) { |
661 // Check if there's lexically declared variable named arguments to avoid | 661 // Check if there's lexically declared variable named arguments to avoid |
662 // redeclaration. See ES#sec-functiondeclarationinstantiation, step 20. | 662 // redeclaration. See ES#sec-functiondeclarationinstantiation, step 20. |
663 arguments_ = nullptr; | 663 arguments_ = nullptr; |
664 } | 664 } |
665 } | 665 } |
666 | 666 |
667 void DeclarationScope::DeclareDefaultFunctionVariables( | 667 void DeclarationScope::DeclareDefaultFunctionVariables( |
668 AstValueFactory* ast_value_factory) { | 668 AstValueFactory* ast_value_factory) { |
669 DCHECK(is_function_scope()); | 669 DCHECK(is_function_scope()); |
670 DCHECK(!is_arrow_scope()); | 670 DCHECK(!is_arrow_scope()); |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
943 } | 943 } |
944 | 944 |
945 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, | 945 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, |
946 InitializationFlag init_flag, VariableKind kind, | 946 InitializationFlag init_flag, VariableKind kind, |
947 MaybeAssignedFlag maybe_assigned_flag) { | 947 MaybeAssignedFlag maybe_assigned_flag) { |
948 DCHECK(!already_resolved_); | 948 DCHECK(!already_resolved_); |
949 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are | 949 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are |
950 // introduced during variable allocation, and TEMPORARY variables are | 950 // introduced during variable allocation, and TEMPORARY variables are |
951 // allocated via NewTemporary(). | 951 // allocated via NewTemporary(). |
952 DCHECK(IsDeclaredVariableMode(mode)); | 952 DCHECK(IsDeclaredVariableMode(mode)); |
953 DCHECK(!GetDeclarationScope()->is_being_lazily_parsed()); | 953 DCHECK_IMPLIES(GetDeclarationScope()->is_being_lazily_parsed(), |
| 954 mode == VAR || mode == LET || mode == CONST); |
954 DCHECK(!GetDeclarationScope()->was_lazily_parsed()); | 955 DCHECK(!GetDeclarationScope()->was_lazily_parsed()); |
955 return Declare(zone(), name, mode, kind, init_flag, maybe_assigned_flag); | 956 return Declare(zone(), name, mode, kind, init_flag, maybe_assigned_flag); |
956 } | 957 } |
957 | 958 |
958 Variable* Scope::DeclareVariable( | 959 Variable* Scope::DeclareVariable( |
959 Declaration* declaration, VariableMode mode, InitializationFlag init, | 960 Declaration* declaration, VariableMode mode, InitializationFlag init, |
960 bool allow_harmony_restrictive_generators, | 961 bool allow_harmony_restrictive_generators, |
961 bool* sloppy_mode_block_scope_function_redefinition, bool* ok) { | 962 bool* sloppy_mode_block_scope_function_redefinition, bool* ok) { |
962 DCHECK(IsDeclaredVariableMode(mode)); | 963 DCHECK(IsDeclaredVariableMode(mode)); |
963 DCHECK(!already_resolved_); | 964 DCHECK(!already_resolved_); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1073 DCHECK(!is_eval_scope()); | 1074 DCHECK(!is_eval_scope()); |
1074 // Unlike DeclareVariable, DeclareVariableName allows declaring variables in | 1075 // Unlike DeclareVariable, DeclareVariableName allows declaring variables in |
1075 // catch scopes: Parser::RewriteCatchPattern bypasses DeclareVariable by | 1076 // catch scopes: Parser::RewriteCatchPattern bypasses DeclareVariable by |
1076 // calling DeclareLocal directly, and it doesn't make sense to add a similar | 1077 // calling DeclareLocal directly, and it doesn't make sense to add a similar |
1077 // bypass mechanism for PreParser. | 1078 // bypass mechanism for PreParser. |
1078 DCHECK(is_declaration_scope() || (IsLexicalVariableMode(mode) && | 1079 DCHECK(is_declaration_scope() || (IsLexicalVariableMode(mode) && |
1079 (is_block_scope() || is_catch_scope()))); | 1080 (is_block_scope() || is_catch_scope()))); |
1080 DCHECK(scope_info_.is_null()); | 1081 DCHECK(scope_info_.is_null()); |
1081 | 1082 |
1082 // Declare the variable in the declaration scope. | 1083 // Declare the variable in the declaration scope. |
1083 variables_.DeclareName(zone(), name, mode); | 1084 if (FLAG_preparser_scope_analysis) { |
| 1085 Variable* var = LookupLocal(name); |
| 1086 if (var == nullptr) { |
| 1087 var = DeclareLocal(name, mode); |
| 1088 } |
| 1089 var->set_is_used(); |
| 1090 } else { |
| 1091 variables_.DeclareName(zone(), name, mode); |
| 1092 } |
1084 } | 1093 } |
1085 | 1094 |
1086 VariableProxy* Scope::NewUnresolved(AstNodeFactory* factory, | 1095 VariableProxy* Scope::NewUnresolved(AstNodeFactory* factory, |
1087 const AstRawString* name, | 1096 const AstRawString* name, |
1088 int start_position, VariableKind kind) { | 1097 int start_position, VariableKind kind) { |
1089 // Note that we must not share the unresolved variables with | 1098 // Note that we must not share the unresolved variables with |
1090 // the same name because they may be removed selectively via | 1099 // the same name because they may be removed selectively via |
1091 // RemoveUnresolved(). | 1100 // RemoveUnresolved(). |
1092 DCHECK(!already_resolved_); | 1101 DCHECK(!already_resolved_); |
1093 DCHECK_EQ(factory->zone(), zone()); | 1102 DCHECK_EQ(factory->zone(), zone()); |
(...skipping 808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1902 unresolved_ = nullptr; | 1911 unresolved_ = nullptr; |
1903 | 1912 |
1904 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1913 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1905 stack = scope->FetchFreeVariables(max_outer_scope, info, stack); | 1914 stack = scope->FetchFreeVariables(max_outer_scope, info, stack); |
1906 } | 1915 } |
1907 | 1916 |
1908 return stack; | 1917 return stack; |
1909 } | 1918 } |
1910 | 1919 |
1911 bool Scope::MustAllocate(Variable* var) { | 1920 bool Scope::MustAllocate(Variable* var) { |
| 1921 if (var == kDummyPreParserLexicalVariable || var == kDummyPreParserVariable) { |
| 1922 return true; |
| 1923 } |
1912 DCHECK(var->location() != VariableLocation::MODULE); | 1924 DCHECK(var->location() != VariableLocation::MODULE); |
1913 // Give var a read/write use if there is a chance it might be accessed | 1925 // Give var a read/write use if there is a chance it might be accessed |
1914 // via an eval() call. This is only possible if the variable has a | 1926 // via an eval() call. This is only possible if the variable has a |
1915 // visible name. | 1927 // visible name. |
1916 if ((var->is_this() || !var->raw_name()->IsEmpty()) && | 1928 if ((var->is_this() || !var->raw_name()->IsEmpty()) && |
1917 (inner_scope_calls_eval_ || is_catch_scope() || is_script_scope())) { | 1929 (inner_scope_calls_eval_ || is_catch_scope() || is_script_scope())) { |
1918 var->set_is_used(); | 1930 var->set_is_used(); |
1919 if (inner_scope_calls_eval_) var->set_maybe_assigned(); | 1931 if (inner_scope_calls_eval_) var->set_maybe_assigned(); |
1920 } | 1932 } |
1921 DCHECK(!var->has_forced_context_allocation() || var->is_used()); | 1933 DCHECK(!var->has_forced_context_allocation() || var->is_used()); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2078 | 2090 |
2079 for (const auto& it : module()->regular_exports()) { | 2091 for (const auto& it : module()->regular_exports()) { |
2080 Variable* var = LookupLocal(it.first); | 2092 Variable* var = LookupLocal(it.first); |
2081 var->AllocateTo(VariableLocation::MODULE, it.second->cell_index); | 2093 var->AllocateTo(VariableLocation::MODULE, it.second->cell_index); |
2082 DCHECK(var->IsExport()); | 2094 DCHECK(var->IsExport()); |
2083 } | 2095 } |
2084 } | 2096 } |
2085 | 2097 |
2086 void Scope::AllocateVariablesRecursively() { | 2098 void Scope::AllocateVariablesRecursively() { |
2087 DCHECK(!already_resolved_); | 2099 DCHECK(!already_resolved_); |
2088 DCHECK_EQ(0, num_stack_slots_); | 2100 DCHECK_IMPLIES(!FLAG_preparser_scope_analysis, num_stack_slots_ == 0); |
| 2101 |
2089 // Don't allocate variables of preparsed scopes. | 2102 // Don't allocate variables of preparsed scopes. |
2090 if (is_declaration_scope() && AsDeclarationScope()->was_lazily_parsed()) { | 2103 if (is_declaration_scope() && AsDeclarationScope()->was_lazily_parsed()) { |
2091 return; | 2104 return; |
2092 } | 2105 } |
2093 | 2106 |
2094 // Allocate variables for inner scopes. | 2107 // Allocate variables for inner scopes. |
2095 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 2108 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
2096 scope->AllocateVariablesRecursively(); | 2109 scope->AllocateVariablesRecursively(); |
2097 } | 2110 } |
2098 | 2111 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2157 MaybeHandle<ScopeInfo> outer = NeedsContext() ? scope_info_ : outer_scope; | 2170 MaybeHandle<ScopeInfo> outer = NeedsContext() ? scope_info_ : outer_scope; |
2158 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 2171 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
2159 if (scope->is_function_scope()) continue; | 2172 if (scope->is_function_scope()) continue; |
2160 scope->AllocateDebuggerScopeInfos(isolate, outer); | 2173 scope->AllocateDebuggerScopeInfos(isolate, outer); |
2161 } | 2174 } |
2162 } | 2175 } |
2163 | 2176 |
2164 void Scope::CollectVariableData(PreParsedScopeData* data) { | 2177 void Scope::CollectVariableData(PreParsedScopeData* data) { |
2165 PreParsedScopeData::ScopeScope scope_scope(data, scope_type(), | 2178 PreParsedScopeData::ScopeScope scope_scope(data, scope_type(), |
2166 start_position(), end_position()); | 2179 start_position(), end_position()); |
2167 // TODO(marja): Add data about the variables. | 2180 for (Variable* local : locals_) { |
2168 | 2181 if (local->mode() == VAR || local->mode() == LET || |
| 2182 local->mode() == CONST) { |
| 2183 scope_scope.AddVariable(local->location(), local->maybe_assigned()); |
| 2184 } |
| 2185 } |
2169 for (Scope* inner = inner_scope_; inner != nullptr; inner = inner->sibling_) { | 2186 for (Scope* inner = inner_scope_; inner != nullptr; inner = inner->sibling_) { |
2170 inner->CollectVariableData(data); | 2187 inner->CollectVariableData(data); |
2171 } | 2188 } |
2172 } | 2189 } |
2173 | 2190 |
2174 int Scope::StackLocalCount() const { | 2191 int Scope::StackLocalCount() const { |
2175 Variable* function = | 2192 Variable* function = |
2176 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 2193 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
2177 return num_stack_slots() - | 2194 return num_stack_slots() - |
2178 (function != nullptr && function->IsStackLocal() ? 1 : 0); | 2195 (function != nullptr && function->IsStackLocal() ? 1 : 0); |
2179 } | 2196 } |
2180 | 2197 |
2181 | 2198 |
2182 int Scope::ContextLocalCount() const { | 2199 int Scope::ContextLocalCount() const { |
2183 if (num_heap_slots() == 0) return 0; | 2200 if (num_heap_slots() == 0) return 0; |
2184 Variable* function = | 2201 Variable* function = |
2185 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 2202 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
2186 bool is_function_var_in_context = | 2203 bool is_function_var_in_context = |
2187 function != nullptr && function->IsContextSlot(); | 2204 function != nullptr && function->IsContextSlot(); |
2188 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 2205 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
2189 (is_function_var_in_context ? 1 : 0); | 2206 (is_function_var_in_context ? 1 : 0); |
2190 } | 2207 } |
2191 | 2208 |
2192 } // namespace internal | 2209 } // namespace internal |
2193 } // namespace v8 | 2210 } // namespace v8 |
OLD | NEW |