Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: src/preparser.h

Issue 1127063003: [es6] implement default parameters via desugaring (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Don't AllocateParameter() if hasParameterExpressions is true Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_PREPARSER_H 5 #ifndef V8_PREPARSER_H
6 #define V8_PREPARSER_H 6 #define V8_PREPARSER_H
7 7
8 #include "src/v8.h" 8 #include "src/v8.h"
9 9
10 #include "src/bailout-reason.h" 10 #include "src/bailout-reason.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 // typedef Factory; 59 // typedef Factory;
60 // }; 60 // };
61 // // ... 61 // // ...
62 // }; 62 // };
63 63
64 template <typename Traits> 64 template <typename Traits>
65 class ParserBase : public Traits { 65 class ParserBase : public Traits {
66 public: 66 public:
67 // Shorten type names defined by Traits. 67 // Shorten type names defined by Traits.
68 typedef typename Traits::Type::Expression ExpressionT; 68 typedef typename Traits::Type::Expression ExpressionT;
69 typedef typename Traits::Type::ExpressionList ExpressionListT;
69 typedef typename Traits::Type::Identifier IdentifierT; 70 typedef typename Traits::Type::Identifier IdentifierT;
70 typedef typename Traits::Type::FormalParameter FormalParameterT; 71 typedef typename Traits::Type::FormalParameter FormalParameterT;
71 typedef typename Traits::Type::FormalParameterScope FormalParameterScopeT; 72 typedef typename Traits::Type::FormalParameterScope FormalParameterScopeT;
72 typedef typename Traits::Type::FunctionLiteral FunctionLiteralT; 73 typedef typename Traits::Type::FunctionLiteral FunctionLiteralT;
73 typedef typename Traits::Type::Literal LiteralT; 74 typedef typename Traits::Type::Literal LiteralT;
74 typedef typename Traits::Type::ObjectLiteralProperty ObjectLiteralPropertyT; 75 typedef typename Traits::Type::ObjectLiteralProperty ObjectLiteralPropertyT;
75 76
76 ParserBase(Zone* zone, Scanner* scanner, uintptr_t stack_limit, 77 ParserBase(Zone* zone, Scanner* scanner, uintptr_t stack_limit,
77 v8::Extension* extension, AstValueFactory* ast_value_factory, 78 v8::Extension* extension, AstValueFactory* ast_value_factory,
78 ParserRecorder* log, typename Traits::Type::Parser this_object) 79 ParserRecorder* log, typename Traits::Type::Parser this_object)
(...skipping 11 matching lines...) Expand all
90 scanner_(scanner), 91 scanner_(scanner),
91 stack_overflow_(false), 92 stack_overflow_(false),
92 allow_lazy_(false), 93 allow_lazy_(false),
93 allow_natives_(false), 94 allow_natives_(false),
94 allow_harmony_arrow_functions_(false), 95 allow_harmony_arrow_functions_(false),
95 allow_harmony_object_literals_(false), 96 allow_harmony_object_literals_(false),
96 allow_harmony_sloppy_(false), 97 allow_harmony_sloppy_(false),
97 allow_harmony_computed_property_names_(false), 98 allow_harmony_computed_property_names_(false),
98 allow_harmony_rest_params_(false), 99 allow_harmony_rest_params_(false),
99 allow_harmony_spreadcalls_(false), 100 allow_harmony_spreadcalls_(false),
101 allow_harmony_default_parameters_(false),
100 allow_strong_mode_(false) {} 102 allow_strong_mode_(false) {}
101 103
102 // Getters that indicate whether certain syntactical constructs are 104 // Getters that indicate whether certain syntactical constructs are
103 // allowed to be parsed by this instance of the parser. 105 // allowed to be parsed by this instance of the parser.
104 bool allow_lazy() const { return allow_lazy_; } 106 bool allow_lazy() const { return allow_lazy_; }
105 bool allow_natives() const { return allow_natives_; } 107 bool allow_natives() const { return allow_natives_; }
106 bool allow_harmony_arrow_functions() const { 108 bool allow_harmony_arrow_functions() const {
107 return allow_harmony_arrow_functions_; 109 return allow_harmony_arrow_functions_;
108 } 110 }
109 bool allow_harmony_modules() const { return scanner()->HarmonyModules(); } 111 bool allow_harmony_modules() const { return scanner()->HarmonyModules(); }
110 bool allow_harmony_classes() const { return scanner()->HarmonyClasses(); } 112 bool allow_harmony_classes() const { return scanner()->HarmonyClasses(); }
111 bool allow_harmony_object_literals() const { 113 bool allow_harmony_object_literals() const {
112 return allow_harmony_object_literals_; 114 return allow_harmony_object_literals_;
113 } 115 }
114 bool allow_harmony_sloppy() const { return allow_harmony_sloppy_; } 116 bool allow_harmony_sloppy() const { return allow_harmony_sloppy_; }
115 bool allow_harmony_unicode() const { return scanner()->HarmonyUnicode(); } 117 bool allow_harmony_unicode() const { return scanner()->HarmonyUnicode(); }
116 bool allow_harmony_computed_property_names() const { 118 bool allow_harmony_computed_property_names() const {
117 return allow_harmony_computed_property_names_; 119 return allow_harmony_computed_property_names_;
118 } 120 }
119 bool allow_harmony_rest_params() const { 121 bool allow_harmony_rest_params() const {
120 return allow_harmony_rest_params_; 122 return allow_harmony_rest_params_;
121 } 123 }
122 bool allow_harmony_spreadcalls() const { return allow_harmony_spreadcalls_; } 124 bool allow_harmony_spreadcalls() const { return allow_harmony_spreadcalls_; }
123 bool allow_harmony_destructuring() const { 125 bool allow_harmony_destructuring() const {
124 return allow_harmony_destructuring_; 126 return allow_harmony_destructuring_;
125 } 127 }
126 bool allow_harmony_spread_arrays() const { 128 bool allow_harmony_spread_arrays() const {
127 return allow_harmony_spread_arrays_; 129 return allow_harmony_spread_arrays_;
128 } 130 }
131 bool allow_harmony_default_parameters() const {
132 return allow_harmony_default_parameters_;
133 }
129 134
130 bool allow_strong_mode() const { return allow_strong_mode_; } 135 bool allow_strong_mode() const { return allow_strong_mode_; }
131 136
132 // Setters that determine whether certain syntactical constructs are 137 // Setters that determine whether certain syntactical constructs are
133 // allowed to be parsed by this instance of the parser. 138 // allowed to be parsed by this instance of the parser.
134 void set_allow_lazy(bool allow) { allow_lazy_ = allow; } 139 void set_allow_lazy(bool allow) { allow_lazy_ = allow; }
135 void set_allow_natives(bool allow) { allow_natives_ = allow; } 140 void set_allow_natives(bool allow) { allow_natives_ = allow; }
136 void set_allow_harmony_arrow_functions(bool allow) { 141 void set_allow_harmony_arrow_functions(bool allow) {
137 allow_harmony_arrow_functions_ = allow; 142 allow_harmony_arrow_functions_ = allow;
138 } 143 }
(...skipping 21 matching lines...) Expand all
160 void set_allow_harmony_spreadcalls(bool allow) { 165 void set_allow_harmony_spreadcalls(bool allow) {
161 allow_harmony_spreadcalls_ = allow; 166 allow_harmony_spreadcalls_ = allow;
162 } 167 }
163 void set_allow_strong_mode(bool allow) { allow_strong_mode_ = allow; } 168 void set_allow_strong_mode(bool allow) { allow_strong_mode_ = allow; }
164 void set_allow_harmony_destructuring(bool allow) { 169 void set_allow_harmony_destructuring(bool allow) {
165 allow_harmony_destructuring_ = allow; 170 allow_harmony_destructuring_ = allow;
166 } 171 }
167 void set_allow_harmony_spread_arrays(bool allow) { 172 void set_allow_harmony_spread_arrays(bool allow) {
168 allow_harmony_spread_arrays_ = allow; 173 allow_harmony_spread_arrays_ = allow;
169 } 174 }
175 void set_allow_harmony_default_parameters(bool allow) {
176 allow_harmony_default_parameters_ = allow;
177 }
178
170 179
171 protected: 180 protected:
172 enum AllowRestrictedIdentifiers { 181 enum AllowRestrictedIdentifiers {
173 kAllowRestrictedIdentifiers, 182 kAllowRestrictedIdentifiers,
174 kDontAllowRestrictedIdentifiers 183 kDontAllowRestrictedIdentifiers
175 }; 184 };
176 185
177 enum Mode { 186 enum Mode {
178 PARSE_LAZILY, 187 PARSE_LAZILY,
179 PARSE_EAGERLY 188 PARSE_EAGERLY
(...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 void AddTemplateExpression(ExpressionT); 916 void AddTemplateExpression(ExpressionT);
908 ExpressionT ParseSuperExpression(bool is_new, 917 ExpressionT ParseSuperExpression(bool is_new,
909 ExpressionClassifier* classifier, bool* ok); 918 ExpressionClassifier* classifier, bool* ok);
910 ExpressionT ParseStrongInitializationExpression( 919 ExpressionT ParseStrongInitializationExpression(
911 ExpressionClassifier* classifier, bool* ok); 920 ExpressionClassifier* classifier, bool* ok);
912 ExpressionT ParseStrongSuperCallExpression(ExpressionClassifier* classifier, 921 ExpressionT ParseStrongSuperCallExpression(ExpressionClassifier* classifier,
913 bool* ok); 922 bool* ok);
914 923
915 void ParseFormalParameter(FormalParameterScopeT* scope, bool is_rest, 924 void ParseFormalParameter(FormalParameterScopeT* scope, bool is_rest,
916 ExpressionClassifier* classifier, bool* ok); 925 ExpressionClassifier* classifier, bool* ok);
917 int ParseFormalParameterList(FormalParameterScopeT* scope, bool* has_rest, 926 int ParseFormalParameterList(FormalParameterScopeT* scope,
927 ExpressionListT initializers,
928 bool* has_parameter_expressions, bool* has_rest,
918 ExpressionClassifier* classifier, bool* ok); 929 ExpressionClassifier* classifier, bool* ok);
919 void CheckArityRestrictions( 930 void CheckArityRestrictions(
920 int param_count, FunctionLiteral::ArityRestriction arity_restriction, 931 int param_count, FunctionLiteral::ArityRestriction arity_restriction,
921 bool has_rest, int formals_start_pos, int formals_end_pos, bool* ok); 932 bool has_rest, int formals_start_pos, int formals_end_pos, bool* ok);
922 933
923 // Checks if the expression is a valid reference expression (e.g., on the 934 // Checks if the expression is a valid reference expression (e.g., on the
924 // left-hand side of assignments). Although ruled out by ECMA as early errors, 935 // left-hand side of assignments). Although ruled out by ECMA as early errors,
925 // we allow calls for web compatibility and rewrite them to a runtime throw. 936 // we allow calls for web compatibility and rewrite them to a runtime throw.
926 ExpressionT CheckAndRewriteReferenceExpression( 937 ExpressionT CheckAndRewriteReferenceExpression(
927 ExpressionT expression, Scanner::Location location, 938 ExpressionT expression, Scanner::Location location,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 bool allow_lazy_; 1021 bool allow_lazy_;
1011 bool allow_natives_; 1022 bool allow_natives_;
1012 bool allow_harmony_arrow_functions_; 1023 bool allow_harmony_arrow_functions_;
1013 bool allow_harmony_object_literals_; 1024 bool allow_harmony_object_literals_;
1014 bool allow_harmony_sloppy_; 1025 bool allow_harmony_sloppy_;
1015 bool allow_harmony_computed_property_names_; 1026 bool allow_harmony_computed_property_names_;
1016 bool allow_harmony_rest_params_; 1027 bool allow_harmony_rest_params_;
1017 bool allow_harmony_spreadcalls_; 1028 bool allow_harmony_spreadcalls_;
1018 bool allow_harmony_destructuring_; 1029 bool allow_harmony_destructuring_;
1019 bool allow_harmony_spread_arrays_; 1030 bool allow_harmony_spread_arrays_;
1031 bool allow_harmony_default_parameters_;
1020 bool allow_strong_mode_; 1032 bool allow_strong_mode_;
1021 }; 1033 };
1022 1034
1023 1035
1024 class PreParserIdentifier { 1036 class PreParserIdentifier {
1025 public: 1037 public:
1026 PreParserIdentifier() : type_(kUnknownIdentifier) {} 1038 PreParserIdentifier() : type_(kUnknownIdentifier) {}
1027 static PreParserIdentifier Default() { 1039 static PreParserIdentifier Default() {
1028 return PreParserIdentifier(kUnknownIdentifier); 1040 return PreParserIdentifier(kUnknownIdentifier);
1029 } 1041 }
(...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after
1786 } 1798 }
1787 inline void MaterializeTemplateCallsiteLiterals(); 1799 inline void MaterializeTemplateCallsiteLiterals();
1788 PreParserExpression NoTemplateTag() { 1800 PreParserExpression NoTemplateTag() {
1789 return PreParserExpression::NoTemplateTag(); 1801 return PreParserExpression::NoTemplateTag();
1790 } 1802 }
1791 static bool IsTaggedTemplate(const PreParserExpression tag) { 1803 static bool IsTaggedTemplate(const PreParserExpression tag) {
1792 return !tag.IsNoTemplateTag(); 1804 return !tag.IsNoTemplateTag();
1793 } 1805 }
1794 1806
1795 V8_INLINE bool DeclareFormalParameter(DuplicateFinder* scope, 1807 V8_INLINE bool DeclareFormalParameter(DuplicateFinder* scope,
1796 PreParserIdentifier param, 1808 PreParserIdentifier param, bool is_rest,
1797 bool is_rest); 1809 int pos);
1798 1810
1799 void CheckConflictingVarDeclarations(Scope* scope, bool* ok) {} 1811 void CheckConflictingVarDeclarations(Scope* scope, bool* ok) {}
1800 1812
1801 // Temporary glue; these functions will move to ParserBase. 1813 // Temporary glue; these functions will move to ParserBase.
1802 PreParserExpression ParseV8Intrinsic(bool* ok); 1814 PreParserExpression ParseV8Intrinsic(bool* ok);
1803 PreParserExpression ParseFunctionLiteral( 1815 PreParserExpression ParseFunctionLiteral(
1804 PreParserIdentifier name, Scanner::Location function_name_location, 1816 PreParserIdentifier name, Scanner::Location function_name_location,
1805 bool name_is_strict_reserved, FunctionKind kind, 1817 bool name_is_strict_reserved, FunctionKind kind,
1806 int function_token_position, FunctionLiteral::FunctionType type, 1818 int function_token_position, FunctionLiteral::FunctionType type,
1807 FunctionLiteral::ArityRestriction arity_restriction, bool* ok); 1819 FunctionLiteral::ArityRestriction arity_restriction, bool* ok);
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
1983 1995
1984 PreParserExpression PreParserTraits::SpreadCallNew(PreParserExpression function, 1996 PreParserExpression PreParserTraits::SpreadCallNew(PreParserExpression function,
1985 PreParserExpressionList args, 1997 PreParserExpressionList args,
1986 int pos) { 1998 int pos) {
1987 return pre_parser_->factory()->NewCallNew(function, args, pos); 1999 return pre_parser_->factory()->NewCallNew(function, args, pos);
1988 } 2000 }
1989 2001
1990 2002
1991 bool PreParserTraits::DeclareFormalParameter( 2003 bool PreParserTraits::DeclareFormalParameter(
1992 DuplicateFinder* duplicate_finder, PreParserIdentifier current_identifier, 2004 DuplicateFinder* duplicate_finder, PreParserIdentifier current_identifier,
1993 bool is_rest) { 2005 bool is_rest, int pos) {
1994 return pre_parser_->scanner()->FindSymbol(duplicate_finder, 1) != 0; 2006 return pre_parser_->scanner()->FindSymbol(duplicate_finder, 1) != 0;
1995 } 2007 }
1996 2008
1997 2009
1998 void PreParserTraits::ParseArrowFunctionFormalParameters( 2010 void PreParserTraits::ParseArrowFunctionFormalParameters(
1999 Scope* scope, PreParserExpression params, 2011 Scope* scope, PreParserExpression params,
2000 const Scanner::Location& params_loc, bool* is_rest, 2012 const Scanner::Location& params_loc, bool* is_rest,
2001 Scanner::Location* duplicate_loc, bool* ok) { 2013 Scanner::Location* duplicate_loc, bool* ok) {
2002 // TODO(wingo): Detect duplicated identifiers in paramlists. Detect parameter 2014 // TODO(wingo): Detect duplicated identifiers in paramlists. Detect parameter
2003 // lists that are too long. 2015 // lists that are too long.
(...skipping 1650 matching lines...) Expand 10 before | Expand all | Expand 10 after
3654 } 3666 }
3655 3667
3656 3668
3657 template <class Traits> 3669 template <class Traits>
3658 void ParserBase<Traits>::ParseFormalParameter(FormalParameterScopeT* scope, 3670 void ParserBase<Traits>::ParseFormalParameter(FormalParameterScopeT* scope,
3659 bool is_rest, 3671 bool is_rest,
3660 ExpressionClassifier* classifier, 3672 ExpressionClassifier* classifier,
3661 bool* ok) { 3673 bool* ok) {
3662 // FormalParameter[Yield,GeneratorParameter] : 3674 // FormalParameter[Yield,GeneratorParameter] :
3663 // BindingElement[?Yield, ?GeneratorParameter] 3675 // BindingElement[?Yield, ?GeneratorParameter]
3676 int pos = peek_position();
3664 IdentifierT name = ParseAndClassifyIdentifier(classifier, ok); 3677 IdentifierT name = ParseAndClassifyIdentifier(classifier, ok);
3665 if (!*ok) return; 3678 if (!*ok) return;
3666 3679
3667 bool was_declared = Traits::DeclareFormalParameter(scope, name, is_rest); 3680 bool was_declared = Traits::DeclareFormalParameter(scope, name, is_rest, pos);
3668 if (was_declared) { 3681 if (was_declared) {
3669 classifier->RecordDuplicateFormalParameterError(scanner()->location()); 3682 classifier->RecordDuplicateFormalParameterError(scanner()->location());
3670 } 3683 }
3671 } 3684 }
3672 3685
3673 3686
3674 template <class Traits> 3687 template <class Traits>
3675 int ParserBase<Traits>::ParseFormalParameterList( 3688 int ParserBase<Traits>::ParseFormalParameterList(
3676 FormalParameterScopeT* scope, bool* is_rest, 3689 FormalParameterScopeT* scope, ExpressionListT initializers,
3690 bool* has_parameter_expressions, bool* is_rest,
3677 ExpressionClassifier* classifier, bool* ok) { 3691 ExpressionClassifier* classifier, bool* ok) {
3678 // FormalParameters[Yield,GeneratorParameter] : 3692 // FormalParameters[Yield,GeneratorParameter] :
3679 // [empty] 3693 // [empty]
3680 // FormalParameterList[?Yield, ?GeneratorParameter] 3694 // FormalParameterList[?Yield, ?GeneratorParameter]
3681 // 3695 //
3682 // FormalParameterList[Yield,GeneratorParameter] : 3696 // FormalParameterList[Yield,GeneratorParameter] :
3683 // FunctionRestParameter[?Yield] 3697 // FunctionRestParameter[?Yield]
3684 // FormalsList[?Yield, ?GeneratorParameter] 3698 // FormalsList[?Yield, ?GeneratorParameter]
3685 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield] 3699 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield]
3686 // 3700 //
3687 // FormalsList[Yield,GeneratorParameter] : 3701 // FormalsList[Yield,GeneratorParameter] :
3688 // FormalParameter[?Yield, ?GeneratorParameter] 3702 // FormalParameter[?Yield, ?GeneratorParameter]
3689 // FormalsList[?Yield, ?GeneratorParameter] , 3703 // FormalsList[?Yield, ?GeneratorParameter] ,
3690 // FormalParameter[?Yield,?GeneratorParameter] 3704 // FormalParameter[?Yield,?GeneratorParameter]
3691 3705
3692 int parameter_count = 0; 3706 int parameter_count = 0;
3693 3707
3694 if (peek() != Token::RPAREN) { 3708 if (peek() != Token::RPAREN) {
3695 do { 3709 do {
3710 Scope* param_scope = NewScope(scope_, BLOCK_SCOPE);
3711 BlockState param_state(&scope_, param_scope);
3712 param_scope->set_start_position(peek_position());
3696 if (++parameter_count > Code::kMaxArguments) { 3713 if (++parameter_count > Code::kMaxArguments) {
3697 ReportMessage(MessageTemplate::kTooManyParameters); 3714 ReportMessage(MessageTemplate::kTooManyParameters);
3698 *ok = false; 3715 *ok = false;
3699 return -1; 3716 return -1;
3700 } 3717 }
3718
3719 int start_pos = peek_position();
3701 *is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS); 3720 *is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS);
3702 ParseFormalParameter(scope, *is_rest, classifier, ok); 3721 ParseFormalParameter(scope, *is_rest, classifier, ok);
3703 if (!*ok) return -1; 3722 if (!*ok) return -1;
3723
3724 // TODO(caitp, dslomov): set *has_parameter_expressions to true if
3725 // formal parameter is an ObjectBindingPattern containing computed
3726 // property keys
3727
3728 ExpressionT initializer = this->EmptyExpression();
3729 if (allow_harmony_default_parameters() && Check(Token::ASSIGN)) {
3730 // Default parameter initializer
3731 static const bool accept_IN = true;
3732 ExpressionClassifier classifier;
3733 initializer = ParseAssignmentExpression(accept_IN, &classifier, ok);
3734 if (!*ok) return -1;
3735 *has_parameter_expressions = true;
3736
3737 // A rest parameter cannot be initialized.
3738 if (*is_rest) {
3739 Scanner::Location loc(start_pos, scanner()->location().end_pos);
3740 ReportMessageAt(loc, MessageTemplate::kBadRestParameterInitializer);
3741 *ok = false;
3742 return -1;
3743 }
3744 }
3745 param_scope->set_end_position(scanner()->location().end_pos);
3746 initializers->Add(initializer, zone());
3704 } while (!*is_rest && Check(Token::COMMA)); 3747 } while (!*is_rest && Check(Token::COMMA));
3705 3748
3706 if (*is_rest && peek() == Token::COMMA) { 3749 if (*is_rest && peek() == Token::COMMA) {
3707 ReportMessageAt(scanner()->peek_location(), 3750 ReportMessageAt(scanner()->peek_location(),
3708 MessageTemplate::kParamAfterRest); 3751 MessageTemplate::kParamAfterRest);
3709 *ok = false; 3752 *ok = false;
3710 return -1; 3753 return -1;
3711 } 3754 }
3712 } 3755 }
3713 3756
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
4027 *ok = false; 4070 *ok = false;
4028 return; 4071 return;
4029 } 4072 }
4030 has_seen_constructor_ = true; 4073 has_seen_constructor_ = true;
4031 return; 4074 return;
4032 } 4075 }
4033 } 4076 }
4034 } } // v8::internal 4077 } } // v8::internal
4035 4078
4036 #endif // V8_PREPARSER_H 4079 #endif // V8_PREPARSER_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698