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/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/ast/ast-expression-rewriter.h" | 10 #include "src/ast/ast-expression-rewriter.h" |
(...skipping 946 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 // NewUnresolved references in current scope. Entrer arrow function | 957 // NewUnresolved references in current scope. Entrer arrow function |
958 // scope for formal parameter parsing. | 958 // scope for formal parameter parsing. |
959 BlockState block_state(&scope_state_, scope); | 959 BlockState block_state(&scope_state_, scope); |
960 if (Check(Token::LPAREN)) { | 960 if (Check(Token::LPAREN)) { |
961 // '(' StrictFormalParameters ')' | 961 // '(' StrictFormalParameters ')' |
962 ParseFormalParameterList(&formals, &ok); | 962 ParseFormalParameterList(&formals, &ok); |
963 if (ok) ok = Check(Token::RPAREN); | 963 if (ok) ok = Check(Token::RPAREN); |
964 } else { | 964 } else { |
965 // BindingIdentifier | 965 // BindingIdentifier |
966 ParseFormalParameter(&formals, &ok); | 966 ParseFormalParameter(&formals, &ok); |
967 if (ok) DeclareFormalParameter(formals.scope, formals.at(0)); | 967 if (ok) DeclareFormalParameters(formals.scope, formals.params); |
968 } | 968 } |
969 } | 969 } |
970 | 970 |
971 if (ok) { | 971 if (ok) { |
972 checkpoint.Restore(&formals.materialized_literals_count); | 972 checkpoint.Restore(&formals.materialized_literals_count); |
973 // Pass `accept_IN=true` to ParseArrowFunctionLiteral --- This should | 973 // Pass `accept_IN=true` to ParseArrowFunctionLiteral --- This should |
974 // not be observable, or else the preparser would have failed. | 974 // not be observable, or else the preparser would have failed. |
975 Expression* expression = ParseArrowFunctionLiteral(true, formals, &ok); | 975 Expression* expression = ParseArrowFunctionLiteral(true, formals, &ok); |
976 if (ok) { | 976 if (ok) { |
977 // Scanning must end at the same position that was recorded | 977 // Scanning must end at the same position that was recorded |
(...skipping 1491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2469 if (parameters->arity > Code::kMaxArguments) { | 2469 if (parameters->arity > Code::kMaxArguments) { |
2470 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); | 2470 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); |
2471 *ok = false; | 2471 *ok = false; |
2472 return; | 2472 return; |
2473 } | 2473 } |
2474 | 2474 |
2475 ExpressionClassifier classifier(this); | 2475 ExpressionClassifier classifier(this); |
2476 if (!parameters->is_simple) { | 2476 if (!parameters->is_simple) { |
2477 this->classifier()->RecordNonSimpleParameter(); | 2477 this->classifier()->RecordNonSimpleParameter(); |
2478 } | 2478 } |
2479 for (int i = 0; i < parameters->arity; ++i) { | 2479 DeclareFormalParameters(parameters->scope, parameters->params); |
2480 auto parameter = parameters->at(i); | 2480 if (!this->classifier() |
2481 DeclareFormalParameter(parameters->scope, parameter); | 2481 ->is_valid_formal_parameter_list_without_duplicates()) { |
2482 if (!this->classifier() | 2482 *duplicate_loc = |
2483 ->is_valid_formal_parameter_list_without_duplicates() && | 2483 this->classifier()->duplicate_formal_parameter_error().location; |
2484 !duplicate_loc->IsValid()) { | |
2485 *duplicate_loc = | |
2486 this->classifier()->duplicate_formal_parameter_error().location; | |
2487 } | |
2488 } | 2484 } |
2489 DCHECK_EQ(parameters->is_simple, parameters->scope->has_simple_parameters()); | 2485 DCHECK_EQ(parameters->is_simple, parameters->scope->has_simple_parameters()); |
2490 } | 2486 } |
2491 | 2487 |
2492 void Parser::ReindexLiterals(const ParserFormalParameters& parameters) { | 2488 void Parser::ReindexLiterals(const ParserFormalParameters& parameters) { |
2493 if (function_state_->materialized_literal_count() > 0) { | 2489 if (function_state_->materialized_literal_count() > 0) { |
2494 AstLiteralReindexer reindexer; | 2490 AstLiteralReindexer reindexer; |
2495 | 2491 |
2496 for (const auto p : parameters.params) { | 2492 for (auto p : parameters.params) { |
2497 if (p.pattern != nullptr) reindexer.Reindex(p.pattern); | 2493 if (p->pattern != nullptr) reindexer.Reindex(p->pattern); |
2498 if (p.initializer != nullptr) reindexer.Reindex(p.initializer); | 2494 if (p->initializer != nullptr) reindexer.Reindex(p->initializer); |
2499 } | 2495 } |
2500 | 2496 |
2501 DCHECK(reindexer.count() <= function_state_->materialized_literal_count()); | 2497 DCHECK(reindexer.count() <= function_state_->materialized_literal_count()); |
2502 } | 2498 } |
2503 } | 2499 } |
2504 | 2500 |
2505 void Parser::PrepareGeneratorVariables(FunctionState* function_state) { | 2501 void Parser::PrepareGeneratorVariables(FunctionState* function_state) { |
2506 // For generators, allocating variables in contexts is currently a win because | 2502 // For generators, allocating variables in contexts is currently a win because |
2507 // it minimizes the work needed to suspend and resume an activation. The | 2503 // it minimizes the work needed to suspend and resume an activation. The |
2508 // code produced for generators relies on this forced context allocation, but | 2504 // code produced for generators relies on this forced context allocation, but |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2917 InitializerRewriter rewriter(stack_limit_, expr, this, scope); | 2913 InitializerRewriter rewriter(stack_limit_, expr, this, scope); |
2918 rewriter.Run(); | 2914 rewriter.Run(); |
2919 } | 2915 } |
2920 | 2916 |
2921 | 2917 |
2922 Block* Parser::BuildParameterInitializationBlock( | 2918 Block* Parser::BuildParameterInitializationBlock( |
2923 const ParserFormalParameters& parameters, bool* ok) { | 2919 const ParserFormalParameters& parameters, bool* ok) { |
2924 DCHECK(!parameters.is_simple); | 2920 DCHECK(!parameters.is_simple); |
2925 DCHECK(scope()->is_function_scope()); | 2921 DCHECK(scope()->is_function_scope()); |
2926 Block* init_block = factory()->NewBlock(NULL, 1, true, kNoSourcePosition); | 2922 Block* init_block = factory()->NewBlock(NULL, 1, true, kNoSourcePosition); |
2927 for (int i = 0; i < parameters.params.length(); ++i) { | 2923 int index = 0; |
2928 auto parameter = parameters.params[i]; | 2924 for (auto parameter : parameters.params) { |
2929 if (parameter.is_rest && parameter.pattern->IsVariableProxy()) break; | 2925 if (parameter->is_rest && parameter->pattern->IsVariableProxy()) break; |
2930 DeclarationDescriptor descriptor; | 2926 DeclarationDescriptor descriptor; |
2931 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; | 2927 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; |
2932 descriptor.scope = scope(); | 2928 descriptor.scope = scope(); |
2933 descriptor.hoist_scope = nullptr; | 2929 descriptor.hoist_scope = nullptr; |
2934 descriptor.mode = LET; | 2930 descriptor.mode = LET; |
2935 descriptor.declaration_pos = parameter.pattern->position(); | 2931 descriptor.declaration_pos = parameter->pattern->position(); |
2936 // The position that will be used by the AssignmentExpression | 2932 // The position that will be used by the AssignmentExpression |
2937 // which copies from the temp parameter to the pattern. | 2933 // which copies from the temp parameter to the pattern. |
2938 // | 2934 // |
2939 // TODO(adamk): Should this be kNoSourcePosition, since | 2935 // TODO(adamk): Should this be kNoSourcePosition, since |
2940 // it's just copying from a temp var to the real param var? | 2936 // it's just copying from a temp var to the real param var? |
2941 descriptor.initialization_pos = parameter.pattern->position(); | 2937 descriptor.initialization_pos = parameter->pattern->position(); |
2942 Expression* initial_value = | 2938 Expression* initial_value = |
2943 factory()->NewVariableProxy(parameters.scope->parameter(i)); | 2939 factory()->NewVariableProxy(parameters.scope->parameter(index)); |
2944 if (parameter.initializer != nullptr) { | 2940 if (parameter->initializer != nullptr) { |
2945 // IS_UNDEFINED($param) ? initializer : $param | 2941 // IS_UNDEFINED($param) ? initializer : $param |
2946 | 2942 |
2947 // Ensure initializer is rewritten | 2943 // Ensure initializer is rewritten |
2948 RewriteParameterInitializer(parameter.initializer, scope()); | 2944 RewriteParameterInitializer(parameter->initializer, scope()); |
2949 | 2945 |
2950 auto condition = factory()->NewCompareOperation( | 2946 auto condition = factory()->NewCompareOperation( |
2951 Token::EQ_STRICT, | 2947 Token::EQ_STRICT, |
2952 factory()->NewVariableProxy(parameters.scope->parameter(i)), | 2948 factory()->NewVariableProxy(parameters.scope->parameter(index)), |
2953 factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition); | 2949 factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition); |
2954 initial_value = factory()->NewConditional( | 2950 initial_value = factory()->NewConditional( |
2955 condition, parameter.initializer, initial_value, kNoSourcePosition); | 2951 condition, parameter->initializer, initial_value, kNoSourcePosition); |
2956 descriptor.initialization_pos = parameter.initializer->position(); | 2952 descriptor.initialization_pos = parameter->initializer->position(); |
2957 } | 2953 } |
2958 | 2954 |
2959 Scope* param_scope = scope(); | 2955 Scope* param_scope = scope(); |
2960 Block* param_block = init_block; | 2956 Block* param_block = init_block; |
2961 if (!parameter.is_simple() && scope()->calls_sloppy_eval()) { | 2957 if (!parameter->is_simple() && scope()->calls_sloppy_eval()) { |
2962 param_scope = NewVarblockScope(); | 2958 param_scope = NewVarblockScope(); |
2963 param_scope->set_start_position(descriptor.initialization_pos); | 2959 param_scope->set_start_position(descriptor.initialization_pos); |
2964 param_scope->set_end_position(parameter.initializer_end_position); | 2960 param_scope->set_end_position(parameter->initializer_end_position); |
2965 param_scope->RecordEvalCall(); | 2961 param_scope->RecordEvalCall(); |
2966 param_block = factory()->NewBlock(NULL, 8, true, kNoSourcePosition); | 2962 param_block = factory()->NewBlock(NULL, 8, true, kNoSourcePosition); |
2967 param_block->set_scope(param_scope); | 2963 param_block->set_scope(param_scope); |
2968 descriptor.hoist_scope = scope(); | 2964 descriptor.hoist_scope = scope(); |
2969 // Pass the appropriate scope in so that PatternRewriter can appropriately | 2965 // Pass the appropriate scope in so that PatternRewriter can appropriately |
2970 // rewrite inner initializers of the pattern to param_scope | 2966 // rewrite inner initializers of the pattern to param_scope |
2971 descriptor.scope = param_scope; | 2967 descriptor.scope = param_scope; |
2972 // Rewrite the outer initializer to point to param_scope | 2968 // Rewrite the outer initializer to point to param_scope |
2973 ReparentParameterExpressionScope(stack_limit(), initial_value, | 2969 ReparentParameterExpressionScope(stack_limit(), initial_value, |
2974 param_scope); | 2970 param_scope); |
2975 } | 2971 } |
2976 | 2972 |
2977 BlockState block_state(&scope_state_, param_scope); | 2973 BlockState block_state(&scope_state_, param_scope); |
2978 DeclarationParsingResult::Declaration decl( | 2974 DeclarationParsingResult::Declaration decl( |
2979 parameter.pattern, parameter.initializer_end_position, initial_value); | 2975 parameter->pattern, parameter->initializer_end_position, initial_value); |
2980 PatternRewriter::DeclareAndInitializeVariables( | 2976 PatternRewriter::DeclareAndInitializeVariables( |
2981 this, param_block, &descriptor, &decl, nullptr, CHECK_OK); | 2977 this, param_block, &descriptor, &decl, nullptr, CHECK_OK); |
2982 | 2978 |
2983 if (param_block != init_block) { | 2979 if (param_block != init_block) { |
2984 param_scope = block_state.FinalizedBlockScope(); | 2980 param_scope = block_state.FinalizedBlockScope(); |
2985 if (param_scope != nullptr) { | 2981 if (param_scope != nullptr) { |
2986 CheckConflictingVarDeclarations(param_scope, CHECK_OK); | 2982 CheckConflictingVarDeclarations(param_scope, CHECK_OK); |
2987 } | 2983 } |
2988 init_block->statements()->Add(param_block, zone()); | 2984 init_block->statements()->Add(param_block, zone()); |
2989 } | 2985 } |
| 2986 ++index; |
2990 } | 2987 } |
2991 return init_block; | 2988 return init_block; |
2992 } | 2989 } |
2993 | 2990 |
2994 Block* Parser::BuildRejectPromiseOnException(Block* inner_block, bool* ok) { | 2991 Block* Parser::BuildRejectPromiseOnException(Block* inner_block, bool* ok) { |
2995 // .promise = %AsyncFunctionPromiseCreate(); | 2992 // .promise = %AsyncFunctionPromiseCreate(); |
2996 // try { | 2993 // try { |
2997 // <inner_block> | 2994 // <inner_block> |
2998 // } catch (.catch) { | 2995 // } catch (.catch) { |
2999 // %RejectPromise(.promise, .catch); | 2996 // %RejectPromise(.promise, .catch); |
(...skipping 2460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5460 | 5457 |
5461 return final_loop; | 5458 return final_loop; |
5462 } | 5459 } |
5463 | 5460 |
5464 #undef CHECK_OK | 5461 #undef CHECK_OK |
5465 #undef CHECK_OK_VOID | 5462 #undef CHECK_OK_VOID |
5466 #undef CHECK_FAILED | 5463 #undef CHECK_FAILED |
5467 | 5464 |
5468 } // namespace internal | 5465 } // namespace internal |
5469 } // namespace v8 | 5466 } // namespace v8 |
OLD | NEW |