1 /* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2  */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /**
8  * This header contains the builtin types available for arguments and return
9  * values, representing their C counterparts. They are used inside higher-order
10  * macros that the user must call, providing a macro that will consume the
11  * arguments provided to it by the higher-order macro. The macros exposed are:
12  *
13  *   CTYPES_FOR_EACH_BOOL_TYPE(MACRO)
14  *   CTYPES_FOR_EACH_CHAR_TYPE(MACRO)
15  *   CTYPES_FOR_EACH_CHAR16_TYPE(MACRO)
16  *   CTYPES_FOR_EACH_INT_TYPE(MACRO)
17  *   CTYPES_FOR_EACH_WRAPPED_INT_TYPE(MACRO)
18  *   CTYPES_FOR_EACH_FLOAT_TYPE(MACRO)
19  *   CTYPES_FOR_EACH_TYPE(MACRO)
20  *
21  * The MACRO name provided to any of these macros will then be repeatedly
22  * invoked as
23  *
24  *   MACRO(typename, ctype, ffitype)
25  *
26  * where 'typename' is the name of the type constructor (accessible as
27  * ctypes.typename), 'ctype' is the corresponding C type declaration (from
28  * which sizeof(ctype) and templated type conversions will be derived), and
29  * 'ffitype' is the ffi_type to use. (Special types, such as 'void' and the
30  * pointer, array, and struct types are handled separately.)
31  */
32 
33 #ifndef ctypes_typedefs_h
34 #define ctypes_typedefs_h
35 
36 // MSVC doesn't have ssize_t. Help it along a little.
37 #ifdef HAVE_SSIZE_T
38 #define CTYPES_SSIZE_T ssize_t
39 #else
40 #define CTYPES_SSIZE_T intptr_t
41 #endif
42 
43 // Some #defines to make handling of types whose length varies by platform
44 // easier. These could be implemented as configure tests, but the expressions
45 // are all statically resolvable so there's no need. (See CTypes.cpp for the
46 // appropriate PR_STATIC_ASSERTs; they can't go here since this header is
47 // used in places where such asserts are illegal.)
48 #define CTYPES_FFI_BOOL (sizeof(bool) == 1 ? ffi_type_uint8 : ffi_type_uint32)
49 #define CTYPES_FFI_LONG (sizeof(long) == 4 ? ffi_type_sint32 : ffi_type_sint64)
50 #define CTYPES_FFI_ULONG (sizeof(long) == 4 ? ffi_type_uint32 : ffi_type_uint64)
51 #define CTYPES_FFI_SIZE_T \
52   (sizeof(size_t) == 4 ? ffi_type_uint32 : ffi_type_uint64)
53 #define CTYPES_FFI_SSIZE_T \
54   (sizeof(size_t) == 4 ? ffi_type_sint32 : ffi_type_sint64)
55 #define CTYPES_FFI_OFF_T \
56   (sizeof(off_t) == 4 ? ffi_type_sint32 : ffi_type_sint64)
57 #define CTYPES_FFI_INTPTR_T \
58   (sizeof(uintptr_t) == 4 ? ffi_type_sint32 : ffi_type_sint64)
59 #define CTYPES_FFI_UINTPTR_T \
60   (sizeof(uintptr_t) == 4 ? ffi_type_uint32 : ffi_type_uint64)
61 
62 #define CTYPES_FOR_EACH_BOOL_TYPE(MACRO) MACRO(bool, bool, CTYPES_FFI_BOOL)
63 
64 #define CTYPES_FOR_EACH_INT_TYPE(MACRO)                  \
65   MACRO(int8_t, int8_t, ffi_type_sint8)                  \
66   MACRO(int16_t, int16_t, ffi_type_sint16)               \
67   MACRO(int32_t, int32_t, ffi_type_sint32)               \
68   MACRO(uint8_t, uint8_t, ffi_type_uint8)                \
69   MACRO(uint16_t, uint16_t, ffi_type_uint16)             \
70   MACRO(uint32_t, uint32_t, ffi_type_uint32)             \
71   MACRO(short, short, ffi_type_sint16)                   \
72   MACRO(unsigned_short, unsigned short, ffi_type_uint16) \
73   MACRO(int, int, ffi_type_sint32)                       \
74   MACRO(unsigned_int, unsigned int, ffi_type_uint32)
75 
76 #define CTYPES_FOR_EACH_WRAPPED_INT_TYPE(MACRO)                  \
77   MACRO(int64_t, int64_t, ffi_type_sint64)                       \
78   MACRO(uint64_t, uint64_t, ffi_type_uint64)                     \
79   MACRO(long, long, CTYPES_FFI_LONG)                             \
80   MACRO(unsigned_long, unsigned long, CTYPES_FFI_ULONG)          \
81   MACRO(long_long, long long, ffi_type_sint64)                   \
82   MACRO(unsigned_long_long, unsigned long long, ffi_type_uint64) \
83   MACRO(size_t, size_t, CTYPES_FFI_SIZE_T)                       \
84   MACRO(ssize_t, CTYPES_SSIZE_T, CTYPES_FFI_SSIZE_T)             \
85   MACRO(off_t, off_t, CTYPES_FFI_OFF_T)                          \
86   MACRO(intptr_t, intptr_t, CTYPES_FFI_INTPTR_T)                 \
87   MACRO(uintptr_t, uintptr_t, CTYPES_FFI_UINTPTR_T)
88 
89 #define CTYPES_FOR_EACH_FLOAT_TYPE(MACRO)   \
90   MACRO(float32_t, float, ffi_type_float)   \
91   MACRO(float64_t, double, ffi_type_double) \
92   MACRO(float, float, ffi_type_float)       \
93   MACRO(double, double, ffi_type_double)
94 
95 #define CTYPES_FOR_EACH_CHAR_TYPE(MACRO)          \
96   MACRO(char, char, ffi_type_uint8)               \
97   MACRO(signed_char, signed char, ffi_type_sint8) \
98   MACRO(unsigned_char, unsigned char, ffi_type_uint8)
99 
100 #define CTYPES_FOR_EACH_CHAR16_TYPE(MACRO) \
101   MACRO(char16_t, char16_t, ffi_type_uint16)
102 
103 #define CTYPES_FOR_EACH_TYPE(MACRO)       \
104   CTYPES_FOR_EACH_BOOL_TYPE(MACRO)        \
105   CTYPES_FOR_EACH_INT_TYPE(MACRO)         \
106   CTYPES_FOR_EACH_WRAPPED_INT_TYPE(MACRO) \
107   CTYPES_FOR_EACH_FLOAT_TYPE(MACRO)       \
108   CTYPES_FOR_EACH_CHAR_TYPE(MACRO)        \
109   CTYPES_FOR_EACH_CHAR16_TYPE(MACRO)
110 
111 #endif /* ctypes_typedefs_h */
112