OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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/parameter-initializer-rewriter.h" | 5 #include "src/parsing/parameter-initializer-rewriter.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 #include <utility> |
| 9 #include <vector> |
| 10 |
7 #include "src/ast/ast.h" | 11 #include "src/ast/ast.h" |
8 #include "src/ast/ast-expression-visitor.h" | 12 #include "src/ast/ast-expression-visitor.h" |
9 #include "src/ast/scopes.h" | 13 #include "src/ast/scopes.h" |
10 | 14 |
11 namespace v8 { | 15 namespace v8 { |
12 namespace internal { | 16 namespace internal { |
13 | 17 |
14 namespace { | 18 namespace { |
15 | 19 |
16 | 20 |
17 class Rewriter final : public AstExpressionVisitor { | 21 class Rewriter final : public AstExpressionVisitor { |
18 public: | 22 public: |
19 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* old_scope, | 23 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* old_scope, |
20 Scope* new_scope) | 24 Scope* new_scope) |
21 : AstExpressionVisitor(stack_limit, initializer), | 25 : AstExpressionVisitor(stack_limit, initializer), |
22 old_scope_(old_scope), | 26 old_scope_(old_scope), |
23 new_scope_(new_scope) {} | 27 new_scope_(new_scope) {} |
| 28 ~Rewriter(); |
24 | 29 |
25 private: | 30 private: |
26 void VisitExpression(Expression* expr) override {} | 31 void VisitExpression(Expression* expr) override {} |
27 | 32 |
28 void VisitFunctionLiteral(FunctionLiteral* expr) override; | 33 void VisitFunctionLiteral(FunctionLiteral* expr) override; |
29 void VisitClassLiteral(ClassLiteral* expr) override; | 34 void VisitClassLiteral(ClassLiteral* expr) override; |
30 void VisitVariableProxy(VariableProxy* expr) override; | 35 void VisitVariableProxy(VariableProxy* expr) override; |
31 | 36 |
32 void VisitBlock(Block* stmt) override; | 37 void VisitBlock(Block* stmt) override; |
33 void VisitTryCatchStatement(TryCatchStatement* stmt) override; | 38 void VisitTryCatchStatement(TryCatchStatement* stmt) override; |
34 void VisitWithStatement(WithStatement* stmt) override; | 39 void VisitWithStatement(WithStatement* stmt) override; |
35 | 40 |
36 Scope* old_scope_; | 41 Scope* old_scope_; |
37 Scope* new_scope_; | 42 Scope* new_scope_; |
| 43 std::vector<std::pair<Variable*, int>> temps_; |
38 }; | 44 }; |
39 | 45 |
| 46 struct LessThanSecond { |
| 47 bool operator()(const std::pair<Variable*, int>& left, |
| 48 const std::pair<Variable*, int>& right) { |
| 49 return left.second < right.second; |
| 50 } |
| 51 }; |
| 52 |
| 53 Rewriter::~Rewriter() { |
| 54 if (!temps_.empty()) { |
| 55 // Ensure that we add temporaries in the order they appeared in old_scope_. |
| 56 std::sort(temps_.begin(), temps_.end(), LessThanSecond()); |
| 57 for (auto var_and_index : temps_) { |
| 58 var_and_index.first->set_scope(new_scope_); |
| 59 new_scope_->AddTemporary(var_and_index.first); |
| 60 } |
| 61 } |
| 62 } |
40 | 63 |
41 void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { | 64 void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { |
42 function_literal->scope()->ReplaceOuterScope(new_scope_); | 65 function_literal->scope()->ReplaceOuterScope(new_scope_); |
43 } | 66 } |
44 | 67 |
45 | 68 |
46 void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) { | 69 void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) { |
47 class_literal->scope()->ReplaceOuterScope(new_scope_); | 70 class_literal->scope()->ReplaceOuterScope(new_scope_); |
48 if (class_literal->extends() != nullptr) { | 71 if (class_literal->extends() != nullptr) { |
49 Visit(class_literal->extends()); | 72 Visit(class_literal->extends()); |
(...skipping 10 matching lines...) Expand all Loading... |
60 // the class scope on their scope chain. | 83 // the class scope on their scope chain. |
61 DCHECK(prop->value()->IsFunctionLiteral()); | 84 DCHECK(prop->value()->IsFunctionLiteral()); |
62 } | 85 } |
63 } | 86 } |
64 | 87 |
65 | 88 |
66 void Rewriter::VisitVariableProxy(VariableProxy* proxy) { | 89 void Rewriter::VisitVariableProxy(VariableProxy* proxy) { |
67 if (proxy->is_resolved()) { | 90 if (proxy->is_resolved()) { |
68 Variable* var = proxy->var(); | 91 Variable* var = proxy->var(); |
69 if (var->mode() != TEMPORARY) return; | 92 if (var->mode() != TEMPORARY) return; |
70 if (old_scope_->RemoveTemporary(var)) { | 93 int index = old_scope_->RemoveTemporary(var); |
71 var->set_scope(new_scope_); | 94 if (index >= 0) { |
72 new_scope_->AddTemporary(var); | 95 temps_.push_back(std::make_pair(var, index)); |
73 } | 96 } |
74 } else if (old_scope_->RemoveUnresolved(proxy)) { | 97 } else if (old_scope_->RemoveUnresolved(proxy)) { |
75 new_scope_->AddUnresolved(proxy); | 98 new_scope_->AddUnresolved(proxy); |
76 } | 99 } |
77 } | 100 } |
78 | 101 |
79 | 102 |
80 void Rewriter::VisitBlock(Block* stmt) { | 103 void Rewriter::VisitBlock(Block* stmt) { |
81 if (stmt->scope() != nullptr) | 104 if (stmt->scope() != nullptr) |
82 stmt->scope()->ReplaceOuterScope(new_scope_); | 105 stmt->scope()->ReplaceOuterScope(new_scope_); |
(...skipping 20 matching lines...) Expand all Loading... |
103 void RewriteParameterInitializerScope(uintptr_t stack_limit, | 126 void RewriteParameterInitializerScope(uintptr_t stack_limit, |
104 Expression* initializer, Scope* old_scope, | 127 Expression* initializer, Scope* old_scope, |
105 Scope* new_scope) { | 128 Scope* new_scope) { |
106 Rewriter rewriter(stack_limit, initializer, old_scope, new_scope); | 129 Rewriter rewriter(stack_limit, initializer, old_scope, new_scope); |
107 rewriter.Run(); | 130 rewriter.Run(); |
108 } | 131 } |
109 | 132 |
110 | 133 |
111 } // namespace internal | 134 } // namespace internal |
112 } // namespace v8 | 135 } // namespace v8 |
OLD | NEW |