Chromium Code Reviews| Index: src/parsing/parameter-initializer-rewriter.cc |
| diff --git a/src/parsing/parameter-initializer-rewriter.cc b/src/parsing/parameter-initializer-rewriter.cc |
| index 3e3587b2bd75bcf6646bcd18635bf1a8f956d55c..8ed35d0392a62a08ddd12385d0f9f5bb7b435712 100644 |
| --- a/src/parsing/parameter-initializer-rewriter.cc |
| +++ b/src/parsing/parameter-initializer-rewriter.cc |
| @@ -4,6 +4,10 @@ |
| #include "src/parsing/parameter-initializer-rewriter.h" |
| +#include <algorithm> |
| +#include <utility> |
| +#include <vector> |
| + |
| #include "src/ast/ast.h" |
| #include "src/ast/ast-expression-visitor.h" |
| #include "src/ast/scopes.h" |
| @@ -21,6 +25,7 @@ class Rewriter final : public AstExpressionVisitor { |
| : AstExpressionVisitor(stack_limit, initializer), |
| old_scope_(old_scope), |
| new_scope_(new_scope) {} |
| + ~Rewriter(); |
| private: |
| void VisitExpression(Expression* expr) override {} |
| @@ -31,8 +36,26 @@ class Rewriter final : public AstExpressionVisitor { |
| Scope* old_scope_; |
| Scope* new_scope_; |
| + std::vector<std::pair<Variable*, int>> temps_; |
| +}; |
| + |
| +struct LessThanSecond { |
| + bool operator()(const std::pair<Variable*, int>& left, |
| + const std::pair<Variable*, int>& right) { |
| + return left.second < right.second; |
| + } |
| }; |
| +Rewriter::~Rewriter() { |
| + if (!temps_.empty()) { |
| + // Ensure that we add temporaries in the order they appeared in old_scope_. |
| + std::sort(temps_.begin(), temps_.end(), LessThanSecond()); |
|
nickie
2016/03/11 10:48:44
You wouldn't need LessThanSecond if the elements o
|
| + for (auto var_and_index : temps_) { |
| + var_and_index.first->set_scope(new_scope_); |
| + new_scope_->AddTemporary(var_and_index.first); |
| + } |
| + } |
| +} |
| void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { |
| function_literal->scope()->ReplaceOuterScope(new_scope_); |
| @@ -63,9 +86,9 @@ void Rewriter::VisitVariableProxy(VariableProxy* proxy) { |
| if (proxy->is_resolved()) { |
| Variable* var = proxy->var(); |
| if (var->mode() != TEMPORARY) return; |
| - if (old_scope_->RemoveTemporary(var)) { |
| - var->set_scope(new_scope_); |
| - new_scope_->AddTemporary(var); |
| + int index = old_scope_->RemoveTemporary(var); |
| + if (index >= 0) { |
| + temps_.push_back(std::make_pair(var, index)); |
| } |
| } else if (old_scope_->RemoveUnresolved(proxy)) { |
| new_scope_->AddUnresolved(proxy); |