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/v8.h" | 5 #include "src/v8.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 1185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1196 // scope for formal parameter parsing. | 1196 // scope for formal parameter parsing. |
1197 BlockState block_state(&scope_, scope); | 1197 BlockState block_state(&scope_, scope); |
1198 if (Check(Token::LPAREN)) { | 1198 if (Check(Token::LPAREN)) { |
1199 // '(' StrictFormalParameters ')' | 1199 // '(' StrictFormalParameters ')' |
1200 ParseFormalParameterList(&formals, &formals_classifier, &ok); | 1200 ParseFormalParameterList(&formals, &formals_classifier, &ok); |
1201 if (ok) ok = Check(Token::RPAREN); | 1201 if (ok) ok = Check(Token::RPAREN); |
1202 } else { | 1202 } else { |
1203 // BindingIdentifier | 1203 // BindingIdentifier |
1204 const bool is_rest = false; | 1204 const bool is_rest = false; |
1205 ParseFormalParameter(is_rest, &formals, &formals_classifier, &ok); | 1205 ParseFormalParameter(is_rest, &formals, &formals_classifier, &ok); |
| 1206 if (ok) { |
| 1207 DeclareFormalParameter( |
| 1208 formals.scope, formals.at(0), formals.is_simple, |
| 1209 &formals_classifier); |
| 1210 } |
1206 } | 1211 } |
1207 } | 1212 } |
1208 | 1213 |
1209 if (ok) { | 1214 if (ok) { |
1210 checkpoint.Restore(&formals.materialized_literals_count); | 1215 checkpoint.Restore(&formals.materialized_literals_count); |
1211 Expression* expression = | 1216 Expression* expression = |
1212 ParseArrowFunctionLiteral(formals, formals_classifier, &ok); | 1217 ParseArrowFunctionLiteral(formals, formals_classifier, &ok); |
1213 if (ok) { | 1218 if (ok) { |
1214 // Scanning must end at the same position that was recorded | 1219 // Scanning must end at the same position that was recorded |
1215 // previously. If not, parsing has been interrupted due to a stack | 1220 // previously. If not, parsing has been interrupted due to a stack |
(...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2216 | 2221 |
2217 // Even if we're not at the top-level of the global or a function | 2222 // Even if we're not at the top-level of the global or a function |
2218 // scope, we treat it as such and introduce the function with its | 2223 // scope, we treat it as such and introduce the function with its |
2219 // initial value upon entering the corresponding scope. | 2224 // initial value upon entering the corresponding scope. |
2220 // In ES6, a function behaves as a lexical binding, except in | 2225 // In ES6, a function behaves as a lexical binding, except in |
2221 // a script scope, or the initial scope of eval or another function. | 2226 // a script scope, or the initial scope of eval or another function. |
2222 VariableMode mode = | 2227 VariableMode mode = |
2223 is_strong(language_mode()) | 2228 is_strong(language_mode()) |
2224 ? CONST | 2229 ? CONST |
2225 : (is_strict(language_mode()) || allow_harmony_sloppy()) && | 2230 : (is_strict(language_mode()) || allow_harmony_sloppy()) && |
2226 !(scope_->is_script_scope() || scope_->is_eval_scope() || | 2231 !scope_->is_declaration_scope() ? LET : VAR; |
2227 scope_->is_function_scope()) | |
2228 ? LET | |
2229 : VAR; | |
2230 VariableProxy* proxy = NewUnresolved(name, mode); | 2232 VariableProxy* proxy = NewUnresolved(name, mode); |
2231 Declaration* declaration = | 2233 Declaration* declaration = |
2232 factory()->NewFunctionDeclaration(proxy, mode, fun, scope_, pos); | 2234 factory()->NewFunctionDeclaration(proxy, mode, fun, scope_, pos); |
2233 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | 2235 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); |
2234 if (names) names->Add(name, zone()); | 2236 if (names) names->Add(name, zone()); |
2235 return factory()->NewEmptyStatement(RelocInfo::kNoPosition); | 2237 return factory()->NewEmptyStatement(RelocInfo::kNoPosition); |
2236 } | 2238 } |
2237 | 2239 |
2238 | 2240 |
2239 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names, | 2241 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names, |
(...skipping 1607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3847 | 3849 |
3848 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { | 3850 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { |
3849 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); | 3851 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); |
3850 } | 3852 } |
3851 | 3853 |
3852 | 3854 |
3853 void ParserTraits::ParseArrowFunctionFormalParameters( | 3855 void ParserTraits::ParseArrowFunctionFormalParameters( |
3854 ParserFormalParameters* parameters, Expression* expr, | 3856 ParserFormalParameters* parameters, Expression* expr, |
3855 const Scanner::Location& params_loc, | 3857 const Scanner::Location& params_loc, |
3856 Scanner::Location* duplicate_loc, bool* ok) { | 3858 Scanner::Location* duplicate_loc, bool* ok) { |
3857 if (parameters->arity >= Code::kMaxArguments) { | 3859 if (parameters->Arity() >= Code::kMaxArguments) { |
3858 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); | 3860 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); |
3859 *ok = false; | 3861 *ok = false; |
3860 return; | 3862 return; |
3861 } | 3863 } |
3862 | 3864 |
3863 // ArrowFunctionFormals :: | 3865 // ArrowFunctionFormals :: |
3864 // Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail) | 3866 // Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail) |
3865 // Tail | 3867 // Tail |
3866 // NonTailArrowFunctionFormals :: | 3868 // NonTailArrowFunctionFormals :: |
3867 // Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy) | 3869 // Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3901 | 3903 |
3902 if (expr->IsVariableProxy()) { | 3904 if (expr->IsVariableProxy()) { |
3903 // When the formal parameter was originally seen, it was parsed as a | 3905 // When the formal parameter was originally seen, it was parsed as a |
3904 // VariableProxy and recorded as unresolved in the scope. Here we undo that | 3906 // VariableProxy and recorded as unresolved in the scope. Here we undo that |
3905 // parse-time side-effect for parameters that are single-names (not | 3907 // parse-time side-effect for parameters that are single-names (not |
3906 // patterns; for patterns that happens uniformly in | 3908 // patterns; for patterns that happens uniformly in |
3907 // PatternRewriter::VisitVariableProxy). | 3909 // PatternRewriter::VisitVariableProxy). |
3908 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); | 3910 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); |
3909 } | 3911 } |
3910 | 3912 |
3911 ++parameters->arity; | 3913 AddFormalParameter(parameters, expr, is_rest); |
3912 ExpressionClassifier classifier; | 3914 } |
3913 DeclareFormalParameter(parameters, expr, is_rest, &classifier); | 3915 |
3914 if (!duplicate_loc->IsValid()) { | 3916 |
3915 *duplicate_loc = classifier.duplicate_formal_parameter_error().location; | 3917 void ParserTraits::ParseArrowFunctionFormalParameterList( |
| 3918 ParserFormalParameters* parameters, Expression* expr, |
| 3919 const Scanner::Location& params_loc, |
| 3920 Scanner::Location* duplicate_loc, bool* ok) { |
| 3921 ParseArrowFunctionFormalParameters(parameters, expr, params_loc, |
| 3922 duplicate_loc, ok); |
| 3923 if (!*ok) return; |
| 3924 |
| 3925 for (int i = 0; i < parameters->Arity(); ++i) { |
| 3926 auto parameter = parameters->at(i); |
| 3927 ExpressionClassifier classifier; |
| 3928 DeclareFormalParameter( |
| 3929 parameters->scope, parameter, parameters->is_simple, &classifier); |
| 3930 if (!duplicate_loc->IsValid()) { |
| 3931 *duplicate_loc = classifier.duplicate_formal_parameter_error().location; |
| 3932 } |
3916 } | 3933 } |
3917 } | 3934 } |
3918 | 3935 |
3919 | 3936 |
3920 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) { | 3937 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) { |
3921 if (parser_->function_state_->materialized_literal_count() > 0) { | 3938 if (parser_->function_state_->materialized_literal_count() > 0) { |
3922 AstLiteralReindexer reindexer; | 3939 AstLiteralReindexer reindexer; |
3923 | 3940 |
3924 for (const auto p : parameters.params) { | 3941 for (const auto p : parameters.params) { |
3925 if (p.pattern != nullptr) reindexer.Reindex(p.pattern); | 3942 if (p.pattern != nullptr) reindexer.Reindex(p.pattern); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4025 Variable* temp = scope_->NewTemporary( | 4042 Variable* temp = scope_->NewTemporary( |
4026 ast_value_factory()->dot_generator_object_string()); | 4043 ast_value_factory()->dot_generator_object_string()); |
4027 function_state.set_generator_object_variable(temp); | 4044 function_state.set_generator_object_variable(temp); |
4028 } | 4045 } |
4029 | 4046 |
4030 Expect(Token::LPAREN, CHECK_OK); | 4047 Expect(Token::LPAREN, CHECK_OK); |
4031 int start_position = scanner()->location().beg_pos; | 4048 int start_position = scanner()->location().beg_pos; |
4032 scope_->set_start_position(start_position); | 4049 scope_->set_start_position(start_position); |
4033 ParserFormalParameters formals(scope); | 4050 ParserFormalParameters formals(scope); |
4034 ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK); | 4051 ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK); |
4035 arity = formals.arity; | 4052 arity = formals.Arity(); |
4036 DCHECK(arity == formals.params.length()); | |
4037 Expect(Token::RPAREN, CHECK_OK); | 4053 Expect(Token::RPAREN, CHECK_OK); |
4038 int formals_end_position = scanner()->location().end_pos; | 4054 int formals_end_position = scanner()->location().end_pos; |
4039 | 4055 |
4040 CheckArityRestrictions(arity, arity_restriction, | 4056 CheckArityRestrictions(arity, arity_restriction, |
4041 formals.has_rest, start_position, | 4057 formals.has_rest, start_position, |
4042 formals_end_position, CHECK_OK); | 4058 formals_end_position, CHECK_OK); |
4043 Expect(Token::LBRACE, CHECK_OK); | 4059 Expect(Token::LBRACE, CHECK_OK); |
4044 | 4060 |
4045 // Determine if the function can be parsed lazily. Lazy parsing is different | 4061 // Determine if the function can be parsed lazily. Lazy parsing is different |
4046 // from lazy compilation; we need to parse more eagerly than we compile. | 4062 // from lazy compilation; we need to parse more eagerly than we compile. |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4292 | 4308 |
4293 | 4309 |
4294 Block* Parser::BuildParameterInitializationBlock( | 4310 Block* Parser::BuildParameterInitializationBlock( |
4295 const ParserFormalParameters& parameters, bool* ok) { | 4311 const ParserFormalParameters& parameters, bool* ok) { |
4296 DCHECK(!parameters.is_simple); | 4312 DCHECK(!parameters.is_simple); |
4297 DCHECK(scope_->is_function_scope()); | 4313 DCHECK(scope_->is_function_scope()); |
4298 Block* init_block = | 4314 Block* init_block = |
4299 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); | 4315 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); |
4300 for (int i = 0; i < parameters.params.length(); ++i) { | 4316 for (int i = 0; i < parameters.params.length(); ++i) { |
4301 auto parameter = parameters.params[i]; | 4317 auto parameter = parameters.params[i]; |
4302 if (parameter.pattern == nullptr) continue; | 4318 // TODO(caitp,rossberg): Remove special handling for rest once desugared. |
| 4319 if (parameter.is_rest) break; |
4303 DeclarationDescriptor descriptor; | 4320 DeclarationDescriptor descriptor; |
4304 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; | 4321 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; |
4305 descriptor.parser = this; | 4322 descriptor.parser = this; |
4306 descriptor.declaration_scope = scope_; | 4323 descriptor.declaration_scope = scope_; |
4307 descriptor.scope = scope_; | 4324 descriptor.scope = scope_; |
4308 descriptor.mode = LET; | 4325 descriptor.mode = LET; |
4309 descriptor.is_const = false; | 4326 descriptor.is_const = false; |
4310 descriptor.needs_init = true; | 4327 descriptor.needs_init = true; |
4311 descriptor.declaration_pos = parameter.pattern->position(); | 4328 descriptor.declaration_pos = parameter.pattern->position(); |
4312 descriptor.initialization_pos = parameter.pattern->position(); | 4329 descriptor.initialization_pos = parameter.pattern->position(); |
(...skipping 1687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6000 Expression* Parser::SpreadCallNew(Expression* function, | 6017 Expression* Parser::SpreadCallNew(Expression* function, |
6001 ZoneList<v8::internal::Expression*>* args, | 6018 ZoneList<v8::internal::Expression*>* args, |
6002 int pos) { | 6019 int pos) { |
6003 args->InsertAt(0, function, zone()); | 6020 args->InsertAt(0, function, zone()); |
6004 | 6021 |
6005 return factory()->NewCallRuntime( | 6022 return factory()->NewCallRuntime( |
6006 ast_value_factory()->reflect_construct_string(), NULL, args, pos); | 6023 ast_value_factory()->reflect_construct_string(), NULL, args, pos); |
6007 } | 6024 } |
6008 } // namespace internal | 6025 } // namespace internal |
6009 } // namespace v8 | 6026 } // namespace v8 |
OLD | NEW |