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

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

Issue 2301183003: Move sloppy block function hoisting logic from Parser to Scope. (Closed)
Patch Set: code review (adamk@) Created 4 years, 3 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 <set> 7 #include <set>
8 8
9 #include "src/accessors.h" 9 #include "src/accessors.h"
10 #include "src/bootstrapper.h" 10 #include "src/bootstrapper.h"
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 442
443 const ModuleScope* Scope::AsModuleScope() const { 443 const ModuleScope* Scope::AsModuleScope() const {
444 DCHECK(is_module_scope()); 444 DCHECK(is_module_scope());
445 return static_cast<const ModuleScope*>(this); 445 return static_cast<const ModuleScope*>(this);
446 } 446 }
447 447
448 int Scope::num_parameters() const { 448 int Scope::num_parameters() const {
449 return is_declaration_scope() ? AsDeclarationScope()->num_parameters() : 0; 449 return is_declaration_scope() ? AsDeclarationScope()->num_parameters() : 0;
450 } 450 }
451 451
452 void DeclarationScope::HoistSloppyBlockFunctions(AstNodeFactory* factory,
453 bool* ok) {
454 DCHECK(is_sloppy(language_mode()));
455 DCHECK(is_function_scope() || is_eval_scope() || is_script_scope() ||
456 (is_block_scope() && outer_scope()->is_function_scope()));
457 DCHECK(HasSimpleParameters() || is_block_scope());
458 bool has_simple_parameters = HasSimpleParameters();
459 // For each variable which is used as a function declaration in a sloppy
460 // block,
461 SloppyBlockFunctionMap* map = sloppy_block_function_map();
462 for (ZoneHashMap::Entry* p = map->Start(); p != nullptr; p = map->Next(p)) {
463 AstRawString* name = static_cast<AstRawString*>(p->key);
464
465 // If the variable wouldn't conflict with a lexical declaration
466 // or parameter,
467
468 // Check if there's a conflict with a parameter.
469 // This depends on the fact that functions always have a scope solely to
470 // hold complex parameters, and the names local to that scope are
471 // precisely the names of the parameters. IsDeclaredParameter(name) does
472 // not hold for names declared by complex parameters, nor are those
473 // bindings necessarily declared lexically, so we have to check for them
474 // explicitly. On the other hand, if there are not complex parameters,
475 // it is sufficient to just check IsDeclaredParameter.
476 if (!has_simple_parameters) {
477 if (outer_scope_->LookupLocal(name) != nullptr) {
478 continue;
479 }
480 } else {
481 if (IsDeclaredParameter(name)) {
482 continue;
483 }
484 }
485
486 bool var_created = false;
487
488 // Write in assignments to var for each block-scoped function declaration
489 auto delegates = static_cast<SloppyBlockFunctionStatement*>(p->value);
490
491 DeclarationScope* decl_scope = this;
492 while (decl_scope->is_eval_scope()) {
493 decl_scope = decl_scope->outer_scope()->GetDeclarationScope();
494 }
495 Scope* outer_scope = decl_scope->outer_scope();
496
497 for (SloppyBlockFunctionStatement* delegate = delegates;
498 delegate != nullptr; delegate = delegate->next()) {
499 // Check if there's a conflict with a lexical declaration
500 Scope* query_scope = delegate->scope()->outer_scope();
501 Variable* var = nullptr;
502 bool should_hoist = true;
503
504 // Note that we perform this loop for each delegate named 'name',
505 // which may duplicate work if those delegates share scopes.
506 // It is not sufficient to just do a Lookup on query_scope: for
507 // example, that does not prevent hoisting of the function in
508 // `{ let e; try {} catch (e) { function e(){} } }`
509 do {
510 var = query_scope->LookupLocal(name);
511 if (var != nullptr && IsLexicalVariableMode(var->mode())) {
512 should_hoist = false;
513 break;
514 }
515 query_scope = query_scope->outer_scope();
516 } while (query_scope != outer_scope);
517
518 if (!should_hoist) continue;
519
520 // Declare a var-style binding for the function in the outer scope
521 if (!var_created) {
522 var_created = true;
523 VariableProxy* proxy = NewUnresolved(factory, name);
524 Declaration* declaration =
525 factory->NewVariableDeclaration(proxy, this, kNoSourcePosition);
526 // Based on the preceding check, it doesn't matter what we pass as
527 // allow_harmony_restrictive_generators and
528 // sloppy_mode_block_scope_function_redefinition.
529 DeclareVariable(declaration, VAR,
530 Variable::DefaultInitializationFlag(VAR), false,
531 nullptr, ok);
532 DCHECK(*ok); // Based on the preceding check, this should not fail
533 if (!*ok) return;
534 }
535
536 // Create VariableProxies for creating an assignment statement
537 // (later). Read from the local lexical scope and write to the function
538 // scope.
539 delegate->set_to(NewUnresolved(factory, name));
540 delegate->set_from(delegate->scope()->NewUnresolved(factory, name));
541 }
542 }
543 }
544
452 void DeclarationScope::Analyze(ParseInfo* info, AnalyzeMode mode) { 545 void DeclarationScope::Analyze(ParseInfo* info, AnalyzeMode mode) {
453 DCHECK(info->literal() != NULL); 546 DCHECK(info->literal() != NULL);
454 DeclarationScope* scope = info->literal()->scope(); 547 DeclarationScope* scope = info->literal()->scope();
455 548
456 // We are compiling one of three cases: 549 // We are compiling one of three cases:
457 // 1) top-level code, 550 // 1) top-level code,
458 // 2) a function/eval/module on the top-level 551 // 2) a function/eval/module on the top-level
459 // 3) a function/eval in a scope that was already resolved. 552 // 3) a function/eval in a scope that was already resolved.
460 DCHECK(scope->scope_type() == SCRIPT_SCOPE || 553 DCHECK(scope->scope_type() == SCRIPT_SCOPE ||
461 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || 554 scope->outer_scope()->scope_type() == SCRIPT_SCOPE ||
(...skipping 1225 matching lines...) Expand 10 before | Expand all | Expand 10 after
1687 Variable* function = 1780 Variable* function =
1688 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; 1781 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr;
1689 bool is_function_var_in_context = 1782 bool is_function_var_in_context =
1690 function != nullptr && function->IsContextSlot(); 1783 function != nullptr && function->IsContextSlot();
1691 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - 1784 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1692 (is_function_var_in_context ? 1 : 0); 1785 (is_function_var_in_context ? 1 : 0);
1693 } 1786 }
1694 1787
1695 } // namespace internal 1788 } // namespace internal
1696 } // namespace v8 1789 } // namespace v8
OLDNEW
« no previous file with comments | « src/ast/scopes.h ('k') | src/ast/variables.h » ('j') | src/parsing/parser.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698