OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 Scope* scope = info->function()->scope(); | 267 Scope* scope = info->function()->scope(); |
268 Scope* top = scope; | 268 Scope* top = scope; |
269 | 269 |
270 // Traverse the scope tree up to the first unresolved scope or the global | 270 // Traverse the scope tree up to the first unresolved scope or the global |
271 // scope and start scope resolution and variable allocation from that scope. | 271 // scope and start scope resolution and variable allocation from that scope. |
272 while (!top->is_global_scope() && | 272 while (!top->is_global_scope() && |
273 !top->outer_scope()->already_resolved()) { | 273 !top->outer_scope()->already_resolved()) { |
274 top = top->outer_scope(); | 274 top = top->outer_scope(); |
275 } | 275 } |
276 | 276 |
277 // Allocated the variables. | 277 // Allocate the variables. |
278 top->AllocateVariables(info->global_scope()); | 278 { |
| 279 AstNodeFactory ast_node_factory(info->isolate()); |
| 280 top->AllocateVariables(info->global_scope(), &ast_node_factory); |
| 281 } |
279 | 282 |
280 #ifdef DEBUG | 283 #ifdef DEBUG |
281 if (info->isolate()->bootstrapper()->IsActive() | 284 if (info->isolate()->bootstrapper()->IsActive() |
282 ? FLAG_print_builtin_scopes | 285 ? FLAG_print_builtin_scopes |
283 : FLAG_print_scopes) { | 286 : FLAG_print_scopes) { |
284 scope->Print(); | 287 scope->Print(); |
285 } | 288 } |
286 #endif | 289 #endif |
287 | 290 |
288 if (FLAG_harmony_scoping) { | 291 if (FLAG_harmony_scoping) { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 name, | 413 name, |
411 mode, | 414 mode, |
412 true, | 415 true, |
413 Variable::NORMAL, | 416 Variable::NORMAL, |
414 init_flag); | 417 init_flag); |
415 var->AllocateTo(Variable::CONTEXT, index); | 418 var->AllocateTo(Variable::CONTEXT, index); |
416 return var; | 419 return var; |
417 } | 420 } |
418 | 421 |
419 | 422 |
420 Variable* Scope::LookupFunctionVar(Handle<String> name) { | 423 Variable* Scope::LookupFunctionVar(Handle<String> name, |
| 424 AstNodeFactory* factory) { |
421 if (function_ != NULL && function_->name().is_identical_to(name)) { | 425 if (function_ != NULL && function_->name().is_identical_to(name)) { |
422 return function_->var(); | 426 return function_->var(); |
423 } else if (!scope_info_.is_null()) { | 427 } else if (!scope_info_.is_null()) { |
424 // If we are backed by a scope info, try to lookup the variable there. | 428 // If we are backed by a scope info, try to lookup the variable there. |
425 VariableMode mode; | 429 VariableMode mode; |
426 int index = scope_info_->FunctionContextSlotIndex(*name, &mode); | 430 int index = scope_info_->FunctionContextSlotIndex(*name, &mode); |
427 if (index < 0) return NULL; | 431 if (index < 0) return NULL; |
428 Variable* var = DeclareFunctionVar(name, mode); | 432 Variable* var = DeclareFunctionVar(name, mode, factory); |
429 var->AllocateTo(Variable::CONTEXT, index); | 433 var->AllocateTo(Variable::CONTEXT, index); |
430 return var; | 434 return var; |
431 } else { | 435 } else { |
432 return NULL; | 436 return NULL; |
433 } | 437 } |
434 } | 438 } |
435 | 439 |
436 | 440 |
437 Variable* Scope::Lookup(Handle<String> name) { | 441 Variable* Scope::Lookup(Handle<String> name) { |
438 for (Scope* scope = this; | 442 for (Scope* scope = this; |
439 scope != NULL; | 443 scope != NULL; |
440 scope = scope->outer_scope()) { | 444 scope = scope->outer_scope()) { |
441 Variable* var = scope->LocalLookup(name); | 445 Variable* var = scope->LocalLookup(name); |
442 if (var != NULL) return var; | 446 if (var != NULL) return var; |
443 } | 447 } |
444 return NULL; | 448 return NULL; |
445 } | 449 } |
446 | 450 |
447 | 451 |
448 Variable* Scope::DeclareFunctionVar(Handle<String> name, VariableMode mode) { | 452 Variable* Scope::DeclareFunctionVar(Handle<String> name, |
| 453 VariableMode mode, |
| 454 AstNodeFactory* factory) { |
449 ASSERT(is_function_scope() && function_ == NULL); | 455 ASSERT(is_function_scope() && function_ == NULL); |
450 Variable* function_var = new Variable( | 456 Variable* function_var = new Variable( |
451 this, name, mode, true, Variable::NORMAL, kCreatedInitialized); | 457 this, name, mode, true, Variable::NORMAL, kCreatedInitialized); |
452 function_ = new(isolate_->zone()) VariableProxy(isolate_, function_var); | 458 function_ = factory->NewVariableProxy(function_var); |
453 return function_var; | 459 return function_var; |
454 } | 460 } |
455 | 461 |
456 | 462 |
457 void Scope::DeclareParameter(Handle<String> name, VariableMode mode) { | 463 void Scope::DeclareParameter(Handle<String> name, VariableMode mode) { |
458 ASSERT(!already_resolved()); | 464 ASSERT(!already_resolved()); |
459 ASSERT(is_function_scope()); | 465 ASSERT(is_function_scope()); |
460 Variable* var = variables_.Declare( | 466 Variable* var = variables_.Declare( |
461 this, name, mode, true, Variable::NORMAL, kCreatedInitialized); | 467 this, name, mode, true, Variable::NORMAL, kCreatedInitialized); |
462 params_.Add(var); | 468 params_.Add(var); |
(...skipping 21 matching lines...) Expand all Loading... |
484 ASSERT(is_global_scope()); | 490 ASSERT(is_global_scope()); |
485 return variables_.Declare(this, | 491 return variables_.Declare(this, |
486 name, | 492 name, |
487 DYNAMIC_GLOBAL, | 493 DYNAMIC_GLOBAL, |
488 true, | 494 true, |
489 Variable::NORMAL, | 495 Variable::NORMAL, |
490 kCreatedInitialized); | 496 kCreatedInitialized); |
491 } | 497 } |
492 | 498 |
493 | 499 |
494 VariableProxy* Scope::NewUnresolved(Handle<String> name, int position) { | 500 VariableProxy* Scope::NewUnresolved(AstNodeFactory* factory, |
| 501 Handle<String> name, |
| 502 int position) { |
495 // Note that we must not share the unresolved variables with | 503 // Note that we must not share the unresolved variables with |
496 // the same name because they may be removed selectively via | 504 // the same name because they may be removed selectively via |
497 // RemoveUnresolved(). | 505 // RemoveUnresolved(). |
498 ASSERT(!already_resolved()); | 506 ASSERT(!already_resolved()); |
499 VariableProxy* proxy = new(isolate_->zone()) VariableProxy( | 507 VariableProxy* proxy = factory->NewVariableProxy(name, false, position); |
500 isolate_, name, false, position); | |
501 unresolved_.Add(proxy); | 508 unresolved_.Add(proxy); |
502 return proxy; | 509 return proxy; |
503 } | 510 } |
504 | 511 |
505 | 512 |
506 void Scope::RemoveUnresolved(VariableProxy* var) { | 513 void Scope::RemoveUnresolved(VariableProxy* var) { |
507 // Most likely (always?) any variable we want to remove | 514 // Most likely (always?) any variable we want to remove |
508 // was just added before, so we search backwards. | 515 // was just added before, so we search backwards. |
509 for (int i = unresolved_.length(); i-- > 0;) { | 516 for (int i = unresolved_.length(); i-- > 0;) { |
510 if (unresolved_[i] == var) { | 517 if (unresolved_[i] == var) { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 if (var->IsStackLocal()) { | 625 if (var->IsStackLocal()) { |
619 stack_locals->Add(var); | 626 stack_locals->Add(var); |
620 } else if (var->IsContextSlot()) { | 627 } else if (var->IsContextSlot()) { |
621 context_locals->Add(var); | 628 context_locals->Add(var); |
622 } | 629 } |
623 } | 630 } |
624 } | 631 } |
625 } | 632 } |
626 | 633 |
627 | 634 |
628 void Scope::AllocateVariables(Scope* global_scope) { | 635 void Scope::AllocateVariables(Scope* global_scope, AstNodeFactory* factory) { |
629 // 1) Propagate scope information. | 636 // 1) Propagate scope information. |
630 bool outer_scope_calls_non_strict_eval = false; | 637 bool outer_scope_calls_non_strict_eval = false; |
631 if (outer_scope_ != NULL) { | 638 if (outer_scope_ != NULL) { |
632 outer_scope_calls_non_strict_eval = | 639 outer_scope_calls_non_strict_eval = |
633 outer_scope_->outer_scope_calls_non_strict_eval() | | 640 outer_scope_->outer_scope_calls_non_strict_eval() | |
634 outer_scope_->calls_non_strict_eval(); | 641 outer_scope_->calls_non_strict_eval(); |
635 } | 642 } |
636 PropagateScopeInfo(outer_scope_calls_non_strict_eval); | 643 PropagateScopeInfo(outer_scope_calls_non_strict_eval); |
637 | 644 |
638 // 2) Resolve variables. | 645 // 2) Resolve variables. |
639 ResolveVariablesRecursively(global_scope); | 646 ResolveVariablesRecursively(global_scope, factory); |
640 | 647 |
641 // 3) Allocate variables. | 648 // 3) Allocate variables. |
642 AllocateVariablesRecursively(); | 649 AllocateVariablesRecursively(); |
643 } | 650 } |
644 | 651 |
645 | 652 |
646 bool Scope::AllowsLazyCompilation() const { | 653 bool Scope::AllowsLazyCompilation() const { |
647 return !force_eager_compilation_ && HasTrivialOuterContext(); | 654 return !force_eager_compilation_ && HasTrivialOuterContext(); |
648 } | 655 } |
649 | 656 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
892 Variable::NORMAL, | 899 Variable::NORMAL, |
893 init_flag); | 900 init_flag); |
894 // Allocate it by giving it a dynamic lookup. | 901 // Allocate it by giving it a dynamic lookup. |
895 var->AllocateTo(Variable::LOOKUP, -1); | 902 var->AllocateTo(Variable::LOOKUP, -1); |
896 } | 903 } |
897 return var; | 904 return var; |
898 } | 905 } |
899 | 906 |
900 | 907 |
901 Variable* Scope::LookupRecursive(Handle<String> name, | 908 Variable* Scope::LookupRecursive(Handle<String> name, |
902 BindingKind* binding_kind) { | 909 BindingKind* binding_kind, |
| 910 AstNodeFactory* factory) { |
903 ASSERT(binding_kind != NULL); | 911 ASSERT(binding_kind != NULL); |
904 // Try to find the variable in this scope. | 912 // Try to find the variable in this scope. |
905 Variable* var = LocalLookup(name); | 913 Variable* var = LocalLookup(name); |
906 | 914 |
907 // We found a variable and we are done. (Even if there is an 'eval' in | 915 // We found a variable and we are done. (Even if there is an 'eval' in |
908 // this scope which introduces the same variable again, the resulting | 916 // this scope which introduces the same variable again, the resulting |
909 // variable remains the same.) | 917 // variable remains the same.) |
910 if (var != NULL) { | 918 if (var != NULL) { |
911 *binding_kind = BOUND; | 919 *binding_kind = BOUND; |
912 return var; | 920 return var; |
913 } | 921 } |
914 | 922 |
915 // We did not find a variable locally. Check against the function variable, | 923 // We did not find a variable locally. Check against the function variable, |
916 // if any. We can do this for all scopes, since the function variable is | 924 // if any. We can do this for all scopes, since the function variable is |
917 // only present - if at all - for function scopes. | 925 // only present - if at all - for function scopes. |
918 *binding_kind = UNBOUND; | 926 *binding_kind = UNBOUND; |
919 var = LookupFunctionVar(name); | 927 var = LookupFunctionVar(name, factory); |
920 if (var != NULL) { | 928 if (var != NULL) { |
921 *binding_kind = BOUND; | 929 *binding_kind = BOUND; |
922 } else if (outer_scope_ != NULL) { | 930 } else if (outer_scope_ != NULL) { |
923 var = outer_scope_->LookupRecursive(name, binding_kind); | 931 var = outer_scope_->LookupRecursive(name, binding_kind, factory); |
924 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { | 932 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { |
925 var->ForceContextAllocation(); | 933 var->ForceContextAllocation(); |
926 } | 934 } |
927 } else { | 935 } else { |
928 ASSERT(is_global_scope()); | 936 ASSERT(is_global_scope()); |
929 } | 937 } |
930 | 938 |
931 if (is_with_scope()) { | 939 if (is_with_scope()) { |
932 // The current scope is a with scope, so the variable binding can not be | 940 // The current scope is a with scope, so the variable binding can not be |
933 // statically resolved. However, note that it was necessary to do a lookup | 941 // statically resolved. However, note that it was necessary to do a lookup |
(...skipping 12 matching lines...) Expand all Loading... |
946 *binding_kind = BOUND_EVAL_SHADOWED; | 954 *binding_kind = BOUND_EVAL_SHADOWED; |
947 } else if (*binding_kind == UNBOUND) { | 955 } else if (*binding_kind == UNBOUND) { |
948 *binding_kind = UNBOUND_EVAL_SHADOWED; | 956 *binding_kind = UNBOUND_EVAL_SHADOWED; |
949 } | 957 } |
950 } | 958 } |
951 return var; | 959 return var; |
952 } | 960 } |
953 | 961 |
954 | 962 |
955 void Scope::ResolveVariable(Scope* global_scope, | 963 void Scope::ResolveVariable(Scope* global_scope, |
956 VariableProxy* proxy) { | 964 VariableProxy* proxy, |
| 965 AstNodeFactory* factory) { |
957 ASSERT(global_scope == NULL || global_scope->is_global_scope()); | 966 ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
958 | 967 |
959 // If the proxy is already resolved there's nothing to do | 968 // If the proxy is already resolved there's nothing to do |
960 // (functions and consts may be resolved by the parser). | 969 // (functions and consts may be resolved by the parser). |
961 if (proxy->var() != NULL) return; | 970 if (proxy->var() != NULL) return; |
962 | 971 |
963 // Otherwise, try to resolve the variable. | 972 // Otherwise, try to resolve the variable. |
964 BindingKind binding_kind; | 973 BindingKind binding_kind; |
965 Variable* var = LookupRecursive(proxy->name(), &binding_kind); | 974 Variable* var = LookupRecursive(proxy->name(), &binding_kind, factory); |
966 switch (binding_kind) { | 975 switch (binding_kind) { |
967 case BOUND: | 976 case BOUND: |
968 // We found a variable binding. | 977 // We found a variable binding. |
969 break; | 978 break; |
970 | 979 |
971 case BOUND_EVAL_SHADOWED: | 980 case BOUND_EVAL_SHADOWED: |
972 // We found a variable variable binding that might be shadowed | 981 // We found a variable variable binding that might be shadowed |
973 // by 'eval' introduced variable bindings. | 982 // by 'eval' introduced variable bindings. |
974 if (var->is_global()) { | 983 if (var->is_global()) { |
975 var = NonLocal(proxy->name(), DYNAMIC_GLOBAL); | 984 var = NonLocal(proxy->name(), DYNAMIC_GLOBAL); |
(...skipping 20 matching lines...) Expand all Loading... |
996 // The variable could not be resolved statically. | 1005 // The variable could not be resolved statically. |
997 var = NonLocal(proxy->name(), DYNAMIC); | 1006 var = NonLocal(proxy->name(), DYNAMIC); |
998 break; | 1007 break; |
999 } | 1008 } |
1000 | 1009 |
1001 ASSERT(var != NULL); | 1010 ASSERT(var != NULL); |
1002 proxy->BindTo(var); | 1011 proxy->BindTo(var); |
1003 } | 1012 } |
1004 | 1013 |
1005 | 1014 |
1006 void Scope::ResolveVariablesRecursively(Scope* global_scope) { | 1015 void Scope::ResolveVariablesRecursively(Scope* global_scope, |
| 1016 AstNodeFactory* factory) { |
1007 ASSERT(global_scope == NULL || global_scope->is_global_scope()); | 1017 ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
1008 | 1018 |
1009 // Resolve unresolved variables for this scope. | 1019 // Resolve unresolved variables for this scope. |
1010 for (int i = 0; i < unresolved_.length(); i++) { | 1020 for (int i = 0; i < unresolved_.length(); i++) { |
1011 ResolveVariable(global_scope, unresolved_[i]); | 1021 ResolveVariable(global_scope, unresolved_[i], factory); |
1012 } | 1022 } |
1013 | 1023 |
1014 // Resolve unresolved variables for inner scopes. | 1024 // Resolve unresolved variables for inner scopes. |
1015 for (int i = 0; i < inner_scopes_.length(); i++) { | 1025 for (int i = 0; i < inner_scopes_.length(); i++) { |
1016 inner_scopes_[i]->ResolveVariablesRecursively(global_scope); | 1026 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, factory); |
1017 } | 1027 } |
1018 } | 1028 } |
1019 | 1029 |
1020 | 1030 |
1021 bool Scope::PropagateScopeInfo(bool outer_scope_calls_non_strict_eval ) { | 1031 bool Scope::PropagateScopeInfo(bool outer_scope_calls_non_strict_eval ) { |
1022 if (outer_scope_calls_non_strict_eval) { | 1032 if (outer_scope_calls_non_strict_eval) { |
1023 outer_scope_calls_non_strict_eval_ = true; | 1033 outer_scope_calls_non_strict_eval_ = true; |
1024 } | 1034 } |
1025 | 1035 |
1026 bool calls_non_strict_eval = | 1036 bool calls_non_strict_eval = |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1230 } | 1240 } |
1231 | 1241 |
1232 | 1242 |
1233 int Scope::ContextLocalCount() const { | 1243 int Scope::ContextLocalCount() const { |
1234 if (num_heap_slots() == 0) return 0; | 1244 if (num_heap_slots() == 0) return 0; |
1235 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1245 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1236 (function_ != NULL && function_->var()->IsContextSlot() ? 1 : 0); | 1246 (function_ != NULL && function_->var()->IsContextSlot() ? 1 : 0); |
1237 } | 1247 } |
1238 | 1248 |
1239 } } // namespace v8::internal | 1249 } } // namespace v8::internal |
OLD | NEW |