pycore_asdl.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef Py_INTERNAL_ASDL_H
  2. #define Py_INTERNAL_ASDL_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. #include "pycore_pyarena.h" // _PyArena_Malloc()
  10. typedef PyObject * identifier;
  11. typedef PyObject * string;
  12. typedef PyObject * object;
  13. typedef PyObject * constant;
  14. /* It would be nice if the code generated by asdl_c.py was completely
  15. independent of Python, but it is a goal the requires too much work
  16. at this stage. So, for example, I'll represent identifiers as
  17. interned Python strings.
  18. */
  19. #define _ASDL_SEQ_HEAD \
  20. Py_ssize_t size; \
  21. void **elements;
  22. typedef struct {
  23. _ASDL_SEQ_HEAD
  24. } asdl_seq;
  25. typedef struct {
  26. _ASDL_SEQ_HEAD
  27. void *typed_elements[1];
  28. } asdl_generic_seq;
  29. typedef struct {
  30. _ASDL_SEQ_HEAD
  31. PyObject *typed_elements[1];
  32. } asdl_identifier_seq;
  33. typedef struct {
  34. _ASDL_SEQ_HEAD
  35. int typed_elements[1];
  36. } asdl_int_seq;
  37. asdl_generic_seq *_Py_asdl_generic_seq_new(Py_ssize_t size, PyArena *arena);
  38. asdl_identifier_seq *_Py_asdl_identifier_seq_new(Py_ssize_t size, PyArena *arena);
  39. asdl_int_seq *_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena);
  40. #define GENERATE_ASDL_SEQ_CONSTRUCTOR(NAME, TYPE) \
  41. asdl_ ## NAME ## _seq *_Py_asdl_ ## NAME ## _seq_new(Py_ssize_t size, PyArena *arena) \
  42. { \
  43. asdl_ ## NAME ## _seq *seq = NULL; \
  44. size_t n; \
  45. /* check size is sane */ \
  46. if (size < 0 || \
  47. (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) { \
  48. PyErr_NoMemory(); \
  49. return NULL; \
  50. } \
  51. n = (size ? (sizeof(TYPE *) * (size - 1)) : 0); \
  52. /* check if size can be added safely */ \
  53. if (n > SIZE_MAX - sizeof(asdl_ ## NAME ## _seq)) { \
  54. PyErr_NoMemory(); \
  55. return NULL; \
  56. } \
  57. n += sizeof(asdl_ ## NAME ## _seq); \
  58. seq = (asdl_ ## NAME ## _seq *)_PyArena_Malloc(arena, n); \
  59. if (!seq) { \
  60. PyErr_NoMemory(); \
  61. return NULL; \
  62. } \
  63. memset(seq, 0, n); \
  64. seq->size = size; \
  65. seq->elements = (void**)seq->typed_elements; \
  66. return seq; \
  67. }
  68. #define asdl_seq_GET_UNTYPED(S, I) _Py_RVALUE((S)->elements[(I)])
  69. #define asdl_seq_GET(S, I) _Py_RVALUE((S)->typed_elements[(I)])
  70. #define asdl_seq_LEN(S) _Py_RVALUE(((S) == NULL ? 0 : (S)->size))
  71. #ifdef Py_DEBUG
  72. # define asdl_seq_SET(S, I, V) \
  73. do { \
  74. Py_ssize_t _asdl_i = (I); \
  75. assert((S) != NULL); \
  76. assert(0 <= _asdl_i && _asdl_i < (S)->size); \
  77. (S)->typed_elements[_asdl_i] = (V); \
  78. } while (0)
  79. #else
  80. # define asdl_seq_SET(S, I, V) _Py_RVALUE((S)->typed_elements[(I)] = (V))
  81. #endif
  82. #ifdef Py_DEBUG
  83. # define asdl_seq_SET_UNTYPED(S, I, V) \
  84. do { \
  85. Py_ssize_t _asdl_i = (I); \
  86. assert((S) != NULL); \
  87. assert(0 <= _asdl_i && _asdl_i < (S)->size); \
  88. (S)->elements[_asdl_i] = (V); \
  89. } while (0)
  90. #else
  91. # define asdl_seq_SET_UNTYPED(S, I, V) _Py_RVALUE((S)->elements[(I)] = (V))
  92. #endif
  93. #ifdef __cplusplus
  94. }
  95. #endif
  96. #endif /* !Py_INTERNAL_ASDL_H */