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

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: Use else-if (and rebase, sorry) 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 3931 matching lines...) Expand 10 before | Expand all | Expand 10 after
3942 expr = right; 3942 expr = right;
3943 } 3943 }
3944 3944
3945 // Only the right-most expression may be a rest parameter. 3945 // Only the right-most expression may be a rest parameter.
3946 DCHECK(!parameters->has_rest); 3946 DCHECK(!parameters->has_rest);
3947 3947
3948 bool is_rest = expr->IsSpread(); 3948 bool is_rest = expr->IsSpread();
3949 if (is_rest) { 3949 if (is_rest) {
3950 expr = expr->AsSpread()->expression(); 3950 expr = expr->AsSpread()->expression();
3951 parameters->has_rest = true; 3951 parameters->has_rest = true;
3952 parameters->rest_array_literal_index =
3953 parser_->function_state_->NextMaterializedLiteralIndex();
3954 ++parameters->materialized_literals_count;
3952 } 3955 }
3953 if (parameters->is_simple) { 3956 if (parameters->is_simple) {
3954 parameters->is_simple = !is_rest && expr->IsVariableProxy(); 3957 parameters->is_simple = expr->IsVariableProxy();
3955 } 3958 }
3956 3959
3957 Expression* initializer = nullptr; 3960 Expression* initializer = nullptr;
3958 if (expr->IsVariableProxy()) { 3961 if (expr->IsVariableProxy()) {
3959 // 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
3960 // 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
3961 // parse-time side-effect for parameters that are single-names (not 3964 // parse-time side-effect for parameters that are single-names (not
3962 // patterns; for patterns that happens uniformly in 3965 // patterns; for patterns that happens uniformly in
3963 // PatternRewriter::VisitVariableProxy). 3966 // PatternRewriter::VisitVariableProxy).
3964 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); 3967 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
3998 } 4001 }
3999 4002
4000 4003
4001 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) { 4004 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) {
4002 if (parser_->function_state_->materialized_literal_count() > 0) { 4005 if (parser_->function_state_->materialized_literal_count() > 0) {
4003 AstLiteralReindexer reindexer; 4006 AstLiteralReindexer reindexer;
4004 4007
4005 for (const auto p : parameters.params) { 4008 for (const auto p : parameters.params) {
4006 if (p.pattern != nullptr) reindexer.Reindex(p.pattern); 4009 if (p.pattern != nullptr) reindexer.Reindex(p.pattern);
4007 } 4010 }
4011
4012 if (parameters.has_rest) {
4013 parameters.rest_array_literal_index = reindexer.NextIndex();
4014 }
4015
4008 DCHECK(reindexer.count() <= 4016 DCHECK(reindexer.count() <=
4009 parser_->function_state_->materialized_literal_count()); 4017 parser_->function_state_->materialized_literal_count());
4010 } 4018 }
4011 } 4019 }
4012 4020
4013 4021
4014 FunctionLiteral* Parser::ParseFunctionLiteral( 4022 FunctionLiteral* Parser::ParseFunctionLiteral(
4015 const AstRawString* function_name, Scanner::Location function_name_location, 4023 const AstRawString* function_name, Scanner::Location function_name_location,
4016 FunctionNameValidity function_name_validity, FunctionKind kind, 4024 FunctionNameValidity function_name_validity, FunctionKind kind,
4017 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
4167 // 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
4168 // 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.
4169 Scanner::BookmarkScope bookmark(scanner()); 4177 Scanner::BookmarkScope bookmark(scanner());
4170 if (is_lazily_parsed) { 4178 if (is_lazily_parsed) {
4171 Scanner::BookmarkScope* maybe_bookmark = 4179 Scanner::BookmarkScope* maybe_bookmark =
4172 bookmark.Set() ? &bookmark : nullptr; 4180 bookmark.Set() ? &bookmark : nullptr;
4173 SkipLazyFunctionBody(&materialized_literal_count, 4181 SkipLazyFunctionBody(&materialized_literal_count,
4174 &expected_property_count, /*CHECK_OK*/ ok, 4182 &expected_property_count, /*CHECK_OK*/ ok,
4175 maybe_bookmark); 4183 maybe_bookmark);
4176 4184
4185 if (formals.materialized_literals_count > 0) {
4186 materialized_literal_count += formals.materialized_literals_count;
4187 }
wingo 2015/09/01 14:41:20 No need for the if, just execute the body uncondit
4188
4177 if (bookmark.HasBeenReset()) { 4189 if (bookmark.HasBeenReset()) {
4178 // Trigger eager (re-)parsing, just below this block. 4190 // Trigger eager (re-)parsing, just below this block.
4179 is_lazily_parsed = false; 4191 is_lazily_parsed = false;
4180 4192
4181 // This is probably an initialization function. Inform the compiler it 4193 // This is probably an initialization function. Inform the compiler it
4182 // 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
4183 // used once. 4195 // used once.
4184 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; 4196 eager_compile_hint = FunctionLiteral::kShouldEagerCompile;
4185 should_be_used_once_hint = true; 4197 should_be_used_once_hint = true;
4186 } 4198 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
4231 4243
4232 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 4244 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
4233 function_name, ast_value_factory(), scope, body, 4245 function_name, ast_value_factory(), scope, body,
4234 materialized_literal_count, expected_property_count, arity, 4246 materialized_literal_count, expected_property_count, arity,
4235 duplicate_parameters, function_type, FunctionLiteral::kIsFunction, 4247 duplicate_parameters, function_type, FunctionLiteral::kIsFunction,
4236 eager_compile_hint, kind, pos); 4248 eager_compile_hint, kind, pos);
4237 function_literal->set_function_token_position(function_token_pos); 4249 function_literal->set_function_token_position(function_token_pos);
4238 if (should_be_used_once_hint) 4250 if (should_be_used_once_hint)
4239 function_literal->set_should_be_used_once_hint(); 4251 function_literal->set_should_be_used_once_hint();
4240 4252
4241 if (scope->has_rest_parameter()) {
4242 // TODO(caitp): enable optimization of functions with rest params
4243 function_literal->set_dont_optimize_reason(kRestParameter);
4244 }
4245
4246 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); 4253 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
4247 return function_literal; 4254 return function_literal;
4248 } 4255 }
4249 4256
4250 4257
4251 void Parser::SkipLazyFunctionBody(int* materialized_literal_count, 4258 void Parser::SkipLazyFunctionBody(int* materialized_literal_count,
4252 int* expected_property_count, bool* ok, 4259 int* expected_property_count, bool* ok,
4253 Scanner::BookmarkScope* bookmark) { 4260 Scanner::BookmarkScope* bookmark) {
4254 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet()); 4261 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet());
4255 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
4370 4377
4371 4378
4372 Block* Parser::BuildParameterInitializationBlock( 4379 Block* Parser::BuildParameterInitializationBlock(
4373 const ParserFormalParameters& parameters, bool* ok) { 4380 const ParserFormalParameters& parameters, bool* ok) {
4374 DCHECK(!parameters.is_simple); 4381 DCHECK(!parameters.is_simple);
4375 DCHECK(scope_->is_function_scope()); 4382 DCHECK(scope_->is_function_scope());
4376 Block* init_block = 4383 Block* init_block =
4377 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); 4384 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
4378 for (int i = 0; i < parameters.params.length(); ++i) { 4385 for (int i = 0; i < parameters.params.length(); ++i) {
4379 auto parameter = parameters.params[i]; 4386 auto parameter = parameters.params[i];
4380 // TODO(caitp,rossberg): Remove special handling for rest once desugared.
4381 if (parameter.is_rest) break;
4382 DeclarationDescriptor descriptor; 4387 DeclarationDescriptor descriptor;
4383 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; 4388 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER;
4384 descriptor.parser = this; 4389 descriptor.parser = this;
4385 descriptor.declaration_scope = scope_; 4390 descriptor.declaration_scope = scope_;
4386 descriptor.scope = scope_; 4391 descriptor.scope = scope_;
4387 descriptor.hoist_scope = nullptr; 4392 descriptor.hoist_scope = nullptr;
4388 descriptor.mode = LET; 4393 descriptor.mode = LET;
4389 descriptor.is_const = false; 4394 descriptor.is_const = false;
4390 descriptor.needs_init = true; 4395 descriptor.needs_init = true;
4391 descriptor.declaration_pos = parameter.pattern->position(); 4396 descriptor.declaration_pos = parameter.pattern->position();
4392 descriptor.initialization_pos = parameter.pattern->position(); 4397 descriptor.initialization_pos = parameter.pattern->position();
4393 descriptor.init_op = Token::INIT_LET; 4398 descriptor.init_op = Token::INIT_LET;
4394 Expression* initial_value = 4399 Expression* initial_value =
4395 factory()->NewVariableProxy(parameters.scope->parameter(i)); 4400 factory()->NewVariableProxy(parameters.scope->parameter(i));
4396 if (parameter.initializer != nullptr) { 4401 if (parameter.initializer != nullptr) {
4397 // IS_UNDEFINED($param) ? initializer : $param 4402 // IS_UNDEFINED($param) ? initializer : $param
4403 DCHECK(!parameter.is_rest);
4398 auto condition = factory()->NewCompareOperation( 4404 auto condition = factory()->NewCompareOperation(
4399 Token::EQ_STRICT, 4405 Token::EQ_STRICT,
4400 factory()->NewVariableProxy(parameters.scope->parameter(i)), 4406 factory()->NewVariableProxy(parameters.scope->parameter(i)),
4401 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), 4407 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
4402 RelocInfo::kNoPosition); 4408 RelocInfo::kNoPosition);
4403 initial_value = factory()->NewConditional( 4409 initial_value = factory()->NewConditional(
4404 condition, parameter.initializer, initial_value, 4410 condition, parameter.initializer, initial_value,
4405 RelocInfo::kNoPosition); 4411 RelocInfo::kNoPosition);
4406 descriptor.initialization_pos = parameter.initializer->position(); 4412 descriptor.initialization_pos = parameter.initializer->position();
4413 } else if (parameter.is_rest) {
4414 // $rest = [];
4415 // for (var $argument_index = $rest_index;
Benedikt Meurer 2015/09/01 03:09:10 I guess there was some discussion about this desug
caitp (gmail) 2015/09/01 03:55:02 I don't think there are plans per se (but you migh
4416 // $argument_index < %_ArgumentsLength();
4417 // ++$argument_index) {
4418 // %AppendElement($rest, %_Arguments($argument_index));
4419 // }
4420 // let <param> = $rest;
4421 DCHECK(parameter.pattern->IsVariableProxy());
4422 DCHECK_EQ(i, parameters.params.length() - 1);
4423
4424 int pos = parameter.pattern->position();
4425 auto var = parameters.scope->parameter(i);
Benedikt Meurer 2015/09/01 03:09:10 Nit: Wow, this heavy autofication. I'm not sure wh
caitp (gmail) 2015/09/01 03:55:02 My feeling is that it's alright for AST nodes (bec
wingo 2015/09/01 14:41:20 I had this same thought when I read this FWIW; but
4426 auto empty_values = new (zone()) ZoneList<Expression*>(0, zone());
4427 auto empty_array = factory()->NewArrayLiteral(
4428 empty_values, parameters.rest_array_literal_index,
4429 is_strong(language_mode()), RelocInfo::kNoPosition);
4430
4431 auto init_array = factory()->NewAssignment(
4432 Token::INIT_VAR, factory()->NewVariableProxy(var), empty_array,
4433 RelocInfo::kNoPosition);
4434
4435 auto loop = factory()->NewForStatement(NULL, RelocInfo::kNoPosition);
4436
4437 auto argument_index =
4438 parameters.scope->NewTemporary(ast_value_factory()->empty_string());
4439 auto init = factory()->NewExpressionStatement(
4440 factory()->NewAssignment(
4441 Token::INIT_VAR, factory()->NewVariableProxy(argument_index),
4442 factory()->NewSmiLiteral(i, RelocInfo::kNoPosition),
4443 RelocInfo::kNoPosition),
4444 RelocInfo::kNoPosition);
4445
4446 auto empty_arguments = new (zone()) ZoneList<Expression*>(0, zone());
4447
4448 // $arguments_index < arguments.length
4449 auto cond = factory()->NewCompareOperation(
4450 Token::LT, factory()->NewVariableProxy(argument_index),
4451 factory()->NewCallRuntime(Runtime::kInlineArgumentsLength,
4452 empty_arguments, RelocInfo::kNoPosition),
4453 RelocInfo::kNoPosition);
4454
4455 // ++argument_index
4456 auto next = factory()->NewExpressionStatement(
4457 factory()->NewCountOperation(
4458 Token::INC, true, factory()->NewVariableProxy(argument_index),
4459 RelocInfo::kNoPosition),
4460 RelocInfo::kNoPosition);
4461
4462 // %_Arguments($arguments_index)
4463 auto arguments_args = new (zone()) ZoneList<Expression*>(1, zone());
4464 arguments_args->Add(factory()->NewVariableProxy(argument_index), zone());
4465
4466 // %AppendElement($rest, %_Arguments($arguments_index))
4467 auto append_element_args = new (zone()) ZoneList<Expression*>(2, zone());
4468
4469 append_element_args->Add(factory()->NewVariableProxy(var), zone());
4470 append_element_args->Add(
4471 factory()->NewCallRuntime(Runtime::kInlineArguments, arguments_args,
4472 RelocInfo::kNoPosition),
4473 zone());
4474
4475 auto body = factory()->NewExpressionStatement(
4476 factory()->NewCallRuntime(Runtime::kAppendElement,
4477 append_element_args,
4478 RelocInfo::kNoPosition),
4479 RelocInfo::kNoPosition);
4480
4481 loop->Initialize(init, cond, next, body);
4482
4483 init_block->AddStatement(
4484 factory()->NewExpressionStatement(init_array, RelocInfo::kNoPosition),
4485 zone());
4486
4487 init_block->AddStatement(loop, zone());
4488
4489 descriptor.initialization_pos = pos;
4407 } 4490 }
4408 4491
4409 Scope* param_scope = scope_; 4492 Scope* param_scope = scope_;
4410 Block* param_block = init_block; 4493 Block* param_block = init_block;
4411 if (!parameter.is_simple() && scope_->calls_sloppy_eval()) { 4494 if (!parameter.is_simple() && scope_->calls_sloppy_eval()) {
4412 param_scope = NewScope(scope_, BLOCK_SCOPE); 4495 param_scope = NewScope(scope_, BLOCK_SCOPE);
4413 param_scope->set_is_declaration_scope(); 4496 param_scope->set_is_declaration_scope();
4414 param_scope->set_start_position(parameter.pattern->position()); 4497 param_scope->set_start_position(parameter.pattern->position());
4415 param_scope->set_end_position(RelocInfo::kNoPosition); 4498 param_scope->set_end_position(RelocInfo::kNoPosition);
4416 param_scope->RecordEvalCall(); 4499 param_scope->RecordEvalCall();
(...skipping 1696 matching lines...) Expand 10 before | Expand all | Expand 10 after
6113 6196
6114 Expression* Parser::SpreadCallNew(Expression* function, 6197 Expression* Parser::SpreadCallNew(Expression* function,
6115 ZoneList<v8::internal::Expression*>* args, 6198 ZoneList<v8::internal::Expression*>* args,
6116 int pos) { 6199 int pos) {
6117 args->InsertAt(0, function, zone()); 6200 args->InsertAt(0, function, zone());
6118 6201
6119 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos); 6202 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos);
6120 } 6203 }
6121 } // namespace internal 6204 } // namespace internal
6122 } // namespace v8 6205 } // 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