| 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 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 typename Traits::Type::Factory* factory); | 214 typename Traits::Type::Factory* factory); |
| 215 ~FunctionState(); | 215 ~FunctionState(); |
| 216 | 216 |
| 217 int NextMaterializedLiteralIndex() { | 217 int NextMaterializedLiteralIndex() { |
| 218 return next_materialized_literal_index_++; | 218 return next_materialized_literal_index_++; |
| 219 } | 219 } |
| 220 int materialized_literal_count() { | 220 int materialized_literal_count() { |
| 221 return next_materialized_literal_index_; | 221 return next_materialized_literal_index_; |
| 222 } | 222 } |
| 223 | 223 |
| 224 int NextHandlerIndex() { return next_handler_index_++; } | |
| 225 int handler_count() { return next_handler_index_; } | |
| 226 | |
| 227 void AddProperty() { expected_property_count_++; } | 224 void AddProperty() { expected_property_count_++; } |
| 228 int expected_property_count() { return expected_property_count_; } | 225 int expected_property_count() { return expected_property_count_; } |
| 229 | 226 |
| 230 Scanner::Location this_location() const { return this_location_; } | 227 Scanner::Location this_location() const { return this_location_; } |
| 231 Scanner::Location super_location() const { return super_location_; } | 228 Scanner::Location super_location() const { return super_location_; } |
| 232 Scanner::Location return_location() const { return return_location_; } | 229 Scanner::Location return_location() const { return return_location_; } |
| 233 void set_this_location(Scanner::Location location) { | 230 void set_this_location(Scanner::Location location) { |
| 234 this_location_ = location; | 231 this_location_ = location; |
| 235 } | 232 } |
| 236 void set_super_location(Scanner::Location location) { | 233 void set_super_location(Scanner::Location location) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 257 } | 254 } |
| 258 | 255 |
| 259 typename Traits::Type::Factory* factory() { return factory_; } | 256 typename Traits::Type::Factory* factory() { return factory_; } |
| 260 | 257 |
| 261 private: | 258 private: |
| 262 // Used to assign an index to each literal that needs materialization in | 259 // Used to assign an index to each literal that needs materialization in |
| 263 // the function. Includes regexp literals, and boilerplate for object and | 260 // the function. Includes regexp literals, and boilerplate for object and |
| 264 // array literals. | 261 // array literals. |
| 265 int next_materialized_literal_index_; | 262 int next_materialized_literal_index_; |
| 266 | 263 |
| 267 // Used to assign a per-function index to try and catch handlers. | |
| 268 int next_handler_index_; | |
| 269 | |
| 270 // Properties count estimation. | 264 // Properties count estimation. |
| 271 int expected_property_count_; | 265 int expected_property_count_; |
| 272 | 266 |
| 273 // Location of most recent use of 'this' (invalid if none). | 267 // Location of most recent use of 'this' (invalid if none). |
| 274 Scanner::Location this_location_; | 268 Scanner::Location this_location_; |
| 275 | 269 |
| 276 // Location of most recent 'return' statement (invalid if none). | 270 // Location of most recent 'return' statement (invalid if none). |
| 277 Scanner::Location return_location_; | 271 Scanner::Location return_location_; |
| 278 | 272 |
| 279 // Location of call to the "super" constructor (invalid if none). | 273 // Location of call to the "super" constructor (invalid if none). |
| (...skipping 18 matching lines...) Expand all Loading... |
| 298 // Annoyingly, arrow functions first parse as comma expressions, then when we | 292 // Annoyingly, arrow functions first parse as comma expressions, then when we |
| 299 // see the => we have to go back and reinterpret the arguments as being formal | 293 // see the => we have to go back and reinterpret the arguments as being formal |
| 300 // parameters. To do so we need to reset some of the parser state back to | 294 // parameters. To do so we need to reset some of the parser state back to |
| 301 // what it was before the arguments were first seen. | 295 // what it was before the arguments were first seen. |
| 302 class Checkpoint BASE_EMBEDDED { | 296 class Checkpoint BASE_EMBEDDED { |
| 303 public: | 297 public: |
| 304 explicit Checkpoint(ParserBase* parser) { | 298 explicit Checkpoint(ParserBase* parser) { |
| 305 function_state_ = parser->function_state_; | 299 function_state_ = parser->function_state_; |
| 306 next_materialized_literal_index_ = | 300 next_materialized_literal_index_ = |
| 307 function_state_->next_materialized_literal_index_; | 301 function_state_->next_materialized_literal_index_; |
| 308 next_handler_index_ = function_state_->next_handler_index_; | |
| 309 expected_property_count_ = function_state_->expected_property_count_; | 302 expected_property_count_ = function_state_->expected_property_count_; |
| 310 } | 303 } |
| 311 | 304 |
| 312 void Restore() { | 305 void Restore() { |
| 313 function_state_->next_materialized_literal_index_ = | 306 function_state_->next_materialized_literal_index_ = |
| 314 next_materialized_literal_index_; | 307 next_materialized_literal_index_; |
| 315 function_state_->next_handler_index_ = next_handler_index_; | |
| 316 function_state_->expected_property_count_ = expected_property_count_; | 308 function_state_->expected_property_count_ = expected_property_count_; |
| 317 } | 309 } |
| 318 | 310 |
| 319 private: | 311 private: |
| 320 FunctionState* function_state_; | 312 FunctionState* function_state_; |
| 321 int next_materialized_literal_index_; | 313 int next_materialized_literal_index_; |
| 322 int next_handler_index_; | |
| 323 int expected_property_count_; | 314 int expected_property_count_; |
| 324 }; | 315 }; |
| 325 | 316 |
| 326 class ParsingModeScope BASE_EMBEDDED { | 317 class ParsingModeScope BASE_EMBEDDED { |
| 327 public: | 318 public: |
| 328 ParsingModeScope(ParserBase* parser, Mode mode) | 319 ParsingModeScope(ParserBase* parser, Mode mode) |
| 329 : parser_(parser), | 320 : parser_(parser), |
| 330 old_mode_(parser->mode()) { | 321 old_mode_(parser->mode()) { |
| 331 parser_->mode_ = mode; | 322 parser_->mode_ = mode; |
| 332 } | 323 } |
| (...skipping 1147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1480 int pos) { | 1471 int pos) { |
| 1481 return PreParserExpression::Default(); | 1472 return PreParserExpression::Default(); |
| 1482 } | 1473 } |
| 1483 PreParserStatement NewReturnStatement(PreParserExpression expression, | 1474 PreParserStatement NewReturnStatement(PreParserExpression expression, |
| 1484 int pos) { | 1475 int pos) { |
| 1485 return PreParserStatement::Default(); | 1476 return PreParserStatement::Default(); |
| 1486 } | 1477 } |
| 1487 PreParserExpression NewFunctionLiteral( | 1478 PreParserExpression NewFunctionLiteral( |
| 1488 PreParserIdentifier name, AstValueFactory* ast_value_factory, | 1479 PreParserIdentifier name, AstValueFactory* ast_value_factory, |
| 1489 Scope* scope, PreParserStatementList body, int materialized_literal_count, | 1480 Scope* scope, PreParserStatementList body, int materialized_literal_count, |
| 1490 int expected_property_count, int handler_count, int parameter_count, | 1481 int expected_property_count, int parameter_count, |
| 1491 FunctionLiteral::ParameterFlag has_duplicate_parameters, | 1482 FunctionLiteral::ParameterFlag has_duplicate_parameters, |
| 1492 FunctionLiteral::FunctionType function_type, | 1483 FunctionLiteral::FunctionType function_type, |
| 1493 FunctionLiteral::IsFunctionFlag is_function, | 1484 FunctionLiteral::IsFunctionFlag is_function, |
| 1494 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind, | 1485 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind, |
| 1495 int position) { | 1486 int position) { |
| 1496 return PreParserExpression::Default(); | 1487 return PreParserExpression::Default(); |
| 1497 } | 1488 } |
| 1498 | 1489 |
| 1499 PreParserExpression NewSpread(PreParserExpression expression, int pos) { | 1490 PreParserExpression NewSpread(PreParserExpression expression, int pos) { |
| 1500 return PreParserExpression::Spread(expression); | 1491 return PreParserExpression::Spread(expression); |
| (...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2039 return pre_parser_->ParseEagerFunctionBody(function_name, pos, fvar, | 2030 return pre_parser_->ParseEagerFunctionBody(function_name, pos, fvar, |
| 2040 fvar_init_op, kind, ok); | 2031 fvar_init_op, kind, ok); |
| 2041 } | 2032 } |
| 2042 | 2033 |
| 2043 | 2034 |
| 2044 template <class Traits> | 2035 template <class Traits> |
| 2045 ParserBase<Traits>::FunctionState::FunctionState( | 2036 ParserBase<Traits>::FunctionState::FunctionState( |
| 2046 FunctionState** function_state_stack, Scope** scope_stack, Scope* scope, | 2037 FunctionState** function_state_stack, Scope** scope_stack, Scope* scope, |
| 2047 FunctionKind kind, typename Traits::Type::Factory* factory) | 2038 FunctionKind kind, typename Traits::Type::Factory* factory) |
| 2048 : next_materialized_literal_index_(0), | 2039 : next_materialized_literal_index_(0), |
| 2049 next_handler_index_(0), | |
| 2050 expected_property_count_(0), | 2040 expected_property_count_(0), |
| 2051 this_location_(Scanner::Location::invalid()), | 2041 this_location_(Scanner::Location::invalid()), |
| 2052 return_location_(Scanner::Location::invalid()), | 2042 return_location_(Scanner::Location::invalid()), |
| 2053 super_location_(Scanner::Location::invalid()), | 2043 super_location_(Scanner::Location::invalid()), |
| 2054 kind_(kind), | 2044 kind_(kind), |
| 2055 generator_object_variable_(NULL), | 2045 generator_object_variable_(NULL), |
| 2056 function_state_stack_(function_state_stack), | 2046 function_state_stack_(function_state_stack), |
| 2057 outer_function_state_(*function_state_stack), | 2047 outer_function_state_(*function_state_stack), |
| 2058 scope_stack_(scope_stack), | 2048 scope_stack_(scope_stack), |
| 2059 outer_scope_(*scope_stack), | 2049 outer_scope_(*scope_stack), |
| (...skipping 988 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3048 expression = ParseAssignmentExpression(false, classifier, CHECK_OK); | 3038 expression = ParseAssignmentExpression(false, classifier, CHECK_OK); |
| 3049 break; | 3039 break; |
| 3050 } | 3040 } |
| 3051 } | 3041 } |
| 3052 if (kind == Yield::kDelegating) { | 3042 if (kind == Yield::kDelegating) { |
| 3053 // var iterator = subject[Symbol.iterator](); | 3043 // var iterator = subject[Symbol.iterator](); |
| 3054 expression = this->GetIterator(expression, factory()); | 3044 expression = this->GetIterator(expression, factory()); |
| 3055 } | 3045 } |
| 3056 typename Traits::Type::YieldExpression yield = | 3046 typename Traits::Type::YieldExpression yield = |
| 3057 factory()->NewYield(generator_object, expression, kind, pos); | 3047 factory()->NewYield(generator_object, expression, kind, pos); |
| 3058 if (kind == Yield::kDelegating) { | |
| 3059 yield->set_index(function_state_->NextHandlerIndex()); | |
| 3060 } | |
| 3061 return yield; | 3048 return yield; |
| 3062 } | 3049 } |
| 3063 | 3050 |
| 3064 | 3051 |
| 3065 // Precedence = 3 | 3052 // Precedence = 3 |
| 3066 template <class Traits> | 3053 template <class Traits> |
| 3067 typename ParserBase<Traits>::ExpressionT | 3054 typename ParserBase<Traits>::ExpressionT |
| 3068 ParserBase<Traits>::ParseConditionalExpression(bool accept_IN, | 3055 ParserBase<Traits>::ParseConditionalExpression(bool accept_IN, |
| 3069 ExpressionClassifier* classifier, | 3056 ExpressionClassifier* classifier, |
| 3070 bool* ok) { | 3057 bool* ok) { |
| (...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3778 // If next token is not `=>`, it's a syntax error anyways. | 3765 // If next token is not `=>`, it's a syntax error anyways. |
| 3779 ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW); | 3766 ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW); |
| 3780 *ok = false; | 3767 *ok = false; |
| 3781 return this->EmptyExpression(); | 3768 return this->EmptyExpression(); |
| 3782 } | 3769 } |
| 3783 | 3770 |
| 3784 typename Traits::Type::StatementList body; | 3771 typename Traits::Type::StatementList body; |
| 3785 int num_parameters = scope->num_parameters(); | 3772 int num_parameters = scope->num_parameters(); |
| 3786 int materialized_literal_count = -1; | 3773 int materialized_literal_count = -1; |
| 3787 int expected_property_count = -1; | 3774 int expected_property_count = -1; |
| 3788 int handler_count = 0; | |
| 3789 Scanner::Location super_loc; | 3775 Scanner::Location super_loc; |
| 3790 | 3776 |
| 3791 { | 3777 { |
| 3792 typename Traits::Type::Factory function_factory(ast_value_factory()); | 3778 typename Traits::Type::Factory function_factory(ast_value_factory()); |
| 3793 FunctionState function_state(&function_state_, &scope_, scope, | 3779 FunctionState function_state(&function_state_, &scope_, scope, |
| 3794 kArrowFunction, &function_factory); | 3780 kArrowFunction, &function_factory); |
| 3795 | 3781 |
| 3796 Expect(Token::ARROW, CHECK_OK); | 3782 Expect(Token::ARROW, CHECK_OK); |
| 3797 | 3783 |
| 3798 if (peek() == Token::LBRACE) { | 3784 if (peek() == Token::LBRACE) { |
| 3799 // Multiple statement body | 3785 // Multiple statement body |
| 3800 Consume(Token::LBRACE); | 3786 Consume(Token::LBRACE); |
| 3801 bool is_lazily_parsed = | 3787 bool is_lazily_parsed = |
| 3802 (mode() == PARSE_LAZILY && scope_->AllowsLazyCompilation()); | 3788 (mode() == PARSE_LAZILY && scope_->AllowsLazyCompilation()); |
| 3803 if (is_lazily_parsed) { | 3789 if (is_lazily_parsed) { |
| 3804 body = this->NewStatementList(0, zone()); | 3790 body = this->NewStatementList(0, zone()); |
| 3805 this->SkipLazyFunctionBody(&materialized_literal_count, | 3791 this->SkipLazyFunctionBody(&materialized_literal_count, |
| 3806 &expected_property_count, CHECK_OK); | 3792 &expected_property_count, CHECK_OK); |
| 3807 } else { | 3793 } else { |
| 3808 body = this->ParseEagerFunctionBody( | 3794 body = this->ParseEagerFunctionBody( |
| 3809 this->EmptyIdentifier(), RelocInfo::kNoPosition, NULL, | 3795 this->EmptyIdentifier(), RelocInfo::kNoPosition, NULL, |
| 3810 Token::INIT_VAR, kArrowFunction, CHECK_OK); | 3796 Token::INIT_VAR, kArrowFunction, CHECK_OK); |
| 3811 materialized_literal_count = | 3797 materialized_literal_count = |
| 3812 function_state.materialized_literal_count(); | 3798 function_state.materialized_literal_count(); |
| 3813 expected_property_count = function_state.expected_property_count(); | 3799 expected_property_count = function_state.expected_property_count(); |
| 3814 handler_count = function_state.handler_count(); | |
| 3815 } | 3800 } |
| 3816 } else { | 3801 } else { |
| 3817 // Single-expression body | 3802 // Single-expression body |
| 3818 int pos = position(); | 3803 int pos = position(); |
| 3819 parenthesized_function_ = false; | 3804 parenthesized_function_ = false; |
| 3820 ExpressionClassifier classifier; | 3805 ExpressionClassifier classifier; |
| 3821 ExpressionT expression = | 3806 ExpressionT expression = |
| 3822 ParseAssignmentExpression(true, &classifier, CHECK_OK); | 3807 ParseAssignmentExpression(true, &classifier, CHECK_OK); |
| 3823 ValidateExpression(&classifier, CHECK_OK); | 3808 ValidateExpression(&classifier, CHECK_OK); |
| 3824 body = this->NewStatementList(1, zone()); | 3809 body = this->NewStatementList(1, zone()); |
| 3825 body->Add(factory()->NewReturnStatement(expression, pos), zone()); | 3810 body->Add(factory()->NewReturnStatement(expression, pos), zone()); |
| 3826 materialized_literal_count = function_state.materialized_literal_count(); | 3811 materialized_literal_count = function_state.materialized_literal_count(); |
| 3827 expected_property_count = function_state.expected_property_count(); | 3812 expected_property_count = function_state.expected_property_count(); |
| 3828 handler_count = function_state.handler_count(); | |
| 3829 } | 3813 } |
| 3830 super_loc = function_state.super_location(); | 3814 super_loc = function_state.super_location(); |
| 3831 | 3815 |
| 3832 scope->set_end_position(scanner()->location().end_pos); | 3816 scope->set_end_position(scanner()->location().end_pos); |
| 3833 | 3817 |
| 3834 // Arrow function formal parameters are parsed as StrictFormalParameterList, | 3818 // Arrow function formal parameters are parsed as StrictFormalParameterList, |
| 3835 // which is not the same as "parameters of a strict function"; it only means | 3819 // which is not the same as "parameters of a strict function"; it only means |
| 3836 // that duplicates are not allowed. Of course, the arrow function may | 3820 // that duplicates are not allowed. Of course, the arrow function may |
| 3837 // itself be strict as well. | 3821 // itself be strict as well. |
| 3838 const bool allow_duplicate_parameters = false; | 3822 const bool allow_duplicate_parameters = false; |
| 3839 this->ValidateFormalParameters(&formals_classifier, language_mode(), | 3823 this->ValidateFormalParameters(&formals_classifier, language_mode(), |
| 3840 allow_duplicate_parameters, CHECK_OK); | 3824 allow_duplicate_parameters, CHECK_OK); |
| 3841 | 3825 |
| 3842 // Validate strict mode. | 3826 // Validate strict mode. |
| 3843 if (is_strict(language_mode())) { | 3827 if (is_strict(language_mode())) { |
| 3844 CheckStrictOctalLiteral(scope->start_position(), | 3828 CheckStrictOctalLiteral(scope->start_position(), |
| 3845 scanner()->location().end_pos, CHECK_OK); | 3829 scanner()->location().end_pos, CHECK_OK); |
| 3846 this->CheckConflictingVarDeclarations(scope, CHECK_OK); | 3830 this->CheckConflictingVarDeclarations(scope, CHECK_OK); |
| 3847 } | 3831 } |
| 3848 } | 3832 } |
| 3849 | 3833 |
| 3850 FunctionLiteralT function_literal = factory()->NewFunctionLiteral( | 3834 FunctionLiteralT function_literal = factory()->NewFunctionLiteral( |
| 3851 this->EmptyIdentifierString(), ast_value_factory(), scope, body, | 3835 this->EmptyIdentifierString(), ast_value_factory(), scope, body, |
| 3852 materialized_literal_count, expected_property_count, handler_count, | 3836 materialized_literal_count, expected_property_count, num_parameters, |
| 3853 num_parameters, FunctionLiteral::kNoDuplicateParameters, | 3837 FunctionLiteral::kNoDuplicateParameters, |
| 3854 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kIsFunction, | 3838 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kIsFunction, |
| 3855 FunctionLiteral::kShouldLazyCompile, FunctionKind::kArrowFunction, | 3839 FunctionLiteral::kShouldLazyCompile, FunctionKind::kArrowFunction, |
| 3856 scope->start_position()); | 3840 scope->start_position()); |
| 3857 | 3841 |
| 3858 function_literal->set_function_token_position(scope->start_position()); | 3842 function_literal->set_function_token_position(scope->start_position()); |
| 3859 if (super_loc.IsValid()) function_state_->set_super_location(super_loc); | 3843 if (super_loc.IsValid()) function_state_->set_super_location(super_loc); |
| 3860 | 3844 |
| 3861 if (fni_ != NULL) this->InferFunctionName(fni_, function_literal); | 3845 if (fni_ != NULL) this->InferFunctionName(fni_, function_literal); |
| 3862 | 3846 |
| 3863 return function_literal; | 3847 return function_literal; |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4049 *ok = false; | 4033 *ok = false; |
| 4050 return; | 4034 return; |
| 4051 } | 4035 } |
| 4052 has_seen_constructor_ = true; | 4036 has_seen_constructor_ = true; |
| 4053 return; | 4037 return; |
| 4054 } | 4038 } |
| 4055 } | 4039 } |
| 4056 } } // v8::internal | 4040 } } // v8::internal |
| 4057 | 4041 |
| 4058 #endif // V8_PREPARSER_H | 4042 #endif // V8_PREPARSER_H |
| OLD | NEW |