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 2679 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; |
3475 IdentifierT name = | 3479 IdentifierT name = |
3476 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, ok); | 3480 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, ok); |
3477 if (!*ok) return; | 3481 if (!*ok) return; |
3478 | 3482 |
| 3483 if (Check(Token::ASSIGN)) { |
| 3484 // Optional argument initializer |
| 3485 // let formalName = IS_UNDEFINED(arguments[i]) ? initializer : arguments[i]; |
| 3486 static const bool accept_IN = true; |
| 3487 ExpressionClassifier classifier; |
| 3488 *initializer = ParseAssignmentExpression(accept_IN, &classifier, ok); |
| 3489 if (!*ok) return; |
| 3490 *has_initializer = true; |
| 3491 } |
| 3492 |
3479 // Store locations for possible future error reports. | 3493 // Store locations for possible future error reports. |
3480 if (!locs->eval_or_arguments.IsValid() && this->IsEvalOrArguments(name)) { | 3494 if (!locs->eval_or_arguments.IsValid() && this->IsEvalOrArguments(name)) { |
3481 locs->eval_or_arguments = scanner()->location(); | 3495 locs->eval_or_arguments = scanner()->location(); |
3482 } | 3496 } |
3483 if (!locs->undefined.IsValid() && this->IsUndefined(name)) { | 3497 if (!locs->undefined.IsValid() && this->IsUndefined(name)) { |
3484 locs->undefined = scanner()->location(); | 3498 locs->undefined = scanner()->location(); |
3485 } | 3499 } |
3486 if (!locs->reserved.IsValid() && is_strict_reserved) { | 3500 if (!locs->reserved.IsValid() && is_strict_reserved) { |
3487 locs->reserved = scanner()->location(); | 3501 locs->reserved = scanner()->location(); |
3488 } | 3502 } |
3489 bool was_declared = Traits::DeclareFormalParameter(scope, name, is_rest); | 3503 bool was_declared = Traits::DeclareFormalParameter(scope, name, is_rest); |
3490 if (!locs->duplicate.IsValid() && was_declared) { | 3504 if (!locs->duplicate.IsValid() && was_declared) { |
3491 locs->duplicate = scanner()->location(); | 3505 locs->duplicate = scanner()->location(); |
3492 } | 3506 } |
3493 } | 3507 } |
3494 | 3508 |
3495 | 3509 |
3496 template <class Traits> | 3510 template <class Traits> |
3497 int ParserBase<Traits>::ParseFormalParameterList( | 3511 int ParserBase<Traits>::ParseFormalParameterList( |
3498 FormalParameterScopeT* scope, FormalParameterErrorLocations* locs, | 3512 FormalParameterScopeT* scope, FormalParameterErrorLocations* locs, |
3499 bool* is_rest, bool* ok) { | 3513 ExpressionListT initializers, bool* has_initializers, bool* is_rest, |
| 3514 bool* ok) { |
3500 // FormalParameters[Yield,GeneratorParameter] : | 3515 // FormalParameters[Yield,GeneratorParameter] : |
3501 // [empty] | 3516 // [empty] |
3502 // FormalParameterList[?Yield, ?GeneratorParameter] | 3517 // FormalParameterList[?Yield, ?GeneratorParameter] |
3503 // | 3518 // |
3504 // FormalParameterList[Yield,GeneratorParameter] : | 3519 // FormalParameterList[Yield,GeneratorParameter] : |
3505 // FunctionRestParameter[?Yield] | 3520 // FunctionRestParameter[?Yield] |
3506 // FormalsList[?Yield, ?GeneratorParameter] | 3521 // FormalsList[?Yield, ?GeneratorParameter] |
3507 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield] | 3522 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield] |
3508 // | 3523 // |
3509 // FormalsList[Yield,GeneratorParameter] : | 3524 // FormalsList[Yield,GeneratorParameter] : |
3510 // FormalParameter[?Yield, ?GeneratorParameter] | 3525 // FormalParameter[?Yield, ?GeneratorParameter] |
3511 // FormalsList[?Yield, ?GeneratorParameter] , | 3526 // FormalsList[?Yield, ?GeneratorParameter] , |
3512 // FormalParameter[?Yield,?GeneratorParameter] | 3527 // FormalParameter[?Yield,?GeneratorParameter] |
3513 | 3528 |
3514 int parameter_count = 0; | 3529 int parameter_count = 0; |
3515 | 3530 DCHECK(scope_->is_function_scope()); |
3516 if (peek() != Token::RPAREN) { | 3531 if (peek() != Token::RPAREN) { |
| 3532 Scope* param_scope = NewScope(scope_, BLOCK_SCOPE); |
| 3533 BlockState param_state(&scope_, param_scope); |
3517 do { | 3534 do { |
3518 if (++parameter_count > Code::kMaxArguments) { | 3535 if (++parameter_count > Code::kMaxArguments) { |
3519 ReportMessage("too_many_parameters"); | 3536 ReportMessage("too_many_parameters"); |
3520 *ok = false; | 3537 *ok = false; |
3521 return -1; | 3538 return -1; |
3522 } | 3539 } |
| 3540 bool has_initializer = false; |
| 3541 ExpressionT initializer = this->EmptyExpression(); |
3523 *is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS); | 3542 *is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS); |
3524 ParseFormalParameter(scope, locs, *is_rest, ok); | 3543 ParseFormalParameter(scope, locs, &initializer, &has_initializer, |
| 3544 *is_rest, ok); |
3525 if (!*ok) return -1; | 3545 if (!*ok) return -1; |
| 3546 initializers->Add(initializer, zone()); |
| 3547 if (has_initializer) *has_initializers = true; |
3526 } while (!*is_rest && Check(Token::COMMA)); | 3548 } while (!*is_rest && Check(Token::COMMA)); |
3527 | 3549 |
3528 if (*is_rest && peek() == Token::COMMA) { | 3550 if (*is_rest && peek() == Token::COMMA) { |
3529 ReportMessageAt(scanner()->peek_location(), "param_after_rest"); | 3551 ReportMessageAt(scanner()->peek_location(), "param_after_rest"); |
3530 *ok = false; | 3552 *ok = false; |
3531 return -1; | 3553 return -1; |
3532 } | 3554 } |
3533 } | 3555 } |
3534 | 3556 |
3535 return parameter_count; | 3557 return parameter_count; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3579 int num_parameters = scope->num_parameters(); | 3601 int num_parameters = scope->num_parameters(); |
3580 int materialized_literal_count = -1; | 3602 int materialized_literal_count = -1; |
3581 int expected_property_count = -1; | 3603 int expected_property_count = -1; |
3582 int handler_count = 0; | 3604 int handler_count = 0; |
3583 Scanner::Location super_loc; | 3605 Scanner::Location super_loc; |
3584 | 3606 |
3585 { | 3607 { |
3586 typename Traits::Type::Factory function_factory(ast_value_factory()); | 3608 typename Traits::Type::Factory function_factory(ast_value_factory()); |
3587 FunctionState function_state(&function_state_, &scope_, scope, | 3609 FunctionState function_state(&function_state_, &scope_, scope, |
3588 kArrowFunction, &function_factory); | 3610 kArrowFunction, &function_factory); |
3589 | 3611 DCHECK(scope->is_arrow_scope()); |
| 3612 Scope* function_body = NewScope(scope, FUNCTION_BODY_SCOPE); |
| 3613 DCHECK_EQ(scope->function_body(), function_body); |
| 3614 BlockState function_body_state(&scope_, function_body); |
3590 if (peek() == Token::ARROW) { | 3615 if (peek() == Token::ARROW) { |
3591 BindingPatternUnexpectedToken(classifier); | 3616 BindingPatternUnexpectedToken(classifier); |
3592 } | 3617 } |
3593 Expect(Token::ARROW, CHECK_OK); | 3618 Expect(Token::ARROW, CHECK_OK); |
3594 | 3619 |
3595 if (peek() == Token::LBRACE) { | 3620 if (peek() == Token::LBRACE) { |
3596 // Multiple statement body | 3621 // Multiple statement body |
3597 Consume(Token::LBRACE); | 3622 Consume(Token::LBRACE); |
3598 bool is_lazily_parsed = | 3623 bool is_lazily_parsed = |
3599 (mode() == PARSE_LAZILY && scope_->AllowsLazyCompilation()); | 3624 (mode() == PARSE_LAZILY && scope_->AllowsLazyCompilation()); |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3840 *ok = false; | 3865 *ok = false; |
3841 return; | 3866 return; |
3842 } | 3867 } |
3843 has_seen_constructor_ = true; | 3868 has_seen_constructor_ = true; |
3844 return; | 3869 return; |
3845 } | 3870 } |
3846 } | 3871 } |
3847 } } // v8::internal | 3872 } } // v8::internal |
3848 | 3873 |
3849 #endif // V8_PREPARSER_H | 3874 #endif // V8_PREPARSER_H |
OLD | NEW |