forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cc
257 lines (226 loc) · 8.97 KB
/
Util.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "rewriter/Util.h"
#include "ast/Helpers.h"
#include "ast/ast.h"
#include "core/core.h"
#include "rewriter/rewriter.h"
using namespace std;
namespace sorbet::rewriter {
ast::ExpressionPtr ASTUtil::dupType(const ast::ExpressionPtr &orig) {
auto send = ast::cast_tree<ast::Send>(orig);
if (send) {
ast::Send::ARGS_store args;
auto dupRecv = dupType(send->recv);
if (!dupRecv) {
return nullptr;
}
if (send->fun == core::Names::enum_()) {
// T.enum() is weird, and accepts values instead of types. Just copy
// it blindly through.
return send->deepCopy();
}
if (send->fun == core::Names::params() && send->numPosArgs == 0 && send->args.size() % 2 == 0) {
// T.proc.params takes inlined keyword argument pairs, and can't handle kwsplat
ast::Send::ARGS_store args;
for (auto i = 0; i < send->args.size(); i += 2) {
ENFORCE(ast::isa_tree<ast::Literal>(send->args[i]));
args.emplace_back(send->args[i].deepCopy());
auto dupedValue = ASTUtil::dupType(send->args[i + 1]);
if (dupedValue == nullptr) {
return nullptr;
}
args.emplace_back(std::move(dupedValue));
}
return ast::MK::Send(send->loc, std::move(dupRecv), send->fun, 0, std::move(args));
}
for (auto &arg : send->args) {
auto dupArg = dupType(arg);
if (!dupArg) {
// This isn't a Type signature, bail out
return nullptr;
}
args.emplace_back(std::move(dupArg));
}
return ast::MK::Send(send->loc, std::move(dupRecv), send->fun, send->numPosArgs, std::move(args));
}
auto *ident = ast::cast_tree<ast::ConstantLit>(orig);
if (ident) {
auto orig = dupType(ident->original);
if (ident->original && !orig) {
return nullptr;
}
return ast::make_expression<ast::ConstantLit>(ident->loc, ident->symbol, std::move(orig));
}
auto *cons = ast::cast_tree<ast::UnresolvedConstantLit>(orig);
if (!cons) {
return nullptr;
}
auto *scopeCnst = ast::cast_tree<ast::UnresolvedConstantLit>(cons->scope);
if (!scopeCnst) {
if (ast::isa_tree<ast::EmptyTree>(cons->scope)) {
return ast::MK::UnresolvedConstant(cons->loc, ast::MK::EmptyTree(), cons->cnst);
}
auto *id = ast::cast_tree<ast::ConstantLit>(cons->scope);
if (id == nullptr) {
return nullptr;
}
ENFORCE(id->symbol == core::Symbols::root());
return ast::MK::UnresolvedConstant(cons->loc, dupType(cons->scope), cons->cnst);
}
auto scope = dupType(cons->scope);
if (scope == nullptr) {
return nullptr;
}
return ast::MK::UnresolvedConstant(cons->loc, std::move(scope), cons->cnst);
}
bool ASTUtil::hasHashValue(core::MutableContext ctx, const ast::Hash &hash, core::NameRef name) {
for (const auto &keyExpr : hash.keys) {
auto *key = ast::cast_tree<ast::Literal>(keyExpr);
if (key && key->isSymbol(ctx) && key->asSymbol(ctx) == name) {
return true;
}
}
return false;
}
bool ASTUtil::hasTruthyHashValue(core::MutableContext ctx, const ast::Hash &hash, core::NameRef name) {
int i = -1;
for (const auto &keyExpr : hash.keys) {
i++;
auto *key = ast::cast_tree<ast::Literal>(keyExpr);
if (key && key->isSymbol(ctx) && key->asSymbol(ctx) == name) {
auto *val = ast::cast_tree<ast::Literal>(hash.values[i]);
if (!val) {
// All non-literals are truthy
return true;
}
if (val->isNil(ctx) || val->isFalse(ctx)) {
return false;
}
return true;
}
}
return false;
}
pair<ast::ExpressionPtr, ast::ExpressionPtr> ASTUtil::extractHashValue(core::MutableContext ctx, ast::Hash &hash,
core::NameRef name) {
int i = -1;
for (auto &keyExpr : hash.keys) {
i++;
auto *key = ast::cast_tree<ast::Literal>(keyExpr);
if (key && key->isSymbol(ctx) && key->asSymbol(ctx) == name) {
auto key = std::move(keyExpr);
auto value = std::move(hash.values[i]);
hash.keys.erase(hash.keys.begin() + i);
hash.values.erase(hash.values.begin() + i);
return make_pair(move(key), move(value));
}
}
return make_pair(nullptr, nullptr);
}
ast::Send *ASTUtil::castSig(ast::ExpressionPtr &expr) {
auto *send = ast::cast_tree<ast::Send>(expr);
if (send == nullptr) {
return nullptr;
}
return ASTUtil::castSig(send);
}
// This will return nullptr if the argument is not the right shape as a sig (i.e. a send to a method called `sig` with 0
// or 1 arguments, that in turn contains a block that contains a send) and it also checks the final method of the send
// against the provided `returns` (so that some uses can specifically look for `void` sigs while others can specifically
// look for non-void sigs).
ast::Send *ASTUtil::castSig(ast::Send *send) {
if (send->fun != core::Names::sig()) {
return nullptr;
}
if (send->block.get() == nullptr) {
return nullptr;
}
// 0 args is common case
// 1 arg is `sig(:final)`
// 2 args is `Sorbet::Private::Static.sig(self, :final)`
if (send->args.size() > 2) {
return nullptr;
}
auto *block = ast::cast_tree<ast::Block>(send->block);
ENFORCE(block);
auto *body = ast::cast_tree<ast::Send>(block->body);
while (body != nullptr && (body->fun == core::Names::checked() || body->fun == core::Names::onFailure())) {
body = ast::cast_tree<ast::Send>(body->recv);
}
if (body != nullptr && (body->fun == core::Names::void_() || body->fun == core::Names::returns())) {
return send;
} else {
return nullptr;
}
}
ast::ExpressionPtr ASTUtil::mkKwArgsHash(const ast::Send *send) {
if (send->args.empty()) {
return nullptr;
}
ast::Hash::ENTRY_store keys;
ast::Hash::ENTRY_store values;
auto [kwStart, kwEnd] = send->kwArgsRange();
for (auto i = kwStart; i < kwEnd; i += 2) {
keys.emplace_back(send->args[i].deepCopy());
values.emplace_back(send->args[i + 1].deepCopy());
}
// handle a double-splat or a hash literal as the last argument
bool explicitEmptyHash = false;
if (send->hasKwSplat() || !send->hasKwArgs()) {
if (auto *hash = ast::cast_tree<ast::Hash>(send->args.back())) {
explicitEmptyHash = hash->keys.empty();
for (auto i = 0; i < hash->keys.size(); ++i) {
keys.emplace_back(hash->keys[i].deepCopy());
values.emplace_back(hash->values[i].deepCopy());
}
}
}
if (!keys.empty() || explicitEmptyHash) {
return ast::MK::Hash(send->loc, std::move(keys), std::move(values));
} else {
return nullptr;
}
}
ast::ExpressionPtr ASTUtil::mkGet(core::Context ctx, core::LocOffsets loc, core::NameRef name, ast::ExpressionPtr rhs,
bool isAttrReader) {
auto ret = ast::MK::SyntheticMethod0(loc, loc, name, move(rhs));
ast::cast_tree_nonnull<ast::MethodDef>(ret).flags.isAttrReader = isAttrReader;
return ret;
}
ast::ExpressionPtr ASTUtil::mkSet(core::Context ctx, core::LocOffsets loc, core::NameRef name, core::LocOffsets argLoc,
ast::ExpressionPtr rhs) {
return ast::MK::SyntheticMethod1(loc, loc, name, ast::MK::Local(argLoc, core::Names::arg0()), move(rhs));
}
ast::ExpressionPtr ASTUtil::mkNilable(core::LocOffsets loc, ast::ExpressionPtr type) {
return ast::MK::Send1(loc, ast::MK::T(loc), core::Names::nilable(), move(type));
}
namespace {
// Returns `true` when the expression passed is an UnresolvedConstantLit with the name `Kernel` and no additional scope.
bool isKernel(const ast::ExpressionPtr &expr) {
if (auto *constRecv = ast::cast_tree<ast::UnresolvedConstantLit>(expr)) {
return ast::isa_tree<ast::EmptyTree>(constRecv->scope) && constRecv->cnst == core::Names::Constants::Kernel();
}
return false;
}
} // namespace
ast::ExpressionPtr ASTUtil::thunkBody(core::MutableContext ctx, ast::ExpressionPtr &node) {
auto *send = ast::cast_tree<ast::Send>(node);
if (send == nullptr) {
return nullptr;
}
if (send->fun != core::Names::lambda() && send->fun != core::Names::proc()) {
return nullptr;
}
// Valid receivers for lambda/proc are either a self reference or `Kernel`
if (!send->recv.isSelfReference() && !isKernel(send->recv)) {
return nullptr;
}
if (send->block == nullptr) {
return nullptr;
}
auto *block = ast::cast_tree<ast::Block>(send->block);
if (!block->args.empty()) {
return nullptr;
}
return std::move(block->body);
}
} // namespace sorbet::rewriter