forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStruct.cc
186 lines (151 loc) · 6.22 KB
/
Struct.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
#include "rewriter/Struct.h"
#include "absl/strings/match.h"
#include "ast/Helpers.h"
#include "ast/ast.h"
#include "core/Context.h"
#include "core/Names.h"
#include "core/core.h"
#include "core/errors/rewriter.h"
#include "rewriter/Util.h"
#include "rewriter/rewriter.h"
using namespace std;
namespace sorbet::rewriter {
namespace {
bool isKeywordInitKey(const core::GlobalState &gs, const ast::ExpressionPtr &node) {
if (auto *lit = ast::cast_tree<ast::Literal>(node)) {
return lit->isSymbol(gs) && lit->asSymbol(gs) == core::Names::keywordInit();
}
return false;
}
bool isMissingInitialize(const core::GlobalState &gs, const ast::Send *send) {
if (!send->hasBlock()) {
return true;
}
auto block = send->block();
if (auto *insSeq = ast::cast_tree<ast::InsSeq>(block->body)) {
auto methodDef = ast::cast_tree<ast::MethodDef>(insSeq->expr);
if (methodDef && methodDef->name == core::Names::initialize()) {
return false;
}
for (auto &&stat : insSeq->stats) {
methodDef = ast::cast_tree<ast::MethodDef>(stat);
if (methodDef && methodDef->name == core::Names::initialize()) {
return false;
}
}
}
return true;
}
} // namespace
vector<ast::ExpressionPtr> Struct::run(core::MutableContext ctx, ast::Assign *asgn) {
vector<ast::ExpressionPtr> empty;
if (ctx.state.runningUnderAutogen) {
return empty;
}
auto lhs = ast::cast_tree<ast::UnresolvedConstantLit>(asgn->lhs);
if (lhs == nullptr) {
return empty;
}
auto send = ast::cast_tree<ast::Send>(asgn->rhs);
if (send == nullptr) {
return empty;
}
auto recv = ast::cast_tree<ast::UnresolvedConstantLit>(send->recv);
if (recv == nullptr) {
return empty;
}
if (!ast::MK::isRootScope(recv->scope) || recv->cnst != core::Symbols::Struct().data(ctx)->name ||
send->fun != core::Names::new_() || (!send->hasPosArgs() && !send->hasKwArgs())) {
return empty;
}
auto loc = asgn->loc;
ast::MethodDef::ARGS_store newArgs;
ast::Send::ARGS_store sigArgs;
ast::ClassDef::RHS_store body;
bool keywordInit = false;
if (send->hasKwArgs()) {
if (!send->hasPosArgs()) {
// leave bad usages like `Struct.new(keyword_init: true)` untouched so we error later
return empty;
}
if (send->hasKwSplat()) {
return empty;
}
if (send->numKwArgs() != 1) {
return empty;
}
if (!isKeywordInitKey(ctx, send->getKwKey(0))) {
return empty;
}
if (auto *lit = ast::cast_tree<ast::Literal>(send->getKwValue(0))) {
if (lit->isTrue(ctx)) {
keywordInit = true;
} else if (!lit->isFalse(ctx)) {
return empty;
}
} else {
return empty;
}
}
for (int i = 0; i < send->numPosArgs(); i++) {
auto *sym = ast::cast_tree<ast::Literal>(send->getPosArg(i));
if (!sym || !sym->isSymbol(ctx)) {
return empty;
}
core::NameRef name = sym->asSymbol(ctx);
auto symLoc = sym->loc;
auto strname = name.shortName(ctx);
if (!strname.empty() && strname.back() == '=') {
if (auto e = ctx.beginError(symLoc, core::errors::Rewriter::InvalidStructMember)) {
e.setHeader("Struct member `{}` cannot end with an equal", strname);
}
}
// TODO(jez) Use Loc::adjust here
if (symLoc.exists() && absl::StartsWith(core::Loc(ctx.file, symLoc).source(ctx).value(), ":")) {
symLoc = core::LocOffsets{symLoc.beginPos() + 1, symLoc.endPos()};
}
sigArgs.emplace_back(ast::MK::Symbol(symLoc, name));
sigArgs.emplace_back(ast::MK::Constant(symLoc, core::Symbols::BasicObject()));
auto argName = ast::MK::Local(symLoc, name);
if (keywordInit) {
argName = ast::make_expression<ast::KeywordArg>(symLoc, move(argName));
}
newArgs.emplace_back(ast::MK::OptionalArg(symLoc, move(argName), ast::MK::Nil(symLoc)));
body.emplace_back(ast::MK::SyntheticMethod0(symLoc, symLoc, name, ast::MK::RaiseUnimplemented(loc)));
body.emplace_back(ast::MK::SyntheticMethod1(symLoc, symLoc, name.addEq(ctx), ast::MK::Local(symLoc, name),
ast::MK::RaiseUnimplemented(loc)));
}
// Elem = type_member(fixed: T.untyped)
{
auto typeMember =
ast::MK::Send(loc, ast::MK::Self(loc), core::Names::typeMember(), loc.copyWithZeroLength(), 0,
ast::MK::SendArgs(ast::MK::Symbol(loc, core::Names::fixed()), ast::MK::Untyped(loc)));
body.emplace_back(
ast::MK::Assign(loc, ast::MK::UnresolvedConstant(loc, ast::MK::EmptyTree(), core::Names::Constants::Elem()),
std::move(typeMember)));
}
if (isMissingInitialize(ctx, send)) {
body.emplace_back(ast::MK::SigVoid(loc, std::move(sigArgs)));
body.emplace_back(ast::MK::SyntheticMethod(loc, loc, core::Names::initialize(), std::move(newArgs),
ast::MK::RaiseUnimplemented(loc)));
}
if (auto *block = send->block()) {
// Steal the trees, because the run is going to remove the original send node from the tree anyway.
if (auto *insSeq = ast::cast_tree<ast::InsSeq>(block->body)) {
for (auto &&stat : insSeq->stats) {
body.emplace_back(move(stat));
}
body.emplace_back(move(insSeq->expr));
} else {
body.emplace_back(move(block->body));
}
// NOTE: the code in this block _STEALS_ trees. No _return empty_'s should go after it
}
ast::ClassDef::ANCESTORS_store ancestors;
ancestors.emplace_back(ast::MK::UnresolvedConstant(loc, ast::MK::Constant(loc, core::Symbols::root()),
core::Names::Constants::Struct()));
vector<ast::ExpressionPtr> stats;
stats.emplace_back(ast::MK::Class(loc, loc, std::move(asgn->lhs), std::move(ancestors), std::move(body)));
return stats;
}
}; // namespace sorbet::rewriter