OLD | NEW |
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 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 DCHECK(has_this_declaration()); | 485 DCHECK(has_this_declaration()); |
486 | 486 |
487 bool subclass_constructor = IsSubclassConstructor(function_kind_); | 487 bool subclass_constructor = IsSubclassConstructor(function_kind_); |
488 Variable* var = Declare( | 488 Variable* var = Declare( |
489 zone(), this, ast_value_factory->this_string(), | 489 zone(), this, ast_value_factory->this_string(), |
490 subclass_constructor ? CONST : VAR, Variable::THIS, | 490 subclass_constructor ? CONST : VAR, Variable::THIS, |
491 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); | 491 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); |
492 receiver_ = var; | 492 receiver_ = var; |
493 } | 493 } |
494 | 494 |
| 495 void DeclarationScope::DeclareArguments(AstValueFactory* ast_value_factory) { |
| 496 DCHECK(is_function_scope()); |
| 497 DCHECK(!is_arrow_scope()); |
| 498 |
| 499 // Check if there's lexically declared variable named arguments to avoid |
| 500 // redeclaration. See ES#sec-functiondeclarationinstantiation, step 20. |
| 501 Variable* arg_variable = LookupLocal(ast_value_factory->arguments_string()); |
| 502 if (arg_variable != nullptr && IsLexicalVariableMode(arg_variable->mode())) { |
| 503 return; |
| 504 } |
| 505 |
| 506 // Declare 'arguments' variable which exists in all non arrow functions. |
| 507 // Note that it might never be accessed, in which case it won't be |
| 508 // allocated during variable allocation. |
| 509 if (arg_variable == nullptr) { |
| 510 arguments_ = Declare(zone(), this, ast_value_factory->arguments_string(), |
| 511 VAR, Variable::ARGUMENTS, kCreatedInitialized); |
| 512 } else { |
| 513 arguments_ = arg_variable; |
| 514 } |
| 515 } |
| 516 |
495 void DeclarationScope::DeclareDefaultFunctionVariables( | 517 void DeclarationScope::DeclareDefaultFunctionVariables( |
496 AstValueFactory* ast_value_factory) { | 518 AstValueFactory* ast_value_factory) { |
497 DCHECK(is_function_scope()); | 519 DCHECK(is_function_scope()); |
498 DCHECK(!is_arrow_scope()); | 520 DCHECK(!is_arrow_scope()); |
499 // Declare 'arguments' variable which exists in all non arrow functions. | |
500 // Note that it might never be accessed, in which case it won't be | |
501 // allocated during variable allocation. | |
502 arguments_ = Declare(zone(), this, ast_value_factory->arguments_string(), VAR, | |
503 Variable::ARGUMENTS, kCreatedInitialized); | |
504 | 521 |
505 new_target_ = Declare(zone(), this, ast_value_factory->new_target_string(), | 522 new_target_ = Declare(zone(), this, ast_value_factory->new_target_string(), |
506 CONST, Variable::NORMAL, kCreatedInitialized); | 523 CONST, Variable::NORMAL, kCreatedInitialized); |
507 | 524 |
508 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) || | 525 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) || |
509 IsAccessorFunction(function_kind_)) { | 526 IsAccessorFunction(function_kind_)) { |
510 this_function_ = | 527 this_function_ = |
511 Declare(zone(), this, ast_value_factory->this_function_string(), CONST, | 528 Declare(zone(), this, ast_value_factory->this_function_string(), CONST, |
512 Variable::NORMAL, kCreatedInitialized); | 529 Variable::NORMAL, kCreatedInitialized); |
513 } | 530 } |
(...skipping 987 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1501 | 1518 |
1502 void Scope::AllocateHeapSlot(Variable* var) { | 1519 void Scope::AllocateHeapSlot(Variable* var) { |
1503 var->AllocateTo(VariableLocation::CONTEXT, num_heap_slots_++); | 1520 var->AllocateTo(VariableLocation::CONTEXT, num_heap_slots_++); |
1504 } | 1521 } |
1505 | 1522 |
1506 void DeclarationScope::AllocateParameterLocals() { | 1523 void DeclarationScope::AllocateParameterLocals() { |
1507 DCHECK(is_function_scope()); | 1524 DCHECK(is_function_scope()); |
1508 | 1525 |
1509 bool uses_sloppy_arguments = false; | 1526 bool uses_sloppy_arguments = false; |
1510 | 1527 |
1511 // Functions have 'arguments' declared implicitly in all non arrow functions. | |
1512 if (arguments_ != nullptr) { | 1528 if (arguments_ != nullptr) { |
| 1529 DCHECK(!is_arrow_scope()); |
1513 // 'arguments' is used. Unless there is also a parameter called | 1530 // 'arguments' is used. Unless there is also a parameter called |
1514 // 'arguments', we must be conservative and allocate all parameters to | 1531 // 'arguments', we must be conservative and allocate all parameters to |
1515 // the context assuming they will be captured by the arguments object. | 1532 // the context assuming they will be captured by the arguments object. |
1516 // If we have a parameter named 'arguments', a (new) value is always | 1533 // If we have a parameter named 'arguments', a (new) value is always |
1517 // assigned to it via the function invocation. Then 'arguments' denotes | 1534 // assigned to it via the function invocation. Then 'arguments' denotes |
1518 // that specific parameter value and cannot be used to access the | 1535 // that specific parameter value and cannot be used to access the |
1519 // parameters, which is why we don't need to allocate an arguments | 1536 // parameters, which is why we don't need to allocate an arguments |
1520 // object in that case. | 1537 // object in that case. |
1521 if (MustAllocate(arguments_) && !has_arguments_parameter_) { | 1538 if (MustAllocate(arguments_) && !has_arguments_parameter_) { |
1522 // In strict mode 'arguments' does not alias formal parameters. | 1539 // In strict mode 'arguments' does not alias formal parameters. |
1523 // Therefore in strict mode we allocate parameters as if 'arguments' | 1540 // Therefore in strict mode we allocate parameters as if 'arguments' |
1524 // were not used. | 1541 // were not used. |
1525 // If the parameter list is not simple, arguments isn't sloppy either. | 1542 // If the parameter list is not simple, arguments isn't sloppy either. |
1526 uses_sloppy_arguments = | 1543 uses_sloppy_arguments = |
1527 is_sloppy(language_mode()) && has_simple_parameters(); | 1544 is_sloppy(language_mode()) && has_simple_parameters(); |
1528 } else { | 1545 } else { |
1529 // 'arguments' is unused. Tell the code generator that it does not need to | 1546 // 'arguments' is unused. Tell the code generator that it does not need to |
1530 // allocate the arguments object by nulling out arguments_. | 1547 // allocate the arguments object by nulling out arguments_. |
1531 arguments_ = nullptr; | 1548 arguments_ = nullptr; |
1532 } | 1549 } |
1533 | |
1534 } else { | |
1535 DCHECK(is_arrow_scope()); | |
1536 } | 1550 } |
1537 | 1551 |
1538 // The same parameter may occur multiple times in the parameters_ list. | 1552 // The same parameter may occur multiple times in the parameters_ list. |
1539 // If it does, and if it is not copied into the context object, it must | 1553 // If it does, and if it is not copied into the context object, it must |
1540 // receive the highest parameter index for that parameter; thus iteration | 1554 // receive the highest parameter index for that parameter; thus iteration |
1541 // order is relevant! | 1555 // order is relevant! |
1542 for (int i = num_parameters() - 1; i >= 0; --i) { | 1556 for (int i = num_parameters() - 1; i >= 0; --i) { |
1543 Variable* var = params_[i]; | 1557 Variable* var = params_[i]; |
1544 DCHECK(!has_rest_ || var != rest_parameter()); | 1558 DCHECK(!has_rest_ || var != rest_parameter()); |
1545 DCHECK_EQ(this, var->scope()); | 1559 DCHECK_EQ(this, var->scope()); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1696 Variable* function = | 1710 Variable* function = |
1697 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1711 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
1698 bool is_function_var_in_context = | 1712 bool is_function_var_in_context = |
1699 function != nullptr && function->IsContextSlot(); | 1713 function != nullptr && function->IsContextSlot(); |
1700 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1714 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1701 (is_function_var_in_context ? 1 : 0); | 1715 (is_function_var_in_context ? 1 : 0); |
1702 } | 1716 } |
1703 | 1717 |
1704 } // namespace internal | 1718 } // namespace internal |
1705 } // namespace v8 | 1719 } // namespace v8 |
OLD | NEW |