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> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "src/ast/ast.h" | 11 #include "src/ast/ast.h" |
12 #include "src/ast/ast-expression-visitor.h" | 12 #include "src/ast/ast-expression-visitor.h" |
13 #include "src/ast/scopes.h" | 13 #include "src/ast/scopes.h" |
14 | 14 |
15 namespace v8 { | 15 namespace v8 { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 | 20 |
21 class Rewriter final : public AstExpressionVisitor { | 21 class Rewriter final : public AstExpressionVisitor { |
22 public: | 22 public: |
23 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* old_scope, | 23 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* old_scope, |
24 Scope* new_scope) | 24 Scope* new_scope) |
25 : AstExpressionVisitor(stack_limit, initializer), | 25 : AstExpressionVisitor(stack_limit, initializer), |
26 old_scope_(old_scope), | 26 old_scope_(old_scope), |
27 new_scope_(new_scope) {} | 27 new_scope_(new_scope), |
| 28 old_scope_closure_(old_scope->ClosureScope()), |
| 29 new_scope_closure_(new_scope->ClosureScope()) {} |
28 ~Rewriter(); | 30 ~Rewriter(); |
29 | 31 |
30 private: | 32 private: |
31 void VisitExpression(Expression* expr) override {} | 33 void VisitExpression(Expression* expr) override {} |
32 | 34 |
33 void VisitFunctionLiteral(FunctionLiteral* expr) override; | 35 void VisitFunctionLiteral(FunctionLiteral* expr) override; |
34 void VisitClassLiteral(ClassLiteral* expr) override; | 36 void VisitClassLiteral(ClassLiteral* expr) override; |
35 void VisitVariableProxy(VariableProxy* expr) override; | 37 void VisitVariableProxy(VariableProxy* expr) override; |
36 | 38 |
37 void VisitBlock(Block* stmt) override; | 39 void VisitBlock(Block* stmt) override; |
38 void VisitTryCatchStatement(TryCatchStatement* stmt) override; | 40 void VisitTryCatchStatement(TryCatchStatement* stmt) override; |
39 void VisitWithStatement(WithStatement* stmt) override; | 41 void VisitWithStatement(WithStatement* stmt) override; |
40 | 42 |
41 Scope* old_scope_; | 43 Scope* old_scope_; |
42 Scope* new_scope_; | 44 Scope* new_scope_; |
| 45 Scope* old_scope_closure_; |
| 46 Scope* new_scope_closure_; |
43 std::vector<std::pair<Variable*, int>> temps_; | 47 std::vector<std::pair<Variable*, int>> temps_; |
44 }; | 48 }; |
45 | 49 |
46 struct LessThanSecond { | 50 struct LessThanSecond { |
47 bool operator()(const std::pair<Variable*, int>& left, | 51 bool operator()(const std::pair<Variable*, int>& left, |
48 const std::pair<Variable*, int>& right) { | 52 const std::pair<Variable*, int>& right) { |
49 return left.second < right.second; | 53 return left.second < right.second; |
50 } | 54 } |
51 }; | 55 }; |
52 | 56 |
53 Rewriter::~Rewriter() { | 57 Rewriter::~Rewriter() { |
54 if (!temps_.empty()) { | 58 if (!temps_.empty()) { |
55 // Ensure that we add temporaries in the order they appeared in old_scope_. | 59 // Ensure that we add temporaries in the order they appeared in old_scope_. |
56 std::sort(temps_.begin(), temps_.end(), LessThanSecond()); | 60 std::sort(temps_.begin(), temps_.end(), LessThanSecond()); |
57 for (auto var_and_index : temps_) { | 61 for (auto var_and_index : temps_) { |
58 var_and_index.first->set_scope(new_scope_); | 62 var_and_index.first->set_scope(new_scope_closure_); |
59 new_scope_->AddTemporary(var_and_index.first); | 63 new_scope_closure_->AddTemporary(var_and_index.first); |
60 } | 64 } |
61 } | 65 } |
62 } | 66 } |
63 | 67 |
64 void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { | 68 void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { |
65 function_literal->scope()->ReplaceOuterScope(new_scope_); | 69 function_literal->scope()->ReplaceOuterScope(new_scope_); |
66 } | 70 } |
67 | 71 |
68 | 72 |
69 void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) { | 73 void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) { |
(...skipping 13 matching lines...) Expand all Loading... |
83 // the class scope on their scope chain. | 87 // the class scope on their scope chain. |
84 DCHECK(prop->value()->IsFunctionLiteral()); | 88 DCHECK(prop->value()->IsFunctionLiteral()); |
85 } | 89 } |
86 } | 90 } |
87 | 91 |
88 | 92 |
89 void Rewriter::VisitVariableProxy(VariableProxy* proxy) { | 93 void Rewriter::VisitVariableProxy(VariableProxy* proxy) { |
90 if (proxy->is_resolved()) { | 94 if (proxy->is_resolved()) { |
91 Variable* var = proxy->var(); | 95 Variable* var = proxy->var(); |
92 if (var->mode() != TEMPORARY) return; | 96 if (var->mode() != TEMPORARY) return; |
93 // For rewriting inside the same ClosureScope (e.g., putting default | 97 // Temporaries are only placed in ClosureScopes. |
94 // parameter values in their own inner scope in certain cases), refrain | 98 DCHECK_EQ(var->scope(), var->scope()->ClosureScope()); |
95 // from invalidly moving temporaries to a block scope. | 99 // If the temporary is already where it should be, return quickly. |
96 if (var->scope()->ClosureScope() == new_scope_->ClosureScope()) return; | 100 if (var->scope() == new_scope_closure_) return; |
97 int index = old_scope_->RemoveTemporary(var); | 101 int index = old_scope_closure_->RemoveTemporary(var); |
98 if (index >= 0) { | 102 if (index >= 0) { |
99 temps_.push_back(std::make_pair(var, index)); | 103 temps_.push_back(std::make_pair(var, index)); |
100 } | 104 } |
101 } else if (old_scope_->RemoveUnresolved(proxy)) { | 105 } else if (old_scope_->RemoveUnresolved(proxy)) { |
102 new_scope_->AddUnresolved(proxy); | 106 new_scope_->AddUnresolved(proxy); |
103 } | 107 } |
104 } | 108 } |
105 | 109 |
106 | 110 |
107 void Rewriter::VisitBlock(Block* stmt) { | 111 void Rewriter::VisitBlock(Block* stmt) { |
(...skipping 22 matching lines...) Expand all Loading... |
130 void RewriteParameterInitializerScope(uintptr_t stack_limit, | 134 void RewriteParameterInitializerScope(uintptr_t stack_limit, |
131 Expression* initializer, Scope* old_scope, | 135 Expression* initializer, Scope* old_scope, |
132 Scope* new_scope) { | 136 Scope* new_scope) { |
133 Rewriter rewriter(stack_limit, initializer, old_scope, new_scope); | 137 Rewriter rewriter(stack_limit, initializer, old_scope, new_scope); |
134 rewriter.Run(); | 138 rewriter.Run(); |
135 } | 139 } |
136 | 140 |
137 | 141 |
138 } // namespace internal | 142 } // namespace internal |
139 } // namespace v8 | 143 } // namespace v8 |
OLD | NEW |