OLD | NEW |
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 3939 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3950 expr = right; | 3950 expr = right; |
3951 } | 3951 } |
3952 | 3952 |
3953 // Only the right-most expression may be a rest parameter. | 3953 // Only the right-most expression may be a rest parameter. |
3954 DCHECK(!parameters->has_rest); | 3954 DCHECK(!parameters->has_rest); |
3955 | 3955 |
3956 bool is_rest = expr->IsSpread(); | 3956 bool is_rest = expr->IsSpread(); |
3957 if (is_rest) { | 3957 if (is_rest) { |
3958 expr = expr->AsSpread()->expression(); | 3958 expr = expr->AsSpread()->expression(); |
3959 parameters->has_rest = true; | 3959 parameters->has_rest = true; |
| 3960 parameters->rest_array_literal_index = |
| 3961 parser_->function_state_->NextMaterializedLiteralIndex(); |
| 3962 ++parameters->materialized_literals_count; |
3960 } | 3963 } |
3961 if (parameters->is_simple) { | 3964 if (parameters->is_simple) { |
3962 parameters->is_simple = !is_rest && expr->IsVariableProxy(); | 3965 parameters->is_simple = !is_rest && expr->IsVariableProxy(); |
3963 } | 3966 } |
3964 | 3967 |
3965 Expression* initializer = nullptr; | 3968 Expression* initializer = nullptr; |
3966 if (expr->IsVariableProxy()) { | 3969 if (expr->IsVariableProxy()) { |
3967 // When the formal parameter was originally seen, it was parsed as a | 3970 // When the formal parameter was originally seen, it was parsed as a |
3968 // VariableProxy and recorded as unresolved in the scope. Here we undo that | 3971 // VariableProxy and recorded as unresolved in the scope. Here we undo that |
3969 // parse-time side-effect for parameters that are single-names (not | 3972 // parse-time side-effect for parameters that are single-names (not |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4006 } | 4009 } |
4007 | 4010 |
4008 | 4011 |
4009 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) { | 4012 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) { |
4010 if (parser_->function_state_->materialized_literal_count() > 0) { | 4013 if (parser_->function_state_->materialized_literal_count() > 0) { |
4011 AstLiteralReindexer reindexer; | 4014 AstLiteralReindexer reindexer; |
4012 | 4015 |
4013 for (const auto p : parameters.params) { | 4016 for (const auto p : parameters.params) { |
4014 if (p.pattern != nullptr) reindexer.Reindex(p.pattern); | 4017 if (p.pattern != nullptr) reindexer.Reindex(p.pattern); |
4015 } | 4018 } |
| 4019 |
| 4020 if (parameters.has_rest) { |
| 4021 parameters.rest_array_literal_index = reindexer.NextIndex(); |
| 4022 } |
| 4023 |
4016 DCHECK(reindexer.count() <= | 4024 DCHECK(reindexer.count() <= |
4017 parser_->function_state_->materialized_literal_count()); | 4025 parser_->function_state_->materialized_literal_count()); |
4018 } | 4026 } |
4019 } | 4027 } |
4020 | 4028 |
4021 | 4029 |
4022 FunctionLiteral* Parser::ParseFunctionLiteral( | 4030 FunctionLiteral* Parser::ParseFunctionLiteral( |
4023 const AstRawString* function_name, Scanner::Location function_name_location, | 4031 const AstRawString* function_name, Scanner::Location function_name_location, |
4024 FunctionNameValidity function_name_validity, FunctionKind kind, | 4032 FunctionNameValidity function_name_validity, FunctionKind kind, |
4025 int function_token_pos, FunctionLiteral::FunctionType function_type, | 4033 int function_token_pos, FunctionLiteral::FunctionType function_type, |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4175 // parsing if it suspect that wasn't a good idea. If so, or if we didn't | 4183 // parsing if it suspect that wasn't a good idea. If so, or if we didn't |
4176 // try to lazy parse in the first place, we'll have to parse eagerly. | 4184 // try to lazy parse in the first place, we'll have to parse eagerly. |
4177 Scanner::BookmarkScope bookmark(scanner()); | 4185 Scanner::BookmarkScope bookmark(scanner()); |
4178 if (is_lazily_parsed) { | 4186 if (is_lazily_parsed) { |
4179 Scanner::BookmarkScope* maybe_bookmark = | 4187 Scanner::BookmarkScope* maybe_bookmark = |
4180 bookmark.Set() ? &bookmark : nullptr; | 4188 bookmark.Set() ? &bookmark : nullptr; |
4181 SkipLazyFunctionBody(&materialized_literal_count, | 4189 SkipLazyFunctionBody(&materialized_literal_count, |
4182 &expected_property_count, /*CHECK_OK*/ ok, | 4190 &expected_property_count, /*CHECK_OK*/ ok, |
4183 maybe_bookmark); | 4191 maybe_bookmark); |
4184 | 4192 |
| 4193 if (formals.materialized_literals_count > 0) { |
| 4194 materialized_literal_count += formals.materialized_literals_count; |
| 4195 } |
| 4196 |
4185 if (bookmark.HasBeenReset()) { | 4197 if (bookmark.HasBeenReset()) { |
4186 // Trigger eager (re-)parsing, just below this block. | 4198 // Trigger eager (re-)parsing, just below this block. |
4187 is_lazily_parsed = false; | 4199 is_lazily_parsed = false; |
4188 | 4200 |
4189 // This is probably an initialization function. Inform the compiler it | 4201 // This is probably an initialization function. Inform the compiler it |
4190 // should also eager-compile this function, and that we expect it to be | 4202 // should also eager-compile this function, and that we expect it to be |
4191 // used once. | 4203 // used once. |
4192 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; | 4204 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; |
4193 should_be_used_once_hint = true; | 4205 should_be_used_once_hint = true; |
4194 } | 4206 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4239 | 4251 |
4240 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 4252 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
4241 function_name, ast_value_factory(), scope, body, | 4253 function_name, ast_value_factory(), scope, body, |
4242 materialized_literal_count, expected_property_count, arity, | 4254 materialized_literal_count, expected_property_count, arity, |
4243 duplicate_parameters, function_type, FunctionLiteral::kIsFunction, | 4255 duplicate_parameters, function_type, FunctionLiteral::kIsFunction, |
4244 eager_compile_hint, kind, pos); | 4256 eager_compile_hint, kind, pos); |
4245 function_literal->set_function_token_position(function_token_pos); | 4257 function_literal->set_function_token_position(function_token_pos); |
4246 if (should_be_used_once_hint) | 4258 if (should_be_used_once_hint) |
4247 function_literal->set_should_be_used_once_hint(); | 4259 function_literal->set_should_be_used_once_hint(); |
4248 | 4260 |
4249 if (scope->has_rest_parameter()) { | |
4250 // TODO(caitp): enable optimization of functions with rest params | |
4251 function_literal->set_dont_optimize_reason(kRestParameter); | |
4252 } | |
4253 | |
4254 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); | 4261 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
4255 return function_literal; | 4262 return function_literal; |
4256 } | 4263 } |
4257 | 4264 |
4258 | 4265 |
4259 void Parser::SkipLazyFunctionBody(int* materialized_literal_count, | 4266 void Parser::SkipLazyFunctionBody(int* materialized_literal_count, |
4260 int* expected_property_count, bool* ok, | 4267 int* expected_property_count, bool* ok, |
4261 Scanner::BookmarkScope* bookmark) { | 4268 Scanner::BookmarkScope* bookmark) { |
4262 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet()); | 4269 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet()); |
4263 if (produce_cached_parse_data()) CHECK(log_); | 4270 if (produce_cached_parse_data()) CHECK(log_); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4378 | 4385 |
4379 | 4386 |
4380 Block* Parser::BuildParameterInitializationBlock( | 4387 Block* Parser::BuildParameterInitializationBlock( |
4381 const ParserFormalParameters& parameters, bool* ok) { | 4388 const ParserFormalParameters& parameters, bool* ok) { |
4382 DCHECK(!parameters.is_simple); | 4389 DCHECK(!parameters.is_simple); |
4383 DCHECK(scope_->is_function_scope()); | 4390 DCHECK(scope_->is_function_scope()); |
4384 Block* init_block = | 4391 Block* init_block = |
4385 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); | 4392 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); |
4386 for (int i = 0; i < parameters.params.length(); ++i) { | 4393 for (int i = 0; i < parameters.params.length(); ++i) { |
4387 auto parameter = parameters.params[i]; | 4394 auto parameter = parameters.params[i]; |
4388 // TODO(caitp,rossberg): Remove special handling for rest once desugared. | |
4389 if (parameter.is_rest) break; | |
4390 DeclarationDescriptor descriptor; | 4395 DeclarationDescriptor descriptor; |
4391 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; | 4396 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; |
4392 descriptor.parser = this; | 4397 descriptor.parser = this; |
4393 descriptor.declaration_scope = scope_; | 4398 descriptor.declaration_scope = scope_; |
4394 descriptor.scope = scope_; | 4399 descriptor.scope = scope_; |
4395 descriptor.hoist_scope = nullptr; | 4400 descriptor.hoist_scope = nullptr; |
4396 descriptor.mode = LET; | 4401 descriptor.mode = LET; |
4397 descriptor.is_const = false; | 4402 descriptor.is_const = false; |
4398 descriptor.needs_init = true; | 4403 descriptor.needs_init = true; |
4399 descriptor.declaration_pos = parameter.pattern->position(); | 4404 descriptor.declaration_pos = parameter.pattern->position(); |
4400 descriptor.initialization_pos = parameter.pattern->position(); | 4405 descriptor.initialization_pos = parameter.pattern->position(); |
4401 descriptor.init_op = Token::INIT_LET; | 4406 descriptor.init_op = Token::INIT_LET; |
4402 Expression* initial_value = | 4407 Expression* initial_value = |
4403 factory()->NewVariableProxy(parameters.scope->parameter(i)); | 4408 factory()->NewVariableProxy(parameters.scope->parameter(i)); |
4404 if (parameter.initializer != nullptr) { | 4409 if (parameter.initializer != nullptr) { |
4405 // IS_UNDEFINED($param) ? initializer : $param | 4410 // IS_UNDEFINED($param) ? initializer : $param |
| 4411 DCHECK(!parameter.is_rest); |
4406 auto condition = factory()->NewCompareOperation( | 4412 auto condition = factory()->NewCompareOperation( |
4407 Token::EQ_STRICT, | 4413 Token::EQ_STRICT, |
4408 factory()->NewVariableProxy(parameters.scope->parameter(i)), | 4414 factory()->NewVariableProxy(parameters.scope->parameter(i)), |
4409 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), | 4415 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), |
4410 RelocInfo::kNoPosition); | 4416 RelocInfo::kNoPosition); |
4411 initial_value = factory()->NewConditional( | 4417 initial_value = factory()->NewConditional( |
4412 condition, parameter.initializer, initial_value, | 4418 condition, parameter.initializer, initial_value, |
4413 RelocInfo::kNoPosition); | 4419 RelocInfo::kNoPosition); |
4414 descriptor.initialization_pos = parameter.initializer->position(); | 4420 descriptor.initialization_pos = parameter.initializer->position(); |
| 4421 } else if (parameter.is_rest) { |
| 4422 // $rest = []; |
| 4423 // for (var $argument_index = $rest_index; |
| 4424 // $argument_index < %_ArgumentsLength(); |
| 4425 // ++$argument_index) { |
| 4426 // %AppendElement($rest, %_Arguments($argument_index)); |
| 4427 // } |
| 4428 // let <param> = $rest; |
| 4429 DCHECK(parameter.pattern->IsVariableProxy()); |
| 4430 DCHECK_EQ(i, parameters.params.length() - 1); |
| 4431 |
| 4432 int pos = parameter.pattern->position(); |
| 4433 Variable* temp_var = parameters.scope->parameter(i); |
| 4434 auto empty_values = new (zone()) ZoneList<Expression*>(0, zone()); |
| 4435 auto empty_array = factory()->NewArrayLiteral( |
| 4436 empty_values, parameters.rest_array_literal_index, |
| 4437 is_strong(language_mode()), RelocInfo::kNoPosition); |
| 4438 |
| 4439 auto init_array = factory()->NewAssignment( |
| 4440 Token::INIT_VAR, factory()->NewVariableProxy(temp_var), empty_array, |
| 4441 RelocInfo::kNoPosition); |
| 4442 |
| 4443 auto loop = factory()->NewForStatement(NULL, RelocInfo::kNoPosition); |
| 4444 |
| 4445 auto argument_index = |
| 4446 parameters.scope->NewTemporary(ast_value_factory()->empty_string()); |
| 4447 auto init = factory()->NewExpressionStatement( |
| 4448 factory()->NewAssignment( |
| 4449 Token::INIT_VAR, factory()->NewVariableProxy(argument_index), |
| 4450 factory()->NewSmiLiteral(i, RelocInfo::kNoPosition), |
| 4451 RelocInfo::kNoPosition), |
| 4452 RelocInfo::kNoPosition); |
| 4453 |
| 4454 auto empty_arguments = new (zone()) ZoneList<Expression*>(0, zone()); |
| 4455 |
| 4456 // $arguments_index < arguments.length |
| 4457 auto cond = factory()->NewCompareOperation( |
| 4458 Token::LT, factory()->NewVariableProxy(argument_index), |
| 4459 factory()->NewCallRuntime(Runtime::kInlineArgumentsLength, |
| 4460 empty_arguments, RelocInfo::kNoPosition), |
| 4461 RelocInfo::kNoPosition); |
| 4462 |
| 4463 // ++argument_index |
| 4464 auto next = factory()->NewExpressionStatement( |
| 4465 factory()->NewCountOperation( |
| 4466 Token::INC, true, factory()->NewVariableProxy(argument_index), |
| 4467 RelocInfo::kNoPosition), |
| 4468 RelocInfo::kNoPosition); |
| 4469 |
| 4470 // %_Arguments($arguments_index) |
| 4471 auto arguments_args = new (zone()) ZoneList<Expression*>(1, zone()); |
| 4472 arguments_args->Add(factory()->NewVariableProxy(argument_index), zone()); |
| 4473 |
| 4474 // %AppendElement($rest, %_Arguments($arguments_index)) |
| 4475 auto append_element_args = new (zone()) ZoneList<Expression*>(2, zone()); |
| 4476 |
| 4477 append_element_args->Add(factory()->NewVariableProxy(temp_var), zone()); |
| 4478 append_element_args->Add( |
| 4479 factory()->NewCallRuntime(Runtime::kInlineArguments, arguments_args, |
| 4480 RelocInfo::kNoPosition), |
| 4481 zone()); |
| 4482 |
| 4483 auto body = factory()->NewExpressionStatement( |
| 4484 factory()->NewCallRuntime(Runtime::kAppendElement, |
| 4485 append_element_args, |
| 4486 RelocInfo::kNoPosition), |
| 4487 RelocInfo::kNoPosition); |
| 4488 |
| 4489 loop->Initialize(init, cond, next, body); |
| 4490 |
| 4491 init_block->AddStatement( |
| 4492 factory()->NewExpressionStatement(init_array, RelocInfo::kNoPosition), |
| 4493 zone()); |
| 4494 |
| 4495 init_block->AddStatement(loop, zone()); |
| 4496 |
| 4497 descriptor.initialization_pos = pos; |
4415 } | 4498 } |
4416 | 4499 |
4417 Scope* param_scope = scope_; | 4500 Scope* param_scope = scope_; |
4418 Block* param_block = init_block; | 4501 Block* param_block = init_block; |
4419 if (!parameter.is_simple() && scope_->calls_sloppy_eval()) { | 4502 if (!parameter.is_simple() && scope_->calls_sloppy_eval()) { |
4420 param_scope = NewScope(scope_, BLOCK_SCOPE); | 4503 param_scope = NewScope(scope_, BLOCK_SCOPE); |
4421 param_scope->set_is_declaration_scope(); | 4504 param_scope->set_is_declaration_scope(); |
4422 param_scope->set_start_position(parameter.pattern->position()); | 4505 param_scope->set_start_position(parameter.pattern->position()); |
4423 param_scope->set_end_position(RelocInfo::kNoPosition); | 4506 param_scope->set_end_position(RelocInfo::kNoPosition); |
4424 param_scope->RecordEvalCall(); | 4507 param_scope->RecordEvalCall(); |
(...skipping 1696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6121 | 6204 |
6122 Expression* Parser::SpreadCallNew(Expression* function, | 6205 Expression* Parser::SpreadCallNew(Expression* function, |
6123 ZoneList<v8::internal::Expression*>* args, | 6206 ZoneList<v8::internal::Expression*>* args, |
6124 int pos) { | 6207 int pos) { |
6125 args->InsertAt(0, function, zone()); | 6208 args->InsertAt(0, function, zone()); |
6126 | 6209 |
6127 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos); | 6210 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos); |
6128 } | 6211 } |
6129 } // namespace internal | 6212 } // namespace internal |
6130 } // namespace v8 | 6213 } // namespace v8 |
OLD | NEW |