Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Side by Side Diff: src/ast/scopes.cc

Issue 2159573002: Turn the unresolved-references list into a linked list using the VariableProxies themselves (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Addressed comment Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ast/scopes.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "src/accessors.h" 7 #include "src/accessors.h"
8 #include "src/ast/scopeinfo.h" 8 #include "src/ast/scopeinfo.h"
9 #include "src/bootstrapper.h" 9 #include "src/bootstrapper.h"
10 #include "src/messages.h" 10 #include "src/messages.h"
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 81
82 // ---------------------------------------------------------------------------- 82 // ----------------------------------------------------------------------------
83 // Implementation of Scope 83 // Implementation of Scope
84 84
85 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, 85 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type,
86 AstValueFactory* ast_value_factory, FunctionKind function_kind) 86 AstValueFactory* ast_value_factory, FunctionKind function_kind)
87 : inner_scopes_(4, zone), 87 : inner_scopes_(4, zone),
88 variables_(zone), 88 variables_(zone),
89 temps_(4, zone), 89 temps_(4, zone),
90 params_(4, zone), 90 params_(4, zone),
91 unresolved_(16, zone), 91 unresolved_(nullptr),
92 decls_(4, zone), 92 decls_(4, zone),
93 module_descriptor_(scope_type == MODULE_SCOPE ? new (zone) 93 module_descriptor_(scope_type == MODULE_SCOPE ? new (zone)
94 ModuleDescriptor(zone) 94 ModuleDescriptor(zone)
95 : NULL), 95 : NULL),
96 sloppy_block_function_map_(zone), 96 sloppy_block_function_map_(zone),
97 already_resolved_(false), 97 already_resolved_(false),
98 ast_value_factory_(ast_value_factory), 98 ast_value_factory_(ast_value_factory),
99 zone_(zone) { 99 zone_(zone) {
100 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null(), 100 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null(),
101 function_kind); 101 function_kind);
102 // The outermost scope must be a script scope. 102 // The outermost scope must be a script scope.
103 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL); 103 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL);
104 } 104 }
105 105
106 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type, 106 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type,
107 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory) 107 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory)
108 : inner_scopes_(4, zone), 108 : inner_scopes_(4, zone),
109 variables_(zone), 109 variables_(zone),
110 temps_(4, zone), 110 temps_(4, zone),
111 params_(4, zone), 111 params_(4, zone),
112 unresolved_(16, zone), 112 unresolved_(nullptr),
adamk 2016/07/18 16:54:34 I wonder if it was this 16 that was causing excess
113 decls_(4, zone), 113 decls_(4, zone),
114 module_descriptor_(NULL), 114 module_descriptor_(NULL),
115 sloppy_block_function_map_(zone), 115 sloppy_block_function_map_(zone),
116 already_resolved_(true), 116 already_resolved_(true),
117 ast_value_factory_(value_factory), 117 ast_value_factory_(value_factory),
118 zone_(zone) { 118 zone_(zone) {
119 SetDefaults(scope_type, NULL, scope_info); 119 SetDefaults(scope_type, NULL, scope_info);
120 if (!scope_info.is_null()) { 120 if (!scope_info.is_null()) {
121 num_heap_slots_ = scope_info_->ContextLength(); 121 num_heap_slots_ = scope_info_->ContextLength();
122 } 122 }
123 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. 123 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context.
124 num_heap_slots_ = Max(num_heap_slots_, 124 num_heap_slots_ = Max(num_heap_slots_,
125 static_cast<int>(Context::MIN_CONTEXT_SLOTS)); 125 static_cast<int>(Context::MIN_CONTEXT_SLOTS));
126 AddInnerScope(inner_scope); 126 AddInnerScope(inner_scope);
127 } 127 }
128 128
129 Scope::Scope(Zone* zone, Scope* inner_scope, 129 Scope::Scope(Zone* zone, Scope* inner_scope,
130 const AstRawString* catch_variable_name, 130 const AstRawString* catch_variable_name,
131 AstValueFactory* value_factory) 131 AstValueFactory* value_factory)
132 : inner_scopes_(1, zone), 132 : inner_scopes_(1, zone),
133 variables_(zone), 133 variables_(zone),
134 temps_(0, zone), 134 temps_(0, zone),
135 params_(0, zone), 135 params_(0, zone),
136 unresolved_(0, zone), 136 unresolved_(nullptr),
137 decls_(0, zone), 137 decls_(0, zone),
138 module_descriptor_(NULL), 138 module_descriptor_(NULL),
139 sloppy_block_function_map_(zone), 139 sloppy_block_function_map_(zone),
140 already_resolved_(true), 140 already_resolved_(true),
141 ast_value_factory_(value_factory), 141 ast_value_factory_(value_factory),
142 zone_(zone) { 142 zone_(zone) {
143 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); 143 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null());
144 AddInnerScope(inner_scope); 144 AddInnerScope(inner_scope);
145 ++num_var_; 145 ++num_var_;
146 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; 146 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 346
347 // Remove this scope from outer scope. 347 // Remove this scope from outer scope.
348 outer_scope()->RemoveInnerScope(this); 348 outer_scope()->RemoveInnerScope(this);
349 349
350 // Reparent inner scopes. 350 // Reparent inner scopes.
351 for (int i = 0; i < inner_scopes_.length(); i++) { 351 for (int i = 0; i < inner_scopes_.length(); i++) {
352 outer_scope()->AddInnerScope(inner_scopes_[i]); 352 outer_scope()->AddInnerScope(inner_scopes_[i]);
353 } 353 }
354 354
355 // Move unresolved variables 355 // Move unresolved variables
356 for (int i = 0; i < unresolved_.length(); i++) { 356 VariableProxy* unresolved = unresolved_;
357 outer_scope()->unresolved_.Add(unresolved_[i], zone()); 357 if (outer_scope()->unresolved_ == nullptr) {
358 outer_scope()->unresolved_ = unresolved;
359 } else if (unresolved != nullptr) {
360 while (unresolved->next_unresolved() != nullptr) {
361 unresolved = unresolved->next_unresolved();
362 }
363 unresolved->set_next_unresolved(outer_scope()->unresolved_);
364 outer_scope()->unresolved_ = unresolved_;
358 } 365 }
359 366
360 PropagateUsageFlagsToScope(outer_scope_); 367 PropagateUsageFlagsToScope(outer_scope_);
361 368
362 return NULL; 369 return NULL;
363 } 370 }
364 371
365 372
366 void Scope::ReplaceOuterScope(Scope* outer) { 373 void Scope::ReplaceOuterScope(Scope* outer) {
367 DCHECK_NOT_NULL(outer); 374 DCHECK_NOT_NULL(outer);
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 DCHECK(is_script_scope()); 529 DCHECK(is_script_scope());
523 return variables_.Declare(this, 530 return variables_.Declare(this,
524 name, 531 name,
525 DYNAMIC_GLOBAL, 532 DYNAMIC_GLOBAL,
526 Variable::NORMAL, 533 Variable::NORMAL,
527 kCreatedInitialized); 534 kCreatedInitialized);
528 } 535 }
529 536
530 537
531 bool Scope::RemoveUnresolved(VariableProxy* var) { 538 bool Scope::RemoveUnresolved(VariableProxy* var) {
532 // Most likely (always?) any variable we want to remove 539 if (unresolved_ == var) {
533 // was just added before, so we search backwards. 540 unresolved_ = var->next_unresolved();
534 for (int i = unresolved_.length(); i-- > 0;) { 541 var->set_next_unresolved(nullptr);
535 if (unresolved_[i] == var) { 542 return true;
536 unresolved_.Remove(i); 543 }
544 VariableProxy* current = unresolved_;
545 while (current != nullptr) {
546 VariableProxy* next = current->next_unresolved();
547 if (var == next) {
548 current->set_next_unresolved(next->next_unresolved());
549 var->set_next_unresolved(nullptr);
537 return true; 550 return true;
538 } 551 }
552 current = next;
539 } 553 }
540 return false; 554 return false;
541 } 555 }
542 556
543 557
544 Variable* Scope::NewTemporary(const AstRawString* name) { 558 Variable* Scope::NewTemporary(const AstRawString* name) {
545 DCHECK(!already_resolved()); 559 DCHECK(!already_resolved());
546 Scope* scope = this->ClosureScope(); 560 Scope* scope = this->ClosureScope();
547 Variable* var = new(zone()) Variable(scope, 561 Variable* var = new(zone()) Variable(scope,
548 name, 562 name,
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 if (scope_info_.is_null()) { 829 if (scope_info_.is_null()) {
816 scope_info_ = ScopeInfo::Create(isolate, zone(), this); 830 scope_info_ = ScopeInfo::Create(isolate, zone(), this);
817 } 831 }
818 return scope_info_; 832 return scope_info_;
819 } 833 }
820 834
821 Handle<StringSet> Scope::CollectNonLocals(Handle<StringSet> non_locals) { 835 Handle<StringSet> Scope::CollectNonLocals(Handle<StringSet> non_locals) {
822 // Collect non-local variables referenced in the scope. 836 // Collect non-local variables referenced in the scope.
823 // TODO(yangguo): store non-local variables explicitly if we can no longer 837 // TODO(yangguo): store non-local variables explicitly if we can no longer
824 // rely on unresolved_ to find them. 838 // rely on unresolved_ to find them.
825 for (int i = 0; i < unresolved_.length(); i++) { 839 for (VariableProxy* proxy = unresolved_; proxy != nullptr;
826 VariableProxy* proxy = unresolved_[i]; 840 proxy = proxy->next_unresolved()) {
827 if (proxy->is_resolved() && proxy->var()->IsStackAllocated()) continue; 841 if (proxy->is_resolved() && proxy->var()->IsStackAllocated()) continue;
828 Handle<String> name = proxy->name(); 842 Handle<String> name = proxy->name();
829 non_locals = StringSet::Add(non_locals, name); 843 non_locals = StringSet::Add(non_locals, name);
830 } 844 }
831 for (int i = 0; i < inner_scopes_.length(); i++) { 845 for (int i = 0; i < inner_scopes_.length(); i++) {
832 non_locals = inner_scopes_[i]->CollectNonLocals(non_locals); 846 non_locals = inner_scopes_[i]->CollectNonLocals(non_locals);
833 } 847 }
834 return non_locals; 848 return non_locals;
835 } 849 }
836 850
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
1211 1225
1212 return true; 1226 return true;
1213 } 1227 }
1214 1228
1215 1229
1216 bool Scope::ResolveVariablesRecursively(ParseInfo* info, 1230 bool Scope::ResolveVariablesRecursively(ParseInfo* info,
1217 AstNodeFactory* factory) { 1231 AstNodeFactory* factory) {
1218 DCHECK(info->script_scope()->is_script_scope()); 1232 DCHECK(info->script_scope()->is_script_scope());
1219 1233
1220 // Resolve unresolved variables for this scope. 1234 // Resolve unresolved variables for this scope.
1221 for (int i = 0; i < unresolved_.length(); i++) { 1235 for (VariableProxy* proxy = unresolved_; proxy != nullptr;
1222 if (!ResolveVariable(info, unresolved_[i], factory)) return false; 1236 proxy = proxy->next_unresolved()) {
1237 if (!ResolveVariable(info, proxy, factory)) return false;
1223 } 1238 }
1224 1239
1225 // Resolve unresolved variables for inner scopes. 1240 // Resolve unresolved variables for inner scopes.
1226 for (int i = 0; i < inner_scopes_.length(); i++) { 1241 for (int i = 0; i < inner_scopes_.length(); i++) {
1227 if (!inner_scopes_[i]->ResolveVariablesRecursively(info, factory)) 1242 if (!inner_scopes_[i]->ResolveVariablesRecursively(info, factory))
1228 return false; 1243 return false;
1229 } 1244 }
1230 1245
1231 return true; 1246 return true;
1232 } 1247 }
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
1536 function_ != NULL && function_->proxy()->var()->IsContextSlot(); 1551 function_ != NULL && function_->proxy()->var()->IsContextSlot();
1537 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - 1552 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() -
1538 (is_function_var_in_context ? 1 : 0); 1553 (is_function_var_in_context ? 1 : 0);
1539 } 1554 }
1540 1555
1541 1556
1542 int Scope::ContextGlobalCount() const { return num_global_slots(); } 1557 int Scope::ContextGlobalCount() const { return num_global_slots(); }
1543 1558
1544 } // namespace internal 1559 } // namespace internal
1545 } // namespace v8 1560 } // namespace v8
OLDNEW
« no previous file with comments | « src/ast/scopes.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698