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 3921 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3932 ParseArrowFunctionFormalParameters(parameters, left, params_loc, ok); | 3932 ParseArrowFunctionFormalParameters(parameters, left, params_loc, ok); |
3933 if (!*ok) return; | 3933 if (!*ok) return; |
3934 // LHS of comma expression should be unparenthesized. | 3934 // LHS of comma expression should be unparenthesized. |
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 int start_pos = expr->position(); | |
3943 | |
3942 if (is_rest) { | 3944 if (is_rest) { |
3943 expr = expr->AsSpread()->expression(); | 3945 expr = expr->AsSpread()->expression(); |
3944 parameters->has_rest = true; | 3946 parameters->has_rest = true; |
3947 | |
3948 if (!expr->IsVariableProxy()) { | |
3949 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); | |
3950 *ok = false; | |
3951 return; | |
3952 } | |
3953 | |
3954 expr = parser_->factory()->NewRestParameter( | |
3955 expr->AsVariableProxy(), | |
3956 parser_->function_state_->NextMaterializedLiteralIndex(), start_pos); | |
3957 ++parameters->materialized_literals_count; | |
3945 } | 3958 } |
3946 if (parameters->is_simple) { | 3959 if (parameters->is_simple) { |
3947 parameters->is_simple = !is_rest && expr->IsVariableProxy(); | 3960 parameters->is_simple = expr->IsVariableProxy(); |
3948 } | 3961 } |
3949 | 3962 |
3950 Expression* initializer = nullptr; | 3963 Expression* initializer = nullptr; |
3951 if (expr->IsVariableProxy()) { | 3964 if (expr->IsVariableProxy()) { |
3952 // When the formal parameter was originally seen, it was parsed as a | 3965 // 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 | 3966 // VariableProxy and recorded as unresolved in the scope. Here we undo that |
3954 // parse-time side-effect for parameters that are single-names (not | 3967 // parse-time side-effect for parameters that are single-names (not |
3955 // patterns; for patterns that happens uniformly in | 3968 // patterns; for patterns that happens uniformly in |
3956 // PatternRewriter::VisitVariableProxy). | 3969 // PatternRewriter::VisitVariableProxy). |
3957 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); | 3970 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); |
(...skipping 28 matching lines...) Expand all Loading... | |
3986 if (!duplicate_loc->IsValid()) { | 3999 if (!duplicate_loc->IsValid()) { |
3987 *duplicate_loc = classifier.duplicate_formal_parameter_error().location; | 4000 *duplicate_loc = classifier.duplicate_formal_parameter_error().location; |
3988 } | 4001 } |
3989 } | 4002 } |
3990 DCHECK_EQ(parameters->is_simple, parameters->scope->has_simple_parameters()); | 4003 DCHECK_EQ(parameters->is_simple, parameters->scope->has_simple_parameters()); |
3991 } | 4004 } |
3992 | 4005 |
3993 | 4006 |
3994 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) { | 4007 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) { |
3995 if (parser_->function_state_->materialized_literal_count() > 0) { | 4008 if (parser_->function_state_->materialized_literal_count() > 0) { |
4009 // Rest parameter array is always the first literal. | |
3996 AstLiteralReindexer reindexer; | 4010 AstLiteralReindexer reindexer; |
3997 | 4011 |
3998 for (const auto p : parameters.params) { | 4012 for (const auto p : parameters.params) { |
adamk
2015/08/28 23:26:37
So is const-correctness the only thing keeping you
caitp (gmail)
2015/08/28 23:58:01
Yeah, it's true that doing it all here could get r
adamk
2015/08/29 01:01:07
The current thing is cheating quite a bit, const-w
caitp (gmail)
2015/08/29 15:51:59
Done.
| |
3999 if (p.pattern != nullptr) reindexer.Reindex(p.pattern); | 4013 if (p.pattern != nullptr) reindexer.Reindex(p.pattern); |
4000 } | 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 Loading... | |
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.has_rest) { | |
4186 DCHECK(formals.materialized_literals_count > 0); | |
4187 materialized_literal_count += formals.materialized_literals_count; | |
4188 } | |
4189 | |
4170 if (bookmark.HasBeenReset()) { | 4190 if (bookmark.HasBeenReset()) { |
4171 // Trigger eager (re-)parsing, just below this block. | 4191 // Trigger eager (re-)parsing, just below this block. |
4172 is_lazily_parsed = false; | 4192 is_lazily_parsed = false; |
4173 | 4193 |
4174 // This is probably an initialization function. Inform the compiler it | 4194 // This is probably an initialization function. Inform the compiler it |
4175 // should also eager-compile this function, and that we expect it to be | 4195 // should also eager-compile this function, and that we expect it to be |
4176 // used once. | 4196 // used once. |
4177 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; | 4197 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; |
4178 should_be_used_once_hint = true; | 4198 should_be_used_once_hint = true; |
4179 } | 4199 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4224 | 4244 |
4225 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 4245 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
4226 function_name, ast_value_factory(), scope, body, | 4246 function_name, ast_value_factory(), scope, body, |
4227 materialized_literal_count, expected_property_count, arity, | 4247 materialized_literal_count, expected_property_count, arity, |
4228 duplicate_parameters, function_type, FunctionLiteral::kIsFunction, | 4248 duplicate_parameters, function_type, FunctionLiteral::kIsFunction, |
4229 eager_compile_hint, kind, pos); | 4249 eager_compile_hint, kind, pos); |
4230 function_literal->set_function_token_position(function_token_pos); | 4250 function_literal->set_function_token_position(function_token_pos); |
4231 if (should_be_used_once_hint) | 4251 if (should_be_used_once_hint) |
4232 function_literal->set_should_be_used_once_hint(); | 4252 function_literal->set_should_be_used_once_hint(); |
4233 | 4253 |
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); | 4254 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
4240 return function_literal; | 4255 return function_literal; |
4241 } | 4256 } |
4242 | 4257 |
4243 | 4258 |
4244 void Parser::SkipLazyFunctionBody(int* materialized_literal_count, | 4259 void Parser::SkipLazyFunctionBody(int* materialized_literal_count, |
4245 int* expected_property_count, bool* ok, | 4260 int* expected_property_count, bool* ok, |
4246 Scanner::BookmarkScope* bookmark) { | 4261 Scanner::BookmarkScope* bookmark) { |
4247 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet()); | 4262 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet()); |
4248 if (produce_cached_parse_data()) CHECK(log_); | 4263 if (produce_cached_parse_data()) CHECK(log_); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4363 | 4378 |
4364 | 4379 |
4365 Block* Parser::BuildParameterInitializationBlock( | 4380 Block* Parser::BuildParameterInitializationBlock( |
4366 const ParserFormalParameters& parameters, bool* ok) { | 4381 const ParserFormalParameters& parameters, bool* ok) { |
4367 DCHECK(!parameters.is_simple); | 4382 DCHECK(!parameters.is_simple); |
4368 DCHECK(scope_->is_function_scope()); | 4383 DCHECK(scope_->is_function_scope()); |
4369 Block* init_block = | 4384 Block* init_block = |
4370 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); | 4385 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); |
4371 for (int i = 0; i < parameters.params.length(); ++i) { | 4386 for (int i = 0; i < parameters.params.length(); ++i) { |
4372 auto parameter = parameters.params[i]; | 4387 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; | 4388 DeclarationDescriptor descriptor; |
4376 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; | 4389 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; |
4377 descriptor.parser = this; | 4390 descriptor.parser = this; |
4378 descriptor.declaration_scope = scope_; | 4391 descriptor.declaration_scope = scope_; |
4379 descriptor.scope = scope_; | 4392 descriptor.scope = scope_; |
4380 descriptor.hoist_scope = nullptr; | 4393 descriptor.hoist_scope = nullptr; |
4381 descriptor.mode = LET; | 4394 descriptor.mode = LET; |
4382 descriptor.is_const = false; | 4395 descriptor.is_const = false; |
4396 descriptor.is_rest = parameter.is_rest; | |
4383 descriptor.needs_init = true; | 4397 descriptor.needs_init = true; |
4384 descriptor.declaration_pos = parameter.pattern->position(); | 4398 descriptor.declaration_pos = parameter.pattern->position(); |
4385 descriptor.initialization_pos = parameter.pattern->position(); | 4399 descriptor.initialization_pos = parameter.pattern->position(); |
4386 descriptor.init_op = Token::INIT_LET; | 4400 descriptor.init_op = Token::INIT_LET; |
4387 Expression* initial_value = | 4401 Expression* initial_value = |
4388 factory()->NewVariableProxy(parameters.scope->parameter(i)); | 4402 factory()->NewVariableProxy(parameters.scope->parameter(i)); |
4389 if (parameter.initializer != nullptr) { | 4403 if (parameter.initializer != nullptr) { |
4390 // IS_UNDEFINED($param) ? initializer : $param | 4404 // IS_UNDEFINED($param) ? initializer : $param |
4391 auto condition = factory()->NewCompareOperation( | 4405 auto condition = factory()->NewCompareOperation( |
4392 Token::EQ_STRICT, | 4406 Token::EQ_STRICT, |
4393 factory()->NewVariableProxy(parameters.scope->parameter(i)), | 4407 factory()->NewVariableProxy(parameters.scope->parameter(i)), |
4394 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), | 4408 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), |
4395 RelocInfo::kNoPosition); | 4409 RelocInfo::kNoPosition); |
4396 initial_value = factory()->NewConditional( | 4410 initial_value = factory()->NewConditional( |
4397 condition, parameter.initializer, initial_value, | 4411 condition, parameter.initializer, initial_value, |
4398 RelocInfo::kNoPosition); | 4412 RelocInfo::kNoPosition); |
4399 descriptor.initialization_pos = parameter.initializer->position(); | 4413 descriptor.initialization_pos = parameter.initializer->position(); |
4400 } | 4414 } |
4401 | 4415 |
4416 if (parameter.is_rest) { | |
adamk
2015/08/28 23:26:37
Please add a comment here with the full desugaring
caitp (gmail)
2015/08/29 15:51:59
Done.
| |
4417 DCHECK_NULL(parameter.initializer); | |
4418 DCHECK(parameter.pattern->IsRestParameter()); | |
4419 DCHECK_EQ(i, parameters.params.length() - 1); | |
4420 | |
4421 auto rest_parameter = parameter.pattern->AsRestParameter(); | |
4422 int pos = rest_parameter->position(); | |
4423 auto var = parameters.scope->parameter(i); | |
4424 auto empty_values = new (zone()) ZoneList<Expression*>(0, zone()); | |
4425 auto empty_array = factory()->NewArrayLiteral( | |
4426 empty_values, rest_parameter->literal_index(), | |
4427 is_strong(language_mode()), RelocInfo::kNoPosition); | |
4428 | |
4429 auto init_array = factory()->NewAssignment( | |
4430 Token::INIT_VAR, factory()->NewVariableProxy(var), empty_array, | |
4431 RelocInfo::kNoPosition); | |
4432 | |
4433 auto loop = factory()->NewForStatement(NULL, RelocInfo::kNoPosition); | |
4434 | |
4435 auto argument_index = | |
4436 parameters.scope->NewTemporary(ast_value_factory()->empty_string()); | |
4437 auto init = factory()->NewExpressionStatement( | |
4438 factory()->NewAssignment( | |
4439 Token::INIT_VAR, factory()->NewVariableProxy(argument_index), | |
4440 factory()->NewSmiLiteral(i, RelocInfo::kNoPosition), | |
4441 RelocInfo::kNoPosition), | |
4442 RelocInfo::kNoPosition); | |
4443 | |
4444 auto empty_arguments = new (zone()) ZoneList<Expression*>(0, zone()); | |
4445 | |
4446 // $arguments_index < arguments.length | |
4447 auto cond = factory()->NewCompareOperation( | |
4448 Token::LT, factory()->NewVariableProxy(argument_index), | |
4449 factory()->NewCallRuntime(Runtime::kInlineArgumentsLength, | |
4450 empty_arguments, RelocInfo::kNoPosition), | |
4451 RelocInfo::kNoPosition); | |
4452 | |
4453 // ++argument_index | |
4454 auto next = factory()->NewExpressionStatement( | |
4455 factory()->NewCountOperation( | |
4456 Token::INC, true, factory()->NewVariableProxy(argument_index), | |
4457 RelocInfo::kNoPosition), | |
4458 RelocInfo::kNoPosition); | |
4459 | |
4460 // %_Arguments($arguments_index) | |
4461 auto arguments_args = new (zone()) ZoneList<Expression*>(1, zone()); | |
4462 arguments_args->Add(factory()->NewVariableProxy(argument_index), zone()); | |
4463 | |
4464 // %AppendElement($rest, %_Arguments($arguments_index)) | |
4465 auto append_element_args = new (zone()) ZoneList<Expression*>(2, zone()); | |
4466 | |
4467 append_element_args->Add(factory()->NewVariableProxy(var), zone()); | |
4468 append_element_args->Add( | |
4469 factory()->NewCallRuntime(Runtime::kInlineArguments, arguments_args, | |
4470 RelocInfo::kNoPosition), | |
4471 zone()); | |
4472 | |
4473 auto body = factory()->NewExpressionStatement( | |
4474 factory()->NewCallRuntime(Runtime::kAppendElement, | |
4475 append_element_args, | |
4476 RelocInfo::kNoPosition), | |
4477 RelocInfo::kNoPosition); | |
4478 | |
4479 loop->Initialize(init, cond, next, body); | |
4480 | |
4481 init_block->AddStatement( | |
4482 factory()->NewExpressionStatement(init_array, RelocInfo::kNoPosition), | |
4483 zone()); | |
4484 | |
4485 init_block->AddStatement(loop, zone()); | |
4486 | |
4487 descriptor.initialization_pos = pos; | |
4488 } | |
4489 | |
4402 Scope* param_scope = scope_; | 4490 Scope* param_scope = scope_; |
4403 Block* param_block = init_block; | 4491 Block* param_block = init_block; |
4404 if (!parameter.is_simple() && scope_->calls_sloppy_eval()) { | 4492 if (!parameter.is_simple() && scope_->calls_sloppy_eval()) { |
4405 param_scope = NewScope(scope_, BLOCK_SCOPE); | 4493 param_scope = NewScope(scope_, BLOCK_SCOPE); |
4406 param_scope->set_is_declaration_scope(); | 4494 param_scope->set_is_declaration_scope(); |
4407 param_scope->set_start_position(parameter.pattern->position()); | 4495 param_scope->set_start_position(parameter.pattern->position()); |
4408 param_scope->set_end_position(RelocInfo::kNoPosition); | 4496 param_scope->set_end_position(RelocInfo::kNoPosition); |
4409 param_scope->RecordEvalCall(); | 4497 param_scope->RecordEvalCall(); |
4410 param_block = factory()->NewBlock(NULL, 8, true, RelocInfo::kNoPosition); | 4498 param_block = factory()->NewBlock(NULL, 8, true, RelocInfo::kNoPosition); |
4411 param_block->set_scope(param_scope); | 4499 param_block->set_scope(param_scope); |
(...skipping 1694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6106 | 6194 |
6107 Expression* Parser::SpreadCallNew(Expression* function, | 6195 Expression* Parser::SpreadCallNew(Expression* function, |
6108 ZoneList<v8::internal::Expression*>* args, | 6196 ZoneList<v8::internal::Expression*>* args, |
6109 int pos) { | 6197 int pos) { |
6110 args->InsertAt(0, function, zone()); | 6198 args->InsertAt(0, function, zone()); |
6111 | 6199 |
6112 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos); | 6200 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos); |
6113 } | 6201 } |
6114 } // namespace internal | 6202 } // namespace internal |
6115 } // namespace v8 | 6203 } // namespace v8 |
OLD | NEW |