Debian Codegraph @

libdrgn/pp.h

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 /**
5 * @file
6 *
7 * Preprocessor utilities.
8 *
9 * See @ref Preprocessor.
10 */
11
12 #ifndef DRGN_PP_H
13 #define DRGN_PP_H
14
15 /**
16 * @ingroup Internals
17 *
18 * @defgroup Preprocessor Preprocessor
19 *
20 * Preprocessor metaprogramming
21 *
22 * This provides several macros that can be used for preprocessor
23 * metaprogramming. It is inspired by the [Boost Preprocessing
24 * library](https://www.boost.org/doc/libs/release/libs/preprocessor/doc/index.html).
25 *
26 * @{
27 */
28
29 /**
30 * Get the number of variadic arguments.
31 *
32 * ```
33 * PP_NARGS(a, b, c) // Expands to 3
34 * #define ARGS x, y
35 * PP_NARGS(ARGS) // Expands to 2
36 * ```
37 *
38 * An empty argument list is considered to have 0 arguments.
39 *
40 * ```
41 * PP_NARGS() // Expands to 0.
42 * ```
43 *
44 * @remark This depends on the `, ##__VA_ARGS__` GNU C extension. `__VA_OPT__`
45 * could be used instead, but it is only supported since GCC 8 (released in
46 * 2018) and Clang 12 (released in 2021).
47 *
48 * @hideinitializer
49 */
50 #define PP_NARGS(...) PP_NARGS_I(__VA_ARGS__)
51 /** @cond */
52 // The extra layer of indirection ensures that arguments are expanded.
53 #define PP_NARGS_I(...) PP_NARGS_II(, ##__VA_ARGS__, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
54 #define PP_NARGS_II(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, N, ...) N
55 /** @endcond */
56
57 /**
58 * Overload a macro based on the number of arguments.
59 *
60 * This expands to @p prefix concatenated with the number of arguments (as
61 * determined by @ref PP_NARGS()). Use it like so:
62 *
63 * ```
64 * #define DEFINE_ARRAY(...) PP_OVERLOAD(DEFINE_ARRAY_I, __VA_ARGS__)(__VA_ARGS__)
65 * #define DEFINE_ARRAY_I2(name, type) DEFINE_ARRAY_I3(a, b, DEFAULT_ARRAY_SIZE)
66 * #define DEFINE_ARRAY_I3(name, type, size) type name[size]
67 * #define DEFAULT_ARRAY_SIZE 5
68 *
69 * DEFINE_ARRAY(int, several); // Expands to int several[5];
70 * DEFINE_ARRAY(int, couple, 2); // Expands to int couple[2];
71 * ```
72 *
73 * @hideinitializer
74 */
75 #define PP_OVERLOAD(prefix, ...) PP_OVERLOAD_CAT_I(prefix, PP_NARGS(__VA_ARGS__))
76 /** @cond */
77 #define PP_OVERLOAD_CAT_I(a, b) PP_OVERLOAD_CAT_II(a, b)
78 #define PP_OVERLOAD_CAT_II(a, b) a##b
79 /** @endcond */
80
81 /**
82 * Expand and concatenate arguments.
83 *
84 * This expands each argument and then joins them with the `##` operator.
85 * `PP_CAT` takes two arguments, `PP_CAT3` takes three, `PP_CAT4` takes four,
86 * etc.
87 *
88 * ```
89 * #define a foo
90 * #define b bar
91 * PP_CAT(a, b) // Expands to foobar
92 * ```
93 *
94 * Intermediate results are not expanded:
95 * ```
96 * #define HELLO oops
97 * PP_CAT3(HELL, O, WORLD) // Expands to HELLOWORLD, _not_ oopsWORLD
98 * ```
99 *
100 * All possible intermediate results must be valid preprocessing tokens:
101 * ```
102 * PP_CAT3(1e, +, 3) // Undefined because +3 is not a valid preprocessing token
103 * ```
104 *
105 * @hideinitializer
106 */
107 #define PP_CAT(_0, _1) PP_CAT_I2(_0, _1)
108 #define PP_CAT3(_0, _1, _2) PP_CAT_I3(_0, _1, _2)
109 #define PP_CAT4(_0, _1, _2, _3) PP_CAT_I4(_0, _1, _2, _3)
110 #define PP_CAT5(_0, _1, _2, _3, _4) PP_CAT_I5(_0, _1, _2, _3, _4)
111 #define PP_CAT6(_0, _1, _2, _3, _4, _5) PP_CAT_I6(_0, _1, _2, _3, _4, _5)
112 #define PP_CAT7(_0, _1, _2, _3, _4, _5, _6) PP_CAT_I7(_0, _1, _2, _3, _4, _5, _6)
113 #define PP_CAT8(_0, _1, _2, _3, _4, _5, _6, _7) PP_CAT_I8(_0, _1, _2, _3, _4, _5, _6, _7)
114 /** @cond */
115 #define PP_CAT_I2(_0, _1) _0##_1
116 #define PP_CAT_I3(_0, _1, _2) _0##_1##_2
117 #define PP_CAT_I4(_0, _1, _2, _3) _0##_1##_2##_3
118 #define PP_CAT_I5(_0, _1, _2, _3, _4) _0##_1##_2##_3##_4
119 #define PP_CAT_I6(_0, _1, _2, _3, _4, _5) _0##_1##_2##_3##_4##_5
120 #define PP_CAT_I7(_0, _1, _2, _3, _4, _5, _6) _0##_1##_2##_3##_4##_5##_6
121 #define PP_CAT_I8(_0, _1, _2, _3, _4, _5, _6, _7) _0##_1##_2##_3##_4##_5##_6##_7
122 /** @endcond */
123
124 /**
125 * Call a macro on each variable argument.
126 *
127 * ```
128 * #define add_string(arg, x) x arg
129 * PP_MAP(add_string, "\n", "abc", "def") // Expands to "abc" "\n" "def" "\n"
130 * ```
131 *
132 * @param[in] func Macro taking @p arg and the next variable argument.
133 * @param[in] arg First argument to pass to @p func.
134 *
135 * @hideinitializer
136 */
137 #define PP_MAP(func, arg, ...) PP_OVERLOAD(PP_MAP_I, __VA_ARGS__)(func, arg, __VA_ARGS__)
138 /** @cond */
139 #define PP_MAP_I64(func, arg, x, ...) func(arg, x) PP_MAP_I63(func, arg, __VA_ARGS__)
140 #define PP_MAP_I63(func, arg, x, ...) func(arg, x) PP_MAP_I62(func, arg, __VA_ARGS__)
141 #define PP_MAP_I62(func, arg, x, ...) func(arg, x) PP_MAP_I61(func, arg, __VA_ARGS__)
142 #define PP_MAP_I61(func, arg, x, ...) func(arg, x) PP_MAP_I60(func, arg, __VA_ARGS__)
143 #define PP_MAP_I60(func, arg, x, ...) func(arg, x) PP_MAP_I59(func, arg, __VA_ARGS__)
144 #define PP_MAP_I59(func, arg, x, ...) func(arg, x) PP_MAP_I58(func, arg, __VA_ARGS__)
145 #define PP_MAP_I58(func, arg, x, ...) func(arg, x) PP_MAP_I57(func, arg, __VA_ARGS__)
146 #define PP_MAP_I57(func, arg, x, ...) func(arg, x) PP_MAP_I56(func, arg, __VA_ARGS__)
147 #define PP_MAP_I56(func, arg, x, ...) func(arg, x) PP_MAP_I55(func, arg, __VA_ARGS__)
148 #define PP_MAP_I55(func, arg, x, ...) func(arg, x) PP_MAP_I54(func, arg, __VA_ARGS__)
149 #define PP_MAP_I54(func, arg, x, ...) func(arg, x) PP_MAP_I53(func, arg, __VA_ARGS__)
150 #define PP_MAP_I53(func, arg, x, ...) func(arg, x) PP_MAP_I52(func, arg, __VA_ARGS__)
151 #define PP_MAP_I52(func, arg, x, ...) func(arg, x) PP_MAP_I51(func, arg, __VA_ARGS__)
152 #define PP_MAP_I51(func, arg, x, ...) func(arg, x) PP_MAP_I50(func, arg, __VA_ARGS__)
153 #define PP_MAP_I50(func, arg, x, ...) func(arg, x) PP_MAP_I49(func, arg, __VA_ARGS__)
154 #define PP_MAP_I49(func, arg, x, ...) func(arg, x) PP_MAP_I48(func, arg, __VA_ARGS__)
155 #define PP_MAP_I48(func, arg, x, ...) func(arg, x) PP_MAP_I47(func, arg, __VA_ARGS__)
156 #define PP_MAP_I47(func, arg, x, ...) func(arg, x) PP_MAP_I46(func, arg, __VA_ARGS__)
157 #define PP_MAP_I46(func, arg, x, ...) func(arg, x) PP_MAP_I45(func, arg, __VA_ARGS__)
158 #define PP_MAP_I45(func, arg, x, ...) func(arg, x) PP_MAP_I44(func, arg, __VA_ARGS__)
159 #define PP_MAP_I44(func, arg, x, ...) func(arg, x) PP_MAP_I43(func, arg, __VA_ARGS__)
160 #define PP_MAP_I43(func, arg, x, ...) func(arg, x) PP_MAP_I42(func, arg, __VA_ARGS__)
161 #define PP_MAP_I42(func, arg, x, ...) func(arg, x) PP_MAP_I41(func, arg, __VA_ARGS__)
162 #define PP_MAP_I41(func, arg, x, ...) func(arg, x) PP_MAP_I40(func, arg, __VA_ARGS__)
163 #define PP_MAP_I40(func, arg, x, ...) func(arg, x) PP_MAP_I39(func, arg, __VA_ARGS__)
164 #define PP_MAP_I39(func, arg, x, ...) func(arg, x) PP_MAP_I38(func, arg, __VA_ARGS__)
165 #define PP_MAP_I38(func, arg, x, ...) func(arg, x) PP_MAP_I37(func, arg, __VA_ARGS__)
166 #define PP_MAP_I37(func, arg, x, ...) func(arg, x) PP_MAP_I36(func, arg, __VA_ARGS__)
167 #define PP_MAP_I36(func, arg, x, ...) func(arg, x) PP_MAP_I35(func, arg, __VA_ARGS__)
168 #define PP_MAP_I35(func, arg, x, ...) func(arg, x) PP_MAP_I34(func, arg, __VA_ARGS__)
169 #define PP_MAP_I34(func, arg, x, ...) func(arg, x) PP_MAP_I33(func, arg, __VA_ARGS__)
170 #define PP_MAP_I33(func, arg, x, ...) func(arg, x) PP_MAP_I32(func, arg, __VA_ARGS__)
171 #define PP_MAP_I32(func, arg, x, ...) func(arg, x) PP_MAP_I31(func, arg, __VA_ARGS__)
172 #define PP_MAP_I31(func, arg, x, ...) func(arg, x) PP_MAP_I30(func, arg, __VA_ARGS__)
173 #define PP_MAP_I30(func, arg, x, ...) func(arg, x) PP_MAP_I29(func, arg, __VA_ARGS__)
174 #define PP_MAP_I29(func, arg, x, ...) func(arg, x) PP_MAP_I28(func, arg, __VA_ARGS__)
175 #define PP_MAP_I28(func, arg, x, ...) func(arg, x) PP_MAP_I27(func, arg, __VA_ARGS__)
176 #define PP_MAP_I27(func, arg, x, ...) func(arg, x) PP_MAP_I26(func, arg, __VA_ARGS__)
177 #define PP_MAP_I26(func, arg, x, ...) func(arg, x) PP_MAP_I25(func, arg, __VA_ARGS__)
178 #define PP_MAP_I25(func, arg, x, ...) func(arg, x) PP_MAP_I24(func, arg, __VA_ARGS__)
179 #define PP_MAP_I24(func, arg, x, ...) func(arg, x) PP_MAP_I23(func, arg, __VA_ARGS__)
180 #define PP_MAP_I23(func, arg, x, ...) func(arg, x) PP_MAP_I22(func, arg, __VA_ARGS__)
181 #define PP_MAP_I22(func, arg, x, ...) func(arg, x) PP_MAP_I21(func, arg, __VA_ARGS__)
182 #define PP_MAP_I21(func, arg, x, ...) func(arg, x) PP_MAP_I20(func, arg, __VA_ARGS__)
183 #define PP_MAP_I20(func, arg, x, ...) func(arg, x) PP_MAP_I19(func, arg, __VA_ARGS__)
184 #define PP_MAP_I19(func, arg, x, ...) func(arg, x) PP_MAP_I18(func, arg, __VA_ARGS__)
185 #define PP_MAP_I18(func, arg, x, ...) func(arg, x) PP_MAP_I17(func, arg, __VA_ARGS__)
186 #define PP_MAP_I17(func, arg, x, ...) func(arg, x) PP_MAP_I16(func, arg, __VA_ARGS__)
187 #define PP_MAP_I16(func, arg, x, ...) func(arg, x) PP_MAP_I15(func, arg, __VA_ARGS__)
188 #define PP_MAP_I15(func, arg, x, ...) func(arg, x) PP_MAP_I14(func, arg, __VA_ARGS__)
189 #define PP_MAP_I14(func, arg, x, ...) func(arg, x) PP_MAP_I13(func, arg, __VA_ARGS__)
190 #define PP_MAP_I13(func, arg, x, ...) func(arg, x) PP_MAP_I12(func, arg, __VA_ARGS__)
191 #define PP_MAP_I12(func, arg, x, ...) func(arg, x) PP_MAP_I11(func, arg, __VA_ARGS__)
192 #define PP_MAP_I11(func, arg, x, ...) func(arg, x) PP_MAP_I10(func, arg, __VA_ARGS__)
193 #define PP_MAP_I10(func, arg, x, ...) func(arg, x) PP_MAP_I9(func, arg, __VA_ARGS__)
194 #define PP_MAP_I9(func, arg, x, ...) func(arg, x) PP_MAP_I8(func, arg, __VA_ARGS__)
195 #define PP_MAP_I8(func, arg, x, ...) func(arg, x) PP_MAP_I7(func, arg, __VA_ARGS__)
196 #define PP_MAP_I7(func, arg, x, ...) func(arg, x) PP_MAP_I6(func, arg, __VA_ARGS__)
197 #define PP_MAP_I6(func, arg, x, ...) func(arg, x) PP_MAP_I5(func, arg, __VA_ARGS__)
198 #define PP_MAP_I5(func, arg, x, ...) func(arg, x) PP_MAP_I4(func, arg, __VA_ARGS__)
199 #define PP_MAP_I4(func, arg, x, ...) func(arg, x) PP_MAP_I3(func, arg, __VA_ARGS__)
200 #define PP_MAP_I3(func, arg, x, ...) func(arg, x) PP_MAP_I2(func, arg, __VA_ARGS__)
201 #define PP_MAP_I2(func, arg, x, ...) func(arg, x) PP_MAP_I1(func, arg, __VA_ARGS__)
202 #define PP_MAP_I1(func, arg, x) func(arg, x)
203 #define PP_MAP_I0(func, arg, x)
204 /** @endcond */
205
206 /**
207 * Create a unique name.
208 *
209 * This can be used to avoid name collisions and shadowing in macros that define
210 * local variables.
211 *
212 * ```
213 * #define SWAP(a, b) SWAP_I(a, b, PP_UNIQUE(tmp))
214 * #define SWAP_I(a, b, tmp) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
215 * ```
216 *
217 * @param[in] prefix Prefix for unique name. This makes the created name more
218 * recognizable in compiler diagnostics and debuggers. This is not expanded.
219 *
220 * @hideinitializer
221 */
222 #define PP_UNIQUE(prefix) PP_CAT(prefix##__PP_UNIQUE_, __COUNTER__)
223
224 /** @} */
225
226 #endif /* DRGN_PP_H */