| 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 | |
| 11 #include "src/ast/ast.h" | 7 #include "src/ast/ast.h" |
| 12 #include "src/ast/ast-expression-visitor.h" | 8 #include "src/ast/ast-expression-visitor.h" |
| 13 #include "src/ast/scopes.h" | 9 #include "src/ast/scopes.h" |
| 14 | 10 |
| 15 namespace v8 { | 11 namespace v8 { |
| 16 namespace internal { | 12 namespace internal { |
| 17 | 13 |
| 18 namespace { | 14 namespace { |
| 19 | 15 |
| 20 | 16 |
| 21 class Rewriter final : public AstExpressionVisitor { | 17 class Rewriter final : public AstExpressionVisitor { |
| 22 public: | 18 public: |
| 23 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* old_scope, | 19 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* param_scope) |
| 24 Scope* new_scope) | |
| 25 : AstExpressionVisitor(stack_limit, initializer), | 20 : AstExpressionVisitor(stack_limit, initializer), |
| 26 old_scope_(old_scope), | 21 param_scope_(param_scope) {} |
| 27 new_scope_(new_scope), | |
| 28 old_scope_closure_(old_scope->ClosureScope()), | |
| 29 new_scope_closure_(new_scope->ClosureScope()) {} | |
| 30 ~Rewriter(); | |
| 31 | 22 |
| 32 private: | 23 private: |
| 33 void VisitExpression(Expression* expr) override {} | 24 void VisitExpression(Expression* expr) override {} |
| 34 | 25 |
| 35 void VisitFunctionLiteral(FunctionLiteral* expr) override; | 26 void VisitFunctionLiteral(FunctionLiteral* expr) override; |
| 36 void VisitClassLiteral(ClassLiteral* expr) override; | 27 void VisitClassLiteral(ClassLiteral* expr) override; |
| 37 void VisitVariableProxy(VariableProxy* expr) override; | 28 void VisitVariableProxy(VariableProxy* expr) override; |
| 38 | 29 |
| 39 void VisitBlock(Block* stmt) override; | 30 void VisitBlock(Block* stmt) override; |
| 40 void VisitTryCatchStatement(TryCatchStatement* stmt) override; | 31 void VisitTryCatchStatement(TryCatchStatement* stmt) override; |
| 41 void VisitWithStatement(WithStatement* stmt) override; | 32 void VisitWithStatement(WithStatement* stmt) override; |
| 42 | 33 |
| 43 Scope* old_scope_; | 34 Scope* param_scope_; |
| 44 Scope* new_scope_; | |
| 45 Scope* old_scope_closure_; | |
| 46 Scope* new_scope_closure_; | |
| 47 std::vector<std::pair<Variable*, int>> temps_; | |
| 48 }; | 35 }; |
| 49 | 36 |
| 50 struct LessThanSecond { | |
| 51 bool operator()(const std::pair<Variable*, int>& left, | |
| 52 const std::pair<Variable*, int>& right) { | |
| 53 return left.second < right.second; | |
| 54 } | |
| 55 }; | |
| 56 | |
| 57 Rewriter::~Rewriter() { | |
| 58 if (!temps_.empty()) { | |
| 59 // Ensure that we add temporaries in the order they appeared in old_scope_. | |
| 60 std::sort(temps_.begin(), temps_.end(), LessThanSecond()); | |
| 61 for (auto var_and_index : temps_) { | |
| 62 var_and_index.first->set_scope(new_scope_closure_); | |
| 63 new_scope_closure_->AddTemporary(var_and_index.first); | |
| 64 } | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { | 37 void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { |
| 69 function_literal->scope()->ReplaceOuterScope(new_scope_); | 38 function_literal->scope()->ReplaceOuterScope(param_scope_); |
| 70 } | 39 } |
| 71 | 40 |
| 72 | 41 |
| 73 void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) { | 42 void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) { |
| 74 class_literal->scope()->ReplaceOuterScope(new_scope_); | 43 class_literal->scope()->ReplaceOuterScope(param_scope_); |
| 75 if (class_literal->extends() != nullptr) { | 44 if (class_literal->extends() != nullptr) { |
| 76 Visit(class_literal->extends()); | 45 Visit(class_literal->extends()); |
| 77 } | 46 } |
| 78 // No need to visit the constructor since it will have the class | 47 // No need to visit the constructor since it will have the class |
| 79 // scope on its scope chain. | 48 // scope on its scope chain. |
| 80 ZoneList<ObjectLiteralProperty*>* props = class_literal->properties(); | 49 ZoneList<ObjectLiteralProperty*>* props = class_literal->properties(); |
| 81 for (int i = 0; i < props->length(); ++i) { | 50 for (int i = 0; i < props->length(); ++i) { |
| 82 ObjectLiteralProperty* prop = props->at(i); | 51 ObjectLiteralProperty* prop = props->at(i); |
| 83 if (!prop->key()->IsLiteral()) { | 52 if (!prop->key()->IsLiteral()) { |
| 84 Visit(prop->key()); | 53 Visit(prop->key()); |
| 85 } | 54 } |
| 86 // No need to visit the values, since all values are functions with | 55 // No need to visit the values, since all values are functions with |
| 87 // the class scope on their scope chain. | 56 // the class scope on their scope chain. |
| 88 DCHECK(prop->value()->IsFunctionLiteral()); | 57 DCHECK(prop->value()->IsFunctionLiteral()); |
| 89 } | 58 } |
| 90 } | 59 } |
| 91 | 60 |
| 92 | 61 |
| 93 void Rewriter::VisitVariableProxy(VariableProxy* proxy) { | 62 void Rewriter::VisitVariableProxy(VariableProxy* proxy) { |
| 94 if (proxy->is_resolved()) { | 63 if (!proxy->is_resolved()) { |
| 95 Variable* var = proxy->var(); | 64 if (param_scope_->outer_scope()->RemoveUnresolved(proxy)) { |
| 96 if (var->mode() != TEMPORARY) return; | 65 param_scope_->AddUnresolved(proxy); |
| 97 // Temporaries are only placed in ClosureScopes. | |
| 98 DCHECK_EQ(var->scope(), var->scope()->ClosureScope()); | |
| 99 // If the temporary is already where it should be, return quickly. | |
| 100 if (var->scope() == new_scope_closure_) return; | |
| 101 int index = old_scope_closure_->RemoveTemporary(var); | |
| 102 if (index >= 0) { | |
| 103 temps_.push_back(std::make_pair(var, index)); | |
| 104 } | 66 } |
| 105 } else if (old_scope_->RemoveUnresolved(proxy)) { | 67 } else { |
| 106 new_scope_->AddUnresolved(proxy); | 68 // Ensure that temporaries we find are already in the correct scope. |
| 69 DCHECK(proxy->var()->mode() != TEMPORARY || |
| 70 proxy->var()->scope() == param_scope_->ClosureScope()); |
| 107 } | 71 } |
| 108 } | 72 } |
| 109 | 73 |
| 110 | 74 |
| 111 void Rewriter::VisitBlock(Block* stmt) { | 75 void Rewriter::VisitBlock(Block* stmt) { |
| 112 if (stmt->scope() != nullptr) | 76 if (stmt->scope() != nullptr) |
| 113 stmt->scope()->ReplaceOuterScope(new_scope_); | 77 stmt->scope()->ReplaceOuterScope(param_scope_); |
| 114 else | 78 else |
| 115 VisitStatements(stmt->statements()); | 79 VisitStatements(stmt->statements()); |
| 116 } | 80 } |
| 117 | 81 |
| 118 | 82 |
| 119 void Rewriter::VisitTryCatchStatement(TryCatchStatement* stmt) { | 83 void Rewriter::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 120 Visit(stmt->try_block()); | 84 Visit(stmt->try_block()); |
| 121 stmt->scope()->ReplaceOuterScope(new_scope_); | 85 stmt->scope()->ReplaceOuterScope(param_scope_); |
| 122 } | 86 } |
| 123 | 87 |
| 124 | 88 |
| 125 void Rewriter::VisitWithStatement(WithStatement* stmt) { | 89 void Rewriter::VisitWithStatement(WithStatement* stmt) { |
| 126 Visit(stmt->expression()); | 90 Visit(stmt->expression()); |
| 127 stmt->scope()->ReplaceOuterScope(new_scope_); | 91 stmt->scope()->ReplaceOuterScope(param_scope_); |
| 128 } | 92 } |
| 129 | 93 |
| 130 | 94 |
| 131 } // anonymous namespace | 95 } // anonymous namespace |
| 132 | 96 |
| 97 void ReparentParameterExpressionScope(uintptr_t stack_limit, Expression* expr, |
| 98 Scope* param_scope) { |
| 99 // The only case that uses this code is block scopes for parameters containing |
| 100 // sloppy eval. |
| 101 DCHECK(param_scope->is_block_scope()); |
| 102 DCHECK(param_scope->is_declaration_scope()); |
| 103 DCHECK(param_scope->calls_sloppy_eval()); |
| 104 DCHECK(param_scope->outer_scope()->is_function_scope()); |
| 133 | 105 |
| 134 void RewriteParameterInitializerScope(uintptr_t stack_limit, | 106 Rewriter rewriter(stack_limit, expr, param_scope); |
| 135 Expression* initializer, Scope* old_scope, | |
| 136 Scope* new_scope) { | |
| 137 Rewriter rewriter(stack_limit, initializer, old_scope, new_scope); | |
| 138 rewriter.Run(); | 107 rewriter.Run(); |
| 139 } | 108 } |
| 140 | 109 |
| 141 | 110 |
| 142 } // namespace internal | 111 } // namespace internal |
| 143 } // namespace v8 | 112 } // namespace v8 |
| OLD | NEW |