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/rewriter.h" | 5 #include "src/parsing/rewriter.h" |
6 | 6 |
7 #include "src/ast/ast.h" | 7 #include "src/ast/ast.h" |
8 #include "src/ast/scopes.h" | 8 #include "src/ast/scopes.h" |
9 #include "src/parsing/parser.h" | 9 #include "src/parsing/parser.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 | 13 |
14 class Processor final : public AstVisitor<Processor> { | 14 class Processor final : public AstVisitor<Processor> { |
15 public: | 15 public: |
16 Processor(Isolate* isolate, DeclarationScope* closure_scope, Variable* result, | 16 Processor(Isolate* isolate, Scope* closure_scope, Variable* result, |
17 AstValueFactory* ast_value_factory) | 17 AstValueFactory* ast_value_factory) |
18 : result_(result), | 18 : result_(result), |
19 result_assigned_(false), | 19 result_assigned_(false), |
20 replacement_(nullptr), | 20 replacement_(nullptr), |
21 is_set_(false), | 21 is_set_(false), |
22 zone_(ast_value_factory->zone()), | 22 zone_(ast_value_factory->zone()), |
23 closure_scope_(closure_scope), | 23 closure_scope_(closure_scope), |
24 factory_(ast_value_factory) { | 24 factory_(ast_value_factory) { |
25 DCHECK_EQ(closure_scope, closure_scope->GetClosureScope()); | 25 DCHECK_EQ(closure_scope, closure_scope->ClosureScope()); |
26 InitializeAstVisitor(isolate); | 26 InitializeAstVisitor(isolate); |
27 } | 27 } |
28 | 28 |
29 Processor(Parser* parser, DeclarationScope* closure_scope, Variable* result, | 29 Processor(Parser* parser, Scope* closure_scope, Variable* result, |
30 AstValueFactory* ast_value_factory) | 30 AstValueFactory* ast_value_factory) |
31 : result_(result), | 31 : result_(result), |
32 result_assigned_(false), | 32 result_assigned_(false), |
33 replacement_(nullptr), | 33 replacement_(nullptr), |
34 is_set_(false), | 34 is_set_(false), |
35 zone_(ast_value_factory->zone()), | 35 zone_(ast_value_factory->zone()), |
36 closure_scope_(closure_scope), | 36 closure_scope_(closure_scope), |
37 factory_(ast_value_factory) { | 37 factory_(ast_value_factory) { |
38 DCHECK_EQ(closure_scope, closure_scope->GetClosureScope()); | 38 DCHECK_EQ(closure_scope, closure_scope->ClosureScope()); |
39 InitializeAstVisitor(parser->stack_limit()); | 39 InitializeAstVisitor(parser->stack_limit()); |
40 } | 40 } |
41 | 41 |
42 void Process(ZoneList<Statement*>* statements); | 42 void Process(ZoneList<Statement*>* statements); |
43 bool result_assigned() const { return result_assigned_; } | 43 bool result_assigned() const { return result_assigned_; } |
44 | 44 |
45 Zone* zone() { return zone_; } | 45 Zone* zone() { return zone_; } |
46 DeclarationScope* closure_scope() { return closure_scope_; } | 46 Scope* closure_scope() { return closure_scope_; } |
47 AstNodeFactory* factory() { return &factory_; } | 47 AstNodeFactory* factory() { return &factory_; } |
48 | 48 |
49 // Returns ".result = value" | 49 // Returns ".result = value" |
50 Expression* SetResult(Expression* value) { | 50 Expression* SetResult(Expression* value) { |
51 result_assigned_ = true; | 51 result_assigned_ = true; |
52 VariableProxy* result_proxy = factory()->NewVariableProxy(result_); | 52 VariableProxy* result_proxy = factory()->NewVariableProxy(result_); |
53 return factory()->NewAssignment(Token::ASSIGN, result_proxy, value, | 53 return factory()->NewAssignment(Token::ASSIGN, result_proxy, value, |
54 kNoSourcePosition); | 54 kNoSourcePosition); |
55 } | 55 } |
56 | 56 |
(...skipping 13 matching lines...) Expand all Loading... |
70 // [replacement_]. In many cases this will just be the original node. | 70 // [replacement_]. In many cases this will just be the original node. |
71 Statement* replacement_; | 71 Statement* replacement_; |
72 | 72 |
73 // To avoid storing to .result all the time, we eliminate some of | 73 // To avoid storing to .result all the time, we eliminate some of |
74 // the stores by keeping track of whether or not we're sure .result | 74 // the stores by keeping track of whether or not we're sure .result |
75 // will be overwritten anyway. This is a bit more tricky than what I | 75 // will be overwritten anyway. This is a bit more tricky than what I |
76 // was hoping for. | 76 // was hoping for. |
77 bool is_set_; | 77 bool is_set_; |
78 | 78 |
79 Zone* zone_; | 79 Zone* zone_; |
80 DeclarationScope* closure_scope_; | 80 Scope* closure_scope_; |
81 AstNodeFactory factory_; | 81 AstNodeFactory factory_; |
82 | 82 |
83 // Node visitors. | 83 // Node visitors. |
84 #define DEF_VISIT(type) void Visit##type(type* node); | 84 #define DEF_VISIT(type) void Visit##type(type* node); |
85 AST_NODE_LIST(DEF_VISIT) | 85 AST_NODE_LIST(DEF_VISIT) |
86 #undef DEF_VISIT | 86 #undef DEF_VISIT |
87 | 87 |
88 void VisitIterationStatement(IterationStatement* stmt); | 88 void VisitIterationStatement(IterationStatement* stmt); |
89 | 89 |
90 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); | 90 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 | 332 |
333 | 333 |
334 // Assumes code has been parsed. Mutates the AST, so the AST should not | 334 // Assumes code has been parsed. Mutates the AST, so the AST should not |
335 // continue to be used in the case of failure. | 335 // continue to be used in the case of failure. |
336 bool Rewriter::Rewrite(ParseInfo* info) { | 336 bool Rewriter::Rewrite(ParseInfo* info) { |
337 FunctionLiteral* function = info->literal(); | 337 FunctionLiteral* function = info->literal(); |
338 DCHECK_NOT_NULL(function); | 338 DCHECK_NOT_NULL(function); |
339 Scope* scope = function->scope(); | 339 Scope* scope = function->scope(); |
340 DCHECK_NOT_NULL(scope); | 340 DCHECK_NOT_NULL(scope); |
341 if (!scope->is_script_scope() && !scope->is_eval_scope()) return true; | 341 if (!scope->is_script_scope() && !scope->is_eval_scope()) return true; |
342 DeclarationScope* closure_scope = scope->GetClosureScope(); | 342 Scope* closure_scope = scope->ClosureScope(); |
343 | 343 |
344 ZoneList<Statement*>* body = function->body(); | 344 ZoneList<Statement*>* body = function->body(); |
345 if (!body->is_empty()) { | 345 if (!body->is_empty()) { |
346 Variable* result = closure_scope->NewTemporary( | 346 Variable* result = closure_scope->NewTemporary( |
347 info->ast_value_factory()->dot_result_string()); | 347 info->ast_value_factory()->dot_result_string()); |
348 // The name string must be internalized at this point. | 348 // The name string must be internalized at this point. |
349 DCHECK(!result->name().is_null()); | 349 DCHECK(!result->name().is_null()); |
350 Processor processor(info->isolate(), closure_scope, result, | 350 Processor processor(info->isolate(), closure_scope, result, |
351 info->ast_value_factory()); | 351 info->ast_value_factory()); |
352 processor.Process(body); | 352 processor.Process(body); |
353 if (processor.HasStackOverflow()) return false; | 353 if (processor.HasStackOverflow()) return false; |
354 | 354 |
355 if (processor.result_assigned()) { | 355 if (processor.result_assigned()) { |
356 int pos = kNoSourcePosition; | 356 int pos = kNoSourcePosition; |
357 VariableProxy* result_proxy = | 357 VariableProxy* result_proxy = |
358 processor.factory()->NewVariableProxy(result, pos); | 358 processor.factory()->NewVariableProxy(result, pos); |
359 Statement* result_statement = | 359 Statement* result_statement = |
360 processor.factory()->NewReturnStatement(result_proxy, pos); | 360 processor.factory()->NewReturnStatement(result_proxy, pos); |
361 body->Add(result_statement, info->zone()); | 361 body->Add(result_statement, info->zone()); |
362 } | 362 } |
363 } | 363 } |
364 | 364 |
365 return true; | 365 return true; |
366 } | 366 } |
367 | 367 |
368 bool Rewriter::Rewrite(Parser* parser, DeclarationScope* closure_scope, | 368 bool Rewriter::Rewrite(Parser* parser, Scope* closure_scope, DoExpression* expr, |
369 DoExpression* expr, AstValueFactory* factory) { | 369 AstValueFactory* factory) { |
370 Block* block = expr->block(); | 370 Block* block = expr->block(); |
371 DCHECK_EQ(closure_scope, closure_scope->GetClosureScope()); | 371 DCHECK_EQ(closure_scope, closure_scope->ClosureScope()); |
372 DCHECK(block->scope() == nullptr || | 372 DCHECK(block->scope() == nullptr || |
373 block->scope()->GetClosureScope() == closure_scope); | 373 block->scope()->ClosureScope() == closure_scope); |
374 ZoneList<Statement*>* body = block->statements(); | 374 ZoneList<Statement*>* body = block->statements(); |
375 VariableProxy* result = expr->result(); | 375 VariableProxy* result = expr->result(); |
376 Variable* result_var = result->var(); | 376 Variable* result_var = result->var(); |
377 | 377 |
378 if (!body->is_empty()) { | 378 if (!body->is_empty()) { |
379 Processor processor(parser, closure_scope, result_var, factory); | 379 Processor processor(parser, closure_scope, result_var, factory); |
380 processor.Process(body); | 380 processor.Process(body); |
381 if (processor.HasStackOverflow()) return false; | 381 if (processor.HasStackOverflow()) return false; |
382 | 382 |
383 if (!processor.result_assigned()) { | 383 if (!processor.result_assigned()) { |
384 AstNodeFactory* node_factory = processor.factory(); | 384 AstNodeFactory* node_factory = processor.factory(); |
385 Expression* undef = node_factory->NewUndefinedLiteral(kNoSourcePosition); | 385 Expression* undef = node_factory->NewUndefinedLiteral(kNoSourcePosition); |
386 Statement* completion = node_factory->NewExpressionStatement( | 386 Statement* completion = node_factory->NewExpressionStatement( |
387 processor.SetResult(undef), expr->position()); | 387 processor.SetResult(undef), expr->position()); |
388 body->Add(completion, factory->zone()); | 388 body->Add(completion, factory->zone()); |
389 } | 389 } |
390 } | 390 } |
391 return true; | 391 return true; |
392 } | 392 } |
393 | 393 |
394 | 394 |
395 } // namespace internal | 395 } // namespace internal |
396 } // namespace v8 | 396 } // namespace v8 |
OLD | NEW |