-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatementCopier.cc
155 lines (135 loc) · 5.53 KB
/
StatementCopier.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (C) 2013 The University of Michigan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Authors - Chun-Hung Hsiao ([email protected])
//
#include "StatementCopier.h"
#define DEFINE_VISIT(type) \
void StatementCopier::Visit##type(type* node) { }
DECLARATION_NODE_LIST(DEFINE_VISIT)
MODULE_NODE_LIST(DEFINE_VISIT)
EXPRESSION_NODE_LIST(DEFINE_VISIT)
DEFINE_VISIT(ModuleStatement)
#undef DEFINE_VISIT
void StatementCopier::VisitBlock(Block* node) {
Block* block = factory_.NewBlock(node->labels(), node->statements()->length(), node->is_initializer_block());
for (int i = 0; i < node->statements()->length(); ++i) {
Visit(node->statements()->at(i));
block->AddStatement(copy_, isolate()->runtime_zone());
}
copy_ = block;
targets_[node] = block;
}
void StatementCopier::VisitExpressionStatement(ExpressionStatement* node) {
copy_ = factory_.NewExpressionStatement(node->expression());
}
void StatementCopier::VisitEmptyStatement(EmptyStatement* node) {
copy_ = factory_.NewEmptyStatement();
}
void StatementCopier::VisitIfStatement(IfStatement* node) {
Visit(node->then_statement());
Statement* then_stmt = copy_;
Visit(node->else_statement());
Statement* else_stmt = copy_;
copy_ = factory_.NewIfStatement(node->condition(), then_stmt, else_stmt);
}
void StatementCopier::VisitContinueStatement(ContinueStatement* node) {
if (targets_.find(node->target()) == targets_.end())
copy_ = factory_.NewContinueStatement(node->target());
else
copy_ = factory_.NewContinueStatement(targets_[node->target()]->AsIterationStatement());
}
void StatementCopier::VisitBreakStatement(BreakStatement* node) {
if (targets_.find(node->target()) == targets_.end())
copy_ = factory_.NewBreakStatement(node->target());
else
copy_ = factory_.NewBreakStatement(targets_[node->target()]);
}
void StatementCopier::VisitReturnStatement(ReturnStatement* node) {
copy_ = factory_.NewReturnStatement(node->expression());
}
void StatementCopier::VisitWithStatement(WithStatement* node) {
Visit(node->statement());
copy_ = factory_.NewWithStatement(node->expression(), copy_);
}
void StatementCopier::VisitSwitchStatement(SwitchStatement* node) {
SwitchStatement* switch_stmt = factory_.NewSwitchStatement(node->labels());
ZoneList<CaseClause*>* cases = new(isolate()->runtime_zone()) ZoneList<CaseClause*>(node->cases()->length(), isolate()->runtime_zone());
for (int i = 0; i < node->cases()->length(); ++i)
cases->Add(CopyCaseClause(node->cases()->at(i)), isolate()->runtime_zone());
switch_stmt->Initialize(node->tag(), cases);
copy_ = switch_stmt;
targets_[node] = switch_stmt;
}
void StatementCopier::VisitDoWhileStatement(DoWhileStatement* node) {
DoWhileStatement* do_while_stmt = factory_.NewDoWhileStatement(node->labels());
Visit(node->body());
do_while_stmt->Initialize(node->cond(), copy_);
copy_ = do_while_stmt;
targets_[node] = do_while_stmt;
}
void StatementCopier::VisitWhileStatement(WhileStatement* node) {
WhileStatement* while_stmt = factory_.NewWhileStatement(node->labels());
Visit(node->body());
while_stmt->Initialize(node->cond(), copy_);
copy_ = while_stmt;
targets_[node] = while_stmt;
}
void StatementCopier::VisitForStatement(ForStatement* node) {
ForStatement* for_stmt = factory_.NewForStatement(node->labels());
Visit(node->init());
Statement* init = copy_;
Visit(node->next());
Statement* next = copy_;
Visit(node->body());
for_stmt->Initialize(init, node->cond(), next, copy_);
copy_ = for_stmt;
targets_[node] = for_stmt;
}
void StatementCopier::VisitForInStatement(ForInStatement* node) {
ForInStatement* for_in_stmt = factory_.NewForInStatement(node->labels());
Visit(node->body());
for_in_stmt->Initialize(node->each(), node->enumerable(), copy_);
copy_ = for_in_stmt;
targets_[node] = for_in_stmt;
}
void StatementCopier::VisitTryCatchStatement(TryCatchStatement* node) {
Visit(node->try_block());
Block* try_block = reinterpret_cast<Block*>(copy_);
Visit(node->catch_block());
Block* catch_block = reinterpret_cast<Block*>(copy_);
copy_ = factory_.NewTryCatchStatement(node->index(), try_block, node->scope(), node->variable(), catch_block);
}
void StatementCopier::VisitTryFinallyStatement(TryFinallyStatement* node) {
Visit(node->try_block());
Block* try_block = reinterpret_cast<Block*>(copy_);
Visit(node->finally_block());
Block* finally_block = reinterpret_cast<Block*>(copy_);
copy_ = factory_.NewTryFinallyStatement(node->index(), try_block, finally_block);
}
void StatementCopier::VisitDebuggerStatement(DebuggerStatement* node) {
copy_ = factory_.NewDebuggerStatement();
}
Statement* StatementCopier::Copy(Statement* stmt) {
Visit(stmt);
return copy_;
}
CaseClause* StatementCopier::CopyCaseClause(CaseClause* clause) {
ZoneList<Statement*>* stmts = new(isolate()->runtime_zone()) ZoneList<Statement*>(clause->statements()->length(), isolate()->runtime_zone());
for (int i = 0; i < clause->statements()->length(); ++i) {
Visit(clause->statements()->at(i));
stmts->Add(copy_, isolate()->runtime_zone());
}
return new(isolate()->runtime_zone()) CaseClause(isolate(), clause->label(), stmts, clause->position());
}