forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleanup.cc
68 lines (56 loc) · 2.06 KB
/
Cleanup.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
#include "rewriter/Cleanup.h"
#include "ast/Helpers.h"
#include "ast/ast.h"
#include "ast/treemap/treemap.h"
#include "core/core.h"
using namespace std;
namespace sorbet::rewriter {
// This pass gets rid of some unnecessary nodes that are likely to have gotten created in the course of the rewriter
// pass, specifically by removing EmptyTree nodes and `nil` nodes in places where they can be safely
// removed (i.e. as part of longer sequences of expressions where they are not a return value)
struct CleanupWalk {
void postTransformInsSeq(core::Context ctx, ast::ExpressionPtr &tree) {
auto &insSeq = ast::cast_tree_nonnull<ast::InsSeq>(tree);
ast::InsSeq::STATS_store newStore;
for (auto &m : insSeq.stats) {
if (ast::isa_tree<ast::EmptyTree>(m)) {
continue;
}
if (ast::isa_tree<ast::Literal>(m)) {
auto lit = ast::cast_tree_nonnull<ast::Literal>(m);
if (lit.isNil(ctx)) {
continue;
}
}
newStore.emplace_back(move(m));
}
if (newStore.empty()) {
tree = move(insSeq.expr);
return;
}
insSeq.stats = std::move(newStore);
}
void postTransformClassDef(core::Context ctx, ast::ExpressionPtr &tree) {
auto &classDef = ast::cast_tree_nonnull<ast::ClassDef>(tree);
ast::ClassDef::RHS_store newStore;
for (auto &m : classDef.rhs) {
if (ast::isa_tree<ast::EmptyTree>(m)) {
continue;
}
if (ast::isa_tree<ast::Literal>(m)) {
auto lit = ast::cast_tree_nonnull<ast::Literal>(m);
if (lit.isNil(ctx)) {
continue;
}
}
newStore.emplace_back(move(m));
}
classDef.rhs = std::move(newStore);
}
};
ast::ExpressionPtr Cleanup::run(core::Context ctx, ast::ExpressionPtr tree) {
CleanupWalk cleanup;
ast::TreeWalk::apply(ctx, cleanup, tree);
return tree;
}
} // namespace sorbet::rewriter