| 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 #ifndef V8_AST_SCOPES_H_ | 5 #ifndef V8_AST_SCOPES_H_ |
| 6 #define V8_AST_SCOPES_H_ | 6 #define V8_AST_SCOPES_H_ |
| 7 | 7 |
| 8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
| 9 #include "src/base/hashmap.h" | 9 #include "src/base/hashmap.h" |
| 10 #include "src/globals.h" | 10 #include "src/globals.h" |
| (...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 // Parameters. The left-most parameter has index 0. | 675 // Parameters. The left-most parameter has index 0. |
| 676 // Only valid for function scopes. | 676 // Only valid for function scopes. |
| 677 Variable* parameter(int index) const { | 677 Variable* parameter(int index) const { |
| 678 DCHECK(is_function_scope()); | 678 DCHECK(is_function_scope()); |
| 679 return params_[index]; | 679 return params_[index]; |
| 680 } | 680 } |
| 681 | 681 |
| 682 // Returns the default function arity excluding default or rest parameters. | 682 // Returns the default function arity excluding default or rest parameters. |
| 683 int default_function_length() const { return arity_; } | 683 int default_function_length() const { return arity_; } |
| 684 | 684 |
| 685 // Returns the number of formal parameters, up to but not including the | 685 // Returns the number of formal parameters, excluding a possible rest |
| 686 // rest parameter index (if the function has rest parameters), i.e. it | 686 // parameter. Examples: |
| 687 // says 2 for | 687 // function foo(a, b) {} ==> 2 |
| 688 // | 688 // function foo(a, b, ...c) {} ==> 2 |
| 689 // function foo(a, b) { ... } | 689 // function foo(a, b, c = 1) {} ==> 3 |
| 690 // | |
| 691 // and | |
| 692 // | |
| 693 // function foo(a, b, ...c) { ... } | |
| 694 // | |
| 695 // but for | |
| 696 // | |
| 697 // function foo(a, b, c = 1) { ... } | |
| 698 // | |
| 699 // we return 3 here. | |
| 700 int num_parameters() const { | 690 int num_parameters() const { |
| 701 return has_rest_parameter() ? params_.length() - 1 : params_.length(); | 691 return has_rest_ ? params_.length() - 1 : params_.length(); |
| 702 } | 692 } |
| 703 | 693 |
| 704 // A function can have at most one rest parameter. Returns Variable* or NULL. | 694 // The function's rest parameter (nullptr if there is none). |
| 705 Variable* rest_parameter(int* index) const { | 695 Variable* rest_parameter() const { |
| 706 if (!has_rest_parameter()) return nullptr; | 696 return has_rest_ ? params_[params_.length() - 1] : nullptr; |
| 707 *index = params_.length() - 1; | |
| 708 return rest_parameter(); | |
| 709 } | 697 } |
| 710 Variable* rest_parameter() const { | |
| 711 DCHECK(has_rest_parameter()); | |
| 712 return params_[params_.length() - 1]; | |
| 713 } | |
| 714 | |
| 715 bool has_rest_parameter() const { return has_rest_; } | |
| 716 | 698 |
| 717 bool has_simple_parameters() const { return has_simple_parameters_; } | 699 bool has_simple_parameters() const { return has_simple_parameters_; } |
| 718 | 700 |
| 719 // TODO(caitp): manage this state in a better way. PreParser must be able to | 701 // TODO(caitp): manage this state in a better way. PreParser must be able to |
| 720 // communicate that the scope is non-simple, without allocating any parameters | 702 // communicate that the scope is non-simple, without allocating any parameters |
| 721 // as the Parser does. This is necessary to ensure that TC39's proposed early | 703 // as the Parser does. This is necessary to ensure that TC39's proposed early |
| 722 // error can be reported consistently regardless of whether lazily parsed or | 704 // error can be reported consistently regardless of whether lazily parsed or |
| 723 // not. | 705 // not. |
| 724 void SetHasNonSimpleParameters() { | 706 void SetHasNonSimpleParameters() { |
| 725 DCHECK(is_function_scope()); | 707 DCHECK(is_function_scope()); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 void AllocateModuleVariables(); | 844 void AllocateModuleVariables(); |
| 863 | 845 |
| 864 private: | 846 private: |
| 865 ModuleDescriptor* module_descriptor_; | 847 ModuleDescriptor* module_descriptor_; |
| 866 }; | 848 }; |
| 867 | 849 |
| 868 } // namespace internal | 850 } // namespace internal |
| 869 } // namespace v8 | 851 } // namespace v8 |
| 870 | 852 |
| 871 #endif // V8_AST_SCOPES_H_ | 853 #endif // V8_AST_SCOPES_H_ |
| OLD | NEW |