5 #ifndef V8_PARSING_PARSER_BASE_H_ 6 #define V8_PARSING_PARSER_BASE_H_ 11 #include "src/ast/ast-source-ranges.h" 12 #include "src/ast/ast.h" 13 #include "src/ast/scopes.h" 14 #include "src/bailout-reason.h" 15 #include "src/base/flags.h" 16 #include "src/base/hashmap.h" 17 #include "src/base/v8-fallthrough.h" 18 #include "src/counters.h" 19 #include "src/globals.h" 21 #include "src/message-template.h" 22 #include "src/parsing/expression-classifier.h" 23 #include "src/parsing/func-name-inferrer.h" 24 #include "src/parsing/scanner.h" 25 #include "src/parsing/token.h" 26 #include "src/pointer-with-payload.h" 27 #include "src/zone/zone-chunk-list.h" 32 enum FunctionNameValidity {
33 kFunctionNameIsStrictReserved,
34 kSkipFunctionNameCheck,
35 kFunctionNameValidityUnknown
38 enum AllowLabelledFunctionStatement {
39 kAllowLabelledFunctionStatement,
40 kDisallowLabelledFunctionStatement,
43 enum class ParseFunctionFlag : uint8_t {
45 kIsGenerator = 1 << 0,
49 typedef base::Flags<ParseFunctionFlag> ParseFunctionFlags;
54 int num_parameters()
const {
58 return arity - has_rest;
61 void UpdateArityAndFunctionLength(
bool is_optional,
bool is_rest) {
62 if (!is_optional && !is_rest && function_length == arity) {
69 bool has_rest =
false;
70 bool is_simple =
true;
71 int function_length = 0;
79 : scanner_(scanner), range_(range) {
80 range_->start = scanner->peek_location().beg_pos;
81 DCHECK_NE(range_->start, kNoSourcePosition);
82 DCHECK_EQ(range_->end, kNoSourcePosition);
86 DCHECK_EQ(kNoSourcePosition, range_->end);
87 range_->end = scanner_->location().end_pos;
88 DCHECK_NE(range_->end, kNoSourcePosition);
109 #define RETURN_IF_PARSE_ERROR \ 110 if (has_error()) return impl()->NullStatement(); 175 template <
typename Impl>
178 enum class ParsePropertyKind : uint8_t {
186 kShorthandOrClassField,
191 template <
typename Impl>
200 typedef typename Types::Block BlockT;
201 typedef typename Types::BreakableStatement BreakableStatementT;
202 typedef typename Types::ClassLiteralProperty ClassLiteralPropertyT;
203 typedef typename Types::ClassPropertyList ClassPropertyListT;
204 typedef typename Types::Expression ExpressionT;
205 typedef typename Types::ExpressionList ExpressionListT;
206 typedef typename Types::FormalParameters FormalParametersT;
207 typedef typename Types::ForStatement ForStatementT;
208 typedef typename Types::FunctionLiteral FunctionLiteralT;
209 typedef typename Types::Identifier IdentifierT;
210 typedef typename Types::IterationStatement IterationStatementT;
211 typedef typename Types::ObjectLiteralProperty ObjectLiteralPropertyT;
212 typedef typename Types::ObjectPropertyList ObjectPropertyListT;
213 typedef typename Types::RewritableExpression RewritableExpressionT;
214 typedef typename Types::Statement StatementT;
215 typedef typename Types::StatementList StatementListT;
216 typedef typename Types::Suspend SuspendExpressionT;
218 typedef typename Types::Factory FactoryT;
220 typedef typename Types::FuncNameInferrer FuncNameInferrer;
221 typedef typename Types::FuncNameInferrer::State FuncNameInferrerState;
222 typedef typename Types::SourceRange SourceRange;
223 typedef typename Types::SourceRangeScope SourceRangeScope;
224 typedef typename Types::Target TargetT;
225 typedef typename Types::TargetScope TargetScopeT;
228 Impl* impl() {
return static_cast<Impl*
>(
this); }
229 const Impl* impl()
const {
return static_cast<const Impl*
>(
this); }
235 int script_id,
bool parsing_module,
bool parsing_on_main_thread)
237 original_scope_(
nullptr),
238 function_state_(
nullptr),
239 extension_(extension),
240 fni_(ast_value_factory),
241 ast_value_factory_(ast_value_factory),
242 ast_node_factory_(ast_value_factory, zone),
243 runtime_call_stats_(runtime_call_stats),
245 parsing_on_main_thread_(parsing_on_main_thread),
246 parsing_module_(parsing_module),
247 stack_limit_(stack_limit),
248 pending_error_handler_(pending_error_handler),
250 classifier_(
nullptr),
252 function_literal_id_(0),
253 script_id_(script_id),
254 default_eager_compile_hint_(FunctionLiteral::kShouldLazyCompile),
255 allow_natives_(
false),
256 allow_harmony_public_fields_(
false),
257 allow_harmony_static_fields_(
false),
258 allow_harmony_dynamic_import_(
false),
259 allow_harmony_import_meta_(
false),
260 allow_harmony_private_fields_(
false),
261 allow_harmony_private_methods_(
false),
262 allow_eval_cache_(
true) {
263 pointer_buffer_.reserve(128);
266 #define ALLOW_ACCESSORS(name) \ 267 bool allow_##name() const { return allow_##name##_; } \ 268 void set_allow_##name(bool allow) { allow_##name##_ = allow; } 270 void set_rewritable_length(
int i) { rewritable_length_ =
i; }
272 ALLOW_ACCESSORS(natives);
273 ALLOW_ACCESSORS(harmony_public_fields);
274 ALLOW_ACCESSORS(harmony_static_fields);
275 ALLOW_ACCESSORS(harmony_dynamic_import);
276 ALLOW_ACCESSORS(harmony_import_meta);
277 ALLOW_ACCESSORS(harmony_private_methods);
278 ALLOW_ACCESSORS(eval_cache);
280 #undef ALLOW_ACCESSORS 282 V8_INLINE
bool has_error()
const {
return scanner()->has_parser_error(); }
283 bool allow_harmony_numeric_separator()
const {
284 return scanner()->allow_harmony_numeric_separator();
286 void set_allow_harmony_numeric_separator(
bool allow) {
287 scanner()->set_allow_harmony_numeric_separator(allow);
290 bool allow_harmony_private_fields()
const {
291 return scanner()->allow_harmony_private_fields();
293 void set_allow_harmony_private_fields(
bool allow) {
294 scanner()->set_allow_harmony_private_fields(allow);
297 uintptr_t stack_limit()
const {
return stack_limit_; }
299 void set_stack_limit(
uintptr_t stack_limit) { stack_limit_ = stack_limit; }
301 void set_default_eager_compile_hint(
302 FunctionLiteral::EagerCompileHint eager_compile_hint) {
303 default_eager_compile_hint_ = eager_compile_hint;
306 FunctionLiteral::EagerCompileHint default_eager_compile_hint()
const {
307 return default_eager_compile_hint_;
310 int GetNextFunctionLiteralId() {
return ++function_literal_id_; }
311 int GetLastFunctionLiteralId()
const {
return function_literal_id_; }
313 void SkipFunctionLiterals(
int delta) { function_literal_id_ += delta; }
315 void ResetFunctionLiteralId() { function_literal_id_ = 0; }
318 Zone* main_zone()
const {
return ast_value_factory()->zone(); }
321 Zone* zone()
const {
return zone_; }
326 enum AllowRestrictedIdentifiers {
327 kAllowRestrictedIdentifiers,
328 kDontAllowRestrictedIdentifiers
331 enum LazyParsingResult { kLazyParsingComplete, kLazyParsingAborted };
333 enum VariableDeclarationContext {
339 class ClassLiteralChecker;
349 : scope_stack_(scope_stack), outer_scope_(*scope_stack) {
350 *scope_stack_ = scope;
355 new (zone)
Scope(zone, *scope_stack, BLOCK_SCOPE)) {}
357 ~
BlockState() { *scope_stack_ = outer_scope_; }
360 Scope**
const scope_stack_;
361 Scope*
const outer_scope_;
372 void AddProperty() { expected_property_count_++; }
373 int expected_property_count() {
return expected_property_count_; }
375 void DisableOptimization(BailoutReason reason) {
376 dont_optimize_reason_ = reason;
378 BailoutReason dont_optimize_reason() {
return dont_optimize_reason_; }
380 void AddSuspend() { suspend_count_++; }
381 int suspend_count()
const {
return suspend_count_; }
382 bool CanSuspend()
const {
return suspend_count_ > 0; }
384 FunctionKind kind()
const {
return scope()->function_kind(); }
386 void RewindDestructuringAssignments(
int pos) {
387 destructuring_assignments_to_rewrite_.Rewind(pos);
390 void AdoptDestructuringAssignmentsFromParentState(
int pos) {
391 const auto& outer_assignments =
392 outer_function_state_->destructuring_assignments_to_rewrite_;
393 DCHECK_GE(outer_assignments.size(), pos);
394 auto it = outer_assignments.begin();
396 for (; it != outer_assignments.end(); ++it) {
398 expr->set_scope(scope_);
399 destructuring_assignments_to_rewrite_.push_back(expr);
401 outer_function_state_->RewindDestructuringAssignments(pos);
405 destructuring_assignments_to_rewrite()
const {
406 return destructuring_assignments_to_rewrite_;
410 return &reported_errors_;
413 bool next_function_is_likely_called()
const {
414 return next_function_is_likely_called_;
417 bool previous_function_was_likely_called()
const {
418 return previous_function_was_likely_called_;
421 void set_next_function_is_likely_called() {
422 next_function_is_likely_called_ =
true;
425 void RecordFunctionOrEvalCall() { contains_function_or_eval_ =
true; }
426 bool contains_function_or_eval()
const {
427 return contains_function_or_eval_;
433 : state_and_prev_value_(state, state->contains_function_or_eval_) {
434 state->contains_function_or_eval_ =
false;
437 bool found = state_and_prev_value_->contains_function_or_eval_;
439 state_and_prev_value_->contains_function_or_eval_ =
440 state_and_prev_value_.GetPayload();
449 void AddDestructuringAssignment(RewritableExpressionT expr) {
450 destructuring_assignments_to_rewrite_.push_back(expr);
454 int expected_property_count_;
468 BailoutReason dont_optimize_reason_;
475 bool next_function_is_likely_called_;
476 bool previous_function_was_likely_called_;
479 bool contains_function_or_eval_;
485 enum Kind { NORMAL, PARAMETER, FOR_EACH };
489 int initialization_pos;
490 Kind declaration_kind;
495 Declaration(ExpressionT pattern,
int initializer_position,
496 ExpressionT initializer)
498 initializer_position(initializer_position),
499 initializer(initializer) {}
502 int initializer_position;
503 int value_beg_position = kNoSourcePosition;
504 ExpressionT initializer;
512 std::vector<Declaration> declarations;
520 : name(parser->impl()->NullIdentifier()),
521 pattern(parser->impl()->NullExpression()),
523 init_block(parser->impl()->NullStatement()),
524 inner_block(parser->impl()->NullStatement()),
525 bound_names(1, parser->zone()) {}
537 : bound_names(1, parser->zone()),
538 mode(ForEachStatement::ENUMERATE),
539 position(kNoSourcePosition),
542 ForEachStatement::VisitMode mode;
551 extends(parser->impl()->NullExpression()),
552 properties(parser->impl()->NewClassPropertyList(4)),
553 static_fields(parser->impl()->NewClassPropertyList(4)),
554 instance_fields(parser->impl()->NewClassPropertyList(4)),
555 constructor(parser->impl()->NullExpression()),
556 has_seen_constructor(
false),
557 has_name_static_property(
false),
558 has_static_computed_names(
false),
559 has_static_class_fields(
false),
560 has_instance_members(
false),
562 static_fields_scope(
nullptr),
563 instance_members_scope(
nullptr),
564 computed_field_count(0) {}
567 ClassPropertyListT properties;
568 ClassPropertyListT static_fields;
569 ClassPropertyListT instance_fields;
570 FunctionLiteralT constructor;
572 bool has_seen_constructor;
573 bool has_name_static_property;
574 bool has_static_computed_names;
575 bool has_static_class_fields;
576 bool has_instance_members;
580 int computed_field_count;
583 enum class PropertyPosition { kObjectLiteral, kClassLiteral };
587 : name(parser->impl()->NullIdentifier()),
588 position(PropertyPosition::kClassLiteral),
589 function_flags(ParseFunctionFlag::kIsNormal),
590 kind(ParsePropertyKind::kNotSet),
591 is_computed_name(
false),
596 bool ParsePropertyKindFromToken(Token::Value token) {
603 kind = ParsePropertyKind::kValue;
606 kind = ParsePropertyKind::kShorthand;
609 kind = ParsePropertyKind::kShorthandOrClassField;
612 kind = ParsePropertyKind::kAssign;
615 kind = ParsePropertyKind::kMethod;
618 case Token::SEMICOLON:
619 kind = ParsePropertyKind::kClassField;
628 PropertyPosition position;
630 ParsePropertyKind kind;
631 bool is_computed_name;
637 ClassLiteralProperty::Kind ClassPropertyKindFor(ParsePropertyKind kind) {
639 case ParsePropertyKind::kAccessorGetter:
640 return ClassLiteralProperty::GETTER;
641 case ParsePropertyKind::kAccessorSetter:
642 return ClassLiteralProperty::SETTER;
643 case ParsePropertyKind::kMethod:
644 return ClassLiteralProperty::METHOD;
645 case ParsePropertyKind::kClassField:
646 return ClassLiteralProperty::FIELD;
653 const AstRawString* ClassFieldVariableName(AstValueFactory* ast_value_factory,
655 std::string name =
".class-field-" + std::to_string(index);
656 return ast_value_factory->GetOneByteString(name.c_str());
659 DeclarationScope* NewScriptScope()
const {
660 return new (zone()) DeclarationScope(zone(), ast_value_factory());
663 DeclarationScope* NewVarblockScope()
const {
664 return new (zone()) DeclarationScope(zone(), scope(), BLOCK_SCOPE);
667 ModuleScope* NewModuleScope(DeclarationScope* parent)
const {
668 return new (zone()) ModuleScope(parent, ast_value_factory());
671 DeclarationScope* NewEvalScope(Scope* parent)
const {
672 return new (zone()) DeclarationScope(zone(), parent, EVAL_SCOPE);
675 Scope* NewScope(ScopeType scope_type)
const {
676 return NewScopeWithParent(scope(), scope_type);
682 Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type)
const {
685 DCHECK_NE(FUNCTION_SCOPE, scope_type);
686 DCHECK_NE(SCRIPT_SCOPE, scope_type);
687 DCHECK_NE(MODULE_SCOPE, scope_type);
688 DCHECK_NOT_NULL(parent);
689 return new (zone()) Scope(zone(), parent, scope_type);
695 DeclarationScope* NewFunctionScope(FunctionKind kind,
696 Zone* parse_zone =
nullptr)
const {
697 DCHECK(ast_value_factory());
698 if (parse_zone ==
nullptr) parse_zone = zone();
699 DeclarationScope* result =
new (zone())
700 DeclarationScope(parse_zone, scope(), FUNCTION_SCOPE, kind);
703 function_state_->RecordFunctionOrEvalCall();
706 if (!IsArrowFunction(kind)) {
707 result->DeclareDefaultFunctionVariables(ast_value_factory());
712 V8_INLINE DeclarationScope* GetDeclarationScope()
const {
713 return scope()->GetDeclarationScope();
715 V8_INLINE DeclarationScope* GetClosureScope()
const {
716 return scope()->GetClosureScope();
719 Scanner* scanner()
const {
return scanner_; }
720 AstValueFactory* ast_value_factory()
const {
return ast_value_factory_; }
721 int position()
const {
return scanner_->location().beg_pos; }
722 int peek_position()
const {
return scanner_->peek_location().beg_pos; }
723 int end_position()
const {
return scanner_->location().end_pos; }
724 int peek_end_position()
const {
return scanner_->peek_location().end_pos; }
725 bool stack_overflow()
const {
726 return pending_error_handler()->stack_overflow();
728 void set_stack_overflow() {
729 scanner_->set_parser_error();
730 pending_error_handler()->set_stack_overflow();
732 void CheckStackOverflow() {
734 if (GetCurrentStackPosition() < stack_limit_) set_stack_overflow();
736 int script_id() {
return script_id_; }
737 void set_script_id(
int id) { script_id_ = id; }
739 V8_INLINE Token::Value peek() {
return scanner()->peek(); }
743 int PositionAfterSemicolon() {
744 return (peek() == Token::SEMICOLON) ? peek_end_position() : end_position();
747 V8_INLINE Token::Value PeekAhead() {
return scanner()->PeekAhead(); }
749 V8_INLINE Token::Value Next() {
return scanner()->Next(); }
751 V8_INLINE
void Consume(Token::Value token) {
752 Token::Value next = scanner()->Next();
755 DCHECK_IMPLIES(!has_error(), next == token);
758 V8_INLINE
bool Check(Token::Value token) {
759 Token::Value next = scanner()->peek();
767 void Expect(Token::Value token) {
768 Token::Value next = Next();
769 if (V8_UNLIKELY(next != token)) {
770 ReportUnexpectedToken(next);
774 void ExpectSemicolon() {
777 Token::Value tok = peek();
778 if (V8_LIKELY(tok == Token::SEMICOLON)) {
782 if (V8_LIKELY(scanner()->HasLineTerminatorBeforeNext() ||
783 Token::IsAutoSemicolon(tok))) {
787 if (scanner()->current_token() == Token::AWAIT && !is_async_function()) {
788 ReportMessageAt(scanner()->location(),
789 MessageTemplate::kAwaitNotInAsyncFunction, kSyntaxError);
793 ReportUnexpectedToken(Next());
796 bool peek_any_identifier() {
return Token::IsAnyIdentifier(peek()); }
798 bool PeekContextualKeyword(
const AstRawString* name) {
799 return peek() == Token::IDENTIFIER &&
800 scanner()->NextSymbol(ast_value_factory()) == name;
803 bool CheckContextualKeyword(
const AstRawString* name) {
804 if (PeekContextualKeyword(name)) {
805 Consume(Token::IDENTIFIER);
811 void ExpectMetaProperty(
const AstRawString* property_name,
812 const char* full_name,
int pos);
814 void ExpectContextualKeyword(
const AstRawString* name) {
815 Expect(Token::IDENTIFIER);
816 if (V8_UNLIKELY(scanner()->CurrentSymbol(ast_value_factory()) != name)) {
817 ReportUnexpectedToken(scanner()->current_token());
821 bool CheckInOrOf(ForEachStatement::VisitMode* visit_mode) {
822 if (Check(Token::IN)) {
823 *visit_mode = ForEachStatement::ENUMERATE;
825 }
else if (CheckContextualKeyword(ast_value_factory()->of_string())) {
826 *visit_mode = ForEachStatement::ITERATE;
833 return peek() == Token::IN ||
834 PeekContextualKeyword(ast_value_factory()->of_string());
839 void CheckStrictOctalLiteral(
int beg_pos,
int end_pos) {
840 Scanner::Location octal = scanner()->octal_position();
841 if (octal.IsValid() && beg_pos <= octal.beg_pos &&
842 octal.end_pos <= end_pos) {
843 MessageTemplate message = scanner()->octal_message();
844 DCHECK_NE(message, MessageTemplate::kNone);
845 impl()->ReportMessageAt(octal, message);
846 scanner()->clear_octal_position();
847 if (message == MessageTemplate::kStrictDecimalWithLeadingZero) {
848 impl()->CountUsage(v8::Isolate::kDecimalWithLeadingZeroInStrictMode);
857 inline bool CheckTemplateEscapes(
bool should_throw) {
858 DCHECK(Token::IsTemplate(scanner()->current_token()));
859 if (!scanner()->has_invalid_template_escape())
return true;
863 impl()->ReportMessageAt(scanner()->invalid_template_escape_location(),
864 scanner()->invalid_template_escape_message());
866 scanner()->clear_invalid_template_escape_message();
870 void CheckDestructuringElement(ExpressionT element,
int beg_pos,
int end_pos);
871 void CheckArrowFormalParameter(ExpressionT formal);
875 void CheckFunctionName(LanguageMode language_mode, IdentifierT function_name,
876 FunctionNameValidity function_name_validity,
877 const Scanner::Location& function_name_loc) {
878 if (impl()->IsNull(function_name))
return;
879 if (function_name_validity == kSkipFunctionNameCheck)
return;
881 if (is_sloppy(language_mode))
return;
883 if (impl()->IsEvalOrArguments(function_name)) {
884 impl()->ReportMessageAt(function_name_loc,
885 MessageTemplate::kStrictEvalArguments);
888 if (function_name_validity == kFunctionNameIsStrictReserved) {
889 impl()->ReportMessageAt(function_name_loc,
890 MessageTemplate::kUnexpectedStrictReserved);
895 typename Types::Factory* factory() {
return &ast_node_factory_; }
897 DeclarationScope* GetReceiverScope()
const {
898 return scope()->GetReceiverScope();
900 LanguageMode language_mode() {
return scope()->language_mode(); }
901 void RaiseLanguageMode(LanguageMode mode) {
902 LanguageMode old = scope()->language_mode();
903 impl()->SetLanguageMode(scope(), old > mode ? old : mode);
905 bool is_generator()
const {
906 return IsGeneratorFunction(function_state_->kind());
908 bool is_async_function()
const {
909 return IsAsyncFunction(function_state_->kind());
911 bool is_async_generator()
const {
912 return IsAsyncGeneratorFunction(function_state_->kind());
914 bool is_resumable()
const {
915 return IsResumableFunction(function_state_->kind());
918 const PendingCompilationErrorHandler* pending_error_handler()
const {
919 return pending_error_handler_;
921 PendingCompilationErrorHandler* pending_error_handler() {
922 return pending_error_handler_;
926 V8_NOINLINE
void ReportMessage(MessageTemplate message) {
927 Scanner::Location source_location = scanner()->location();
928 impl()->ReportMessageAt(source_location, message,
929 static_cast<const char*>(
nullptr), kSyntaxError);
932 template <
typename T>
933 V8_NOINLINE
void ReportMessage(MessageTemplate message, T arg,
934 ParseErrorType error_type = kSyntaxError) {
935 Scanner::Location source_location = scanner()->location();
936 impl()->ReportMessageAt(source_location, message, arg, error_type);
939 V8_NOINLINE
void ReportMessageAt(Scanner::Location location,
940 MessageTemplate message,
941 ParseErrorType error_type) {
942 impl()->ReportMessageAt(location, message,
943 static_cast<const char*>(
nullptr), error_type);
946 V8_NOINLINE
void ReportUnexpectedToken(Token::Value token);
947 V8_NOINLINE
void ReportUnexpectedTokenAt(
948 Scanner::Location location, Token::Value token,
949 MessageTemplate message = MessageTemplate::kUnexpectedToken);
951 V8_NOINLINE
void ReportClassifierError(
952 const typename ExpressionClassifier::Error& error) {
953 if (classifier()->does_error_reporting()) {
954 impl()->ReportMessageAt(error.location, error.message(), error.arg);
956 impl()->ReportUnidentifiableError();
960 void ValidateExpression() {
961 if (!classifier()->is_valid_expression()) {
962 ReportClassifierError(classifier()->expression_error());
966 void ValidateFormalParameterInitializer() {
967 if (!classifier()->is_valid_formal_parameter_initializer()) {
968 ReportClassifierError(classifier()->formal_parameter_initializer_error());
972 void ValidateBindingPattern() {
973 if (!classifier()->is_valid_binding_pattern()) {
974 ReportClassifierError(classifier()->binding_pattern_error());
979 void ValidatePattern() {
980 if (!classifier()->is_valid_pattern()) {
981 ReportClassifierError(classifier()->pattern_error());
985 void ValidatePattern(ExpressionT expression) {
986 if (expression->is_parenthesized()) {
987 impl()->ReportMessageAt(
988 Scanner::Location(expression->position(), end_position()),
989 MessageTemplate::kInvalidDestructuringTarget);
994 void ValidateFormalParameters(LanguageMode language_mode,
995 const FormalParametersT& parameters,
996 bool allow_duplicates) {
997 if (!allow_duplicates && parameters.has_duplicate()) {
998 if (classifier()->does_error_reporting()) {
999 impl()->ReportMessageAt(parameters.duplicate_location(),
1000 MessageTemplate::kParamDupe);
1002 impl()->ReportUnidentifiableError();
1004 }
else if (is_strict(language_mode) &&
1005 !classifier()->is_valid_strict_mode_formal_parameters()) {
1006 ReportClassifierError(classifier()->strict_mode_formal_parameter_error());
1010 void ValidateArrowFormalParameters(ExpressionT expression) {
1011 ValidateBindingPattern();
1012 if (!expression->is_parenthesized() && !impl()->IsIdentifier(expression)) {
1014 impl()->ReportMessageAt(
1015 Scanner::Location(expression->position(), position()),
1016 MessageTemplate::kMalformedArrowFunParamList);
1018 DCHECK_IMPLIES(IsAsyncFunction(next_arrow_function_kind_),
1019 classifier()->is_valid_async_arrow_formal_parameters());
1022 void ValidateLetPattern() {
1023 if (!classifier()->is_valid_let_pattern()) {
1024 ReportClassifierError(classifier()->let_pattern_error());
1033 IdentifierT ParseIdentifier(AllowRestrictedIdentifiers);
1034 V8_INLINE IdentifierT ParseAndClassifyIdentifier();
1040 IdentifierT ParseIdentifierOrStrictReservedWord(FunctionKind function_kind,
1041 bool* is_strict_reserved,
1043 IdentifierT ParseIdentifierOrStrictReservedWord(
bool* is_strict_reserved,
1045 return ParseIdentifierOrStrictReservedWord(function_state_->kind(),
1046 is_strict_reserved, is_await);
1049 V8_INLINE IdentifierT ParseIdentifierName();
1051 ExpressionT ParseIdentifierNameOrPrivateName();
1053 ExpressionT ParseRegExpLiteral();
1055 ExpressionT ParseBindingPattern();
1056 ExpressionT ParsePrimaryExpression();
1060 V8_INLINE ExpressionT ParseExpression();
1069 ExpressionT ParseExpressionCoverGrammar();
1070 ExpressionT ParseArrowFormalsWithRest(ExpressionListT* list);
1072 ExpressionT ParseArrayLiteral();
1074 inline static bool IsAccessor(ParsePropertyKind kind) {
1075 return IsInRange(kind, ParsePropertyKind::kAccessorGetter,
1076 ParsePropertyKind::kAccessorSetter);
1079 ExpressionT ParsePropertyName(ParsePropertyInfo* prop_info);
1080 ExpressionT ParseObjectLiteral();
1081 ClassLiteralPropertyT ParseClassPropertyDefinition(
1082 ClassInfo* class_info, ParsePropertyInfo* prop_info,
bool has_extends);
1083 void CheckClassFieldName(IdentifierT name,
bool is_static);
1084 void CheckClassMethodName(IdentifierT name, ParsePropertyKind type,
1085 ParseFunctionFlags flags,
bool is_static,
1086 bool* has_seen_constructor);
1087 ExpressionT ParseMemberInitializer(ClassInfo* class_info,
int beg_pos,
1089 ObjectLiteralPropertyT ParseObjectPropertyDefinition(
1090 ParsePropertyInfo* prop_info,
bool* has_seen_proto);
1091 void ParseArguments(ExpressionListT* args,
bool* has_spread,
1093 void ParseArguments(ExpressionListT* args,
bool* has_spread) {
1094 ParseArguments(args, has_spread,
false);
1097 ExpressionT ParseAssignmentExpression();
1098 ExpressionT ParseYieldExpression();
1099 V8_INLINE ExpressionT ParseConditionalExpression();
1100 ExpressionT ParseConditionalContinuation(ExpressionT expression,
int pos);
1101 ExpressionT ParseBinaryContinuation(ExpressionT x,
int prec,
int prec1);
1102 V8_INLINE ExpressionT ParseBinaryExpression(
int prec);
1103 ExpressionT ParseUnaryOrPrefixExpression();
1104 ExpressionT ParseAwaitExpression();
1105 V8_INLINE ExpressionT ParseUnaryExpression();
1106 V8_INLINE ExpressionT ParsePostfixExpression();
1107 V8_INLINE ExpressionT ParseLeftHandSideExpression();
1108 ExpressionT ParseLeftHandSideContinuation(ExpressionT expression);
1109 ExpressionT ParseMemberWithPresentNewPrefixesExpression();
1110 V8_INLINE ExpressionT ParseMemberWithNewPrefixesExpression();
1111 ExpressionT ParseFunctionExpression();
1112 V8_INLINE ExpressionT ParseMemberExpression();
1113 V8_INLINE ExpressionT
1114 ParseMemberExpressionContinuation(ExpressionT expression) {
1115 if (!Token::IsMember(peek()))
return expression;
1116 return DoParseMemberExpressionContinuation(expression);
1118 ExpressionT DoParseMemberExpressionContinuation(ExpressionT expression);
1120 ExpressionT ParseArrowFunctionLiteral(
const FormalParametersT& parameters);
1121 void ParseAsyncFunctionBody(Scope* scope, StatementListT* body);
1122 ExpressionT ParseAsyncFunctionLiteral();
1123 ExpressionT ParseClassLiteral(IdentifierT name,
1124 Scanner::Location class_name_location,
1125 bool name_is_strict_reserved,
1126 int class_token_pos);
1127 ExpressionT ParseTemplateLiteral(ExpressionT tag,
int start,
bool tagged);
1128 ExpressionT ParseSuperExpression(
bool is_new);
1129 ExpressionT ParseImportExpressions();
1130 ExpressionT ParseNewTargetExpression();
1132 V8_INLINE
void ParseFormalParameter(FormalParametersT* parameters);
1133 void ParseFormalParameterList(FormalParametersT* parameters);
1134 void CheckArityRestrictions(
int param_count, FunctionKind function_type,
1135 bool has_rest,
int formals_start_pos,
1136 int formals_end_pos);
1138 BlockT ParseVariableDeclarations(VariableDeclarationContext var_context,
1139 DeclarationParsingResult* parsing_result,
1140 ZonePtrList<const AstRawString>* names);
1141 StatementT ParseAsyncFunctionDeclaration(
1142 ZonePtrList<const AstRawString>* names,
bool default_export);
1143 StatementT ParseFunctionDeclaration();
1144 StatementT ParseHoistableDeclaration(ZonePtrList<const AstRawString>* names,
1145 bool default_export);
1146 StatementT ParseHoistableDeclaration(
int pos, ParseFunctionFlags flags,
1147 ZonePtrList<const AstRawString>* names,
1148 bool default_export);
1149 StatementT ParseClassDeclaration(ZonePtrList<const AstRawString>* names,
1150 bool default_export);
1151 StatementT ParseNativeDeclaration();
1154 enum class FunctionBodyType { kExpression, kBlock };
1156 void ParseFunctionBody(StatementListT* result, IdentifierT function_name,
1157 int pos,
const FormalParametersT& parameters,
1159 FunctionLiteral::FunctionType function_type,
1160 FunctionBodyType body_type);
1167 static const int kLazyParseTrialLimit = 200;
1173 V8_INLINE
void ParseStatementList(StatementListT* body,
1174 Token::Value end_token) {
1175 LazyParsingResult result = ParseStatementList(body, end_token,
false);
1177 DCHECK_EQ(result, kLazyParsingComplete);
1179 V8_INLINE LazyParsingResult ParseStatementList(StatementListT* body,
1180 Token::Value end_token,
1182 StatementT ParseStatementListItem();
1184 StatementT ParseStatement(ZonePtrList<const AstRawString>* labels,
1185 ZonePtrList<const AstRawString>* own_labels) {
1186 return ParseStatement(labels, own_labels,
1187 kDisallowLabelledFunctionStatement);
1189 StatementT ParseStatement(ZonePtrList<const AstRawString>* labels,
1190 ZonePtrList<const AstRawString>* own_labels,
1191 AllowLabelledFunctionStatement allow_function);
1192 BlockT ParseBlock(ZonePtrList<const AstRawString>* labels);
1197 StatementT ParseScopedStatement(ZonePtrList<const AstRawString>* labels);
1199 StatementT ParseVariableStatement(VariableDeclarationContext var_context,
1200 ZonePtrList<const AstRawString>* names);
1203 ExpressionT ParseV8Intrinsic();
1205 StatementT ParseDebuggerStatement();
1207 StatementT ParseExpressionOrLabelledStatement(
1208 ZonePtrList<const AstRawString>* labels,
1209 ZonePtrList<const AstRawString>* own_labels,
1210 AllowLabelledFunctionStatement allow_function);
1211 StatementT ParseIfStatement(ZonePtrList<const AstRawString>* labels);
1212 StatementT ParseContinueStatement();
1213 StatementT ParseBreakStatement(ZonePtrList<const AstRawString>* labels);
1214 StatementT ParseReturnStatement();
1215 StatementT ParseWithStatement(ZonePtrList<const AstRawString>* labels);
1216 StatementT ParseDoWhileStatement(ZonePtrList<const AstRawString>* labels,
1217 ZonePtrList<const AstRawString>* own_labels);
1218 StatementT ParseWhileStatement(ZonePtrList<const AstRawString>* labels,
1219 ZonePtrList<const AstRawString>* own_labels);
1220 StatementT ParseThrowStatement();
1221 StatementT ParseSwitchStatement(ZonePtrList<const AstRawString>* labels);
1222 V8_INLINE StatementT ParseTryStatement();
1223 StatementT ParseForStatement(ZonePtrList<const AstRawString>* labels,
1224 ZonePtrList<const AstRawString>* own_labels);
1225 StatementT ParseForEachStatementWithDeclarations(
1226 int stmt_pos, ForInfo* for_info, ZonePtrList<const AstRawString>* labels,
1227 ZonePtrList<const AstRawString>* own_labels, Scope* inner_block_scope);
1228 StatementT ParseForEachStatementWithoutDeclarations(
1229 int stmt_pos, ExpressionT expression,
int lhs_beg_pos,
int lhs_end_pos,
1230 ForInfo* for_info, ZonePtrList<const AstRawString>* labels,
1231 ZonePtrList<const AstRawString>* own_labels);
1235 ForStatementT ParseStandardForLoop(
1236 int stmt_pos, ZonePtrList<const AstRawString>* labels,
1237 ZonePtrList<const AstRawString>* own_labels, ExpressionT* cond,
1238 StatementT* next, StatementT* body);
1241 StatementT ParseStandardForLoopWithLexicalDeclarations(
1242 int stmt_pos, StatementT init, ForInfo* for_info,
1243 ZonePtrList<const AstRawString>* labels,
1244 ZonePtrList<const AstRawString>* own_labels);
1245 StatementT ParseForAwaitStatement(
1246 ZonePtrList<const AstRawString>* labels,
1247 ZonePtrList<const AstRawString>* own_labels);
1249 bool IsNextLetKeyword();
1254 V8_INLINE ExpressionT
1255 RewriteInvalidReferenceExpression(ExpressionT expression,
int beg_pos,
1256 int end_pos, MessageTemplate message);
1257 ExpressionT RewriteInvalidReferenceExpression(ExpressionT expression,
1258 int beg_pos,
int end_pos,
1259 MessageTemplate message,
1260 ParseErrorType type);
1262 bool IsValidReferenceExpression(ExpressionT expression);
1264 bool IsAssignableIdentifier(ExpressionT expression) {
1265 if (!impl()->IsIdentifier(expression))
return false;
1266 if (is_strict(language_mode()) &&
1267 impl()->IsEvalOrArguments(impl()->AsIdentifier(expression))) {
1292 static void MarkLoopVariableAsAssigned(
1293 Scope* scope, Variable* var,
1294 typename DeclarationDescriptor::Kind declaration_kind);
1296 FunctionKind FunctionKindForImpl(
bool is_method, ParseFunctionFlags flags) {
1297 static const FunctionKind kFunctionKinds[][2][2] = {
1301 FunctionKind::kNormalFunction, FunctionKind::kAsyncFunction},
1303 FunctionKind::kGeneratorFunction,
1304 FunctionKind::kAsyncGeneratorFunction},
1309 FunctionKind::kConciseMethod, FunctionKind::kAsyncConciseMethod},
1311 FunctionKind::kConciseGeneratorMethod,
1312 FunctionKind::kAsyncConciseGeneratorMethod},
1314 return kFunctionKinds[is_method]
1315 [(flags & ParseFunctionFlag::kIsGenerator) != 0]
1316 [(flags & ParseFunctionFlag::kIsAsync) != 0];
1319 inline FunctionKind FunctionKindFor(ParseFunctionFlags flags) {
1320 const bool kIsMethod =
false;
1321 return FunctionKindForImpl(kIsMethod, flags);
1324 inline FunctionKind MethodKindFor(ParseFunctionFlags flags) {
1325 const bool kIsMethod =
true;
1326 return FunctionKindForImpl(kIsMethod, flags);
1332 Call::PossiblyEval CheckPossibleEvalCall(ExpressionT expression,
1334 if (impl()->IsIdentifier(expression) &&
1335 impl()->IsEval(impl()->AsIdentifier(expression))) {
1336 scope->RecordInnerScopeEvalCall();
1337 function_state_->RecordFunctionOrEvalCall();
1338 if (is_sloppy(scope->language_mode())) {
1341 scope->GetDeclarationScope()->RecordEvalCall();
1348 scope->RecordEvalCall();
1350 return Call::IS_POSSIBLY_EVAL;
1352 return Call::NOT_EVAL;
1357 inline StatementT BuildReturnStatement(ExpressionT expr,
int pos,
1358 int end_pos = kNoSourcePosition) {
1359 if (impl()->IsNull(expr)) {
1360 expr = factory()->NewUndefinedLiteral(kNoSourcePosition);
1361 }
else if (is_async_generator()) {
1364 expr = factory()->NewAwait(expr, kNoSourcePosition);
1365 function_state_->AddSuspend();
1367 if (is_async_function()) {
1368 return factory()->NewAsyncReturnStatement(expr, pos, end_pos);
1370 return factory()->NewReturnStatement(expr, pos, end_pos);
1373 ModuleDescriptor* module()
const {
1374 return scope()->AsModuleScope()->module();
1376 Scope* scope()
const {
return scope_; }
1380 V8_INLINE ExpressionClassifier* classifier()
const {
1381 DCHECK_NOT_NULL(classifier_);
1387 V8_INLINE
void Accumulate(
unsigned productions) {
1388 DCHECK_NOT_NULL(classifier_);
1389 ExpressionClassifier* previous = classifier_->previous();
1390 DCHECK_NOT_NULL(previous);
1391 previous->Accumulate(classifier_, productions);
1392 classifier_ = previous;
1398 : parser_(parser), previous_accept_IN_(parser->accept_IN_) {
1399 parser_->accept_IN_ = accept_IN;
1402 ~
AcceptINScope() { parser_->accept_IN_ = previous_accept_IN_; }
1406 bool previous_accept_IN_;
1417 V8_INLINE
void AccumulateFormalParameterContainmentErrors() {
1418 Accumulate(ExpressionClassifier::FormalParameterInitializerProduction |
1419 ExpressionClassifier::AsyncArrowFormalParametersProduction);
1422 std::vector<void*>* pointer_buffer() {
return &pointer_buffer_; }
1427 Scope* original_scope_;
1428 FunctionState* function_state_;
1429 v8::Extension* extension_;
1430 FuncNameInferrer fni_;
1431 AstValueFactory* ast_value_factory_;
1432 typename Types::Factory ast_node_factory_;
1433 RuntimeCallStats* runtime_call_stats_;
1435 bool parsing_on_main_thread_;
1436 const bool parsing_module_;
1438 PendingCompilationErrorHandler* pending_error_handler_;
1444 ExpressionClassifier* classifier_;
1446 std::vector<void*> pointer_buffer_;
1450 Scope::Snapshot scope_snapshot_;
1455 int rewritable_length_ = -1;
1457 int function_literal_id_;
1460 FunctionLiteral::EagerCompileHint default_eager_compile_hint_;
1461 FunctionKind next_arrow_function_kind_ = FunctionKind::kArrowFunction;
1463 bool accept_IN_ =
true;
1465 bool allow_natives_;
1466 bool allow_harmony_public_fields_;
1467 bool allow_harmony_static_fields_;
1468 bool allow_harmony_dynamic_import_;
1469 bool allow_harmony_import_meta_;
1470 bool allow_harmony_private_fields_;
1471 bool allow_harmony_private_methods_;
1472 bool allow_eval_cache_;
1475 template <
typename Impl>
1476 ParserBase<Impl>::FunctionState::FunctionState(
1477 FunctionState** function_state_stack, Scope** scope_stack,
1478 DeclarationScope* scope)
1479 : BlockState(scope_stack, scope),
1480 expected_property_count_(0),
1482 function_state_stack_(function_state_stack),
1483 outer_function_state_(*function_state_stack),
1485 destructuring_assignments_to_rewrite_(scope->zone()),
1486 reported_errors_(16, scope->zone()),
1487 dont_optimize_reason_(BailoutReason::kNoReason),
1488 next_function_is_likely_called_(false),
1489 previous_function_was_likely_called_(false),
1490 contains_function_or_eval_(false) {
1491 *function_state_stack =
this;
1492 if (outer_function_state_) {
1493 outer_function_state_->previous_function_was_likely_called_ =
1494 outer_function_state_->next_function_is_likely_called_;
1495 outer_function_state_->next_function_is_likely_called_ =
false;
1499 template <
typename Impl>
1500 ParserBase<Impl>::FunctionState::~FunctionState() {
1501 *function_state_stack_ = outer_function_state_;
1504 template <
typename Impl>
1505 void ParserBase<Impl>::ReportUnexpectedToken(Token::Value token) {
1506 return ReportUnexpectedTokenAt(scanner_->location(), token);
1509 template <
typename Impl>
1510 void ParserBase<Impl>::ReportUnexpectedTokenAt(
1511 Scanner::Location source_location, Token::Value token,
1512 MessageTemplate message) {
1513 const char* arg =
nullptr;
1514 impl()->GetUnexpectedTokenMessage(token, &message, &source_location, &arg);
1515 if (Impl::IsPreParser()) {
1516 impl()->ReportUnidentifiableError();
1518 impl()->ReportMessageAt(source_location, message, arg);
1522 template <
typename Impl>
1523 typename ParserBase<Impl>::IdentifierT ParserBase<Impl>::ParseIdentifier(
1524 AllowRestrictedIdentifiers allow_restricted_identifiers) {
1525 ExpressionClassifier classifier(
this);
1526 auto result = ParseAndClassifyIdentifier();
1528 if (allow_restricted_identifiers == kDontAllowRestrictedIdentifiers &&
1529 is_strict(language_mode()) && impl()->IsEvalOrArguments(result)) {
1530 impl()->ReportMessageAt(scanner()->location(),
1531 MessageTemplate::kStrictEvalArguments);
1537 template <
typename Impl>
1538 typename ParserBase<Impl>::IdentifierT
1539 ParserBase<Impl>::ParseAndClassifyIdentifier() {
1540 Token::Value next = Next();
1541 STATIC_ASSERT(Token::IDENTIFIER + 1 == Token::ASYNC);
1542 if (IsInRange(next, Token::IDENTIFIER, Token::ASYNC)) {
1543 IdentifierT name = impl()->GetSymbol();
1551 if (V8_UNLIKELY(impl()->IsEvalOrArguments(name))) {
1552 if (impl()->IsArguments(name) && scope()->ShouldBanArguments()) {
1553 ReportMessage(MessageTemplate::kArgumentsDisallowedInInitializer);
1554 return impl()->EmptyIdentifierString();
1557 classifier()->RecordStrictModeFormalParameterError(
1558 scanner()->location(), MessageTemplate::kStrictEvalArguments);
1562 }
else if (next == Token::AWAIT && !parsing_module_ && !is_async_function()) {
1563 classifier()->RecordAsyncArrowFormalParametersError(
1564 scanner()->location(), MessageTemplate::kAwaitBindingIdentifier);
1565 return impl()->GetSymbol();
1566 }
else if (is_sloppy(language_mode()) &&
1567 (Token::IsStrictReservedWord(next) ||
1568 (next == Token::YIELD && !is_generator()))) {
1569 IdentifierT name = impl()->GetSymbol();
1570 classifier()->RecordStrictModeFormalParameterError(
1571 scanner()->location(), MessageTemplate::kUnexpectedStrictReserved);
1572 if (impl()->IdentifierEquals(name, ast_value_factory()->let_string())) {
1573 classifier()->RecordLetPatternError(
1574 scanner()->location(), MessageTemplate::kLetInLexicalBinding);
1578 ReportUnexpectedToken(next);
1579 return impl()->EmptyIdentifierString();
1583 template <
class Impl>
1584 typename ParserBase<Impl>::IdentifierT
1585 ParserBase<Impl>::ParseIdentifierOrStrictReservedWord(
1586 FunctionKind function_kind,
bool* is_strict_reserved,
bool* is_await) {
1587 Token::Value next = Next();
1588 if (next == Token::IDENTIFIER || (next == Token::AWAIT && !parsing_module_ &&
1589 !IsAsyncFunction(function_kind)) ||
1590 next == Token::ASYNC) {
1591 *is_strict_reserved =
false;
1592 *is_await = next == Token::AWAIT;
1593 }
else if (Token::IsStrictReservedWord(next) ||
1594 (next == Token::YIELD && !IsGeneratorFunction(function_kind))) {
1595 *is_strict_reserved =
true;
1597 ReportUnexpectedToken(next);
1598 return impl()->EmptyIdentifierString();
1601 return impl()->GetSymbol();
1604 template <
typename Impl>
1605 typename ParserBase<Impl>::IdentifierT ParserBase<Impl>::ParseIdentifierName() {
1606 Token::Value next = Next();
1607 if (!Token::IsAnyIdentifier(next) && next != Token::ESCAPED_KEYWORD &&
1608 !Token::IsKeyword(next)) {
1609 ReportUnexpectedToken(next);
1610 return impl()->EmptyIdentifierString();
1613 return impl()->GetSymbol();
1616 template <
typename Impl>
1617 typename ParserBase<Impl>::ExpressionT
1618 ParserBase<Impl>::ParseIdentifierNameOrPrivateName() {
1619 int pos = position();
1622 if (allow_harmony_private_fields() && Check(Token::PRIVATE_NAME)) {
1623 name = impl()->GetSymbol();
1625 impl()->ExpressionFromIdentifier(name, pos, InferName::kNo);
1626 key_proxy->set_is_private_name();
1629 name = ParseIdentifierName();
1630 key = factory()->NewStringLiteral(name, pos);
1632 impl()->PushLiteralName(name);
1636 template <
typename Impl>
1637 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseRegExpLiteral() {
1638 int pos = peek_position();
1639 if (!scanner()->ScanRegExpPattern()) {
1641 ReportMessage(MessageTemplate::kUnterminatedRegExp);
1642 return impl()->FailureExpression();
1645 IdentifierT js_pattern = impl()->GetNextSymbol();
1646 Maybe<RegExp::Flags> flags = scanner()->ScanRegExpFlags();
1647 if (flags.IsNothing()) {
1649 ReportMessage(MessageTemplate::kMalformedRegExpFlags);
1650 return impl()->FailureExpression();
1652 int js_flags = flags.FromJust();
1654 return factory()->NewRegExpLiteral(js_pattern, js_flags, pos);
1657 template <
typename Impl>
1658 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseBindingPattern() {
1664 int beg_pos = peek_position();
1665 Token::Value token = peek();
1668 if (Token::IsAnyIdentifier(token)) {
1669 IdentifierT name = ParseAndClassifyIdentifier();
1670 if (V8_UNLIKELY(is_strict(language_mode()) &&
1671 impl()->IsEvalOrArguments(name))) {
1672 impl()->ReportMessageAt(scanner()->location(),
1673 MessageTemplate::kStrictEvalArguments);
1674 return impl()->FailureExpression();
1676 return impl()->ExpressionFromIdentifier(name, beg_pos);
1679 CheckStackOverflow();
1680 classifier()->RecordNonSimpleParameter();
1682 if (token == Token::LBRACK) {
1683 result = ParseArrayLiteral();
1684 }
else if (token == Token::LBRACE) {
1685 result = ParseObjectLiteral();
1687 ReportUnexpectedToken(Next());
1688 return impl()->FailureExpression();
1691 ValidateBindingPattern();
1695 template <
typename Impl>
1696 typename ParserBase<Impl>::ExpressionT
1697 ParserBase<Impl>::ParsePrimaryExpression() {
1698 CheckStackOverflow();
1717 int beg_pos = peek_position();
1718 Token::Value token = peek();
1720 if (IsInRange(token, Token::IDENTIFIER,
1721 Token::ESCAPED_STRICT_RESERVED_WORD)) {
1723 IdentifierT name = ParseAndClassifyIdentifier();
1724 InferName infer = InferName::kYes;
1725 if (V8_UNLIKELY(impl()->IsAsync(name) &&
1726 !scanner()->HasLineTerminatorBeforeNext())) {
1727 if (peek() == Token::FUNCTION) {
1728 return ParseAsyncFunctionLiteral();
1731 if (peek_any_identifier() && PeekAhead() == Token::ARROW) {
1732 name = ParseAndClassifyIdentifier();
1734 if (!classifier()->is_valid_async_arrow_formal_parameters()) {
1735 ReportClassifierError(
1736 classifier()->async_arrow_formal_parameters_error());
1737 return impl()->FailureExpression();
1740 next_arrow_function_kind_ = FunctionKind::kAsyncArrowFunction;
1741 infer = InferName::kNo;
1745 if (peek() == Token::ARROW) {
1746 scope_snapshot_ = std::move(Scope::Snapshot(scope()));
1747 rewritable_length_ =
static_cast<int>(
1748 function_state_->destructuring_assignments_to_rewrite().size());
1750 return impl()->ExpressionFromIdentifier(name, beg_pos, infer);
1752 DCHECK_IMPLIES(Token::IsAnyIdentifier(token), token == Token::ENUM);
1754 if (Token::IsLiteral(token)) {
1755 return impl()->ExpressionFromLiteral(Next(), beg_pos);
1760 Consume(Token::THIS);
1761 return impl()->ThisExpression(beg_pos);
1764 case Token::ASSIGN_DIV:
1766 return ParseRegExpLiteral();
1769 return ParseArrayLiteral();
1772 return ParseObjectLiteral();
1774 case Token::LPAREN: {
1775 Consume(Token::LPAREN);
1776 Scope::Snapshot scope_snapshot(scope());
1777 int rewritable_length =
static_cast<int>(
1778 function_state_->destructuring_assignments_to_rewrite().size());
1779 if (Check(Token::RPAREN)) {
1782 if (peek() != Token::ARROW) ReportUnexpectedToken(Token::RPAREN);
1783 scope_snapshot_ = std::move(scope_snapshot);
1784 rewritable_length_ = rewritable_length;
1785 return factory()->NewEmptyParentheses(beg_pos);
1789 if (peek() == Token::FUNCTION ||
1790 (peek() == Token::ASYNC && PeekAhead() == Token::FUNCTION)) {
1791 function_state_->set_next_function_is_likely_called();
1793 AcceptINScope scope(
this,
true);
1794 ExpressionT expr = ParseExpressionCoverGrammar();
1795 expr->mark_parenthesized();
1796 Expect(Token::RPAREN);
1798 if (peek() == Token::ARROW) {
1799 scope_snapshot_ = std::move(scope_snapshot);
1800 rewritable_length_ = rewritable_length;
1806 case Token::CLASS: {
1807 Consume(Token::CLASS);
1808 int class_token_pos = position();
1809 IdentifierT name = impl()->NullIdentifier();
1810 bool is_strict_reserved_name =
false;
1811 Scanner::Location class_name_location = Scanner::Location::invalid();
1812 if (peek_any_identifier()) {
1813 bool is_await =
false;
1814 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
1816 class_name_location = scanner()->location();
1818 classifier()->RecordAsyncArrowFormalParametersError(
1819 scanner()->location(), MessageTemplate::kAwaitBindingIdentifier);
1822 return ParseClassLiteral(name, class_name_location,
1823 is_strict_reserved_name, class_token_pos);
1826 case Token::TEMPLATE_SPAN:
1827 case Token::TEMPLATE_TAIL:
1828 return ParseTemplateLiteral(impl()->NullExpression(), beg_pos,
false);
1831 if (allow_natives() || extension_ !=
nullptr) {
1832 return ParseV8Intrinsic();
1840 ReportUnexpectedToken(Next());
1841 return impl()->FailureExpression();
1844 template <
typename Impl>
1845 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseExpression() {
1846 ExpressionClassifier classifier(
this);
1847 AcceptINScope scope(
this,
true);
1848 ExpressionT result = ParseExpressionCoverGrammar();
1849 ValidateExpression();
1853 template <
typename Impl>
1854 typename ParserBase<Impl>::ExpressionT
1855 ParserBase<Impl>::ParseExpressionCoverGrammar() {
1860 ExpressionListT list(pointer_buffer());
1861 ExpressionT expression;
1863 if (V8_UNLIKELY(peek() == Token::ELLIPSIS)) {
1864 return ParseArrowFormalsWithRest(&list);
1867 expression = ParseAssignmentExpression();
1868 CheckArrowFormalParameter(expression);
1869 list.Add(expression);
1871 if (!Check(Token::COMMA))
break;
1873 if (peek() == Token::RPAREN && PeekAhead() == Token::ARROW) {
1880 if (peek() == Token::FUNCTION &&
1881 function_state_->previous_function_was_likely_called()) {
1882 function_state_->set_next_function_is_likely_called();
1890 if (list.length() == 1)
return expression;
1891 return impl()->ExpressionListToExpression(list);
1894 template <
typename Impl>
1895 typename ParserBase<Impl>::ExpressionT
1896 ParserBase<Impl>::ParseArrowFormalsWithRest(
1897 typename ParserBase<Impl>::ExpressionListT* list) {
1898 Consume(Token::ELLIPSIS);
1900 Scanner::Location ellipsis = scanner()->location();
1901 int pattern_pos = peek_position();
1902 ExpressionT pattern = ParseBindingPattern();
1904 classifier()->RecordNonSimpleParameter();
1906 if (V8_UNLIKELY(peek() == Token::ASSIGN)) {
1907 ReportMessage(MessageTemplate::kRestDefaultInitializer);
1908 return impl()->FailureExpression();
1911 ExpressionT spread =
1912 factory()->NewSpread(pattern, ellipsis.beg_pos, pattern_pos);
1913 if (V8_UNLIKELY(peek() == Token::COMMA)) {
1914 ReportMessage(MessageTemplate::kParamAfterRest);
1915 return impl()->FailureExpression();
1921 if (peek() != Token::RPAREN || PeekAhead() != Token::ARROW) {
1922 ReportUnexpectedTokenAt(ellipsis, Token::ELLIPSIS);
1923 return impl()->FailureExpression();
1927 return impl()->ExpressionListToExpression(*list);
1930 template <
typename Impl>
1931 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseArrayLiteral() {
1935 int pos = peek_position();
1936 ExpressionListT values(pointer_buffer());
1937 int first_spread_index = -1;
1938 Consume(Token::LBRACK);
1939 while (!Check(Token::RBRACK)) {
1941 if (peek() == Token::COMMA) {
1942 elem = factory()->NewTheHoleLiteral();
1943 }
else if (Check(Token::ELLIPSIS)) {
1944 int start_pos = position();
1945 int expr_pos = peek_position();
1946 AcceptINScope scope(
this,
true);
1947 ExpressionT argument = ParseAssignmentExpression();
1948 elem = factory()->NewSpread(argument, start_pos, expr_pos);
1950 if (first_spread_index < 0) {
1951 first_spread_index = values.length();
1954 if (argument->IsAssignment()) {
1955 classifier()->RecordPatternError(
1956 Scanner::Location(start_pos, end_position()),
1957 MessageTemplate::kInvalidDestructuringTarget);
1959 CheckDestructuringElement(argument, start_pos, end_position());
1962 if (peek() == Token::COMMA) {
1963 classifier()->RecordPatternError(
1964 Scanner::Location(start_pos, end_position()),
1965 MessageTemplate::kElementAfterRest);
1968 int beg_pos = peek_position();
1969 AcceptINScope scope(
this,
true);
1970 elem = ParseAssignmentExpression();
1971 CheckDestructuringElement(elem, beg_pos, end_position());
1974 if (peek() != Token::RBRACK) {
1975 Expect(Token::COMMA);
1976 if (elem->IsFailureExpression())
return elem;
1980 return factory()->NewArrayLiteral(values, first_spread_index, pos);
1983 template <
class Impl>
1984 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParsePropertyName(
1985 ParsePropertyInfo* prop_info) {
1986 DCHECK_EQ(prop_info->kind, ParsePropertyKind::kNotSet);
1987 DCHECK_EQ(prop_info->function_flags, ParseFunctionFlag::kIsNormal);
1988 DCHECK(!prop_info->is_computed_name);
1990 if (Check(Token::ASYNC)) {
1991 Token::Value token = peek();
1992 if ((token != Token::MUL && prop_info->ParsePropertyKindFromToken(token)) ||
1993 scanner()->HasLineTerminatorBeforeNext()) {
1994 prop_info->name = impl()->GetSymbol();
1995 impl()->PushLiteralName(prop_info->name);
1996 return factory()->NewStringLiteral(prop_info->name, position());
1998 prop_info->function_flags = ParseFunctionFlag::kIsAsync;
1999 prop_info->kind = ParsePropertyKind::kMethod;
2002 if (Check(Token::MUL)) {
2003 prop_info->function_flags |= ParseFunctionFlag::kIsGenerator;
2004 prop_info->kind = ParsePropertyKind::kMethod;
2007 if (prop_info->kind == ParsePropertyKind::kNotSet &&
2008 Check(Token::IDENTIFIER)) {
2009 IdentifierT symbol = impl()->GetSymbol();
2010 if (!prop_info->ParsePropertyKindFromToken(peek())) {
2011 if (impl()->IdentifierEquals(symbol, ast_value_factory()->get_string())) {
2012 prop_info->kind = ParsePropertyKind::kAccessorGetter;
2013 }
else if (impl()->IdentifierEquals(symbol,
2014 ast_value_factory()->set_string())) {
2015 prop_info->kind = ParsePropertyKind::kAccessorSetter;
2018 if (!IsAccessor(prop_info->kind)) {
2019 prop_info->name = symbol;
2020 impl()->PushLiteralName(prop_info->name);
2021 return factory()->NewStringLiteral(prop_info->name, position());
2025 int pos = peek_position();
2036 bool is_array_index;
2039 case Token::PRIVATE_NAME:
2040 prop_info->is_private =
true;
2041 is_array_index =
false;
2042 Consume(Token::PRIVATE_NAME);
2043 if (prop_info->kind == ParsePropertyKind::kNotSet) {
2044 prop_info->ParsePropertyKindFromToken(peek());
2046 prop_info->name = impl()->GetSymbol();
2047 if (prop_info->position == PropertyPosition::kObjectLiteral ||
2048 prop_info->is_static ||
2049 (!allow_harmony_private_methods() &&
2050 (IsAccessor(prop_info->kind) ||
2051 prop_info->kind == ParsePropertyKind::kMethod))) {
2052 ReportUnexpectedToken(Next());
2053 return impl()->FailureExpression();
2058 Consume(Token::STRING);
2059 prop_info->name = impl()->GetSymbol();
2060 is_array_index = impl()->IsArrayIndex(prop_info->name, &index);
2064 Consume(Token::SMI);
2065 index = scanner()->smi_value();
2066 is_array_index =
true;
2068 prop_info->name = impl()->GetSymbol();
2071 case Token::NUMBER: {
2072 Consume(Token::NUMBER);
2073 prop_info->name = impl()->GetNumberAsSymbol();
2074 is_array_index = impl()->IsArrayIndex(prop_info->name, &index);
2077 case Token::LBRACK: {
2078 prop_info->name = impl()->NullIdentifier();
2079 prop_info->is_computed_name =
true;
2080 Consume(Token::LBRACK);
2081 ExpressionClassifier computed_name_classifier(
this);
2082 AcceptINScope scope(
this,
true);
2083 ExpressionT expression = ParseAssignmentExpression();
2084 ValidateExpression();
2085 AccumulateFormalParameterContainmentErrors();
2086 Expect(Token::RBRACK);
2087 if (prop_info->kind == ParsePropertyKind::kNotSet) {
2088 prop_info->ParsePropertyKindFromToken(peek());
2093 case Token::ELLIPSIS:
2094 if (prop_info->kind == ParsePropertyKind::kNotSet) {
2095 prop_info->name = impl()->NullIdentifier();
2096 Consume(Token::ELLIPSIS);
2097 AcceptINScope scope(
this,
true);
2098 ExpressionT expression = ParseAssignmentExpression();
2099 prop_info->kind = ParsePropertyKind::kSpread;
2101 CheckDestructuringElement(expression, expression->position(),
2103 if (!IsValidReferenceExpression(expression)) {
2104 classifier()->RecordBindingPatternError(
2105 Scanner::Location(expression->position(), end_position()),
2106 MessageTemplate::kInvalidRestBindingPattern);
2107 classifier()->RecordPatternError(
2108 Scanner::Location(expression->position(), end_position()),
2109 MessageTemplate::kInvalidRestAssignmentPattern);
2112 if (peek() != Token::RBRACE) {
2113 classifier()->RecordPatternError(scanner()->location(),
2114 MessageTemplate::kElementAfterRest);
2121 prop_info->name = ParseIdentifierName();
2122 is_array_index =
false;
2126 if (prop_info->kind == ParsePropertyKind::kNotSet) {
2127 prop_info->ParsePropertyKindFromToken(peek());
2129 impl()->PushLiteralName(prop_info->name);
2130 return is_array_index ? factory()->NewNumberLiteral(index, pos)
2131 : factory()->NewStringLiteral(prop_info->name, pos);
2134 template <
typename Impl>
2135 typename ParserBase<Impl>::ClassLiteralPropertyT
2136 ParserBase<Impl>::ParseClassPropertyDefinition(ClassInfo* class_info,
2137 ParsePropertyInfo* prop_info,
2139 DCHECK_NOT_NULL(class_info);
2140 DCHECK_EQ(prop_info->position, PropertyPosition::kClassLiteral);
2142 Token::Value name_token = peek();
2143 DCHECK_IMPLIES(name_token == Token::PRIVATE_NAME,
2144 allow_harmony_private_fields());
2146 int property_beg_pos = scanner()->peek_location().beg_pos;
2147 int name_token_position = property_beg_pos;
2148 ExpressionT name_expression;
2149 if (name_token == Token::STATIC) {
2150 Consume(Token::STATIC);
2151 name_token_position = scanner()->peek_location().beg_pos;
2152 if (peek() == Token::LPAREN) {
2153 prop_info->kind = ParsePropertyKind::kMethod;
2155 prop_info->name = impl()->GetSymbol();
2157 factory()->NewStringLiteral(prop_info->name, position());
2158 }
else if (peek() == Token::ASSIGN || peek() == Token::SEMICOLON ||
2159 peek() == Token::RBRACE) {
2161 prop_info->name = impl()->GetSymbol();
2163 factory()->NewStringLiteral(prop_info->name, position());
2164 }
else if (peek() == Token::PRIVATE_NAME) {
2166 ReportUnexpectedToken(Next());
2167 return impl()->NullLiteralProperty();
2169 prop_info->is_static =
true;
2170 name_expression = ParsePropertyName(prop_info);
2173 name_expression = ParsePropertyName(prop_info);
2176 if (!class_info->has_name_static_property && prop_info->is_static &&
2177 impl()->IsName(prop_info->name)) {
2178 class_info->has_name_static_property =
true;
2181 switch (prop_info->kind) {
2182 case ParsePropertyKind::kAssign:
2183 case ParsePropertyKind::kClassField:
2184 case ParsePropertyKind::kShorthandOrClassField:
2185 case ParsePropertyKind::kNotSet:
2193 if (allow_harmony_public_fields() || allow_harmony_private_fields()) {
2194 prop_info->kind = ParsePropertyKind::kClassField;
2195 prop_info->is_private = name_token == Token::PRIVATE_NAME;
2196 if (prop_info->is_static && !allow_harmony_static_fields()) {
2197 ReportUnexpectedToken(Next());
2198 return impl()->NullLiteralProperty();
2200 if (!prop_info->is_computed_name) {
2201 CheckClassFieldName(prop_info->name, prop_info->is_static);
2203 ExpressionT initializer = ParseMemberInitializer(
2204 class_info, property_beg_pos, prop_info->is_static);
2206 ClassLiteralPropertyT result = factory()->NewClassLiteralProperty(
2207 name_expression, initializer, ClassLiteralProperty::FIELD,
2208 prop_info->is_static, prop_info->is_computed_name,
2209 prop_info->is_private);
2210 impl()->SetFunctionNameFromPropertyName(result, prop_info->name);
2214 ReportUnexpectedToken(Next());
2215 return impl()->NullLiteralProperty();
2218 case ParsePropertyKind::kMethod: {
2227 if (!prop_info->is_computed_name) {
2228 CheckClassMethodName(prop_info->name, ParsePropertyKind::kMethod,
2229 prop_info->function_flags, prop_info->is_static,
2230 &class_info->has_seen_constructor);
2233 FunctionKind kind = MethodKindFor(prop_info->function_flags);
2235 if (!prop_info->is_static && impl()->IsConstructor(prop_info->name)) {
2236 class_info->has_seen_constructor =
true;
2237 kind = has_extends ? FunctionKind::kDerivedConstructor
2238 : FunctionKind::kBaseConstructor;
2241 ExpressionT value = impl()->ParseFunctionLiteral(
2242 prop_info->name, scanner()->location(), kSkipFunctionNameCheck, kind,
2243 name_token_position, FunctionLiteral::kAccessorOrMethod,
2244 language_mode(),
nullptr);
2246 ClassLiteralPropertyT result = factory()->NewClassLiteralProperty(
2247 name_expression, value, ClassLiteralProperty::METHOD,
2248 prop_info->is_static, prop_info->is_computed_name,
2249 prop_info->is_private);
2250 impl()->SetFunctionNameFromPropertyName(result, prop_info->name);
2254 case ParsePropertyKind::kAccessorGetter:
2255 case ParsePropertyKind::kAccessorSetter: {
2256 DCHECK_EQ(prop_info->function_flags, ParseFunctionFlag::kIsNormal);
2257 bool is_get = prop_info->kind == ParsePropertyKind::kAccessorGetter;
2259 if (!prop_info->is_computed_name) {
2260 CheckClassMethodName(prop_info->name, prop_info->kind,
2261 ParseFunctionFlag::kIsNormal, prop_info->is_static,
2262 &class_info->has_seen_constructor);
2266 name_expression = factory()->NewStringLiteral(
2267 prop_info->name, name_expression->position());
2270 FunctionKind kind = is_get ? FunctionKind::kGetterFunction
2271 : FunctionKind::kSetterFunction;
2273 FunctionLiteralT value = impl()->ParseFunctionLiteral(
2274 prop_info->name, scanner()->location(), kSkipFunctionNameCheck, kind,
2275 name_token_position, FunctionLiteral::kAccessorOrMethod,
2276 language_mode(),
nullptr);
2278 ClassLiteralProperty::Kind property_kind =
2279 is_get ? ClassLiteralProperty::GETTER : ClassLiteralProperty::SETTER;
2280 ClassLiteralPropertyT result = factory()->NewClassLiteralProperty(
2281 name_expression, value, property_kind, prop_info->is_static,
2282 prop_info->is_computed_name, prop_info->is_private);
2283 const AstRawString* prefix =
2284 is_get ? ast_value_factory()->get_space_string()
2285 : ast_value_factory()->set_space_string();
2286 impl()->SetFunctionNameFromPropertyName(result, prop_info->name, prefix);
2289 case ParsePropertyKind::kValue:
2290 case ParsePropertyKind::kShorthand:
2291 case ParsePropertyKind::kSpread:
2292 ReportUnexpectedTokenAt(
2293 Scanner::Location(name_token_position, name_expression->position()),
2295 return impl()->NullLiteralProperty();
2300 template <
typename Impl>
2301 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseMemberInitializer(
2302 ClassInfo* class_info,
int beg_pos,
bool is_static) {
2303 DeclarationScope* initializer_scope =
2304 is_static ? class_info->static_fields_scope
2305 : class_info->instance_members_scope;
2307 if (initializer_scope ==
nullptr) {
2309 NewFunctionScope(FunctionKind::kClassMembersInitializerFunction);
2311 initializer_scope->set_start_position(beg_pos);
2312 initializer_scope->SetLanguageMode(LanguageMode::kStrict);
2315 ExpressionT initializer;
2316 if (Check(Token::ASSIGN)) {
2317 FunctionState initializer_state(&function_state_, &scope_,
2319 ExpressionClassifier expression_classifier(
this);
2321 AcceptINScope scope(
this,
true);
2322 initializer = ParseAssignmentExpression();
2323 ValidateExpression();
2329 impl()->RewriteDestructuringAssignments();
2331 initializer = factory()->NewUndefinedLiteral(kNoSourcePosition);
2334 initializer_scope->set_end_position(end_position());
2336 class_info->static_fields_scope = initializer_scope;
2337 class_info->has_static_class_fields =
true;
2339 class_info->instance_members_scope = initializer_scope;
2340 class_info->has_instance_members =
true;
2346 template <
typename Impl>
2347 typename ParserBase<Impl>::ObjectLiteralPropertyT
2348 ParserBase<Impl>::ParseObjectPropertyDefinition(ParsePropertyInfo* prop_info,
2349 bool* has_seen_proto) {
2350 DCHECK_EQ(prop_info->position, PropertyPosition::kObjectLiteral);
2351 Token::Value name_token = peek();
2352 Scanner::Location next_loc = scanner()->peek_location();
2354 ExpressionT name_expression = ParsePropertyName(prop_info);
2355 IdentifierT name = prop_info->name;
2356 ParseFunctionFlags function_flags = prop_info->function_flags;
2357 ParsePropertyKind kind = prop_info->kind;
2359 switch (prop_info->kind) {
2360 case ParsePropertyKind::kSpread:
2361 DCHECK_EQ(function_flags, ParseFunctionFlag::kIsNormal);
2362 DCHECK(!prop_info->is_computed_name);
2363 DCHECK_EQ(Token::ELLIPSIS, name_token);
2365 prop_info->is_computed_name =
true;
2366 prop_info->is_rest =
true;
2368 return factory()->NewObjectLiteralProperty(
2369 factory()->NewTheHoleLiteral(), name_expression,
2370 ObjectLiteralProperty::SPREAD,
true);
2372 case ParsePropertyKind::kValue: {
2373 DCHECK_EQ(function_flags, ParseFunctionFlag::kIsNormal);
2375 if (!prop_info->is_computed_name &&
2376 impl()->IdentifierEquals(name, ast_value_factory()->proto_string())) {
2377 if (*has_seen_proto) {
2378 classifier()->RecordExpressionError(scanner()->location(),
2379 MessageTemplate::kDuplicateProto);
2381 *has_seen_proto =
true;
2383 Consume(Token::COLON);
2384 int beg_pos = peek_position();
2385 AcceptINScope scope(
this,
true);
2386 ExpressionT value = ParseAssignmentExpression();
2387 CheckDestructuringElement(value, beg_pos, end_position());
2389 ObjectLiteralPropertyT result = factory()->NewObjectLiteralProperty(
2390 name_expression, value, prop_info->is_computed_name);
2391 impl()->SetFunctionNameFromPropertyName(result, name);
2395 case ParsePropertyKind::kAssign:
2396 case ParsePropertyKind::kShorthandOrClassField:
2397 case ParsePropertyKind::kShorthand: {
2404 DCHECK_EQ(function_flags, ParseFunctionFlag::kIsNormal);
2406 if (!Token::IsIdentifier(name_token, language_mode(),
2407 this->is_generator(),
2408 parsing_module_ || is_async_function())) {
2409 ReportUnexpectedToken(Next());
2410 return impl()->NullLiteralProperty();
2413 DCHECK(!prop_info->is_computed_name);
2415 if (name_token == Token::LET) {
2416 classifier()->RecordLetPatternError(
2417 scanner()->location(), MessageTemplate::kLetInLexicalBinding);
2419 if (name_token == Token::AWAIT) {
2420 DCHECK(!is_async_function());
2421 classifier()->RecordAsyncArrowFormalParametersError(
2422 next_loc, MessageTemplate::kAwaitBindingIdentifier);
2425 impl()->ExpressionFromIdentifier(name, next_loc.beg_pos);
2426 if (!IsAssignableIdentifier(lhs)) {
2427 classifier()->RecordPatternError(next_loc,
2428 MessageTemplate::kStrictEvalArguments);
2432 if (peek() == Token::ASSIGN) {
2433 Consume(Token::ASSIGN);
2434 ExpressionClassifier rhs_classifier(
this);
2435 AcceptINScope scope(
this,
true);
2436 ExpressionT rhs = ParseAssignmentExpression();
2437 ValidateExpression();
2438 AccumulateFormalParameterContainmentErrors();
2439 value = factory()->NewAssignment(Token::ASSIGN, lhs, rhs,
2441 classifier()->RecordExpressionError(
2442 Scanner::Location(next_loc.beg_pos, end_position()),
2443 MessageTemplate::kInvalidCoverInitializedName);
2445 impl()->SetFunctionNameFromIdentifierRef(rhs, lhs);
2450 ObjectLiteralPropertyT result = factory()->NewObjectLiteralProperty(
2451 name_expression, value, ObjectLiteralProperty::COMPUTED,
false);
2452 impl()->SetFunctionNameFromPropertyName(result, name);
2456 case ParsePropertyKind::kMethod: {
2461 classifier()->RecordPatternError(
2462 Scanner::Location(next_loc.beg_pos, end_position()),
2463 MessageTemplate::kInvalidDestructuringTarget);
2465 FunctionKind kind = MethodKindFor(function_flags);
2467 ExpressionT value = impl()->ParseFunctionLiteral(
2468 name, scanner()->location(), kSkipFunctionNameCheck, kind,
2469 next_loc.beg_pos, FunctionLiteral::kAccessorOrMethod, language_mode(),
2472 ObjectLiteralPropertyT result = factory()->NewObjectLiteralProperty(
2473 name_expression, value, ObjectLiteralProperty::COMPUTED,
2474 prop_info->is_computed_name);
2475 impl()->SetFunctionNameFromPropertyName(result, name);
2479 case ParsePropertyKind::kAccessorGetter:
2480 case ParsePropertyKind::kAccessorSetter: {
2481 DCHECK_EQ(function_flags, ParseFunctionFlag::kIsNormal);
2482 bool is_get = kind == ParsePropertyKind::kAccessorGetter;
2484 classifier()->RecordPatternError(
2485 Scanner::Location(next_loc.beg_pos, end_position()),
2486 MessageTemplate::kInvalidDestructuringTarget);
2488 if (!prop_info->is_computed_name) {
2493 factory()->NewStringLiteral(name, name_expression->position());
2496 FunctionKind kind = is_get ? FunctionKind::kGetterFunction
2497 : FunctionKind::kSetterFunction;
2499 FunctionLiteralT value = impl()->ParseFunctionLiteral(
2500 name, scanner()->location(), kSkipFunctionNameCheck, kind,
2501 next_loc.beg_pos, FunctionLiteral::kAccessorOrMethod, language_mode(),
2504 ObjectLiteralPropertyT result = factory()->NewObjectLiteralProperty(
2505 name_expression, value,
2506 is_get ? ObjectLiteralProperty::GETTER
2507 : ObjectLiteralProperty::SETTER,
2508 prop_info->is_computed_name);
2509 const AstRawString* prefix =
2510 is_get ? ast_value_factory()->get_space_string()
2511 : ast_value_factory()->set_space_string();
2512 impl()->SetFunctionNameFromPropertyName(result, name, prefix);
2516 case ParsePropertyKind::kClassField:
2517 case ParsePropertyKind::kNotSet:
2518 ReportUnexpectedToken(Next());
2519 return impl()->NullLiteralProperty();
2524 template <
typename Impl>
2525 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseObjectLiteral() {
2529 int pos = peek_position();
2530 ObjectPropertyListT properties(pointer_buffer());
2531 int number_of_boilerplate_properties = 0;
2533 bool has_computed_names =
false;
2534 bool has_rest_property =
false;
2535 bool has_seen_proto =
false;
2537 Consume(Token::LBRACE);
2539 while (!Check(Token::RBRACE)) {
2540 FuncNameInferrerState fni_state(&fni_);
2542 ParsePropertyInfo prop_info(
this);
2543 prop_info.position = PropertyPosition::kObjectLiteral;
2544 ObjectLiteralPropertyT
property =
2545 ParseObjectPropertyDefinition(&prop_info, &has_seen_proto);
2546 if (impl()->IsNull(property))
return impl()->FailureExpression();
2548 if (prop_info.is_computed_name) {
2549 has_computed_names =
true;
2552 if (prop_info.is_rest) {
2553 has_rest_property =
true;
2556 if (impl()->IsBoilerplateProperty(property) && !has_computed_names) {
2559 number_of_boilerplate_properties++;
2562 properties.Add(property);
2564 if (peek() != Token::RBRACE) {
2565 Expect(Token::COMMA);
2576 if (has_rest_property && properties.length() > Code::kMaxArguments) {
2577 this->classifier()->RecordPatternError(Scanner::Location(pos, position()),
2578 MessageTemplate::kTooManyArguments);
2581 return impl()->InitializeObjectLiteral(factory()->NewObjectLiteral(
2582 properties, number_of_boilerplate_properties, pos, has_rest_property));
2585 template <
typename Impl>
2586 void ParserBase<Impl>::ParseArguments(
2587 typename ParserBase<Impl>::ExpressionListT* args,
bool* has_spread,
2592 *has_spread =
false;
2593 Consume(Token::LPAREN);
2595 while (peek() != Token::RPAREN) {
2596 int start_pos = peek_position();
2597 bool is_spread = Check(Token::ELLIPSIS);
2598 int expr_pos = peek_position();
2600 AcceptINScope scope(
this,
true);
2601 ExpressionT argument = ParseAssignmentExpression();
2603 if (V8_UNLIKELY(maybe_arrow)) {
2604 CheckArrowFormalParameter(argument);
2606 classifier()->RecordNonSimpleParameter();
2607 if (argument->IsAssignment()) {
2608 classifier()->RecordAsyncArrowFormalParametersError(
2609 scanner()->location(), MessageTemplate::kRestDefaultInitializer);
2611 if (peek() == Token::COMMA) {
2612 classifier()->RecordAsyncArrowFormalParametersError(
2613 scanner()->peek_location(), MessageTemplate::kParamAfterRest);
2619 argument = factory()->NewSpread(argument, start_pos, expr_pos);
2621 args->Add(argument);
2622 if (!Check(Token::COMMA))
break;
2625 if (args->length() > Code::kMaxArguments) {
2626 ReportMessage(MessageTemplate::kTooManyArguments);
2630 Scanner::Location location = scanner_->location();
2631 if (!Check(Token::RPAREN)) {
2632 impl()->ReportMessageAt(location, MessageTemplate::kUnterminatedArgList);
2637 template <
typename Impl>
2638 typename ParserBase<Impl>::ExpressionT
2639 ParserBase<Impl>::ParseAssignmentExpression() {
2645 int lhs_beg_pos = peek_position();
2647 if (peek() == Token::YIELD && is_generator()) {
2648 return ParseYieldExpression();
2651 FuncNameInferrerState fni_state(&fni_);
2652 ExpressionClassifier arrow_formals_classifier(
this);
2654 DCHECK_IMPLIES(!has_error(), -1 == rewritable_length_);
2655 DCHECK_IMPLIES(!has_error(), scope_snapshot_.IsCleared());
2656 DCHECK_IMPLIES(!has_error(),
2657 FunctionKind::kArrowFunction == next_arrow_function_kind_);
2658 ExpressionT expression = ParseConditionalExpression();
2660 Token::Value op = peek();
2662 if (!Token::IsArrowOrAssignmentOp(op)) {
2663 if (expression->IsProperty()) {
2664 Accumulate(~ExpressionClassifier::PatternProduction);
2666 Accumulate(ExpressionClassifier::AllProductions);
2672 if (V8_UNLIKELY(op == Token::ARROW)) {
2673 ValidateArrowFormalParameters(expression);
2674 Scanner::Location loc(lhs_beg_pos, end_position());
2675 DeclarationScope* scope = NewFunctionScope(next_arrow_function_kind_);
2678 next_arrow_function_kind_ = FunctionKind::kArrowFunction;
2680 if (has_error())
return impl()->FailureExpression();
2683 scope_snapshot_.Reparent(scope);
2685 FormalParametersT parameters(scope);
2686 if (!classifier()->is_simple_parameter_list()) {
2687 scope->SetHasNonSimpleParameters();
2688 parameters.is_simple =
false;
2691 scope->set_start_position(lhs_beg_pos);
2692 impl()->DeclareArrowFunctionFormalParameters(¶meters, expression, loc);
2694 expression = ParseArrowFunctionLiteral(parameters);
2695 Accumulate(ExpressionClassifier::AsyncArrowFormalParametersProduction);
2703 if (V8_UNLIKELY(expression->IsPattern() && op == Token::ASSIGN)) {
2704 ValidatePattern(expression);
2708 Accumulate(~ExpressionClassifier::ExpressionProduction);
2709 impl()->MarkPatternAsAssigned(expression);
2712 int pos = position();
2714 ExpressionClassifier rhs_classifier(
this);
2715 ExpressionT right = ParseAssignmentExpression();
2716 ValidateExpression();
2717 AccumulateFormalParameterContainmentErrors();
2718 ExpressionT result = factory()->NewAssignment(op, expression, right, pos);
2720 auto rewritable = factory()->NewRewritableExpression(result, scope());
2721 impl()->QueueDestructuringAssignmentForRewriting(rewritable);
2725 if (V8_UNLIKELY(!IsValidReferenceExpression(expression))) {
2726 expression = RewriteInvalidReferenceExpression(
2727 expression, lhs_beg_pos, end_position(),
2728 MessageTemplate::kInvalidLhsInAssignment);
2730 impl()->MarkExpressionAsAssigned(expression);
2733 Scanner::Location op_location = scanner()->location();
2735 ExpressionT right = ParseAssignmentExpression();
2738 ValidateExpression();
2739 AccumulateFormalParameterContainmentErrors();
2741 if (op == Token::ASSIGN) {
2746 if (impl()->IsThisProperty(expression)) function_state_->AddProperty();
2748 impl()->CheckAssigningFunctionLiteralToProperty(expression, right);
2753 if (right->IsCall() || right->IsCallNew()) {
2754 fni_.RemoveLastFunction();
2759 impl()->SetFunctionNameFromIdentifierRef(right, expression);
2761 if (expression->IsProperty()) {
2762 classifier()->RecordBindingPatternError(
2763 Scanner::Location(expression->position(), end_position()),
2764 MessageTemplate::kInvalidPropertyBindingPattern);
2767 classifier()->RecordPatternError(
2768 op_location, MessageTemplate::kUnexpectedToken, Token::String(op));
2769 fni_.RemoveLastFunction();
2772 return factory()->NewAssignment(op, expression, right, op_location.beg_pos);
2775 template <
typename Impl>
2776 typename ParserBase<Impl>::ExpressionT
2777 ParserBase<Impl>::ParseYieldExpression() {
2780 int pos = peek_position();
2781 classifier()->RecordFormalParameterInitializerError(
2782 scanner()->peek_location(), MessageTemplate::kYieldInParameter);
2783 Consume(Token::YIELD);
2785 CheckStackOverflow();
2788 ExpressionT expression = impl()->NullExpression();
2789 bool delegating =
false;
2790 if (!scanner()->HasLineTerminatorBeforeNext()) {
2791 if (Check(Token::MUL)) delegating =
true;
2794 case Token::SEMICOLON:
2805 if (!delegating)
break;
2809 expression = ParseAssignmentExpression();
2815 ExpressionT yieldstar = factory()->NewYieldStar(expression, pos);
2816 impl()->RecordSuspendSourceRange(yieldstar, PositionAfterSemicolon());
2817 function_state_->AddSuspend();
2818 if (IsAsyncGeneratorFunction(function_state_->kind())) {
2820 function_state_->AddSuspend();
2821 function_state_->AddSuspend();
2829 factory()->NewYield(expression, pos, Suspend::kOnExceptionThrow);
2830 impl()->RecordSuspendSourceRange(yield, PositionAfterSemicolon());
2831 function_state_->AddSuspend();
2836 template <
typename Impl>
2837 typename ParserBase<Impl>::ExpressionT
2838 ParserBase<Impl>::ParseConditionalExpression() {
2843 int pos = peek_position();
2845 ExpressionT expression = ParseBinaryExpression(4);
2846 return peek() == Token::CONDITIONAL
2847 ? ParseConditionalContinuation(expression, pos)
2851 template <
typename Impl>
2852 typename ParserBase<Impl>::ExpressionT
2853 ParserBase<Impl>::ParseConditionalContinuation(ExpressionT expression,
2855 SourceRange then_range, else_range;
2859 SourceRangeScope range_scope(scanner(), &then_range);
2860 Consume(Token::CONDITIONAL);
2864 AcceptINScope scope(
this,
true);
2865 left = ParseAssignmentExpression();
2869 SourceRangeScope range_scope(scanner(), &else_range);
2870 Expect(Token::COLON);
2871 right = ParseAssignmentExpression();
2873 ExpressionT expr = factory()->NewConditional(expression, left, right, pos);
2874 impl()->RecordConditionalSourceRange(expr, then_range, else_range);
2879 template <
typename Impl>
2880 typename ParserBase<Impl>::ExpressionT
2881 ParserBase<Impl>::ParseBinaryContinuation(ExpressionT x,
int prec,
int prec1) {
2884 while (Token::Precedence(peek(), accept_IN_) == prec1) {
2885 SourceRange right_range;
2886 int pos = peek_position();
2890 SourceRangeScope right_range_scope(scanner(), &right_range);
2893 const bool is_right_associative = op == Token::EXP;
2894 const int next_prec = is_right_associative ? prec1 : prec1 + 1;
2895 y = ParseBinaryExpression(next_prec);
2901 if (Token::IsCompareOp(op)) {
2903 Token::Value cmp = op;
2905 case Token::NE: cmp = Token::EQ;
break;
2906 case Token::NE_STRICT: cmp = Token::EQ_STRICT;
break;
2909 x = factory()->NewCompareOperation(cmp, x, y, pos);
2912 x = factory()->NewUnaryOperation(Token::NOT, x, pos);
2914 }
else if (!impl()->ShortcutNumericLiteralBinaryExpression(&x, y, op,
2916 !impl()->CollapseNaryExpression(&x, y, op, pos, right_range)) {
2918 x = factory()->NewBinaryOperation(op, x, y, pos);
2919 if (op == Token::OR || op == Token::AND) {
2920 impl()->RecordBinaryOperationSourceRange(x, right_range);
2925 }
while (prec1 >= prec);
2931 template <
typename Impl>
2932 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseBinaryExpression(
2935 ExpressionT x = ParseUnaryExpression();
2936 int prec1 = Token::Precedence(peek(), accept_IN_);
2937 if (prec1 >= prec) {
2938 return ParseBinaryContinuation(x, prec, prec1);
2943 template <
typename Impl>
2944 typename ParserBase<Impl>::ExpressionT
2945 ParserBase<Impl>::ParseUnaryOrPrefixExpression() {
2946 Token::Value op = Next();
2947 int pos = position();
2950 if (op == Token::NOT && peek() == Token::FUNCTION) {
2951 function_state_->set_next_function_is_likely_called();
2954 CheckStackOverflow();
2956 ExpressionT expression = ParseUnaryExpression();
2958 if (Token::IsUnaryOp(op)) {
2959 if (op == Token::DELETE) {
2960 if (impl()->IsIdentifier(expression) && is_strict(language_mode())) {
2962 ReportMessage(MessageTemplate::kStrictDelete);
2963 return impl()->FailureExpression();
2966 if (impl()->IsPropertyWithPrivateFieldKey(expression)) {
2967 ReportMessage(MessageTemplate::kDeletePrivateField);
2968 return impl()->FailureExpression();
2972 if (peek() == Token::EXP) {
2973 ReportUnexpectedToken(Next());
2974 return impl()->FailureExpression();
2978 return impl()->BuildUnaryExpression(expression, op, pos);
2981 DCHECK(Token::IsCountOp(op));
2983 if (V8_UNLIKELY(!IsValidReferenceExpression(expression))) {
2984 expression = RewriteInvalidReferenceExpression(
2985 expression, expression->position(), end_position(),
2986 MessageTemplate::kInvalidLhsInPrefixOp);
2988 impl()->MarkExpressionAsAssigned(expression);
2990 return factory()->NewCountOperation(op,
true , expression,
2994 template <
typename Impl>
2995 typename ParserBase<Impl>::ExpressionT
2996 ParserBase<Impl>::ParseAwaitExpression() {
2997 classifier()->RecordFormalParameterInitializerError(
2998 scanner()->peek_location(),
2999 MessageTemplate::kAwaitExpressionFormalParameter);
3000 int await_pos = peek_position();
3001 Consume(Token::AWAIT);
3003 CheckStackOverflow();
3005 ExpressionT value = ParseUnaryExpression();
3007 ExpressionT expr = factory()->NewAwait(value, await_pos);
3008 function_state_->AddSuspend();
3009 impl()->RecordSuspendSourceRange(expr, PositionAfterSemicolon());
3013 template <
typename Impl>
3014 typename ParserBase<Impl>::ExpressionT
3015 ParserBase<Impl>::ParseUnaryExpression() {
3029 Token::Value op = peek();
3030 if (Token::IsUnaryOrCountOp(op))
return ParseUnaryOrPrefixExpression();
3031 if (is_async_function() && op == Token::AWAIT) {
3032 return ParseAwaitExpression();
3034 return ParsePostfixExpression();
3037 template <
typename Impl>
3038 typename ParserBase<Impl>::ExpressionT
3039 ParserBase<Impl>::ParsePostfixExpression() {
3043 int lhs_beg_pos = peek_position();
3044 ExpressionT expression = ParseLeftHandSideExpression();
3045 if (!scanner()->HasLineTerminatorBeforeNext() && Token::IsCountOp(peek())) {
3046 if (V8_UNLIKELY(!IsValidReferenceExpression(expression))) {
3047 expression = RewriteInvalidReferenceExpression(
3048 expression, lhs_beg_pos, end_position(),
3049 MessageTemplate::kInvalidLhsInPostfixOp);
3051 impl()->MarkExpressionAsAssigned(expression);
3053 Token::Value next = Next();
3055 factory()->NewCountOperation(next,
3063 template <
typename Impl>
3064 typename ParserBase<Impl>::ExpressionT
3065 ParserBase<Impl>::ParseLeftHandSideExpression() {
3069 ExpressionT result = ParseMemberWithNewPrefixesExpression();
3070 if (!Token::IsPropertyOrCall(peek()))
return result;
3071 return ParseLeftHandSideContinuation(result);
3074 template <
typename Impl>
3075 typename ParserBase<Impl>::ExpressionT
3076 ParserBase<Impl>::ParseLeftHandSideContinuation(ExpressionT result) {
3077 DCHECK(Token::IsPropertyOrCall(peek()));
3079 if (V8_UNLIKELY(peek() == Token::LPAREN && impl()->IsIdentifier(result) &&
3080 scanner()->current_token() == Token::ASYNC &&
3081 !scanner()->HasLineTerminatorBeforeNext())) {
3082 DCHECK(impl()->IsAsync(impl()->AsIdentifier(result)));
3083 int pos = position();
3085 Scope::Snapshot scope_snapshot(scope());
3086 int rewritable_length =
static_cast<int>(
3087 function_state_->destructuring_assignments_to_rewrite().size());
3089 ExpressionListT args(pointer_buffer());
3091 ParseArguments(&args, &has_spread,
true);
3092 if (V8_LIKELY(peek() == Token::ARROW)) {
3093 fni_.RemoveAsyncKeywordFromEnd();
3094 if (!classifier()->is_valid_async_arrow_formal_parameters()) {
3095 ReportClassifierError(
3096 classifier()->async_arrow_formal_parameters_error());
3097 return impl()->FailureExpression();
3099 next_arrow_function_kind_ = FunctionKind::kAsyncArrowFunction;
3100 scope_snapshot_ = std::move(scope_snapshot);
3101 rewritable_length_ = rewritable_length;
3103 if (!args.length())
return factory()->NewEmptyParentheses(pos);
3105 ExpressionT result = impl()->ExpressionListToExpression(args);
3106 result->mark_parenthesized();
3111 result = impl()->SpreadCall(result, args, pos, Call::NOT_EVAL);
3113 result = factory()->NewCall(result, args, pos, Call::NOT_EVAL);
3116 fni_.RemoveLastFunction();
3117 if (!Token::IsPropertyOrCall(peek()))
return result;
3123 case Token::LBRACK: {
3124 Consume(Token::LBRACK);
3125 int pos = position();
3126 AcceptINScope scope(
this,
true);
3127 ExpressionT index = ParseExpressionCoverGrammar();
3128 result = factory()->NewProperty(result, index, pos);
3129 Expect(Token::RBRACK);
3134 case Token::PERIOD: {
3135 Consume(Token::PERIOD);
3136 int pos = position();
3137 ExpressionT key = ParseIdentifierNameOrPrivateName();
3138 result = factory()->NewProperty(result, key, pos);
3143 case Token::LPAREN: {
3145 if (Token::IsCallable(scanner()->current_token())) {
3155 pos = peek_position();
3159 if (result->IsFunctionLiteral()) {
3160 result->AsFunctionLiteral()->SetShouldEagerCompile();
3161 result->AsFunctionLiteral()->mark_as_iife();
3165 ExpressionListT args(pointer_buffer());
3166 ParseArguments(&args, &has_spread);
3175 Call::PossiblyEval is_possibly_eval =
3176 CheckPossibleEvalCall(result, scope());
3179 result = impl()->SpreadCall(result, args, pos, is_possibly_eval);
3181 result = factory()->NewCall(result, args, pos, is_possibly_eval);
3184 fni_.RemoveLastFunction();
3190 DCHECK(Token::IsTemplate(peek()));
3191 result = ParseTemplateLiteral(result, position(),
true);
3194 }
while (Token::IsPropertyOrCall(peek()));
3198 template <
typename Impl>
3199 typename ParserBase<Impl>::ExpressionT
3200 ParserBase<Impl>::ParseMemberWithPresentNewPrefixesExpression() {
3220 Consume(Token::NEW);
3221 int new_pos = position();
3224 CheckStackOverflow();
3226 if (peek() == Token::SUPER) {
3227 const bool is_new =
true;
3228 result = ParseSuperExpression(is_new);
3229 }
else if (allow_harmony_dynamic_import() && peek() == Token::IMPORT &&
3230 (!allow_harmony_import_meta() || PeekAhead() == Token::LPAREN)) {
3231 impl()->ReportMessageAt(scanner()->peek_location(),
3232 MessageTemplate::kImportCallNotNewExpression);
3233 return impl()->FailureExpression();
3234 }
else if (peek() == Token::PERIOD) {
3235 result = ParseNewTargetExpression();
3236 return ParseMemberExpressionContinuation(result);
3238 result = ParseMemberWithNewPrefixesExpression();
3240 if (peek() == Token::LPAREN) {
3243 ExpressionListT args(pointer_buffer());
3245 ParseArguments(&args, &has_spread);
3248 result = impl()->SpreadCallNew(result, args, new_pos);
3250 result = factory()->NewCallNew(result, args, new_pos);
3254 return ParseMemberExpressionContinuation(result);
3257 ExpressionListT args(pointer_buffer());
3258 return factory()->NewCallNew(result, args, new_pos);
3261 template <
typename Impl>
3262 typename ParserBase<Impl>::ExpressionT
3263 ParserBase<Impl>::ParseMemberWithNewPrefixesExpression() {
3264 return peek() == Token::NEW ? ParseMemberWithPresentNewPrefixesExpression()
3265 : ParseMemberExpression();
3268 template <
typename Impl>
3269 typename ParserBase<Impl>::ExpressionT
3270 ParserBase<Impl>::ParseFunctionExpression() {
3271 Consume(Token::FUNCTION);
3272 int function_token_position = position();
3274 FunctionKind function_kind = Check(Token::MUL)
3275 ? FunctionKind::kGeneratorFunction
3276 : FunctionKind::kNormalFunction;
3277 IdentifierT name = impl()->NullIdentifier();
3278 bool is_strict_reserved_name =
false;
3279 Scanner::Location function_name_location = Scanner::Location::invalid();
3280 FunctionLiteral::FunctionType function_type =
3281 FunctionLiteral::kAnonymousExpression;
3282 if (impl()->ParsingDynamicFunctionDeclaration()) {
3285 Consume(Token::IDENTIFIER);
3286 DCHECK_IMPLIES(!has_error(),
3287 scanner()->CurrentSymbol(ast_value_factory()) ==
3288 ast_value_factory()->anonymous_string());
3289 }
else if (peek_any_identifier()) {
3290 bool is_await =
false;
3291 name = ParseIdentifierOrStrictReservedWord(
3292 function_kind, &is_strict_reserved_name, &is_await);
3293 function_name_location = scanner()->location();
3294 function_type = FunctionLiteral::kNamedExpression;
3296 FunctionLiteralT result = impl()->ParseFunctionLiteral(
3297 name, function_name_location,
3298 is_strict_reserved_name ? kFunctionNameIsStrictReserved
3299 : kFunctionNameValidityUnknown,
3300 function_kind, function_token_position, function_type, language_mode(),
3303 if (impl()->IsNull(result))
return impl()->FailureExpression();
3307 template <
typename Impl>
3308 typename ParserBase<Impl>::ExpressionT
3309 ParserBase<Impl>::ParseMemberExpression() {
3324 if (peek() == Token::FUNCTION) {
3325 result = ParseFunctionExpression();
3326 }
else if (peek() == Token::SUPER) {
3327 const bool is_new =
false;
3328 result = ParseSuperExpression(is_new);
3329 }
else if (allow_harmony_dynamic_import() && peek() == Token::IMPORT) {
3330 result = ParseImportExpressions();
3332 result = ParsePrimaryExpression();
3335 return ParseMemberExpressionContinuation(result);
3338 template <
typename Impl>
3339 typename ParserBase<Impl>::ExpressionT
3340 ParserBase<Impl>::ParseImportExpressions() {
3341 DCHECK(allow_harmony_dynamic_import());
3343 Consume(Token::IMPORT);
3344 int pos = position();
3345 if (allow_harmony_import_meta() && peek() == Token::PERIOD) {
3346 ExpectMetaProperty(ast_value_factory()->meta_string(),
"import.meta", pos);
3347 if (!parsing_module_) {
3348 impl()->ReportMessageAt(scanner()->location(),
3349 MessageTemplate::kImportMetaOutsideModule);
3350 return impl()->FailureExpression();
3353 return impl()->ImportMetaExpression(pos);
3355 Expect(Token::LPAREN);
3356 if (peek() == Token::RPAREN) {
3357 impl()->ReportMessageAt(scanner()->location(),
3358 MessageTemplate::kImportMissingSpecifier);
3359 return impl()->FailureExpression();
3361 AcceptINScope scope(
this,
true);
3362 ExpressionT arg = ParseAssignmentExpression();
3363 Expect(Token::RPAREN);
3365 return factory()->NewImportCallExpression(arg, pos);
3368 template <
typename Impl>
3369 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseSuperExpression(
3371 Consume(Token::SUPER);
3372 int pos = position();
3374 DeclarationScope* scope = GetReceiverScope();
3375 FunctionKind kind = scope->function_kind();
3376 if (IsConciseMethod(kind) || IsAccessorFunction(kind) ||
3377 IsClassConstructor(kind)) {
3378 if (Token::IsProperty(peek())) {
3379 scope->RecordSuperPropertyUsage();
3380 return impl()->NewSuperPropertyReference(pos);
3384 if (!is_new && peek() == Token::LPAREN && IsDerivedConstructor(kind)) {
3387 return impl()->NewSuperCallReference(pos);
3391 impl()->ReportMessageAt(scanner()->location(),
3392 MessageTemplate::kUnexpectedSuper);
3393 return impl()->FailureExpression();
3396 template <
typename Impl>
3397 void ParserBase<Impl>::ExpectMetaProperty(
const AstRawString* property_name,
3398 const char* full_name,
int pos) {
3399 Consume(Token::PERIOD);
3400 ExpectContextualKeyword(property_name);
3401 if (V8_UNLIKELY(scanner()->literal_contains_escapes())) {
3402 impl()->ReportMessageAt(Scanner::Location(pos, end_position()),
3403 MessageTemplate::kInvalidEscapedMetaProperty,
3408 template <
typename Impl>
3409 typename ParserBase<Impl>::ExpressionT
3410 ParserBase<Impl>::ParseNewTargetExpression() {
3411 int pos = position();
3412 ExpectMetaProperty(ast_value_factory()->target_string(),
"new.target", pos);
3414 if (!GetReceiverScope()->is_function_scope()) {
3415 impl()->ReportMessageAt(scanner()->location(),
3416 MessageTemplate::kUnexpectedNewTarget);
3417 return impl()->FailureExpression();
3420 return impl()->NewTargetExpression(pos);
3423 template <
typename Impl>
3424 typename ParserBase<Impl>::ExpressionT
3425 ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) {
3426 DCHECK(Token::IsMember(peek()));
3431 case Token::LBRACK: {
3432 Consume(Token::LBRACK);
3433 int pos = position();
3434 AcceptINScope scope(
this,
true);
3435 ExpressionT index = ParseExpressionCoverGrammar();
3436 expression = factory()->NewProperty(expression, index, pos);
3437 impl()->PushPropertyName(index);
3438 Expect(Token::RBRACK);
3441 case Token::PERIOD: {
3442 Consume(Token::PERIOD);
3443 int pos = peek_position();
3444 ExpressionT key = ParseIdentifierNameOrPrivateName();
3445 expression = factory()->NewProperty(expression, key, pos);
3449 DCHECK(Token::IsTemplate(peek()));
3451 if (scanner()->current_token() == Token::IDENTIFIER) {
3454 pos = peek_position();
3455 if (expression->IsFunctionLiteral()) {
3458 expression->AsFunctionLiteral()->SetShouldEagerCompile();
3461 expression = ParseTemplateLiteral(expression, pos,
true);
3465 }
while (Token::IsMember(peek()));
3469 template <
typename Impl>
3470 void ParserBase<Impl>::ParseFormalParameter(FormalParametersT* parameters) {
3473 bool is_rest = parameters->has_rest;
3475 FuncNameInferrerState fni_state(&fni_);
3476 ExpressionT pattern = ParseBindingPattern();
3477 if (!impl()->IsIdentifier(pattern)) {
3478 parameters->is_simple =
false;
3481 ExpressionT initializer = impl()->NullExpression();
3482 if (Check(Token::ASSIGN)) {
3484 ReportMessage(MessageTemplate::kRestDefaultInitializer);
3488 ExpressionClassifier init_classifier(
this);
3489 AcceptINScope scope(
this,
true);
3490 initializer = ParseAssignmentExpression();
3491 ValidateExpression();
3492 parameters->is_simple =
false;
3493 Accumulate(ExpressionClassifier::FormalParameterInitializerProduction);
3495 classifier()->RecordNonSimpleParameter();
3496 impl()->SetFunctionNameFromIdentifierRef(initializer, pattern);
3499 impl()->AddFormalParameter(parameters, pattern, initializer, end_position(),
3503 template <
typename Impl>
3504 void ParserBase<Impl>::ParseFormalParameterList(FormalParametersT* parameters) {
3516 DCHECK_EQ(0, parameters->arity);
3518 if (peek() != Token::RPAREN) {
3521 if (parameters->arity + 1 > Code::kMaxArguments) {
3522 ReportMessage(MessageTemplate::kTooManyParameters);
3525 parameters->has_rest = Check(Token::ELLIPSIS);
3526 ParseFormalParameter(parameters);
3528 if (parameters->has_rest) {
3529 parameters->is_simple =
false;
3530 classifier()->RecordNonSimpleParameter();
3531 if (peek() == Token::COMMA) {
3532 impl()->ReportMessageAt(scanner()->peek_location(),
3533 MessageTemplate::kParamAfterRest);
3538 if (!Check(Token::COMMA))
break;
3539 if (peek() == Token::RPAREN) {
3546 impl()->DeclareFormalParameters(parameters);
3549 template <
typename Impl>
3550 typename ParserBase<Impl>::BlockT ParserBase<Impl>::ParseVariableDeclarations(
3551 VariableDeclarationContext var_context,
3552 DeclarationParsingResult* parsing_result,
3553 ZonePtrList<const AstRawString>* names) {
3561 DCHECK_NOT_NULL(parsing_result);
3562 parsing_result->descriptor.declaration_kind = DeclarationDescriptor::NORMAL;
3563 parsing_result->descriptor.declaration_pos = peek_position();
3564 parsing_result->descriptor.initialization_pos = peek_position();
3566 BlockT init_block = impl()->NullStatement();
3567 if (var_context != kForStatement) {
3568 init_block = factory()->NewBlock(1,
true);
3573 parsing_result->descriptor.mode = VariableMode::kVar;
3574 Consume(Token::VAR);
3577 Consume(Token::CONST);
3578 DCHECK_NE(var_context, kStatement);
3579 parsing_result->descriptor.mode = VariableMode::kConst;
3582 Consume(Token::LET);
3583 DCHECK_NE(var_context, kStatement);
3584 parsing_result->descriptor.mode = VariableMode::kLet;
3591 parsing_result->descriptor.scope = scope();
3593 int bindings_start = peek_position();
3596 FuncNameInferrerState fni_state(&fni_);
3598 ExpressionT pattern = impl()->NullExpression();
3599 int decl_pos = peek_position();
3601 ExpressionClassifier pattern_classifier(
this);
3602 pattern = ParseBindingPattern();
3604 if (IsLexicalVariableMode(parsing_result->descriptor.mode)) {
3605 ValidateLetPattern();
3608 Scanner::Location variable_loc = scanner()->location();
3610 bool single_name = impl()->IsIdentifier(pattern);
3612 impl()->PushVariableName(impl()->AsIdentifier(pattern));
3615 ExpressionT value = impl()->NullExpression();
3616 int initializer_position = kNoSourcePosition;
3617 int value_beg_position = kNoSourcePosition;
3618 if (Check(Token::ASSIGN)) {
3619 value_beg_position = peek_position();
3621 ExpressionClassifier classifier(
this);
3622 AcceptINScope scope(
this, var_context != kForStatement);
3623 value = ParseAssignmentExpression();
3624 ValidateExpression();
3625 variable_loc.end_pos = end_position();
3627 if (!parsing_result->first_initializer_loc.IsValid()) {
3628 parsing_result->first_initializer_loc = variable_loc;
3633 if (!value->IsCall() && !value->IsCallNew()) {
3636 fni_.RemoveLastFunction();
3640 impl()->SetFunctionNameFromIdentifierRef(value, pattern);
3643 initializer_position = end_position();
3645 if (var_context != kForStatement || !PeekInOrOf()) {
3647 if (parsing_result->descriptor.mode == VariableMode::kConst ||
3648 !impl()->IsIdentifier(pattern)) {
3649 impl()->ReportMessageAt(
3650 Scanner::Location(decl_pos, end_position()),
3651 MessageTemplate::kDeclarationMissingInitializer,
3652 !impl()->IsIdentifier(pattern) ?
"destructuring" :
"const");
3653 return impl()->NullStatement();
3656 if (parsing_result->descriptor.mode == VariableMode::kLet) {
3657 value = factory()->NewUndefinedLiteral(position());
3662 initializer_position = position();
3665 typename DeclarationParsingResult::Declaration decl(
3666 pattern, initializer_position, value);
3667 decl.value_beg_position = value_beg_position;
3668 if (var_context == kForStatement) {
3670 parsing_result->declarations.push_back(decl);
3676 impl()->DeclareAndInitializeVariables(
3677 init_block, &parsing_result->descriptor, &decl, names);
3679 }
while (Check(Token::COMMA));
3681 parsing_result->bindings_loc =
3682 Scanner::Location(bindings_start, end_position());
3687 template <
typename Impl>
3688 typename ParserBase<Impl>::StatementT
3689 ParserBase<Impl>::ParseFunctionDeclaration() {
3690 Consume(Token::FUNCTION);
3692 int pos = position();
3693 ParseFunctionFlags flags = ParseFunctionFlag::kIsNormal;
3694 if (Check(Token::MUL)) {
3695 impl()->ReportMessageAt(
3696 scanner()->location(),
3697 MessageTemplate::kGeneratorInSingleStatementContext);
3698 return impl()->NullStatement();
3700 return ParseHoistableDeclaration(pos, flags,
nullptr,
false);
3703 template <
typename Impl>
3704 typename ParserBase<Impl>::StatementT
3705 ParserBase<Impl>::ParseHoistableDeclaration(
3706 ZonePtrList<const AstRawString>* names,
bool default_export) {
3707 Consume(Token::FUNCTION);
3709 int pos = position();
3710 ParseFunctionFlags flags = ParseFunctionFlag::kIsNormal;
3711 if (Check(Token::MUL)) {
3712 flags |= ParseFunctionFlag::kIsGenerator;
3714 return ParseHoistableDeclaration(pos, flags, names, default_export);
3717 template <
typename Impl>
3718 typename ParserBase<Impl>::StatementT
3719 ParserBase<Impl>::ParseHoistableDeclaration(
3720 int pos, ParseFunctionFlags flags, ZonePtrList<const AstRawString>* names,
3721 bool default_export) {
3722 CheckStackOverflow();
3735 DCHECK_IMPLIES((flags & ParseFunctionFlag::kIsAsync) != 0,
3736 (flags & ParseFunctionFlag::kIsGenerator) == 0);
3738 if ((flags & ParseFunctionFlag::kIsAsync) != 0 && Check(Token::MUL)) {
3740 flags |= ParseFunctionFlag::kIsGenerator;
3744 FunctionNameValidity name_validity;
3745 IdentifierT variable_name;
3746 if (default_export && peek() == Token::LPAREN) {
3747 impl()->GetDefaultStrings(&name, &variable_name);
3748 name_validity = kSkipFunctionNameCheck;
3750 bool is_strict_reserved =
false;
3751 bool is_await =
false;
3752 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved, &is_await);
3753 name_validity = is_strict_reserved ? kFunctionNameIsStrictReserved
3754 : kFunctionNameValidityUnknown;
3755 variable_name = name;
3758 FuncNameInferrerState fni_state(&fni_);
3759 impl()->PushEnclosingName(name);
3761 FunctionKind kind = FunctionKindFor(flags);
3763 FunctionLiteralT
function = impl()->ParseFunctionLiteral(
3764 name, scanner()->location(), name_validity, kind, pos,
3765 FunctionLiteral::kDeclaration, language_mode(),
nullptr);
3770 (!scope()->is_declaration_scope() || scope()->is_module_scope())
3771 ? VariableMode::kLet
3772 : VariableMode::kVar;
3778 bool is_sloppy_block_function = is_sloppy(language_mode()) &&
3779 !scope()->is_declaration_scope() &&
3780 flags == ParseFunctionFlag::kIsNormal;
3782 return impl()->DeclareFunction(variable_name,
function, mode, pos,
3783 is_sloppy_block_function, names);
3786 template <
typename Impl>
3787 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseClassDeclaration(
3788 ZonePtrList<const AstRawString>* names,
bool default_export) {
3807 int class_token_pos = position();
3808 IdentifierT name = impl()->NullIdentifier();
3809 bool is_strict_reserved =
false;
3810 IdentifierT variable_name = impl()->NullIdentifier();
3811 if (default_export && (peek() == Token::EXTENDS || peek() == Token::LBRACE)) {
3812 impl()->GetDefaultStrings(&name, &variable_name);
3814 bool is_await =
false;
3815 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved, &is_await);
3816 variable_name = name;
3819 ExpressionClassifier no_classifier(
this);
3820 ExpressionT value = ParseClassLiteral(name, scanner()->location(),
3821 is_strict_reserved, class_token_pos);
3822 ValidateExpression();
3823 int end_pos = position();
3824 return impl()->DeclareClass(variable_name, value, names, class_token_pos,
3832 template <
typename Impl>
3833 typename ParserBase<Impl>::StatementT
3834 ParserBase<Impl>::ParseNativeDeclaration() {
3835 function_state_->DisableOptimization(BailoutReason::kNativeFunctionLiteral);
3837 int pos = peek_position();
3838 Consume(Token::FUNCTION);
3840 IdentifierT name = ParseIdentifier(kAllowRestrictedIdentifiers);
3841 Expect(Token::LPAREN);
3842 if (peek() != Token::RPAREN) {
3844 ParseIdentifier(kAllowRestrictedIdentifiers);
3845 }
while (Check(Token::COMMA));
3847 Expect(Token::RPAREN);
3848 Expect(Token::SEMICOLON);
3849 return impl()->DeclareNative(name, pos);
3852 template <
typename Impl>
3853 typename ParserBase<Impl>::StatementT
3854 ParserBase<Impl>::ParseAsyncFunctionDeclaration(
3855 ZonePtrList<const AstRawString>* names,
bool default_export) {
3859 DCHECK_EQ(scanner()->current_token(), Token::ASYNC);
3860 int pos = position();
3861 DCHECK(!scanner()->HasLineTerminatorBeforeNext());
3862 Consume(Token::FUNCTION);
3863 ParseFunctionFlags flags = ParseFunctionFlag::kIsAsync;
3864 return ParseHoistableDeclaration(pos, flags, names, default_export);
3867 template <
typename Impl>
3868 void ParserBase<Impl>::ParseFunctionBody(
3869 typename ParserBase<Impl>::StatementListT* body, IdentifierT function_name,
3870 int pos,
const FormalParametersT& parameters, FunctionKind kind,
3871 FunctionLiteral::FunctionType function_type, FunctionBodyType body_type) {
3872 DeclarationScope* function_scope = scope()->AsDeclarationScope();
3873 DeclarationScope* inner_scope = function_scope;
3875 if (!parameters.is_simple) {
3876 inner_scope = NewVarblockScope();
3877 inner_scope->set_start_position(scanner()->location().beg_pos);
3881 BlockState block_state(&scope_, inner_scope);
3883 if (IsResumableFunction(kind)) impl()->PrepareGeneratorVariables();
3885 if (body_type == FunctionBodyType::kExpression) {
3886 ExpressionClassifier classifier(
this);
3887 ExpressionT expression = ParseAssignmentExpression();
3888 ValidateExpression();
3890 if (IsAsyncFunction(kind)) {
3891 BlockT block = factory()->NewBlock(1,
true);
3892 impl()->RewriteAsyncFunctionBody(body, block, expression);
3894 body->Add(BuildReturnStatement(expression, expression->position()));
3898 DCHECK_EQ(FunctionBodyType::kBlock, body_type);
3901 Token::Value closing_token = function_type == FunctionLiteral::kWrapped
3905 if (IsAsyncGeneratorFunction(kind)) {
3906 impl()->ParseAndRewriteAsyncGeneratorFunctionBody(pos, kind, body);
3907 }
else if (IsGeneratorFunction(kind)) {
3908 impl()->ParseAndRewriteGeneratorFunctionBody(pos, kind, body);
3909 }
else if (IsAsyncFunction(kind)) {
3910 ParseAsyncFunctionBody(inner_scope, body);
3912 ParseStatementList(body, closing_token);
3915 if (IsDerivedConstructor(kind)) {
3916 body->Add(factory()->NewReturnStatement(impl()->ThisExpression(),
3917 kNoSourcePosition));
3919 Expect(closing_token);
3923 scope()->set_end_position(end_position());
3925 bool allow_duplicate_parameters =
false;
3927 if (parameters.is_simple) {
3928 DCHECK_EQ(inner_scope, function_scope);
3929 if (is_sloppy(function_scope->language_mode())) {
3930 impl()->InsertSloppyBlockFunctionVarBindings(function_scope);
3932 allow_duplicate_parameters = is_sloppy(function_scope->language_mode()) &&
3933 !IsConciseMethod(kind) &&
3934 !IsArrowFunction(kind);
3936 BlockT inner_block = factory()->NewBlock(
true, *body);
3937 inner_block->set_scope(inner_scope);
3939 DCHECK_NOT_NULL(inner_scope);
3940 DCHECK_EQ(function_scope, scope());
3941 DCHECK_EQ(function_scope, inner_scope->outer_scope());
3942 impl()->SetLanguageMode(function_scope, inner_scope->language_mode());
3944 if (has_error())
return;
3945 BlockT init_block = impl()->BuildParameterInitializationBlock(parameters);
3947 if (is_sloppy(inner_scope->language_mode())) {
3948 impl()->InsertSloppyBlockFunctionVarBindings(inner_scope);
3952 if (IsAsyncFunction(kind) && !IsAsyncGeneratorFunction(kind)) {
3953 init_block = impl()->BuildRejectPromiseOnException(init_block);
3956 inner_scope->set_end_position(end_position());
3957 if (inner_scope->FinalizeBlockScope() !=
nullptr) {
3958 impl()->CheckConflictingVarDeclarations(inner_scope);
3959 impl()->InsertShadowingVarBindingInitializers(inner_block);
3961 inner_block->set_scope(
nullptr);
3963 inner_scope =
nullptr;
3965 body->Add(init_block);
3966 body->Add(inner_block);
3969 ValidateFormalParameters(language_mode(), parameters,
3970 allow_duplicate_parameters);
3972 if (!IsArrowFunction(kind)) {
3976 function_scope->DeclareArguments(ast_value_factory());
3979 impl()->DeclareFunctionNameVar(function_name, function_type, function_scope);
3982 template <
typename Impl>
3983 void ParserBase<Impl>::CheckArityRestrictions(
int param_count,
3984 FunctionKind function_kind,
3986 int formals_start_pos,
3987 int formals_end_pos) {
3988 if (IsGetterFunction(function_kind)) {
3989 if (param_count != 0) {
3990 impl()->ReportMessageAt(
3991 Scanner::Location(formals_start_pos, formals_end_pos),
3992 MessageTemplate::kBadGetterArity);
3994 }
else if (IsSetterFunction(function_kind)) {
3995 if (param_count != 1) {
3996 impl()->ReportMessageAt(
3997 Scanner::Location(formals_start_pos, formals_end_pos),
3998 MessageTemplate::kBadSetterArity);
4001 impl()->ReportMessageAt(
4002 Scanner::Location(formals_start_pos, formals_end_pos),
4003 MessageTemplate::kBadSetterRestParameter);
4008 template <
typename Impl>
4009 bool ParserBase<Impl>::IsNextLetKeyword() {
4010 DCHECK_EQ(Token::LET, peek());
4011 Token::Value next_next = PeekAhead();
4012 switch (next_next) {
4015 case Token::IDENTIFIER:
4026 case Token::FUTURE_STRICT_RESERVED_WORD:
4027 return is_sloppy(language_mode());
4033 template <
typename Impl>
4034 typename ParserBase<Impl>::ExpressionT
4035 ParserBase<Impl>::ParseArrowFunctionLiteral(
4036 const FormalParametersT& formal_parameters) {
4037 const RuntimeCallCounterId counters[2][2] = {
4038 {RuntimeCallCounterId::kParseBackgroundArrowFunctionLiteral,
4039 RuntimeCallCounterId::kParseArrowFunctionLiteral},
4040 {RuntimeCallCounterId::kPreParseBackgroundArrowFunctionLiteral,
4041 RuntimeCallCounterId::kPreParseArrowFunctionLiteral}};
4042 RuntimeCallTimerScope runtime_timer(
4043 runtime_call_stats_,
4044 counters[Impl::IsPreParser()][parsing_on_main_thread_]);
4045 base::ElapsedTimer timer;
4046 if (V8_UNLIKELY(FLAG_log_function_events)) timer.Start();
4048 DCHECK_IMPLIES(!has_error(), peek() == Token::ARROW);
4049 if (scanner_->HasLineTerminatorBeforeNext()) {
4053 ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW);
4054 return impl()->FailureExpression();
4057 int expected_property_count = -1;
4058 int suspend_count = 0;
4059 int function_literal_id = GetNextFunctionLiteralId();
4061 FunctionKind kind = formal_parameters.scope->function_kind();
4062 FunctionLiteral::EagerCompileHint eager_compile_hint =
4063 default_eager_compile_hint_;
4064 bool can_preparse = impl()->parse_lazily() &&
4065 eager_compile_hint == FunctionLiteral::kShouldLazyCompile;
4068 bool is_lazy_top_level_function =
4069 can_preparse && impl()->AllowsLazyParsingWithoutUnresolvedVariables();
4070 bool has_braces =
true;
4071 ProducedPreParsedScopeData* produced_preparsed_scope_data =
nullptr;
4072 StatementListT body(pointer_buffer());
4074 FunctionState function_state(&function_state_, &scope_,
4075 formal_parameters.scope);
4077 DCHECK_IMPLIES(!has_error(), -1 != rewritable_length_);
4080 function_state.AdoptDestructuringAssignmentsFromParentState(
4081 rewritable_length_);
4082 rewritable_length_ = -1;
4084 Consume(Token::ARROW);
4086 if (peek() == Token::LBRACE) {
4088 DCHECK_EQ(scope(), formal_parameters.scope);
4089 if (is_lazy_top_level_function) {
4096 int dummy_num_parameters = -1;
4097 DCHECK_NE(kind & FunctionKind::kArrowFunction, 0);
4098 FunctionLiteral::EagerCompileHint hint;
4099 bool did_preparse_successfully = impl()->SkipFunction(
4100 nullptr, kind, FunctionLiteral::kAnonymousExpression,
4101 formal_parameters.scope, &dummy_num_parameters,
4102 &produced_preparsed_scope_data,
false, &hint);
4106 ValidateFormalParameters(language_mode(), formal_parameters,
false);
4108 DCHECK_NULL(produced_preparsed_scope_data);
4110 if (did_preparse_successfully) {
4114 function_state.RewindDestructuringAssignments(0);
4118 Consume(Token::LBRACE);
4119 AcceptINScope scope(
this,
true);
4120 ParseFunctionBody(&body, impl()->NullIdentifier(), kNoSourcePosition,
4121 formal_parameters, kind,
4122 FunctionLiteral::kAnonymousExpression,
4123 FunctionBodyType::kBlock);
4125 return impl()->FailureExpression();
4128 Consume(Token::LBRACE);
4129 AcceptINScope scope(
this,
true);
4130 ParseFunctionBody(&body, impl()->NullIdentifier(), kNoSourcePosition,
4131 formal_parameters, kind,
4132 FunctionLiteral::kAnonymousExpression,
4133 FunctionBodyType::kBlock);
4134 expected_property_count = function_state.expected_property_count();
4139 ParseFunctionBody(&body, impl()->NullIdentifier(), kNoSourcePosition,
4140 formal_parameters, kind,
4141 FunctionLiteral::kAnonymousExpression,
4142 FunctionBodyType::kExpression);
4143 expected_property_count = function_state.expected_property_count();
4146 formal_parameters.scope->set_end_position(end_position());
4149 if (is_strict(language_mode())) {
4150 CheckStrictOctalLiteral(formal_parameters.scope->start_position(),
4153 impl()->CheckConflictingVarDeclarations(formal_parameters.scope);
4155 impl()->RewriteDestructuringAssignments();
4156 suspend_count = function_state.suspend_count();
4159 FunctionLiteralT function_literal = factory()->NewFunctionLiteral(
4160 impl()->EmptyIdentifierString(), formal_parameters.scope, body,
4161 expected_property_count, formal_parameters.num_parameters(),
4162 formal_parameters.function_length,
4163 FunctionLiteral::kNoDuplicateParameters,
4164 FunctionLiteral::kAnonymousExpression, eager_compile_hint,
4165 formal_parameters.scope->start_position(), has_braces,
4166 function_literal_id, produced_preparsed_scope_data);
4168 function_literal->set_suspend_count(suspend_count);
4169 function_literal->set_function_token_position(
4170 formal_parameters.scope->start_position());
4172 impl()->AddFunctionForNameInference(function_literal);
4174 if (V8_UNLIKELY((FLAG_log_function_events))) {
4175 Scope* scope = formal_parameters.scope;
4176 double ms = timer.Elapsed().InMillisecondsF();
4177 const char* event_name =
4178 is_lazy_top_level_function ?
"preparse-no-resolution" :
"parse";
4179 const char* name =
"arrow function";
4180 logger_->FunctionEvent(event_name, script_id(), ms, scope->start_position(),
4181 scope->end_position(), name, strlen(name));
4184 return function_literal;
4187 template <
typename Impl>
4188 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseClassLiteral(
4189 IdentifierT name, Scanner::Location class_name_location,
4190 bool name_is_strict_reserved,
int class_token_pos) {
4191 bool is_anonymous = impl()->IsNull(name);
4194 if (!is_anonymous) {
4195 if (name_is_strict_reserved) {
4196 impl()->ReportMessageAt(class_name_location,
4197 MessageTemplate::kUnexpectedStrictReserved);
4198 return impl()->FailureExpression();
4200 if (impl()->IsEvalOrArguments(name)) {
4201 impl()->ReportMessageAt(class_name_location,
4202 MessageTemplate::kStrictEvalArguments);
4203 return impl()->FailureExpression();
4207 Scope* block_scope = NewScope(BLOCK_SCOPE);
4208 BlockState block_state(&scope_, block_scope);
4209 RaiseLanguageMode(LanguageMode::kStrict);
4211 ClassInfo class_info(
this);
4212 class_info.is_anonymous = is_anonymous;
4213 impl()->DeclareClassVariable(name, &class_info, class_token_pos);
4215 scope()->set_start_position(end_position());
4216 if (Check(Token::EXTENDS)) {
4217 FuncNameInferrerState fni_state(&fni_);
4218 class_info.extends = ParseLeftHandSideExpression();
4219 ValidateExpression();
4222 Expect(Token::LBRACE);
4224 const bool has_extends = !impl()->IsNull(class_info.extends);
4225 while (peek() != Token::RBRACE) {
4226 if (Check(Token::SEMICOLON))
continue;
4227 FuncNameInferrerState fni_state(&fni_);
4230 bool is_constructor = !class_info.has_seen_constructor;
4231 ParsePropertyInfo prop_info(
this);
4232 prop_info.position = PropertyPosition::kClassLiteral;
4233 ClassLiteralPropertyT
property =
4234 ParseClassPropertyDefinition(&class_info, &prop_info, has_extends);
4236 if (has_error())
return impl()->FailureExpression();
4238 ClassLiteralProperty::Kind property_kind =
4239 ClassPropertyKindFor(prop_info.kind);
4240 if (!class_info.has_static_computed_names && prop_info.is_static &&
4241 prop_info.is_computed_name) {
4242 class_info.has_static_computed_names =
true;
4244 if (prop_info.is_computed_name && !prop_info.is_private &&
4245 property_kind == ClassLiteralProperty::FIELD) {
4246 class_info.computed_field_count++;
4248 is_constructor &= class_info.has_seen_constructor;
4250 impl()->DeclareClassProperty(name, property, prop_info.name, property_kind,
4251 prop_info.is_static, is_constructor,
4252 prop_info.is_computed_name,
4253 prop_info.is_private, &class_info);
4254 impl()->InferFunctionName();
4257 Expect(Token::RBRACE);
4258 int end_pos = end_position();
4259 block_scope->set_end_position(end_pos);
4260 return impl()->RewriteClassLiteral(block_scope, name, &class_info,
4261 class_token_pos, end_pos);
4264 template <
typename Impl>
4265 void ParserBase<Impl>::ParseAsyncFunctionBody(Scope* scope,
4266 StatementListT* body) {
4267 BlockT block = impl()->NullStatement();
4269 StatementListT statements(pointer_buffer());
4270 ParseStatementList(&statements, Token::RBRACE);
4271 block = factory()->NewBlock(
true, statements);
4273 impl()->RewriteAsyncFunctionBody(
4274 body, block, factory()->NewUndefinedLiteral(kNoSourcePosition));
4275 scope->set_end_position(end_position());
4278 template <
typename Impl>
4279 typename ParserBase<Impl>::ExpressionT
4280 ParserBase<Impl>::ParseAsyncFunctionLiteral() {
4287 DCHECK_EQ(scanner()->current_token(), Token::ASYNC);
4288 int pos = position();
4289 Consume(Token::FUNCTION);
4290 bool is_strict_reserved =
false;
4291 IdentifierT name = impl()->NullIdentifier();
4292 FunctionLiteral::FunctionType type = FunctionLiteral::kAnonymousExpression;
4294 ParseFunctionFlags flags = ParseFunctionFlag::kIsAsync;
4295 if (Check(Token::MUL)) flags |= ParseFunctionFlag::kIsGenerator;
4296 const FunctionKind kind = FunctionKindFor(flags);
4298 if (impl()->ParsingDynamicFunctionDeclaration()) {
4304 Consume(Token::IDENTIFIER);
4305 DCHECK_IMPLIES(!has_error(),
4306 scanner()->CurrentSymbol(ast_value_factory()) ==
4307 ast_value_factory()->anonymous_string());
4308 }
else if (peek_any_identifier()) {
4309 type = FunctionLiteral::kNamedExpression;
4310 bool is_await =
false;
4311 name = ParseIdentifierOrStrictReservedWord(kind, &is_strict_reserved,
4317 FunctionLiteralT result = impl()->ParseFunctionLiteral(
4318 name, scanner()->location(),
4319 is_strict_reserved ? kFunctionNameIsStrictReserved
4320 : kFunctionNameValidityUnknown,
4321 kind, pos, type, language_mode(),
nullptr);
4322 if (impl()->IsNull(result))
return impl()->FailureExpression();
4326 template <
typename Impl>
4327 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseTemplateLiteral(
4328 ExpressionT tag,
int start,
bool tagged) {
4339 DCHECK(peek() == Token::TEMPLATE_SPAN || peek() == Token::TEMPLATE_TAIL);
4344 set_allow_eval_cache(
false);
4347 bool forbid_illegal_escapes = !tagged;
4352 if (peek() == Token::TEMPLATE_TAIL) {
4353 Consume(Token::TEMPLATE_TAIL);
4354 int pos = position();
4355 typename Impl::TemplateLiteralState ts = impl()->OpenTemplateLiteral(pos);
4356 bool is_valid = CheckTemplateEscapes(forbid_illegal_escapes);
4357 impl()->AddTemplateSpan(&ts, is_valid,
true);
4358 return impl()->CloseTemplateLiteral(&ts, start, tag);
4361 Consume(Token::TEMPLATE_SPAN);
4362 int pos = position();
4363 typename Impl::TemplateLiteralState ts = impl()->OpenTemplateLiteral(pos);
4364 bool is_valid = CheckTemplateEscapes(forbid_illegal_escapes);
4365 impl()->AddTemplateSpan(&ts, is_valid,
false);
4375 int expr_pos = peek_position();
4376 AcceptINScope scope(
this,
true);
4377 ExpressionT expression = ParseExpressionCoverGrammar();
4378 impl()->AddTemplateExpression(&ts, expression);
4380 if (peek() != Token::RBRACE) {
4381 impl()->ReportMessageAt(Scanner::Location(expr_pos, peek_position()),
4382 MessageTemplate::kUnterminatedTemplateExpr);
4383 return impl()->FailureExpression();
4388 next = scanner()->ScanTemplateContinuation();
4392 bool is_valid = CheckTemplateEscapes(forbid_illegal_escapes);
4393 impl()->AddTemplateSpan(&ts, is_valid, next == Token::TEMPLATE_TAIL);
4394 }
while (next == Token::TEMPLATE_SPAN);
4396 DCHECK_IMPLIES(!has_error(), next == Token::TEMPLATE_TAIL);
4398 return impl()->CloseTemplateLiteral(&ts, start, tag);
4401 template <
typename Impl>
4402 typename ParserBase<Impl>::ExpressionT
4403 ParserBase<Impl>::RewriteInvalidReferenceExpression(ExpressionT expression,
4404 int beg_pos,
int end_pos,
4405 MessageTemplate message) {
4406 return RewriteInvalidReferenceExpression(expression, beg_pos, end_pos,
4407 message, kReferenceError);
4410 template <
typename Impl>
4411 typename ParserBase<Impl>::ExpressionT
4412 ParserBase<Impl>::RewriteInvalidReferenceExpression(ExpressionT expression,
4413 int beg_pos,
int end_pos,
4414 MessageTemplate message,
4415 ParseErrorType type) {
4416 DCHECK(!IsValidReferenceExpression(expression));
4417 if (impl()->IsIdentifier(expression)) {
4418 DCHECK(is_strict(language_mode()));
4419 DCHECK(impl()->IsEvalOrArguments(impl()->AsIdentifier(expression)));
4421 ReportMessageAt(Scanner::Location(beg_pos, end_pos),
4422 MessageTemplate::kStrictEvalArguments, kSyntaxError);
4423 return impl()->FailureExpression();
4425 if (expression->IsCall() && !expression->AsCall()->is_tagged_template()) {
4430 is_strict(language_mode())
4431 ? v8::Isolate::kAssigmentExpressionLHSIsCallInStrict
4432 : v8::Isolate::kAssigmentExpressionLHSIsCallInSloppy);
4433 ExpressionT error = impl()->NewThrowReferenceError(message, beg_pos);
4434 return factory()->NewProperty(expression, error, beg_pos);
4436 ReportMessageAt(Scanner::Location(beg_pos, end_pos), message, type);
4437 return impl()->FailureExpression();
4440 template <
typename Impl>
4441 void ParserBase<Impl>::CheckArrowFormalParameter(ExpressionT formal) {
4442 if (formal->is_parenthesized() ||
4443 !(impl()->IsIdentifier(formal) || formal->IsPattern() ||
4444 formal->IsAssignment())) {
4445 classifier()->RecordBindingPatternError(
4446 Scanner::Location(formal->position(), end_position()),
4447 MessageTemplate::kInvalidDestructuringTarget);
4448 }
else if (!impl()->IsIdentifier(formal)) {
4449 classifier()->RecordNonSimpleParameter();
4453 template <
typename Impl>
4454 bool ParserBase<Impl>::IsValidReferenceExpression(ExpressionT expression) {
4455 return IsAssignableIdentifier(expression) ||
4456 (expression->IsProperty() && classifier()->is_valid_expression());
4459 template <
typename Impl>
4460 void ParserBase<Impl>::CheckDestructuringElement(ExpressionT expression,
4461 int begin,
int end) {
4462 if (IsValidReferenceExpression(expression)) {
4467 if (expression->IsProperty()) {
4468 classifier()->RecordBindingPatternError(
4469 Scanner::Location(begin, end),
4470 MessageTemplate::kInvalidPropertyBindingPattern);
4474 if (expression->is_parenthesized() ||
4475 (!expression->IsPattern() && !expression->IsAssignment())) {
4476 classifier()->RecordPatternError(
4477 Scanner::Location(begin, end),
4478 MessageTemplate::kInvalidDestructuringTarget);
4482 template <
typename Impl>
4483 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseV8Intrinsic() {
4487 int pos = peek_position();
4488 Consume(Token::MOD);
4490 IdentifierT name = ParseIdentifier(kAllowRestrictedIdentifiers);
4491 if (peek() != Token::LPAREN) {
4492 impl()->ReportUnexpectedToken(peek());
4493 return impl()->FailureExpression();
4496 ExpressionListT args(pointer_buffer());
4497 ParseArguments(&args, &has_spread);
4500 ReportMessageAt(Scanner::Location(pos, position()),
4501 MessageTemplate::kIntrinsicWithSpread, kSyntaxError);
4502 return impl()->FailureExpression();
4505 return impl()->NewV8Intrinsic(name, args, pos);
4508 template <
typename Impl>
4509 typename ParserBase<Impl>::LazyParsingResult
4510 ParserBase<Impl>::ParseStatementList(StatementListT* body,
4511 Token::Value end_token,
bool may_abort) {
4514 DCHECK_NOT_NULL(body);
4516 while (peek() == Token::STRING) {
4517 bool use_strict =
false;
4518 bool use_asm =
false;
4520 Scanner::Location token_loc = scanner()->peek_location();
4522 if (scanner()->NextLiteralEquals(
"use strict")) {
4524 }
else if (scanner()->NextLiteralEquals(
"use asm")) {
4528 StatementT stat = ParseStatementListItem();
4529 if (impl()->IsNull(stat))
return kLazyParsingComplete;
4534 if (!impl()->IsStringLiteral(stat))
break;
4538 RaiseLanguageMode(LanguageMode::kStrict);
4539 if (!scope()->HasSimpleParameters()) {
4543 impl()->ReportMessageAt(token_loc,
4544 MessageTemplate::kIllegalLanguageModeDirective,
4546 return kLazyParsingComplete;
4548 }
else if (use_asm) {
4550 impl()->SetAsmModule();
4555 RaiseLanguageMode(LanguageMode::kSloppy);
4562 TargetScopeT target_scope(
this);
4563 int count_statements = 0;
4566 while (peek() == Token::IDENTIFIER) {
4567 StatementT stat = ParseStatementListItem();
4572 if (++count_statements > kLazyParseTrialLimit)
return kLazyParsingAborted;
4577 while (peek() != end_token) {
4578 StatementT stat = ParseStatementListItem();
4579 if (impl()->IsNull(stat))
return kLazyParsingComplete;
4580 if (stat->IsEmptyStatement())
continue;
4584 return kLazyParsingComplete;
4587 template <
typename Impl>
4588 typename ParserBase<Impl>::StatementT
4589 ParserBase<Impl>::ParseStatementListItem() {
4608 case Token::FUNCTION:
4609 return ParseHoistableDeclaration(
nullptr,
false);
4611 Consume(Token::CLASS);
4612 return ParseClassDeclaration(
nullptr,
false);
4615 return ParseVariableStatement(kStatementListItem,
nullptr);
4617 if (IsNextLetKeyword()) {
4618 return ParseVariableStatement(kStatementListItem,
nullptr);
4622 if (PeekAhead() == Token::FUNCTION &&
4623 !scanner()->HasLineTerminatorAfterNext()) {
4624 Consume(Token::ASYNC);
4625 return ParseAsyncFunctionDeclaration(
nullptr,
false);
4631 return ParseStatement(
nullptr,
nullptr, kAllowLabelledFunctionStatement);
4634 template <
typename Impl>
4635 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseStatement(
4636 ZonePtrList<const AstRawString>* labels,
4637 ZonePtrList<const AstRawString>* own_labels,
4638 AllowLabelledFunctionStatement allow_function) {
4657 DCHECK_IMPLIES(labels ==
nullptr, own_labels ==
nullptr);
4667 return ParseBlock(labels);
4668 case Token::SEMICOLON:
4670 return factory()->EmptyStatement();
4672 return ParseIfStatement(labels);
4674 return ParseDoWhileStatement(labels, own_labels);
4676 return ParseWhileStatement(labels, own_labels);
4678 if (V8_UNLIKELY(is_async_function() && PeekAhead() == Token::AWAIT)) {
4679 return ParseForAwaitStatement(labels, own_labels);
4681 return ParseForStatement(labels, own_labels);
4682 case Token::CONTINUE:
4683 return ParseContinueStatement();
4685 return ParseBreakStatement(labels);
4687 return ParseReturnStatement();
4689 return ParseThrowStatement();
4696 if (labels ==
nullptr)
return ParseTryStatement();
4697 StatementListT statements(pointer_buffer());
4698 BlockT result = factory()->NewBlock(
false, labels);
4699 TargetT target(
this, result);
4700 StatementT statement = ParseTryStatement();
4701 statements.Add(statement);
4702 result->InitializeStatements(statements, zone());
4706 return ParseWithStatement(labels);
4708 return ParseSwitchStatement(labels);
4709 case Token::FUNCTION:
4715 impl()->ReportMessageAt(scanner()->peek_location(),
4716 is_strict(language_mode())
4717 ? MessageTemplate::kStrictFunction
4718 : MessageTemplate::kSloppyFunction);
4719 return impl()->NullStatement();
4720 case Token::DEBUGGER:
4721 return ParseDebuggerStatement();
4723 return ParseVariableStatement(kStatement,
nullptr);
4725 if (!scanner()->HasLineTerminatorAfterNext() &&
4726 PeekAhead() == Token::FUNCTION) {
4727 impl()->ReportMessageAt(
4728 scanner()->peek_location(),
4729 MessageTemplate::kAsyncFunctionInSingleStatementContext);
4730 return impl()->NullStatement();
4734 return ParseExpressionOrLabelledStatement(labels, own_labels,
4739 template <
typename Impl>
4740 typename ParserBase<Impl>::BlockT ParserBase<Impl>::ParseBlock(
4741 ZonePtrList<const AstRawString>* labels) {
4745 BlockT body = factory()->NewBlock(
false, labels);
4746 StatementListT statements(pointer_buffer());
4749 Expect(Token::LBRACE);
4751 CheckStackOverflow();
4754 BlockState block_state(zone(), &scope_);
4755 scope()->set_start_position(scanner()->location().beg_pos);
4756 TargetT target(
this, body);
4758 while (peek() != Token::RBRACE) {
4759 StatementT stat = ParseStatementListItem();
4760 if (impl()->IsNull(stat))
return body;
4761 if (stat->IsEmptyStatement())
continue;
4762 statements.Add(stat);
4765 Expect(Token::RBRACE);
4766 int end_pos = end_position();
4767 scope()->set_end_position(end_pos);
4769 impl()->RecordBlockSourceRange(body, end_pos);
4770 body->set_scope(scope()->FinalizeBlockScope());
4773 body->InitializeStatements(statements, zone_);
4777 template <
typename Impl>
4778 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseScopedStatement(
4779 ZonePtrList<const AstRawString>* labels) {
4780 if (is_strict(language_mode()) || peek() != Token::FUNCTION) {
4781 return ParseStatement(labels,
nullptr);
4785 BlockState block_state(zone(), &scope_);
4786 scope()->set_start_position(scanner()->location().beg_pos);
4787 BlockT block = factory()->NewBlock(1,
false);
4788 StatementT body = ParseFunctionDeclaration();
4789 block->statements()->Add(body, zone());
4790 scope()->set_end_position(end_position());
4791 block->set_scope(scope()->FinalizeBlockScope());
4796 template <
typename Impl>
4797 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseVariableStatement(
4798 VariableDeclarationContext var_context,
4799 ZonePtrList<const AstRawString>* names) {
4815 DeclarationParsingResult parsing_result;
4817 ParseVariableDeclarations(var_context, &parsing_result, names);
4822 template <
typename Impl>
4823 typename ParserBase<Impl>::StatementT
4824 ParserBase<Impl>::ParseDebuggerStatement() {
4831 int pos = peek_position();
4832 Consume(Token::DEBUGGER);
4834 return factory()->NewDebuggerStatement(pos);
4837 template <
typename Impl>
4838 typename ParserBase<Impl>::StatementT
4839 ParserBase<Impl>::ParseExpressionOrLabelledStatement(
4840 ZonePtrList<const AstRawString>* labels,
4841 ZonePtrList<const AstRawString>* own_labels,
4842 AllowLabelledFunctionStatement allow_function) {
4850 int pos = peek_position();
4853 case Token::FUNCTION:
4857 ReportUnexpectedToken(Next());
4858 return impl()->NullStatement();
4860 Token::Value next_next = PeekAhead();
4864 if (next_next != Token::LBRACK &&
4865 ((next_next != Token::LBRACE && next_next != Token::IDENTIFIER) ||
4866 scanner_->HasLineTerminatorAfterNext())) {
4869 impl()->ReportMessageAt(scanner()->peek_location(),
4870 MessageTemplate::kUnexpectedLexicalDeclaration);
4871 return impl()->NullStatement();
4877 bool starts_with_identifier = peek_any_identifier();
4878 ExpressionT expr = ParseExpression();
4879 if (peek() == Token::COLON && starts_with_identifier &&
4880 impl()->IsIdentifier(expr)) {
4883 impl()->DeclareLabel(&labels, &own_labels,
4884 impl()->AsIdentifierExpression(expr));
4885 Consume(Token::COLON);
4887 if (peek() == Token::FUNCTION && is_sloppy(language_mode()) &&
4888 allow_function == kAllowLabelledFunctionStatement) {
4889 return ParseFunctionDeclaration();
4891 return ParseStatement(labels, own_labels, allow_function);
4897 if (extension_ !=
nullptr && peek() == Token::FUNCTION &&
4898 !scanner()->HasLineTerminatorBeforeNext() && impl()->IsNative(expr) &&
4899 !scanner()->literal_contains_escapes()) {
4900 return ParseNativeDeclaration();
4905 if (expr->IsFailureExpression())
return impl()->NullStatement();
4906 return factory()->NewExpressionStatement(expr, pos);
4909 template <
typename Impl>
4910 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseIfStatement(
4911 ZonePtrList<const AstRawString>* labels) {
4915 int pos = peek_position();
4917 Expect(Token::LPAREN);
4918 ExpressionT condition = ParseExpression();
4919 Expect(Token::RPAREN);
4921 SourceRange then_range, else_range;
4922 StatementT then_statement = impl()->NullStatement();
4924 SourceRangeScope range_scope(scanner(), &then_range);
4925 then_statement = ParseScopedStatement(labels);
4928 StatementT else_statement = impl()->NullStatement();
4929 if (Check(Token::ELSE)) {
4930 else_statement = ParseScopedStatement(labels);
4931 else_range = SourceRange::ContinuationOf(then_range, end_position());
4933 else_statement = factory()->EmptyStatement();
4936 factory()->NewIfStatement(condition, then_statement, else_statement, pos);
4937 impl()->RecordIfStatementSourceRange(stmt, then_range, else_range);
4941 template <
typename Impl>
4942 typename ParserBase<Impl>::StatementT
4943 ParserBase<Impl>::ParseContinueStatement() {
4947 int pos = peek_position();
4948 Consume(Token::CONTINUE);
4949 IdentifierT label = impl()->NullIdentifier();
4950 Token::Value tok = peek();
4951 if (!scanner()->HasLineTerminatorBeforeNext() &&
4952 !Token::IsAutoSemicolon(tok)) {
4954 label = ParseIdentifier(kAllowRestrictedIdentifiers);
4956 IterationStatementT target = impl()->LookupContinueTarget(label);
4957 if (impl()->IsNull(target)) {
4959 MessageTemplate message = MessageTemplate::kIllegalContinue;
4960 BreakableStatementT breakable_target = impl()->LookupBreakTarget(label);
4961 if (impl()->IsNull(label)) {
4962 message = MessageTemplate::kNoIterationStatement;
4963 }
else if (impl()->IsNull(breakable_target)) {
4964 message = MessageTemplate::kUnknownLabel;
4966 ReportMessage(message, label);
4967 return impl()->NullStatement();
4970 StatementT stmt = factory()->NewContinueStatement(target, pos);
4971 impl()->RecordJumpStatementSourceRange(stmt, end_position());
4975 template <
typename Impl>
4976 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseBreakStatement(
4977 ZonePtrList<const AstRawString>* labels) {
4981 int pos = peek_position();
4982 Consume(Token::BREAK);
4983 IdentifierT label = impl()->NullIdentifier();
4984 Token::Value tok = peek();
4985 if (!scanner()->HasLineTerminatorBeforeNext() &&
4986 !Token::IsAutoSemicolon(tok)) {
4988 label = ParseIdentifier(kAllowRestrictedIdentifiers);
4992 if (!impl()->IsNull(label) && impl()->ContainsLabel(labels, label)) {
4994 return factory()->EmptyStatement();
4996 BreakableStatementT target = impl()->LookupBreakTarget(label);
4997 if (impl()->IsNull(target)) {
4999 MessageTemplate message = MessageTemplate::kIllegalBreak;
5000 if (!impl()->IsNull(label)) {
5001 message = MessageTemplate::kUnknownLabel;
5003 ReportMessage(message, label);
5004 return impl()->NullStatement();
5007 StatementT stmt = factory()->NewBreakStatement(target, pos);
5008 impl()->RecordJumpStatementSourceRange(stmt, end_position());
5012 template <
typename Impl>
5013 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseReturnStatement() {
5020 Consume(Token::RETURN);
5021 Scanner::Location loc = scanner()->location();
5023 switch (GetDeclarationScope()->scope_type()) {
5027 impl()->ReportMessageAt(loc, MessageTemplate::kIllegalReturn);
5028 return impl()->NullStatement();
5033 Token::Value tok = peek();
5034 ExpressionT return_value = impl()->NullExpression();
5035 if (scanner()->HasLineTerminatorBeforeNext() || Token::IsAutoSemicolon(tok)) {
5036 if (IsDerivedConstructor(function_state_->kind())) {
5037 return_value = impl()->ThisExpression(loc.beg_pos);
5040 return_value = ParseExpression();
5044 return_value = impl()->RewriteReturn(return_value, loc.beg_pos);
5045 int continuation_pos = end_position();
5047 BuildReturnStatement(return_value, loc.beg_pos, continuation_pos);
5048 impl()->RecordJumpStatementSourceRange(stmt, end_position());
5052 template <
typename Impl>
5053 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseWithStatement(
5054 ZonePtrList<const AstRawString>* labels) {
5058 Consume(Token::WITH);
5059 int pos = position();
5061 if (is_strict(language_mode())) {
5062 ReportMessage(MessageTemplate::kStrictWith);
5063 return impl()->NullStatement();
5066 Expect(Token::LPAREN);
5067 ExpressionT expr = ParseExpression();
5068 Expect(Token::RPAREN);
5070 Scope* with_scope = NewScope(WITH_SCOPE);
5071 StatementT body = impl()->NullStatement();
5073 BlockState block_state(&scope_, with_scope);
5074 with_scope->set_start_position(scanner()->peek_location().beg_pos);
5075 body = ParseStatement(labels,
nullptr);
5076 with_scope->set_end_position(end_position());
5078 return factory()->NewWithStatement(with_scope, expr, body, pos);
5081 template <
typename Impl>
5082 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseDoWhileStatement(
5083 ZonePtrList<const AstRawString>* labels,
5084 ZonePtrList<const AstRawString>* own_labels) {
5088 factory()->NewDoWhileStatement(labels, own_labels, peek_position());
5089 TargetT target(
this, loop);
5091 SourceRange body_range;
5092 StatementT body = impl()->NullStatement();
5096 CheckStackOverflow();
5098 SourceRangeScope range_scope(scanner(), &body_range);
5099 body = ParseStatement(
nullptr,
nullptr);
5101 Expect(Token::WHILE);
5102 Expect(Token::LPAREN);
5104 ExpressionT cond = ParseExpression();
5105 Expect(Token::RPAREN);
5111 Check(Token::SEMICOLON);
5113 loop->Initialize(cond, body);
5114 impl()->RecordIterationStatementSourceRange(loop, body_range);
5119 template <
typename Impl>
5120 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseWhileStatement(
5121 ZonePtrList<const AstRawString>* labels,
5122 ZonePtrList<const AstRawString>* own_labels) {
5126 auto loop = factory()->NewWhileStatement(labels, own_labels, peek_position());
5127 TargetT target(
this, loop);
5129 SourceRange body_range;
5130 StatementT body = impl()->NullStatement();
5132 Consume(Token::WHILE);
5133 Expect(Token::LPAREN);
5134 ExpressionT cond = ParseExpression();
5135 Expect(Token::RPAREN);
5137 SourceRangeScope range_scope(scanner(), &body_range);
5138 body = ParseStatement(
nullptr,
nullptr);
5141 loop->Initialize(cond, body);
5142 impl()->RecordIterationStatementSourceRange(loop, body_range);
5147 template <
typename Impl>
5148 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseThrowStatement() {
5152 Consume(Token::THROW);
5153 int pos = position();
5154 if (scanner()->HasLineTerminatorBeforeNext()) {
5155 ReportMessage(MessageTemplate::kNewlineAfterThrow);
5156 return impl()->NullStatement();
5158 ExpressionT exception = ParseExpression();
5161 StatementT stmt = impl()->NewThrowStatement(exception, pos);
5162 impl()->RecordThrowSourceRange(stmt, end_position());
5167 template <
typename Impl>
5168 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseSwitchStatement(
5169 ZonePtrList<const AstRawString>* labels) {
5176 int switch_pos = peek_position();
5178 Consume(Token::SWITCH);
5179 Expect(Token::LPAREN);
5180 ExpressionT tag = ParseExpression();
5181 Expect(Token::RPAREN);
5183 auto switch_statement =
5184 factory()->NewSwitchStatement(labels, tag, switch_pos);
5187 BlockState cases_block_state(zone(), &scope_);
5188 scope()->set_start_position(switch_pos);
5189 scope()->SetNonlinear();
5190 TargetT target(
this, switch_statement);
5192 bool default_seen =
false;
5193 Expect(Token::LBRACE);
5194 while (peek() != Token::RBRACE) {
5196 ExpressionT label = impl()->NullExpression();
5197 StatementListT statements(pointer_buffer());
5198 SourceRange clause_range;
5200 SourceRangeScope range_scope(scanner(), &clause_range);
5201 if (Check(Token::CASE)) {
5202 label = ParseExpression();
5204 Expect(Token::DEFAULT);
5206 ReportMessage(MessageTemplate::kMultipleDefaultsInSwitch);
5207 return impl()->NullStatement();
5209 default_seen =
true;
5211 Expect(Token::COLON);
5212 while (peek() != Token::CASE && peek() != Token::DEFAULT &&
5213 peek() != Token::RBRACE) {
5214 StatementT stat = ParseStatementListItem();
5215 if (impl()->IsNull(stat))
return stat;
5216 if (stat->IsEmptyStatement())
continue;
5217 statements.Add(stat);
5220 auto clause = factory()->NewCaseClause(label, statements);
5221 impl()->RecordCaseClauseSourceRange(clause, clause_range);
5222 switch_statement->cases()->Add(clause, zone());
5224 Expect(Token::RBRACE);
5226 int end_pos = end_position();
5227 scope()->set_end_position(end_pos);
5228 impl()->RecordSwitchStatementSourceRange(switch_statement, end_pos);
5229 Scope* switch_scope = scope()->FinalizeBlockScope();
5230 if (switch_scope !=
nullptr) {
5231 return impl()->RewriteSwitchStatement(switch_statement, switch_scope);
5233 return switch_statement;
5237 template <
typename Impl>
5238 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseTryStatement() {
5250 Consume(Token::TRY);
5251 int pos = position();
5253 BlockT try_block = ParseBlock(
nullptr);
5255 CatchInfo catch_info(
this);
5257 if (peek() != Token::CATCH && peek() != Token::FINALLY) {
5258 ReportMessage(MessageTemplate::kNoCatchOrFinally);
5259 return impl()->NullStatement();
5262 SourceRange catch_range, finally_range;
5264 BlockT catch_block = impl()->NullStatement();
5266 SourceRangeScope catch_range_scope(scanner(), &catch_range);
5267 if (Check(Token::CATCH)) {
5269 has_binding = Check(Token::LPAREN);
5272 catch_info.scope = NewScope(CATCH_SCOPE);
5273 catch_info.scope->set_start_position(scanner()->location().beg_pos);
5276 BlockState catch_block_state(&scope_, catch_info.scope);
5277 StatementListT catch_statements(pointer_buffer());
5282 BlockState catch_variable_block_state(zone(), &scope_);
5283 scope()->set_start_position(scanner()->location().beg_pos);
5289 if (peek_any_identifier()) {
5291 ParseIdentifier(kDontAllowRestrictedIdentifiers);
5293 ExpressionClassifier pattern_classifier(
this);
5294 catch_info.pattern = ParseBindingPattern();
5297 Expect(Token::RPAREN);
5298 RETURN_IF_PARSE_ERROR;
5299 impl()->RewriteCatchPattern(&catch_info);
5300 if (!impl()->IsNull(catch_info.init_block)) {
5301 catch_statements.Add(catch_info.init_block);
5304 catch_info.inner_block = ParseBlock(
nullptr);
5305 catch_statements.Add(catch_info.inner_block);
5306 RETURN_IF_PARSE_ERROR;
5307 impl()->ValidateCatchBlock(catch_info);
5308 scope()->set_end_position(end_position());
5309 catch_block = factory()->NewBlock(
false, catch_statements);
5310 catch_block->set_scope(scope()->FinalizeBlockScope());
5314 catch_info.scope->set_end_position(end_position());
5316 catch_block = ParseBlock(
nullptr);
5321 BlockT finally_block = impl()->NullStatement();
5322 DCHECK(has_error() || peek() == Token::FINALLY ||
5323 !impl()->IsNull(catch_block));
5325 SourceRangeScope range_scope(scanner(), &finally_range);
5326 if (Check(Token::FINALLY)) {
5327 finally_block = ParseBlock(
nullptr);
5331 RETURN_IF_PARSE_ERROR;
5332 return impl()->RewriteTryStatement(try_block, catch_block, catch_range,
5333 finally_block, finally_range, catch_info,
5337 template <
typename Impl>
5338 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseForStatement(
5339 ZonePtrList<const AstRawString>* labels,
5340 ZonePtrList<const AstRawString>* own_labels) {
5349 int stmt_pos = peek_position();
5350 ForInfo for_info(
this);
5352 Consume(Token::FOR);
5353 Expect(Token::LPAREN);
5355 if (peek() == Token::CONST || (peek() == Token::LET && IsNextLetKeyword())) {
5358 BlockState for_state(zone(), &scope_);
5359 scope()->set_start_position(scanner()->location().beg_pos);
5364 typename FunctionState::FunctionOrEvalRecordingScope recording_scope(
5369 Scope* inner_block_scope = NewScope(BLOCK_SCOPE);
5371 BlockState inner_state(&scope_, inner_block_scope);
5372 ParseVariableDeclarations(kForStatement, &for_info.parsing_result,
5375 DCHECK(IsLexicalVariableMode(for_info.parsing_result.descriptor.mode));
5376 for_info.position = scanner()->location().beg_pos;
5378 if (CheckInOrOf(&for_info.mode)) {
5379 scope()->set_is_hidden();
5380 return ParseForEachStatementWithDeclarations(
5381 stmt_pos, &for_info, labels, own_labels, inner_block_scope);
5384 Expect(Token::SEMICOLON);
5386 StatementT init = impl()->BuildInitializationBlock(&for_info.parsing_result,
5387 &for_info.bound_names);
5389 Scope* finalized = inner_block_scope->FinalizeBlockScope();
5391 DCHECK_NULL(finalized);
5393 return ParseStandardForLoopWithLexicalDeclarations(
5394 stmt_pos, init, &for_info, labels, own_labels);
5397 StatementT init = impl()->NullStatement();
5398 if (peek() == Token::VAR) {
5399 ParseVariableDeclarations(kForStatement, &for_info.parsing_result,
nullptr);
5400 DCHECK_EQ(for_info.parsing_result.descriptor.mode, VariableMode::kVar);
5401 for_info.position = scanner()->location().beg_pos;
5403 if (CheckInOrOf(&for_info.mode)) {
5404 return ParseForEachStatementWithDeclarations(stmt_pos, &for_info, labels,
5405 own_labels,
nullptr);
5408 init = impl()->BuildInitializationBlock(&for_info.parsing_result,
nullptr);
5409 }
else if (peek() != Token::SEMICOLON) {
5411 int lhs_beg_pos = peek_position();
5412 ExpressionClassifier classifier(
this);
5413 ExpressionT expression;
5415 AcceptINScope scope(
this,
false);
5416 expression = ParseExpressionCoverGrammar();
5418 int lhs_end_pos = end_position();
5420 bool is_for_each = CheckInOrOf(&for_info.mode);
5421 bool is_destructuring = is_for_each && expression->IsPattern();
5423 if (is_destructuring) {
5424 ValidatePattern(expression);
5426 ValidateExpression();
5430 return ParseForEachStatementWithoutDeclarations(
5431 stmt_pos, expression, lhs_beg_pos, lhs_end_pos, &for_info, labels,
5435 init = factory()->NewExpressionStatement(expression, lhs_beg_pos);
5438 Expect(Token::SEMICOLON);
5441 ExpressionT cond = impl()->NullExpression();
5442 StatementT next = impl()->NullStatement();
5443 StatementT body = impl()->NullStatement();
5444 ForStatementT loop =
5445 ParseStandardForLoop(stmt_pos, labels, own_labels, &cond, &next, &body);
5446 RETURN_IF_PARSE_ERROR;
5447 loop->Initialize(init, cond, next, body);
5451 template <
typename Impl>
5452 typename ParserBase<Impl>::StatementT
5453 ParserBase<Impl>::ParseForEachStatementWithDeclarations(
5454 int stmt_pos, ForInfo* for_info, ZonePtrList<const AstRawString>* labels,
5455 ZonePtrList<const AstRawString>* own_labels, Scope* inner_block_scope) {
5457 if (for_info->parsing_result.declarations.size() != 1) {
5458 impl()->ReportMessageAt(for_info->parsing_result.bindings_loc,
5459 MessageTemplate::kForInOfLoopMultiBindings,
5460 ForEachStatement::VisitModeString(for_info->mode));
5461 return impl()->NullStatement();
5463 if (for_info->parsing_result.first_initializer_loc.IsValid() &&
5464 (is_strict(language_mode()) ||
5465 for_info->mode == ForEachStatement::ITERATE ||
5466 IsLexicalVariableMode(for_info->parsing_result.descriptor.mode) ||
5467 !impl()->IsIdentifier(
5468 for_info->parsing_result.declarations[0].pattern))) {
5469 impl()->ReportMessageAt(for_info->parsing_result.first_initializer_loc,
5470 MessageTemplate::kForInOfLoopInitializer,
5471 ForEachStatement::VisitModeString(for_info->mode));
5472 return impl()->NullStatement();
5476 for_info->parsing_result.descriptor.declaration_kind =
5477 DeclarationDescriptor::FOR_EACH;
5479 BlockT init_block = impl()->RewriteForVarInLegacy(*for_info);
5481 auto loop = factory()->NewForEachStatement(for_info->mode, labels, own_labels,
5483 TargetT target(
this, loop);
5485 ExpressionT enumerable = impl()->NullExpression();
5486 if (for_info->mode == ForEachStatement::ITERATE) {
5487 ExpressionClassifier classifier(
this);
5488 AcceptINScope scope(
this,
true);
5489 enumerable = ParseAssignmentExpression();
5490 ValidateExpression();
5492 enumerable = ParseExpression();
5495 Expect(Token::RPAREN);
5497 Scope* for_scope =
nullptr;
5498 if (inner_block_scope !=
nullptr) {
5499 for_scope = inner_block_scope->outer_scope();
5500 DCHECK_EQ(for_scope, scope());
5501 inner_block_scope->set_start_position(scanner()->location().beg_pos);
5504 ExpressionT each_variable = impl()->NullExpression();
5505 BlockT body_block = impl()->NullStatement();
5507 BlockState block_state(
5508 &scope_, inner_block_scope !=
nullptr ? inner_block_scope : scope_);
5510 SourceRange body_range;
5511 StatementT body = impl()->NullStatement();
5513 SourceRangeScope range_scope(scanner(), &body_range);
5514 body = ParseStatement(
nullptr,
nullptr);
5516 impl()->RecordIterationStatementSourceRange(loop, body_range);
5518 impl()->DesugarBindingInForEachStatement(for_info, &body_block,
5520 body_block->statements()->Add(body, zone());
5522 if (inner_block_scope !=
nullptr) {
5523 inner_block_scope->set_end_position(end_position());
5524 body_block->set_scope(inner_block_scope->FinalizeBlockScope());
5528 StatementT final_loop = impl()->InitializeForEachStatement(
5529 loop, each_variable, enumerable, body_block);
5531 init_block = impl()->CreateForEachStatementTDZ(init_block, *for_info);
5533 if (for_scope !=
nullptr) {
5534 for_scope->set_end_position(end_position());
5535 for_scope = for_scope->FinalizeBlockScope();
5539 if (!impl()->IsNull(init_block)) {
5540 init_block->statements()->Add(final_loop, zone());
5541 init_block->set_scope(for_scope);
5545 DCHECK_NULL(for_scope);
5549 template <
typename Impl>
5550 typename ParserBase<Impl>::StatementT
5551 ParserBase<Impl>::ParseForEachStatementWithoutDeclarations(
5552 int stmt_pos, ExpressionT expression,
int lhs_beg_pos,
int lhs_end_pos,
5553 ForInfo* for_info, ZonePtrList<const AstRawString>* labels,
5554 ZonePtrList<const AstRawString>* own_labels) {
5556 if (expression->IsPattern()) {
5557 if (expression->is_parenthesized()) {
5558 impl()->ReportMessageAt(
5559 Scanner::Location(expression->position(), end_position()),
5560 MessageTemplate::kInvalidDestructuringTarget);
5562 }
else if (V8_UNLIKELY(!IsValidReferenceExpression(expression))) {
5563 expression = RewriteInvalidReferenceExpression(
5564 expression, lhs_beg_pos, lhs_end_pos, MessageTemplate::kInvalidLhsInFor,
5568 auto loop = factory()->NewForEachStatement(for_info->mode, labels, own_labels,
5570 TargetT target(
this, loop);
5572 ExpressionT enumerable = impl()->NullExpression();
5573 if (for_info->mode == ForEachStatement::ITERATE) {
5574 ExpressionClassifier classifier(
this);
5575 AcceptINScope scope(
this,
true);
5576 enumerable = ParseAssignmentExpression();
5577 ValidateExpression();
5579 enumerable = ParseExpression();
5582 Expect(Token::RPAREN);
5584 StatementT body = impl()->NullStatement();
5585 SourceRange body_range;
5587 SourceRangeScope range_scope(scanner(), &body_range);
5588 body = ParseStatement(
nullptr,
nullptr);
5590 impl()->RecordIterationStatementSourceRange(loop, body_range);
5591 RETURN_IF_PARSE_ERROR;
5592 return impl()->InitializeForEachStatement(loop, expression, enumerable, body);
5595 template <
typename Impl>
5596 typename ParserBase<Impl>::StatementT
5597 ParserBase<Impl>::ParseStandardForLoopWithLexicalDeclarations(
5598 int stmt_pos, StatementT init, ForInfo* for_info,
5599 ZonePtrList<const AstRawString>* labels,
5600 ZonePtrList<const AstRawString>* own_labels) {
5603 Scope* inner_scope = NewScope(BLOCK_SCOPE);
5604 ForStatementT loop = impl()->NullStatement();
5605 ExpressionT cond = impl()->NullExpression();
5606 StatementT next = impl()->NullStatement();
5607 StatementT body = impl()->NullStatement();
5609 BlockState block_state(&scope_, inner_scope);
5610 scope()->set_start_position(scanner()->location().beg_pos);
5612 ParseStandardForLoop(stmt_pos, labels, own_labels, &cond, &next, &body);
5613 RETURN_IF_PARSE_ERROR;
5614 scope()->set_end_position(end_position());
5617 scope()->set_end_position(end_position());
5618 if (for_info->bound_names.length() > 0 &&
5619 function_state_->contains_function_or_eval()) {
5620 scope()->set_is_hidden();
5621 return impl()->DesugarLexicalBindingsInForStatement(
5622 loop, init, cond, next, body, inner_scope, *for_info);
5624 inner_scope = inner_scope->FinalizeBlockScope();
5625 DCHECK_NULL(inner_scope);
5629 Scope* for_scope = scope()->FinalizeBlockScope();
5630 if (for_scope !=
nullptr) {
5641 DCHECK(!impl()->IsNull(init));
5642 BlockT block = factory()->NewBlock(2,
false);
5643 block->statements()->Add(init, zone());
5644 block->statements()->Add(loop, zone());
5645 block->set_scope(for_scope);
5646 loop->Initialize(impl()->NullStatement(), cond, next, body);
5650 loop->Initialize(init, cond, next, body);
5654 template <
typename Impl>
5655 typename ParserBase<Impl>::ForStatementT ParserBase<Impl>::ParseStandardForLoop(
5656 int stmt_pos, ZonePtrList<const AstRawString>* labels,
5657 ZonePtrList<const AstRawString>* own_labels, ExpressionT* cond,
5658 StatementT* next, StatementT* body) {
5659 ForStatementT loop = factory()->NewForStatement(labels, own_labels, stmt_pos);
5660 TargetT target(
this, loop);
5662 if (peek() != Token::SEMICOLON) {
5663 *cond = ParseExpression();
5665 Expect(Token::SEMICOLON);
5667 if (peek() != Token::RPAREN) {
5668 ExpressionT exp = ParseExpression();
5669 *next = factory()->NewExpressionStatement(exp, exp->position());
5671 Expect(Token::RPAREN);
5673 SourceRange body_range;
5675 SourceRangeScope range_scope(scanner(), &body_range);
5676 *body = ParseStatement(
nullptr,
nullptr);
5678 impl()->RecordIterationStatementSourceRange(loop, body_range);
5683 template <
typename Impl>
5684 void ParserBase<Impl>::MarkLoopVariableAsAssigned(
5685 Scope* scope, Variable* var,
5686 typename DeclarationDescriptor::Kind declaration_kind) {
5687 if (!IsLexicalVariableMode(var->mode()) &&
5688 (!scope->is_function_scope() ||
5689 declaration_kind == DeclarationDescriptor::FOR_EACH)) {
5690 var->set_maybe_assigned();
5694 template <
typename Impl>
5695 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseForAwaitStatement(
5696 ZonePtrList<const AstRawString>* labels,
5697 ZonePtrList<const AstRawString>* own_labels) {
5699 DCHECK(is_async_function());
5701 int stmt_pos = peek_position();
5703 ForInfo for_info(
this);
5704 for_info.mode = ForEachStatement::ITERATE;
5707 BlockState for_state(zone(), &scope_);
5709 Expect(Token::AWAIT);
5710 Expect(Token::LPAREN);
5711 scope()->set_start_position(scanner()->location().beg_pos);
5712 scope()->set_is_hidden();
5714 auto loop = factory()->NewForOfStatement(labels, own_labels, stmt_pos);
5715 TargetT target(
this, loop);
5717 ExpressionT each_variable = impl()->NullExpression();
5719 bool has_declarations =
false;
5720 Scope* inner_block_scope = NewScope(BLOCK_SCOPE);
5722 if (peek() == Token::VAR || peek() == Token::CONST ||
5723 (peek() == Token::LET && IsNextLetKeyword())) {
5729 has_declarations =
true;
5732 BlockState inner_state(&scope_, inner_block_scope);
5733 ParseVariableDeclarations(kForStatement, &for_info.parsing_result,
5736 for_info.position = scanner()->location().beg_pos;
5739 if (for_info.parsing_result.declarations.size() != 1) {
5740 impl()->ReportMessageAt(for_info.parsing_result.bindings_loc,
5741 MessageTemplate::kForInOfLoopMultiBindings,
5743 return impl()->NullStatement();
5747 if (for_info.parsing_result.first_initializer_loc.IsValid()) {
5748 impl()->ReportMessageAt(for_info.parsing_result.first_initializer_loc,
5749 MessageTemplate::kForInOfLoopInitializer,
5751 return impl()->NullStatement();
5757 int lhs_beg_pos = peek_position();
5758 BlockState inner_state(&scope_, inner_block_scope);
5759 ExpressionClassifier classifier(
this);
5760 ExpressionT lhs = each_variable = ParseLeftHandSideExpression();
5761 int lhs_end_pos = end_position();
5763 if (lhs->IsPattern()) {
5764 ValidatePattern(lhs);
5766 ValidateExpression();
5767 if (V8_UNLIKELY(!IsValidReferenceExpression(lhs))) {
5768 each_variable = RewriteInvalidReferenceExpression(
5769 lhs, lhs_beg_pos, lhs_end_pos, MessageTemplate::kInvalidLhsInFor,
5775 ExpectContextualKeyword(ast_value_factory()->of_string());
5776 int each_keyword_pos = scanner()->location().beg_pos;
5778 const bool kAllowIn =
true;
5779 ExpressionT iterable = impl()->NullExpression();
5782 ExpressionClassifier classifier(
this);
5783 AcceptINScope scope(
this, kAllowIn);
5784 iterable = ParseAssignmentExpression();
5785 ValidateExpression();
5788 Expect(Token::RPAREN);
5790 StatementT body = impl()->NullStatement();
5792 BlockState block_state(&scope_, inner_block_scope);
5793 scope()->set_start_position(scanner()->location().beg_pos);
5795 SourceRange body_range;
5797 SourceRangeScope range_scope(scanner(), &body_range);
5798 body = ParseStatement(
nullptr,
nullptr);
5799 scope()->set_end_position(end_position());
5801 impl()->RecordIterationStatementSourceRange(loop, body_range);
5803 if (has_declarations) {
5804 BlockT body_block = impl()->NullStatement();
5805 impl()->DesugarBindingInForEachStatement(&for_info, &body_block,
5807 body_block->statements()->Add(body, zone());
5808 body_block->set_scope(scope()->FinalizeBlockScope());
5811 Scope* block_scope = scope()->FinalizeBlockScope();
5812 DCHECK_NULL(block_scope);
5816 const bool finalize =
true;
5817 StatementT final_loop = impl()->InitializeForOfStatement(
5818 loop, each_variable, iterable, body, finalize, IteratorType::kAsync,
5821 if (!has_declarations) {
5822 Scope* for_scope = scope()->FinalizeBlockScope();
5823 DCHECK_NULL(for_scope);
5829 impl()->CreateForEachStatementTDZ(impl()->NullStatement(), for_info);
5831 scope()->set_end_position(end_position());
5832 Scope* for_scope = scope()->FinalizeBlockScope();
5834 if (!impl()->IsNull(init_block)) {
5835 init_block->statements()->Add(final_loop, zone());
5836 init_block->set_scope(for_scope);
5839 DCHECK_NULL(for_scope);
5843 template <
typename Impl>
5844 void ParserBase<Impl>::CheckClassMethodName(IdentifierT name,
5845 ParsePropertyKind type,
5846 ParseFunctionFlags flags,
5848 bool* has_seen_constructor) {
5849 DCHECK(type == ParsePropertyKind::kMethod || IsAccessor(type));
5851 AstValueFactory* avf = ast_value_factory();
5854 if (impl()->IdentifierEquals(name, avf->prototype_string())) {
5855 ReportMessage(MessageTemplate::kStaticPrototype);
5858 }
else if (impl()->IdentifierEquals(name,
5859 avf->private_constructor_string())) {
5860 ReportMessage(MessageTemplate::kConstructorIsPrivate);
5862 }
else if (impl()->IdentifierEquals(name, avf->constructor_string())) {
5863 if (flags != ParseFunctionFlag::kIsNormal || IsAccessor(type)) {
5864 MessageTemplate msg = (flags & ParseFunctionFlag::kIsGenerator) != 0
5865 ? MessageTemplate::kConstructorIsGenerator
5866 : (flags & ParseFunctionFlag::kIsAsync) != 0
5867 ? MessageTemplate::kConstructorIsAsync
5868 : MessageTemplate::kConstructorIsAccessor;
5872 if (*has_seen_constructor) {
5873 ReportMessage(MessageTemplate::kDuplicateConstructor);
5876 *has_seen_constructor =
true;
5881 template <
typename Impl>
5882 void ParserBase<Impl>::CheckClassFieldName(IdentifierT name,
bool is_static) {
5883 AstValueFactory* avf = ast_value_factory();
5884 if (is_static && impl()->IdentifierEquals(name, avf->prototype_string())) {
5885 ReportMessage(MessageTemplate::kStaticPrototype);
5889 if (impl()->IdentifierEquals(name, avf->constructor_string()) ||
5890 impl()->IdentifierEquals(name, avf->private_constructor_string())) {
5891 ReportMessage(MessageTemplate::kConstructorClassField);
5896 #undef RETURN_IF_PARSE_ERROR 5901 #endif // V8_PARSING_PARSER_BASE_H_