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

Side by Side Diff: src/parser.cc

Issue 1104223002: [es6] implement optional parameters via desugaring (with scoping) (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Make scoping sort-of work-ish, still no hole-checking Created 5 years, 7 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 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/bailout-reason.h" 9 #include "src/bailout-reason.h"
10 #include "src/base/platform/platform.h" 10 #include "src/base/platform/platform.h"
(...skipping 1115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1126 ? FunctionLiteral::ANONYMOUS_EXPRESSION 1126 ? FunctionLiteral::ANONYMOUS_EXPRESSION
1127 : FunctionLiteral::NAMED_EXPRESSION) 1127 : FunctionLiteral::NAMED_EXPRESSION)
1128 : FunctionLiteral::DECLARATION; 1128 : FunctionLiteral::DECLARATION;
1129 bool ok = true; 1129 bool ok = true;
1130 1130
1131 if (shared_info->is_arrow()) { 1131 if (shared_info->is_arrow()) {
1132 Scope* scope = NewScope(scope_, ARROW_SCOPE); 1132 Scope* scope = NewScope(scope_, ARROW_SCOPE);
1133 scope->set_start_position(shared_info->start_position()); 1133 scope->set_start_position(shared_info->start_position());
1134 FormalParameterErrorLocations error_locs; 1134 FormalParameterErrorLocations error_locs;
1135 bool has_rest = false; 1135 bool has_rest = false;
1136 bool has_initializers = false;
1137 ZoneList<Expression*>* initializers =
1138 new (zone()) ZoneList<Expression*>(0, zone());
1136 if (Check(Token::LPAREN)) { 1139 if (Check(Token::LPAREN)) {
1137 // '(' StrictFormalParameters ')' 1140 // '(' StrictFormalParameters ')'
1138 ParseFormalParameterList(scope, &error_locs, &has_rest, &ok); 1141 Scope* parameter_scope = NewScope(scope, PARAMETER_SCOPE);
1142 ParseFormalParameterList(scope, parameter_scope, &error_locs,
1143 initializers, &has_initializers,
1144 &has_rest, &ok);
1139 if (ok) ok = Check(Token::RPAREN); 1145 if (ok) ok = Check(Token::RPAREN);
1140 } else { 1146 } else {
1141 // BindingIdentifier 1147 // BindingIdentifier
1142 ParseFormalParameter(scope, &error_locs, has_rest, &ok); 1148 ParseFormalParameter(scope, nullptr, &error_locs,
1149 nullptr, nullptr, has_rest, &ok);
1143 } 1150 }
1144 1151
1145 if (ok) { 1152 if (ok) {
1146 ExpressionClassifier classifier; 1153 ExpressionClassifier classifier;
1147 Expression* expression = ParseArrowFunctionLiteral( 1154 Expression* expression = ParseArrowFunctionLiteral(
1148 scope, error_locs, has_rest, &classifier, &ok); 1155 scope, error_locs, has_rest, &classifier, &ok);
1149 // TODO(dslomov): report error if not a valid expression. 1156 // TODO(dslomov): report error if not a valid expression.
1150 if (ok) { 1157 if (ok) {
1151 // Scanning must end at the same position that was recorded 1158 // Scanning must end at the same position that was recorded
1152 // previously. If not, parsing has been interrupted due to a stack 1159 // previously. If not, parsing has been interrupted due to a stack
(...skipping 2736 matching lines...) Expand 10 before | Expand all | Expand 10 after
3889 // TODO(wingo): Make a better message. 3896 // TODO(wingo): Make a better message.
3890 ReportMessageAt(params_loc, "malformed_arrow_function_parameter_list"); 3897 ReportMessageAt(params_loc, "malformed_arrow_function_parameter_list");
3891 *ok = false; 3898 *ok = false;
3892 return; 3899 return;
3893 } 3900 }
3894 3901
3895 DeclareArrowFunctionParameters(scope, params, params_loc, error_locs, ok); 3902 DeclareArrowFunctionParameters(scope, params, params_loc, error_locs, ok);
3896 } 3903 }
3897 3904
3898 3905
3906 ZoneList<Statement*>* Parser::DesugarInitializeParameters(
3907 bool has_initializers, ZoneList<Expression*>* initializers) {
3908 ZoneList<Statement*>* body = new (zone()) ZoneList<Statement*>(0, zone());
3909 if (has_initializers) {
3910 for (int i = 0; i < initializers->length(); ++i) {
3911 Expression* initializer = initializers->at(i);
3912 // TODO(caitp): ensure proper TDZ behaviour --- need hole-check for
3913 // all parameter bindings, including ones without initializers
3914 if (initializer) {
3915 // IS_UNDEFINED(%_Arguments(i)) ? <initializer> : %_Arguments(i);
3916 const AstRawString* fn_name = ast_value_factory()->empty_string();
3917 const Runtime::Function* arguments =
3918 Runtime::FunctionForId(Runtime::kInlineArguments);
3919 ZoneList<Expression*>* arguments_i0 =
3920 new (zone()) ZoneList<Expression*>(0, zone());
3921 arguments_i0->Add(
3922 factory()->NewSmiLiteral(i, RelocInfo::kNoPosition), zone());
3923
3924 ZoneList<Expression*>* arguments_i1 =
3925 new (zone()) ZoneList<Expression*>(0, zone());
3926 arguments_i1->Add(
3927 factory()->NewSmiLiteral(i, RelocInfo::kNoPosition), zone());
3928
3929 Expression* arg_or_default = factory()->NewConditional(
3930 // condition:
3931 factory()->NewCompareOperation(
3932 Token::EQ_STRICT,
3933 factory()->NewCallRuntime(fn_name, arguments, arguments_i0,
3934 RelocInfo::kNoPosition),
3935 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
3936 RelocInfo::kNoPosition),
3937 // if true:
3938 initializer,
3939 // if false:
3940 factory()->NewCallRuntime(fn_name, arguments, arguments_i1,
3941 RelocInfo::kNoPosition),
3942 RelocInfo::kNoPosition);
3943
3944 Expression* assign = factory()->NewAssignment(
3945 Token::ASSIGN,
3946 factory()->NewVariableProxy(
3947 scope_->parameter(i), RelocInfo::kNoPosition),
3948 arg_or_default,
3949 RelocInfo::kNoPosition);
3950
3951 body->Add(
3952 factory()->NewExpressionStatement(
3953 assign, RelocInfo::kNoPosition),
3954 zone());
3955 }
3956 }
3957 }
3958 return body;
3959 }
3960
3961
3899 FunctionLiteral* Parser::ParseFunctionLiteral( 3962 FunctionLiteral* Parser::ParseFunctionLiteral(
3900 const AstRawString* function_name, Scanner::Location function_name_location, 3963 const AstRawString* function_name, Scanner::Location function_name_location,
3901 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos, 3964 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos,
3902 FunctionLiteral::FunctionType function_type, 3965 FunctionLiteral::FunctionType function_type,
3903 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { 3966 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
3904 // Function :: 3967 // Function ::
3905 // '(' FormalParameterList? ')' '{' FunctionBody '}' 3968 // '(' FormalParameterList? ')' '{' FunctionBody '}'
3906 // 3969 //
3907 // Getter :: 3970 // Getter ::
3908 // '(' ')' '{' FunctionBody '}' 3971 // '(' ')' '{' FunctionBody '}'
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
3954 // compiling a function in an inner declaration scope in the eval, e.g. a 4017 // compiling a function in an inner declaration scope in the eval, e.g. a
3955 // nested function, and hoisting works normally relative to that. 4018 // nested function, and hoisting works normally relative to that.
3956 Scope* declaration_scope = scope_->DeclarationScope(); 4019 Scope* declaration_scope = scope_->DeclarationScope();
3957 Scope* original_declaration_scope = original_scope_->DeclarationScope(); 4020 Scope* original_declaration_scope = original_scope_->DeclarationScope();
3958 Scope* scope = function_type == FunctionLiteral::DECLARATION && 4021 Scope* scope = function_type == FunctionLiteral::DECLARATION &&
3959 is_sloppy(language_mode()) && 4022 is_sloppy(language_mode()) &&
3960 (original_scope_ == original_declaration_scope || 4023 (original_scope_ == original_declaration_scope ||
3961 declaration_scope != original_declaration_scope) 4024 declaration_scope != original_declaration_scope)
3962 ? NewScope(declaration_scope, FUNCTION_SCOPE, kind) 4025 ? NewScope(declaration_scope, FUNCTION_SCOPE, kind)
3963 : NewScope(scope_, FUNCTION_SCOPE, kind); 4026 : NewScope(scope_, FUNCTION_SCOPE, kind);
4027 Scope* parameter_scope = NewScope(scope, PARAMETER_SCOPE);
3964 ZoneList<Statement*>* body = NULL; 4028 ZoneList<Statement*>* body = NULL;
3965 int materialized_literal_count = -1; 4029 int materialized_literal_count = -1;
3966 int expected_property_count = -1; 4030 int expected_property_count = -1;
3967 int handler_count = 0; 4031 int handler_count = 0;
3968 FormalParameterErrorLocations error_locs; 4032 FormalParameterErrorLocations error_locs;
3969 FunctionLiteral::EagerCompileHint eager_compile_hint = 4033 FunctionLiteral::EagerCompileHint eager_compile_hint =
3970 parenthesized_function_ ? FunctionLiteral::kShouldEagerCompile 4034 parenthesized_function_ ? FunctionLiteral::kShouldEagerCompile
3971 : FunctionLiteral::kShouldLazyCompile; 4035 : FunctionLiteral::kShouldLazyCompile;
3972 // Parse function body. 4036 // Parse function body.
3973 { 4037 {
(...skipping 13 matching lines...) Expand all
3987 // expressions. This also marks the FunctionState as a generator. 4051 // expressions. This also marks the FunctionState as a generator.
3988 Variable* temp = scope_->DeclarationScope()->NewTemporary( 4052 Variable* temp = scope_->DeclarationScope()->NewTemporary(
3989 ast_value_factory()->dot_generator_object_string()); 4053 ast_value_factory()->dot_generator_object_string());
3990 function_state.set_generator_object_variable(temp); 4054 function_state.set_generator_object_variable(temp);
3991 } 4055 }
3992 4056
3993 bool has_rest = false; 4057 bool has_rest = false;
3994 Expect(Token::LPAREN, CHECK_OK); 4058 Expect(Token::LPAREN, CHECK_OK);
3995 int start_position = scanner()->location().beg_pos; 4059 int start_position = scanner()->location().beg_pos;
3996 scope_->set_start_position(start_position); 4060 scope_->set_start_position(start_position);
3997 num_parameters = 4061 ZoneList<Expression*>* initializers =
3998 ParseFormalParameterList(scope, &error_locs, &has_rest, CHECK_OK); 4062 new (zone()) ZoneList<Expression*>(0, zone());
4063 bool has_initializers = false;
4064 num_parameters = ParseFormalParameterList(scope, parameter_scope,
4065 &error_locs, initializers,
4066 &has_initializers, &has_rest,
4067 CHECK_OK);
3999 Expect(Token::RPAREN, CHECK_OK); 4068 Expect(Token::RPAREN, CHECK_OK);
4000 int formals_end_position = scanner()->location().end_pos; 4069 int formals_end_position = scanner()->location().end_pos;
4001 4070
4002 CheckArityRestrictions(num_parameters, arity_restriction, start_position, 4071 CheckArityRestrictions(num_parameters, arity_restriction, start_position,
4003 formals_end_position, CHECK_OK); 4072 formals_end_position, CHECK_OK);
4004 4073
4005 Expect(Token::LBRACE, CHECK_OK); 4074 Expect(Token::LBRACE, CHECK_OK);
4006 4075
4007 // If we have a named function expression, we add a local variable 4076 // If we have a named function expression, we add a local variable
4008 // declaration to the body of the function with the name of the 4077 // declaration to the body of the function with the name of the
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
4067 parenthesized_function_ = false; // The bit was set for this function only. 4136 parenthesized_function_ = false; // The bit was set for this function only.
4068 4137
4069 if (is_lazily_parsed) { 4138 if (is_lazily_parsed) {
4070 for (Scope* s = scope_->outer_scope(); 4139 for (Scope* s = scope_->outer_scope();
4071 s != nullptr && (s != s->DeclarationScope()); s = s->outer_scope()) { 4140 s != nullptr && (s != s->DeclarationScope()); s = s->outer_scope()) {
4072 s->ForceContextAllocation(); 4141 s->ForceContextAllocation();
4073 } 4142 }
4074 SkipLazyFunctionBody(&materialized_literal_count, 4143 SkipLazyFunctionBody(&materialized_literal_count,
4075 &expected_property_count, CHECK_OK); 4144 &expected_property_count, CHECK_OK);
4076 } else { 4145 } else {
4077 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op, 4146 body = DesugarInitializeParameters(has_initializers, initializers);
4078 kind, CHECK_OK); 4147 ZoneList<Statement*>* inner_body =
4148 ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op,
4149 kind, CHECK_OK);
4150 body->AddAll(*inner_body, zone());
4079 materialized_literal_count = function_state.materialized_literal_count(); 4151 materialized_literal_count = function_state.materialized_literal_count();
4080 expected_property_count = function_state.expected_property_count(); 4152 expected_property_count = function_state.expected_property_count();
4081 handler_count = function_state.handler_count(); 4153 handler_count = function_state.handler_count();
4082 4154
4083 if (is_strong(language_mode()) && IsSubclassConstructor(kind)) { 4155 if (is_strong(language_mode()) && IsSubclassConstructor(kind)) {
4084 if (!function_state.super_location().IsValid()) { 4156 if (!function_state.super_location().IsValid()) {
4085 ReportMessageAt(function_name_location, 4157 ReportMessageAt(function_name_location,
4086 "strong_super_call_missing", kReferenceError); 4158 "strong_super_call_missing", kReferenceError);
4087 *ok = false; 4159 *ok = false;
4088 return nullptr; 4160 return nullptr;
(...skipping 1733 matching lines...) Expand 10 before | Expand all | Expand 10 after
5822 5894
5823 Expression* Parser::SpreadCallNew(Expression* function, 5895 Expression* Parser::SpreadCallNew(Expression* function,
5824 ZoneList<v8::internal::Expression*>* args, 5896 ZoneList<v8::internal::Expression*>* args,
5825 int pos) { 5897 int pos) {
5826 args->InsertAt(0, function, zone()); 5898 args->InsertAt(0, function, zone());
5827 5899
5828 return factory()->NewCallRuntime( 5900 return factory()->NewCallRuntime(
5829 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 5901 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
5830 } 5902 }
5831 } } // namespace v8::internal 5903 } } // namespace v8::internal
OLDNEW
« src/globals.h ('K') | « src/parser.h ('k') | src/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698