Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Executors.cxx
Go to the documentation of this file.
1// Bindings
2#include "cpyrt.h"
3
4using namespace cppjit;
5#include "CPPInstance.h"
6#include "DeclareExecutors.h"
7#include "LowLevelViews.h"
8#include "ProxyWrappers.h"
9#include "PyStrings.h"
10#include "TypeManip.h"
11#include "Utility.h"
12#include "cppjit_interop.h"
13
14// Standard
15#include <complex>
16#include <cstring>
17#include <map>
18#include <new>
19#include <sstream>
20#include <sys/types.h>
21#include <utility>
22
23//- data _____________________________________________________________________
24namespace cppjit::cpyrt {
25typedef std::unordered_map<std::string, ef_t> ExecFactories_t;
27
29
30extern std::set<std::string> gIteratorTypes;
31} // namespace cppjit::cpyrt
32
33//- helpers ------------------------------------------------------------------
34namespace {
35
36#ifdef WITH_THREAD
37class GILControl {
38public:
39 GILControl() : fSave(PyEval_SaveThread()) {}
40 ~GILControl() { PyEval_RestoreThread(fSave); }
41
42private:
43 PyThreadState* fSave;
44};
45#endif
46
47} // unnamed namespace
48
49#ifdef WITH_THREAD
50#define CPPJIT_IMPL_GILCALL(rtype, tcode) \
51 static inline rtype GILCall##tcode(interop::TCppMethod_t method, \
52 interop::TCppObject_t self, \
53 cpyrt::CallContext* ctxt) { \
54 if (!ReleasesGIL(ctxt)) \
55 return interop::Call##tcode(method, self, ctxt->GetEncodedSize(), \
56 ctxt->GetArgs()); \
57 GILControl gc{}; \
58 return interop::Call##tcode(method, self, ctxt->GetEncodedSize(), \
59 ctxt->GetArgs()); \
60 }
61#else
62#define CPPJIT_IMPL_GILCALL(rtype, tcode) \
63 static inline rtype GILCall##tcode(interop::TCppMethod_t method, \
64 interop::TCppObject_t self, \
65 cpyrt::CallContext* ctxt) { \
66 return interop::Call##tcode(method, self, ctxt->GetEncodedSize(), \
67 ctxt->GetArgs()); \
68 }
69#endif
70
71// clang-format off
73CPPJIT_IMPL_GILCALL(unsigned char, B)
83// clang-format on
84
85static inline interop::TCppObject_t GILCallO(interop::TCppMethod_t method,
86 interop::TCppObject_t self,
87 cpyrt::CallContext* ctxt,
88 interop::TCppScope_t klass) {
90#ifdef WITH_THREAD
91 if (!ReleasesGIL(ctxt))
92#endif
93 return interop::CallO(method, self, ctxt->GetEncodedSize(), ctxt->GetArgs(),
94 klass_ty);
95#ifdef WITH_THREAD
96 GILControl gc{};
97 return interop::CallO(method, self, ctxt->GetEncodedSize(), ctxt->GetArgs(),
98 klass_ty);
99#endif
100}
101
102static inline interop::TCppObject_t
104 cpyrt::CallContext* ctxt) {
105#ifdef WITH_THREAD
106 if (!ReleasesGIL(ctxt))
107#endif
108 return interop::CallConstructor(method, klass, ctxt->GetEncodedSize(),
109 ctxt->GetArgs());
110#ifdef WITH_THREAD
111 GILControl gc{};
112 return interop::CallConstructor(method, klass, ctxt->GetEncodedSize(),
113 ctxt->GetArgs());
114#endif
115}
116
117static inline PyObject* cpyrt_PyText_FromLong(long cl) {
118 // python chars are range(256)
119 if (cl < -256 || cl > 255) {
120 PyErr_SetString(PyExc_ValueError, "char conversion out of range");
121 return nullptr;
122 }
123 int c = (int)cl;
124 if (c < 0)
125 return PyUnicode_FromFormat("%c", 256 - std::abs(c));
126 return PyUnicode_FromFormat("%c", c);
127}
128
129static inline PyObject* cpyrt_PyText_FromULong(unsigned long uc) {
130 // TODO: range check here?
131 if (255 < uc) {
132 PyErr_SetString(PyExc_ValueError, "char conversion out of range");
133 return nullptr;
134 }
135 int c = (int)uc;
136 return PyUnicode_FromFormat("%c", c);
137}
138
139static inline PyObject* cpyrt_PyBool_FromLong(long b) {
140 PyObject* result = (bool)b ? Py_True : Py_False;
141 Py_INCREF(result);
142 return result;
143}
144
145//- base executor implementation ---------------------------------------------
147
148//- executors for built-ins --------------------------------------------------
149PyObject* cpyrt::BoolExecutor::Execute(interop::TCppMethod_t method,
151 CallContext* ctxt) {
152 // execute <method> with argument <self, ctxt>, construct python bool return
153 // value
154 bool retval = GILCallB(method, self, ctxt);
155 PyObject* result = retval ? Py_True : Py_False;
156 Py_INCREF(result);
157 return result;
158}
159
160//----------------------------------------------------------------------------
161PyObject* cpyrt::BoolConstRefExecutor::Execute(interop::TCppMethod_t method,
163 CallContext* ctxt) {
164 // execute <method> with argument <self, ctxt>, construct python bool return
165 // value
166 return cpyrt_PyBool_FromLong(*((bool*)GILCallR(method, self, ctxt)));
167}
168
169//----------------------------------------------------------------------------
170PyObject* cpyrt::CharExecutor::Execute(interop::TCppMethod_t method,
172 CallContext* ctxt) {
173 // execute <method with argument <self, ctxt>, construct python string return
174 // value with the single char
175 return cpyrt_PyText_FromLong((int)GILCallC(method, self, ctxt));
176}
177
178//----------------------------------------------------------------------------
179PyObject* cpyrt::CharConstRefExecutor::Execute(interop::TCppMethod_t method,
181 CallContext* ctxt) {
182 // execute <method> with argument <self, ctxt>, construct python string return
183 // value with the single char
184 return cpyrt_PyText_FromLong(*((char*)GILCallR(method, self, ctxt)));
185}
186
187//----------------------------------------------------------------------------
188PyObject* cpyrt::UCharExecutor::Execute(interop::TCppMethod_t method,
190 CallContext* ctxt) {
191 // execute <method> with argument <self, args>, construct python string return
192 // value with the single char
193 return cpyrt_PyText_FromLong((unsigned char)GILCallB(method, self, ctxt));
194}
195
196//----------------------------------------------------------------------------
197PyObject* cpyrt::UCharConstRefExecutor::Execute(interop::TCppMethod_t method,
199 CallContext* ctxt) {
200 // execute <method> with argument <self, ctxt>, construct python string return
201 // value with the single char from the pointer return
202 return cpyrt_PyText_FromLong(*((unsigned char*)GILCallR(method, self, ctxt)));
203}
204
205//----------------------------------------------------------------------------
206PyObject* cpyrt::Int8ConstRefExecutor::Execute(interop::TCppMethod_t method,
208 CallContext* ctxt) {
209 return PyLong_FromLong(*((int8_t*)GILCallR(method, self, ctxt)));
210}
211
212//----------------------------------------------------------------------------
213PyObject* cpyrt::UInt8ConstRefExecutor::Execute(interop::TCppMethod_t method,
215 CallContext* ctxt) {
216 return PyLong_FromLong(*((uint8_t*)GILCallR(method, self, ctxt)));
217}
218
219//----------------------------------------------------------------------------
220PyObject* cpyrt::WCharExecutor::Execute(interop::TCppMethod_t method,
222 CallContext* ctxt) {
223 // execute <method> with argument <self, args>, construct python string return
224 // value with the single wide char
225 wchar_t res = (wchar_t)GILCallL(method, self, ctxt);
226 return PyUnicode_FromWideChar(&res, 1);
227}
228
229//----------------------------------------------------------------------------
230PyObject* cpyrt::Char16Executor::Execute(interop::TCppMethod_t method,
232 CallContext* ctxt) {
233 // execute <method> with argument <self, args>, construct python string return
234 // value with the single char16
235 char16_t res = (char16_t)GILCallL(method, self, ctxt);
236 return PyUnicode_DecodeUTF16((const char*)&res, sizeof(char16_t), nullptr,
237 nullptr);
238}
239
240//----------------------------------------------------------------------------
241PyObject* cpyrt::Char32Executor::Execute(interop::TCppMethod_t method,
243 CallContext* ctxt) {
244 // execute <method> with argument <self, args>, construct python string return
245 // value with the single char32
246 char32_t res = (char32_t)GILCallL(method, self, ctxt);
247 return PyUnicode_DecodeUTF32((const char*)&res, sizeof(char32_t), nullptr,
248 nullptr);
249}
250
251//----------------------------------------------------------------------------
252PyObject* cpyrt::IntExecutor::Execute(interop::TCppMethod_t method,
254 CallContext* ctxt) {
255 // execute <method> with argument <self, ctxt>, construct python int return
256 // value
257 return PyLong_FromLong((int)GILCallI(method, self, ctxt));
258}
259
260//----------------------------------------------------------------------------
261PyObject* cpyrt::Int8Executor::Execute(interop::TCppMethod_t method,
263 CallContext* ctxt) {
264 // execute <method> with argument <self, ctxt>, construct python int return
265 // value
266 return PyLong_FromLong((int8_t)GILCallC(method, self, ctxt));
267}
268
269//----------------------------------------------------------------------------
270PyObject* cpyrt::UInt8Executor::Execute(interop::TCppMethod_t method,
272 CallContext* ctxt) {
273 // execute <method> with argument <self, ctxt>, construct python int return
274 // value
275 return PyLong_FromLong((uint8_t)GILCallB(method, self, ctxt));
276}
277
278//----------------------------------------------------------------------------
279PyObject* cpyrt::ShortExecutor::Execute(interop::TCppMethod_t method,
281 CallContext* ctxt) {
282 // execute <method> with argument <self, ctxt>, construct python int return
283 // value
284 return PyLong_FromLong((short)GILCallH(method, self, ctxt));
285}
286
287//----------------------------------------------------------------------------
288PyObject* cpyrt::LongExecutor::Execute(interop::TCppMethod_t method,
290 CallContext* ctxt) {
291 // execute <method> with argument <self, ctxt>, construct python long return
292 // value
293 return PyLong_FromLong((long)GILCallL(method, self, ctxt));
294}
295
296//----------------------------------------------------------------------------
297PyObject* cpyrt::ULongExecutor::Execute(interop::TCppMethod_t method,
299 CallContext* ctxt) {
300 // execute <method> with argument <self, ctxt>, construct python unsigned long
301 // return value
302 return PyLong_FromUnsignedLong((unsigned long)GILCallLL(method, self, ctxt));
303}
304
305//----------------------------------------------------------------------------
306PyObject* cpyrt::LongLongExecutor::Execute(interop::TCppMethod_t method,
308 CallContext* ctxt) {
309 // execute <method> with argument <self, ctxt>, construct python long long
310 // return value
311 PY_LONG_LONG result = GILCallLL(method, self, ctxt);
312 return PyLong_FromLongLong(result);
313}
314
315//----------------------------------------------------------------------------
316PyObject* cpyrt::ULongLongExecutor::Execute(interop::TCppMethod_t method,
318 CallContext* ctxt) {
319 // execute <method> with argument <self, ctxt>, construct python unsigned long
320 // long return value
321 PY_ULONG_LONG result = (PY_ULONG_LONG)GILCallLL(method, self, ctxt);
322 return PyLong_FromUnsignedLongLong(result);
323}
324
325//----------------------------------------------------------------------------
326PyObject* cpyrt::FloatExecutor::Execute(interop::TCppMethod_t method,
328 CallContext* ctxt) {
329 // execute <method> with argument <self, ctxt>, construct python float return
330 // value
331 return PyFloat_FromDouble((double)GILCallF(method, self, ctxt));
332}
333
334//----------------------------------------------------------------------------
335PyObject* cpyrt::DoubleExecutor::Execute(interop::TCppMethod_t method,
337 CallContext* ctxt) {
338 // execute <method> with argument <self, ctxt>, construct python float return
339 // value
340 return PyFloat_FromDouble((double)GILCallD(method, self, ctxt));
341}
342
343//----------------------------------------------------------------------------
344PyObject* cpyrt::LongDoubleExecutor::Execute(interop::TCppMethod_t method,
346 CallContext* ctxt) {
347 // execute <method> with argument <self, ctxt>, construct python float return
348 // value
349 return PyFloat_FromDouble((double)GILCallLD(method, self, ctxt));
350}
351
352//----------------------------------------------------------------------------
354 // prepare "buffer" for by-ref returns, used with __setitem__
355 if (pyobject) {
356 Py_INCREF(pyobject);
357 fAssignable = pyobject;
358 return true;
359 }
360
361 fAssignable = nullptr;
362 return false;
363}
364
365//----------------------------------------------------------------------------
366#define CPPJIT_IMPL_REFEXEC(name, type, stype, F1, F2) \
367 PyObject* cpyrt::name##RefExecutor::Execute(interop::TCppMethod_t method, \
368 interop::TCppObject_t self, \
369 CallContext* ctxt) { \
370 type* ref = (type*)GILCallR(method, self, ctxt); \
371 if (!ref) { /* can happen if wrapper compilation fails */ \
372 PyErr_SetString(PyExc_ReferenceError, \
373 "attempt to access a null-pointer"); \
374 return nullptr; \
375 } \
376 if (!fAssignable) \
377 return F1((stype) * ref); \
378 else { \
379 *ref = (type)F2(fAssignable); \
380 Py_DECREF(fAssignable); \
381 fAssignable = nullptr; \
382 if (*ref == (type) - 1 && PyErr_Occurred()) \
383 return nullptr; \
384 Py_INCREF(Py_None); \
385 return Py_None; \
386 } \
387 }
388
389// clang-format off
390CPPJIT_IMPL_REFEXEC(Bool, bool, long, cpyrt_PyBool_FromLong, PyLong_AsLong)
391CPPJIT_IMPL_REFEXEC(Char, char, long, cpyrt_PyText_FromLong, PyLong_AsLong)
392CPPJIT_IMPL_REFEXEC(UChar, unsigned char, unsigned long, cpyrt_PyText_FromULong, PyLongOrInt_AsULong)
393CPPJIT_IMPL_REFEXEC(Int8, int8_t, long, PyLong_FromLong, PyLong_AsLong)
394CPPJIT_IMPL_REFEXEC(UInt8, uint8_t, unsigned long, PyLong_FromLong, PyLongOrInt_AsULong)
395CPPJIT_IMPL_REFEXEC(Short, short, long, PyLong_FromLong, PyLong_AsLong)
396CPPJIT_IMPL_REFEXEC(UShort, unsigned short, unsigned long, PyLong_FromLong, PyLongOrInt_AsULong)
397CPPJIT_IMPL_REFEXEC(Int, int, long, PyLong_FromLong, PyLong_AsLong)
398CPPJIT_IMPL_REFEXEC(UInt, unsigned int, unsigned long, PyLong_FromUnsignedLong, PyLongOrInt_AsULong)
399CPPJIT_IMPL_REFEXEC(Long, long, long, PyLong_FromLong, PyLong_AsLong)
400CPPJIT_IMPL_REFEXEC(ULong, unsigned long, unsigned long, PyLong_FromUnsignedLong, PyLongOrInt_AsULong)
401CPPJIT_IMPL_REFEXEC(LongLong, PY_LONG_LONG, PY_LONG_LONG, PyLong_FromLongLong, PyLong_AsLongLong)
402CPPJIT_IMPL_REFEXEC(ULongLong, PY_ULONG_LONG, PY_ULONG_LONG, PyLong_FromUnsignedLongLong, PyLongOrInt_AsULong64)
403CPPJIT_IMPL_REFEXEC(Float, float, double, PyFloat_FromDouble, PyFloat_AsDouble)
404CPPJIT_IMPL_REFEXEC(Double, double, double, PyFloat_FromDouble, PyFloat_AsDouble)
405CPPJIT_IMPL_REFEXEC(LongDouble, PY_LONG_DOUBLE, PY_LONG_DOUBLE, PyFloat_FromDouble, PyFloat_AsDouble)
406// clang-format on
407
408template <typename T>
409static inline PyObject* PyComplex_FromComplex(const std::complex<T>& c) {
410 return PyComplex_FromDoubles(c.real(), c.imag());
411}
412
413template <typename T>
414static inline std::complex<T> PyComplex_AsComplex(PyObject* pycplx) {
415 Py_complex cplx = PyComplex_AsCComplex(pycplx);
416 return std::complex<T>(cplx.real, cplx.imag);
417}
418
419CPPJIT_IMPL_REFEXEC(ComplexD, std::complex<double>, std::complex<double>,
420 PyComplex_FromComplex<double>, PyComplex_AsComplex<double>)
421
422//----------------------------------------------------------------------------
423PyObject* cpyrt::STLStringRefExecutor::Execute(interop::TCppMethod_t method,
425 CallContext* ctxt) {
426 // execute <method> with argument <self, ctxt>, return python string return
427 // value
428 std::string* result = (std::string*)GILCallR(method, self, ctxt);
429 if (!fAssignable) {
430 return PyUnicode_FromStringAndSize(result->c_str(), result->size());
431 }
432
433 if (!PyUnicode_Check(fAssignable)) {
434 PyErr_Format(PyExc_TypeError, "wrong type in assignment (string expected)");
435 return nullptr;
436 }
437
438 *result = std::string(PyUnicode_AsUTF8(fAssignable),
439 PyUnicode_GET_LENGTH(fAssignable));
440
441 Py_DECREF(fAssignable);
442 fAssignable = nullptr;
443
445}
446
447//----------------------------------------------------------------------------
448PyObject* cpyrt::VoidExecutor::Execute(interop::TCppMethod_t method,
450 CallContext* ctxt) {
451 // execute <method> with argument <self, ctxt>, return None
452 GILCallV(method, self, ctxt);
453 if (PyErr_Occurred())
454 return nullptr;
456}
457
458//----------------------------------------------------------------------------
459PyObject* cpyrt::CStringExecutor::Execute(interop::TCppMethod_t method,
461 CallContext* ctxt) {
462 // execute <method> with argument <self, ctxt>, construct python string return
463 // value
464 char* result = (char*)GILCallR(method, self, ctxt);
465 if (!result) {
466 Py_INCREF(PyStrings::gEmptyString);
468 }
469
470 return PyUnicode_FromString(result);
471}
472
473//----------------------------------------------------------------------------
474PyObject* cpyrt::CStringRefExecutor::Execute(interop::TCppMethod_t method,
476 CallContext* ctxt) {
477 // execute <method> with argument <self, ctxt>, construct python string return
478 // value
479 char** result = (char**)GILCallR(method, self, ctxt);
480 if (!result || !*result) {
481 Py_INCREF(PyStrings::gEmptyString);
483 }
484
485 return PyUnicode_FromString(*result);
486}
487
488//----------------------------------------------------------------------------
489PyObject* cpyrt::WCStringExecutor::Execute(interop::TCppMethod_t method,
491 CallContext* ctxt) {
492 // execute <method> with argument <self, ctxt>, construct python unicode
493 // return value
494 wchar_t* result = (wchar_t*)GILCallR(method, self, ctxt);
495 if (!result) {
496 wchar_t w = L'\0';
497 return PyUnicode_FromWideChar(&w, 0);
498 }
499
500 return PyUnicode_FromWideChar(result, wcslen(result));
501}
502
503//----------------------------------------------------------------------------
504PyObject* cpyrt::CString16Executor::Execute(interop::TCppMethod_t method,
506 CallContext* ctxt) {
507 // execute <method> with argument <self, ctxt>, construct python unicode
508 // return value
509 char16_t* result = (char16_t*)GILCallR(method, self, ctxt);
510 if (!result) {
511 char16_t w = u'\0';
512 return PyUnicode_DecodeUTF16((const char*)&w, 0, nullptr, nullptr);
513 }
514
515 return PyUnicode_DecodeUTF16((const char*)result,
516 std::char_traits<char16_t>::length(result) *
517 sizeof(char16_t),
518 nullptr, nullptr);
519}
520
521//----------------------------------------------------------------------------
522PyObject* cpyrt::CString32Executor::Execute(interop::TCppMethod_t method,
524 CallContext* ctxt) {
525 // execute <method> with argument <self, ctxt>, construct python unicode
526 // return value
527 char32_t* result = (char32_t*)GILCallR(method, self, ctxt);
528 if (!result) {
529 char32_t w = U'\0';
530 return PyUnicode_DecodeUTF32((const char*)&w, 0, nullptr, nullptr);
531 }
532
533 return PyUnicode_DecodeUTF32((const char*)result,
534 std::char_traits<char32_t>::length(result) *
535 sizeof(char32_t),
536 nullptr, nullptr);
537}
538
539//- pointer/array executors --------------------------------------------------
540PyObject* cpyrt::VoidArrayExecutor::Execute(interop::TCppMethod_t method,
542 CallContext* ctxt) {
543 // execute <method> with argument <self, ctxt>, construct python long return
544 // value
545 intptr_t* result = (intptr_t*)GILCallR(method, self, ctxt);
546 if (!result) {
547 Py_INCREF(gNullPtrObject);
548 return gNullPtrObject;
549 }
551}
552
553//----------------------------------------------------------------------------
554#define CPPJIT_IMPL_ARRAY_EXEC(name, type, suffix) \
555 PyObject* cpyrt::name##ArrayExecutor::Execute(interop::TCppMethod_t method, \
556 interop::TCppObject_t self, \
557 CallContext* ctxt) { \
558 return CreateLowLevelView##suffix((type*)GILCallR(method, self, ctxt), \
559 fShape); \
560 }
561
562// clang-format off
563CPPJIT_IMPL_ARRAY_EXEC(Bool, bool, )
564CPPJIT_IMPL_ARRAY_EXEC(SChar, signed char, )
565CPPJIT_IMPL_ARRAY_EXEC(UChar, unsigned char, )
566CPPJIT_IMPL_ARRAY_EXEC(Byte, std::byte, )
567CPPJIT_IMPL_ARRAY_EXEC(Int8, int8_t, _i8)
568CPPJIT_IMPL_ARRAY_EXEC(UInt8, uint8_t, _i8)
569CPPJIT_IMPL_ARRAY_EXEC(Short, short, )
570CPPJIT_IMPL_ARRAY_EXEC(UShort, unsigned short, )
571CPPJIT_IMPL_ARRAY_EXEC(Int, int, )
572CPPJIT_IMPL_ARRAY_EXEC(UInt, unsigned int, )
573CPPJIT_IMPL_ARRAY_EXEC(Long, long, )
574CPPJIT_IMPL_ARRAY_EXEC(ULong, unsigned long, )
575CPPJIT_IMPL_ARRAY_EXEC(LLong, long long, )
576CPPJIT_IMPL_ARRAY_EXEC(ULLong, unsigned long long, )
577CPPJIT_IMPL_ARRAY_EXEC(Float, float, )
578CPPJIT_IMPL_ARRAY_EXEC(Double, double, )
579CPPJIT_IMPL_ARRAY_EXEC(LDouble, long double, )
580CPPJIT_IMPL_ARRAY_EXEC(ComplexF, std::complex<float>, )
581CPPJIT_IMPL_ARRAY_EXEC(ComplexD, std::complex<double>, )
582CPPJIT_IMPL_ARRAY_EXEC(ComplexI, std::complex<int>, )
583CPPJIT_IMPL_ARRAY_EXEC(ComplexL, std::complex<long>, )
584// clang-format on
585
586//- special cases ------------------------------------------------------------
587#define CPPJIT_COMPLEX_EXEC(code, type) \
588 PyObject* cpyrt::Complex##code##Executor::Execute( \
589 interop::TCppMethod_t method, interop::TCppObject_t self, \
590 CallContext* ctxt) { \
591 static interop::TCppScope_t scopeid = \
592 interop::GetScope("std::complex<" #type ">"); \
593 std::complex<type>* result = \
594 (std::complex<type>*)GILCallO(method, self, ctxt, scopeid).data; \
595 if (!result) { \
596 PyErr_SetString(PyExc_ValueError, \
597 "NULL result where temporary expected"); \
598 return nullptr; \
599 } \
600 \
601 PyObject* pyres = PyComplex_FromDoubles(result->real(), result->imag()); \
602 ::operator delete(result); /* interop::CallO calls ::operator new */ \
603 return pyres; \
604 }
605
606CPPJIT_COMPLEX_EXEC(D, double)
607
608//----------------------------------------------------------------------------
609PyObject* cpyrt::STLStringExecutor::Execute(interop::TCppMethod_t method,
611 CallContext* ctxt) {
612 // execute <method> with argument <self, ctxt>, construct python string return
613 // value
614
615 // TODO: make use of GILLCallS (?!)
616 static interop::TCppScope_t sSTLStringScope =
617 interop::GetFullScope("std::string");
618 std::string* result =
619 (std::string*)GILCallO(method, self, ctxt, sSTLStringScope).data;
620 if (!result) {
621 Py_INCREF(PyStrings::gEmptyString);
623 }
624 if (PyErr_Occurred()) {
625 delete result;
626 return nullptr;
627 }
628
629 PyObject* pyresult =
630 PyUnicode_FromStringAndSize(result->c_str(), result->size());
631 delete result; // interop::CallO allocates and constructs a string, so it must
632 // be properly destroyed
633
634 return pyresult;
635}
636
637//----------------------------------------------------------------------------
638PyObject* cpyrt::STLWStringExecutor::Execute(interop::TCppMethod_t method,
640 CallContext* ctxt) {
641 // execute <method> with argument <self, ctxt>, construct python string return
642 // value
643 static interop::TCppScope_t sSTLWStringScope =
644 interop::GetFullScope("std::wstring");
645 std::wstring* result =
646 (std::wstring*)GILCallO(method, self, ctxt, sSTLWStringScope).data;
647 if (!result) {
648 wchar_t w = L'\0';
649 return PyUnicode_FromWideChar(&w, 0);
650 }
651
652 PyObject* pyresult = PyUnicode_FromWideChar(result->c_str(), result->size());
653 delete result; // interop::CallO allocates and constructs a string, so it must
654 // be properly destroyed
655
656 return pyresult;
657}
658
659//----------------------------------------------------------------------------
660PyObject* cpyrt::InstancePtrExecutor::Execute(interop::TCppMethod_t method,
662 CallContext* ctxt) {
663 // execute <method> with argument <self, ctxt>, construct python proxy object
664 // return value
665 return BindCppObject((void*)GILCallR(method, self, ctxt), fClass);
666}
667
668//----------------------------------------------------------------------------
669cpyrt::InstanceExecutor::InstanceExecutor(interop::TCppScope_t klass)
670 : fClass(klass), fFlags(CPPInstance::kIsValue | CPPInstance::kIsOwner) {
671 /* empty */
672}
673
674//----------------------------------------------------------------------------
675PyObject* cpyrt::InstanceExecutor::Execute(interop::TCppMethod_t method,
677 CallContext* ctxt) {
678 // execution will bring a temporary in existence
679 interop::TCppObject_t value = GILCallO(method, self, ctxt, fClass);
680
681 if (!value) {
682 if (!PyErr_Occurred()) // callee may have set a python error itself
683 PyErr_Format(PyExc_ValueError,
684 "cannot allocate the temporary returned by '%s': the size "
685 "of its return type is unknown",
686 interop::GetMethodSignature(method, true).c_str());
687 return nullptr;
688 }
689
690 // the result can then be bound
692 if (!pyobj)
693 return nullptr;
694
695 // python ref counting will now control this object's life span; it will be
696 // deleted b/c it is marked as a by-value object owned by python (from fFlags)
697 return pyobj;
698}
699
700//----------------------------------------------------------------------------
701cpyrt::IteratorExecutor::IteratorExecutor(interop::TCppScope_t klass)
702 : InstanceExecutor(klass) {
704 CPPInstance::kNoWrapConv; // adds to flags from base class
705}
706
707//----------------------------------------------------------------------------
708PyObject* cpyrt::InstanceRefExecutor::Execute(interop::TCppMethod_t method,
710 CallContext* ctxt) {
711 // executor binds the result to the left-hand side, overwriting if an old
712 // object
713 PyObject* result = BindCppObject((void*)GILCallR(method, self, ctxt), fClass);
714 if (!result || !fAssignable)
715 return result;
716 else {
717 // this generic code is quite slow compared to its C++ equivalent ...
718 PyObject* assign = PyObject_GetAttr(result, PyStrings::gAssign);
719 if (!assign) {
720 PyErr_Clear();
721 PyObject* descr = PyObject_Str(result);
722 if (descr && PyUnicode_CheckExact(descr)) {
723 PyErr_Format(PyExc_TypeError, "cannot assign to return object (%s)",
724 PyUnicode_AsUTF8(descr));
725 } else {
726 PyErr_SetString(PyExc_TypeError, "cannot assign to result");
727 }
728 Py_XDECREF(descr);
729 Py_DECREF(result);
730 Py_DECREF(fAssignable);
731 fAssignable = nullptr;
732 return nullptr;
733 }
734
735 PyObject* res2 =
736 PyObject_CallFunction(assign, const_cast<char*>("O"), fAssignable);
737
738 Py_DECREF(assign);
739 Py_DECREF(result);
740 Py_DECREF(fAssignable);
741 fAssignable = nullptr;
742
743 if (res2) {
744 Py_DECREF(res2); // typically, *this from operator=()
746 }
747
748 return nullptr;
749 }
750}
751
752//----------------------------------------------------------------------------
754 PyObject* pystr = PyObject_Str(pyobj);
755 if (pystr) {
756 PyErr_Format(PyExc_TypeError, "C++ object expected, got %s",
757 PyUnicode_AsUTF8(pystr));
758 Py_DECREF(pystr);
759 } else
760 PyErr_SetString(PyExc_TypeError, "C++ object expected");
761 return nullptr;
762}
763
764PyObject* cpyrt::InstancePtrPtrExecutor::Execute(interop::TCppMethod_t method,
766 CallContext* ctxt) {
767 // execute <method> with argument <self, ctxt>, construct python C++ proxy
768 // object return ptr value
769 if (fAssignable && !CPPInstance_Check(fAssignable))
770 return SetInstanceCheckError(fAssignable);
771
772 void** result = (void**)GILCallR(method, self, ctxt);
773 if (!fAssignable)
774 return BindCppObject((void*)result, fClass,
776
777 CPPInstance* cppinst = (CPPInstance*)fAssignable;
778 *result = cppinst->GetObject();
779
780 Py_DECREF(fAssignable);
781 fAssignable = nullptr;
782
784}
785
786//----------------------------------------------------------------------------
787PyObject* cpyrt::InstancePtrRefExecutor::Execute(interop::TCppMethod_t method,
789 CallContext* ctxt) {
790 // execute <method> with argument <self, ctxt>, construct python C++ proxy
791 // object ignoring ref) return ptr value
792 if (fAssignable && !CPPInstance_Check(fAssignable))
793 return SetInstanceCheckError(fAssignable);
794
795 void** result = (void**)GILCallR(method, self, ctxt);
796 if (!fAssignable)
797 return BindCppObject(*result, fClass);
798
799 CPPInstance* cppinst = (CPPInstance*)fAssignable;
800 *result = cppinst->GetObject();
801
802 Py_DECREF(fAssignable);
803 fAssignable = nullptr;
804
806}
807
808//----------------------------------------------------------------------------
809PyObject* cpyrt::InstanceArrayExecutor::Execute(interop::TCppMethod_t method,
811 CallContext* ctxt) {
812 // execute <method> with argument <self, ctxt>, construct TupleOfInstances
813 // from return value
814 return BindCppObjectArray((void*)GILCallR(method, self, ctxt), fClass,
815 {fSize});
816}
817
818//----------------------------------------------------------------------------
819PyObject* cpyrt::ConstructorExecutor::Execute(interop::TCppMethod_t method,
821 CallContext* ctxt) {
822 // package return address in PyObject* for caller to handle appropriately (see
823 // CPPConstructor for the actual build of the PyObject)
824 return (PyObject*)GILCallConstructor(method, interop::TCppScope_t(klass.data),
825 ctxt)
826 .data;
827}
828
829//----------------------------------------------------------------------------
830PyObject* cpyrt::PyObjectExecutor::Execute(interop::TCppMethod_t method,
832 CallContext* ctxt) {
833 // execute <method> with argument <self, ctxt>, return python object
834 return (PyObject*)GILCallR(method, self, ctxt);
835}
836
837//----------------------------------------------------------------------------
838PyObject* cpyrt::FunctionPointerExecutor::Execute(interop::TCppMethod_t method,
840 CallContext* ctxt) {
841 // execute <method> with argument <self, ctxt>, return std::function from func
842 // ptr
843
844 // A function pointer in clang is represented by a Type, not a FunctionDecl
845 // and it's not possible to get the latter from the former: the backend will
846 // need to support both. Since that is far in the future, we'll use a
847 // std::function instead.
848 void* address = (void*)GILCallR(method, self, ctxt);
849 if (address)
851
852 // There's currently now way to return a typed function nullptr, but given
853 // that this is a return value (thus can not be set) and overloading function
854 // based on function pointer return type is likely uncommon ...
855 Py_INCREF(gNullPtrObject);
856 return gNullPtrObject;
857}
858
859//- factories ----------------------------------------------------------------
860cpyrt::Executor* cpyrt::CreateExecutor(const std::string& fullType,
861 cdims_t dims) {
862 // The matching of the fulltype to an executor factory goes through up to 4
863 // levels:
864 // 1) full, qualified match
865 // 2) drop '&' as by ref/full type is often pretty much the same python-wise
866 // 3) C++ classes, either by ref/ptr or by value
867 // 4) additional special case for enums
868 //
869 // If all fails, void is used, which will cause the return type to be ignored
870 // on use
871
872 if (fullType.empty())
873 return nullptr;
874
875 // an exactly matching executor is best
876 ExecFactories_t::iterator h = gExecFactories.find(fullType);
877 if (h != gExecFactories.end())
878 return (h->second)(dims);
879
880 // resolve typedefs etc. (the type itself is kept for function pointers)
882 interop::GetType(fullType, /*enable_slow_lookup=*/true);
883 const std::string resolvedType =
885
886 // a full, qualified matching executor is preferred
887 if (resolvedType != fullType) {
888 h = gExecFactories.find(resolvedType);
889 if (h != gExecFactories.end())
890 return (h->second)(dims);
891 }
892
893 //-- nothing? ok, collect information about the type and possible
894 // qualifiers/decorators
895 bool isConst = strncmp(resolvedType.c_str(), "const", 5) == 0;
896 const std::string& cpd = TypeManip::compound(resolvedType);
897 std::string realType = TypeManip::clean_type(resolvedType, false);
898
899 // accept unqualified type (as python does not know about qualifiers)
900 h = gExecFactories.find(realType + cpd);
901 if (h != gExecFactories.end())
902 return (h->second)(dims);
903
904 // drop const, as that is mostly meaningless to python (with the exception
905 // of c-strings, but those are specialized in the converter map)
906 if (isConst) {
907 realType = TypeManip::remove_const(realType);
908 h = gExecFactories.find(realType + cpd);
909 if (h != gExecFactories.end())
910 return (h->second)(dims);
911 }
912
913 // simple array types
914 if (!cpd.empty() && (std::string::size_type)std::count(cpd.begin(), cpd.end(),
915 '*') == cpd.size()) {
916 h = gExecFactories.find(realType + " ptr");
917 if (h != gExecFactories.end())
918 return (h->second)((!dims || dims.ndim() < (dim_t)cpd.size())
919 ? dims_t(cpd.size())
920 : dims);
921 }
922
923 //-- still nothing? try pointer instead of array (for builtins)
924 if (cpd == "[]") {
925 h = gExecFactories.find(realType + "*");
926 if (h != gExecFactories.end())
927 return (h->second)(dims);
928 }
929
930 // C++ classes and special cases
931 Executor* result = 0;
932 if (interop::TCppScope_t klass = interop::GetFullScope(realType)) {
933 if (Utility::IsSTLIterator(realType) ||
934 gIteratorTypes.find(fullType) != gIteratorTypes.end()) {
935 if (cpd == "")
936 return new IteratorExecutor(klass);
937 }
938
939 if (cpd == "")
940 result = new InstanceExecutor(klass);
941 else if (cpd == "&")
942 result = new InstanceRefExecutor(klass);
943 else if (cpd == "**" || cpd == "*[]" || cpd == "&*")
944 result = new InstancePtrPtrExecutor(klass);
945 else if (cpd == "*&")
946 result = new InstancePtrRefExecutor(klass);
947 else if (cpd == "[]") {
948 Py_ssize_t asize = TypeManip::array_size(resolvedType);
949 if (0 < asize)
950 result = new InstanceArrayExecutor(klass, asize);
951 else
952 result = new InstancePtrRefExecutor(klass);
953 } else
954 result = new InstancePtrExecutor(klass);
955 } else if (std::string fnRet, fnSig;
956 interop::GetFnTypeSigAsString(type, fnRet, fnSig)) {
957 // this is a function pointer
958 result = new FunctionPointerExecutor(fnRet, fnSig);
959 } else {
960 // unknown: void* may work ("user knows best"), void will fail on use of
961 // return value
962 h = (cpd == "") ? gExecFactories.find("void")
963 : gExecFactories.find("void ptr");
964 }
965
966 if (!result && h != gExecFactories.end())
967 // executor factory available, use it to create executor
968 result = (h->second)(dims);
969
970 return result; // may still be null
971}
972
974 // The matching of the fulltype to an executor factory goes through up to 4
975 // levels:
976 // 1) full, qualified match
977 // 2) drop '&' as by ref/full type is often pretty much the same python-wise
978 // 3) C++ classes, either by ref/ptr or by value
979 // 4) additional special case for enums
980 //
981 // If all fails, void is used, which will cause the return type to be ignored
982 // on use
983
984 // an exactly matching executor is best
985 std::string fullType = interop::GetTypeAsString(type);
986 if (fullType.size() >= 2 &&
987 fullType.compare(fullType.size() - 2, 2, " &") == 0)
988 fullType = fullType.substr(0, fullType.size() - 2) + "&";
989
990 ExecFactories_t::iterator h = gExecFactories.find(fullType);
991 if (h != gExecFactories.end())
992 return (h->second)(dims);
993
994 // resolve typedefs etc.
996 {
997 // if resolvedType is a reference to enum
998 // then it should be reduced to reference
999 // to the underlying interger
1000 resolvedType = interop::ResolveEnumReferenceType(resolvedType);
1001 // similarly for pointers
1002 resolvedType = interop::ResolveEnumPointerType(resolvedType);
1003 }
1004 // FIXME: avoid string comparisons and parsing
1005 std::string resolvedTypeStr = interop::GetTypeAsString(resolvedType);
1006 if (interop::IsFunctionPointerType(resolvedType)) {
1007 resolvedTypeStr.erase(
1008 std::remove(resolvedTypeStr.begin(), resolvedTypeStr.end(), ' '),
1009 resolvedTypeStr.end());
1010 if (resolvedTypeStr.rfind("(void)") != std::string::npos)
1011 resolvedTypeStr =
1012 resolvedTypeStr.substr(0, resolvedTypeStr.size() - 6) + "()";
1013 }
1014
1015 // a full, qualified matching executor is preferred
1016 if (resolvedTypeStr != fullType) {
1017 h = gExecFactories.find(resolvedTypeStr);
1018 if (h != gExecFactories.end())
1019 return (h->second)(dims);
1020 }
1021
1022 //-- nothing? ok, collect information about the type and possible
1023 // qualifiers/decorators
1024 bool isConst = strncmp(resolvedTypeStr.c_str(), "const", 5) == 0;
1025 const std::string& cpd = TypeManip::compound(resolvedTypeStr);
1027 ? resolvedType
1028 : interop::GetRealType(resolvedType);
1029 std::string realTypeStr = interop::IsFunctionPointerType(resolvedType)
1030 ? resolvedTypeStr
1031 : interop::GetTypeAsString(realType);
1032 const std::string compounded = cpd.empty() ? realTypeStr : realTypeStr + cpd;
1033
1034 // accept unqualified type (as python does not know about qualifiers)
1035 h = gExecFactories.find(compounded);
1036 if (h != gExecFactories.end())
1037 return (h->second)(dims);
1038
1039 // drop const, as that is mostly meaningless to python (with the exception
1040 // of c-strings, but those are specialized in the converter map)
1041 if (isConst) {
1042 realTypeStr = TypeManip::remove_const(realTypeStr);
1043 h = gExecFactories.find(compounded);
1044 if (h != gExecFactories.end())
1045 return (h->second)(dims);
1046 }
1047
1048 // simple array types
1049 if (!cpd.empty() && (std::string::size_type)std::count(cpd.begin(), cpd.end(),
1050 '*') == cpd.size()) {
1051 h = gExecFactories.find(realTypeStr + " ptr");
1052 if (h != gExecFactories.end())
1053 return (h->second)((!dims || dims.ndim() < (dim_t)cpd.size())
1054 ? dims_t(cpd.size())
1055 : dims);
1056 }
1057
1058 //-- still nothing? try pointer instead of array (for builtins)
1059 if (cpd == "[]") {
1060 h = gExecFactories.find(realTypeStr + "*");
1061 if (h != gExecFactories.end())
1062 return (h->second)(dims);
1063 }
1064
1065 // C++ classes and special cases
1066 Executor* result = 0;
1067 if (interop::IsClassType(realType)) {
1069 if (resolvedTypeStr.find("iterator") != std::string::npos ||
1070 gIteratorTypes.find(fullType) != gIteratorTypes.end()) {
1071 if (cpd == "")
1072 return new IteratorExecutor(klass);
1073 }
1074
1075 if (cpd == "")
1076 result = new InstanceExecutor(klass);
1077 else if (cpd == "&")
1078 result = new InstanceRefExecutor(klass);
1079 else if (cpd == "**" || cpd == "*[]" || cpd == "&*")
1080 result = new InstancePtrPtrExecutor(klass);
1081 else if (cpd == "*&")
1082 result = new InstancePtrRefExecutor(klass);
1083 else if (cpd == "[]") {
1084 Py_ssize_t asize = TypeManip::array_size(resolvedTypeStr);
1085 if (0 < asize)
1086 result = new InstanceArrayExecutor(klass, asize);
1087 else
1088 result = new InstancePtrRefExecutor(klass);
1089 } else
1090 result = new InstancePtrExecutor(klass);
1091 } else if (std::string fnRet, fnSig;
1092 interop::GetFnTypeSigAsString(resolvedType, fnRet, fnSig)) {
1093 // this is a function pointer (checked on the resolved type: the real type
1094 // of a member function pointer is a bare function type, which must not
1095 // pass here, as it would return 16 bytes through an 8-byte return slot)
1096 result = new FunctionPointerExecutor(fnRet, fnSig);
1097 } else {
1098 // unknown: void* may work ("user knows best"), void will fail on use of
1099 // return value
1100 h = (cpd == "") ? gExecFactories.find("void")
1101 : gExecFactories.find("void ptr");
1102 }
1103
1104 if (!result && h != gExecFactories.end())
1105 // executor factory available, use it to create executor
1106 result = (h->second)(dims);
1107
1108 return result; // may still be null
1109}
1110
1111//----------------------------------------------------------------------------
1114 if (p && p->HasState())
1115 delete p; // state-less executors are always shared
1116}
1117
1118//----------------------------------------------------------------------------
1120bool cpyrt::RegisterExecutor(const std::string& name, ef_t fac) {
1121 // register a custom executor
1122 auto f = gExecFactories.find(name);
1123 if (f != gExecFactories.end())
1124 return false;
1125
1126 gExecFactories[name] = fac;
1127 return true;
1128}
1129
1130//----------------------------------------------------------------------------
1132bool cpyrt::UnregisterExecutor(const std::string& name) {
1133 // remove a custom executor
1134 auto f = gExecFactories.find(name);
1135 if (f != gExecFactories.end()) {
1136 gExecFactories.erase(f);
1137 return true;
1138 }
1139 return false;
1140}
1141
1142//----------------------------------------------------------------------------
1145 CallContext* ctxt) {
1146 return GILCallR(meth, obj, ctxt);
1147}
1148
1149//----------------------------------------------------------------------------
1150namespace {
1151
1152using namespace cppjit::cpyrt;
1153
1154#define WSTRING1 "std::basic_string<wchar_t>"
1155#define WSTRING2 \
1156 "std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_" \
1157 "t>>"
1158
1159//-- aliasing special case: C complex (is binary compatible with C++
1160// std::complex)
1161#ifndef _WIN32
1162#define CCOMPLEX_D "_Complex double"
1163#define CCOMPLEX_F "_Complex float"
1164#else
1165#define CCOMPLEX_D "_C_double_complex"
1166#define CCOMPLEX_F "_C_float_complex"
1167#endif
1168
1169struct InitExecFactories_t {
1170public:
1171 InitExecFactories_t() {
1172 // load all executor factories in the global map 'gExecFactories'
1174
1175 // factories for built-ins
1176 gf["bool"] = (ef_t) + [](cdims_t) {
1177 static BoolExecutor e{};
1178 return &e;
1179 };
1180 gf["bool&"] = (ef_t) + [](cdims_t) { return new BoolRefExecutor{}; };
1181 gf["const bool&"] = (ef_t) + [](cdims_t) {
1182 static BoolConstRefExecutor e{};
1183 return &e;
1184 };
1185 gf["char"] = (ef_t) + [](cdims_t) {
1186 static CharExecutor e{};
1187 return &e;
1188 };
1189 gf["signed char"] = gf["char"];
1190 gf["unsigned char"] = (ef_t) + [](cdims_t) {
1191 static UCharExecutor e{};
1192 return &e;
1193 };
1194 gf["char&"] = (ef_t) + [](cdims_t) { return new CharRefExecutor{}; };
1195 gf["signed char&"] = gf["char&"];
1196 gf["unsigned char&"] =
1197 (ef_t) + [](cdims_t) { return new UCharRefExecutor{}; };
1198 gf["const char&"] = (ef_t) + [](cdims_t) {
1199 static CharConstRefExecutor e{};
1200 return &e;
1201 };
1202 gf["const signed char&"] = gf["const char&"];
1203 gf["const unsigned char&"] = (ef_t) + [](cdims_t) {
1204 static UCharConstRefExecutor e{};
1205 return &e;
1206 };
1207 gf["wchar_t"] = (ef_t) + [](cdims_t) {
1208 static WCharExecutor e{};
1209 return &e;
1210 };
1211 gf["char16_t"] = (ef_t) + [](cdims_t) {
1212 static Char16Executor e{};
1213 return &e;
1214 };
1215 gf["char32_t"] = (ef_t) + [](cdims_t) {
1216 static Char32Executor e{};
1217 return &e;
1218 };
1219 gf["int8_t"] = (ef_t) + [](cdims_t) {
1220 static Int8Executor e{};
1221 return &e;
1222 };
1223 gf["int8_t&"] = (ef_t) + [](cdims_t) { return new Int8RefExecutor{}; };
1224 gf["const int8_t&"] = (ef_t) + [](cdims_t) {
1225 static Int8ConstRefExecutor e{};
1226 return &e;
1227 };
1228 gf["uint8_t"] = (ef_t) + [](cdims_t) {
1229 static UInt8Executor e{};
1230 return &e;
1231 };
1232 gf["uint8_t&"] = (ef_t) + [](cdims_t) { return new UInt8RefExecutor{}; };
1233 gf["const uint8_t&"] = (ef_t) + [](cdims_t) {
1234 static UInt8ConstRefExecutor e{};
1235 return &e;
1236 };
1237 gf["short"] = (ef_t) + [](cdims_t) {
1238 static ShortExecutor e{};
1239 return &e;
1240 };
1241 gf["short&"] = (ef_t) + [](cdims_t) { return new ShortRefExecutor{}; };
1242 gf["int"] = (ef_t) + [](cdims_t) {
1243 static IntExecutor e{};
1244 return &e;
1245 };
1246 gf["int&"] = (ef_t) + [](cdims_t) { return new IntRefExecutor{}; };
1247 gf["unsigned short"] = gf["int"];
1248 gf["unsigned short&"] =
1249 (ef_t) + [](cdims_t) { return new UShortRefExecutor{}; };
1250 gf["unsigned long"] = (ef_t) + [](cdims_t) {
1251 static ULongExecutor e{};
1252 return &e;
1253 };
1254 gf["unsigned long&"] =
1255 (ef_t) + [](cdims_t) { return new ULongRefExecutor{}; };
1256 gf["unsigned int"] = gf["unsigned long"];
1257 gf["unsigned int&"] =
1258 (ef_t) + [](cdims_t) { return new UIntRefExecutor{}; };
1259 gf["long"] = (ef_t) + [](cdims_t) {
1260 static LongExecutor e{};
1261 return &e;
1262 };
1263 gf["long&"] = (ef_t) + [](cdims_t) { return new LongRefExecutor{}; };
1264 gf["unsigned long"] = (ef_t) + [](cdims_t) {
1265 static ULongExecutor e{};
1266 return &e;
1267 };
1268 gf["unsigned long&"] =
1269 (ef_t) + [](cdims_t) { return new ULongRefExecutor{}; };
1270 gf["long long"] = (ef_t) + [](cdims_t) {
1271 static LongLongExecutor e{};
1272 return &e;
1273 };
1274 gf["long long&"] =
1275 (ef_t) + [](cdims_t) { return new LongLongRefExecutor{}; };
1276 gf["unsigned long long"] = (ef_t) + [](cdims_t) {
1277 static ULongLongExecutor e{};
1278 return &e;
1279 };
1280 gf["unsigned long long&"] =
1281 (ef_t) + [](cdims_t) { return new ULongLongRefExecutor{}; };
1282
1283 gf["float"] = (ef_t) + [](cdims_t) {
1284 static FloatExecutor e{};
1285 return &e;
1286 };
1287 gf["float&"] = (ef_t) + [](cdims_t) { return new FloatRefExecutor{}; };
1288 gf["double"] = (ef_t) + [](cdims_t) {
1289 static DoubleExecutor e{};
1290 return &e;
1291 };
1292 gf["double&"] = (ef_t) + [](cdims_t) { return new DoubleRefExecutor{}; };
1293 gf["long double"] = (ef_t) + [](cdims_t) {
1294 static LongDoubleExecutor e{};
1295 return &e;
1296 }; // TODO: lost precision
1297 gf["long double&"] =
1298 (ef_t) + [](cdims_t) { return new LongDoubleRefExecutor{}; };
1299 gf["std::complex<double>"] = (ef_t) + [](cdims_t) {
1300 static ComplexDExecutor e{};
1301 return &e;
1302 };
1303 gf["std::complex<double>&"] =
1304 (ef_t) + [](cdims_t) { return new ComplexDRefExecutor{}; };
1305 gf["void"] = (ef_t) + [](cdims_t) {
1306 static VoidExecutor e{};
1307 return &e;
1308 };
1309
1310 // pointer/array factories
1311 gf["void ptr"] =
1312 (ef_t) + [](cdims_t d) { return new VoidArrayExecutor{d}; };
1313 gf["bool ptr"] =
1314 (ef_t) + [](cdims_t d) { return new BoolArrayExecutor{d}; };
1315 gf["unsigned char ptr"] =
1316 (ef_t) + [](cdims_t d) { return new UCharArrayExecutor{d}; };
1317 gf["const unsigned char ptr"] = gf["unsigned char ptr"];
1318 gf["std::byte ptr"] =
1319 (ef_t) + [](cdims_t d) { return new ByteArrayExecutor{d}; };
1320 gf["const std::byte ptr"] = gf["std::byte ptr"];
1321 gf["int8_t ptr"] =
1322 (ef_t) + [](cdims_t d) { return new Int8ArrayExecutor{d}; };
1323 gf["uint8_t ptr"] =
1324 (ef_t) + [](cdims_t d) { return new UInt8ArrayExecutor{d}; };
1325 gf["short ptr"] =
1326 (ef_t) + [](cdims_t d) { return new ShortArrayExecutor{d}; };
1327 gf["unsigned short ptr"] =
1328 (ef_t) + [](cdims_t d) { return new UShortArrayExecutor{d}; };
1329 gf["int ptr"] = (ef_t) + [](cdims_t d) { return new IntArrayExecutor{d}; };
1330 gf["unsigned int ptr"] =
1331 (ef_t) + [](cdims_t d) { return new UIntArrayExecutor{d}; };
1332 gf["long ptr"] =
1333 (ef_t) + [](cdims_t d) { return new LongArrayExecutor{d}; };
1334 gf["unsigned long ptr"] =
1335 (ef_t) + [](cdims_t d) { return new ULongArrayExecutor{d}; };
1336 gf["long long ptr"] =
1337 (ef_t) + [](cdims_t d) { return new LLongArrayExecutor{d}; };
1338 gf["unsigned long long ptr"] =
1339 (ef_t) + [](cdims_t d) { return new ULLongArrayExecutor{d}; };
1340 gf["float ptr"] =
1341 (ef_t) + [](cdims_t d) { return new FloatArrayExecutor{d}; };
1342 gf["double ptr"] =
1343 (ef_t) + [](cdims_t d) { return new DoubleArrayExecutor{d}; };
1344 gf["long double ptr"] =
1345 (ef_t) + [](cdims_t d) { return new LDoubleArrayExecutor{d}; };
1346 gf["std::complex<float> ptr"] =
1347 (ef_t) + [](cdims_t d) { return new ComplexFArrayExecutor{d}; };
1348 gf["std::complex<double> ptr"] =
1349 (ef_t) + [](cdims_t d) { return new ComplexDArrayExecutor{d}; };
1350 gf["std::complex<int> ptr"] =
1351 (ef_t) + [](cdims_t d) { return new ComplexIArrayExecutor{d}; };
1352 gf["std::complex<long> ptr"] =
1353 (ef_t) + [](cdims_t d) { return new ComplexLArrayExecutor{d}; };
1354
1355 // aliases
1356 gf["internal_enum_type_t"] = gf["int"];
1357 gf["internal_enum_type_t&"] = gf["int&"];
1358 gf["internal_enum_type_t ptr"] = gf["int ptr"];
1359 gf["std::byte"] = gf["uint8_t"];
1360 gf["std::byte&"] = gf["uint8_t&"];
1361 gf["const std::byte&"] = gf["const uint8_t&"];
1362 gf["const byte&"] = gf["const uint8_t&"];
1363 gf["std::int8_t"] = gf["int8_t"];
1364 gf["std::int8_t&"] = gf["int8_t&"];
1365 gf["const std::int8_t&"] = gf["const int8_t&"];
1366 gf["std::int8_t ptr"] = gf["int8_t ptr"];
1367 gf["std::uint8_t"] = gf["uint8_t"];
1368 gf["std::uint8_t&"] = gf["uint8_t&"];
1369 gf["const std::uint8_t&"] = gf["const uint8_t&"];
1370 gf["std::uint8_t ptr"] = gf["uint8_t ptr"];
1371#ifdef _WIN32
1372 gf["__int64"] = gf["long long"];
1373 gf["__int64&"] = gf["long long&"];
1374 gf["__int64 ptr"] = gf["long long ptr"];
1375 gf["unsigned __int64"] = gf["unsigned long long"];
1376 gf["unsigned __int64&"] = gf["unsigned long long&"];
1377 gf["unsigned __int64 ptr"] = gf["unsigned long long ptr"];
1378#endif
1379 gf[CCOMPLEX_D] = gf["std::complex<double>"];
1380 gf[CCOMPLEX_D "&"] = gf["std::complex<double>&"];
1381 gf[CCOMPLEX_F " ptr"] = gf["std::complex<float> ptr"];
1382 gf[CCOMPLEX_D " ptr"] = gf["std::complex<double> ptr"];
1383
1384 // We always need these executors when cppjit is based on an unpatched
1385 // ROOT, because the "long long" types are always converted to Long64_t
1386 // and ULong64_t already at the ROOT Meta level.
1387 // See
1388 // https://github.com/root-project/root/issues/15872#issuecomment-2174092763
1389 gf["Long64_t"] = gf["long long"];
1390 gf["Long64_t&"] = gf["long long&"];
1391 gf["Long64_t ptr"] = gf["long long ptr"];
1392 gf["ULong64_t"] = gf["unsigned long long"];
1393 gf["ULong64_t&"] = gf["unsigned long long&"];
1394 gf["ULong64_t ptr"] = gf["unsigned long long ptr"];
1395
1396 // factories for special cases
1397 gf["const char*"] = (ef_t) + [](cdims_t) {
1398 static CStringExecutor e{};
1399 return &e;
1400 };
1401 gf["char*"] = gf["const char*"];
1402 gf["const char*&"] = (ef_t) + [](cdims_t) {
1403 static CStringRefExecutor e{};
1404 return &e;
1405 };
1406 gf["char*&"] = gf["const char*&"];
1407 gf["const signed char*"] = gf["const char*"];
1408 // gf["signed char*"] = gf["char*"];
1409 gf["signed char ptr"] =
1410 (ef_t) + [](cdims_t d) { return new SCharArrayExecutor{d}; };
1411 gf["wchar_t*"] = (ef_t) + [](cdims_t) {
1412 static WCStringExecutor e{};
1413 return &e;
1414 };
1415 gf["char16_t*"] = (ef_t) + [](cdims_t) {
1416 static CString16Executor e{};
1417 return &e;
1418 };
1419 gf["char32_t*"] = (ef_t) + [](cdims_t) {
1420 static CString32Executor e{};
1421 return &e;
1422 };
1423 gf["std::basic_string<char>"] = (ef_t) + [](cdims_t) {
1424 static STLStringExecutor e{};
1425 return &e;
1426 };
1427 gf["std::basic_string<char>&"] =
1428 (ef_t) + [](cdims_t) { return new STLStringRefExecutor{}; };
1429 gf["std::basic_string<wchar_t>"] = (ef_t) + [](cdims_t) {
1430 static STLWStringExecutor e{};
1431 return &e;
1432 };
1433 gf[WSTRING1] = gf["std::basic_string<wchar_t>"];
1434 gf[WSTRING2] = gf["std::basic_string<wchar_t>"];
1435 gf["__init__"] = (ef_t) + [](cdims_t) {
1436 static ConstructorExecutor e{};
1437 return &e;
1438 };
1439 gf["PyObject*"] = (ef_t) + [](cdims_t) {
1440 static PyObjectExecutor e{};
1441 return &e;
1442 };
1443 gf["_object*"] = gf["PyObject*"];
1444 gf["FILE*"] = gf["void ptr"];
1445 }
1446} initExecvFactories_;
1447
1448} // unnamed namespace
#define CCOMPLEX_D
#define CCOMPLEX_F
#define WSTRING1
#define WSTRING2
std::string fRetType
dims_t fShape
interop::TCppScope_t fClass
std::string fSignature
dim_t fSize
uint32_t fFlags
static interop::TCppObject_t GILCallConstructor(interop::TCppMethod_t method, interop::TCppScope_t klass, cpyrt::CallContext *ctxt)
static float GILCallF(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:79
static PyObject * PyComplex_FromComplex(const std::complex< T > &c)
static PY_LONG_LONG GILCallLL(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:78
static PyObject * SetInstanceCheckError(PyObject *pyobj)
static std::complex< T > PyComplex_AsComplex(PyObject *pycplx)
static interop::TCppObject_t GILCallO(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt, interop::TCppScope_t klass)
Definition Executors.cxx:85
static void GILCallV(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:72
static void * GILCallR(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:82
static short GILCallH(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:75
#define CPPJIT_IMPL_GILCALL(rtype, tcode)
Definition Executors.cxx:62
#define CPPJIT_COMPLEX_EXEC(code, type)
static double GILCallD(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:80
static long GILCallL(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:77
#define CPPJIT_IMPL_ARRAY_EXEC(name, type, suffix)
static PY_LONG_DOUBLE GILCallLD(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:81
static PyObject * cpyrt_PyBool_FromLong(long b)
static int GILCallI(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:76
static unsigned char GILCallB(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:73
static PyObject * cpyrt_PyText_FromULong(unsigned long uc)
#define CPPJIT_IMPL_REFEXEC(name, type, stype, F1, F2)
static char GILCallC(interop::TCppMethod_t method, interop::TCppObject_t self, cpyrt::CallContext *ctxt)
Definition Executors.cxx:74
static PyObject * cpyrt_PyText_FromLong(long cl)
_object PyObject
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
char name[80]
Definition TGX11.cxx:142
#define CPYRT_EXPORT
Definition CommonDefs.h:25
virtual bool SetAssignable(PyObject *)
long double PY_LONG_DOUBLE
unsigned long long PY_ULONG_LONG
long long PY_LONG_LONG
#define Py_RETURN_NONE
Definition cpyrt.h:69
RooArgList L(Args_t &&... args)
Definition RooArgList.h:156
std::string remove_const(const std::string &cppname)
Definition TypeManip.cxx:80
std::string clean_type(const std::string &cppname, bool template_strip=true, bool const_strip=true)
Py_ssize_t array_size(const std::string &name)
std::string compound(const std::string &name)
bool IsSTLIterator(const std::string &classname)
Definition Utility.cxx:1547
PyObject * FuncPtr2StdFunction(const std::string &retType, const std::string &signature, void *address)
Definition Utility.cxx:1112
static ExecFactories_t gExecFactories
Definition Executors.cxx:26
PyObject * gNullPtrObject
PyObject * CreatePointerView(void *ptr, cdims_t shape=0)
PyObject * BindCppObject(interop::TCppObject_t object, interop::TCppScope_t klass, const unsigned flags=0)
PyObject * BindCppObjectArray(interop::TCppObject_t address, interop::TCppScope_t klass, cdims_t dims)
Py_ssize_t dim_t
Definition API.h:100
CPYRT_EXTERN Executor * CreateExecutor(const std::string &name, cdims_t=0)
bool ReleasesGIL(CallContext *ctxt)
Dimensions dims_t
Definition API.h:113
PyObject * BindCppObjectNoCast(interop::TCppObject_t object, interop::TCppScope_t klass, const unsigned flags=0)
CPYRT_EXPORT void DestroyExecutor(Executor *p)
unsigned long PyLongOrInt_AsULong(PyObject *pyobject)
Definition Utility.cxx:125
std::set< std::string > gIteratorTypes
bool CPPInstance_Check(T *object)
CPYRT_EXTERN bool UnregisterExecutor(const std::string &name)
PY_ULONG_LONG PyLongOrInt_AsULong64(PyObject *pyobject)
Definition Utility.cxx:152
std::unordered_map< std::string, ef_t > ExecFactories_t
Definition Executors.cxx:25
Executor *(* ef_t)(cdims_t)
Definition Executors.h:40
CPYRT_EXTERN void * CallVoidP(interop::TCppMethod_t, interop::TCppObject_t, CallContext *)
CPYRT_EXTERN bool RegisterExecutor(const std::string &name, ExecutorFactory_t)
Cpp::TypeRef TCppType_t
Definition API.h:42
TCppScope_t GetFullScope(const std::string &scope_name)
Cpp::ObjectRef TCppObject_t
Definition API.h:43
TCppType_t GetTypeFromScope(TCppScope_t klass)
std::string GetTypeAsString(TCppType_t type)
TCppType_t ResolveEnumPointerType(TCppType_t type)
bool IsClassType(TCppType_t type)
bool GetFnTypeSigAsString(TCppType_t fn_type, std::string &ret_type, std::string &signature)
Spells out a function type, or a pointer or reference to one, as the return type and the parenthesize...
Cpp::FuncRef TCppMethod_t
Definition API.h:44
bool IsFunctionPointerType(TCppType_t type)
TCppObject_t CallO(TCppMethod_t method, TCppObject_t self, size_t nargs, void *args, TCppType_t result_type)
TCppType_t ResolveType(TCppType_t cppitem_name)
std::string GetMethodSignature(TCppMethod_t, bool show_formal_args, TCppIndex_t max_args=(TCppIndex_t) -1)
TCppObject_t CallConstructor(TCppMethod_t method, TCppScope_t klass, size_t nargs, void *args)
TCppScope_t GetScopeFromType(TCppType_t type)
TCppType_t GetRealType(TCppType_t type)
TCppType_t GetType(const std::string &name, bool enable_slow_lookup=false)
TCppType_t ResolveEnumReferenceType(TCppType_t type)
Cpp::DeclRef TCppScope_t
Definition API.h:41
Definition API.cxx:34
Parameter * GetArgs(size_t sz)
Definition CallContext.h:62