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

Side by Side Diff: src/scopes.cc

Issue 20459: Not sure what happened, but my revert did not get everything out. Fixing the... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « src/scopes.h ('k') | src/variables.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 receiver_(NULL), 132 receiver_(NULL),
133 function_(NULL), 133 function_(NULL),
134 arguments_(NULL), 134 arguments_(NULL),
135 arguments_shadow_(NULL), 135 arguments_shadow_(NULL),
136 illegal_redecl_(NULL), 136 illegal_redecl_(NULL),
137 scope_inside_with_(false), 137 scope_inside_with_(false),
138 scope_contains_with_(false), 138 scope_contains_with_(false),
139 scope_calls_eval_(false), 139 scope_calls_eval_(false),
140 outer_scope_calls_eval_(false), 140 outer_scope_calls_eval_(false),
141 inner_scope_calls_eval_(false), 141 inner_scope_calls_eval_(false),
142 outer_scope_is_eval_scope_(false),
142 force_eager_compilation_(false), 143 force_eager_compilation_(false),
143 num_stack_slots_(0), 144 num_stack_slots_(0),
144 num_heap_slots_(0) { 145 num_heap_slots_(0) {
145 // At some point we might want to provide outer scopes to 146 // At some point we might want to provide outer scopes to
146 // eval scopes (by walking the stack and reading the scope info). 147 // eval scopes (by walking the stack and reading the scope info).
147 // In that case, the ASSERT below needs to be adjusted. 148 // In that case, the ASSERT below needs to be adjusted.
148 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); 149 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
149 ASSERT(!HasIllegalRedeclaration()); 150 ASSERT(!HasIllegalRedeclaration());
150 } 151 }
151 152
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 306
306 void Scope::AllocateVariables() { 307 void Scope::AllocateVariables() {
307 ASSERT(outer_scope_ == NULL); // eval or global scopes only 308 ASSERT(outer_scope_ == NULL); // eval or global scopes only
308 309
309 // 1) Propagate scope information. 310 // 1) Propagate scope information.
310 // If we are in an eval scope, we may have other outer scopes about 311 // If we are in an eval scope, we may have other outer scopes about
311 // which we don't know anything at this point. Thus we must be conservative 312 // which we don't know anything at this point. Thus we must be conservative
312 // and assume they may invoke eval themselves. Eventually we could capture 313 // and assume they may invoke eval themselves. Eventually we could capture
313 // this information in the ScopeInfo and then use it here (by traversing 314 // this information in the ScopeInfo and then use it here (by traversing
314 // the call chain stack, at compile time). 315 // the call chain stack, at compile time).
315 PropagateScopeInfo(is_eval_scope()); 316 bool eval_scope = is_eval_scope();
317 PropagateScopeInfo(eval_scope, eval_scope);
316 318
317 // 2) Resolve variables. 319 // 2) Resolve variables.
318 Scope* global_scope = NULL; 320 Scope* global_scope = NULL;
319 if (is_global_scope()) global_scope = this; 321 if (is_global_scope()) global_scope = this;
320 ResolveVariablesRecursively(global_scope); 322 ResolveVariablesRecursively(global_scope);
321 323
322 // 3) Allocate variables. 324 // 3) Allocate variables.
323 AllocateVariablesRecursively(); 325 AllocateVariablesRecursively();
324 } 326 }
325 327
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 437
436 // Scope info. 438 // Scope info.
437 if (HasTrivialOuterContext()) { 439 if (HasTrivialOuterContext()) {
438 Indent(n1, "// scope has trivial outer context\n"); 440 Indent(n1, "// scope has trivial outer context\n");
439 } 441 }
440 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); 442 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
441 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); 443 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
442 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); 444 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
443 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n"); 445 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
444 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); 446 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
447 if (outer_scope_is_eval_scope_) {
448 Indent(n1, "// outer scope is 'eval' scope\n");
449 }
445 if (num_stack_slots_ > 0) { Indent(n1, "// "); 450 if (num_stack_slots_ > 0) { Indent(n1, "// ");
446 PrintF("%d stack slots\n", num_stack_slots_); } 451 PrintF("%d stack slots\n", num_stack_slots_); }
447 if (num_heap_slots_ > 0) { Indent(n1, "// "); 452 if (num_heap_slots_ > 0) { Indent(n1, "// ");
448 PrintF("%d heap slots\n", num_heap_slots_); } 453 PrintF("%d heap slots\n", num_heap_slots_); }
449 454
450 // Print locals. 455 // Print locals.
451 PrettyPrinter printer; 456 PrettyPrinter printer;
452 Indent(n1, "// function var\n"); 457 Indent(n1, "// function var\n");
453 if (function_ != NULL) { 458 if (function_ != NULL) {
454 PrintVar(&printer, n1, function_); 459 PrintVar(&printer, n1, function_);
(...skipping 20 matching lines...) Expand all
475 PrintF("\n"); 480 PrintF("\n");
476 inner_scopes_[i]->Print(n1); 481 inner_scopes_[i]->Print(n1);
477 } 482 }
478 } 483 }
479 484
480 Indent(n0, "}\n"); 485 Indent(n0, "}\n");
481 } 486 }
482 #endif // DEBUG 487 #endif // DEBUG
483 488
484 489
485 Variable* Scope::NonLocal(Handle<String> name) { 490 Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
486 // Space optimization: reuse existing non-local with the same name. 491 // Space optimization: reuse existing non-local with the same name
492 // and mode.
487 for (int i = 0; i < nonlocals_.length(); i++) { 493 for (int i = 0; i < nonlocals_.length(); i++) {
488 Variable* var = nonlocals_[i]; 494 Variable* var = nonlocals_[i];
489 if (var->name().is_identical_to(name)) { 495 if (var->name().is_identical_to(name) && var->mode() == mode) {
490 ASSERT(var->mode() == Variable::DYNAMIC);
491 return var; 496 return var;
492 } 497 }
493 } 498 }
494 499
495 // Otherwise create a new new-local and add it to the list. 500 // Otherwise create a new non-local and add it to the list.
496 Variable* var = new Variable( 501 Variable* var = new Variable(NULL, name, mode, true, false);
497 NULL /* we don't know the scope */,
498 name, Variable::DYNAMIC, true, false);
499 nonlocals_.Add(var); 502 nonlocals_.Add(var);
500 503
501 // Allocate it by giving it a dynamic lookup. 504 // Allocate it by giving it a dynamic lookup.
502 var->rewrite_ = new Slot(var, Slot::LOOKUP, -1); 505 var->rewrite_ = new Slot(var, Slot::LOOKUP, -1);
503 506
504 return var; 507 return var;
505 } 508 }
506 509
507 510
508 // Lookup a variable starting with this scope. The result is either 511 // Lookup a variable starting with this scope. The result is either
509 // the statically resolved (local!) variable belonging to an outer scope, 512 // the statically resolved (local!) variable belonging to an outer scope,
510 // or NULL. It may be NULL because a) we couldn't find a variable, or b) 513 // or NULL. It may be NULL because a) we couldn't find a variable, or b)
511 // because the variable is just a guess (and may be shadowed by another 514 // because the variable is just a guess (and may be shadowed by another
512 // variable that is introduced dynamically via an 'eval' call or a 'with' 515 // variable that is introduced dynamically via an 'eval' call or a 'with'
513 // statement). 516 // statement).
514 Variable* Scope::LookupRecursive(Handle<String> name, bool inner_lookup) { 517 Variable* Scope::LookupRecursive(Handle<String> name,
518 bool inner_lookup,
519 Variable** invalidated_local) {
515 // If we find a variable, but the current scope calls 'eval', the found 520 // If we find a variable, but the current scope calls 'eval', the found
516 // variable may not be the correct one (the 'eval' may introduce a 521 // variable may not be the correct one (the 'eval' may introduce a
517 // property with the same name). In that case, remember that the variable 522 // property with the same name). In that case, remember that the variable
518 // found is just a guess. 523 // found is just a guess.
519 bool guess = scope_calls_eval_; 524 bool guess = scope_calls_eval_;
520 525
521 // Try to find the variable in this scope. 526 // Try to find the variable in this scope.
522 Variable* var = LookupLocal(name); 527 Variable* var = LookupLocal(name);
523 528
524 if (var != NULL) { 529 if (var != NULL) {
(...skipping 10 matching lines...) Expand all
535 // only present - if at all - for function scopes. 540 // only present - if at all - for function scopes.
536 // 541 //
537 // This lookup corresponds to a lookup in the "intermediate" scope sitting 542 // This lookup corresponds to a lookup in the "intermediate" scope sitting
538 // between this scope and the outer scope. (ECMA-262, 3rd., requires that 543 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
539 // the name of named function literal is kept in an intermediate scope 544 // the name of named function literal is kept in an intermediate scope
540 // in between this scope and the next outer scope.) 545 // in between this scope and the next outer scope.)
541 if (function_ != NULL && function_->name().is_identical_to(name)) { 546 if (function_ != NULL && function_->name().is_identical_to(name)) {
542 var = function_; 547 var = function_;
543 548
544 } else if (outer_scope_ != NULL) { 549 } else if (outer_scope_ != NULL) {
545 var = outer_scope_->LookupRecursive(name, true /* inner lookup */); 550 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
546 // We may have found a variable in an outer scope. However, if 551 // We may have found a variable in an outer scope. However, if
547 // the current scope is inside a 'with', the actual variable may 552 // the current scope is inside a 'with', the actual variable may
548 // be a property introduced via the 'with' statement. Then, the 553 // be a property introduced via the 'with' statement. Then, the
549 // variable we may have found is just a guess. 554 // variable we may have found is just a guess.
550 if (scope_inside_with_) 555 if (scope_inside_with_)
551 guess = true; 556 guess = true;
552 } 557 }
553 558
554 // If we did not find a variable, we are done. 559 // If we did not find a variable, we are done.
555 if (var == NULL) 560 if (var == NULL)
556 return NULL; 561 return NULL;
557 } 562 }
558 563
559 ASSERT(var != NULL); 564 ASSERT(var != NULL);
560 565
561 // If this is a lookup from an inner scope, mark the variable. 566 // If this is a lookup from an inner scope, mark the variable.
562 if (inner_lookup) 567 if (inner_lookup)
563 var->is_accessed_from_inner_scope_ = true; 568 var->is_accessed_from_inner_scope_ = true;
564 569
565 // If the variable we have found is just a guess, invalidate the result. 570 // If the variable we have found is just a guess, invalidate the result.
566 if (guess) 571 if (guess) {
572 *invalidated_local = var;
567 var = NULL; 573 var = NULL;
574 }
568 575
569 return var; 576 return var;
570 } 577 }
571 578
572 579
573 void Scope::ResolveVariable(Scope* global_scope, VariableProxy* proxy) { 580 void Scope::ResolveVariable(Scope* global_scope, VariableProxy* proxy) {
574 ASSERT(global_scope == NULL || global_scope->is_global_scope()); 581 ASSERT(global_scope == NULL || global_scope->is_global_scope());
575 582
576 // If the proxy is already resolved there's nothing to do 583 // If the proxy is already resolved there's nothing to do
577 // (functions and consts may be resolved by the parser). 584 // (functions and consts may be resolved by the parser).
578 if (proxy->var() != NULL) return; 585 if (proxy->var() != NULL) return;
579 586
580 // Otherwise, try to resolve the variable. 587 // Otherwise, try to resolve the variable.
581 Variable* var = LookupRecursive(proxy->name(), false); 588 Variable* invalidated_local = NULL;
589 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
582 590
583 if (proxy->inside_with()) { 591 if (proxy->inside_with()) {
584 // If we are inside a local 'with' statement, all bets are off 592 // If we are inside a local 'with' statement, all bets are off
585 // and we cannot resolve the proxy to a local variable even if 593 // and we cannot resolve the proxy to a local variable even if
586 // we found an outer matching variable. 594 // we found an outer matching variable.
587 // Note that we must do a lookup anyway, because if we find one, 595 // Note that we must do a lookup anyway, because if we find one,
588 // we must mark that variable as potentially accessed from this 596 // we must mark that variable as potentially accessed from this
589 // inner scope (the property may not be in the 'with' object). 597 // inner scope (the property may not be in the 'with' object).
590 var = NonLocal(proxy->name()); 598 var = NonLocal(proxy->name(), Variable::DYNAMIC);
591 599
592 } else { 600 } else {
593 // We are not inside a local 'with' statement. 601 // We are not inside a local 'with' statement.
594 602
595 if (var == NULL) { 603 if (var == NULL) {
596 // We did not find the variable. We have a global variable 604 // We did not find the variable. We have a global variable
597 // if we are in the global scope (we know already that we 605 // if we are in the global scope (we know already that we
598 // are outside a 'with' statement) or if there is no way 606 // are outside a 'with' statement) or if there is no way
599 // that the variable might be introduced dynamically (through 607 // that the variable might be introduced dynamically (through
600 // a local or outer eval() call, or an outer 'with' statement), 608 // a local or outer eval() call, or an outer 'with' statement),
601 // or we don't know about the outer scope (because we are 609 // or we don't know about the outer scope (because we are
602 // in an eval scope). 610 // in an eval scope).
603 if (!is_global_scope() && 611 if (!is_global_scope() &&
604 (is_eval_scope() || outer_scope_calls_eval_ || 612 (scope_inside_with_ || outer_scope_is_eval_scope_)) {
605 scope_calls_eval_ || scope_inside_with_)) { 613 // If we are inside a with statement or the code is executed
606 // We must look up the variable at runtime, and we don't 614 // using eval, we give up and look up the variable at runtime.
607 // know anything else. 615 var = NonLocal(proxy->name(), Variable::DYNAMIC);
608 var = NonLocal(proxy->name()); 616
617 } else if (!is_global_scope() &&
618 (scope_calls_eval_ || outer_scope_calls_eval_)) {
619 // If the code is not executed using eval and there are no
620 // with scopes, either we have a local or a global variable
621 // that might be shadowed by an eval-introduced variable.
622 if (invalidated_local != NULL) {
623 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
624 var->set_local_if_not_shadowed(invalidated_local);
625 } else {
626 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
627 }
609 628
610 } else { 629 } else {
611 // We must have a global variable. 630 // We must have a global variable.
612 ASSERT(global_scope != NULL); 631 ASSERT(global_scope != NULL);
613 var = new Variable(global_scope, proxy->name(), 632 var = new Variable(global_scope, proxy->name(),
614 Variable::DYNAMIC, true, false); 633 Variable::DYNAMIC, true, false);
615 // Ideally we simply rewrite these variables into property 634 // Ideally we simply rewrite these variables into property
616 // accesses. Unfortunately, we cannot do this here at the 635 // accesses. Unfortunately, we cannot do this here at the
617 // moment because then we can't differentiate between 636 // moment because then we can't differentiate between
618 // global variable ('x') and global property ('this.x') access. 637 // global variable ('x') and global property ('this.x') access.
(...skipping 17 matching lines...) Expand all
636 ResolveVariable(global_scope, unresolved_[i]); 655 ResolveVariable(global_scope, unresolved_[i]);
637 } 656 }
638 657
639 // Resolve unresolved variables for inner scopes. 658 // Resolve unresolved variables for inner scopes.
640 for (int i = 0; i < inner_scopes_.length(); i++) { 659 for (int i = 0; i < inner_scopes_.length(); i++) {
641 inner_scopes_[i]->ResolveVariablesRecursively(global_scope); 660 inner_scopes_[i]->ResolveVariablesRecursively(global_scope);
642 } 661 }
643 } 662 }
644 663
645 664
646 bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval) { 665 bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
666 bool outer_scope_is_eval_scope) {
647 if (outer_scope_calls_eval) { 667 if (outer_scope_calls_eval) {
648 outer_scope_calls_eval_ = true; 668 outer_scope_calls_eval_ = true;
649 } 669 }
650 670
651 bool b = scope_calls_eval_ || outer_scope_calls_eval_; 671 if (outer_scope_is_eval_scope) {
672 outer_scope_is_eval_scope_ = true;
673 }
674
675 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
676 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
652 for (int i = 0; i < inner_scopes_.length(); i++) { 677 for (int i = 0; i < inner_scopes_.length(); i++) {
653 Scope* inner_scope = inner_scopes_[i]; 678 Scope* inner_scope = inner_scopes_[i];
654 if (inner_scope->PropagateScopeInfo(b)) { 679 if (inner_scope->PropagateScopeInfo(calls_eval, is_eval)) {
655 inner_scope_calls_eval_ = true; 680 inner_scope_calls_eval_ = true;
656 } 681 }
657 if (inner_scope->force_eager_compilation_) { 682 if (inner_scope->force_eager_compilation_) {
658 force_eager_compilation_ = true; 683 force_eager_compilation_ = true;
659 } 684 }
660 } 685 }
661 686
662 return scope_calls_eval_ || inner_scope_calls_eval_; 687 return scope_calls_eval_ || inner_scope_calls_eval_;
663 } 688 }
664 689
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && 917 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
893 !must_have_local_context) { 918 !must_have_local_context) {
894 num_heap_slots_ = 0; 919 num_heap_slots_ = 0;
895 } 920 }
896 921
897 // Allocation done. 922 // Allocation done.
898 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); 923 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
899 } 924 }
900 925
901 } } // namespace v8::internal 926 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/scopes.h ('k') | src/variables.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698