| 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 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 761 | 761 |
| 762 Variable* Scope::LookupInScopeInfo(const AstRawString* name) { | 762 Variable* Scope::LookupInScopeInfo(const AstRawString* name) { |
| 763 Handle<String> name_handle = name->string(); | 763 Handle<String> name_handle = name->string(); |
| 764 // The Scope is backed up by ScopeInfo. This means it cannot operate in a | 764 // The Scope is backed up by ScopeInfo. This means it cannot operate in a |
| 765 // heap-independent mode, and all strings must be internalized immediately. So | 765 // heap-independent mode, and all strings must be internalized immediately. So |
| 766 // it's ok to get the Handle<String> here. | 766 // it's ok to get the Handle<String> here. |
| 767 // If we have a serialized scope info, we might find the variable there. | 767 // If we have a serialized scope info, we might find the variable there. |
| 768 // There should be no local slot with the given name. | 768 // There should be no local slot with the given name. |
| 769 DCHECK_LT(scope_info_->StackSlotIndex(*name_handle), 0); | 769 DCHECK_LT(scope_info_->StackSlotIndex(*name_handle), 0); |
| 770 | 770 |
| 771 bool found = false; |
| 772 |
| 773 VariableLocation location; |
| 774 int index; |
| 771 VariableMode mode; | 775 VariableMode mode; |
| 772 InitializationFlag init_flag; | 776 InitializationFlag init_flag; |
| 773 MaybeAssignedFlag maybe_assigned_flag; | 777 MaybeAssignedFlag maybe_assigned_flag; |
| 774 | 778 |
| 775 VariableLocation location = VariableLocation::CONTEXT; | 779 { |
| 776 int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, | 780 location = VariableLocation::CONTEXT; |
| 777 &init_flag, &maybe_assigned_flag); | 781 index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, |
| 778 if (index < 0 && scope_type() == MODULE_SCOPE) { | 782 &init_flag, &maybe_assigned_flag); |
| 783 found = index >= 0; |
| 784 } |
| 785 |
| 786 if (!found && scope_type() == MODULE_SCOPE) { |
| 779 location = VariableLocation::MODULE; | 787 location = VariableLocation::MODULE; |
| 780 index = scope_info_->ModuleIndex(name_handle, &mode, &init_flag, | 788 index = scope_info_->ModuleIndex(name_handle, &mode, &init_flag, |
| 781 &maybe_assigned_flag); | 789 &maybe_assigned_flag); |
| 790 found = index != 0; |
| 782 } | 791 } |
| 783 | 792 |
| 784 if (index < 0) { | 793 if (!found) { |
| 785 index = scope_info_->FunctionContextSlotIndex(*name_handle); | 794 index = scope_info_->FunctionContextSlotIndex(*name_handle); |
| 786 if (index < 0) return nullptr; // Nowhere found. | 795 if (index < 0) return nullptr; // Nowhere found. |
| 787 Variable* var = AsDeclarationScope()->DeclareFunctionVar(name); | 796 Variable* var = AsDeclarationScope()->DeclareFunctionVar(name); |
| 788 DCHECK_EQ(CONST, var->mode()); | 797 DCHECK_EQ(CONST, var->mode()); |
| 789 var->AllocateTo(VariableLocation::CONTEXT, index); | 798 var->AllocateTo(VariableLocation::CONTEXT, index); |
| 790 return variables_.Lookup(name); | 799 return variables_.Lookup(name); |
| 791 } | 800 } |
| 792 | 801 |
| 793 VariableKind kind = NORMAL_VARIABLE; | 802 VariableKind kind = NORMAL_VARIABLE; |
| 794 if (location == VariableLocation::CONTEXT && | 803 if (location == VariableLocation::CONTEXT && |
| (...skipping 1123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1918 } | 1927 } |
| 1919 | 1928 |
| 1920 if (this_function_ != nullptr && !MustAllocate(this_function_)) { | 1929 if (this_function_ != nullptr && !MustAllocate(this_function_)) { |
| 1921 this_function_ = nullptr; | 1930 this_function_ = nullptr; |
| 1922 } | 1931 } |
| 1923 } | 1932 } |
| 1924 | 1933 |
| 1925 void ModuleScope::AllocateModuleVariables() { | 1934 void ModuleScope::AllocateModuleVariables() { |
| 1926 for (const auto& it : module()->regular_imports()) { | 1935 for (const auto& it : module()->regular_imports()) { |
| 1927 Variable* var = LookupLocal(it.first); | 1936 Variable* var = LookupLocal(it.first); |
| 1928 var->AllocateTo(VariableLocation::MODULE, Variable::kModuleImportIndex); | 1937 var->AllocateTo(VariableLocation::MODULE, it.second->cell_index); |
| 1938 DCHECK(!var->IsExport()); |
| 1929 } | 1939 } |
| 1930 | 1940 |
| 1931 for (const auto& it : module()->regular_exports()) { | 1941 for (const auto& it : module()->regular_exports()) { |
| 1932 Variable* var = LookupLocal(it.first); | 1942 Variable* var = LookupLocal(it.first); |
| 1933 var->AllocateTo(VariableLocation::MODULE, Variable::kModuleExportIndex); | 1943 var->AllocateTo(VariableLocation::MODULE, it.second->cell_index); |
| 1944 DCHECK(var->IsExport()); |
| 1934 } | 1945 } |
| 1935 } | 1946 } |
| 1936 | 1947 |
| 1937 void Scope::AllocateVariablesRecursively() { | 1948 void Scope::AllocateVariablesRecursively() { |
| 1938 DCHECK(!already_resolved_); | 1949 DCHECK(!already_resolved_); |
| 1939 DCHECK_EQ(0, num_stack_slots_); | 1950 DCHECK_EQ(0, num_stack_slots_); |
| 1940 // Don't allocate variables of preparsed scopes. | 1951 // Don't allocate variables of preparsed scopes. |
| 1941 if (is_declaration_scope() && AsDeclarationScope()->is_lazily_parsed()) { | 1952 if (is_declaration_scope() && AsDeclarationScope()->is_lazily_parsed()) { |
| 1942 return; | 1953 return; |
| 1943 } | 1954 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2025 Variable* function = | 2036 Variable* function = |
| 2026 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 2037 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
| 2027 bool is_function_var_in_context = | 2038 bool is_function_var_in_context = |
| 2028 function != nullptr && function->IsContextSlot(); | 2039 function != nullptr && function->IsContextSlot(); |
| 2029 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 2040 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 2030 (is_function_var_in_context ? 1 : 0); | 2041 (is_function_var_in_context ? 1 : 0); |
| 2031 } | 2042 } |
| 2032 | 2043 |
| 2033 } // namespace internal | 2044 } // namespace internal |
| 2034 } // namespace v8 | 2045 } // namespace v8 |
| OLD | NEW |