Debian Codegraph @

src/c/realize_c_type.c

raw
1
2 #ifdef MS_WIN32
3 #include "misc_win32.h"
4 #else
5 #include "misc_thread_posix.h"
6 #endif
7
8
9 typedef struct {
10 struct _cffi_type_context_s ctx; /* inlined substructure */
11 PyObject *types_dict;
12 PyObject *included_ffis;
13 PyObject *included_libs;
14 PyObject *_keepalive1;
15 PyObject *_keepalive2;
16 } builder_c_t;
17
18
19 static PyObject *all_primitives[_CFFI__NUM_PRIM];
20 static CTypeDescrObject *g_ct_voidp, *g_ct_chararray, *g_ct_int, *g_file_struct;
21
22 static PyObject *build_primitive_type(int num); /* forward */
23
24 #define primitive_in_range(num) ((num) >= 0 && (num) < _CFFI__NUM_PRIM)
25 #define get_primitive_type(num) \
26 ((primitive_in_range(num) && all_primitives[num] != NULL) ? \
27 all_primitives[num] : build_primitive_type(num))
28
29 static int init_global_types_dict(PyObject *ffi_type_dict)
30 {
31 int err;
32 PyObject *ct_void, *ct_char, *ct2, *pnull;
33 /* XXX some leaks in case these functions fail, but well,
34 MemoryErrors during importing an extension module are kind
35 of bad anyway */
36
37 ct_void = get_primitive_type(_CFFI_PRIM_VOID); // 'void'
38 if (ct_void == NULL)
39 return -1;
40
41 ct2 = new_pointer_type((CTypeDescrObject *)ct_void); // 'void *'
42 if (ct2 == NULL)
43 return -1;
44 g_ct_voidp = (CTypeDescrObject *)ct2;
45
46 ct_char = get_primitive_type(_CFFI_PRIM_CHAR); // 'char'
47 if (ct_char == NULL)
48 return -1;
49
50 ct2 = new_pointer_type((CTypeDescrObject *)ct_char); // 'char *'
51 if (ct2 == NULL)
52 return -1;
53
54 ct2 = new_array_type((CTypeDescrObject *)ct2, -1); // 'char[]'
55 if (ct2 == NULL)
56 return -1;
57 g_ct_chararray = (CTypeDescrObject *)ct2;
58
59 g_ct_int = (CTypeDescrObject *)get_primitive_type(_CFFI_PRIM_INT); // 'int'
60 if (g_ct_int == NULL)
61 return -1;
62
63 ct2 = new_struct_or_union_type("FILE",
64 CT_STRUCT | CT_IS_FILE); // 'FILE'
65 if (ct2 == NULL)
66 return -1;
67 g_file_struct = (CTypeDescrObject *)ct2;
68
69 pnull = new_simple_cdata(NULL, g_ct_voidp);
70 if (pnull == NULL)
71 return -1;
72 err = PyDict_SetItemString(ffi_type_dict, "NULL", pnull);
73 Py_DECREF(pnull);
74 if (err < 0)
75 return err;
76
77 #ifdef Py_GIL_DISABLED
78 // Ensure that all primitive types are initialised to avoid race conditions
79 // on the first access.
80 for (Py_ssize_t i = 0; i < _CFFI__NUM_PRIM; i++) {
81 ct2 = get_primitive_type(i);
82 if (ct2 == NULL)
83 return -1;
84 }
85 #endif
86
87 return 0;
88 }
89
90 static CTypeDescrObject *_get_ct_int(void)
91 {
92 return g_ct_int;
93 }
94
95 static void free_builder_c(builder_c_t *builder, int ctx_is_static)
96 {
97 if (!ctx_is_static) {
98 size_t i;
99 const void *mem[] = {builder->ctx.types,
100 builder->ctx.globals,
101 builder->ctx.struct_unions,
102 //builder->ctx.fields: allocated with struct_unions
103 builder->ctx.enums,
104 builder->ctx.typenames};
105 for (i = 0; i < sizeof(mem) / sizeof(*mem); i++) {
106 if (mem[i] != NULL)
107 PyMem_Free((void *)mem[i]);
108 }
109 }
110 Py_XDECREF(builder->included_ffis);
111 Py_XDECREF(builder->included_libs);
112 Py_XDECREF(builder->types_dict);
113 Py_XDECREF(builder->_keepalive1);
114 Py_XDECREF(builder->_keepalive2);
115 }
116
117 static int init_builder_c(builder_c_t *builder,
118 const struct _cffi_type_context_s *ctx)
119 {
120 PyObject *ldict = PyDict_New();
121 if (ldict == NULL)
122 return -1;
123
124 if (ctx)
125 builder->ctx = *ctx;
126 else
127 memset(&builder->ctx, 0, sizeof(builder->ctx));
128
129 builder->types_dict = ldict;
130 builder->included_ffis = NULL;
131 builder->included_libs = NULL;
132 builder->_keepalive1 = NULL;
133 builder->_keepalive2 = NULL;
134 return 0;
135 }
136
137 static PyObject *build_primitive_type(int num)
138 {
139 /* XXX too many translations between here and new_primitive_type() */
140 static const char *primitive_name[] = {
141 NULL,
142 "_Bool",
143 "char",
144 "signed char",
145 "unsigned char",
146 "short",
147 "unsigned short",
148 "int",
149 "unsigned int",
150 "long",
151 "unsigned long",
152 "long long",
153 "unsigned long long",
154 "float",
155 "double",
156 "long double",
157 "wchar_t",
158 "int8_t",
159 "uint8_t",
160 "int16_t",
161 "uint16_t",
162 "int32_t",
163 "uint32_t",
164 "int64_t",
165 "uint64_t",
166 "intptr_t",
167 "uintptr_t",
168 "ptrdiff_t",
169 "size_t",
170 "ssize_t",
171 "int_least8_t",
172 "uint_least8_t",
173 "int_least16_t",
174 "uint_least16_t",
175 "int_least32_t",
176 "uint_least32_t",
177 "int_least64_t",
178 "uint_least64_t",
179 "int_fast8_t",
180 "uint_fast8_t",
181 "int_fast16_t",
182 "uint_fast16_t",
183 "int_fast32_t",
184 "uint_fast32_t",
185 "int_fast64_t",
186 "uint_fast64_t",
187 "intmax_t",
188 "uintmax_t",
189 "_cffi_float_complex_t",
190 "_cffi_double_complex_t",
191 "char16_t",
192 "char32_t",
193 };
194 PyObject *x;
195
196 assert(sizeof(primitive_name) == sizeof(*primitive_name) * _CFFI__NUM_PRIM);
197 if (num == _CFFI_PRIM_VOID) {
198 x = new_void_type();
199 }
200 else if (primitive_in_range(num) && primitive_name[num] != NULL) {
201 x = new_primitive_type(primitive_name[num]);
202 }
203 else if (num == _CFFI__UNKNOWN_PRIM) {
204 PyErr_SetString(FFIError, "primitive integer type with an unexpected "
205 "size (or not an integer type at all)");
206 return NULL;
207 }
208 else if (num == _CFFI__UNKNOWN_FLOAT_PRIM) {
209 PyErr_SetString(FFIError, "primitive floating-point type with an "
210 "unexpected size (or not a float type at all)");
211 return NULL;
212 }
213 else if (num == _CFFI__UNKNOWN_LONG_DOUBLE) {
214 PyErr_SetString(FFIError, "primitive floating-point type is "
215 "'long double', not supported for now with "
216 "the syntax 'typedef double... xxx;'");
217 return NULL;
218 }
219 else {
220 PyErr_Format(PyExc_NotImplementedError, "prim=%d", num);
221 return NULL;
222 }
223
224 all_primitives[num] = x;
225 return x;
226 }
227
228 static PyObject *realize_global_int(builder_c_t *builder, int gindex)
229 {
230 int neg;
231 char got[64];
232 unsigned long long value;
233 struct _cffi_getconst_s gc;
234 const struct _cffi_global_s *g = &builder->ctx.globals[gindex];
235 gc.ctx = &builder->ctx;
236 gc.gindex = gindex;
237 /* note: we cast g->address to this function type; we do the same
238 in parse_c_type:parse_sequel() too. Note that the called function
239 may be declared simply with "unsigned long long *" as argument,
240 which is fine as it is the first field in _cffi_getconst_s. */
241 assert(&gc.value == (unsigned long long *)&gc);
242 neg = ((int(*)(struct _cffi_getconst_s *))g->address)(&gc);
243 value = gc.value;
244
245 switch (neg) {
246
247 case 0:
248 if (value <= (unsigned long long)LONG_MAX)
249 return PyLong_FromLong((long)value);
250 else
251 return PyLong_FromUnsignedLongLong(value);
252
253 case 1:
254 if ((long long)value >= (long long)LONG_MIN)
255 return PyLong_FromLong((long)value);
256 else
257 return PyLong_FromLongLong((long long)value);
258
259 default:
260 break;
261 }
262 if (neg == 2)
263 sprintf(got, "%llu (0x%llx)", value, value);
264 else
265 sprintf(got, "%lld", (long long)value);
266 PyErr_Format(FFIError, "the C compiler says '%.200s' is equal to %s, "
267 "but the cdef disagrees", g->name, got);
268 return NULL;
269 }
270
271 static CTypeDescrObject *
272 unwrap_fn_as_fnptr(PyObject *x)
273 {
274 assert(PyTuple_Check(x));
275 return (CTypeDescrObject *)PyTuple_GET_ITEM(x, 0);
276 }
277
278 static CTypeDescrObject *
279 unexpected_fn_type(PyObject *x)
280 {
281 CTypeDescrObject *ct = unwrap_fn_as_fnptr(x);
282 char *text1 = ct->ct_name;
283 char *text2 = text1 + ct->ct_name_position + 1;
284 size_t prefix_size = ct->ct_name_position - 2;
285 char *buf = PyMem_Malloc(prefix_size + 1);
286 if (buf == NULL) {
287 return NULL;
288 }
289 memcpy(buf, text1, prefix_size);
290 buf[prefix_size] = '\0';
291 assert(text2[-3] == '(');
292 PyErr_Format(FFIError, "the type '%s%s' is a function type, not a "
293 "pointer-to-function type", buf, text2);
294 PyMem_Free(buf);
295 return NULL;
296 }
297
298 static PyObject *
299 realize_c_type_or_func(builder_c_t *builder,
300 _cffi_opcode_t opcodes[], int index); /* forward */
301
302
303 /* Interpret an opcodes[] array. If opcodes == ctx->types, store all
304 the intermediate types back in the opcodes[]. Returns a new
305 reference.
306 */
307 static CTypeDescrObject *
308 realize_c_type(builder_c_t *builder, _cffi_opcode_t opcodes[], int index)
309 {
310 PyObject *x = realize_c_type_or_func(builder, opcodes, index);
311 if (x == NULL || CTypeDescr_Check(x))
312 return (CTypeDescrObject *)x;
313 else {
314 unexpected_fn_type(x);
315 Py_DECREF(x);
316 return NULL;
317 }
318 }
319
320 static void _realize_name(char *target, const char *prefix, const char *srcname)
321 {
322 /* "xyz" => "struct xyz"
323 "$xyz" => "xyz"
324 "$1" => "struct $1"
325 */
326 if (srcname[0] == '$' && srcname[1] != '$' &&
327 !('0' <= srcname[1] && srcname[1] <= '9')) {
328 strcpy(target, &srcname[1]);
329 }
330 else {
331 strcpy(target, prefix);
332 strcat(target, srcname);
333 }
334 }
335
336 static void _unrealize_name(char *target, const char *srcname)
337 {
338 /* reverse of _realize_name() */
339 if (strncmp(srcname, "struct ", 7) == 0) {
340 strcpy(target, &srcname[7]);
341 }
342 else if (strncmp(srcname, "union ", 6) == 0) {
343 strcpy(target, &srcname[6]);
344 }
345 else if (strncmp(srcname, "enum ", 5) == 0) {
346 strcpy(target, &srcname[5]);
347 }
348 else {
349 strcpy(target, "$");
350 strcat(target, srcname);
351 }
352 }
353
354 static PyObject * /* forward */
355 _fetch_external_struct_or_union(const struct _cffi_struct_union_s *s,
356 PyObject *included_ffis, int recursion);
357
358 static PyObject *
359 _realize_c_struct_or_union(builder_c_t *builder, int sindex)
360 {
361 PyObject *x;
362 _cffi_opcode_t op2;
363 const struct _cffi_struct_union_s *s;
364
365 if (sindex == _CFFI__IO_FILE_STRUCT) {
366 /* returns a single global cached opaque type */
367 Py_INCREF(g_file_struct);
368 return (PyObject *)g_file_struct;
369 }
370
371 s = &builder->ctx.struct_unions[sindex];
372 op2 = _CFFI_LOAD_OP(builder->ctx.types[s->type_index]);
373 if ((((uintptr_t)op2) & 1) == 0) {
374 x = (PyObject *)op2; /* found already in the "primary" slot */
375 Py_INCREF(x);
376 }
377 else {
378 CTypeDescrObject *ct = NULL;
379
380 if (!(s->flags & _CFFI_F_EXTERNAL)) {
381 int flags = (s->flags & _CFFI_F_UNION) ? CT_UNION : CT_STRUCT;
382 int is_opaque = (s->flags & _CFFI_F_OPAQUE);
383 flags |= is_opaque ? CT_IS_OPAQUE : 0;
384 char *name = alloca(8 + strlen(s->name));
385 _realize_name(name,
386 (s->flags & _CFFI_F_UNION) ? "union " : "struct ",
387 s->name);
388 if (strcmp(name, "struct _IO_FILE") == 0)
389 x = _realize_c_struct_or_union(builder, _CFFI__IO_FILE_STRUCT);
390 else
391 x = new_struct_or_union_type(name, flags);
392 if (x == NULL)
393 return NULL;
394
395 if (!is_opaque) {
396 assert(s->first_field_index >= 0);
397 ct = (CTypeDescrObject *)x;
398 ct->ct_size = (Py_ssize_t)s->size;
399 ct->ct_length = s->alignment; /* may be -1 */
400 ct->ct_lazy_field_list = 1;
401 ct->ct_extra = builder;
402 }
403 else {
404 assert(s->first_field_index < 0);
405 }
406 }
407 else {
408 assert(s->first_field_index < 0);
409 x = _fetch_external_struct_or_union(s, builder->included_ffis, 0);
410 if (x == NULL) {
411 if (!PyErr_Occurred())
412 PyErr_Format(FFIError, "'%s %.200s' should come from "
413 "ffi.include() but was not found",
414 (s->flags & _CFFI_F_UNION) ? "union"
415 : "struct", s->name);
416 return NULL;
417 }
418 if (!(s->flags & _CFFI_F_OPAQUE)) {
419 if (((CTypeDescrObject *)x)->ct_flags & CT_IS_OPAQUE) {
420 const char *prefix = (s->flags & _CFFI_F_UNION) ? "union"
421 : "struct";
422 PyErr_Format(PyExc_NotImplementedError,
423 "'%s %.200s' is opaque in the ffi.include(), "
424 "but no longer in the ffi doing the include "
425 "(workaround: don't use ffi.include() but "
426 "duplicate the declarations of everything "
427 "using %s %.200s)",
428 prefix, s->name, prefix, s->name);
429 Py_DECREF(x);
430 return NULL;
431 }
432 }
433 }
434 if (ct != NULL) {
435 cffi_set_flag(ct->ct_unrealized_struct_or_union, 0);
436 }
437
438 /* Update the "primary" OP_STRUCT_UNION slot */
439 assert((((uintptr_t)x) & 1) == 0);
440 assert(builder->ctx.types[s->type_index] == op2);
441 Py_INCREF(x);
442 #ifdef Py_GIL_DISABLED
443 cffi_atomic_store(&builder->ctx.types[s->type_index], x);
444 #else
445 builder->ctx.types[s->type_index] = x;
446 #endif
447 if (ct != NULL && s->size == (size_t)-2) {
448 /* oops, this struct is unnamed and we couldn't generate
449 a C expression to get its size. We have to rely on
450 complete_struct_or_union() to compute it now. */
451 if (do_realize_lazy_struct(ct) < 0) {
452 #ifdef Py_GIL_DISABLED
453 cffi_atomic_store(&builder->ctx.types[s->type_index], op2);
454 #else
455 builder->ctx.types[s->type_index] = op2;
456 #endif
457 return NULL;
458 }
459 }
460 }
461 return x;
462 }
463
464 static PyObject *
465 realize_c_type_or_func_now(builder_c_t *builder, _cffi_opcode_t op,
466 _cffi_opcode_t opcodes[], int index)
467 {
468 PyObject *x, *y, *z;
469 Py_ssize_t length = -1;
470
471 switch (_CFFI_GETOP(op)) {
472
473 case _CFFI_OP_PRIMITIVE:
474 x = get_primitive_type(_CFFI_GETARG(op));
475 Py_XINCREF(x);
476 break;
477
478 case _CFFI_OP_POINTER:
479 y = realize_c_type_or_func(builder, opcodes, _CFFI_GETARG(op));
480 if (y == NULL)
481 return NULL;
482 if (CTypeDescr_Check(y)) {
483 x = new_pointer_type((CTypeDescrObject *)y);
484 }
485 else {
486 assert(PyTuple_Check(y)); /* from _CFFI_OP_FUNCTION */
487 x = PyTuple_GET_ITEM(y, 0);
488 Py_INCREF(x);
489 }
490 Py_DECREF(y);
491 break;
492
493 case _CFFI_OP_ARRAY:
494 length = (Py_ssize_t)opcodes[index + 1];
495 /* fall-through */
496 case _CFFI_OP_OPEN_ARRAY:
497 y = (PyObject *)realize_c_type(builder, opcodes, _CFFI_GETARG(op));
498 if (y == NULL)
499 return NULL;
500 z = new_pointer_type((CTypeDescrObject *)y);
501 Py_DECREF(y);
502 if (z == NULL)
503 return NULL;
504 x = new_array_type((CTypeDescrObject *)z, length);
505 Py_DECREF(z);
506 break;
507
508 case _CFFI_OP_STRUCT_UNION:
509 x = _realize_c_struct_or_union(builder, _CFFI_GETARG(op));
510 break;
511
512 case _CFFI_OP_ENUM:
513 {
514 const struct _cffi_enum_s *e;
515 _cffi_opcode_t op2;
516
517 e = &builder->ctx.enums[_CFFI_GETARG(op)];
518 op2 = _CFFI_LOAD_OP(builder->ctx.types[e->type_index]);
519 if ((((uintptr_t)op2) & 1) == 0) {
520 x = (PyObject *)op2;
521 Py_INCREF(x);
522 }
523 else {
524 PyObject *enumerators = NULL, *enumvalues = NULL, *tmp;
525 Py_ssize_t i, j, n = 0;
526 const char *p;
527 int gindex;
528 PyObject *args;
529 PyObject *basetd = get_primitive_type(e->type_prim);
530 if (basetd == NULL)
531 return NULL;
532
533 if (*e->enumerators != '\0') {
534 n++;
535 for (p = e->enumerators; *p != '\0'; p++)
536 n += (*p == ',');
537 }
538 enumerators = PyTuple_New(n);
539 if (enumerators == NULL)
540 return NULL;
541
542 enumvalues = PyTuple_New(n);
543 if (enumvalues == NULL) {
544 Py_DECREF(enumerators);
545 return NULL;
546 }
547
548 p = e->enumerators;
549 for (i = 0; i < n; i++) {
550 j = 0;
551 while (p[j] != ',' && p[j] != '\0')
552 j++;
553 tmp = PyUnicode_FromStringAndSize(p, j);
554 if (tmp == NULL)
555 break;
556 PyTuple_SET_ITEM(enumerators, i, tmp);
557
558 gindex = search_in_globals(&builder->ctx, p, j);
559 assert(gindex >= 0);
560 assert(builder->ctx.globals[gindex].type_op ==
561 _CFFI_OP(_CFFI_OP_ENUM, -1));
562
563 tmp = realize_global_int(builder, gindex);
564 if (tmp == NULL)
565 break;
566 PyTuple_SET_ITEM(enumvalues, i, tmp);
567
568 p += j + 1;
569 }
570
571 args = NULL;
572 if (!PyErr_Occurred()) {
573 char *name = alloca(6 + strlen(e->name));
574 _realize_name(name, "enum ", e->name);
575 args = Py_BuildValue("(sOOO)", name, enumerators,
576 enumvalues, basetd);
577 }
578 Py_DECREF(enumerators);
579 Py_DECREF(enumvalues);
580 if (args == NULL)
581 return NULL;
582
583 x = b_new_enum_type(NULL, args);
584 Py_DECREF(args);
585 if (x == NULL)
586 return NULL;
587
588 /* Update the "primary" _CFFI_OP_ENUM slot, which
589 may be the same or a different slot than the "current" one */
590 assert((((uintptr_t)x) & 1) == 0);
591 assert(builder->ctx.types[e->type_index] == op2);
592 Py_INCREF(x);
593 #ifdef Py_GIL_DISABLED
594 cffi_atomic_store(&builder->ctx.types[e->type_index], x);
595 #else
596 builder->ctx.types[e->type_index] = x;
597 #endif
598
599 /* Done, leave without updating the "current" slot because
600 it may be done already above. If not, never mind, the
601 next call to realize_c_type() will do it. */
602 return x;
603 }
604 break;
605 }
606
607 case _CFFI_OP_FUNCTION:
608 {
609 PyObject *fargs;
610 int i, base_index, num_args, ellipsis, abi;
611
612 y = (PyObject *)realize_c_type(builder, opcodes, _CFFI_GETARG(op));
613 if (y == NULL)
614 return NULL;
615
616 base_index = index + 1;
617 num_args = 0;
618 /* note that if the arguments are already built, they have a
619 pointer in the 'opcodes' array, and GETOP() returns a
620 random even value. But OP_FUNCTION_END is odd, so the
621 condition below still works correctly. */
622 while (_CFFI_GETOP(_CFFI_LOAD_OP(opcodes[base_index + num_args]))
623 != _CFFI_OP_FUNCTION_END)
624 num_args++;
625
626 ellipsis = _CFFI_GETARG(opcodes[base_index + num_args]) & 0x01;
627 abi = _CFFI_GETARG(opcodes[base_index + num_args]) & 0xFE;
628 switch (abi) {
629 case 0:
630 abi = FFI_DEFAULT_ABI;
631 break;
632 case 2:
633 #if defined(MS_WIN32) && !defined(_WIN64)
634 abi = FFI_STDCALL;
635 #else
636 abi = FFI_DEFAULT_ABI;
637 #endif
638 break;
639 default:
640 PyErr_Format(FFIError, "abi number %d not supported", abi);
641 Py_DECREF(y);
642 return NULL;
643 }
644
645 fargs = PyTuple_New(num_args);
646 if (fargs == NULL) {
647 Py_DECREF(y);
648 return NULL;
649 }
650
651 for (i = 0; i < num_args; i++) {
652 z = (PyObject *)realize_c_type(builder, opcodes, base_index + i);
653 if (z == NULL) {
654 Py_DECREF(fargs);
655 Py_DECREF(y);
656 return NULL;
657 }
658 PyTuple_SET_ITEM(fargs, i, z);
659 }
660
661 z = new_function_type(fargs, (CTypeDescrObject *)y, ellipsis, abi);
662 Py_DECREF(fargs);
663 Py_DECREF(y);
664 if (z == NULL)
665 return NULL;
666
667 x = PyTuple_Pack(1, z); /* hack: hide the CT_FUNCTIONPTR. it will
668 be revealed again by the OP_POINTER */
669 Py_DECREF(z);
670 break;
671 }
672
673 case _CFFI_OP_NOOP:
674 x = realize_c_type_or_func(builder, opcodes, _CFFI_GETARG(op));
675 break;
676
677 case _CFFI_OP_TYPENAME:
678 {
679 /* essential: the TYPENAME opcode resolves the type index looked
680 up in the 'ctx->typenames' array, but it does so in 'ctx->types'
681 instead of in 'opcodes'! */
682 int type_index = builder->ctx.typenames[_CFFI_GETARG(op)].type_index;
683 x = realize_c_type_or_func(builder, builder->ctx.types, type_index);
684 break;
685 }
686
687 default:
688 PyErr_Format(PyExc_NotImplementedError, "op=%d", (int)_CFFI_GETOP(op));
689 return NULL;
690 }
691
692 return x;
693 }
694
695 #ifdef Py_GIL_DISABLED
696 #ifdef MS_WIN32
697 static __declspec(thread) int _realize_recursion_level;
698 #elif defined(USE__THREAD)
699 static __thread int _realize_recursion_level;
700 #else
701 #error "Cannot detect thread-local keyword"
702 #endif
703 #else
704 static int _realize_recursion_level;
705 #endif
706
707 static PyObject *
708 realize_c_type_or_func_lock_held(builder_c_t *builder,
709 _cffi_opcode_t opcodes[], int index)
710 {
711
712 PyObject *x;
713 _cffi_opcode_t op = _CFFI_LOAD_OP(opcodes[index]);
714
715 if ((((uintptr_t)op) & 1) == 0) {
716 x = (PyObject *)op;
717 Py_INCREF(x);
718 return x;
719 }
720
721 if (_realize_recursion_level >= 1000) {
722 PyErr_Format(PyExc_RuntimeError,
723 "type-building recursion too deep or infinite. "
724 "This is known to occur e.g. in ``struct s { void(*callable)"
725 "(struct s); }''. Please report if you get this error and "
726 "really need support for your case.");
727 return NULL;
728 }
729 _realize_recursion_level++;
730 x = realize_c_type_or_func_now(builder, op, opcodes, index);
731 _realize_recursion_level--;
732
733 if (x != NULL && opcodes == builder->ctx.types && opcodes[index] != x) {
734 assert((((uintptr_t)x) & 1) == 0);
735 assert((((uintptr_t)opcodes[index]) & 1) == 1);
736 Py_INCREF(x);
737 #ifdef Py_GIL_DISABLED
738 cffi_atomic_store(&opcodes[index], x);
739 #else
740 opcodes[index] = x;
741 #endif
742 }
743
744 return x;
745 }
746
747
748 static PyObject *
749 realize_c_type_or_func(builder_c_t *builder,
750 _cffi_opcode_t opcodes[], int index)
751 {
752 PyObject *x;
753 CFFI_LOCK();
754 x = realize_c_type_or_func_lock_held(builder, opcodes, index);
755 CFFI_UNLOCK();
756 return x;
757 }
758
759 static CTypeDescrObject *
760 realize_c_func_return_type(builder_c_t *builder,
761 _cffi_opcode_t opcodes[], int index)
762 {
763 PyObject *x;
764 _cffi_opcode_t op = opcodes[index];
765
766 if ((((uintptr_t)op) & 1) == 0) {
767 /* already built: assert that it is a function and fish
768 for the return type */
769 x = (PyObject *)op;
770 assert(PyTuple_Check(x)); /* from _CFFI_OP_FUNCTION */
771 x = PyTuple_GET_ITEM(x, 0);
772 assert(CTypeDescr_Check(x));
773 assert(((CTypeDescrObject *)x)->ct_flags & CT_FUNCTIONPTR);
774 x = PyTuple_GET_ITEM(((CTypeDescrObject *)x)->ct_stuff, 1);
775 assert(CTypeDescr_Check(x));
776 Py_INCREF(x);
777 return (CTypeDescrObject *)x;
778 }
779 else {
780 assert(_CFFI_GETOP(op) == _CFFI_OP_FUNCTION);
781 return realize_c_type(builder, opcodes, _CFFI_GETARG(opcodes[index]));
782 }
783 }
784
785 static int do_realize_lazy_struct_lock_held(CTypeDescrObject *ct)
786 {
787 /* This is called by force_lazy_struct() in _cffi_backend.c */
788 assert(ct->ct_flags & (CT_STRUCT | CT_UNION));
789
790 if (cffi_check_flag(ct->ct_lazy_field_list)) {
791 builder_c_t *builder;
792 char *p;
793 int n, i, sflags;
794 const struct _cffi_struct_union_s *s;
795 const struct _cffi_field_s *fld;
796 PyObject *fields, *res;
797
798 // opaque types should never set ct->ct_lazy_field_list
799 assert(!(ct->ct_flags & CT_IS_OPAQUE));
800
801 builder = ct->ct_extra;
802 assert(builder != NULL);
803
804 p = alloca(2 + strlen(ct->ct_name));
805 _unrealize_name(p, ct->ct_name);
806
807 n = search_in_struct_unions(&builder->ctx, p, strlen(p));
808 if (n < 0)
809 Py_FatalError("lost a struct/union!");
810
811 s = &builder->ctx.struct_unions[n];
812 fld = &builder->ctx.fields[s->first_field_index];
813
814 /* XXX painfully build all the Python objects that are the args
815 to b_complete_struct_or_union() */
816
817 fields = PyList_New(s->num_fields);
818 if (fields == NULL)
819 return -1;
820
821 for (i = 0; i < s->num_fields; i++, fld++) {
822 _cffi_opcode_t op = fld->field_type_op;
823 int fbitsize = -1;
824 PyObject *f;
825 CTypeDescrObject *ctf;
826
827 switch (_CFFI_GETOP(op)) {
828
829 case _CFFI_OP_BITFIELD:
830 assert(fld->field_size >= 0);
831 fbitsize = (int)fld->field_size;
832 /* fall-through */
833 case _CFFI_OP_NOOP:
834 ctf = realize_c_type(builder, builder->ctx.types,
835 _CFFI_GETARG(op));
836 break;
837
838 default:
839 Py_DECREF(fields);
840 PyErr_Format(PyExc_NotImplementedError, "field op=%d",
841 (int)_CFFI_GETOP(op));
842 return -1;
843 }
844
845 if (ctf != NULL && fld->field_offset == (size_t)-1) {
846 /* unnamed struct, with field positions and sizes entirely
847 determined by complete_struct_or_union() and not checked.
848 Or, bitfields (field_size >= 0), similarly not checked. */
849 assert(fld->field_size == (size_t)-1 || fbitsize >= 0);
850 }
851 else if (ctf == NULL || detect_custom_layout(ct, SF_STD_FIELD_POS,
852 ctf->ct_size, fld->field_size,
853 "wrong size for field '",
854 fld->name, "'") < 0) {
855 Py_DECREF(fields);
856 return -1;
857 }
858
859 f = Py_BuildValue("(sOin)", fld->name, ctf,
860 fbitsize, (Py_ssize_t)fld->field_offset);
861 if (f == NULL) {
862 Py_DECREF(fields);
863 return -1;
864 }
865 PyList_SET_ITEM(fields, i, f);
866 }
867
868 sflags = 0;
869 if (s->flags & _CFFI_F_CHECK_FIELDS)
870 sflags |= SF_STD_FIELD_POS;
871 if (s->flags & _CFFI_F_PACKED)
872 sflags |= SF_PACKED;
873
874 ct->ct_extra = NULL;
875 cffi_set_flag(ct->ct_under_construction, 1);
876 res = b_complete_struct_or_union_lock_held(ct, fields, s->size, s->alignment,
877 sflags, 0);
878 cffi_set_flag(ct->ct_under_construction, 0);
879 Py_DECREF(fields);
880
881 if (res == NULL) {
882 ct->ct_extra = builder;
883 return -1;
884 }
885
886 assert(ct->ct_stuff != NULL);
887 cffi_set_flag(ct->ct_lazy_field_list, 0);
888 Py_DECREF(res);
889 }
890 return ct->ct_stuff != NULL;
891 }
892
893
894 static int do_realize_lazy_struct(CTypeDescrObject *ct)
895 {
896 int res = 0;
897 CFFI_LOCK();
898 res = do_realize_lazy_struct_lock_held(ct);
899 CFFI_UNLOCK();
900 return res;
901 }