forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathType.hpp
More file actions
430 lines (351 loc) · 11.1 KB
/
Copy pathType.hpp
File metadata and controls
430 lines (351 loc) · 11.1 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#pragma once
#include <Python.h>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <mutex>
#include <set>
#include <map>
#include <tuple>
#include <utility>
#include <atomic>
#include <iostream>
#include "ReprAccumulator.hpp"
#include "SerializationContext.hpp"
#include "HashAccumulator.hpp"
#include "util.hpp"
class SerializationBuffer;
class DeserializationBuffer;
class Type;
class None;
class Bool;
class UInt8;
class UInt16;
class UInt32;
class UInt64;
class Int8;
class Int16;
class Int32;
class Int64;
class Float32;
class Float64;
class String;
class Bytes;
class OneOf;
class Value;
class TupleOf;
class PointerTo;
class ListOf;
class NamedTuple;
class Tuple;
class Dict;
class ConstDict;
class Alternative;
class ConcreteAlternative;
class PythonSubclass;
class PythonObjectOfType;
class Class;
class HeldClass;
class Function;
class BoundMethod;
class Forward;
typedef uint8_t* instance_ptr;
typedef void (*compiled_code_entrypoint)(instance_ptr, instance_ptr*);
void updateTypeRepForType(Type* t, PyTypeObject* pyType);
class Type {
public:
enum TypeCategory {
catNone,
catBool,
catUInt8,
catUInt16,
catUInt32,
catUInt64,
catInt8,
catInt16,
catInt32,
catInt64,
catString,
catBytes,
catFloat32,
catFloat64,
catValue,
catOneOf,
catTupleOf,
catPointerTo,
catListOf,
catNamedTuple,
catTuple,
catDict,
catConstDict,
catAlternative,
catConcreteAlternative, //concrete Alternative subclass
catPythonSubclass, //subclass of a nativepython type
catPythonObjectOfType, //a python object that matches 'isinstance' on a particular type
catBoundMethod,
catClass,
catHeldClass,
catFunction,
catForward
};
TypeCategory getTypeCategory() const {
return m_typeCategory;
}
bool isComposite() const {
return (
m_typeCategory == catTuple ||
m_typeCategory == catNamedTuple
);
}
const std::string& name() const {
return m_name;
}
size_t bytecount() const {
return m_size;
}
Type* pickConcreteSubclass(instance_ptr data) {
assertForwardsResolved();
return this->check([&](auto& subtype) {
return subtype.pickConcreteSubclassConcrete(data);
});
}
Type* pickConcreteSubclassConcrete(instance_ptr data) {
return this;
}
void repr(instance_ptr self, ReprAccumulator& out);
bool cmp(instance_ptr left, instance_ptr right, int pyComparisonOp);
int32_t hash32(instance_ptr left);
template<class buf_t>
void serialize(instance_ptr left, buf_t& buffer) {
assertForwardsResolved();
return this->check([&](auto& subtype) {
return subtype.serialize(left, buffer);
});
}
template<class buf_t>
void deserialize(instance_ptr left, buf_t& buffer) {
assertForwardsResolved();
return this->check([&](auto& subtype) {
return subtype.deserialize(left, buffer);
});
}
void swap(instance_ptr left, instance_ptr right);
static char byteCompare(uint8_t* l, uint8_t* r, size_t count);
template<class T>
auto check(const T& f) -> decltype(f(*this)) {
switch (m_typeCategory) {
case catNone:
return f(*(None*)this);
case catBool:
return f(*(Bool*)this);
case catUInt8:
return f(*(UInt8*)this);
case catUInt16:
return f(*(UInt16*)this);
case catUInt32:
return f(*(UInt32*)this);
case catUInt64:
return f(*(UInt64*)this);
case catInt8:
return f(*(Int8*)this);
case catInt16:
return f(*(Int16*)this);
case catInt32:
return f(*(Int32*)this);
case catInt64:
return f(*(Int64*)this);
case catString:
return f(*(String*)this);
case catBytes:
return f(*(Bytes*)this);
case catFloat32:
return f(*(Float32*)this);
case catFloat64:
return f(*(Float64*)this);
case catValue:
return f(*(Value*)this);
case catOneOf:
return f(*(OneOf*)this);
case catTupleOf:
return f(*(TupleOf*)this);
case catPointerTo:
return f(*(PointerTo*)this);
case catListOf:
return f(*(ListOf*)this);
case catNamedTuple:
return f(*(NamedTuple*)this);
case catTuple:
return f(*(Tuple*)this);
case catDict:
return f(*(Dict*)this);
case catConstDict:
return f(*(ConstDict*)this);
case catAlternative:
return f(*(Alternative*)this);
case catConcreteAlternative:
return f(*(ConcreteAlternative*)this);
case catPythonSubclass:
return f(*(PythonSubclass*)this);
case catPythonObjectOfType:
return f(*(PythonObjectOfType*)this);
case catClass:
return f(*(Class*)this);
case catHeldClass:
return f(*(HeldClass*)this);
case catFunction:
return f(*(Function*)this);
case catBoundMethod:
return f(*(BoundMethod*)this);
case catForward:
return f(*(Forward*)this);
default:
throw std::runtime_error("Invalid type found");
}
}
Type* getBaseType() const {
return m_base;
}
void assertForwardsResolved() const {
if (m_references_unresolved_forwards || m_failed_resolution) {
throw std::logic_error("Type has unresolved forwards.");
}
}
//this MUST be called while holding the GIL
template<class resolve_py_callable_to_type>
Type* guaranteeForwardsResolved(const resolve_py_callable_to_type& resolver) {
if (m_failed_resolution) {
throw std::runtime_error("Type failed to resolve the first time it was triggered.");
}
if (m_checking_for_references_unresolved_forwards) {
//it's ok to bail early. the call stack will recurse
//back to this point, and during the unwind will ensure
//that we check that any forwards are resolved.
return this;
}
if (m_references_unresolved_forwards) {
m_checking_for_references_unresolved_forwards = true;
m_references_unresolved_forwards = false;
Type* res;
try {
res = this->check([&](auto& subtype) {
return subtype.guaranteeForwardsResolvedConcrete(resolver);
});
} catch(...) {
m_checking_for_references_unresolved_forwards = false;
m_failed_resolution = true;
throw;
}
m_checking_for_references_unresolved_forwards = false;
forwardTypesMayHaveChanged();
if (res != this) {
return res->guaranteeForwardsResolved(resolver);
}
return res;
}
return this;
}
template<class resolve_py_callable_to_type>
Type* guaranteeForwardsResolvedConcrete(resolve_py_callable_to_type& resolver) {
typedef Type* type_ptr;
bool didSomething = false;
visitReferencedTypes([&](type_ptr& t) {
t = t->guaranteeForwardsResolved(resolver);
});
forwardTypesMayHaveChanged();
return this;
}
void _forwardTypesMayHaveChanged() {}
template<class visitor_type>
void _visitReferencedTypes(const visitor_type& v) {}
template<class visitor_type>
void _visitContainedTypes(const visitor_type& v) {}
// call subtype.constructor
void constructor(instance_ptr self);
template<class ptr_func>
void constructor(int64_t count, const ptr_func& ptrToChild) {
assertForwardsResolved();
this->check([&](auto& subtype) {
for (long k = 0; k < count; k++) {
subtype.constructor(ptrToChild(k));
}
});
}
// call subtype.destroy
void destroy(instance_ptr self);
template<class ptr_func>
void destroy(int64_t count, const ptr_func& ptrToChild) {
assertForwardsResolved();
this->check([&](auto& subtype) {
for (long k = 0; k < count; k++) {
subtype.destroy(ptrToChild(k));
}
});
}
template<class visitor_type>
void visitContainedTypes(const visitor_type& v) {
this->check([&](auto& subtype) {
subtype._visitContainedTypes(v);
});
}
template<class visitor_type>
void visitReferencedTypes(const visitor_type& v) {
this->check([&](auto& subtype) {
subtype._visitReferencedTypes(v);
});
}
void forwardTypesMayHaveChanged();
// call subtype.copy_constructor
void copy_constructor(instance_ptr self, instance_ptr other);
template<class ptr_func_dest, class ptr_func_src>
void copy_constructor(int64_t count, const ptr_func_dest& ptrToTarget, const ptr_func_src& ptrToSrc) {
assertForwardsResolved();
this->check([&](auto& subtype) {
for (long k = 0; k < count; k++) {
subtype.copy_constructor(ptrToTarget(k), ptrToSrc(k));
}
});
}
// call subtype.assign
void assign(instance_ptr self, instance_ptr other);
PyTypeObject* getTypeRep() const {
return mTypeRep;
}
void setTypeRep(PyTypeObject* o) {
mTypeRep = o;
}
bool is_default_constructible() const {
return m_is_default_constructible;
}
bool references_unresolved_forwards() const {
return m_references_unresolved_forwards;
}
bool isBinaryCompatibleWith(Type* other);
bool isBinaryCompatibleWithConcrete(Type* other) {
return false;
}
protected:
Type(TypeCategory in_typeCategory) :
m_typeCategory(in_typeCategory),
m_size(0),
m_is_default_constructible(false),
m_name("Undefined"),
mTypeRep(nullptr),
m_base(nullptr),
m_references_unresolved_forwards(false),
m_checking_for_references_unresolved_forwards(false),
m_failed_resolution(false)
{}
TypeCategory m_typeCategory;
size_t m_size;
bool m_is_default_constructible;
std::string m_name;
PyTypeObject* mTypeRep;
Type* m_base;
bool m_references_unresolved_forwards;
bool m_checking_for_references_unresolved_forwards;
bool m_failed_resolution;
std::set<Type*> mUses;
enum BinaryCompatibilityCategory { Incompatible, Checking, Compatible };
std::map<Type*, BinaryCompatibilityCategory> mIsBinaryCompatible;
};