| 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 1123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1134 void ParseFormalParameter(FormalParametersT* parameters, bool* ok); | 1134 void ParseFormalParameter(FormalParametersT* parameters, bool* ok); |
| 1135 void ParseFormalParameterList(FormalParametersT* parameters, bool* ok); | 1135 void ParseFormalParameterList(FormalParametersT* parameters, bool* ok); |
| 1136 void CheckArityRestrictions(int param_count, FunctionKind function_type, | 1136 void CheckArityRestrictions(int param_count, FunctionKind function_type, |
| 1137 bool has_rest, int formals_start_pos, | 1137 bool has_rest, int formals_start_pos, |
| 1138 int formals_end_pos, bool* ok); | 1138 int formals_end_pos, bool* ok); |
| 1139 | 1139 |
| 1140 BlockT ParseVariableDeclarations(VariableDeclarationContext var_context, | 1140 BlockT ParseVariableDeclarations(VariableDeclarationContext var_context, |
| 1141 DeclarationParsingResult* parsing_result, | 1141 DeclarationParsingResult* parsing_result, |
| 1142 ZoneList<const AstRawString*>* names, | 1142 ZoneList<const AstRawString*>* names, |
| 1143 bool* ok); | 1143 bool* ok); |
| 1144 StatementT ParseHoistableDeclaration(ZoneList<const AstRawString*>* names, |
| 1145 bool default_export, bool* ok); |
| 1146 StatementT ParseHoistableDeclaration(int pos, ParseFunctionFlags flags, |
| 1147 ZoneList<const AstRawString*>* names, |
| 1148 bool default_export, bool* ok); |
| 1144 | 1149 |
| 1145 // Under some circumstances, we allow preparsing to abort if the preparsed | 1150 // Under some circumstances, we allow preparsing to abort if the preparsed |
| 1146 // function is "long and trivial", and fully parse instead. Our current | 1151 // function is "long and trivial", and fully parse instead. Our current |
| 1147 // definition of "long and trivial" is: | 1152 // definition of "long and trivial" is: |
| 1148 // - over kLazyParseTrialLimit statements | 1153 // - over kLazyParseTrialLimit statements |
| 1149 // - all starting with an identifier (i.e., no if, for, while, etc.) | 1154 // - all starting with an identifier (i.e., no if, for, while, etc.) |
| 1150 static const int kLazyParseTrialLimit = 200; | 1155 static const int kLazyParseTrialLimit = 200; |
| 1151 | 1156 |
| 1152 // TODO(nikolaos, marja): The first argument should not really be passed | 1157 // TODO(nikolaos, marja): The first argument should not really be passed |
| 1153 // by value. The method is expected to add the parsed statements to the | 1158 // by value. The method is expected to add the parsed statements to the |
| (...skipping 2432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3586 } while (Check(Token::COMMA)); | 3591 } while (Check(Token::COMMA)); |
| 3587 | 3592 |
| 3588 parsing_result->bindings_loc = | 3593 parsing_result->bindings_loc = |
| 3589 Scanner::Location(bindings_start, scanner()->location().end_pos); | 3594 Scanner::Location(bindings_start, scanner()->location().end_pos); |
| 3590 | 3595 |
| 3591 DCHECK(*ok); | 3596 DCHECK(*ok); |
| 3592 return init_block; | 3597 return init_block; |
| 3593 } | 3598 } |
| 3594 | 3599 |
| 3595 template <typename Impl> | 3600 template <typename Impl> |
| 3601 typename ParserBase<Impl>::StatementT |
| 3602 ParserBase<Impl>::ParseHoistableDeclaration( |
| 3603 ZoneList<const AstRawString*>* names, bool default_export, bool* ok) { |
| 3604 Expect(Token::FUNCTION, CHECK_OK_CUSTOM(NullStatement)); |
| 3605 int pos = position(); |
| 3606 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; |
| 3607 if (Check(Token::MUL)) { |
| 3608 flags |= ParseFunctionFlags::kIsGenerator; |
| 3609 } |
| 3610 return ParseHoistableDeclaration(pos, flags, names, default_export, ok); |
| 3611 } |
| 3612 |
| 3613 template <typename Impl> |
| 3614 typename ParserBase<Impl>::StatementT |
| 3615 ParserBase<Impl>::ParseHoistableDeclaration( |
| 3616 int pos, ParseFunctionFlags flags, ZoneList<const AstRawString*>* names, |
| 3617 bool default_export, bool* ok) { |
| 3618 // FunctionDeclaration :: |
| 3619 // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}' |
| 3620 // 'function' '(' FormalParameters ')' '{' FunctionBody '}' |
| 3621 // GeneratorDeclaration :: |
| 3622 // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}' |
| 3623 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}' |
| 3624 // |
| 3625 // The anonymous forms are allowed iff [default_export] is true. |
| 3626 // |
| 3627 // 'function' and '*' (if present) have been consumed by the caller. |
| 3628 |
| 3629 const bool is_generator = flags & ParseFunctionFlags::kIsGenerator; |
| 3630 const bool is_async = flags & ParseFunctionFlags::kIsAsync; |
| 3631 DCHECK(!is_generator || !is_async); |
| 3632 |
| 3633 IdentifierT name; |
| 3634 FunctionNameValidity name_validity; |
| 3635 IdentifierT variable_name; |
| 3636 if (default_export && peek() == Token::LPAREN) { |
| 3637 impl()->GetDefaultStrings(&name, &variable_name); |
| 3638 name_validity = kSkipFunctionNameCheck; |
| 3639 } else { |
| 3640 bool is_strict_reserved; |
| 3641 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved, |
| 3642 CHECK_OK_CUSTOM(NullStatement)); |
| 3643 name_validity = is_strict_reserved ? kFunctionNameIsStrictReserved |
| 3644 : kFunctionNameValidityUnknown; |
| 3645 variable_name = name; |
| 3646 } |
| 3647 |
| 3648 FuncNameInferrer::State fni_state(fni_); |
| 3649 impl()->PushEnclosingName(name); |
| 3650 FunctionLiteralT function = impl()->ParseFunctionLiteral( |
| 3651 name, scanner()->location(), name_validity, |
| 3652 is_generator ? FunctionKind::kGeneratorFunction |
| 3653 : is_async ? FunctionKind::kAsyncFunction |
| 3654 : FunctionKind::kNormalFunction, |
| 3655 pos, FunctionLiteral::kDeclaration, language_mode(), |
| 3656 CHECK_OK_CUSTOM(NullStatement)); |
| 3657 |
| 3658 return impl()->DeclareFunction(variable_name, function, pos, is_generator, |
| 3659 is_async, names, ok); |
| 3660 } |
| 3661 |
| 3662 template <typename Impl> |
| 3596 void ParserBase<Impl>::CheckArityRestrictions(int param_count, | 3663 void ParserBase<Impl>::CheckArityRestrictions(int param_count, |
| 3597 FunctionKind function_kind, | 3664 FunctionKind function_kind, |
| 3598 bool has_rest, | 3665 bool has_rest, |
| 3599 int formals_start_pos, | 3666 int formals_start_pos, |
| 3600 int formals_end_pos, bool* ok) { | 3667 int formals_end_pos, bool* ok) { |
| 3601 if (IsGetterFunction(function_kind)) { | 3668 if (IsGetterFunction(function_kind)) { |
| 3602 if (param_count != 0) { | 3669 if (param_count != 0) { |
| 3603 impl()->ReportMessageAt( | 3670 impl()->ReportMessageAt( |
| 3604 Scanner::Location(formals_start_pos, formals_end_pos), | 3671 Scanner::Location(formals_start_pos, formals_end_pos), |
| 3605 MessageTemplate::kBadGetterArity); | 3672 MessageTemplate::kBadGetterArity); |
| (...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4369 has_seen_constructor_ = true; | 4436 has_seen_constructor_ = true; |
| 4370 return; | 4437 return; |
| 4371 } | 4438 } |
| 4372 } | 4439 } |
| 4373 | 4440 |
| 4374 | 4441 |
| 4375 } // namespace internal | 4442 } // namespace internal |
| 4376 } // namespace v8 | 4443 } // namespace v8 |
| 4377 | 4444 |
| 4378 #endif // V8_PARSING_PARSER_BASE_H | 4445 #endif // V8_PARSING_PARSER_BASE_H |
| OLD | NEW |