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

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

Issue 2193793002: Put Scopes into temporary Zone (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: . Created 4 years, 4 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
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 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 return false; 273 return false;
274 } 274 }
275 } 275 }
276 276
277 #ifdef DEBUG 277 #ifdef DEBUG
278 if (info->script_is_native() ? FLAG_print_builtin_scopes 278 if (info->script_is_native() ? FLAG_print_builtin_scopes
279 : FLAG_print_scopes) { 279 : FLAG_print_scopes) {
280 scope->Print(); 280 scope->Print();
281 } 281 }
282 scope->CheckScopePositions(); 282 scope->CheckScopePositions();
283 scope->CheckZones();
283 #endif 284 #endif
284 285
285 info->set_scope(scope); 286 info->set_scope(scope);
286 return true; 287 return true;
287 } 288 }
288 289
289 void Scope::DeclareThis(AstValueFactory* ast_value_factory) { 290 void Scope::DeclareThis(AstValueFactory* ast_value_factory) {
290 DCHECK(!already_resolved()); 291 DCHECK(!already_resolved());
291 DCHECK(is_declaration_scope()); 292 DCHECK(is_declaration_scope());
292 DCHECK(has_this_declaration()); 293 DCHECK(has_this_declaration());
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 } else if (!scope_info_.is_null()) { 499 } else if (!scope_info_.is_null()) {
499 // If we are backed by a scope info, try to lookup the variable there. 500 // If we are backed by a scope info, try to lookup the variable there.
500 VariableMode mode; 501 VariableMode mode;
501 int index = scope_info_->FunctionContextSlotIndex(*(name->string()), &mode); 502 int index = scope_info_->FunctionContextSlotIndex(*(name->string()), &mode);
502 if (index < 0) return NULL; 503 if (index < 0) return NULL;
503 Variable* var = new (zone()) 504 Variable* var = new (zone())
504 Variable(this, name, mode, Variable::NORMAL, kCreatedInitialized); 505 Variable(this, name, mode, Variable::NORMAL, kCreatedInitialized);
505 VariableProxy* proxy = factory->NewVariableProxy(var); 506 VariableProxy* proxy = factory->NewVariableProxy(var);
506 VariableDeclaration* declaration = 507 VariableDeclaration* declaration =
507 factory->NewVariableDeclaration(proxy, mode, this, kNoSourcePosition); 508 factory->NewVariableDeclaration(proxy, mode, this, kNoSourcePosition);
509 DCHECK_EQ(factory->zone(), zone());
508 DeclareFunctionVar(declaration); 510 DeclareFunctionVar(declaration);
509 var->AllocateTo(VariableLocation::CONTEXT, index); 511 var->AllocateTo(VariableLocation::CONTEXT, index);
510 return var; 512 return var;
511 } else { 513 } else {
512 return NULL; 514 return NULL;
513 } 515 }
514 } 516 }
515 517
516 518
517 Variable* Scope::Lookup(const AstRawString* name) { 519 Variable* Scope::Lookup(const AstRawString* name) {
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
883 if (proxy->is_resolved() && proxy->var()->IsStackAllocated()) continue; 885 if (proxy->is_resolved() && proxy->var()->IsStackAllocated()) continue;
884 Handle<String> name = proxy->name(); 886 Handle<String> name = proxy->name();
885 non_locals = StringSet::Add(non_locals, name); 887 non_locals = StringSet::Add(non_locals, name);
886 } 888 }
887 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { 889 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) {
888 non_locals = scope->CollectNonLocals(non_locals); 890 non_locals = scope->CollectNonLocals(non_locals);
889 } 891 }
890 return non_locals; 892 return non_locals;
891 } 893 }
892 894
895 void Scope::CollectUnresolvableLocals(VariableProxy** still_unresolved,
896 Scope* max_outer_scope) {
897 BindingKind binding_kind;
898 for (VariableProxy *proxy = unresolved_, *next = nullptr; proxy != nullptr;
899 proxy = next) {
900 next = proxy->next_unresolved();
901 // Note that we pass nullptr as AstNodeFactory: this phase should not create
902 // any new AstNodes, since none of the Scopes involved are backed up by
adamk 2016/07/29 22:12:51 Is there an obvious place to add a DCHECK for this
marja 2016/08/01 11:02:50 I added a DCHECK_NOT_NULL(factory); statement in L
903 // ScopeInfo.
904 if (LookupRecursive(proxy, &binding_kind, nullptr, max_outer_scope) ==
905 nullptr) {
906 proxy->set_next_unresolved(*still_unresolved);
907 *still_unresolved = proxy;
908 }
909 }
910
911 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) {
912 scope->CollectUnresolvableLocals(still_unresolved, max_outer_scope);
913 }
914 }
915
916 void Scope::AnalyzePartially(Scope* migrate_to,
917 AstNodeFactory* ast_node_factory) {
918 // Try to resolve unresolved variables for this Scope and collect those which
919 // cannot be resolved inside. It doesn't make sense to try to resolve them in
920 // the outer Scopes here, because they are incomplete.
921 VariableProxy* still_unresolved = nullptr;
922 CollectUnresolvableLocals(&still_unresolved, this);
923
924 // Re-create the VariableProxies in the right Zone and insert them into
925 // migrate_to.
926 for (VariableProxy* proxy = still_unresolved; proxy != nullptr;
927 proxy = proxy->next_unresolved()) {
928 migrate_to->NewUnresolved(
929 ast_node_factory, proxy->raw_name(),
930 proxy->is_this() ? Variable::THIS : Variable::NORMAL, proxy->position(),
adamk 2016/07/29 22:12:51 I had to go read some code to understand why you d
marja 2016/08/01 11:02:50 Yeah, this is not good, and it's also nontrivial t
931 proxy->end_position());
932 }
933
934 // Gather info from inner scopes...
935 PropagateScopeInfo(false);
adamk 2016/07/29 22:12:51 This is called before variable resolution in Scope
marja 2016/08/01 11:02:50 I moved this call up, just to keep it more unified
936
937 // ... and push it up to migrate_to. Note that migrate_to and this Scope
938 // describe the same Scope, just in different Zones.
939 PropagateUsageFlagsToScope(migrate_to);
940 if (inner_scope_calls_eval_) {
941 migrate_to->inner_scope_calls_eval_ = true;
942 }
943 DCHECK(!force_eager_compilation_);
944 migrate_to->set_start_position(start_position_);
945 migrate_to->set_end_position(end_position_);
946 migrate_to->language_mode_ = language_mode_;
adamk 2016/07/29 22:12:51 One thing I'd recommend before submitting would be
marja 2016/08/01 11:02:50 Done: I didn't have these things from the start, a
947 outer_scope_->RemoveInnerScope(this);
948 DCHECK_EQ(outer_scope_, migrate_to->outer_scope_);
949 DCHECK_EQ(outer_scope_->zone(), migrate_to->zone());
950 }
893 951
894 #ifdef DEBUG 952 #ifdef DEBUG
895 static const char* Header(ScopeType scope_type, FunctionKind function_kind, 953 static const char* Header(ScopeType scope_type, FunctionKind function_kind,
896 bool is_declaration_scope) { 954 bool is_declaration_scope) {
897 switch (scope_type) { 955 switch (scope_type) {
898 case EVAL_SCOPE: return "eval"; 956 case EVAL_SCOPE: return "eval";
899 // TODO(adamk): Should we print concise method scopes specially? 957 // TODO(adamk): Should we print concise method scopes specially?
900 case FUNCTION_SCOPE: 958 case FUNCTION_SCOPE:
901 if (IsGeneratorFunction(function_kind)) return "function*"; 959 if (IsGeneratorFunction(function_kind)) return "function*";
902 if (IsAsyncFunction(function_kind)) return "async function"; 960 if (IsAsyncFunction(function_kind)) return "async function";
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
1091 // A scope is allowed to have invalid positions if it is hidden and has no 1149 // A scope is allowed to have invalid positions if it is hidden and has no
1092 // inner scopes 1150 // inner scopes
1093 if (!is_hidden() && inner_scope_ == nullptr) { 1151 if (!is_hidden() && inner_scope_ == nullptr) {
1094 CHECK_NE(kNoSourcePosition, start_position()); 1152 CHECK_NE(kNoSourcePosition, start_position());
1095 CHECK_NE(kNoSourcePosition, end_position()); 1153 CHECK_NE(kNoSourcePosition, end_position());
1096 } 1154 }
1097 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { 1155 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) {
1098 scope->CheckScopePositions(); 1156 scope->CheckScopePositions();
1099 } 1157 }
1100 } 1158 }
1159
1160 void Scope::CheckZones() {
1161 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) {
1162 CHECK_EQ(scope->zone(), zone());
1163 }
1164 }
1101 #endif // DEBUG 1165 #endif // DEBUG
1102 1166
1103 1167
1104 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) { 1168 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) {
1105 if (dynamics_ == NULL) dynamics_ = new (zone()) DynamicScopePart(zone()); 1169 if (dynamics_ == NULL) dynamics_ = new (zone()) DynamicScopePart(zone());
1106 VariableMap* map = dynamics_->GetMap(mode); 1170 VariableMap* map = dynamics_->GetMap(mode);
1107 Variable* var = map->Lookup(name); 1171 Variable* var = map->Lookup(name);
1108 if (var == NULL) { 1172 if (var == NULL) {
1109 // Declare a new non-local. 1173 // Declare a new non-local.
1110 InitializationFlag init_flag = (mode == VAR) 1174 InitializationFlag init_flag = (mode == VAR)
1111 ? kCreatedInitialized : kNeedsInitialization; 1175 ? kCreatedInitialized : kNeedsInitialization;
1112 var = map->Declare(NULL, 1176 var = map->Declare(NULL,
1113 name, 1177 name,
1114 mode, 1178 mode,
1115 Variable::NORMAL, 1179 Variable::NORMAL,
1116 init_flag); 1180 init_flag);
1117 // Allocate it by giving it a dynamic lookup. 1181 // Allocate it by giving it a dynamic lookup.
1118 var->AllocateTo(VariableLocation::LOOKUP, -1); 1182 var->AllocateTo(VariableLocation::LOOKUP, -1);
1119 } 1183 }
1120 return var; 1184 return var;
1121 } 1185 }
1122 1186
1123
1124 Variable* Scope::LookupRecursive(VariableProxy* proxy, 1187 Variable* Scope::LookupRecursive(VariableProxy* proxy,
1125 BindingKind* binding_kind, 1188 BindingKind* binding_kind,
1126 AstNodeFactory* factory) { 1189 AstNodeFactory* factory,
1190 Scope* max_outer_scope) {
1127 DCHECK(binding_kind != NULL); 1191 DCHECK(binding_kind != NULL);
1128 if (already_resolved() && is_with_scope()) { 1192 if (already_resolved() && is_with_scope()) {
1129 // Short-cut: if the scope is deserialized from a scope info, variable 1193 // Short-cut: if the scope is deserialized from a scope info, variable
1130 // allocation is already fixed. We can simply return with dynamic lookup. 1194 // allocation is already fixed. We can simply return with dynamic lookup.
1131 *binding_kind = DYNAMIC_LOOKUP; 1195 *binding_kind = DYNAMIC_LOOKUP;
1132 return NULL; 1196 return NULL;
1133 } 1197 }
1134 1198
1135 // Try to find the variable in this scope. 1199 // Try to find the variable in this scope.
1136 Variable* var = LookupLocal(proxy->raw_name()); 1200 Variable* var = LookupLocal(proxy->raw_name());
1137 1201
1138 // We found a variable and we are done. (Even if there is an 'eval' in 1202 // We found a variable and we are done. (Even if there is an 'eval' in
1139 // this scope which introduces the same variable again, the resulting 1203 // this scope which introduces the same variable again, the resulting
1140 // variable remains the same.) 1204 // variable remains the same.)
1141 if (var != NULL) { 1205 if (var != NULL) {
1142 *binding_kind = BOUND; 1206 *binding_kind = BOUND;
1143 return var; 1207 return var;
1144 } 1208 }
1145 1209
1146 // We did not find a variable locally. Check against the function variable, 1210 // We did not find a variable locally. Check against the function variable,
1147 // if any. We can do this for all scopes, since the function variable is 1211 // if any. We can do this for all scopes, since the function variable is
1148 // only present - if at all - for function scopes. 1212 // only present - if at all - for function scopes.
1149 *binding_kind = UNBOUND; 1213 *binding_kind = UNBOUND;
1150 var = LookupFunctionVar(proxy->raw_name(), factory); 1214 var = LookupFunctionVar(proxy->raw_name(), factory);
1151 if (var != NULL) { 1215 if (var != NULL) {
1152 *binding_kind = BOUND; 1216 *binding_kind = BOUND;
1153 } else if (outer_scope_ != NULL) { 1217 } else if (outer_scope_ != nullptr && this != max_outer_scope) {
1154 var = outer_scope_->LookupRecursive(proxy, binding_kind, factory); 1218 var = outer_scope_->LookupRecursive(proxy, binding_kind, factory,
1219 max_outer_scope);
1155 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { 1220 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) {
1156 var->ForceContextAllocation(); 1221 var->ForceContextAllocation();
1157 } 1222 }
1158 } else { 1223 } else {
1159 DCHECK(is_script_scope()); 1224 DCHECK(is_script_scope() || this == max_outer_scope);
1160 } 1225 }
1161 1226
1162 // "this" can't be shadowed by "eval"-introduced bindings or by "with" scopes. 1227 // "this" can't be shadowed by "eval"-introduced bindings or by "with" scopes.
1163 // TODO(wingo): There are other variables in this category; add them. 1228 // TODO(wingo): There are other variables in this category; add them.
1164 bool name_can_be_shadowed = var == nullptr || !var->is_this(); 1229 bool name_can_be_shadowed = var == nullptr || !var->is_this();
1165 1230
1166 if (is_with_scope() && name_can_be_shadowed) { 1231 if (is_with_scope() && name_can_be_shadowed) {
1167 DCHECK(!already_resolved()); 1232 DCHECK(!already_resolved());
1168 // The current scope is a with scope, so the variable binding can not be 1233 // The current scope is a with scope, so the variable binding can not be
1169 // statically resolved. However, note that it was necessary to do a lookup 1234 // statically resolved. However, note that it was necessary to do a lookup
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
1577 function_ != NULL && function_->proxy()->var()->IsContextSlot(); 1642 function_ != NULL && function_->proxy()->var()->IsContextSlot();
1578 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - 1643 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() -
1579 (is_function_var_in_context ? 1 : 0); 1644 (is_function_var_in_context ? 1 : 0);
1580 } 1645 }
1581 1646
1582 1647
1583 int Scope::ContextGlobalCount() const { return num_global_slots(); } 1648 int Scope::ContextGlobalCount() const { return num_global_slots(); }
1584 1649
1585 } // namespace internal 1650 } // namespace internal
1586 } // namespace v8 1651 } // namespace v8
OLDNEW
« no previous file with comments | « src/ast/scopes.h ('k') | src/parsing/parser.h » ('j') | src/parsing/parser.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698