File indexing completed on 2025-01-30 10:18:04
0001 #ifndef Py_INTERNAL_ASDL_H
0002 #define Py_INTERNAL_ASDL_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006
0007 #ifndef Py_BUILD_CORE
0008 # error "this header requires Py_BUILD_CORE define"
0009 #endif
0010
0011 #include "pycore_pyarena.h" // _PyArena_Malloc()
0012
0013 typedef PyObject * identifier;
0014 typedef PyObject * string;
0015 typedef PyObject * object;
0016 typedef PyObject * constant;
0017
0018
0019
0020
0021
0022
0023
0024 #define _ASDL_SEQ_HEAD \
0025 Py_ssize_t size; \
0026 void **elements;
0027
0028 typedef struct {
0029 _ASDL_SEQ_HEAD
0030 } asdl_seq;
0031
0032 typedef struct {
0033 _ASDL_SEQ_HEAD
0034 void *typed_elements[1];
0035 } asdl_generic_seq;
0036
0037 typedef struct {
0038 _ASDL_SEQ_HEAD
0039 PyObject *typed_elements[1];
0040 } asdl_identifier_seq;
0041
0042 typedef struct {
0043 _ASDL_SEQ_HEAD
0044 int typed_elements[1];
0045 } asdl_int_seq;
0046
0047 asdl_generic_seq *_Py_asdl_generic_seq_new(Py_ssize_t size, PyArena *arena);
0048 asdl_identifier_seq *_Py_asdl_identifier_seq_new(Py_ssize_t size, PyArena *arena);
0049 asdl_int_seq *_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena);
0050
0051
0052 #define GENERATE_ASDL_SEQ_CONSTRUCTOR(NAME, TYPE) \
0053 asdl_ ## NAME ## _seq *_Py_asdl_ ## NAME ## _seq_new(Py_ssize_t size, PyArena *arena) \
0054 { \
0055 asdl_ ## NAME ## _seq *seq = NULL; \
0056 size_t n; \
0057 \
0058 if (size < 0 || \
0059 (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) { \
0060 PyErr_NoMemory(); \
0061 return NULL; \
0062 } \
0063 n = (size ? (sizeof(TYPE *) * (size - 1)) : 0); \
0064 \
0065 if (n > SIZE_MAX - sizeof(asdl_ ## NAME ## _seq)) { \
0066 PyErr_NoMemory(); \
0067 return NULL; \
0068 } \
0069 n += sizeof(asdl_ ## NAME ## _seq); \
0070 seq = (asdl_ ## NAME ## _seq *)_PyArena_Malloc(arena, n); \
0071 if (!seq) { \
0072 PyErr_NoMemory(); \
0073 return NULL; \
0074 } \
0075 memset(seq, 0, n); \
0076 seq->size = size; \
0077 seq->elements = (void**)seq->typed_elements; \
0078 return seq; \
0079 }
0080
0081 #define asdl_seq_GET_UNTYPED(S, I) _Py_RVALUE((S)->elements[(I)])
0082 #define asdl_seq_GET(S, I) _Py_RVALUE((S)->typed_elements[(I)])
0083 #define asdl_seq_LEN(S) _Py_RVALUE(((S) == NULL ? 0 : (S)->size))
0084
0085 #ifdef Py_DEBUG
0086 # define asdl_seq_SET(S, I, V) \
0087 do { \
0088 Py_ssize_t _asdl_i = (I); \
0089 assert((S) != NULL); \
0090 assert(0 <= _asdl_i && _asdl_i < (S)->size); \
0091 (S)->typed_elements[_asdl_i] = (V); \
0092 } while (0)
0093 #else
0094 # define asdl_seq_SET(S, I, V) _Py_RVALUE((S)->typed_elements[(I)] = (V))
0095 #endif
0096
0097 #ifdef Py_DEBUG
0098 # define asdl_seq_SET_UNTYPED(S, I, V) \
0099 do { \
0100 Py_ssize_t _asdl_i = (I); \
0101 assert((S) != NULL); \
0102 assert(0 <= _asdl_i && _asdl_i < (S)->size); \
0103 (S)->elements[_asdl_i] = (V); \
0104 } while (0)
0105 #else
0106 # define asdl_seq_SET_UNTYPED(S, I, V) _Py_RVALUE((S)->elements[(I)] = (V))
0107 #endif
0108
0109 #ifdef __cplusplus
0110 }
0111 #endif
0112 #endif