Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
util_internal.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2014, Wind River Systems, Inc.
3 * Copyright (c) 2020, Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
15#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_
16#define ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_
17
18#include "util_loops.h"
19
20/* IS_ENABLED() helpers */
21
22/* This is called from IS_ENABLED(), and sticks on a "_XXXX" prefix,
23 * it will now be "_XXXX1" if config_macro is "1", or just "_XXXX" if it's
24 * undefined.
25 * ENABLED: Z_IS_ENABLED2(_XXXX1)
26 * DISABLED Z_IS_ENABLED2(_XXXX)
27 */
28#define Z_IS_ENABLED1(config_macro) Z_IS_ENABLED2(_XXXX##config_macro)
29
30/* Here's the core trick, we map "_XXXX1" to "_YYYY," (i.e. a string
31 * with a trailing comma), so it has the effect of making this a
32 * two-argument tuple to the preprocessor only in the case where the
33 * value is defined to "1"
34 * ENABLED: _YYYY, <--- note comma!
35 * DISABLED: _XXXX
36 */
37#define _XXXX1 _YYYY,
38
39/* Then we append an extra argument to fool the gcc preprocessor into
40 * accepting it as a varargs macro.
41 * arg1 arg2 arg3
42 * ENABLED: Z_IS_ENABLED3(_YYYY, 1, 0)
43 * DISABLED Z_IS_ENABLED3(_XXXX 1, 0)
44 */
45#define Z_IS_ENABLED2(one_or_two_args) Z_IS_ENABLED3(one_or_two_args 1, 0)
46
47/* And our second argument is thus now cooked to be 1 in the case
48 * where the value is defined to 1, and 0 if not:
49 */
50#define Z_IS_ENABLED3(ignore_this, val, ...) val
51
52/* Used internally by COND_CODE_1 and COND_CODE_0. */
53#define Z_COND_CODE_1(_flag, _if_1_code, _else_code) \
54 __COND_CODE(_XXXX##_flag, _if_1_code, _else_code)
55#define Z_COND_CODE_0(_flag, _if_0_code, _else_code) \
56 __COND_CODE(_ZZZZ##_flag, _if_0_code, _else_code)
57#define _ZZZZ0 _YYYY,
58#define __COND_CODE(one_or_two_args, _if_code, _else_code) \
59 __GET_ARG2_DEBRACKET(one_or_two_args _if_code, _else_code)
60
61/* Gets second argument and removes brackets around that argument. It
62 * is expected that the parameter is provided in brackets/parentheses.
63 */
64#define __GET_ARG2_DEBRACKET(ignore_this, val, ...) __DEBRACKET val
65
66/* Used to remove brackets from around a single argument. */
67#define __DEBRACKET(...) __VA_ARGS__
68
69/* Used by IS_EMPTY() */
70/* reference: https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/ */
71#define Z_HAS_COMMA(...) \
72 NUM_VA_ARGS_LESS_1_IMPL(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, \
73 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
74 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
75 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
76#define Z_TRIGGER_PARENTHESIS_(...) ,
77#define Z_IS_EMPTY_(...) \
78 Z_IS_EMPTY__( \
79 Z_HAS_COMMA(__VA_ARGS__), \
80 Z_HAS_COMMA(Z_TRIGGER_PARENTHESIS_ __VA_ARGS__), \
81 Z_HAS_COMMA(__VA_ARGS__ (/*empty*/)), \
82 Z_HAS_COMMA(Z_TRIGGER_PARENTHESIS_ __VA_ARGS__ (/*empty*/)))
83#define Z_CAT5(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
84#define Z_IS_EMPTY__(_0, _1, _2, _3) \
85 Z_HAS_COMMA(Z_CAT5(Z_IS_EMPTY_CASE_, _0, _1, _2, _3))
86#define Z_IS_EMPTY_CASE_0001 ,
87
88/* Used by LIST_DROP_EMPTY() */
89/* Adding ',' after each element would add empty element at the end of
90 * list, which is hard to remove, so instead precede each element with ',',
91 * this way first element is empty, and this one is easy to drop.
92 */
93#define Z_LIST_ADD_ELEM(e) EMPTY, e
94#define Z_LIST_DROP_FIRST(...) GET_ARGS_LESS_N(1, __VA_ARGS__)
95#define Z_LIST_NO_EMPTIES(e) \
96 COND_CODE_1(IS_EMPTY(e), (), (Z_LIST_ADD_ELEM(e)))
97
98#define UTIL_CAT(a, ...) UTIL_PRIMITIVE_CAT(a, __VA_ARGS__)
99#define UTIL_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__
100#define UTIL_CHECK_N(x, n, ...) n
101#define UTIL_CHECK(...) UTIL_CHECK_N(__VA_ARGS__, 0,)
102#define UTIL_NOT(x) UTIL_CHECK(UTIL_PRIMITIVE_CAT(UTIL_NOT_, x))
103#define UTIL_NOT_0 ~, 1,
104#define UTIL_COMPL(b) UTIL_PRIMITIVE_CAT(UTIL_COMPL_, b)
105#define UTIL_COMPL_0 1
106#define UTIL_COMPL_1 0
107#define UTIL_BOOL(x) UTIL_COMPL(UTIL_NOT(x))
108
109#define UTIL_EVAL(...) __VA_ARGS__
110#define UTIL_EXPAND(...) __VA_ARGS__
111#define UTIL_REPEAT(...) UTIL_LISTIFY(__VA_ARGS__)
112
113/* Implementation details for NUM_VA_ARGS_LESS_1 */
114#define NUM_VA_ARGS_LESS_1_IMPL( \
115 _ignored, \
116 _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
117 _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
118 _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
119 _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
120 _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
121 _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
122 _61, _62, N, ...) N
123
124/* Used by MACRO_MAP_CAT */
125#define MACRO_MAP_CAT_(...) \
126 /* To make sure it works also for 2 arguments in total */ \
127 MACRO_MAP_CAT_N(NUM_VA_ARGS_LESS_1(__VA_ARGS__), __VA_ARGS__)
128#define MACRO_MAP_CAT_N_(N, ...) UTIL_CAT(MACRO_MC_, N)(__VA_ARGS__,)
129#define MACRO_MC_0(...)
130#define MACRO_MC_1(m, a, ...) m(a)
131#define MACRO_MC_2(m, a, ...) UTIL_CAT(m(a), MACRO_MC_1(m, __VA_ARGS__,))
132#define MACRO_MC_3(m, a, ...) UTIL_CAT(m(a), MACRO_MC_2(m, __VA_ARGS__,))
133#define MACRO_MC_4(m, a, ...) UTIL_CAT(m(a), MACRO_MC_3(m, __VA_ARGS__,))
134#define MACRO_MC_5(m, a, ...) UTIL_CAT(m(a), MACRO_MC_4(m, __VA_ARGS__,))
135#define MACRO_MC_6(m, a, ...) UTIL_CAT(m(a), MACRO_MC_5(m, __VA_ARGS__,))
136#define MACRO_MC_7(m, a, ...) UTIL_CAT(m(a), MACRO_MC_6(m, __VA_ARGS__,))
137#define MACRO_MC_8(m, a, ...) UTIL_CAT(m(a), MACRO_MC_7(m, __VA_ARGS__,))
138#define MACRO_MC_9(m, a, ...) UTIL_CAT(m(a), MACRO_MC_8(m, __VA_ARGS__,))
139#define MACRO_MC_10(m, a, ...) UTIL_CAT(m(a), MACRO_MC_9(m, __VA_ARGS__,))
140#define MACRO_MC_11(m, a, ...) UTIL_CAT(m(a), MACRO_MC_10(m, __VA_ARGS__,))
141#define MACRO_MC_12(m, a, ...) UTIL_CAT(m(a), MACRO_MC_11(m, __VA_ARGS__,))
142#define MACRO_MC_13(m, a, ...) UTIL_CAT(m(a), MACRO_MC_12(m, __VA_ARGS__,))
143#define MACRO_MC_14(m, a, ...) UTIL_CAT(m(a), MACRO_MC_13(m, __VA_ARGS__,))
144#define MACRO_MC_15(m, a, ...) UTIL_CAT(m(a), MACRO_MC_14(m, __VA_ARGS__,))
145
146#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_ */
Internals for looping macros.