ic-macros  0.1.5
assert_type.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, Intercreate, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #include <stdint.h>
14 
16 
24 #define IC_IS_TYPE(type, expr) __builtin_types_compatible_p(type, __typeof__(expr))
25 
32 #define IC_ASSERT_TYPE(type, expr) \
33  IC_BUILD_ASSERT(IC_IS_TYPE(type, expr), #expr " is not of type " #type)
34 
42 #define IC_REQUIRE_TYPE(type, expr) \
43  ({ \
44  IC_REQUIRE_CONSTANT(expr); \
45  IC_ASSERT_TYPE(type, expr); \
46  expr; \
47  })
48 
49 #define _IC_IS_UINT(expr) \
50  (__builtin_types_compatible_p(uint8_t, __typeof__(expr)) \
51  || __builtin_types_compatible_p(unsigned char, __typeof__(expr)) \
52  || __builtin_types_compatible_p(uint16_t, __typeof__(expr)) \
53  || __builtin_types_compatible_p(unsigned short int, __typeof__(expr)) \
54  || __builtin_types_compatible_p(uint32_t, __typeof__(expr)) \
55  || __builtin_types_compatible_p(unsigned int, __typeof__(expr)) \
56  || __builtin_types_compatible_p(unsigned long int, __typeof__(expr)) \
57  || __builtin_types_compatible_p(uint64_t, __typeof__(expr)) \
58  || __builtin_types_compatible_p(unsigned long long int, __typeof__(expr)))
59 
67 #define IC_IS_UINT(expr) \
68  ({ \
69  IC_ASSERT_CONSTANT(expr); \
70  _IC_IS_UINT(expr); \
71  })
72 
79 #define IC_Z_REQUIRE_UINT(expr) \
80  ({ \
81  __auto_type const _ic_z_is_uint_expr_ = (expr); \
82  IC_BUILD_ASSERT(_IC_IS_UINT(expr)); \
83  _ic_z_is_uint_expr_; \
84  })
85 
93 #define IC_REQUIRE_UINT(expr) \
94  ({ \
95  IC_BUILD_ASSERT(IC_IS_UINT(expr), #expr " is not an unsigned integer type"); \
96  expr; \
97  })
98 
99 #define _IC_IS_INT(expr) \
100  (__builtin_types_compatible_p(int8_t, __typeof__(expr)) \
101  || __builtin_types_compatible_p(signed char, __typeof__(expr)) \
102  || __builtin_types_compatible_p(int16_t, __typeof__(expr)) \
103  || __builtin_types_compatible_p(signed short int, __typeof__(expr)) \
104  || __builtin_types_compatible_p(int32_t, __typeof__(expr)) \
105  || __builtin_types_compatible_p(int, __typeof__(expr)) \
106  || __builtin_types_compatible_p(signed long int, __typeof__(expr)) \
107  || __builtin_types_compatible_p(int64_t, __typeof__(expr)) \
108  || __builtin_types_compatible_p(signed long long int, __typeof__(expr)))
109 
117 #define IC_IS_INT(expr) \
118  ({ \
119  IC_ASSERT_CONSTANT(expr); \
120  _IC_IS_INT(expr); \
121  })
122 
130 #define IC_REQUIRE_INT(expr) \
131  ({ \
132  IC_BUILD_ASSERT(IC_IS_INT(expr), #expr " is not a signed integer type"); \
133  expr; \
134  })
135 
143 #define IC_IS_FP(expr) \
144  ({ \
145  IC_ASSERT_CONSTANT(expr); \
146  (__builtin_types_compatible_p(float, __typeof__(expr)) \
147  || __builtin_types_compatible_p(double, __typeof__(expr)) \
148  || __builtin_types_compatible_p(long double, __typeof__(expr))); \
149  })
150 
158 #define IC_REQUIRE_FP(expr) \
159  ({ \
160  IC_BUILD_ASSERT(IC_IS_FP(IC_REQUIRE_CONSTANT(expr))); \
161  expr; \
162  })
163 
164 #ifdef __cplusplus
165 }
166 #endif