forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternativeType.cpp
More file actions
213 lines (159 loc) · 5.75 KB
/
Copy pathAlternativeType.cpp
File metadata and controls
213 lines (159 loc) · 5.75 KB
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
#include "AllTypes.hpp"
bool Alternative::isBinaryCompatibleWithConcrete(Type* other) {
if (other->getTypeCategory() == TypeCategory::catConcreteAlternative) {
other = other->getBaseType();
}
if (other->getTypeCategory() != m_typeCategory) {
return false;
}
Alternative* otherO = (Alternative*)other;
if (m_subtypes.size() != otherO->m_subtypes.size()) {
return false;
}
for (long k = 0; k < m_subtypes.size(); k++) {
if (m_subtypes[k].first != otherO->m_subtypes[k].first) {
return false;
}
if (!m_subtypes[k].second->isBinaryCompatibleWith(otherO->m_subtypes[k].second)) {
return false;
}
}
return true;
}
int64_t Alternative::refcount(instance_ptr i) const {
if (m_all_alternatives_empty) {
return 0;
}
return ((layout**)i)[0]->refcount;
}
void Alternative::_forwardTypesMayHaveChanged() {
m_size = sizeof(void*);
m_is_default_constructible = false;
m_all_alternatives_empty = true;
m_arg_positions.clear();
m_default_construction_ix = 0;
m_default_construction_type = nullptr;
for (auto& subtype_pair: m_subtypes) {
if (subtype_pair.second->bytecount() > 0) {
m_all_alternatives_empty = false;
}
if (m_arg_positions.find(subtype_pair.first) != m_arg_positions.end()) {
throw std::runtime_error("Can't create an alternative with " +
subtype_pair.first + " defined twice.");
}
m_arg_positions[subtype_pair.first] = m_arg_positions.size();
if (subtype_pair.second->is_default_constructible() && !m_is_default_constructible) {
m_is_default_constructible = true;
m_default_construction_ix = m_arg_positions[subtype_pair.first];
}
}
m_size = (m_all_alternatives_empty ? 1 : sizeof(void*));
}
bool Alternative::cmp(instance_ptr left, instance_ptr right, int pyComparisonOp) {
if (m_all_alternatives_empty) {
if (*(uint8_t*)left < *(uint8_t*)right) {
return cmpResultToBoolForPyOrdering(pyComparisonOp, -1);
}
if (*(uint8_t*)left > *(uint8_t*)right) {
return cmpResultToBoolForPyOrdering(pyComparisonOp, 1);
}
return cmpResultToBoolForPyOrdering(pyComparisonOp, 0);
}
layout& record_l = **(layout**)left;
layout& record_r = **(layout**)right;
if ( &record_l == &record_r ) {
return cmpResultToBoolForPyOrdering(pyComparisonOp, 0);
}
if (record_l.which < record_r.which) {
return cmpResultToBoolForPyOrdering(pyComparisonOp, -1);
}
if (record_l.which > record_r.which) {
return cmpResultToBoolForPyOrdering(pyComparisonOp, 1);
}
return m_subtypes[record_l.which].second->cmp(record_l.data, record_r.data, pyComparisonOp);
}
void Alternative::repr(instance_ptr self, ReprAccumulator& stream) {
PushReprState isNew(stream, self);
if (!isNew) {
stream << m_name << "(" << (void*)self << ")";
return;
}
stream << m_subtypes[which(self)].first;
m_subtypes[which(self)].second->repr(eltPtr(self), stream);
}
int32_t Alternative::hash32(instance_ptr left) {
Hash32Accumulator acc((int)TypeCategory::catAlternative);
acc.add(which(left));
acc.add(m_subtypes[which(left)].second->hash32(eltPtr(left)));
return acc.get();
}
instance_ptr Alternative::eltPtr(instance_ptr self) const {
if (m_all_alternatives_empty) {
return self;
}
layout& record = **(layout**)self;
return record.data;
}
int64_t Alternative::which(instance_ptr self) const {
if (m_all_alternatives_empty) {
return *(uint8_t*)self;
}
layout& record = **(layout**)self;
return record.which;
}
void Alternative::destroy(instance_ptr self) {
if (m_all_alternatives_empty) {
return;
}
layout& record = **(layout**)self;
if (record.refcount.fetch_sub(1) == 1) {
m_subtypes[record.which].second->destroy(record.data);
free(*(layout**)self);
}
}
void Alternative::copy_constructor(instance_ptr self, instance_ptr other) {
if (m_all_alternatives_empty) {
*(uint8_t*)self = *(uint8_t*)other;
return;
}
(*(layout**)self) = (*(layout**)other);
if (*(layout**)self) {
(*(layout**)self)->refcount++;
}
}
void Alternative::assign(instance_ptr self, instance_ptr other) {
layout* old = (*(layout**)self);
(*(layout**)self) = (*(layout**)other);
if (*(layout**)self) {
(*(layout**)self)->refcount++;
}
destroy((instance_ptr)&old);
}
// static
Alternative* Alternative::Make(std::string name,
const std::vector<std::pair<std::string, NamedTuple*> >& types,
const std::map<std::string, Function*>& methods //methods preclude us from being in the memo
) {
if (methods.size()) {
return new Alternative(name, types, methods);
}
static std::mutex guard;
std::lock_guard<std::mutex> lock(guard);
typedef std::pair<std::string, std::vector<std::pair<std::string, NamedTuple*> > > keytype;
static std::map<keytype, Alternative*> m;
auto it = m.find(keytype(name, types));
if (it == m.end()) {
it = m.insert(std::make_pair(keytype(name, types), new Alternative(name, types, methods))).first;
}
return it->second;
}
Type* Alternative::pickConcreteSubclassConcrete(instance_ptr data) {
uint8_t i = which(data);
return ConcreteAlternative::Make(this, i);
}
void Alternative::constructor(instance_ptr self) {
if (!m_default_construction_type) {
m_default_construction_type = ConcreteAlternative::Make(this, m_default_construction_ix);
}
m_default_construction_type->constructor(self);
}