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

Side by Side Diff: src/parser.cc

Issue 1272673003: [es6] Re-implement rest parameters via desugaring. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Tiny bit more cleanup Created 5 years, 3 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/preparser.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/parser.h" 5 #include "src/parser.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/ast-literal-reindexer.h" 9 #include "src/ast-literal-reindexer.h"
10 #include "src/bailout-reason.h" 10 #include "src/bailout-reason.h"
(...skipping 3924 matching lines...) Expand 10 before | Expand all | Expand 10 after
3935 expr = right; 3935 expr = right;
3936 } 3936 }
3937 3937
3938 // Only the right-most expression may be a rest parameter. 3938 // Only the right-most expression may be a rest parameter.
3939 DCHECK(!parameters->has_rest); 3939 DCHECK(!parameters->has_rest);
3940 3940
3941 bool is_rest = expr->IsSpread(); 3941 bool is_rest = expr->IsSpread();
3942 if (is_rest) { 3942 if (is_rest) {
3943 expr = expr->AsSpread()->expression(); 3943 expr = expr->AsSpread()->expression();
3944 parameters->has_rest = true; 3944 parameters->has_rest = true;
3945
3946 if (!expr->IsVariableProxy()) {
3947 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList);
wingo 2015/08/31 13:07:31 Why is this necessary? ParseArrowFunctionFormalPa
caitp (gmail) 2015/08/31 13:23:40 You're right, I realized that this morning but hav
3948 *ok = false;
3949 return;
3950 }
3951
3952 parameters->rest_array_literal_index =
3953 parser_->function_state_->NextMaterializedLiteralIndex();
3954 ++parameters->materialized_literals_count;
3945 } 3955 }
3946 if (parameters->is_simple) { 3956 if (parameters->is_simple) {
3947 parameters->is_simple = !is_rest && expr->IsVariableProxy(); 3957 parameters->is_simple = expr->IsVariableProxy();
3948 } 3958 }
3949 3959
3950 Expression* initializer = nullptr; 3960 Expression* initializer = nullptr;
3951 if (expr->IsVariableProxy()) { 3961 if (expr->IsVariableProxy()) {
3952 // When the formal parameter was originally seen, it was parsed as a 3962 // When the formal parameter was originally seen, it was parsed as a
3953 // VariableProxy and recorded as unresolved in the scope. Here we undo that 3963 // VariableProxy and recorded as unresolved in the scope. Here we undo that
3954 // parse-time side-effect for parameters that are single-names (not 3964 // parse-time side-effect for parameters that are single-names (not
3955 // patterns; for patterns that happens uniformly in 3965 // patterns; for patterns that happens uniformly in
3956 // PatternRewriter::VisitVariableProxy). 3966 // PatternRewriter::VisitVariableProxy).
3957 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); 3967 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
3991 } 4001 }
3992 4002
3993 4003
3994 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) { 4004 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) {
3995 if (parser_->function_state_->materialized_literal_count() > 0) { 4005 if (parser_->function_state_->materialized_literal_count() > 0) {
3996 AstLiteralReindexer reindexer; 4006 AstLiteralReindexer reindexer;
3997 4007
3998 for (const auto p : parameters.params) { 4008 for (const auto p : parameters.params) {
3999 if (p.pattern != nullptr) reindexer.Reindex(p.pattern); 4009 if (p.pattern != nullptr) reindexer.Reindex(p.pattern);
4000 } 4010 }
4011
4012 if (parameters.has_rest) {
4013 parameters.rest_array_literal_index = reindexer.NextIndex();
4014 }
4015
4001 DCHECK(reindexer.count() <= 4016 DCHECK(reindexer.count() <=
4002 parser_->function_state_->materialized_literal_count()); 4017 parser_->function_state_->materialized_literal_count());
4003 } 4018 }
4004 } 4019 }
4005 4020
4006 4021
4007 FunctionLiteral* Parser::ParseFunctionLiteral( 4022 FunctionLiteral* Parser::ParseFunctionLiteral(
4008 const AstRawString* function_name, Scanner::Location function_name_location, 4023 const AstRawString* function_name, Scanner::Location function_name_location,
4009 FunctionNameValidity function_name_validity, FunctionKind kind, 4024 FunctionNameValidity function_name_validity, FunctionKind kind,
4010 int function_token_pos, FunctionLiteral::FunctionType function_type, 4025 int function_token_pos, FunctionLiteral::FunctionType function_type,
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
4160 // parsing if it suspect that wasn't a good idea. If so, or if we didn't 4175 // parsing if it suspect that wasn't a good idea. If so, or if we didn't
4161 // try to lazy parse in the first place, we'll have to parse eagerly. 4176 // try to lazy parse in the first place, we'll have to parse eagerly.
4162 Scanner::BookmarkScope bookmark(scanner()); 4177 Scanner::BookmarkScope bookmark(scanner());
4163 if (is_lazily_parsed) { 4178 if (is_lazily_parsed) {
4164 Scanner::BookmarkScope* maybe_bookmark = 4179 Scanner::BookmarkScope* maybe_bookmark =
4165 bookmark.Set() ? &bookmark : nullptr; 4180 bookmark.Set() ? &bookmark : nullptr;
4166 SkipLazyFunctionBody(&materialized_literal_count, 4181 SkipLazyFunctionBody(&materialized_literal_count,
4167 &expected_property_count, /*CHECK_OK*/ ok, 4182 &expected_property_count, /*CHECK_OK*/ ok,
4168 maybe_bookmark); 4183 maybe_bookmark);
4169 4184
4185 if (formals.materialized_literals_count > 0) {
4186 materialized_literal_count += formals.materialized_literals_count;
4187 }
wingo 2015/08/31 13:07:31 Is this bit specific to this patch or is it a gene
caitp (gmail) 2015/08/31 13:23:40 Well, it's a mix. See https://code.google.com/p/v8
4188
4170 if (bookmark.HasBeenReset()) { 4189 if (bookmark.HasBeenReset()) {
4171 // Trigger eager (re-)parsing, just below this block. 4190 // Trigger eager (re-)parsing, just below this block.
4172 is_lazily_parsed = false; 4191 is_lazily_parsed = false;
4173 4192
4174 // This is probably an initialization function. Inform the compiler it 4193 // This is probably an initialization function. Inform the compiler it
4175 // should also eager-compile this function, and that we expect it to be 4194 // should also eager-compile this function, and that we expect it to be
4176 // used once. 4195 // used once.
4177 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; 4196 eager_compile_hint = FunctionLiteral::kShouldEagerCompile;
4178 should_be_used_once_hint = true; 4197 should_be_used_once_hint = true;
4179 } 4198 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
4224 4243
4225 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 4244 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
4226 function_name, ast_value_factory(), scope, body, 4245 function_name, ast_value_factory(), scope, body,
4227 materialized_literal_count, expected_property_count, arity, 4246 materialized_literal_count, expected_property_count, arity,
4228 duplicate_parameters, function_type, FunctionLiteral::kIsFunction, 4247 duplicate_parameters, function_type, FunctionLiteral::kIsFunction,
4229 eager_compile_hint, kind, pos); 4248 eager_compile_hint, kind, pos);
4230 function_literal->set_function_token_position(function_token_pos); 4249 function_literal->set_function_token_position(function_token_pos);
4231 if (should_be_used_once_hint) 4250 if (should_be_used_once_hint)
4232 function_literal->set_should_be_used_once_hint(); 4251 function_literal->set_should_be_used_once_hint();
4233 4252
4234 if (scope->has_rest_parameter()) {
4235 // TODO(caitp): enable optimization of functions with rest params
4236 function_literal->set_dont_optimize_reason(kRestParameter);
4237 }
4238
4239 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); 4253 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
4240 return function_literal; 4254 return function_literal;
4241 } 4255 }
4242 4256
4243 4257
4244 void Parser::SkipLazyFunctionBody(int* materialized_literal_count, 4258 void Parser::SkipLazyFunctionBody(int* materialized_literal_count,
4245 int* expected_property_count, bool* ok, 4259 int* expected_property_count, bool* ok,
4246 Scanner::BookmarkScope* bookmark) { 4260 Scanner::BookmarkScope* bookmark) {
4247 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet()); 4261 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet());
4248 if (produce_cached_parse_data()) CHECK(log_); 4262 if (produce_cached_parse_data()) CHECK(log_);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
4363 4377
4364 4378
4365 Block* Parser::BuildParameterInitializationBlock( 4379 Block* Parser::BuildParameterInitializationBlock(
4366 const ParserFormalParameters& parameters, bool* ok) { 4380 const ParserFormalParameters& parameters, bool* ok) {
4367 DCHECK(!parameters.is_simple); 4381 DCHECK(!parameters.is_simple);
4368 DCHECK(scope_->is_function_scope()); 4382 DCHECK(scope_->is_function_scope());
4369 Block* init_block = 4383 Block* init_block =
4370 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); 4384 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
4371 for (int i = 0; i < parameters.params.length(); ++i) { 4385 for (int i = 0; i < parameters.params.length(); ++i) {
4372 auto parameter = parameters.params[i]; 4386 auto parameter = parameters.params[i];
4373 // TODO(caitp,rossberg): Remove special handling for rest once desugared.
4374 if (parameter.is_rest) break;
4375 DeclarationDescriptor descriptor; 4387 DeclarationDescriptor descriptor;
4376 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; 4388 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER;
4377 descriptor.parser = this; 4389 descriptor.parser = this;
4378 descriptor.declaration_scope = scope_; 4390 descriptor.declaration_scope = scope_;
4379 descriptor.scope = scope_; 4391 descriptor.scope = scope_;
4380 descriptor.hoist_scope = nullptr; 4392 descriptor.hoist_scope = nullptr;
4381 descriptor.mode = LET; 4393 descriptor.mode = LET;
4382 descriptor.is_const = false; 4394 descriptor.is_const = false;
4383 descriptor.needs_init = true; 4395 descriptor.needs_init = true;
4384 descriptor.declaration_pos = parameter.pattern->position(); 4396 descriptor.declaration_pos = parameter.pattern->position();
4385 descriptor.initialization_pos = parameter.pattern->position(); 4397 descriptor.initialization_pos = parameter.pattern->position();
4386 descriptor.init_op = Token::INIT_LET; 4398 descriptor.init_op = Token::INIT_LET;
4387 Expression* initial_value = 4399 Expression* initial_value =
4388 factory()->NewVariableProxy(parameters.scope->parameter(i)); 4400 factory()->NewVariableProxy(parameters.scope->parameter(i));
4389 if (parameter.initializer != nullptr) { 4401 if (parameter.initializer != nullptr) {
4390 // IS_UNDEFINED($param) ? initializer : $param 4402 // IS_UNDEFINED($param) ? initializer : $param
4391 auto condition = factory()->NewCompareOperation( 4403 auto condition = factory()->NewCompareOperation(
4392 Token::EQ_STRICT, 4404 Token::EQ_STRICT,
4393 factory()->NewVariableProxy(parameters.scope->parameter(i)), 4405 factory()->NewVariableProxy(parameters.scope->parameter(i)),
4394 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), 4406 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
4395 RelocInfo::kNoPosition); 4407 RelocInfo::kNoPosition);
4396 initial_value = factory()->NewConditional( 4408 initial_value = factory()->NewConditional(
4397 condition, parameter.initializer, initial_value, 4409 condition, parameter.initializer, initial_value,
4398 RelocInfo::kNoPosition); 4410 RelocInfo::kNoPosition);
4399 descriptor.initialization_pos = parameter.initializer->position(); 4411 descriptor.initialization_pos = parameter.initializer->position();
4400 } 4412 }
4401 4413
4414 if (parameter.is_rest) {
wingo 2015/08/31 13:07:31 This should probably be "else if" and the previous
adamk 2015/08/31 21:17:28 Agreed on the else-if suggestion here.
4415 // $rest = [];
4416 // for (var $argument_index = $rest_index;
4417 // $argument_index < %_ArgumentsLength();
4418 // ++$argument_index) {
4419 // %AppendElement($rest, %_Arguments($argument_index));
4420 // }
4421 // let <param> = $rest;
4422 DCHECK_NULL(parameter.initializer);
adamk 2015/08/31 21:17:28 That'll get rid of this DCHECK (or rather replace
caitp (gmail) 2015/08/31 22:43:15 Right, forgot about this one. Done in the last pat
4423 DCHECK(parameter.pattern->IsVariableProxy());
4424 DCHECK_EQ(i, parameters.params.length() - 1);
4425
4426 int pos = parameter.pattern->position();
4427 auto var = parameters.scope->parameter(i);
4428 auto empty_values = new (zone()) ZoneList<Expression*>(0, zone());
4429 auto empty_array = factory()->NewArrayLiteral(
4430 empty_values, parameters.rest_array_literal_index,
wingo 2015/08/31 13:07:31 Is it necessary to reserve a literal index in the
caitp (gmail) 2015/08/31 13:23:40 It seems to be necessary. Without it, an extra slo
4431 is_strong(language_mode()), RelocInfo::kNoPosition);
4432
4433 auto init_array = factory()->NewAssignment(
4434 Token::INIT_VAR, factory()->NewVariableProxy(var), empty_array,
4435 RelocInfo::kNoPosition);
4436
4437 auto loop = factory()->NewForStatement(NULL, RelocInfo::kNoPosition);
4438
4439 auto argument_index =
4440 parameters.scope->NewTemporary(ast_value_factory()->empty_string());
4441 auto init = factory()->NewExpressionStatement(
4442 factory()->NewAssignment(
4443 Token::INIT_VAR, factory()->NewVariableProxy(argument_index),
4444 factory()->NewSmiLiteral(i, RelocInfo::kNoPosition),
4445 RelocInfo::kNoPosition),
4446 RelocInfo::kNoPosition);
4447
4448 auto empty_arguments = new (zone()) ZoneList<Expression*>(0, zone());
4449
4450 // $arguments_index < arguments.length
4451 auto cond = factory()->NewCompareOperation(
4452 Token::LT, factory()->NewVariableProxy(argument_index),
4453 factory()->NewCallRuntime(Runtime::kInlineArgumentsLength,
4454 empty_arguments, RelocInfo::kNoPosition),
4455 RelocInfo::kNoPosition);
4456
4457 // ++argument_index
4458 auto next = factory()->NewExpressionStatement(
4459 factory()->NewCountOperation(
4460 Token::INC, true, factory()->NewVariableProxy(argument_index),
4461 RelocInfo::kNoPosition),
4462 RelocInfo::kNoPosition);
4463
4464 // %_Arguments($arguments_index)
4465 auto arguments_args = new (zone()) ZoneList<Expression*>(1, zone());
4466 arguments_args->Add(factory()->NewVariableProxy(argument_index), zone());
4467
4468 // %AppendElement($rest, %_Arguments($arguments_index))
4469 auto append_element_args = new (zone()) ZoneList<Expression*>(2, zone());
4470
4471 append_element_args->Add(factory()->NewVariableProxy(var), zone());
4472 append_element_args->Add(
4473 factory()->NewCallRuntime(Runtime::kInlineArguments, arguments_args,
4474 RelocInfo::kNoPosition),
4475 zone());
4476
4477 auto body = factory()->NewExpressionStatement(
4478 factory()->NewCallRuntime(Runtime::kAppendElement,
4479 append_element_args,
4480 RelocInfo::kNoPosition),
4481 RelocInfo::kNoPosition);
4482
4483 loop->Initialize(init, cond, next, body);
4484
4485 init_block->AddStatement(
4486 factory()->NewExpressionStatement(init_array, RelocInfo::kNoPosition),
4487 zone());
4488
4489 init_block->AddStatement(loop, zone());
4490
4491 descriptor.initialization_pos = pos;
4492 }
4493
4402 Scope* param_scope = scope_; 4494 Scope* param_scope = scope_;
4403 Block* param_block = init_block; 4495 Block* param_block = init_block;
4404 if (!parameter.is_simple() && scope_->calls_sloppy_eval()) { 4496 if (!parameter.is_simple() && scope_->calls_sloppy_eval()) {
4405 param_scope = NewScope(scope_, BLOCK_SCOPE); 4497 param_scope = NewScope(scope_, BLOCK_SCOPE);
4406 param_scope->set_is_declaration_scope(); 4498 param_scope->set_is_declaration_scope();
4407 param_scope->set_start_position(parameter.pattern->position()); 4499 param_scope->set_start_position(parameter.pattern->position());
4408 param_scope->set_end_position(RelocInfo::kNoPosition); 4500 param_scope->set_end_position(RelocInfo::kNoPosition);
4409 param_scope->RecordEvalCall(); 4501 param_scope->RecordEvalCall();
4410 param_block = factory()->NewBlock(NULL, 8, true, RelocInfo::kNoPosition); 4502 param_block = factory()->NewBlock(NULL, 8, true, RelocInfo::kNoPosition);
4411 param_block->set_scope(param_scope); 4503 param_block->set_scope(param_scope);
(...skipping 1694 matching lines...) Expand 10 before | Expand all | Expand 10 after
6106 6198
6107 Expression* Parser::SpreadCallNew(Expression* function, 6199 Expression* Parser::SpreadCallNew(Expression* function,
6108 ZoneList<v8::internal::Expression*>* args, 6200 ZoneList<v8::internal::Expression*>* args,
6109 int pos) { 6201 int pos) {
6110 args->InsertAt(0, function, zone()); 6202 args->InsertAt(0, function, zone());
6111 6203
6112 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos); 6204 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos);
6113 } 6205 }
6114 } // namespace internal 6206 } // namespace internal
6115 } // namespace v8 6207 } // namespace v8
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | src/preparser.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698