cast.h 4.7 KB

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