cast: downgrade license from LGPL3+ to LGPLv2.1+
[ccan] / ccan / cast / cast.h
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_CAST_H
3 #define CCAN_CAST_H
4 #include "config.h"
5 #include <stdint.h>
6 #include <ccan/build_assert/build_assert.h>
7
8 /**
9  * cast_signed - cast a (const) char * to/from (const) signed/unsigned char *.
10  * @type: some char * variant.
11  * @expr: expression (of some char * variant) to cast.
12  *
13  * Some libraries insist on an unsigned char in various places; cast_signed
14  * makes sure (with suitable compiler) that the expression you are casting
15  * only differs in signed/unsigned, not in type or const-ness.
16  */
17 #define cast_signed(type, expr)                                         \
18         ((type)(expr)                                                   \
19          + BUILD_ASSERT_OR_ZERO(cast_sign_compatible(type, (expr))))
20
21 /**
22  * cast_const - remove a const qualifier from a pointer.
23  * @type: some pointer type.
24  * @expr: expression to cast.
25  *
26  * This ensures that you are only removing the const qualifier from an
27  * expression.  The expression must otherwise match @type.
28  *
29  * If @type is a pointer to a pointer, you must use cast_const2 (etc).
30  *
31  * Example:
32  *      // Dumb open-coded strstr variant.
33  *      static char *find_needle(const char *haystack)
34  *      {
35  *              size_t i;
36  *              for (i = 0; i < strlen(haystack); i++)
37  *              if (memcmp("needle", haystack+i, strlen("needle")) == 0)
38  *                      return cast_const(char *, haystack+i);
39  *              return NULL;
40  *      }
41  */
42 #define cast_const(type, expr)                                          \
43         ((type)((intptr_t)(expr)                                        \
44                 + BUILD_ASSERT_OR_ZERO(cast_const_compat1((expr), type))))
45
46 /**
47  * cast_const2 - remove a const qualifier from a pointer to a pointer.
48  * @type: some pointer to pointer type.
49  * @expr: expression to cast.
50  *
51  * This ensures that you are only removing the const qualifier from an
52  * expression.  The expression must otherwise match @type.
53  */
54 #define cast_const2(type, expr)                                         \
55         ((type)((intptr_t)(expr)                                        \
56                 + BUILD_ASSERT_OR_ZERO(cast_const_compat2((expr), type))))
57
58 /**
59  * cast_const3 - remove a const from a pointer to a pointer to a pointer..
60  * @type: some pointer to pointer to pointer type.
61  * @expr: expression to cast.
62  *
63  * This ensures that you are only removing the const qualifier from an
64  * expression.  The expression must otherwise match @type.
65  */
66 #define cast_const3(type, expr)                                         \
67         ((type)((intptr_t)(expr)                                        \
68                 + BUILD_ASSERT_OR_ZERO(cast_const_compat3((expr), type))))
69
70
71 /**
72  * cast_static - explicit mimic of implicit cast.
73  * @type: some type.
74  * @expr: expression to cast.
75  *
76  * This ensures that the cast is not to or from a pointer: it can only be
77  * an implicit cast, such as a pointer to a similar const pointer, or between
78  * integral types.
79  */
80 #if HAVE_COMPOUND_LITERALS
81 #define cast_static(type, expr)                 \
82         ((struct { type x; }){(expr)}.x)
83 #else
84 #define cast_static(type, expr)                 \
85         ((type)(expr))
86 #endif
87
88 /* Herein lies the gcc magic to evoke compile errors. */
89 #if HAVE_BUILTIN_CHOOSE_EXPR && HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
90 #define cast_sign_compatible(t, e) \
91   __builtin_choose_expr(                                                \
92           __builtin_types_compatible_p(__typeof__(t), char *) ||        \
93           __builtin_types_compatible_p(__typeof__(t), signed char *) || \
94           __builtin_types_compatible_p(__typeof__(t), unsigned char *), \
95           /* if type is not const qualified */                          \
96           __builtin_types_compatible_p(__typeof__(e), char *) ||        \
97           __builtin_types_compatible_p(__typeof__(e), signed char *) || \
98           __builtin_types_compatible_p(__typeof__(e), unsigned char *), \
99           /* and if it is... */                                         \
100           __builtin_types_compatible_p(__typeof__(e), const char *) ||  \
101           __builtin_types_compatible_p(__typeof__(e), const signed char *) || \
102           __builtin_types_compatible_p(__typeof__(e), const unsigned char *) ||\
103           __builtin_types_compatible_p(__typeof__(e), char *) ||        \
104           __builtin_types_compatible_p(__typeof__(e), signed char *) || \
105           __builtin_types_compatible_p(__typeof__(e), unsigned char *)  \
106           )
107
108 #define cast_const_strip1(expr)                 \
109         __typeof__(*(struct { int z; __typeof__(expr) x; }){0}.x)
110 #define cast_const_strip2(expr) \
111         __typeof__(**(struct { int z; __typeof__(expr) x; }){0}.x)
112 #define cast_const_strip3(expr) \
113         __typeof__(***(struct { int z; __typeof__(expr) x; }){0}.x)
114 #define cast_const_compat1(expr, type)                                  \
115         __builtin_types_compatible_p(cast_const_strip1(expr),           \
116                                      cast_const_strip1(type))
117 #define cast_const_compat2(expr, type)                                  \
118         __builtin_types_compatible_p(cast_const_strip2(expr),           \
119                                      cast_const_strip2(type))
120 #define cast_const_compat3(expr, type)                                  \
121         __builtin_types_compatible_p(cast_const_strip3(expr),           \
122                                      cast_const_strip3(type))
123 #else
124 #define cast_sign_compatible(type, expr)                \
125         (sizeof(*(type)0) == 1 && sizeof(*(expr)) == 1)
126 #define cast_const_compat1(expr, type)          (1)
127 #define cast_const_compat2(expr, type)          (1)
128 #define cast_const_compat3(expr, type)          (1)
129 #endif
130 #endif /* CCAN_CAST_H */