OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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/scopeinfo.h" | 5 #include "src/ast/scopeinfo.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "src/ast/context-slot-cache.h" | 9 #include "src/ast/context-slot-cache.h" |
10 #include "src/ast/scopes.h" | 10 #include "src/ast/scopes.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 &context_globals); | 25 &context_globals); |
26 const int stack_local_count = stack_locals.length(); | 26 const int stack_local_count = stack_locals.length(); |
27 const int context_local_count = context_locals.length(); | 27 const int context_local_count = context_locals.length(); |
28 const int context_global_count = context_globals.length(); | 28 const int context_global_count = context_globals.length(); |
29 // Make sure we allocate the correct amount. | 29 // Make sure we allocate the correct amount. |
30 DCHECK_EQ(scope->ContextLocalCount(), context_local_count); | 30 DCHECK_EQ(scope->ContextLocalCount(), context_local_count); |
31 DCHECK_EQ(scope->ContextGlobalCount(), context_global_count); | 31 DCHECK_EQ(scope->ContextGlobalCount(), context_global_count); |
32 | 32 |
33 // Determine use and location of the "this" binding if it is present. | 33 // Determine use and location of the "this" binding if it is present. |
34 VariableAllocationInfo receiver_info; | 34 VariableAllocationInfo receiver_info; |
35 if (scope->has_this_declaration()) { | 35 if (scope->is_declaration_scope() && |
36 Variable* var = scope->receiver(); | 36 scope->AsDeclarationScope()->has_this_declaration()) { |
| 37 Variable* var = scope->AsDeclarationScope()->receiver(); |
37 if (!var->is_used()) { | 38 if (!var->is_used()) { |
38 receiver_info = UNUSED; | 39 receiver_info = UNUSED; |
39 } else if (var->IsContextSlot()) { | 40 } else if (var->IsContextSlot()) { |
40 receiver_info = CONTEXT; | 41 receiver_info = CONTEXT; |
41 } else { | 42 } else { |
42 DCHECK(var->IsParameter()); | 43 DCHECK(var->IsParameter()); |
43 receiver_info = STACK; | 44 receiver_info = STACK; |
44 } | 45 } |
45 } else { | 46 } else { |
46 receiver_info = NONE; | 47 receiver_info = NONE; |
47 } | 48 } |
48 | 49 |
49 bool has_new_target = scope->new_target_var() != nullptr; | 50 bool has_new_target = |
| 51 scope->is_declaration_scope() && |
| 52 scope->AsDeclarationScope()->new_target_var() != nullptr; |
50 | 53 |
51 // Determine use and location of the function variable if it is present. | 54 // Determine use and location of the function variable if it is present. |
52 VariableAllocationInfo function_name_info; | 55 VariableAllocationInfo function_name_info; |
53 VariableMode function_variable_mode; | 56 VariableMode function_variable_mode; |
54 if (scope->is_function_scope() && scope->function() != NULL) { | 57 if (scope->is_function_scope() && |
55 Variable* var = scope->function()->proxy()->var(); | 58 scope->AsDeclarationScope()->function() != nullptr) { |
| 59 Variable* var = scope->AsDeclarationScope()->function()->proxy()->var(); |
56 if (!var->is_used()) { | 60 if (!var->is_used()) { |
57 function_name_info = UNUSED; | 61 function_name_info = UNUSED; |
58 } else if (var->IsContextSlot()) { | 62 } else if (var->IsContextSlot()) { |
59 function_name_info = CONTEXT; | 63 function_name_info = CONTEXT; |
60 } else { | 64 } else { |
61 DCHECK(var->IsStackLocal()); | 65 DCHECK(var->IsStackLocal()); |
62 function_name_info = STACK; | 66 function_name_info = STACK; |
63 } | 67 } |
64 function_variable_mode = var->mode(); | 68 function_variable_mode = var->mode(); |
65 } else { | 69 } else { |
66 function_name_info = NONE; | 70 function_name_info = NONE; |
67 function_variable_mode = VAR; | 71 function_variable_mode = VAR; |
68 } | 72 } |
69 DCHECK(context_global_count == 0 || scope->scope_type() == SCRIPT_SCOPE); | 73 DCHECK(context_global_count == 0 || scope->scope_type() == SCRIPT_SCOPE); |
70 | 74 |
71 const bool has_function_name = function_name_info != NONE; | 75 const bool has_function_name = function_name_info != NONE; |
72 const bool has_receiver = receiver_info == STACK || receiver_info == CONTEXT; | 76 const bool has_receiver = receiver_info == STACK || receiver_info == CONTEXT; |
73 const int parameter_count = scope->num_parameters(); | 77 const int parameter_count = scope->num_parameters(); |
74 const int length = kVariablePartIndex + parameter_count + | 78 const int length = kVariablePartIndex + parameter_count + |
75 (1 + stack_local_count) + 2 * context_local_count + | 79 (1 + stack_local_count) + 2 * context_local_count + |
76 2 * context_global_count + | 80 2 * context_global_count + |
77 (has_receiver ? 1 : 0) + (has_function_name ? 2 : 0); | 81 (has_receiver ? 1 : 0) + (has_function_name ? 2 : 0); |
78 | 82 |
79 Factory* factory = isolate->factory(); | 83 Factory* factory = isolate->factory(); |
80 Handle<ScopeInfo> scope_info = factory->NewScopeInfo(length); | 84 Handle<ScopeInfo> scope_info = factory->NewScopeInfo(length); |
81 | 85 |
82 bool has_simple_parameters = | 86 bool has_simple_parameters = |
83 scope->is_function_scope() && scope->has_simple_parameters(); | 87 scope->is_function_scope() && |
| 88 scope->AsDeclarationScope()->has_simple_parameters(); |
| 89 FunctionKind function_kind = |
| 90 scope->is_declaration_scope() |
| 91 ? scope->AsDeclarationScope()->function_kind() |
| 92 : kNormalFunction; |
84 | 93 |
85 // Encode the flags. | 94 // Encode the flags. |
86 int flags = ScopeTypeField::encode(scope->scope_type()) | | 95 int flags = ScopeTypeField::encode(scope->scope_type()) | |
87 CallsEvalField::encode(scope->calls_eval()) | | 96 CallsEvalField::encode(scope->calls_eval()) | |
88 LanguageModeField::encode(scope->language_mode()) | | 97 LanguageModeField::encode(scope->language_mode()) | |
89 DeclarationScopeField::encode(scope->is_declaration_scope()) | | 98 DeclarationScopeField::encode(scope->is_declaration_scope()) | |
90 ReceiverVariableField::encode(receiver_info) | | 99 ReceiverVariableField::encode(receiver_info) | |
91 HasNewTargetField::encode(has_new_target) | | 100 HasNewTargetField::encode(has_new_target) | |
92 FunctionVariableField::encode(function_name_info) | | 101 FunctionVariableField::encode(function_name_info) | |
93 FunctionVariableMode::encode(function_variable_mode) | | 102 FunctionVariableMode::encode(function_variable_mode) | |
94 AsmModuleField::encode(scope->asm_module()) | | 103 AsmModuleField::encode(scope->asm_module()) | |
95 AsmFunctionField::encode(scope->asm_function()) | | 104 AsmFunctionField::encode(scope->asm_function()) | |
96 HasSimpleParametersField::encode(has_simple_parameters) | | 105 HasSimpleParametersField::encode(has_simple_parameters) | |
97 FunctionKindField::encode(scope->function_kind()); | 106 FunctionKindField::encode(function_kind); |
98 scope_info->SetFlags(flags); | 107 scope_info->SetFlags(flags); |
99 scope_info->SetParameterCount(parameter_count); | 108 scope_info->SetParameterCount(parameter_count); |
100 scope_info->SetStackLocalCount(stack_local_count); | 109 scope_info->SetStackLocalCount(stack_local_count); |
101 scope_info->SetContextLocalCount(context_local_count); | 110 scope_info->SetContextLocalCount(context_local_count); |
102 scope_info->SetContextGlobalCount(context_global_count); | 111 scope_info->SetContextGlobalCount(context_global_count); |
103 | 112 |
104 int index = kVariablePartIndex; | 113 int index = kVariablePartIndex; |
105 // Add parameters. | 114 // Add parameters. |
106 DCHECK(index == scope_info->ParameterEntriesIndex()); | 115 DCHECK(index == scope_info->ParameterEntriesIndex()); |
107 for (int i = 0; i < parameter_count; ++i) { | 116 if (scope->is_declaration_scope()) { |
108 scope_info->set(index++, *scope->parameter(i)->name()); | 117 for (int i = 0; i < parameter_count; ++i) { |
| 118 scope_info->set(index++, |
| 119 *scope->AsDeclarationScope()->parameter(i)->name()); |
| 120 } |
109 } | 121 } |
110 | 122 |
111 // Add stack locals' names. We are assuming that the stack locals' | 123 // Add stack locals' names. We are assuming that the stack locals' |
112 // slots are allocated in increasing order, so we can simply add | 124 // slots are allocated in increasing order, so we can simply add |
113 // them to the ScopeInfo object. | 125 // them to the ScopeInfo object. |
114 int first_slot_index; | 126 int first_slot_index; |
115 if (stack_local_count > 0) { | 127 if (stack_local_count > 0) { |
116 first_slot_index = stack_locals[0]->index(); | 128 first_slot_index = stack_locals[0]->index(); |
117 } else { | 129 } else { |
118 first_slot_index = 0; | 130 first_slot_index = 0; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 uint32_t value = | 176 uint32_t value = |
165 ContextLocalMode::encode(var->mode()) | | 177 ContextLocalMode::encode(var->mode()) | |
166 ContextLocalInitFlag::encode(var->initialization_flag()) | | 178 ContextLocalInitFlag::encode(var->initialization_flag()) | |
167 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); | 179 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); |
168 scope_info->set(index++, Smi::FromInt(value)); | 180 scope_info->set(index++, Smi::FromInt(value)); |
169 } | 181 } |
170 | 182 |
171 // If the receiver is allocated, add its index. | 183 // If the receiver is allocated, add its index. |
172 DCHECK(index == scope_info->ReceiverEntryIndex()); | 184 DCHECK(index == scope_info->ReceiverEntryIndex()); |
173 if (has_receiver) { | 185 if (has_receiver) { |
174 int var_index = scope->receiver()->index(); | 186 int var_index = scope->AsDeclarationScope()->receiver()->index(); |
175 scope_info->set(index++, Smi::FromInt(var_index)); | 187 scope_info->set(index++, Smi::FromInt(var_index)); |
176 // ?? DCHECK(receiver_info != CONTEXT || var_index == | 188 // ?? DCHECK(receiver_info != CONTEXT || var_index == |
177 // scope_info->ContextLength() - 1); | 189 // scope_info->ContextLength() - 1); |
178 } | 190 } |
179 | 191 |
180 // If present, add the function variable name and its index. | 192 // If present, add the function variable name and its index. |
181 DCHECK(index == scope_info->FunctionNameEntryIndex()); | 193 DCHECK(index == scope_info->FunctionNameEntryIndex()); |
182 if (has_function_name) { | 194 if (has_function_name) { |
183 int var_index = scope->function()->proxy()->var()->index(); | 195 int var_index = |
184 scope_info->set(index++, *scope->function()->proxy()->name()); | 196 scope->AsDeclarationScope()->function()->proxy()->var()->index(); |
| 197 scope_info->set(index++, |
| 198 *scope->AsDeclarationScope()->function()->proxy()->name()); |
185 scope_info->set(index++, Smi::FromInt(var_index)); | 199 scope_info->set(index++, Smi::FromInt(var_index)); |
186 DCHECK(function_name_info != CONTEXT || | 200 DCHECK(function_name_info != CONTEXT || |
187 var_index == scope_info->ContextLength() - 1); | 201 var_index == scope_info->ContextLength() - 1); |
188 } | 202 } |
189 | 203 |
190 DCHECK(index == scope_info->length()); | 204 DCHECK(index == scope_info->length()); |
191 DCHECK(scope->num_parameters() == scope_info->ParameterCount()); | 205 DCHECK(scope->num_parameters() == scope_info->ParameterCount()); |
192 DCHECK(scope->num_heap_slots() == scope_info->ContextLength() || | 206 DCHECK(scope->num_heap_slots() == scope_info->ContextLength() || |
193 (scope->num_heap_slots() == kVariablePartIndex && | 207 (scope->num_heap_slots() == kVariablePartIndex && |
194 scope_info->ContextLength() == 0)); | 208 scope_info->ContextLength() == 0)); |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 ContextLocalNameEntriesIndex() + ContextLocalCount(), this); | 692 ContextLocalNameEntriesIndex() + ContextLocalCount(), this); |
679 } | 693 } |
680 | 694 |
681 PrintF("}\n"); | 695 PrintF("}\n"); |
682 } | 696 } |
683 #endif // DEBUG | 697 #endif // DEBUG |
684 | 698 |
685 | 699 |
686 } // namespace internal | 700 } // namespace internal |
687 } // namespace v8 | 701 } // namespace v8 |
OLD | NEW |