Debian Codegraph @

lib_pypy/hpy/devel/include/hpy.h

raw symbols SCIP
1 #ifndef HPy_H
2 #define HPy_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 /* ~~~~~~~~~~~~~~~~ HPy ABI version ~~~~~~~~~~~~~~~ */
8 // NOTE: these must be kept on sync with the equivalent variables in hpy/devel/abitag.py
9 /**
10 * The ABI version.
11 *
12 * Minor version N+1 is binary compatible to minor version N. Major versions
13 * are not binary compatible (note: HPy can run several binary incompatible
14 * versions in one process).
15 */
16 #define HPY_ABI_VERSION 0
17 #define HPY_ABI_VERSION_MINOR 0
18 #define HPY_ABI_TAG "hpy0"
19
20
21 /* ~~~~~~~~~~~~~~~~ HPy ABI macros ~~~~~~~~~~~~~~~~ */
22
23 /* The following macros are used to determine which kind of module we want to
24 compile. The build system must set one of these (e.g. by using `gcc
25 -D...`). This is the approach used by the setuptools support provided by
26 hpy.devel:
27
28 - HPY_ABI_CPYTHON
29 - HPY_ABI_UNIVERSAL
30 - HPY_ABI_HYBRID
31
32 In addition we also define HPY_ABI which is a string literal containing a
33 string representation of it.
34 */
35
36 #if defined(HPY_ABI_CPYTHON)
37 # if defined(HPY_ABI_HYBRID)
38 # error "Conflicting macros are defined: HPY_ABI_CPYTHON and HPY_ABI_HYBRID"
39 # endif
40 # if defined(HPY_ABI_UNIVERSAL)
41 # error "Conflicting macros are defined: HPY_ABI_CPYTHON and HPY_ABI_UNIVERSAL"
42 # endif
43 # define HPY_ABI "cpython"
44
45 #elif defined(HPY_ABI_HYBRID)
46 # if defined(HPY_ABI_UNIVERSAL)
47 # error "Conflicting macros are defined: HPY_ABI_HYBRID and HPY_ABI_UNIVERSAL"
48 # endif
49 # define HPY_ABI "hybrid"
50
51 #elif defined(HPY_ABI_UNIVERSAL)
52 # define HPY_ABI "universal"
53
54 #else
55 # error "Cannot determine the desired HPy ABI: you must set one of HPY_ABI_CPYTHON, HPY_ABI_UNIVERSAL or HPY_ABI_HYBRID"
56 #endif
57
58
59 #if defined(HPY_ABI_CPYTHON) || defined(HPY_ABI_HYBRID)
60 # define PY_SSIZE_T_CLEAN
61 # include <Python.h>
62 #endif
63
64 #include <stdlib.h>
65 #include <stdint.h>
66 #include <stdarg.h>
67
68 /* ~~~~~~~~~~~~~~~~ useful macros ~~~~~~~~~~~~~~~~ */
69
70 #ifdef __GNUC__
71 # define _HPy_HIDDEN __attribute__((visibility("hidden")))
72 # define _HPy_UNUSED __attribute__((unused))
73 #else
74 # define _HPy_HIDDEN
75 # define _HPy_UNUSED
76 #endif /* __GNUC__ */
77 #define _HPy_UNUSED_ARG(x) (__HPy_UNUSED_TAGGED ## x) _HPy_UNUSED
78
79 #if defined(__clang__) || \
80 (defined(__GNUC__) && \
81 ((__GNUC__ >= 3) || \
82 (__GNUC__ == 2) && (__GNUC_MINOR__ >= 5)))
83 # define _HPy_NO_RETURN __attribute__((__noreturn__))
84 #elif defined(_MSC_VER)
85 # define _HPy_NO_RETURN __declspec(noreturn)
86 #else
87 # define _HPy_NO_RETURN
88 #endif
89
90
91 // clang and gcc supports __has_attribute, MSVC doesn't. This should be enough
92 // to be able to use it portably
93 #ifdef __has_attribute
94 # define _HPY_compiler_has_attribute(x) __has_attribute(x)
95 #else
96 # define _HPY_compiler_has_attribute(x) 0
97 #endif
98
99 #ifdef HPY_ABI_UNIVERSAL
100 # if _HPY_compiler_has_attribute(error)
101 // gcc, clang>=14
102 # define _HPY_LEGACY __attribute__((error("Cannot use legacy functions when targeting the HPy Universal ABI")))
103 # else
104 // we don't have any diagnostic feature, too bad
105 # define _HPY_LEGACY
106 # endif
107 #else
108 // in non-universal modes, we don't attach any attribute
109 # define _HPY_LEGACY
110 #endif
111
112 #if defined(_MSC_VER) && defined(__cplusplus) // MSVC C4576
113 # define _hconv(h) {h}
114 # define _hfconv(h) {h}
115 # define _htsconv(h) {h}
116 # define _hgconv(h) {h}
117 #else
118 # define _hconv(h) ((HPy){h})
119 # define _hfconv(h) ((HPyField){h})
120 # define _htsconv(h) ((HPyThreadState){h})
121 # define _hgconv(h) ((HPyGlobal){h})
122 #endif
123 /* ~~~~~~~~~~~~~~~~ HPyAPI declaration ~~~~~~~~~~~~~~~~ */
124
125 /* We have three different kind of API functions: */
126
127 /**
128 * Public API functions which are exposed to the user, e.g.
129 * ``HPy_Add`` or ``HPyType_FromSpec``. Generally speaking they are
130 * thin shims dispatching to the actual implementation:
131 *
132 * * In CPython-ABI mode they directly call the corresponding Py* or
133 * ``HPyAPI_IMPL`` equivalent, e.g. ``PyObject_Add`` or
134 * ``ctx_Type_FromSpec``.
135 *
136 * * In Universal-ABI mode, they always resolve to an indirect call
137 * through ``HPyContext *``, i.e. ``ctx->ctx_Add(...)``, which on CPython
138 * dispaches to ``ctx_Add``.
139 */
140 #define HPyAPI_FUNC _HPy_UNUSED static inline
141
142 /** An alias for ``HPyAPI_FUNC`` so we can handle it properly in the docs. */
143 #define HPyAPI_INLINE_HELPER HPyAPI_FUNC
144
145 /**
146 * CPython implementations for ``HPyAPI_FUNC``
147 * functions. Generally speaking, they are put in ctx_*.c files and they are
148 * prefixed by ctx\_.
149 *
150 * Some of these functions are needed by the CPython ABI mode, and by
151 * CPython's implementation of hpy.universal: these can be found in
152 * hpy/devel/src/runtime/ctx_*.c, e.g. ``ctx_Type_FromSpec`` and
153 * ``ctx_Tuple_FromArray``.
154 *
155 * Some other are used ONLY by ``hpy.universal`` and can be found in
156 * hpy/universal/src/ctx_*.c.
157 */
158 #define HPyAPI_IMPL _HPy_HIDDEN
159
160 /**
161 * These functions are part of the public API but **not** of
162 * the ABI. They are helpers which are meant to be compiled togeher with every
163 * extension. E.g. ``HPyArg_Parse`` and ``HPyHelpers_AddType``.
164 */
165 #define HPyAPI_HELPER _HPy_HIDDEN
166
167
168 /* ~~~~~~~~~~~~~~~~ Definition of the type HPy ~~~~~~~~~~~~~~~~ */
169
170 /* HPy handles are fully opaque: depending on the implementation, the _i can
171 be either an integer or a pointer. A few examples:
172
173 * in CPython ABI mode, ._i is a PyObject*
174
175 * in Universal ABI mode, the meaning of ._i depends on the implementation:
176
177 - CPython (i.e., the code in hpy/universal/src/): ._i is the bitwise
178 invert of a PyObject*
179
180 - PyPy: ._i is an index into a list
181
182 - GraalPy: ._i is a tagged value, either an index into a list,
183 or an immediate integer or double value
184
185 - Debug mode: _i is a pointer to a DebugHandle, which contains a
186 another HPy among other stuff
187 */
188 typedef struct _HPy_s { intptr_t _i; } HPy;
189 typedef struct { intptr_t _i; } HPyField;
190 typedef struct { intptr_t _i; } HPyGlobal;
191 typedef struct { intptr_t _lst; } HPyListBuilder;
192 typedef struct { intptr_t _tup; } HPyTupleBuilder;
193 typedef struct { intptr_t _i; } HPyTracker;
194 typedef struct { intptr_t _i; } HPyThreadState;
195
196
197 /* A null handle is officially defined as a handle whose _i is 0. This is true
198 in all ABI modes. */
199 #define HPy_NULL _hconv(0)
200 #define HPy_IsNull(h) ((h)._i == 0)
201
202 #define HPyField_NULL _hfconv(0)
203 #define HPyField_IsNull(f) ((f)._i == 0)
204
205 /* Convenience functions to cast between HPy and void*. We need to decide
206 whether these are part of the official API or not, and maybe introduce a
207 better naming convention. For now, they are needed for ujson. */
208 static inline HPy HPy_FromVoidP(void *p) { return _hconv((intptr_t)p); }
209 static inline void* HPy_AsVoidP(HPy h) { return (void*)h._i; }
210
211
212 /* ~~~~~~~~~~~~~~~~ Definition of other types ~~~~~~~~~~~~~~~~ */
213
214 typedef struct _HPyContext_s HPyContext;
215
216 /** An enumeration of the different kinds of source code strings. */
217 typedef enum {
218 /** Parse isolated expressions (e.g. ``a + b``). */
219 HPy_SourceKind_Expr = 0,
220
221 /**
222 * Parse sequences of statements as read from a file or other source. This
223 * is the symbol to use when compiling arbitrarily long Python source code.
224 */
225 HPy_SourceKind_File = 1,
226
227 /**
228 * Parse a single statement. This is the mode used for the interactive
229 * interpreter loop.
230 */
231 HPy_SourceKind_Single = 2,
232 } HPy_SourceKind;
233
234 #ifdef HPY_ABI_CPYTHON
235 typedef Py_ssize_t HPy_ssize_t;
236 typedef Py_hash_t HPy_hash_t;
237 typedef Py_UCS4 HPy_UCS4;
238
239 # define HPY_SSIZE_T_MAX PY_SSIZE_T_MAX
240 # define HPY_SSIZE_T_MIN PY_SSIZE_T_MIN
241
242 #else
243 typedef intptr_t HPy_ssize_t;
244 typedef intptr_t HPy_hash_t;
245 typedef uint32_t HPy_UCS4;
246
247 # define HPY_SSIZE_T_MAX INTPTR_MAX
248 # define HPY_SSIZE_T_MIN (-HPY_SSIZE_T_MAX-1)
249
250 /* HPyCapsule field keys */
251 typedef enum {
252 HPyCapsule_key_Pointer = 0,
253 HPyCapsule_key_Name = 1,
254 HPyCapsule_key_Context = 2,
255 HPyCapsule_key_Destructor = 3,
256 } _HPyCapsule_key;
257 #endif
258
259
260 /* ~~~~~~~~~~~~~~~~ Additional #includes ~~~~~~~~~~~~~~~~ */
261
262 #include "hpy/cpy_types.h"
263 #include "hpy/hpyexports.h"
264 #include "hpy/macros.h"
265 #include "hpy/hpyfunc.h"
266 #include "hpy/hpydef.h"
267 #include "hpy/hpytype.h"
268 #include "hpy/hpymodule.h"
269 #include "hpy/runtime/argparse.h"
270 #include "hpy/runtime/buildvalue.h"
271 #include "hpy/runtime/format.h"
272 #include "hpy/runtime/helpers.h"
273 #include "hpy/runtime/structseq.h"
274
275 #ifdef HPY_ABI_CPYTHON
276 # include "hpy/cpython/autogen_ctx.h"
277 # include "hpy/runtime/ctx_funcs.h"
278 # include "hpy/runtime/ctx_type.h"
279 # include "hpy/cpython/misc.h"
280 # include "hpy/cpython/autogen_api_impl.h"
281 #else
282 # include "hpy/universal/autogen_ctx.h"
283 # include "hpy/universal/autogen_trampolines.h"
284 # include "hpy/universal/misc_trampolines.h"
285 #endif
286
287 #include "hpy/inline_helpers.h"
288
289 #ifdef __cplusplus
290 }
291 #endif
292 #endif /* HPy_H */