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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 90 |
91 | 91 |
92 // ---------------------------------------------------------------------------- | 92 // ---------------------------------------------------------------------------- |
93 // Implementation of Scope | 93 // Implementation of Scope |
94 | 94 |
95 Scope::Scope(Zone* zone) | 95 Scope::Scope(Zone* zone) |
96 : zone_(zone), | 96 : zone_(zone), |
97 outer_scope_(nullptr), | 97 outer_scope_(nullptr), |
98 variables_(zone), | 98 variables_(zone), |
99 locals_(4, zone), | 99 locals_(4, zone), |
100 decls_(4, zone), | |
101 scope_type_(SCRIPT_SCOPE) { | 100 scope_type_(SCRIPT_SCOPE) { |
102 SetDefaults(); | 101 SetDefaults(); |
103 } | 102 } |
104 | 103 |
105 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type) | 104 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type) |
106 : zone_(zone), | 105 : zone_(zone), |
107 outer_scope_(outer_scope), | 106 outer_scope_(outer_scope), |
108 variables_(zone), | 107 variables_(zone), |
109 locals_(4, zone), | 108 locals_(4, zone), |
110 decls_(4, zone), | |
111 scope_type_(scope_type) { | 109 scope_type_(scope_type) { |
112 DCHECK_NE(SCRIPT_SCOPE, scope_type); | 110 DCHECK_NE(SCRIPT_SCOPE, scope_type); |
113 SetDefaults(); | 111 SetDefaults(); |
114 set_language_mode(outer_scope->language_mode()); | 112 set_language_mode(outer_scope->language_mode()); |
115 force_context_allocation_ = | 113 force_context_allocation_ = |
116 !is_function_scope() && outer_scope->has_forced_context_allocation(); | 114 !is_function_scope() && outer_scope->has_forced_context_allocation(); |
117 outer_scope_->AddInnerScope(this); | 115 outer_scope_->AddInnerScope(this); |
118 } | 116 } |
119 | 117 |
120 Scope::Snapshot::Snapshot(Scope* scope) | 118 Scope::Snapshot::Snapshot(Scope* scope) |
121 : outer_scope_(scope), | 119 : outer_scope_(scope), |
122 top_inner_scope_(scope->inner_scope_), | 120 top_inner_scope_(scope->inner_scope_), |
123 top_unresolved_(scope->unresolved_), | 121 top_unresolved_(scope->unresolved_), |
124 top_local_(scope->GetClosureScope()->locals_.length()), | 122 top_local_(scope->GetClosureScope()->locals_.length()), |
125 top_decl_(scope->GetClosureScope()->decls_.length()) {} | 123 top_decl_(scope->GetClosureScope()->decls_.end()) {} |
126 | 124 |
127 DeclarationScope::DeclarationScope(Zone* zone, | 125 DeclarationScope::DeclarationScope(Zone* zone, |
128 AstValueFactory* ast_value_factory) | 126 AstValueFactory* ast_value_factory) |
129 : Scope(zone), | 127 : Scope(zone), |
130 function_kind_(kNormalFunction), | 128 function_kind_(kNormalFunction), |
131 params_(4, zone), | 129 params_(4, zone), |
132 sloppy_block_function_map_(zone) { | 130 sloppy_block_function_map_(zone) { |
133 DCHECK_EQ(scope_type_, SCRIPT_SCOPE); | 131 DCHECK_EQ(scope_type_, SCRIPT_SCOPE); |
134 SetDefaults(); | 132 SetDefaults(); |
135 | 133 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 module_descriptor_->AddRegularImport(ModuleDescriptor::Entry::Deserialize( | 203 module_descriptor_->AddRegularImport(ModuleDescriptor::Entry::Deserialize( |
206 isolate, avfactory, serialized_entry)); | 204 isolate, avfactory, serialized_entry)); |
207 } | 205 } |
208 } | 206 } |
209 | 207 |
210 Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info) | 208 Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info) |
211 : zone_(zone), | 209 : zone_(zone), |
212 outer_scope_(nullptr), | 210 outer_scope_(nullptr), |
213 variables_(zone), | 211 variables_(zone), |
214 locals_(0, zone), | 212 locals_(0, zone), |
215 decls_(0, zone), | |
216 scope_info_(scope_info), | 213 scope_info_(scope_info), |
217 scope_type_(scope_type) { | 214 scope_type_(scope_type) { |
218 DCHECK(!scope_info.is_null()); | 215 DCHECK(!scope_info.is_null()); |
219 SetDefaults(); | 216 SetDefaults(); |
220 #ifdef DEBUG | 217 #ifdef DEBUG |
221 already_resolved_ = true; | 218 already_resolved_ = true; |
222 #endif | 219 #endif |
223 if (scope_info->CallsEval()) RecordEvalCall(); | 220 if (scope_info->CallsEval()) RecordEvalCall(); |
224 set_language_mode(scope_info->language_mode()); | 221 set_language_mode(scope_info->language_mode()); |
225 num_heap_slots_ = scope_info->ContextLength(); | 222 num_heap_slots_ = scope_info->ContextLength(); |
226 DCHECK_LE(Context::MIN_CONTEXT_SLOTS, num_heap_slots_); | 223 DCHECK_LE(Context::MIN_CONTEXT_SLOTS, num_heap_slots_); |
227 } | 224 } |
228 | 225 |
229 DeclarationScope::DeclarationScope(Zone* zone, ScopeType scope_type, | 226 DeclarationScope::DeclarationScope(Zone* zone, ScopeType scope_type, |
230 Handle<ScopeInfo> scope_info) | 227 Handle<ScopeInfo> scope_info) |
231 : Scope(zone, scope_type, scope_info), | 228 : Scope(zone, scope_type, scope_info), |
232 function_kind_(scope_info->function_kind()), | 229 function_kind_(scope_info->function_kind()), |
233 params_(0, zone), | 230 params_(0, zone), |
234 sloppy_block_function_map_(zone) { | 231 sloppy_block_function_map_(zone) { |
235 DCHECK_NE(scope_type, SCRIPT_SCOPE); | 232 DCHECK_NE(scope_type, SCRIPT_SCOPE); |
236 SetDefaults(); | 233 SetDefaults(); |
237 } | 234 } |
238 | 235 |
239 Scope::Scope(Zone* zone, const AstRawString* catch_variable_name, | 236 Scope::Scope(Zone* zone, const AstRawString* catch_variable_name, |
240 Handle<ScopeInfo> scope_info) | 237 Handle<ScopeInfo> scope_info) |
241 : zone_(zone), | 238 : zone_(zone), |
242 outer_scope_(nullptr), | 239 outer_scope_(nullptr), |
243 variables_(zone), | 240 variables_(zone), |
244 locals_(0, zone), | 241 locals_(0, zone), |
245 decls_(0, zone), | |
246 scope_info_(scope_info), | 242 scope_info_(scope_info), |
247 scope_type_(CATCH_SCOPE) { | 243 scope_type_(CATCH_SCOPE) { |
248 SetDefaults(); | 244 SetDefaults(); |
249 #ifdef DEBUG | 245 #ifdef DEBUG |
250 already_resolved_ = true; | 246 already_resolved_ = true; |
251 #endif | 247 #endif |
252 // Cache the catch variable, even though it's also available via the | 248 // Cache the catch variable, even though it's also available via the |
253 // scope_info, as the parser expects that a catch scope always has the catch | 249 // scope_info, as the parser expects that a catch scope always has the catch |
254 // variable as first and only variable. | 250 // variable as first and only variable. |
255 Variable* variable = Declare(zone, this, catch_variable_name, VAR, | 251 Variable* variable = Declare(zone, this, catch_variable_name, VAR, |
(...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
942 | 938 |
943 // We add a declaration node for every declaration. The compiler | 939 // We add a declaration node for every declaration. The compiler |
944 // will only generate code if necessary. In particular, declarations | 940 // will only generate code if necessary. In particular, declarations |
945 // for inner local variables that do not represent functions won't | 941 // for inner local variables that do not represent functions won't |
946 // result in any generated code. | 942 // result in any generated code. |
947 // | 943 // |
948 // This will lead to multiple declaration nodes for the | 944 // This will lead to multiple declaration nodes for the |
949 // same variable if it is declared several times. This is not a | 945 // same variable if it is declared several times. This is not a |
950 // semantic issue, but it may be a performance issue since it may | 946 // semantic issue, but it may be a performance issue since it may |
951 // lead to repeated DeclareEvalVar or DeclareEvalFunction calls. | 947 // lead to repeated DeclareEvalVar or DeclareEvalFunction calls. |
952 decls_.Add(declaration, zone()); | 948 decls_.Add(declaration); |
953 proxy->BindTo(var); | 949 proxy->BindTo(var); |
954 return var; | 950 return var; |
955 } | 951 } |
956 | 952 |
957 VariableProxy* Scope::NewUnresolved(AstNodeFactory* factory, | 953 VariableProxy* Scope::NewUnresolved(AstNodeFactory* factory, |
958 const AstRawString* name, | 954 const AstRawString* name, |
959 int start_position, VariableKind kind) { | 955 int start_position, VariableKind kind) { |
960 // Note that we must not share the unresolved variables with | 956 // Note that we must not share the unresolved variables with |
961 // the same name because they may be removed selectively via | 957 // the same name because they may be removed selectively via |
962 // RemoveUnresolved(). | 958 // RemoveUnresolved(). |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1024 | 1020 |
1025 Variable* Scope::NewTemporary(const AstRawString* name) { | 1021 Variable* Scope::NewTemporary(const AstRawString* name) { |
1026 DeclarationScope* scope = GetClosureScope(); | 1022 DeclarationScope* scope = GetClosureScope(); |
1027 Variable* var = new (zone()) | 1023 Variable* var = new (zone()) |
1028 Variable(scope, name, TEMPORARY, NORMAL_VARIABLE, kCreatedInitialized); | 1024 Variable(scope, name, TEMPORARY, NORMAL_VARIABLE, kCreatedInitialized); |
1029 scope->AddLocal(var); | 1025 scope->AddLocal(var); |
1030 return var; | 1026 return var; |
1031 } | 1027 } |
1032 | 1028 |
1033 Declaration* Scope::CheckConflictingVarDeclarations() { | 1029 Declaration* Scope::CheckConflictingVarDeclarations() { |
1034 int length = decls_.length(); | 1030 for (Declaration* decl : decls_) { |
1035 for (int i = 0; i < length; i++) { | |
1036 Declaration* decl = decls_[i]; | |
1037 VariableMode mode = decl->proxy()->var()->mode(); | 1031 VariableMode mode = decl->proxy()->var()->mode(); |
1038 if (IsLexicalVariableMode(mode) && !is_block_scope()) continue; | 1032 if (IsLexicalVariableMode(mode) && !is_block_scope()) continue; |
1039 | 1033 |
1040 // Iterate through all scopes until and including the declaration scope. | 1034 // Iterate through all scopes until and including the declaration scope. |
1041 Scope* previous = NULL; | 1035 Scope* previous = NULL; |
1042 Scope* current = decl->scope(); | 1036 Scope* current = decl->scope(); |
1043 // Lexical vs lexical conflicts within the same scope have already been | 1037 // Lexical vs lexical conflicts within the same scope have already been |
1044 // captured in Parser::Declare. The only conflicts we still need to check | 1038 // captured in Parser::Declare. The only conflicts we still need to check |
1045 // are lexical vs VAR, or any declarations within a declaration block scope | 1039 // are lexical vs VAR, or any declarations within a declaration block scope |
1046 // vs lexical declarations in its surrounding (function) scope. | 1040 // vs lexical declarations in its surrounding (function) scope. |
(...skipping 14 matching lines...) Expand all Loading... |
1061 | 1055 |
1062 Declaration* Scope::CheckLexDeclarationsConflictingWith( | 1056 Declaration* Scope::CheckLexDeclarationsConflictingWith( |
1063 const ZoneList<const AstRawString*>& names) { | 1057 const ZoneList<const AstRawString*>& names) { |
1064 DCHECK(is_block_scope()); | 1058 DCHECK(is_block_scope()); |
1065 for (int i = 0; i < names.length(); ++i) { | 1059 for (int i = 0; i < names.length(); ++i) { |
1066 Variable* var = LookupLocal(names.at(i)); | 1060 Variable* var = LookupLocal(names.at(i)); |
1067 if (var != nullptr) { | 1061 if (var != nullptr) { |
1068 // Conflict; find and return its declaration. | 1062 // Conflict; find and return its declaration. |
1069 DCHECK(IsLexicalVariableMode(var->mode())); | 1063 DCHECK(IsLexicalVariableMode(var->mode())); |
1070 const AstRawString* name = names.at(i); | 1064 const AstRawString* name = names.at(i); |
1071 for (int j = 0; j < decls_.length(); ++j) { | 1065 for (Declaration* decl : decls_) { |
1072 if (decls_[j]->proxy()->raw_name() == name) { | 1066 if (decl->proxy()->raw_name() == name) return decl; |
1073 return decls_[j]; | |
1074 } | |
1075 } | 1067 } |
1076 DCHECK(false); | 1068 DCHECK(false); |
1077 } | 1069 } |
1078 } | 1070 } |
1079 return nullptr; | 1071 return nullptr; |
1080 } | 1072 } |
1081 | 1073 |
1082 void DeclarationScope::AllocateVariables(ParseInfo* info, AnalyzeMode mode) { | 1074 void DeclarationScope::AllocateVariables(ParseInfo* info, AnalyzeMode mode) { |
1083 // Module variables must be allocated before variable resolution | 1075 // Module variables must be allocated before variable resolution |
1084 // to ensure that AccessNeedsHoleCheck() can detect import variables. | 1076 // to ensure that AccessNeedsHoleCheck() can detect import variables. |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1238 non_locals = StringSet::Add(non_locals, proxy->name()); | 1230 non_locals = StringSet::Add(non_locals, proxy->name()); |
1239 } | 1231 } |
1240 return non_locals; | 1232 return non_locals; |
1241 } | 1233 } |
1242 | 1234 |
1243 void DeclarationScope::ResetAfterPreparsing(AstValueFactory* ast_value_factory, | 1235 void DeclarationScope::ResetAfterPreparsing(AstValueFactory* ast_value_factory, |
1244 bool aborted) { | 1236 bool aborted) { |
1245 DCHECK(is_function_scope()); | 1237 DCHECK(is_function_scope()); |
1246 | 1238 |
1247 // Reset all non-trivial members. | 1239 // Reset all non-trivial members. |
1248 decls_.Rewind(0); | 1240 decls_.Clear(); |
1249 locals_.Rewind(0); | 1241 locals_.Rewind(0); |
1250 sloppy_block_function_map_.Clear(); | 1242 sloppy_block_function_map_.Clear(); |
1251 variables_.Clear(); | 1243 variables_.Clear(); |
1252 // Make sure we won't walk the scope tree from here on. | 1244 // Make sure we won't walk the scope tree from here on. |
1253 inner_scope_ = nullptr; | 1245 inner_scope_ = nullptr; |
1254 unresolved_ = nullptr; | 1246 unresolved_ = nullptr; |
1255 | 1247 |
1256 // TODO(verwaest): We should properly preparse the parameters (no declarations | 1248 // TODO(verwaest): We should properly preparse the parameters (no declarations |
1257 // should be created), and reparse on abort. | 1249 // should be created), and reparse on abort. |
1258 if (aborted) { | 1250 if (aborted) { |
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2025 Variable* function = | 2017 Variable* function = |
2026 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 2018 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
2027 bool is_function_var_in_context = | 2019 bool is_function_var_in_context = |
2028 function != nullptr && function->IsContextSlot(); | 2020 function != nullptr && function->IsContextSlot(); |
2029 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 2021 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
2030 (is_function_var_in_context ? 1 : 0); | 2022 (is_function_var_in_context ? 1 : 0); |
2031 } | 2023 } |
2032 | 2024 |
2033 } // namespace internal | 2025 } // namespace internal |
2034 } // namespace v8 | 2026 } // namespace v8 |
OLD | NEW |