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

Side by Side Diff: src/parser.cc

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, 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
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | src/scopes.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 861 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 set_allow_harmony_classes(FLAG_harmony_classes); 872 set_allow_harmony_classes(FLAG_harmony_classes);
873 set_allow_harmony_object_literals(FLAG_harmony_object_literals); 873 set_allow_harmony_object_literals(FLAG_harmony_object_literals);
874 set_allow_harmony_sloppy(FLAG_harmony_sloppy); 874 set_allow_harmony_sloppy(FLAG_harmony_sloppy);
875 set_allow_harmony_unicode(FLAG_harmony_unicode); 875 set_allow_harmony_unicode(FLAG_harmony_unicode);
876 set_allow_harmony_computed_property_names( 876 set_allow_harmony_computed_property_names(
877 FLAG_harmony_computed_property_names); 877 FLAG_harmony_computed_property_names);
878 set_allow_harmony_rest_params(FLAG_harmony_rest_parameters); 878 set_allow_harmony_rest_params(FLAG_harmony_rest_parameters);
879 set_allow_harmony_spreadcalls(FLAG_harmony_spreadcalls); 879 set_allow_harmony_spreadcalls(FLAG_harmony_spreadcalls);
880 set_allow_harmony_destructuring(FLAG_harmony_destructuring); 880 set_allow_harmony_destructuring(FLAG_harmony_destructuring);
881 set_allow_harmony_spread_arrays(FLAG_harmony_spread_arrays); 881 set_allow_harmony_spread_arrays(FLAG_harmony_spread_arrays);
882 set_allow_harmony_default_parameters(FLAG_harmony_default_parameters);
882 set_allow_strong_mode(FLAG_strong_mode); 883 set_allow_strong_mode(FLAG_strong_mode);
883 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 884 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
884 ++feature) { 885 ++feature) {
885 use_counts_[feature] = 0; 886 use_counts_[feature] = 0;
886 } 887 }
887 if (info->ast_value_factory() == NULL) { 888 if (info->ast_value_factory() == NULL) {
888 // info takes ownership of AstValueFactory. 889 // info takes ownership of AstValueFactory.
889 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed())); 890 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed()));
890 info->set_ast_value_factory_owned(); 891 info->set_ast_value_factory_owned();
891 ast_value_factory_ = info->ast_value_factory(); 892 ast_value_factory_ = info->ast_value_factory();
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
1135 ? FunctionLiteral::ANONYMOUS_EXPRESSION 1136 ? FunctionLiteral::ANONYMOUS_EXPRESSION
1136 : FunctionLiteral::NAMED_EXPRESSION) 1137 : FunctionLiteral::NAMED_EXPRESSION)
1137 : FunctionLiteral::DECLARATION; 1138 : FunctionLiteral::DECLARATION;
1138 bool ok = true; 1139 bool ok = true;
1139 1140
1140 if (shared_info->is_arrow()) { 1141 if (shared_info->is_arrow()) {
1141 Scope* scope = NewScope(scope_, ARROW_SCOPE); 1142 Scope* scope = NewScope(scope_, ARROW_SCOPE);
1142 scope->set_start_position(shared_info->start_position()); 1143 scope->set_start_position(shared_info->start_position());
1143 ExpressionClassifier formals_classifier; 1144 ExpressionClassifier formals_classifier;
1144 bool has_rest = false; 1145 bool has_rest = false;
1146 bool has_parameter_expressions = false;
1147
1148 // TODO(caitp): make default parameters work in arrow functions
1149 ZoneList<Expression*>* initializers =
1150 new (zone()) ZoneList<Expression*>(0, zone());
1145 if (Check(Token::LPAREN)) { 1151 if (Check(Token::LPAREN)) {
1146 // '(' StrictFormalParameters ')' 1152 // '(' StrictFormalParameters ')'
1147 ParseFormalParameterList(scope, &has_rest, &formals_classifier, &ok); 1153 ParseFormalParameterList(scope, initializers,
1154 &has_parameter_expressions, &has_rest,
1155 &formals_classifier, &ok);
1148 if (ok) ok = Check(Token::RPAREN); 1156 if (ok) ok = Check(Token::RPAREN);
1149 } else { 1157 } else {
1150 // BindingIdentifier 1158 // BindingIdentifier
1151 ParseFormalParameter(scope, has_rest, &formals_classifier, &ok); 1159 ParseFormalParameter(scope, has_rest, &formals_classifier, &ok);
1152 } 1160 }
1153 1161
1154 if (ok) { 1162 if (ok) {
1155 Expression* expression = 1163 Expression* expression =
1156 ParseArrowFunctionLiteral(scope, has_rest, formals_classifier, &ok); 1164 ParseArrowFunctionLiteral(scope, has_rest, formals_classifier, &ok);
1157 if (ok) { 1165 if (ok) {
(...skipping 2233 matching lines...) Expand 10 before | Expand all | Expand 10 after
3391 3399
3392 inner_scope->set_end_position(scanner()->location().end_pos); 3400 inner_scope->set_end_position(scanner()->location().end_pos);
3393 inner_block->set_scope(inner_scope); 3401 inner_block->set_scope(inner_scope);
3394 scope_ = for_scope; 3402 scope_ = for_scope;
3395 3403
3396 outer_loop->Initialize(NULL, NULL, NULL, inner_block); 3404 outer_loop->Initialize(NULL, NULL, NULL, inner_block);
3397 return outer_block; 3405 return outer_block;
3398 } 3406 }
3399 3407
3400 3408
3409 ZoneList<Statement*>* Parser::DesugarInitializeParameters(
3410 Scope* scope, bool has_parameter_expressions,
3411 ZoneList<Expression*>* initializers) {
3412 DCHECK(scope->is_function_scope());
3413
3414 if (has_parameter_expressions) {
3415 // If has_parameter_expressions for the function is true, each parameter is
3416 // desugared as follows:
3417 //
3418 // SingleNameBinding :
3419 // let <name> = %_Arguments(<index>);
3420 // SingleNameBinding Initializer
3421 // let <name> = IS_UNDEFINED(%_Arguments(<index>)) ? <initializer>
3422 // : %_Arguments(<index>);
3423 //
3424 // TODO(caitp, dslomov): support BindingPatterns & rest parameters
3425 //
3426 scope->UndeclareParametersForExpressions();
3427 ZoneList<Statement*>* body =
3428 new (zone()) ZoneList<Statement*>(initializers->length(), zone());
3429 for (int i = 0; i < initializers->length(); ++i) {
3430 Expression* initializer = initializers->at(i);
3431
3432 // Position of parameter VariableProxy, for hole-checking
3433 int pos = scope->parameter_position(i);
3434
3435 static const int kCapacity = 1;
3436 static const bool kIsInitializerBlock = true;
3437 Block* param_block =
3438 factory()->NewBlock(nullptr, kCapacity, kIsInitializerBlock, pos);
3439
3440 static const VariableMode kMode = LET;
rossberg 2015/06/01 15:46:39 Nit: drop this auxiliary
caitp (gmail) 2015/06/01 15:56:30 Done.
3441 VariableProxy* proxy =
3442 NewUnresolved(scope->parameter(i)->raw_name(), kMode);
3443 VariableDeclaration* declaration = factory()->NewVariableDeclaration(
3444 proxy, kMode, scope, RelocInfo::kNoPosition);
3445
3446 bool ok = true;
3447 // All formal parameters have been removed from the scope VariableMap,
3448 // and so Declare() should not be able to fail.
3449 proxy = factory()->NewVariableProxy(Declare(declaration, true, &ok), pos);
3450 DCHECK(ok);
3451
3452 const AstRawString* fn_name = ast_value_factory()->empty_string();
3453 const Runtime::Function* arguments =
3454 Runtime::FunctionForId(Runtime::kInlineArguments);
3455 ZoneList<Expression*>* arguments_i0 =
3456 new (zone()) ZoneList<Expression*>(1, zone());
3457 arguments_i0->Add(factory()->NewSmiLiteral(i, RelocInfo::kNoPosition),
3458 zone());
3459
3460 if (initializer != nullptr) {
rossberg 2015/06/01 15:46:39 Nit: switch branches (simple first)
caitp (gmail) 2015/06/01 15:56:30 Done.
3461 // IS_UNDEFINED(%_Arguments(i)) ? <initializer> : %_Arguments(i);
3462 ZoneList<Expression*>* arguments_i1 =
3463 new (zone()) ZoneList<Expression*>(1, zone());
3464 arguments_i1->Add(factory()->NewSmiLiteral(i, RelocInfo::kNoPosition),
3465 zone());
3466
3467 Expression* arg_or_default = factory()->NewConditional(
3468 // condition:
3469 factory()->NewCompareOperation(
3470 Token::EQ_STRICT,
3471 factory()->NewCallRuntime(fn_name, arguments, arguments_i0,
3472 RelocInfo::kNoPosition),
3473 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
3474 RelocInfo::kNoPosition),
3475 // if true:
3476 initializer,
3477 // if false:
3478 factory()->NewCallRuntime(fn_name, arguments, arguments_i1,
3479 RelocInfo::kNoPosition),
3480 RelocInfo::kNoPosition);
3481
3482 Expression* assign = factory()->NewAssignment(
3483 Token::INIT_LET, proxy, arg_or_default, RelocInfo::kNoPosition);
3484
3485 param_block->AddStatement(
3486 factory()->NewExpressionStatement(assign, RelocInfo::kNoPosition),
3487 zone());
3488 proxy->var()->set_initializer_position(initializer->position());
3489 } else {
3490 // let <name> = %_Arguments(i)
3491 Expression* assign = factory()->NewAssignment(
3492 Token::INIT_LET, proxy,
3493 factory()->NewCallRuntime(fn_name, arguments, arguments_i0,
3494 RelocInfo::kNoPosition),
3495 RelocInfo::kNoPosition);
3496 param_block->AddStatement(
3497 factory()->NewExpressionStatement(assign, RelocInfo::kNoPosition),
3498 zone());
3499 proxy->var()->set_initializer_position(pos);
3500 }
3501 body->Add(param_block, zone());
3502 }
3503 return body;
3504 } else {
3505 // If has_parameter_expressions is false, remove the unnecessary parameter
3506 // block scopes.
3507 ZoneList<Scope*>* scopes = scope->inner_scopes();
3508 for (int i = 0; i < scopes->length(); ++i) {
3509 Scope* scope = scopes->at(i);
3510 DCHECK(scope->is_block_scope());
3511 scope->FinalizeBlockScope();
3512 }
3513 return nullptr;
3514 }
3515 }
3516
3517
3401 Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels, 3518 Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
3402 bool* ok) { 3519 bool* ok) {
3403 // ForStatement :: 3520 // ForStatement ::
3404 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement 3521 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
3405 3522
3406 int stmt_pos = peek_position(); 3523 int stmt_pos = peek_position();
3407 bool is_const = false; 3524 bool is_const = false;
3408 Statement* init = NULL; 3525 Statement* init = NULL;
3409 ZoneList<const AstRawString*> lexical_bindings(1, zone()); 3526 ZoneList<const AstRawString*> lexical_bindings(1, zone());
3410 3527
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
3761 const AstRawString* raw_name = expr->AsVariableProxy()->raw_name(); 3878 const AstRawString* raw_name = expr->AsVariableProxy()->raw_name();
3762 Scanner::Location param_location(expr->position(), 3879 Scanner::Location param_location(expr->position(),
3763 expr->position() + raw_name->length()); 3880 expr->position() + raw_name->length());
3764 3881
3765 // When the formal parameter was originally seen, it was parsed as a 3882 // When the formal parameter was originally seen, it was parsed as a
3766 // VariableProxy and recorded as unresolved in the scope. Here we undo that 3883 // VariableProxy and recorded as unresolved in the scope. Here we undo that
3767 // parse-time side-effect. 3884 // parse-time side-effect.
3768 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); 3885 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
3769 3886
3770 bool is_rest = false; 3887 bool is_rest = false;
3771 bool is_duplicate = DeclareFormalParameter(scope, raw_name, is_rest); 3888 int pos = expr->position();
3889 bool is_duplicate = DeclareFormalParameter(scope, raw_name, is_rest, pos);
3772 3890
3773 if (is_duplicate && !duplicate_loc->IsValid()) { 3891 if (is_duplicate && !duplicate_loc->IsValid()) {
3774 *duplicate_loc = param_location; 3892 *duplicate_loc = param_location;
3775 } 3893 }
3776 } 3894 }
3777 3895
3778 3896
3779 void ParserTraits::ParseArrowFunctionFormalParameters( 3897 void ParserTraits::ParseArrowFunctionFormalParameters(
3780 Scope* scope, Expression* params, const Scanner::Location& params_loc, 3898 Scope* scope, Expression* params, const Scanner::Location& params_loc,
3781 bool* is_rest, Scanner::Location* duplicate_loc, bool* ok) { 3899 bool* is_rest, Scanner::Location* duplicate_loc, bool* ok) {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
3872 3990
3873 // Calling a generator returns a generator object. That object is stored 3991 // Calling a generator returns a generator object. That object is stored
3874 // in a temporary variable, a definition that is used by "yield" 3992 // in a temporary variable, a definition that is used by "yield"
3875 // expressions. This also marks the FunctionState as a generator. 3993 // expressions. This also marks the FunctionState as a generator.
3876 Variable* temp = scope_->DeclarationScope()->NewTemporary( 3994 Variable* temp = scope_->DeclarationScope()->NewTemporary(
3877 ast_value_factory()->dot_generator_object_string()); 3995 ast_value_factory()->dot_generator_object_string());
3878 function_state.set_generator_object_variable(temp); 3996 function_state.set_generator_object_variable(temp);
3879 } 3997 }
3880 3998
3881 bool has_rest = false; 3999 bool has_rest = false;
4000 bool has_parameter_expressions = false;
3882 Expect(Token::LPAREN, CHECK_OK); 4001 Expect(Token::LPAREN, CHECK_OK);
3883 int start_position = scanner()->location().beg_pos; 4002 int start_position = scanner()->location().beg_pos;
3884 scope_->set_start_position(start_position); 4003 scope_->set_start_position(start_position);
3885 num_parameters = ParseFormalParameterList(scope, &has_rest, 4004 ZoneList<Expression*>* initializers =
3886 &formals_classifier, CHECK_OK); 4005 new (zone()) ZoneList<Expression*>(0, zone());
4006 num_parameters = ParseFormalParameterList(
4007 scope, initializers, &has_parameter_expressions, &has_rest,
4008 &formals_classifier, CHECK_OK);
3887 Expect(Token::RPAREN, CHECK_OK); 4009 Expect(Token::RPAREN, CHECK_OK);
3888 int formals_end_position = scanner()->location().end_pos; 4010 int formals_end_position = scanner()->location().end_pos;
3889 4011
3890 CheckArityRestrictions(num_parameters, arity_restriction, has_rest, 4012 CheckArityRestrictions(num_parameters, arity_restriction, has_rest,
3891 start_position, formals_end_position, CHECK_OK); 4013 start_position, formals_end_position, CHECK_OK);
3892 4014
3893 Expect(Token::LBRACE, CHECK_OK); 4015 Expect(Token::LBRACE, CHECK_OK);
3894 4016
3895 // If we have a named function expression, we add a local variable 4017 // If we have a named function expression, we add a local variable
3896 // declaration to the body of the function with the name of the 4018 // declaration to the body of the function with the name of the
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
3977 is_lazily_parsed = false; 4099 is_lazily_parsed = false;
3978 4100
3979 // This is probably an initialization function. Inform the compiler it 4101 // This is probably an initialization function. Inform the compiler it
3980 // should also eager-compile this function, and that we expect it to be 4102 // should also eager-compile this function, and that we expect it to be
3981 // used once. 4103 // used once.
3982 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; 4104 eager_compile_hint = FunctionLiteral::kShouldEagerCompile;
3983 should_be_used_once_hint = true; 4105 should_be_used_once_hint = true;
3984 } 4106 }
3985 } 4107 }
3986 if (!is_lazily_parsed) { 4108 if (!is_lazily_parsed) {
3987 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op, 4109 body = DesugarInitializeParameters(scope, has_parameter_expressions,
3988 kind, CHECK_OK); 4110 initializers);
4111 if (has_parameter_expressions) {
4112 // TODO(caitp): Function body scope must be a declaration scope
4113 Scope* function_body_scope = NewScope(scope, BLOCK_SCOPE);
4114 function_body_scope->set_start_position(scope->start_position());
4115 function_body_scope->SetScopeName(function_name);
4116 BlockState function_body_state(&scope_, function_body_scope);
4117 ZoneList<Statement*>* inner_body = ParseEagerFunctionBody(
4118 function_name, pos, fvar, fvar_init_op, kind, CHECK_OK);
4119
4120 // Declare Block node
4121 Block* block =
4122 factory()->NewBlock(nullptr, inner_body->length(), false, pos);
4123 block->set_scope(function_body_scope);
4124 for (int i = 0; i < inner_body->length(); ++i) {
4125 block->AddStatement(inner_body->at(i), zone());
4126 }
4127
4128 scope->set_end_position(function_body_scope->end_position());
4129 body->Add(block, zone());
4130 } else {
4131 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op,
4132 kind, CHECK_OK);
4133 }
3989 materialized_literal_count = function_state.materialized_literal_count(); 4134 materialized_literal_count = function_state.materialized_literal_count();
3990 expected_property_count = function_state.expected_property_count(); 4135 expected_property_count = function_state.expected_property_count();
3991 handler_count = function_state.handler_count(); 4136 handler_count = function_state.handler_count();
3992 4137
3993 if (is_strong(language_mode()) && IsSubclassConstructor(kind)) { 4138 if (is_strong(language_mode()) && IsSubclassConstructor(kind)) {
3994 if (!function_state.super_location().IsValid()) { 4139 if (!function_state.super_location().IsValid()) {
3995 ReportMessageAt(function_name_location, 4140 ReportMessageAt(function_name_location,
3996 MessageTemplate::kStrongSuperCallMissing, 4141 MessageTemplate::kStrongSuperCallMissing,
3997 kReferenceError); 4142 kReferenceError);
3998 *ok = false; 4143 *ok = false;
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
4262 reusable_preparser_->set_allow_harmony_computed_property_names( 4407 reusable_preparser_->set_allow_harmony_computed_property_names(
4263 allow_harmony_computed_property_names()); 4408 allow_harmony_computed_property_names());
4264 reusable_preparser_->set_allow_harmony_rest_params( 4409 reusable_preparser_->set_allow_harmony_rest_params(
4265 allow_harmony_rest_params()); 4410 allow_harmony_rest_params());
4266 reusable_preparser_->set_allow_harmony_spreadcalls( 4411 reusable_preparser_->set_allow_harmony_spreadcalls(
4267 allow_harmony_spreadcalls()); 4412 allow_harmony_spreadcalls());
4268 reusable_preparser_->set_allow_harmony_destructuring( 4413 reusable_preparser_->set_allow_harmony_destructuring(
4269 allow_harmony_destructuring()); 4414 allow_harmony_destructuring());
4270 reusable_preparser_->set_allow_harmony_spread_arrays( 4415 reusable_preparser_->set_allow_harmony_spread_arrays(
4271 allow_harmony_spread_arrays()); 4416 allow_harmony_spread_arrays());
4417 reusable_preparser_->set_allow_harmony_default_parameters(
4418 allow_harmony_default_parameters());
4272 reusable_preparser_->set_allow_strong_mode(allow_strong_mode()); 4419 reusable_preparser_->set_allow_strong_mode(allow_strong_mode());
4273 } 4420 }
4274 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction( 4421 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction(
4275 language_mode(), function_state_->kind(), logger, bookmark); 4422 language_mode(), function_state_->kind(), logger, bookmark);
4276 if (pre_parse_timer_ != NULL) { 4423 if (pre_parse_timer_ != NULL) {
4277 pre_parse_timer_->Stop(); 4424 pre_parse_timer_->Stop();
4278 } 4425 }
4279 return result; 4426 return result;
4280 } 4427 }
4281 4428
(...skipping 1502 matching lines...) Expand 10 before | Expand all | Expand 10 after
5784 5931
5785 Expression* Parser::SpreadCallNew(Expression* function, 5932 Expression* Parser::SpreadCallNew(Expression* function,
5786 ZoneList<v8::internal::Expression*>* args, 5933 ZoneList<v8::internal::Expression*>* args,
5787 int pos) { 5934 int pos) {
5788 args->InsertAt(0, function, zone()); 5935 args->InsertAt(0, function, zone());
5789 5936
5790 return factory()->NewCallRuntime( 5937 return factory()->NewCallRuntime(
5791 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 5938 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
5792 } 5939 }
5793 } } // namespace v8::internal 5940 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | src/scopes.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698