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.h" | 10 #include "src/ast/ast.h" |
(...skipping 2273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2284 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | 2284 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); |
2285 proxy->var()->set_initializer_position(position()); | 2285 proxy->var()->set_initializer_position(position()); |
2286 Assignment* assignment = | 2286 Assignment* assignment = |
2287 factory()->NewAssignment(Token::INIT, proxy, value, pos); | 2287 factory()->NewAssignment(Token::INIT, proxy, value, pos); |
2288 Statement* assignment_statement = | 2288 Statement* assignment_statement = |
2289 factory()->NewExpressionStatement(assignment, kNoSourcePosition); | 2289 factory()->NewExpressionStatement(assignment, kNoSourcePosition); |
2290 if (names) names->Add(variable_name, zone()); | 2290 if (names) names->Add(variable_name, zone()); |
2291 return assignment_statement; | 2291 return assignment_statement; |
2292 } | 2292 } |
2293 | 2293 |
2294 | 2294 Block* Parser::ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok) { |
2295 Block* Parser::ParseBlock(ZoneList<const AstRawString*>* labels, | |
2296 bool finalize_block_scope, bool* ok) { | |
2297 // The harmony mode uses block elements instead of statements. | 2295 // The harmony mode uses block elements instead of statements. |
2298 // | 2296 // |
2299 // Block :: | 2297 // Block :: |
2300 // '{' StatementList '}' | 2298 // '{' StatementList '}' |
2301 | 2299 |
2302 // Construct block expecting 16 statements. | 2300 // Construct block expecting 16 statements. |
2303 Block* body = factory()->NewBlock(labels, 16, false, kNoSourcePosition); | 2301 Block* body = factory()->NewBlock(labels, 16, false, kNoSourcePosition); |
2304 Scope* block_scope = NewScope(BLOCK_SCOPE); | 2302 Scope* block_scope = NewScope(BLOCK_SCOPE); |
2305 | 2303 |
2306 // Parse the statements and collect escaping labels. | 2304 // Parse the statements and collect escaping labels. |
2307 Expect(Token::LBRACE, CHECK_OK); | 2305 Expect(Token::LBRACE, CHECK_OK); |
2308 block_scope->set_start_position(scanner()->location().beg_pos); | 2306 block_scope->set_start_position(scanner()->location().beg_pos); |
2309 { | 2307 { |
2310 BlockState block_state(&scope_state_, block_scope); | 2308 BlockState block_state(&scope_state_, block_scope); |
2311 Target target(&this->target_stack_, body); | 2309 Target target(&this->target_stack_, body); |
2312 | 2310 |
2313 while (peek() != Token::RBRACE) { | 2311 while (peek() != Token::RBRACE) { |
2314 Statement* stat = ParseStatementListItem(CHECK_OK); | 2312 Statement* stat = ParseStatementListItem(CHECK_OK); |
2315 if (stat && !stat->IsEmpty()) { | 2313 if (stat && !stat->IsEmpty()) { |
2316 body->statements()->Add(stat, zone()); | 2314 body->statements()->Add(stat, zone()); |
2317 } | 2315 } |
2318 } | 2316 } |
2319 } | 2317 } |
2320 Expect(Token::RBRACE, CHECK_OK); | 2318 Expect(Token::RBRACE, CHECK_OK); |
2321 block_scope->set_end_position(scanner()->location().end_pos); | 2319 block_scope->set_end_position(scanner()->location().end_pos); |
2322 if (finalize_block_scope) { | 2320 block_scope = block_scope->FinalizeBlockScope(); |
2323 block_scope = block_scope->FinalizeBlockScope(); | |
2324 } | |
2325 body->set_scope(block_scope); | 2321 body->set_scope(block_scope); |
2326 return body; | 2322 return body; |
2327 } | 2323 } |
2328 | 2324 |
2329 | 2325 |
2330 Block* Parser::ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok) { | |
2331 return ParseBlock(labels, true, ok); | |
2332 } | |
2333 | |
2334 | |
2335 Block* Parser::DeclarationParsingResult::BuildInitializationBlock( | 2326 Block* Parser::DeclarationParsingResult::BuildInitializationBlock( |
2336 ZoneList<const AstRawString*>* names, bool* ok) { | 2327 ZoneList<const AstRawString*>* names, bool* ok) { |
2337 Block* result = descriptor.parser->factory()->NewBlock( | 2328 Block* result = descriptor.parser->factory()->NewBlock( |
2338 NULL, 1, true, descriptor.declaration_pos); | 2329 NULL, 1, true, descriptor.declaration_pos); |
2339 for (auto declaration : declarations) { | 2330 for (auto declaration : declarations) { |
2340 PatternRewriter::DeclareAndInitializeVariables( | 2331 PatternRewriter::DeclareAndInitializeVariables( |
2341 result, &descriptor, &declaration, names, CHECK_OK); | 2332 result, &descriptor, &declaration, names, CHECK_OK); |
2342 } | 2333 } |
2343 return result; | 2334 return result; |
2344 } | 2335 } |
(...skipping 1869 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4214 } | 4205 } |
4215 | 4206 |
4216 DoExpression* Parser::ParseDoExpression(bool* ok) { | 4207 DoExpression* Parser::ParseDoExpression(bool* ok) { |
4217 // AssignmentExpression :: | 4208 // AssignmentExpression :: |
4218 // do '{' StatementList '}' | 4209 // do '{' StatementList '}' |
4219 int pos = peek_position(); | 4210 int pos = peek_position(); |
4220 | 4211 |
4221 Expect(Token::DO, CHECK_OK); | 4212 Expect(Token::DO, CHECK_OK); |
4222 Variable* result = | 4213 Variable* result = |
4223 scope()->NewTemporary(ast_value_factory()->dot_result_string()); | 4214 scope()->NewTemporary(ast_value_factory()->dot_result_string()); |
4224 Block* block = ParseBlock(nullptr, false, CHECK_OK); | 4215 Block* block = ParseBlock(nullptr, CHECK_OK); |
4225 DoExpression* expr = factory()->NewDoExpression(block, result, pos); | 4216 DoExpression* expr = factory()->NewDoExpression(block, result, pos); |
4226 if (!Rewriter::Rewrite(this, expr, ast_value_factory())) { | 4217 if (!Rewriter::Rewrite(this, scope()->ClosureScope(), expr, |
| 4218 ast_value_factory())) { |
4227 *ok = false; | 4219 *ok = false; |
4228 return nullptr; | 4220 return nullptr; |
4229 } | 4221 } |
4230 block->set_scope(block->scope()->FinalizeBlockScope()); | |
4231 return expr; | 4222 return expr; |
4232 } | 4223 } |
4233 | 4224 |
4234 void ParserTraits::ParseArrowFunctionFormalParameterList( | 4225 void ParserTraits::ParseArrowFunctionFormalParameterList( |
4235 ParserFormalParameters* parameters, Expression* expr, | 4226 ParserFormalParameters* parameters, Expression* expr, |
4236 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, | 4227 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, |
4237 const Scope::Snapshot& scope_snapshot, bool* ok) { | 4228 const Scope::Snapshot& scope_snapshot, bool* ok) { |
4238 if (expr->IsEmptyParentheses()) return; | 4229 if (expr->IsEmptyParentheses()) return; |
4239 | 4230 |
4240 ParseArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos, | 4231 ParseArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos, |
(...skipping 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5098 | 5089 |
5099 if (property_name != ast_value_factory()->constructor_string()) { | 5090 if (property_name != ast_value_factory()->constructor_string()) { |
5100 SetFunctionNameFromPropertyName(property, property_name); | 5091 SetFunctionNameFromPropertyName(property, property_name); |
5101 } | 5092 } |
5102 } | 5093 } |
5103 | 5094 |
5104 Expect(Token::RBRACE, CHECK_OK); | 5095 Expect(Token::RBRACE, CHECK_OK); |
5105 int end_pos = scanner()->location().end_pos; | 5096 int end_pos = scanner()->location().end_pos; |
5106 | 5097 |
5107 if (constructor == NULL) { | 5098 if (constructor == NULL) { |
5108 DCHECK_EQ(this->scope(), block_scope); | 5099 DCHECK_EQ(scope(), block_scope); |
5109 constructor = DefaultConstructor(name, has_extends, pos, end_pos, | 5100 constructor = DefaultConstructor(name, has_extends, pos, end_pos, |
5110 block_scope->language_mode()); | 5101 block_scope->language_mode()); |
5111 } | 5102 } |
5112 | 5103 |
5113 // Note that we do not finalize this block scope because it is | 5104 // Note that we do not finalize this block scope because it is |
5114 // used as a sentinel value indicating an anonymous class. | 5105 // used as a sentinel value indicating an anonymous class. |
5115 block_scope->set_end_position(end_pos); | 5106 block_scope->set_end_position(end_pos); |
5116 | 5107 |
5117 if (name != NULL) { | 5108 if (name != NULL) { |
5118 DCHECK_NOT_NULL(proxy); | 5109 DCHECK_NOT_NULL(proxy); |
5119 proxy->var()->set_initializer_position(end_pos); | 5110 proxy->var()->set_initializer_position(end_pos); |
5120 } | 5111 } |
5121 | 5112 |
5122 Block* do_block = factory()->NewBlock(nullptr, 1, false, pos); | 5113 Block* do_block = factory()->NewBlock(nullptr, 1, false, pos); |
5123 do_block->set_scope(block_scope); | 5114 do_block->set_scope(block_scope); |
5124 Variable* result_var = | 5115 Variable* result_var = |
5125 block_scope->NewTemporary(ast_value_factory()->empty_string()); | 5116 block_scope->NewTemporary(ast_value_factory()->empty_string()); |
5126 DoExpression* do_expr = factory()->NewDoExpression(do_block, result_var, pos); | 5117 DoExpression* do_expr = factory()->NewDoExpression(do_block, result_var, pos); |
5127 | 5118 |
5128 ClassLiteral* class_literal = factory()->NewClassLiteral( | 5119 ClassLiteral* class_literal = factory()->NewClassLiteral( |
5129 proxy, extends, constructor, properties, pos, end_pos); | 5120 proxy, extends, constructor, properties, pos, end_pos); |
5130 | 5121 |
5131 do_block->statements()->Add( | 5122 do_block->statements()->Add( |
5132 factory()->NewExpressionStatement(class_literal, pos), zone()); | 5123 factory()->NewExpressionStatement(class_literal, pos), zone()); |
5133 do_expr->set_represented_function(constructor); | 5124 do_expr->set_represented_function(constructor); |
5134 Rewriter::Rewrite(this, do_expr, ast_value_factory()); | 5125 Rewriter::Rewrite(this, scope()->ClosureScope(), do_expr, |
| 5126 ast_value_factory()); |
5135 | 5127 |
5136 return do_expr; | 5128 return do_expr; |
5137 } | 5129 } |
5138 | 5130 |
5139 | 5131 |
5140 Expression* Parser::ParseV8Intrinsic(bool* ok) { | 5132 Expression* Parser::ParseV8Intrinsic(bool* ok) { |
5141 // CallRuntime :: | 5133 // CallRuntime :: |
5142 // '%' Identifier Arguments | 5134 // '%' Identifier Arguments |
5143 | 5135 |
5144 int pos = peek_position(); | 5136 int pos = peek_position(); |
(...skipping 1488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6633 do_block_->statements()->Add(validate_iterator, zone); | 6625 do_block_->statements()->Add(validate_iterator, zone); |
6634 do_block_->statements()->Add(loop, zone); | 6626 do_block_->statements()->Add(loop, zone); |
6635 do_block_->statements()->Add(maybe_return_value, zone); | 6627 do_block_->statements()->Add(maybe_return_value, zone); |
6636 | 6628 |
6637 Block* do_block = factory->NewBlock(nullptr, 2, false, nopos); | 6629 Block* do_block = factory->NewBlock(nullptr, 2, false, nopos); |
6638 do_block->statements()->Add(do_block_, zone); | 6630 do_block->statements()->Add(do_block_, zone); |
6639 do_block->statements()->Add(get_value, zone); | 6631 do_block->statements()->Add(get_value, zone); |
6640 | 6632 |
6641 Variable* dot_result = scope->NewTemporary(avfactory->dot_result_string()); | 6633 Variable* dot_result = scope->NewTemporary(avfactory->dot_result_string()); |
6642 yield_star = factory->NewDoExpression(do_block, dot_result, nopos); | 6634 yield_star = factory->NewDoExpression(do_block, dot_result, nopos); |
6643 Rewriter::Rewrite(parser_, yield_star, avfactory); | 6635 Rewriter::Rewrite(parser_, scope->ClosureScope(), yield_star, avfactory); |
6644 } | 6636 } |
6645 | 6637 |
6646 return yield_star; | 6638 return yield_star; |
6647 } | 6639 } |
6648 | 6640 |
6649 Statement* ParserTraits::CheckCallable(Variable* var, Expression* error, | 6641 Statement* ParserTraits::CheckCallable(Variable* var, Expression* error, |
6650 int pos) { | 6642 int pos) { |
6651 auto factory = parser_->factory(); | 6643 auto factory = parser_->factory(); |
6652 auto avfactory = parser_->ast_value_factory(); | 6644 auto avfactory = parser_->ast_value_factory(); |
6653 const int nopos = kNoSourcePosition; | 6645 const int nopos = kNoSourcePosition; |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7110 node->Print(Isolate::Current()); | 7102 node->Print(Isolate::Current()); |
7111 } | 7103 } |
7112 #endif // DEBUG | 7104 #endif // DEBUG |
7113 | 7105 |
7114 #undef CHECK_OK | 7106 #undef CHECK_OK |
7115 #undef CHECK_OK_VOID | 7107 #undef CHECK_OK_VOID |
7116 #undef CHECK_FAILED | 7108 #undef CHECK_FAILED |
7117 | 7109 |
7118 } // namespace internal | 7110 } // namespace internal |
7119 } // namespace v8 | 7111 } // namespace v8 |
OLD | NEW |