blob: ed8a28eb7b628ae621b0fddeacf665ead6989038 [file] [log] [blame]
Tim Peters7d3a5112000-07-08 04:17:21 +00001/***********************************************************
2Copyright (c) 2000, BeOpen.com.
3All rights reserved.
4
5See the file "Misc/COPYRIGHT" for information on usage and
6redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7******************************************************************/
8
9#ifndef Py_PYPORT_H
Vladimir Marangozov14a4d882000-07-10 04:59:49 +000010#define Py_PYPORT_H
Tim Peters7d3a5112000-07-08 04:17:21 +000011
12/**************************************************************************
13Symbols and macros to supply platform-independent interfaces to basic
Tim Peters1be46842000-07-23 18:10:18 +000014C language & library operations whose spellings vary across platforms.
Tim Peters7d3a5112000-07-08 04:17:21 +000015
16Please try to make documentation here as clear as possible: by definition,
17the stuff here is trying to illuminate C's darkest corners.
18
19Config #defines referenced here:
20
21SIGNED_RIGHT_SHIFT_ZERO_FILLS
22Meaning: To be defined iff i>>j does not extend the sign bit when i is a
23 signed integral type and i < 0.
24Used in: Py_ARITHMETIC_RIGHT_SHIFT
Tim Peters1be46842000-07-23 18:10:18 +000025
26RETSIGTYPE
27Meaning: Expands to void or int, depending on what the platform wants
28 signal handlers to return. Note that only void is ANSI!
29Used in: Py_RETURN_FROM_SIGNAL_HANDLER
Tim Peters8315ea52000-07-23 19:28:35 +000030
31Py_DEBUG
32Meaning: Extra checks compiled in for debug mode.
33Used in: Py_SAFE_DOWNCAST
Tim Peters7d3a5112000-07-08 04:17:21 +000034**************************************************************************/
35
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/* Py_ARITHMETIC_RIGHT_SHIFT
42 * C doesn't define whether a right-shift of a signed integer sign-extends
43 * or zero-fills. Here a macro to force sign extension:
44 * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
45 * Return I >> J, forcing sign extension.
46 * Requirements:
47 * I is of basic signed type TYPE (char, short, int, long, or long long).
48 * TYPE is one of char, short, int, long, or long long, although long long
49 * must not be used except on platforms that support it.
50 * J is an integer >= 0 and strictly less than the number of bits in TYPE
51 * (because C doesn't define what happens for J outside that range either).
52 * Caution:
53 * I may be evaluated more than once.
54 */
55#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
56#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
57 ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
58#else
59#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
60#endif
61
Tim Peters1be46842000-07-23 18:10:18 +000062/* Py_FORCE_EXPANSION
63 * "Simply" returns its argument. However, macro expansions within the
64 * argument are evaluated. This unfortunate trickery is needed to get
65 * token-pasting to work as desired in some cases.
66 */
67#define Py_FORCE_EXPANSION(X) X
68
69/* Py_RETURN_FROM_SIGNAL_HANDLER
70 * The return from a signal handler varies depending on whether RETSIGTYPE
71 * is int or void. The macro Py_RETURN_FROM_SIGNAL_HANDLER(VALUE) expands
72 * to
73 * return VALUE
74 * if RETSIGTYPE is int, else to nothing if RETSIGTYPE is void.
75 */
76#define int_PySIGRETURN(VALUE) return VALUE
77#define void_PySIGRETURN(VALUE)
78#define Py_RETURN_FROM_SIGNAL_HANDLER(VALUE) \
79 Py_FORCE_EXPANSION(RETSIGTYPE) ## _PySIGRETURN(VALUE)
80
Tim Peters8315ea52000-07-23 19:28:35 +000081/* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
82 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
83 * assert-fails if any information is lost.
84 * Caution:
85 * VALUE may be evaluated more than once.
86 */
87#ifdef Py_DEBUG
88#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
89 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
90#else
91#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
92#endif
93
Tim Peters7d3a5112000-07-08 04:17:21 +000094#ifdef __cplusplus
95}
96#endif
97
98#endif /* Py_PYPORT_H */