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/v8.h" | 8 #include "src/v8.h" |
9 | 9 |
10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 // typedef Factory; | 77 // typedef Factory; |
78 // }; | 78 // }; |
79 // // ... | 79 // // ... |
80 // }; | 80 // }; |
81 | 81 |
82 template <typename Traits> | 82 template <typename Traits> |
83 class ParserBase : public Traits { | 83 class ParserBase : public Traits { |
84 public: | 84 public: |
85 // Shorten type names defined by Traits. | 85 // Shorten type names defined by Traits. |
86 typedef typename Traits::Type::Expression ExpressionT; | 86 typedef typename Traits::Type::Expression ExpressionT; |
| 87 typedef typename Traits::Type::ExpressionList ExpressionListT; |
87 typedef typename Traits::Type::Identifier IdentifierT; | 88 typedef typename Traits::Type::Identifier IdentifierT; |
88 typedef typename Traits::Type::FormalParameter FormalParameterT; | 89 typedef typename Traits::Type::FormalParameter FormalParameterT; |
89 typedef typename Traits::Type::FormalParameterScope FormalParameterScopeT; | 90 typedef typename Traits::Type::FormalParameterScope FormalParameterScopeT; |
90 typedef typename Traits::Type::FunctionLiteral FunctionLiteralT; | 91 typedef typename Traits::Type::FunctionLiteral FunctionLiteralT; |
91 typedef typename Traits::Type::Literal LiteralT; | 92 typedef typename Traits::Type::Literal LiteralT; |
92 typedef typename Traits::Type::ObjectLiteralProperty ObjectLiteralPropertyT; | 93 typedef typename Traits::Type::ObjectLiteralProperty ObjectLiteralPropertyT; |
93 | 94 |
94 ParserBase(Zone* zone, Scanner* scanner, uintptr_t stack_limit, | 95 ParserBase(Zone* zone, Scanner* scanner, uintptr_t stack_limit, |
95 v8::Extension* extension, AstValueFactory* ast_value_factory, | 96 v8::Extension* extension, AstValueFactory* ast_value_factory, |
96 ParserRecorder* log, typename Traits::Type::Parser this_object) | 97 ParserRecorder* log, typename Traits::Type::Parser this_object) |
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 ExpressionClassifier* classifier, bool* ok); | 758 ExpressionClassifier* classifier, bool* ok); |
758 void AddTemplateExpression(ExpressionT); | 759 void AddTemplateExpression(ExpressionT); |
759 ExpressionT ParseSuperExpression(bool is_new, | 760 ExpressionT ParseSuperExpression(bool is_new, |
760 ExpressionClassifier* classifier, bool* ok); | 761 ExpressionClassifier* classifier, bool* ok); |
761 ExpressionT ParseStrongInitializationExpression( | 762 ExpressionT ParseStrongInitializationExpression( |
762 ExpressionClassifier* classifier, bool* ok); | 763 ExpressionClassifier* classifier, bool* ok); |
763 ExpressionT ParseStrongSuperCallExpression(ExpressionClassifier* classifier, | 764 ExpressionT ParseStrongSuperCallExpression(ExpressionClassifier* classifier, |
764 bool* ok); | 765 bool* ok); |
765 | 766 |
766 void ParseFormalParameter(FormalParameterScopeT* scope, | 767 void ParseFormalParameter(FormalParameterScopeT* scope, |
767 FormalParameterErrorLocations* locs, bool is_rest, | 768 FormalParameterErrorLocations* locs, |
768 bool* ok); | 769 ExpressionT* initializer, bool* has_initializer, |
| 770 bool is_rest, bool* ok); |
769 int ParseFormalParameterList(FormalParameterScopeT* scope, | 771 int ParseFormalParameterList(FormalParameterScopeT* scope, |
770 FormalParameterErrorLocations* locs, | 772 FormalParameterErrorLocations* locs, |
771 bool* has_rest, bool* ok); | 773 ExpressionListT initializers, |
| 774 bool* has_initializers, bool* has_rest, |
| 775 bool* ok); |
772 void CheckArityRestrictions( | 776 void CheckArityRestrictions( |
773 int param_count, FunctionLiteral::ArityRestriction arity_restriction, | 777 int param_count, FunctionLiteral::ArityRestriction arity_restriction, |
774 int formals_start_pos, int formals_end_pos, bool* ok); | 778 int formals_start_pos, int formals_end_pos, bool* ok); |
775 | 779 |
776 // Checks if the expression is a valid reference expression (e.g., on the | 780 // Checks if the expression is a valid reference expression (e.g., on the |
777 // left-hand side of assignments). Although ruled out by ECMA as early errors, | 781 // left-hand side of assignments). Although ruled out by ECMA as early errors, |
778 // we allow calls for web compatibility and rewrite them to a runtime throw. | 782 // we allow calls for web compatibility and rewrite them to a runtime throw. |
779 ExpressionT CheckAndRewriteReferenceExpression( | 783 ExpressionT CheckAndRewriteReferenceExpression( |
780 ExpressionT expression, | 784 ExpressionT expression, |
781 Scanner::Location location, const char* message, bool* ok); | 785 Scanner::Location location, const char* message, bool* ok); |
(...skipping 935 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1717 } | 1721 } |
1718 inline void MaterializeTemplateCallsiteLiterals(); | 1722 inline void MaterializeTemplateCallsiteLiterals(); |
1719 PreParserExpression NoTemplateTag() { | 1723 PreParserExpression NoTemplateTag() { |
1720 return PreParserExpression::NoTemplateTag(); | 1724 return PreParserExpression::NoTemplateTag(); |
1721 } | 1725 } |
1722 static bool IsTaggedTemplate(const PreParserExpression tag) { | 1726 static bool IsTaggedTemplate(const PreParserExpression tag) { |
1723 return !tag.IsNoTemplateTag(); | 1727 return !tag.IsNoTemplateTag(); |
1724 } | 1728 } |
1725 | 1729 |
1726 V8_INLINE bool DeclareFormalParameter(DuplicateFinder* scope, | 1730 V8_INLINE bool DeclareFormalParameter(DuplicateFinder* scope, |
1727 PreParserIdentifier param, | 1731 PreParserIdentifier param, bool is_rest, |
1728 bool is_rest); | 1732 int pos); |
1729 | 1733 |
1730 void CheckConflictingVarDeclarations(Scope* scope, bool* ok) {} | 1734 void CheckConflictingVarDeclarations(Scope* scope, bool* ok) {} |
1731 | 1735 |
1732 // Temporary glue; these functions will move to ParserBase. | 1736 // Temporary glue; these functions will move to ParserBase. |
1733 PreParserExpression ParseV8Intrinsic(bool* ok); | 1737 PreParserExpression ParseV8Intrinsic(bool* ok); |
1734 PreParserExpression ParseFunctionLiteral( | 1738 PreParserExpression ParseFunctionLiteral( |
1735 PreParserIdentifier name, Scanner::Location function_name_location, | 1739 PreParserIdentifier name, Scanner::Location function_name_location, |
1736 bool name_is_strict_reserved, FunctionKind kind, | 1740 bool name_is_strict_reserved, FunctionKind kind, |
1737 int function_token_position, FunctionLiteral::FunctionType type, | 1741 int function_token_position, FunctionLiteral::FunctionType type, |
1738 FunctionLiteral::ArityRestriction arity_restriction, bool* ok); | 1742 FunctionLiteral::ArityRestriction arity_restriction, bool* ok); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1909 | 1913 |
1910 PreParserExpression PreParserTraits::SpreadCallNew(PreParserExpression function, | 1914 PreParserExpression PreParserTraits::SpreadCallNew(PreParserExpression function, |
1911 PreParserExpressionList args, | 1915 PreParserExpressionList args, |
1912 int pos) { | 1916 int pos) { |
1913 return pre_parser_->factory()->NewCallNew(function, args, pos); | 1917 return pre_parser_->factory()->NewCallNew(function, args, pos); |
1914 } | 1918 } |
1915 | 1919 |
1916 | 1920 |
1917 bool PreParserTraits::DeclareFormalParameter( | 1921 bool PreParserTraits::DeclareFormalParameter( |
1918 DuplicateFinder* duplicate_finder, PreParserIdentifier current_identifier, | 1922 DuplicateFinder* duplicate_finder, PreParserIdentifier current_identifier, |
1919 bool is_rest) { | 1923 bool is_rest, int pos) { |
1920 return pre_parser_->scanner()->FindSymbol(duplicate_finder, 1) != 0; | 1924 return pre_parser_->scanner()->FindSymbol(duplicate_finder, 1) != 0; |
1921 } | 1925 } |
1922 | 1926 |
1923 | 1927 |
1924 void PreParserTraits::ParseArrowFunctionFormalParameters( | 1928 void PreParserTraits::ParseArrowFunctionFormalParameters( |
1925 Scope* scope, PreParserExpression params, | 1929 Scope* scope, PreParserExpression params, |
1926 const Scanner::Location& params_loc, | 1930 const Scanner::Location& params_loc, |
1927 FormalParameterErrorLocations* error_locs, bool* is_rest, bool* ok) { | 1931 FormalParameterErrorLocations* error_locs, bool* is_rest, bool* ok) { |
1928 // TODO(wingo): Detect duplicated identifiers in paramlists. Detect parameter | 1932 // TODO(wingo): Detect duplicated identifiers in paramlists. Detect parameter |
1929 // lists that are too long. | 1933 // lists that are too long. |
(...skipping 1531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3461 } | 3465 } |
3462 } | 3466 } |
3463 DCHECK(false); | 3467 DCHECK(false); |
3464 return this->EmptyExpression(); | 3468 return this->EmptyExpression(); |
3465 } | 3469 } |
3466 | 3470 |
3467 | 3471 |
3468 template <class Traits> | 3472 template <class Traits> |
3469 void ParserBase<Traits>::ParseFormalParameter( | 3473 void ParserBase<Traits>::ParseFormalParameter( |
3470 FormalParameterScopeT* scope, FormalParameterErrorLocations* locs, | 3474 FormalParameterScopeT* scope, FormalParameterErrorLocations* locs, |
3471 bool is_rest, bool* ok) { | 3475 ExpressionT* initializer, bool* has_initializer, bool is_rest, bool* ok) { |
3472 // FormalParameter[Yield,GeneratorParameter] : | 3476 // FormalParameter[Yield,GeneratorParameter] : |
3473 // BindingElement[?Yield, ?GeneratorParameter] | 3477 // BindingElement[?Yield, ?GeneratorParameter] |
3474 bool is_strict_reserved; | 3478 bool is_strict_reserved; |
| 3479 int pos = peek_position(); |
3475 IdentifierT name = | 3480 IdentifierT name = |
3476 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, ok); | 3481 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, ok); |
3477 if (!*ok) return; | 3482 if (!*ok) return; |
3478 | 3483 |
| 3484 if (Check(Token::ASSIGN)) { |
| 3485 // Optional argument initializer |
| 3486 // let formalName = IS_UNDEFINED(arguments[i]) ? initializer : arguments[i]; |
| 3487 static const bool accept_IN = true; |
| 3488 ExpressionClassifier classifier; |
| 3489 *initializer = ParseAssignmentExpression(accept_IN, &classifier, ok); |
| 3490 if (!*ok) return; |
| 3491 *has_initializer = true; |
| 3492 } |
| 3493 |
3479 // Store locations for possible future error reports. | 3494 // Store locations for possible future error reports. |
3480 if (!locs->eval_or_arguments.IsValid() && this->IsEvalOrArguments(name)) { | 3495 if (!locs->eval_or_arguments.IsValid() && this->IsEvalOrArguments(name)) { |
3481 locs->eval_or_arguments = scanner()->location(); | 3496 locs->eval_or_arguments = scanner()->location(); |
3482 } | 3497 } |
3483 if (!locs->undefined.IsValid() && this->IsUndefined(name)) { | 3498 if (!locs->undefined.IsValid() && this->IsUndefined(name)) { |
3484 locs->undefined = scanner()->location(); | 3499 locs->undefined = scanner()->location(); |
3485 } | 3500 } |
3486 if (!locs->reserved.IsValid() && is_strict_reserved) { | 3501 if (!locs->reserved.IsValid() && is_strict_reserved) { |
3487 locs->reserved = scanner()->location(); | 3502 locs->reserved = scanner()->location(); |
3488 } | 3503 } |
3489 bool was_declared = Traits::DeclareFormalParameter(scope, name, is_rest); | 3504 bool was_declared = Traits::DeclareFormalParameter(scope, name, is_rest, pos); |
3490 if (!locs->duplicate.IsValid() && was_declared) { | 3505 if (!locs->duplicate.IsValid() && was_declared) { |
3491 locs->duplicate = scanner()->location(); | 3506 locs->duplicate = scanner()->location(); |
3492 } | 3507 } |
3493 } | 3508 } |
3494 | 3509 |
3495 | 3510 |
3496 template <class Traits> | 3511 template <class Traits> |
3497 int ParserBase<Traits>::ParseFormalParameterList( | 3512 int ParserBase<Traits>::ParseFormalParameterList( |
3498 FormalParameterScopeT* scope, FormalParameterErrorLocations* locs, | 3513 FormalParameterScopeT* scope, FormalParameterErrorLocations* locs, |
3499 bool* is_rest, bool* ok) { | 3514 ExpressionListT initializers, bool* has_initializers, bool* is_rest, |
| 3515 bool* ok) { |
3500 // FormalParameters[Yield,GeneratorParameter] : | 3516 // FormalParameters[Yield,GeneratorParameter] : |
3501 // [empty] | 3517 // [empty] |
3502 // FormalParameterList[?Yield, ?GeneratorParameter] | 3518 // FormalParameterList[?Yield, ?GeneratorParameter] |
3503 // | 3519 // |
3504 // FormalParameterList[Yield,GeneratorParameter] : | 3520 // FormalParameterList[Yield,GeneratorParameter] : |
3505 // FunctionRestParameter[?Yield] | 3521 // FunctionRestParameter[?Yield] |
3506 // FormalsList[?Yield, ?GeneratorParameter] | 3522 // FormalsList[?Yield, ?GeneratorParameter] |
3507 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield] | 3523 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield] |
3508 // | 3524 // |
3509 // FormalsList[Yield,GeneratorParameter] : | 3525 // FormalsList[Yield,GeneratorParameter] : |
3510 // FormalParameter[?Yield, ?GeneratorParameter] | 3526 // FormalParameter[?Yield, ?GeneratorParameter] |
3511 // FormalsList[?Yield, ?GeneratorParameter] , | 3527 // FormalsList[?Yield, ?GeneratorParameter] , |
3512 // FormalParameter[?Yield,?GeneratorParameter] | 3528 // FormalParameter[?Yield,?GeneratorParameter] |
3513 | 3529 |
3514 int parameter_count = 0; | 3530 int parameter_count = 0; |
3515 | 3531 DCHECK(scope_->is_function_scope()); |
3516 if (peek() != Token::RPAREN) { | 3532 if (peek() != Token::RPAREN) { |
3517 do { | 3533 do { |
| 3534 Scope* param_scope = NewScope(scope_, BLOCK_SCOPE); |
| 3535 param_scope->set_start_position(scanner()->peek_location().beg_pos); |
| 3536 BlockState param_state(&scope_, param_scope); |
3518 if (++parameter_count > Code::kMaxArguments) { | 3537 if (++parameter_count > Code::kMaxArguments) { |
3519 ReportMessage("too_many_parameters"); | 3538 ReportMessage("too_many_parameters"); |
3520 *ok = false; | 3539 *ok = false; |
3521 return -1; | 3540 return -1; |
3522 } | 3541 } |
| 3542 bool has_initializer = false; |
| 3543 ExpressionT initializer = this->EmptyExpression(); |
3523 *is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS); | 3544 *is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS); |
3524 ParseFormalParameter(scope, locs, *is_rest, ok); | 3545 ParseFormalParameter(scope, locs, &initializer, &has_initializer, |
| 3546 *is_rest, ok); |
| 3547 param_scope->set_start_position(scanner()->location().end_pos); |
3525 if (!*ok) return -1; | 3548 if (!*ok) return -1; |
| 3549 initializers->Add(initializer, zone()); |
| 3550 if (has_initializer) *has_initializers = true; |
3526 } while (!*is_rest && Check(Token::COMMA)); | 3551 } while (!*is_rest && Check(Token::COMMA)); |
3527 | 3552 |
3528 if (*is_rest && peek() == Token::COMMA) { | 3553 if (*is_rest && peek() == Token::COMMA) { |
3529 ReportMessageAt(scanner()->peek_location(), "param_after_rest"); | 3554 ReportMessageAt(scanner()->peek_location(), "param_after_rest"); |
3530 *ok = false; | 3555 *ok = false; |
3531 return -1; | 3556 return -1; |
3532 } | 3557 } |
3533 } | 3558 } |
3534 | 3559 |
3535 return parameter_count; | 3560 return parameter_count; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3579 int num_parameters = scope->num_parameters(); | 3604 int num_parameters = scope->num_parameters(); |
3580 int materialized_literal_count = -1; | 3605 int materialized_literal_count = -1; |
3581 int expected_property_count = -1; | 3606 int expected_property_count = -1; |
3582 int handler_count = 0; | 3607 int handler_count = 0; |
3583 Scanner::Location super_loc; | 3608 Scanner::Location super_loc; |
3584 | 3609 |
3585 { | 3610 { |
3586 typename Traits::Type::Factory function_factory(ast_value_factory()); | 3611 typename Traits::Type::Factory function_factory(ast_value_factory()); |
3587 FunctionState function_state(&function_state_, &scope_, scope, | 3612 FunctionState function_state(&function_state_, &scope_, scope, |
3588 kArrowFunction, &function_factory); | 3613 kArrowFunction, &function_factory); |
3589 | 3614 DCHECK(scope->is_arrow_scope()); |
| 3615 Scope* function_body = NewScope(scope, FUNCTION_BODY_SCOPE); |
| 3616 DCHECK_EQ(scope->function_body(), function_body); |
| 3617 BlockState function_body_state(&scope_, function_body); |
3590 if (peek() == Token::ARROW) { | 3618 if (peek() == Token::ARROW) { |
3591 BindingPatternUnexpectedToken(classifier); | 3619 BindingPatternUnexpectedToken(classifier); |
3592 } | 3620 } |
3593 Expect(Token::ARROW, CHECK_OK); | 3621 Expect(Token::ARROW, CHECK_OK); |
3594 | 3622 |
3595 if (peek() == Token::LBRACE) { | 3623 if (peek() == Token::LBRACE) { |
3596 // Multiple statement body | 3624 // Multiple statement body |
3597 Consume(Token::LBRACE); | 3625 Consume(Token::LBRACE); |
3598 bool is_lazily_parsed = | 3626 bool is_lazily_parsed = |
3599 (mode() == PARSE_LAZILY && scope_->AllowsLazyCompilation()); | 3627 (mode() == PARSE_LAZILY && scope_->AllowsLazyCompilation()); |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3840 *ok = false; | 3868 *ok = false; |
3841 return; | 3869 return; |
3842 } | 3870 } |
3843 has_seen_constructor_ = true; | 3871 has_seen_constructor_ = true; |
3844 return; | 3872 return; |
3845 } | 3873 } |
3846 } | 3874 } |
3847 } } // v8::internal | 3875 } } // v8::internal |
3848 | 3876 |
3849 #endif // V8_PREPARSER_H | 3877 #endif // V8_PREPARSER_H |
OLD | NEW |