blob: 6362c63f8ec65863e97ffac96f362a1e6ced4c9d [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
3// found in the LICENSE file.
4
5#include "src/parsing/parameter-initializer-rewriter.h"
6
Ben Murdochc5610432016-08-08 18:44:38 +01007#include <algorithm>
8#include <utility>
9#include <vector>
10
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/ast/ast.h"
12#include "src/ast/ast-expression-visitor.h"
13#include "src/ast/scopes.h"
14
15namespace v8 {
16namespace internal {
17
18namespace {
19
20
21class Rewriter final : public AstExpressionVisitor {
22 public:
23 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* old_scope,
24 Scope* new_scope)
25 : AstExpressionVisitor(stack_limit, initializer),
26 old_scope_(old_scope),
27 new_scope_(new_scope) {}
Ben Murdochc5610432016-08-08 18:44:38 +010028 ~Rewriter();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029
30 private:
31 void VisitExpression(Expression* expr) override {}
32
33 void VisitFunctionLiteral(FunctionLiteral* expr) override;
34 void VisitClassLiteral(ClassLiteral* expr) override;
35 void VisitVariableProxy(VariableProxy* expr) override;
36
Ben Murdochc5610432016-08-08 18:44:38 +010037 void VisitBlock(Block* stmt) override;
38 void VisitTryCatchStatement(TryCatchStatement* stmt) override;
39 void VisitWithStatement(WithStatement* stmt) override;
40
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 Scope* old_scope_;
42 Scope* new_scope_;
Ben Murdochc5610432016-08-08 18:44:38 +010043 std::vector<std::pair<Variable*, int>> temps_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044};
45
Ben Murdochc5610432016-08-08 18:44:38 +010046struct 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
53Rewriter::~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}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063
64void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) {
65 function_literal->scope()->ReplaceOuterScope(new_scope_);
66}
67
68
69void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) {
70 class_literal->scope()->ReplaceOuterScope(new_scope_);
71 if (class_literal->extends() != nullptr) {
72 Visit(class_literal->extends());
73 }
74 // No need to visit the constructor since it will have the class
75 // scope on its scope chain.
76 ZoneList<ObjectLiteralProperty*>* props = class_literal->properties();
77 for (int i = 0; i < props->length(); ++i) {
78 ObjectLiteralProperty* prop = props->at(i);
79 if (!prop->key()->IsLiteral()) {
80 Visit(prop->key());
81 }
82 // No need to visit the values, since all values are functions with
83 // the class scope on their scope chain.
84 DCHECK(prop->value()->IsFunctionLiteral());
85 }
86}
87
88
89void Rewriter::VisitVariableProxy(VariableProxy* proxy) {
90 if (proxy->is_resolved()) {
91 Variable* var = proxy->var();
Ben Murdochda12d292016-06-02 14:46:10 +010092 if (var->mode() != TEMPORARY) return;
Ben Murdochc5610432016-08-08 18:44:38 +010093 // For rewriting inside the same ClosureScope (e.g., putting default
94 // parameter values in their own inner scope in certain cases), refrain
95 // from invalidly moving temporaries to a block scope.
96 if (var->scope()->ClosureScope() == new_scope_->ClosureScope()) return;
97 int index = old_scope_->RemoveTemporary(var);
98 if (index >= 0) {
99 temps_.push_back(std::make_pair(var, index));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100 }
101 } else if (old_scope_->RemoveUnresolved(proxy)) {
102 new_scope_->AddUnresolved(proxy);
103 }
104}
105
106
Ben Murdochc5610432016-08-08 18:44:38 +0100107void Rewriter::VisitBlock(Block* stmt) {
108 if (stmt->scope() != nullptr)
109 stmt->scope()->ReplaceOuterScope(new_scope_);
110 else
111 VisitStatements(stmt->statements());
112}
113
114
115void Rewriter::VisitTryCatchStatement(TryCatchStatement* stmt) {
116 Visit(stmt->try_block());
117 stmt->scope()->ReplaceOuterScope(new_scope_);
118}
119
120
121void Rewriter::VisitWithStatement(WithStatement* stmt) {
122 Visit(stmt->expression());
123 stmt->scope()->ReplaceOuterScope(new_scope_);
124}
125
126
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127} // anonymous namespace
128
129
130void RewriteParameterInitializerScope(uintptr_t stack_limit,
131 Expression* initializer, Scope* old_scope,
132 Scope* new_scope) {
133 Rewriter rewriter(stack_limit, initializer, old_scope, new_scope);
134 rewriter.Run();
135}
136
137
138} // namespace internal
139} // namespace v8