| 1 |
#ifndef HPy_H |
| 2 |
#define HPy_H |
| 3 |
#ifdef __cplusplus |
| 4 |
extern "C" { |
| 5 |
#endif |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#define HPY_ABI_VERSION 0 |
| 17 |
#define HPY_ABI_VERSION_MINOR 0 |
| 18 |
#define HPY_ABI_TAG "hpy0" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 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 |
| 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 |
|
| 92 |
|
| 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 |
|
| 102 |
# define _HPY_LEGACY __attribute__((error("Cannot use legacy functions when targeting the HPy Universal ABI"))) |
| 103 |
# else |
| 104 |
|
| 105 |
# define _HPY_LEGACY |
| 106 |
# endif |
| 107 |
#else |
| 108 |
|
| 109 |
# define _HPY_LEGACY |
| 110 |
#endif |
| 111 |
|
| 112 |
#if defined(_MSC_VER) && defined(__cplusplus) |
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#define HPyAPI_FUNC _HPy_UNUSED static inline |
| 141 |
|
| 142 |
|
| 143 |
#define HPyAPI_INLINE_HELPER HPyAPI_FUNC |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
#define HPyAPI_IMPL _HPy_HIDDEN |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
#define HPyAPI_HELPER _HPy_HIDDEN |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 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 |
|
| 198 |
|
| 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 |
|
| 206 |
|
| 207 |
|
| 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 |
|
| 213 |
|
| 214 |
typedef struct _HPyContext_s HPyContext; |
| 215 |
|
| 216 |
|
| 217 |
typedef enum { |
| 218 |
|
| 219 |
HPy_SourceKind_Expr = 0, |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
HPy_SourceKind_File = 1, |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 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 |
|
| 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 |
|
| 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 |