-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperationPrinter.cc
190 lines (159 loc) · 5.52 KB
/
OperationPrinter.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// 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 "OperationPrinter.h"
#include <algorithm>
#include <checks.h>
#include <map>
#include "BuiltIns.h"
using std::sort;
using std::map;
#define DEFINE_UNREACHABLE_VISIT(type) \
void OperationPrinter::Visit##type(type* node) { \
UNREACHABLE(); \
}
MODULE_NODE_LIST(DEFINE_UNREACHABLE_VISIT)
DECLARATION_NODE_LIST(DEFINE_UNREACHABLE_VISIT)
DEFINE_UNREACHABLE_VISIT(Block)
DEFINE_UNREACHABLE_VISIT(ModuleStatement)
DEFINE_UNREACHABLE_VISIT(ExpressionStatement)
DEFINE_UNREACHABLE_VISIT(EmptyStatement)
DEFINE_UNREACHABLE_VISIT(WithStatement)
DEFINE_UNREACHABLE_VISIT(DoWhileStatement)
DEFINE_UNREACHABLE_VISIT(ForStatement)
DEFINE_UNREACHABLE_VISIT(TryCatchStatement)
DEFINE_UNREACHABLE_VISIT(TryFinallyStatement)
DEFINE_UNREACHABLE_VISIT(ObjectLiteral)
DEFINE_UNREACHABLE_VISIT(ArrayLiteral)
DEFINE_UNREACHABLE_VISIT(Assignment)
DEFINE_UNREACHABLE_VISIT(CountOperation)
#undef DEFINE_UNREACHABLE_VISIT
#define DEFINE_EMPTY_VISIT(type) \
void OperationPrinter::Visit##type(type* node) { \
value_ = ""; \
}
DEFINE_EMPTY_VISIT(FunctionLiteral)
DEFINE_EMPTY_VISIT(SharedFunctionInfoLiteral)
DEFINE_EMPTY_VISIT(VariableProxy)
DEFINE_EMPTY_VISIT(Literal)
DEFINE_EMPTY_VISIT(RegExpLiteral)
DEFINE_EMPTY_VISIT(ThisFunction)
#undef DEFINE_EMPTY_VISIT
void OperationPrinter::VisitContinueStatement(ContinueStatement* node) {
value_ = "continue";
}
void OperationPrinter::VisitBreakStatement(BreakStatement* node) {
value_ = "break";
}
void OperationPrinter::VisitReturnStatement(ReturnStatement* node) {
value_ = "return";
}
void OperationPrinter::VisitIfStatement(IfStatement* node) {
value_ = "if";
}
void OperationPrinter::VisitSwitchStatement(SwitchStatement* node) {
value_ = "switch";
}
void OperationPrinter::VisitWhileStatement(WhileStatement* node) {
value_ = "while";
}
void OperationPrinter::VisitForInStatement(ForInStatement* node) {
value_ = "for";
}
void OperationPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
value_ = "debugger";
}
void OperationPrinter::VisitConditional(Conditional* node) {
value_ = "?:";
}
void OperationPrinter::VisitThrow(Throw* node) {
value_ = "throw";
}
void OperationPrinter::VisitProperty(Property* node) {
Literal* key = node->key()->AsLiteral();
value_ = (key != NULL && key->handle()->IsSymbol()) ? "." : "[]";
}
void OperationPrinter::VisitCall(Call* node) {
if (node->expression()->node_type() == AstNode::kProperty) {
Literal* method = reinterpret_cast<Property*>(node->expression())->key()->AsLiteral();
if (method != NULL && method->handle()->IsSymbol()) {
string name = *Handle<String>::cast(method->handle())->ToCString();
value_ = BuiltIns::FindMethod(name) ? "." + name + "()" : ".()";
} else
value_ = "[]()";
} else if (node->expression()->node_type() == AstNode::kVariableProxy) {
string name = *reinterpret_cast<VariableProxy*>(node->expression())->name()->ToCString();
value_ = BuiltIns::FindFunction(name) ? name + "()" : "()";
} else
value_ = "()";
}
void OperationPrinter::VisitCallNew(CallNew* node) {
if (node->expression()->node_type() == AstNode::kVariableProxy) {
string name = *reinterpret_cast<VariableProxy*>(node->expression())->name()->ToCString();
value_ = BuiltIns::FindConstructor(name) ? name : "new";
} else
value_ = "new";
}
void OperationPrinter::VisitCallRuntime(CallRuntime* node) {
value_ = "()";
}
void OperationPrinter::VisitUnaryOperation(UnaryOperation* node) {
if (node->op() == Token::ADD)
value_ = "pos";
else if (node->op() == Token::SUB)
value_ = "neg";
else
value_ = Token::String(node->op());
}
void OperationPrinter::VisitBinaryOperation(BinaryOperation* node) {
value_ = Token::String(node->op());
}
void OperationPrinter::VisitCompareOperation(CompareOperation* node) {
value_ = Token::String(node->op());
}
void OperationPrinter::VisitCanonicalFunctionEntry(CanonicalFunctionEntry* node) {
value_ = "begin";
}
void OperationPrinter::VisitCanonicalAssignment(CanonicalAssignment* node) {
Visit(node->value());
if (value_.empty())
value_ = "=";
}
void OperationPrinter::VisitCanonicalPropertyAssignment(CanonicalPropertyAssignment* node) {
Literal* key = node->target()->key()->AsLiteral();
value_ = key != NULL && key->handle()->IsSymbol() ? ".=" : "[]=";
}
void OperationPrinter::VisitCanonicalFunctionExit(CanonicalFunctionExit* node) {
value_ = "end";
}
#if 0
void OperationPrinter::VisitEndIfStatement(EndIfStatement* node) {
value_ = "end_if";
}
void OperationPrinter::VisitEndSwitchStatement(EndSwitchStatement* node) {
value_ = "end_switch";
}
void OperationPrinter::VisitEndWhileStatement(EndWhileStatement* node) {
value_ = "end_while";
}
void OperationPrinter::VisitEndForInStatement(EndForInStatement* node) {
value_ = "end_for";
}
#endif
string OperationPrinter::Print(Statement* node) {
Visit(node);
return value_;
}