Chromium Code Reviews| 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_PREPARSER_H | 5 #ifndef V8_PREPARSER_H |
| 6 #define V8_PREPARSER_H | 6 #define V8_PREPARSER_H |
| 7 | 7 |
| 8 #include "src/bailout-reason.h" | 8 #include "src/bailout-reason.h" |
| 9 #include "src/expression-classifier.h" | 9 #include "src/expression-classifier.h" |
| 10 #include "src/func-name-inferrer.h" | 10 #include "src/func-name-inferrer.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 stack_limit_(stack_limit), | 103 stack_limit_(stack_limit), |
| 104 zone_(zone), | 104 zone_(zone), |
| 105 scanner_(scanner), | 105 scanner_(scanner), |
| 106 stack_overflow_(false), | 106 stack_overflow_(false), |
| 107 allow_lazy_(false), | 107 allow_lazy_(false), |
| 108 allow_natives_(false), | 108 allow_natives_(false), |
| 109 allow_harmony_arrow_functions_(false), | 109 allow_harmony_arrow_functions_(false), |
| 110 allow_harmony_sloppy_(false), | 110 allow_harmony_sloppy_(false), |
| 111 allow_harmony_sloppy_let_(false), | 111 allow_harmony_sloppy_let_(false), |
| 112 allow_harmony_rest_parameters_(false), | 112 allow_harmony_rest_parameters_(false), |
| 113 allow_harmony_default_parameters_(false), | |
| 113 allow_harmony_spreadcalls_(false), | 114 allow_harmony_spreadcalls_(false), |
| 114 allow_harmony_destructuring_(false), | 115 allow_harmony_destructuring_(false), |
| 115 allow_harmony_spread_arrays_(false), | 116 allow_harmony_spread_arrays_(false), |
| 116 allow_harmony_new_target_(false), | 117 allow_harmony_new_target_(false), |
| 117 allow_strong_mode_(false), | 118 allow_strong_mode_(false), |
| 118 allow_legacy_const_(true) {} | 119 allow_legacy_const_(true) {} |
| 119 | 120 |
| 120 #define ALLOW_ACCESSORS(name) \ | 121 #define ALLOW_ACCESSORS(name) \ |
| 121 bool allow_##name() const { return allow_##name##_; } \ | 122 bool allow_##name() const { return allow_##name##_; } \ |
| 122 void set_allow_##name(bool allow) { allow_##name##_ = allow; } | 123 void set_allow_##name(bool allow) { allow_##name##_ = allow; } |
| 123 | 124 |
| 124 ALLOW_ACCESSORS(lazy); | 125 ALLOW_ACCESSORS(lazy); |
| 125 ALLOW_ACCESSORS(natives); | 126 ALLOW_ACCESSORS(natives); |
| 126 ALLOW_ACCESSORS(harmony_arrow_functions); | 127 ALLOW_ACCESSORS(harmony_arrow_functions); |
| 127 ALLOW_ACCESSORS(harmony_sloppy); | 128 ALLOW_ACCESSORS(harmony_sloppy); |
| 128 ALLOW_ACCESSORS(harmony_sloppy_let); | 129 ALLOW_ACCESSORS(harmony_sloppy_let); |
| 129 ALLOW_ACCESSORS(harmony_rest_parameters); | 130 ALLOW_ACCESSORS(harmony_rest_parameters); |
| 131 ALLOW_ACCESSORS(harmony_default_parameters); | |
| 130 ALLOW_ACCESSORS(harmony_spreadcalls); | 132 ALLOW_ACCESSORS(harmony_spreadcalls); |
| 131 ALLOW_ACCESSORS(harmony_destructuring); | 133 ALLOW_ACCESSORS(harmony_destructuring); |
| 132 ALLOW_ACCESSORS(harmony_spread_arrays); | 134 ALLOW_ACCESSORS(harmony_spread_arrays); |
| 133 ALLOW_ACCESSORS(harmony_new_target); | 135 ALLOW_ACCESSORS(harmony_new_target); |
| 134 ALLOW_ACCESSORS(strong_mode); | 136 ALLOW_ACCESSORS(strong_mode); |
| 135 ALLOW_ACCESSORS(legacy_const); | 137 ALLOW_ACCESSORS(legacy_const); |
| 136 #undef ALLOW_ACCESSORS | 138 #undef ALLOW_ACCESSORS |
| 137 | 139 |
| 138 protected: | 140 protected: |
| 139 enum AllowRestrictedIdentifiers { | 141 enum AllowRestrictedIdentifiers { |
| (...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 691 ExpressionClassifier* classifier, bool* ok); | 693 ExpressionClassifier* classifier, bool* ok); |
| 692 void AddTemplateExpression(ExpressionT); | 694 void AddTemplateExpression(ExpressionT); |
| 693 ExpressionT ParseSuperExpression(bool is_new, | 695 ExpressionT ParseSuperExpression(bool is_new, |
| 694 ExpressionClassifier* classifier, bool* ok); | 696 ExpressionClassifier* classifier, bool* ok); |
| 695 ExpressionT ParseNewTargetExpression(bool* ok); | 697 ExpressionT ParseNewTargetExpression(bool* ok); |
| 696 ExpressionT ParseStrongInitializationExpression( | 698 ExpressionT ParseStrongInitializationExpression( |
| 697 ExpressionClassifier* classifier, bool* ok); | 699 ExpressionClassifier* classifier, bool* ok); |
| 698 ExpressionT ParseStrongSuperCallExpression(ExpressionClassifier* classifier, | 700 ExpressionT ParseStrongSuperCallExpression(ExpressionClassifier* classifier, |
| 699 bool* ok); | 701 bool* ok); |
| 700 | 702 |
| 701 void ParseFormalParameter(bool is_rest, | 703 void ParseFormalParameter(FormalParametersT* parameters, |
| 702 FormalParametersT* parameters, | |
| 703 ExpressionClassifier* classifier, bool* ok); | 704 ExpressionClassifier* classifier, bool* ok); |
| 704 void ParseFormalParameterList(FormalParametersT* parameters, | 705 void ParseFormalParameterList(FormalParametersT* parameters, |
| 705 ExpressionClassifier* classifier, bool* ok); | 706 ExpressionClassifier* classifier, bool* ok); |
| 706 void CheckArityRestrictions( | 707 void CheckArityRestrictions( |
| 707 int param_count, FunctionLiteral::ArityRestriction arity_restriction, | 708 int param_count, FunctionLiteral::ArityRestriction arity_restriction, |
| 708 bool has_rest, int formals_start_pos, int formals_end_pos, bool* ok); | 709 bool has_rest, int formals_start_pos, int formals_end_pos, bool* ok); |
| 709 | 710 |
| 710 // Checks if the expression is a valid reference expression (e.g., on the | 711 // Checks if the expression is a valid reference expression (e.g., on the |
| 711 // left-hand side of assignments). Although ruled out by ECMA as early errors, | 712 // left-hand side of assignments). Although ruled out by ECMA as early errors, |
| 712 // we allow calls for web compatibility and rewrite them to a runtime throw. | 713 // we allow calls for web compatibility and rewrite them to a runtime throw. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 793 | 794 |
| 794 Scanner* scanner_; | 795 Scanner* scanner_; |
| 795 bool stack_overflow_; | 796 bool stack_overflow_; |
| 796 | 797 |
| 797 bool allow_lazy_; | 798 bool allow_lazy_; |
| 798 bool allow_natives_; | 799 bool allow_natives_; |
| 799 bool allow_harmony_arrow_functions_; | 800 bool allow_harmony_arrow_functions_; |
| 800 bool allow_harmony_sloppy_; | 801 bool allow_harmony_sloppy_; |
| 801 bool allow_harmony_sloppy_let_; | 802 bool allow_harmony_sloppy_let_; |
| 802 bool allow_harmony_rest_parameters_; | 803 bool allow_harmony_rest_parameters_; |
| 804 bool allow_harmony_default_parameters_; | |
| 803 bool allow_harmony_spreadcalls_; | 805 bool allow_harmony_spreadcalls_; |
| 804 bool allow_harmony_destructuring_; | 806 bool allow_harmony_destructuring_; |
| 805 bool allow_harmony_spread_arrays_; | 807 bool allow_harmony_spread_arrays_; |
| 806 bool allow_harmony_new_target_; | 808 bool allow_harmony_new_target_; |
| 807 bool allow_strong_mode_; | 809 bool allow_strong_mode_; |
| 808 bool allow_legacy_const_; | 810 bool allow_legacy_const_; |
| 809 }; | 811 }; |
| 810 | 812 |
| 811 | 813 |
| 812 class PreParserIdentifier { | 814 class PreParserIdentifier { |
| (...skipping 812 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1625 inline void MaterializeTemplateCallsiteLiterals(); | 1627 inline void MaterializeTemplateCallsiteLiterals(); |
| 1626 PreParserExpression NoTemplateTag() { | 1628 PreParserExpression NoTemplateTag() { |
| 1627 return PreParserExpression::NoTemplateTag(); | 1629 return PreParserExpression::NoTemplateTag(); |
| 1628 } | 1630 } |
| 1629 static bool IsTaggedTemplate(const PreParserExpression tag) { | 1631 static bool IsTaggedTemplate(const PreParserExpression tag) { |
| 1630 return !tag.IsNoTemplateTag(); | 1632 return !tag.IsNoTemplateTag(); |
| 1631 } | 1633 } |
| 1632 | 1634 |
| 1633 void AddFormalParameter( | 1635 void AddFormalParameter( |
| 1634 PreParserFormalParameters* parameters, PreParserExpression pattern, | 1636 PreParserFormalParameters* parameters, PreParserExpression pattern, |
| 1635 bool is_rest) { | 1637 PreParserExpression initializer, bool is_rest) { |
| 1636 ++parameters->arity; | 1638 ++parameters->arity; |
| 1637 } | 1639 } |
| 1638 void DeclareFormalParameter(Scope* scope, PreParserIdentifier parameter, | 1640 void DeclareFormalParameter(Scope* scope, PreParserIdentifier parameter, |
| 1639 bool is_simple, | 1641 bool is_simple, |
| 1640 ExpressionClassifier* classifier) {} | 1642 ExpressionClassifier* classifier) {} |
| 1641 | 1643 |
| 1642 void CheckConflictingVarDeclarations(Scope* scope, bool* ok) {} | 1644 void CheckConflictingVarDeclarations(Scope* scope, bool* ok) {} |
| 1643 | 1645 |
| 1644 // Temporary glue; these functions will move to ParserBase. | 1646 // Temporary glue; these functions will move to ParserBase. |
| 1645 PreParserExpression ParseV8Intrinsic(bool* ok); | 1647 PreParserExpression ParseV8Intrinsic(bool* ok); |
| (...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2274 result = this->ParseArrowFunctionLiteral(parameters, args_classifier, | 2276 result = this->ParseArrowFunctionLiteral(parameters, args_classifier, |
| 2275 CHECK_OK); | 2277 CHECK_OK); |
| 2276 } else if (allow_harmony_arrow_functions() && | 2278 } else if (allow_harmony_arrow_functions() && |
| 2277 allow_harmony_rest_parameters() && Check(Token::ELLIPSIS)) { | 2279 allow_harmony_rest_parameters() && Check(Token::ELLIPSIS)) { |
| 2278 // (...x) => y | 2280 // (...x) => y |
| 2279 Scope* scope = | 2281 Scope* scope = |
| 2280 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); | 2282 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); |
| 2281 FormalParametersT formals(scope); | 2283 FormalParametersT formals(scope); |
| 2282 scope->set_start_position(beg_pos); | 2284 scope->set_start_position(beg_pos); |
| 2283 ExpressionClassifier formals_classifier; | 2285 ExpressionClassifier formals_classifier; |
| 2284 const bool is_rest = true; | 2286 formals.has_rest = true; |
| 2285 this->ParseFormalParameter(is_rest, &formals, &formals_classifier, | 2287 this->ParseFormalParameter(&formals, &formals_classifier, CHECK_OK); |
| 2286 CHECK_OK); | |
| 2287 Traits::DeclareFormalParameter( | 2288 Traits::DeclareFormalParameter( |
| 2288 formals.scope, formals.at(0), formals.is_simple, | 2289 formals.scope, formals.at(0), formals.is_simple, |
| 2289 &formals_classifier); | 2290 &formals_classifier); |
| 2290 if (peek() == Token::COMMA) { | 2291 if (peek() == Token::COMMA) { |
| 2291 ReportMessageAt(scanner()->peek_location(), | 2292 ReportMessageAt(scanner()->peek_location(), |
| 2292 MessageTemplate::kParamAfterRest); | 2293 MessageTemplate::kParamAfterRest); |
| 2293 *ok = false; | 2294 *ok = false; |
| 2294 return this->EmptyExpression(); | 2295 return this->EmptyExpression(); |
| 2295 } | 2296 } |
| 2296 Expect(Token::RPAREN, CHECK_OK); | 2297 Expect(Token::RPAREN, CHECK_OK); |
| (...skipping 1318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3615 return expression; | 3616 return expression; |
| 3616 } | 3617 } |
| 3617 } | 3618 } |
| 3618 DCHECK(false); | 3619 DCHECK(false); |
| 3619 return this->EmptyExpression(); | 3620 return this->EmptyExpression(); |
| 3620 } | 3621 } |
| 3621 | 3622 |
| 3622 | 3623 |
| 3623 template <class Traits> | 3624 template <class Traits> |
| 3624 void ParserBase<Traits>::ParseFormalParameter( | 3625 void ParserBase<Traits>::ParseFormalParameter( |
| 3625 bool is_rest, FormalParametersT* parameters, | 3626 FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) { |
|
adamk
2015/08/12 18:22:59
This rest refactoring seems separate, could it be
rossberg
2015/08/13 11:06:36
Split into separate CL: https://codereview.chromiu
| |
| 3626 ExpressionClassifier* classifier, bool* ok) { | |
| 3627 // FormalParameter[Yield,GeneratorParameter] : | 3627 // FormalParameter[Yield,GeneratorParameter] : |
| 3628 // BindingElement[?Yield, ?GeneratorParameter] | 3628 // BindingElement[?Yield, ?GeneratorParameter] |
| 3629 bool is_rest = parameters->has_rest; | |
| 3629 | 3630 |
| 3630 Token::Value next = peek(); | 3631 Token::Value next = peek(); |
| 3631 ExpressionT pattern = ParsePrimaryExpression(classifier, ok); | 3632 ExpressionT pattern = ParsePrimaryExpression(classifier, ok); |
| 3632 if (!*ok) return; | 3633 if (!*ok) return; |
| 3633 | 3634 |
| 3634 ValidateBindingPattern(classifier, ok); | 3635 ValidateBindingPattern(classifier, ok); |
| 3635 if (!*ok) return; | 3636 if (!*ok) return; |
| 3636 | 3637 |
| 3637 if (!allow_harmony_destructuring() && !Traits::IsIdentifier(pattern)) { | 3638 if (!Traits::IsIdentifier(pattern)) { |
| 3638 ReportUnexpectedToken(next); | 3639 if (is_rest || !allow_harmony_destructuring()) { |
| 3639 *ok = false; | 3640 ReportUnexpectedToken(next); |
| 3640 return; | 3641 *ok = false; |
| 3642 return; | |
| 3643 } | |
| 3644 parameters->is_simple = false; | |
| 3641 } | 3645 } |
| 3642 | 3646 |
| 3643 if (parameters->is_simple) { | 3647 ExpressionT initializer = Traits::EmptyExpression(); |
| 3644 parameters->is_simple = !is_rest && Traits::IsIdentifier(pattern); | 3648 if (!is_rest && allow_harmony_default_parameters() && Check(Token::ASSIGN)) { |
| 3649 ExpressionClassifier init_classifier; | |
| 3650 initializer = ParseAssignmentExpression(true, &init_classifier, ok); | |
| 3651 if (!*ok) return; | |
| 3652 ValidateExpression(&init_classifier, ok); | |
| 3653 if (!*ok) return; | |
| 3654 parameters->is_simple = false; | |
| 3645 } | 3655 } |
| 3646 parameters->has_rest = is_rest; | 3656 |
| 3647 if (is_rest && !Traits::IsIdentifier(pattern)) { | 3657 Traits::AddFormalParameter(parameters, pattern, initializer, is_rest); |
| 3648 ReportUnexpectedToken(next); | |
| 3649 *ok = false; | |
| 3650 return; | |
| 3651 } | |
| 3652 Traits::AddFormalParameter(parameters, pattern, is_rest); | |
| 3653 } | 3658 } |
| 3654 | 3659 |
| 3655 | 3660 |
| 3656 template <class Traits> | 3661 template <class Traits> |
| 3657 void ParserBase<Traits>::ParseFormalParameterList( | 3662 void ParserBase<Traits>::ParseFormalParameterList( |
| 3658 FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) { | 3663 FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) { |
| 3659 // FormalParameters[Yield,GeneratorParameter] : | 3664 // FormalParameters[Yield,GeneratorParameter] : |
| 3660 // [empty] | 3665 // [empty] |
| 3661 // FormalParameterList[?Yield, ?GeneratorParameter] | 3666 // FormalParameterList[?Yield, ?GeneratorParameter] |
| 3662 // | 3667 // |
| 3663 // FormalParameterList[Yield,GeneratorParameter] : | 3668 // FormalParameterList[Yield,GeneratorParameter] : |
| 3664 // FunctionRestParameter[?Yield] | 3669 // FunctionRestParameter[?Yield] |
| 3665 // FormalsList[?Yield, ?GeneratorParameter] | 3670 // FormalsList[?Yield, ?GeneratorParameter] |
| 3666 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield] | 3671 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield] |
| 3667 // | 3672 // |
| 3668 // FormalsList[Yield,GeneratorParameter] : | 3673 // FormalsList[Yield,GeneratorParameter] : |
| 3669 // FormalParameter[?Yield, ?GeneratorParameter] | 3674 // FormalParameter[?Yield, ?GeneratorParameter] |
| 3670 // FormalsList[?Yield, ?GeneratorParameter] , | 3675 // FormalsList[?Yield, ?GeneratorParameter] , |
| 3671 // FormalParameter[?Yield,?GeneratorParameter] | 3676 // FormalParameter[?Yield,?GeneratorParameter] |
| 3672 | 3677 |
| 3673 DCHECK_EQ(0, parameters->Arity()); | 3678 DCHECK_EQ(0, parameters->Arity()); |
| 3674 | 3679 |
| 3675 if (peek() != Token::RPAREN) { | 3680 if (peek() != Token::RPAREN) { |
| 3676 do { | 3681 do { |
| 3677 if (parameters->Arity() > Code::kMaxArguments) { | 3682 if (parameters->Arity() > Code::kMaxArguments) { |
| 3678 ReportMessage(MessageTemplate::kTooManyParameters); | 3683 ReportMessage(MessageTemplate::kTooManyParameters); |
| 3679 *ok = false; | 3684 *ok = false; |
| 3680 return; | 3685 return; |
| 3681 } | 3686 } |
| 3682 bool is_rest = allow_harmony_rest_parameters() && Check(Token::ELLIPSIS); | 3687 parameters->has_rest = |
| 3683 ParseFormalParameter(is_rest, parameters, classifier, ok); | 3688 allow_harmony_rest_parameters() && Check(Token::ELLIPSIS); |
| 3689 ParseFormalParameter(parameters, classifier, ok); | |
| 3684 if (!*ok) return; | 3690 if (!*ok) return; |
| 3685 } while (!parameters->has_rest && Check(Token::COMMA)); | 3691 } while (!parameters->has_rest && Check(Token::COMMA)); |
| 3686 | 3692 |
| 3687 if (parameters->has_rest && peek() == Token::COMMA) { | 3693 if (parameters->has_rest) { |
| 3688 ReportMessageAt(scanner()->peek_location(), | 3694 parameters->is_simple = false; |
| 3695 if (peek() == Token::COMMA) { | |
| 3696 ReportMessageAt(scanner()->peek_location(), | |
| 3689 MessageTemplate::kParamAfterRest); | 3697 MessageTemplate::kParamAfterRest); |
| 3690 *ok = false; | 3698 *ok = false; |
| 3691 return; | 3699 return; |
| 3700 } | |
| 3692 } | 3701 } |
| 3693 } | 3702 } |
| 3694 | 3703 |
| 3695 for (int i = 0; i < parameters->Arity(); ++i) { | 3704 for (int i = 0; i < parameters->Arity(); ++i) { |
| 3696 auto parameter = parameters->at(i); | 3705 auto parameter = parameters->at(i); |
| 3697 Traits::DeclareFormalParameter( | 3706 Traits::DeclareFormalParameter( |
| 3698 parameters->scope, parameter, parameters->is_simple, classifier); | 3707 parameters->scope, parameter, parameters->is_simple, classifier); |
| 3699 } | 3708 } |
| 3700 } | 3709 } |
| 3701 | 3710 |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4020 *ok = false; | 4029 *ok = false; |
| 4021 return; | 4030 return; |
| 4022 } | 4031 } |
| 4023 has_seen_constructor_ = true; | 4032 has_seen_constructor_ = true; |
| 4024 return; | 4033 return; |
| 4025 } | 4034 } |
| 4026 } | 4035 } |
| 4027 } } // v8::internal | 4036 } } // v8::internal |
| 4028 | 4037 |
| 4029 #endif // V8_PREPARSER_H | 4038 #endif // V8_PREPARSER_H |
| OLD | NEW |