forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewriter.cc
191 lines (161 loc) · 6.69 KB
/
rewriter.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
191
#include "rewriter/rewriter.h"
#include "ast/treemap/treemap.h"
#include "ast/verifier/verifier.h"
#include "common/typecase.h"
#include "main/pipeline/semantic_extension/SemanticExtension.h"
#include "rewriter/ClassNew.h"
#include "rewriter/Command.h"
#include "rewriter/DSLBuilder.h"
#include "rewriter/DefaultArgs.h"
#include "rewriter/Delegate.h"
#include "rewriter/Flatfiles.h"
#include "rewriter/InterfaceWrapper.h"
#include "rewriter/Mattr.h"
#include "rewriter/Minitest.h"
#include "rewriter/MixinEncryptedProp.h"
#include "rewriter/Private.h"
#include "rewriter/Prop.h"
#include "rewriter/ProtobufDescriptorPool.h"
#include "rewriter/Rails.h"
#include "rewriter/Regexp.h"
#include "rewriter/SelfNew.h"
#include "rewriter/Struct.h"
#include "rewriter/TEnum.h"
#include "rewriter/TypeMembers.h"
#include "rewriter/attr_reader.h"
#include "rewriter/cleanup.h"
#include "rewriter/flatten.h"
#include "rewriter/module_function.h"
using namespace std;
namespace sorbet::rewriter {
class Rewriterer {
friend class Rewriter;
public:
unique_ptr<ast::ClassDef> postTransformClassDef(core::MutableContext ctx, unique_ptr<ast::ClassDef> classDef) {
Command::run(ctx, classDef.get());
Rails::run(ctx, classDef.get());
TEnum::run(ctx, classDef.get());
Flatfiles::run(ctx, classDef.get());
Prop::run(ctx, classDef.get());
TypeMembers::run(ctx, classDef.get());
DefaultArgs::run(ctx, classDef.get());
for (auto &extension : ctx.state.semanticExtensions) {
extension->run(ctx, classDef.get());
}
ast::Expression *prevStat = nullptr;
UnorderedMap<ast::Expression *, vector<unique_ptr<ast::Expression>>> replaceNodes;
for (auto &stat : classDef->rhs) {
typecase(
stat.get(),
[&](ast::Assign *assign) {
vector<unique_ptr<ast::Expression>> nodes;
nodes = Struct::run(ctx, assign);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
nodes = ClassNew::run(ctx, assign);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
nodes = ProtobufDescriptorPool::run(ctx, assign);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
nodes = Regexp::run(ctx, assign);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
},
[&](ast::Send *send) {
vector<unique_ptr<ast::Expression>> nodes;
nodes = MixinEncryptedProp::run(ctx, send);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
nodes = Minitest::run(ctx, send);
if (!nodes.empty()) {
replaceNodes[stat.get()] = move(nodes);
return;
}
nodes = DSLBuilder::run(ctx, send);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
nodes = Private::run(ctx, send);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
nodes = Delegate::run(ctx, send);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
// This one is different: it gets an extra prevStat argument.
nodes = AttrReader::run(ctx, send, prevStat);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
// This one is also a little different: it gets the ClassDef kind
nodes = Mattr::run(ctx, send, classDef->kind);
if (!nodes.empty()) {
replaceNodes[stat.get()] = std::move(nodes);
return;
}
},
[&](ast::Expression *e) {});
prevStat = stat.get();
}
if (replaceNodes.empty()) {
ModuleFunction::run(ctx, classDef.get());
return classDef;
}
auto oldRHS = std::move(classDef->rhs);
classDef->rhs.clear();
classDef->rhs.reserve(oldRHS.size());
for (auto &stat : oldRHS) {
if (replaceNodes.find(stat.get()) == replaceNodes.end()) {
classDef->rhs.emplace_back(std::move(stat));
} else {
for (auto &newNode : replaceNodes.at(stat.get())) {
classDef->rhs.emplace_back(std::move(newNode));
}
}
}
ModuleFunction::run(ctx, classDef.get());
return classDef;
}
// NOTE: this case differs from the `Send` typecase branch in `postTransformClassDef` above, as it will apply to all
// sends, not just those that are present in the RHS of a `ClassDef`.
unique_ptr<ast::Expression> postTransformSend(core::MutableContext ctx, unique_ptr<ast::Send> send) {
if (auto expr = InterfaceWrapper::run(ctx, send.get())) {
return expr;
}
if (auto expr = SelfNew::run(ctx, send.get())) {
return expr;
}
return send;
}
private:
Rewriterer() = default;
};
unique_ptr<ast::Expression> Rewriter::run(core::MutableContext ctx, unique_ptr<ast::Expression> tree) {
auto ast = std::move(tree);
Rewriterer rewriter;
ast = ast::TreeMap::apply(ctx, rewriter, std::move(ast));
// This AST flattening pass requires that we mutate the AST in a way that our previous DSL passes were not designed
// around, which is why it runs all at once and is not expressed as a `patch` method like the other DSL passes. This
// is a rare case: in general, we should *not* add new DSL passes here.
auto flattened = Flatten::run(ctx, std::move(ast));
auto cleaned = Cleanup::run(ctx, std::move(flattened));
auto verifiedResult = ast::Verifier::run(ctx, std::move(cleaned));
return verifiedResult;
}
}; // namespace sorbet::rewriter