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_PARSING_PARSER_BASE_H | 5 #ifndef V8_PARSING_PARSER_BASE_H |
6 #define V8_PARSING_PARSER_BASE_H | 6 #define V8_PARSING_PARSER_BASE_H |
7 | 7 |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/scopes.h" | 9 #include "src/ast/scopes.h" |
10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 } | 52 } |
53 | 53 |
54 static inline bool operator&(ParseFunctionFlags bitfield, | 54 static inline bool operator&(ParseFunctionFlags bitfield, |
55 ParseFunctionFlags mask) { | 55 ParseFunctionFlags mask) { |
56 typedef unsigned char T; | 56 typedef unsigned char T; |
57 return static_cast<T>(bitfield) & static_cast<T>(mask); | 57 return static_cast<T>(bitfield) & static_cast<T>(mask); |
58 } | 58 } |
59 | 59 |
60 struct FormalParametersBase { | 60 struct FormalParametersBase { |
61 explicit FormalParametersBase(DeclarationScope* scope) : scope(scope) {} | 61 explicit FormalParametersBase(DeclarationScope* scope) : scope(scope) {} |
| 62 |
| 63 int num_parameters() const { |
| 64 // Don't include the rest parameter into the function's formal parameter |
| 65 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, |
| 66 // which says whether we need to create an arguments adaptor frame). |
| 67 return arity - has_rest; |
| 68 } |
| 69 |
| 70 void UpdateArityAndFunctionLength(bool is_optional, bool is_rest) { |
| 71 if (!is_optional && !is_rest && function_length == arity) { |
| 72 ++function_length; |
| 73 } |
| 74 ++arity; |
| 75 } |
| 76 |
62 DeclarationScope* scope; | 77 DeclarationScope* scope; |
63 bool has_rest = false; | 78 bool has_rest = false; |
64 bool is_simple = true; | 79 bool is_simple = true; |
65 int materialized_literals_count = 0; | 80 int materialized_literals_count = 0; |
| 81 int function_length = 0; |
| 82 int arity = 0; |
66 }; | 83 }; |
67 | 84 |
68 | 85 |
69 // ---------------------------------------------------------------------------- | 86 // ---------------------------------------------------------------------------- |
70 // The CHECK_OK macro is a convenient macro to enforce error | 87 // The CHECK_OK macro is a convenient macro to enforce error |
71 // handling for functions that may fail (by returning !*ok). | 88 // handling for functions that may fail (by returning !*ok). |
72 // | 89 // |
73 // CAUTION: This macro appends extra statements after a call, | 90 // CAUTION: This macro appends extra statements after a call, |
74 // thus it must never be used where only a single statement | 91 // thus it must never be used where only a single statement |
75 // is correct (e.g. an if statement branch w/o braces)! | 92 // is correct (e.g. an if statement branch w/o braces)! |
(...skipping 2201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2277 impl()->RewriteNonPattern(CHECK_OK_CUSTOM(EmptyFunctionLiteral)); | 2294 impl()->RewriteNonPattern(CHECK_OK_CUSTOM(EmptyFunctionLiteral)); |
2278 } else { | 2295 } else { |
2279 value = factory()->NewUndefinedLiteral(kNoSourcePosition); | 2296 value = factory()->NewUndefinedLiteral(kNoSourcePosition); |
2280 } | 2297 } |
2281 initializer_scope->set_end_position(scanner()->location().end_pos); | 2298 initializer_scope->set_end_position(scanner()->location().end_pos); |
2282 typename Types::StatementList body = impl()->NewStatementList(1); | 2299 typename Types::StatementList body = impl()->NewStatementList(1); |
2283 body->Add(factory()->NewReturnStatement(value, kNoSourcePosition), zone()); | 2300 body->Add(factory()->NewReturnStatement(value, kNoSourcePosition), zone()); |
2284 FunctionLiteralT function_literal = factory()->NewFunctionLiteral( | 2301 FunctionLiteralT function_literal = factory()->NewFunctionLiteral( |
2285 impl()->EmptyIdentifierString(), initializer_scope, body, | 2302 impl()->EmptyIdentifierString(), initializer_scope, body, |
2286 initializer_state.materialized_literal_count(), | 2303 initializer_state.materialized_literal_count(), |
2287 initializer_state.expected_property_count(), 0, | 2304 initializer_state.expected_property_count(), 0, 0, |
2288 FunctionLiteral::kNoDuplicateParameters, | 2305 FunctionLiteral::kNoDuplicateParameters, |
2289 FunctionLiteral::kAnonymousExpression, default_eager_compile_hint_, | 2306 FunctionLiteral::kAnonymousExpression, default_eager_compile_hint_, |
2290 initializer_scope->start_position()); | 2307 initializer_scope->start_position()); |
2291 function_literal->set_is_class_field_initializer(true); | 2308 function_literal->set_is_class_field_initializer(true); |
2292 return function_literal; | 2309 return function_literal; |
2293 } | 2310 } |
2294 | 2311 |
2295 template <typename Impl> | 2312 template <typename Impl> |
2296 typename ParserBase<Impl>::ObjectLiteralPropertyT | 2313 typename ParserBase<Impl>::ObjectLiteralPropertyT |
2297 ParserBase<Impl>::ParseObjectPropertyDefinition(ObjectLiteralChecker* checker, | 2314 ParserBase<Impl>::ParseObjectPropertyDefinition(ObjectLiteralChecker* checker, |
(...skipping 1174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3472 // [empty] | 3489 // [empty] |
3473 // FunctionRestParameter[?Yield] | 3490 // FunctionRestParameter[?Yield] |
3474 // FormalParameterList[?Yield] | 3491 // FormalParameterList[?Yield] |
3475 // FormalParameterList[?Yield] , | 3492 // FormalParameterList[?Yield] , |
3476 // FormalParameterList[?Yield] , FunctionRestParameter[?Yield] | 3493 // FormalParameterList[?Yield] , FunctionRestParameter[?Yield] |
3477 // | 3494 // |
3478 // FormalParameterList[Yield] : | 3495 // FormalParameterList[Yield] : |
3479 // FormalParameter[?Yield] | 3496 // FormalParameter[?Yield] |
3480 // FormalParameterList[?Yield] , FormalParameter[?Yield] | 3497 // FormalParameterList[?Yield] , FormalParameter[?Yield] |
3481 | 3498 |
3482 DCHECK_EQ(0, parameters->Arity()); | 3499 DCHECK_EQ(0, parameters->arity); |
3483 | 3500 |
3484 if (peek() != Token::RPAREN) { | 3501 if (peek() != Token::RPAREN) { |
3485 while (true) { | 3502 while (true) { |
3486 if (parameters->Arity() > Code::kMaxArguments) { | 3503 if (parameters->arity > Code::kMaxArguments) { |
3487 ReportMessage(MessageTemplate::kTooManyParameters); | 3504 ReportMessage(MessageTemplate::kTooManyParameters); |
3488 *ok = false; | 3505 *ok = false; |
3489 return; | 3506 return; |
3490 } | 3507 } |
3491 parameters->has_rest = Check(Token::ELLIPSIS); | 3508 parameters->has_rest = Check(Token::ELLIPSIS); |
3492 ParseFormalParameter(parameters, CHECK_OK_CUSTOM(Void)); | 3509 ParseFormalParameter(parameters, CHECK_OK_CUSTOM(Void)); |
3493 | 3510 |
3494 if (parameters->has_rest) { | 3511 if (parameters->has_rest) { |
3495 parameters->is_simple = false; | 3512 parameters->is_simple = false; |
3496 classifier()->RecordNonSimpleParameter(); | 3513 classifier()->RecordNonSimpleParameter(); |
3497 if (peek() == Token::COMMA) { | 3514 if (peek() == Token::COMMA) { |
3498 impl()->ReportMessageAt(scanner()->peek_location(), | 3515 impl()->ReportMessageAt(scanner()->peek_location(), |
3499 MessageTemplate::kParamAfterRest); | 3516 MessageTemplate::kParamAfterRest); |
3500 *ok = false; | 3517 *ok = false; |
3501 return; | 3518 return; |
3502 } | 3519 } |
3503 break; | 3520 break; |
3504 } | 3521 } |
3505 if (!Check(Token::COMMA)) break; | 3522 if (!Check(Token::COMMA)) break; |
3506 if (allow_harmony_trailing_commas() && peek() == Token::RPAREN) { | 3523 if (allow_harmony_trailing_commas() && peek() == Token::RPAREN) { |
3507 // allow the trailing comma | 3524 // allow the trailing comma |
3508 break; | 3525 break; |
3509 } | 3526 } |
3510 } | 3527 } |
3511 } | 3528 } |
3512 | 3529 |
3513 for (int i = 0; i < parameters->Arity(); ++i) { | 3530 for (int i = 0; i < parameters->arity; ++i) { |
3514 auto parameter = parameters->at(i); | 3531 auto parameter = parameters->at(i); |
3515 impl()->DeclareFormalParameter(parameters->scope, parameter); | 3532 impl()->DeclareFormalParameter(parameters->scope, parameter); |
3516 } | 3533 } |
3517 } | 3534 } |
3518 | 3535 |
3519 template <typename Impl> | 3536 template <typename Impl> |
3520 typename ParserBase<Impl>::BlockT ParserBase<Impl>::ParseVariableDeclarations( | 3537 typename ParserBase<Impl>::BlockT ParserBase<Impl>::ParseVariableDeclarations( |
3521 VariableDeclarationContext var_context, | 3538 VariableDeclarationContext var_context, |
3522 DeclarationParsingResult* parsing_result, | 3539 DeclarationParsingResult* parsing_result, |
3523 ZoneList<const AstRawString*>* names, bool* ok) { | 3540 ZoneList<const AstRawString*>* names, bool* ok) { |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3905 if (peek() == Token::ARROW && scanner_->HasAnyLineTerminatorBeforeNext()) { | 3922 if (peek() == Token::ARROW && scanner_->HasAnyLineTerminatorBeforeNext()) { |
3906 // ASI inserts `;` after arrow parameters if a line terminator is found. | 3923 // ASI inserts `;` after arrow parameters if a line terminator is found. |
3907 // `=> ...` is never a valid expression, so report as syntax error. | 3924 // `=> ...` is never a valid expression, so report as syntax error. |
3908 // If next token is not `=>`, it's a syntax error anyways. | 3925 // If next token is not `=>`, it's a syntax error anyways. |
3909 ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW); | 3926 ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW); |
3910 *ok = false; | 3927 *ok = false; |
3911 return impl()->EmptyExpression(); | 3928 return impl()->EmptyExpression(); |
3912 } | 3929 } |
3913 | 3930 |
3914 StatementListT body = impl()->NullStatementList(); | 3931 StatementListT body = impl()->NullStatementList(); |
3915 int num_parameters = formal_parameters.scope->num_parameters(); | |
3916 int materialized_literal_count = -1; | 3932 int materialized_literal_count = -1; |
3917 int expected_property_count = -1; | 3933 int expected_property_count = -1; |
3918 | 3934 |
3919 FunctionKind kind = formal_parameters.scope->function_kind(); | 3935 FunctionKind kind = formal_parameters.scope->function_kind(); |
3920 FunctionLiteral::EagerCompileHint eager_compile_hint = | 3936 FunctionLiteral::EagerCompileHint eager_compile_hint = |
3921 default_eager_compile_hint_; | 3937 default_eager_compile_hint_; |
3922 bool can_preparse = mode() == PARSE_LAZILY && | 3938 bool can_preparse = mode() == PARSE_LAZILY && |
3923 eager_compile_hint == FunctionLiteral::kShouldLazyCompile; | 3939 eager_compile_hint == FunctionLiteral::kShouldLazyCompile; |
3924 // TODO(marja): consider lazy-parsing inner arrow functions too. is_this | 3940 // TODO(marja): consider lazy-parsing inner arrow functions too. is_this |
3925 // handling in Scope::ResolveVariable needs to change. | 3941 // handling in Scope::ResolveVariable needs to change. |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4020 CheckStrictOctalLiteral(formal_parameters.scope->start_position(), | 4036 CheckStrictOctalLiteral(formal_parameters.scope->start_position(), |
4021 scanner()->location().end_pos, CHECK_OK); | 4037 scanner()->location().end_pos, CHECK_OK); |
4022 } | 4038 } |
4023 impl()->CheckConflictingVarDeclarations(formal_parameters.scope, CHECK_OK); | 4039 impl()->CheckConflictingVarDeclarations(formal_parameters.scope, CHECK_OK); |
4024 | 4040 |
4025 impl()->RewriteDestructuringAssignments(); | 4041 impl()->RewriteDestructuringAssignments(); |
4026 } | 4042 } |
4027 | 4043 |
4028 FunctionLiteralT function_literal = factory()->NewFunctionLiteral( | 4044 FunctionLiteralT function_literal = factory()->NewFunctionLiteral( |
4029 impl()->EmptyIdentifierString(), formal_parameters.scope, body, | 4045 impl()->EmptyIdentifierString(), formal_parameters.scope, body, |
4030 materialized_literal_count, expected_property_count, num_parameters, | 4046 materialized_literal_count, expected_property_count, |
| 4047 formal_parameters.num_parameters(), formal_parameters.function_length, |
4031 FunctionLiteral::kNoDuplicateParameters, | 4048 FunctionLiteral::kNoDuplicateParameters, |
4032 FunctionLiteral::kAnonymousExpression, eager_compile_hint, | 4049 FunctionLiteral::kAnonymousExpression, eager_compile_hint, |
4033 formal_parameters.scope->start_position()); | 4050 formal_parameters.scope->start_position()); |
4034 | 4051 |
4035 function_literal->set_function_token_position( | 4052 function_literal->set_function_token_position( |
4036 formal_parameters.scope->start_position()); | 4053 formal_parameters.scope->start_position()); |
4037 if (should_be_used_once_hint) { | 4054 if (should_be_used_once_hint) { |
4038 function_literal->set_should_be_used_once_hint(); | 4055 function_literal->set_should_be_used_once_hint(); |
4039 } | 4056 } |
4040 | 4057 |
(...skipping 1405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5446 has_seen_constructor_ = true; | 5463 has_seen_constructor_ = true; |
5447 return; | 5464 return; |
5448 } | 5465 } |
5449 } | 5466 } |
5450 | 5467 |
5451 | 5468 |
5452 } // namespace internal | 5469 } // namespace internal |
5453 } // namespace v8 | 5470 } // namespace v8 |
5454 | 5471 |
5455 #endif // V8_PARSING_PARSER_BASE_H | 5472 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |