Debian Codegraph @

debian/rust-vendor/ctor/src/macros/perform.rs

raw
1 //! A set of macros to perform chained macro invocations.
2
3 /// The top-level macro that invokes one sub-macro and renders that output.
4 #[macro_export]
5 #[doc(hidden)]
6 macro_rules! __perform {
7 ( $input:tt, $macro:path $( [ $($args:tt)* ] )? ) => {
8 $macro ! ( @entry next=$crate::__perform[[@complete]], input=$input $(, args=[$($args)*])? );
9 };
10 ( [@complete], ($($input:tt)*) ) => {
11 $($input)*
12 };
13 ( $($input:tt)* ) => {
14 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
15 };
16 }
17
18 /// Pass-through macro that stringifies the input for debugging.
19 #[macro_export]
20 #[doc(hidden)]
21 macro_rules! __debug {
22 ( @entry next=$next:path[$next_args:tt], input=$input:tt ) => {
23 const _: () = { stringify! $input ; };
24 $next ! ( $next_args, $input );
25 };
26
27 ( $($input:tt)* ) => {
28 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
29 };
30 }
31
32 /// Stringifies the input.
33 #[macro_export]
34 #[doc(hidden)]
35 macro_rules! __stringify {
36 ( @entry next=$next:path[$next_args:tt], input=$input:tt, args=[$(prefix=[$($prefix:tt)*])? $(suffix=[$($suffix:tt)*])?] ) => {
37 $next ! ( $next_args, ($($($prefix)*)? stringify! $input $($($suffix)*)? ) );
38 };
39
40 ( $($input:tt)* ) => {
41 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
42 };
43 }
44
45 /// Emits the arguments, ignoring the inputs.
46 #[macro_export]
47 #[doc(hidden)]
48 macro_rules! __emit {
49 ( @entry next=$next:path[$next_args:tt], input=$input:tt, args=[$($args:tt)*] ) => {
50 $next ! ( $next_args, ($($args)*) );
51 };
52
53 ( $($input:tt)* ) => {
54 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
55 };
56 }
57
58 /// Surrounds the input with the arguments.
59 #[macro_export]
60 #[doc(hidden)]
61 macro_rules! __surround {
62 ( @entry next=$next:path[$next_args:tt], input=($($input:tt)*),
63 args=[ $( prefix=[$($prefix:tt)*] )? $( suffix=[$($suffix:tt)*] )? ] ) => {
64 $next ! ( $next_args, ( $($($prefix)*)? $($input)* $($($suffix)*)? ) );
65 };
66
67 ( $($input:tt)* ) => {
68 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
69 };
70 }
71
72 /// Surrounds the input with the arguments.
73 #[macro_export]
74 #[doc(hidden)]
75 macro_rules! __brace {
76 ( @entry next=$next:path[$next_args:tt], input=($($input:tt)*), args=[ () ] ) => {
77 $next ! ( $next_args, ( ( $($input)* ) ) );
78 };
79
80 ( @entry next=$next:path[$next_args:tt], input=($($input:tt)*), args=[ [] ] ) => {
81 $next ! ( $next_args, ( [ $($input)* ] ) );
82 };
83
84 ( @entry next=$next:path[$next_args:tt], input=($($input:tt)*), args=[ {} ] ) => {
85 $next ! ( $next_args, ( { $($input)* } ) );
86 };
87
88 ( $($input:tt)* ) => {
89 const _: () = { compile_error!(concat!("Unexpected input to __brace: ", stringify!($($input)*))); };
90 };
91 }
92
93 /// Removes surrounding braces from the input.
94 #[macro_export]
95 #[doc(hidden)]
96 macro_rules! __unbrace {
97 ( @entry next=$next:path[$next_args:tt], input=(($($input:tt)*))) => {
98 $next ! ( $next_args, ( $($input)* ) );
99 };
100
101 ( @entry next=$next:path[$next_args:tt], input=([$($input:tt)*])) => {
102 $next ! ( $next_args, [ $($input)* ] );
103 };
104
105 ( @entry next=$next:path[$next_args:tt], input=({$($input:tt)*})) => {
106 $next ! ( $next_args, { $($input)* } );
107 };
108
109 ( $($input:tt)* ) => {
110 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
111 };
112 }
113
114 /// Passes the input through unchanged.
115 #[macro_export]
116 #[doc(hidden)]
117 macro_rules! __identity {
118 ( @entry next=$next:path[$next_args:tt], input=$input:tt ) => {
119 $next!($next_args, $input);
120 };
121 }
122
123 /// Sinks the input, ignoring any chained output
124 #[macro_export]
125 #[doc(hidden)]
126 macro_rules! __sink {
127 ( @entry next=$next:path[$next_args:tt], input=$input:tt ) => {};
128 }
129
130 /// Splits the input and processes it down multiple paths, in order. The next
131 /// macro will be called for each path.
132 #[macro_export]
133 #[doc(hidden)]
134 macro_rules! __split {
135 ( @entry next=$next:path[$next_args:tt], input=$input:tt, args=[
136 $($macro:path $([$($args:tt)*])?),*
137 $(,)?
138 ] ) => {
139 $(
140 $macro ! ( @entry next=$next[$next_args], input=$input $(, args=[$($args)*])? );
141 )*
142 };
143
144 ( $($input:tt)* ) => {
145 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
146 };
147 }
148
149 /// Runs the same input through multiple macros and passes the output of each to
150 /// the next macro concatenated.
151 #[macro_export]
152 #[doc(hidden)]
153 macro_rules! __parallel {
154 // Entry point, start with empty accumulator
155 ( @entry next=$next:path[$next_args:tt], input=$input:tt, args=[
156 $macro:path $([$($args:tt)*])? $(,)?
157 ] ) => {
158 compile_error!("__parallel: Specify at least two macros to avoid unnecessary overhead");
159 };
160
161 // Optimization for 2 items
162 ( @entry next=$next:path[$next_args:tt], input=$i:tt, args=[$m0:path $([$($m0_args:tt)*])?, $m1:path $([$($m1_args:tt)*])? $(,)?] ) => {
163 $m0 ! ( @entry
164 next=$crate::__parallel[[@continue2 [[$m1] $([$($m1_args)*])?] $i $next [$next_args]]],
165 input=$i
166 $(, args=[$($m0_args)*])?
167 );
168 };
169
170 ( [@continue2 [[$m1:path] $($m1_args:tt)?] $i:tt $next:path [$next_args:tt]], $accum:tt ) => {
171 $m1 ! ( @entry
172 next=$crate::__parallel[[@finish2 $accum $next [$next_args]]],
173 input=$i $(, args=$m1_args)?
174 );
175 };
176
177 ( [@finish2 ($($accum1:tt)*) $next:path [$next_args:tt]], ($($accum2:tt)*) ) => {
178 $next ! ( $next_args, ($($accum1)* $($accum2)*) );
179 };
180
181 // Entry point, start with empty accumulator
182 ( @entry next=$next:path[$next_args:tt], input=$input:tt, args=$args:tt ) => {
183 $crate::__parallel!( @process $input, $args, (), [$next[$next_args]] );
184 };
185
186 // Exit point, all parallel is done, emit accumulator to next macro
187 ( @process $input:tt, [], $accum:tt, [$next:path[$next_args:tt]] ) => {
188 $next ! ( $next_args, $accum );
189 };
190
191 // Continue point, call the next macro in the parallel chain
192 ( @process $input:tt, [$next:path $([$($args:tt)*])?, $($stack:tt)*], $accum:tt, $final:tt ) => {
193 $next!(
194 @entry next=$crate::__parallel[[@continue [$($stack)*] $input $accum $final]],
195 input=$input $(, args=[$($args)*])?
196 );
197 };
198
199 ( [@continue [$($stack:tt)*] $input:tt ($( $accum:tt )*) $final:tt], ($($output:tt)*) ) => {
200 $crate::__parallel!( @process $input, [$($stack)*], ($($accum)* $($output)*), $final);
201 };
202
203 ( $($input:tt)* ) => {
204 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
205 };
206 }
207
208 /// Processes input through a chain of macros, emitting the final output to the
209 /// next macro.
210 #[macro_export]
211 #[doc(hidden)]
212 macro_rules! __chain {
213 // Entry point
214 ( @entry next=$next:path[$next_args:tt], input=$input:tt, args= [$nextc:path $([$($argsc:tt)*])?, $($stack:tt)*] ) => {
215 $nextc!( @entry next=$crate::__chain[[@continue [$($stack)*] [$next[$next_args]]]], input=$input $(, args=[$($argsc)*])?);
216 };
217
218 // Continue point, call the next macro in the chain
219 ( @process $input:tt, [$next:path $([$($args:tt)*])?, $($stack:tt)*], $final:tt ) => {
220 $next!( @entry next=$crate::__chain[[@continue [$($stack)*] $final]], input=$input $(, args=[$($args)*])?);
221 };
222
223 // Return from macro call, complete
224 ( [@continue [] [$next:path[$next_args:tt]]], $input:tt ) => {
225 $next ! ( $next_args, $input );
226 };
227 ( [@continue [$next:path $([$($args:tt)*])?, $($stack:tt)*] $final:tt], $input:tt ) => {
228 $next!( @entry next=$crate::__chain[[@continue [$($stack)*] $final]], input=$input $(, args=[$($args)*])?);
229 };
230
231 ( $($input:tt)* ) => {
232 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
233 };
234 }
235
236 /// Separates the token trees of the input into multiple paths and runs them in parallel.
237 ///
238 /// If there are more inputs than macros, the remaining inputs are passed through.
239 #[macro_export]
240 #[doc(hidden)]
241 macro_rules! __separate {
242 // Optimization for 1 item (treat it like the second macro)
243 ( @entry next=$next:path[$next_args:tt], input=($i0:tt $($rest:tt)*), args=[$m0:path $([$($m0_args:tt)*])? $(,)?] ) => {
244 $m0 ! ( @entry
245 next=$crate::__separate[[@finish2 () [$($rest)*] $next [$next_args]]],
246 input=$i0
247 $(, args=[$($m0_args)*])?
248 );
249 };
250
251 // Optimization for 2 items
252 ( @entry next=$next:path[$next_args:tt], input=($i0:tt $i1:tt $($rest:tt)*), args=[$m0:path $([$($m0_args:tt)*])?, $m1:path $([$($m1_args:tt)*])? $(,)?] ) => {
253 $m0 ! ( @entry
254 next=$crate::__separate[[@continue2 [[$m1] $([$($m1_args)*])?], $i1, [$($rest)*] $next [$next_args]]],
255 input=$i0
256 $(, args=[$($m0_args)*])?
257 );
258 };
259
260 ( [@continue2 [[$m1:path] $($m1_args:tt)?], $i1:tt, $rest:tt $next:path [$next_args:tt]], $accum:tt ) => {
261 $m1 ! ( @entry
262 next=$crate::__separate[[@finish2 $accum $rest $next [$next_args]]],
263 input=$i1 $(, args=$m1_args)?
264 );
265 };
266
267 ( [@finish2 ($($accum1:tt)*) [$($rest:tt)*] $next:path [$next_args:tt]], ($($accum2:tt)*) ) => {
268 $next ! ( $next_args, ($($accum1)* $($accum2)* $($rest)*) );
269 };
270
271 // Entry point, start with empty accumulator
272 ( @entry next=$next:path[$next_args:tt], input=$input:tt, args=$args:tt ) => {
273 $crate::__separate!( @process $input, $args, (), [$next[$next_args]] );
274 };
275
276 // Exit point, all separate is done, emit accumulator to next macro
277 ( @process ($($input:tt)*), [], ($($accum:tt)*), [$next:path[$next_args:tt]] ) => {
278 $next ! ( $next_args, ($($accum)* $($input)*) );
279 };
280
281 // Continue point, call the next macro in the separate chain
282 ( @process ($input:tt $($input_rest:tt)*), [$next:path $([$($args:tt)*])?, $($stack:tt)*], $accum:tt, $final:tt ) => {
283 $next!(
284 @entry next=$crate::__separate[[@continue [$($stack)*] ($($input_rest)*) $accum $final]],
285 input=$input $(, args=[$($args)*])?
286 );
287 };
288
289 ( [@continue [$($stack:tt)*] $input:tt ($( $accum:tt )*) $final:tt], ($($output:tt)*) ) => {
290 $crate::__separate!( @process $input, [$($stack)*], ($($accum)* $($output)*), $final);
291 };
292
293 ( $($input:tt)* ) => {
294 const _: () = { compile_error!(concat!("Unexpected input to __separate: ", stringify!($($input)*))); };
295 };
296 }
297
298 /// Pass the first tt in the input to a macro, then write both the input and
299 /// output to the next macro. Any additional input is passed through after.
300 #[macro_export]
301 #[doc(hidden)]
302 macro_rules! __expand {
303 ( @entry next=$next:path[$next_args:tt], input=($first:tt $($rest:tt)*), args=[$macro:path $([$($args:tt)*])? $(,)?] ) => {
304 $macro ! ( @entry next=$crate::__expand[[@continue [$next[$next_args]] ($first) ($($rest)*)]], input=$first $(, args=[$($args)*])? );
305 };
306
307 ( [@continue [$next:path[$next_args:tt]] ($($input:tt)*) ($($rest:tt)*)], ($($output:tt)*) ) => {
308 $next ! ( $next_args, ($($input)* $($output)* $($rest)*) );
309 };
310
311 ( $($input:tt)* ) => {
312 const _: () = { compile_error!(concat!("Unexpected input to __expand: ", stringify!($($input)*))); };
313 };
314 }
315
316 /// Zip up multiple arrays into a single array of tuples. The arrays must all be the same length.
317 #[macro_export]
318 #[doc(hidden)]
319 macro_rules! __zip {
320 ( @entry next=$next:path[$next_args:tt], input=$input:tt ) => {
321 $crate::__zip!( @process accum=(), input=$input, next=[$next[$next_args]] );
322 };
323
324 ( @process accum=$accum:tt, input=($(())*), next=[$next:path[$next_args:tt]] ) => {
325 $next ! ( $next_args, $accum );
326 };
327
328 ( @process accum=($($accum:tt)*), input=(
329 $( ($first:tt $($inner:tt)*) )*
330 ), next=$next:tt ) => {
331 $crate::__zip!( @process accum=($($accum)* ($($first)*)), input=($(($($inner)*))*), next=$next );
332 };
333
334 ( $($input:tt)* ) => {
335 const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); };
336 };
337 }
338
339 /// Re-arrange items from the input into a new order.
340 #[macro_export]
341 #[doc(hidden)]
342 macro_rules! __pick {
343 // Lower-recursion path for single items
344 ( @entry next=$next:path[$next_args:tt], input=($i0:tt $($rest:tt)*), args=[0]) => {
345 $next ! ( $next_args, ($i0) );
346 };
347 ( @entry next=$next:path[$next_args:tt], input=($i0:tt $i1:tt $($rest:tt)*), args=[1]) => {
348 $next ! ( $next_args, ($i1) );
349 };
350 ( @entry next=$next:path[$next_args:tt], input=($i0:tt $i1:tt $i2:tt $($rest:tt)*), args=[2]) => {
351 $next ! ( $next_args, ($i2) );
352 };
353
354 ( @entry next=$next:path[$next_args:tt], input=$input:tt, args=$args:tt ) => {
355 $crate::__pick!( @process $input, $input, (), $args, [$next[$next_args]] );
356 };
357
358 ( @process $input:tt, $input_:tt, $accum:tt, [], [$next:path[$next_args:tt]] ) => {
359 $next ! ( $next_args, $accum );
360 };
361
362 ( @process ($i0:tt $($rest:tt)*), $input:tt, ($($accum:tt)*), [0 $($args:tt)*], $next:tt) => {
363 $crate::__pick!( @process $input, $input, ($($accum)* $i0), [$($args)*], $next );
364 };
365 ( @process ($i0:tt $i1:tt $($rest:tt)*), $input:tt, ($($accum:tt)*), [1 $($args:tt)*], $next:tt) => {
366 $crate::__pick!( @process $input, $input, ($($accum)* $i1), [$($args)*], $next );
367 };
368 ( @process ($i0:tt $i1:tt $i2:tt $($rest:tt)*), $input:tt, ($($accum:tt)*), [2 $($args:tt)*], $next:tt) => {
369 $crate::__pick!( @process $input, $input, ($($accum)* $i2), [$($args)*], $next );
370 };
371 ( @process ($i0:tt $i1:tt $i2:tt $i3:tt $($rest:tt)*), $input:tt, ($($accum:tt)*), [3 $($args:tt)*], $next:tt) => {
372 $crate::__pick!( @process $input, $input, ($($accum)* $i3), [$($args)*], $next );
373 };
374 ( @process ($i0:tt $i1:tt $i2:tt $i3:tt $i4:tt $($rest:tt)*), $input:tt, ($($accum:tt)*), [4 $($args:tt)*], $next:tt) => {
375 $crate::__pick!( @process $input, $input, ($($accum)* $i4), [$($args)*], $next );
376 };
377 ( @process ($i0:tt $i1:tt $i2:tt $i3:tt $i4:tt $i5:tt $($rest:tt)*), $input:tt, ($($accum:tt)*), [5 $($args:tt)*], $next:tt) => {
378 $crate::__pick!( @process $input, $input, ($($accum)* $i5), [$($args)*], $next );
379 };
380 }
381
382 /// Calls a macro for each tokentree in the input and accumulates the results.
383 #[macro_export]
384 #[doc(hidden)]
385 macro_rules! __for_each {
386 // Entry, empty
387 ( @entry next=$next:path[$next_args:tt], input=(), args=[$macro_name:path $([$($args:tt)*])?] ) => {
388 $next ! ( $next_args, () );
389 };
390 // Entry, dispatch first item
391 ( @entry next=$next:path[$next_args:tt], input=($input:tt $($rest:tt)*), args=[$macro_name:path $([$($macro_args:tt)*])?] ) => {
392 $macro_name ! ( @entry next=$crate::__for_each[
393 [@continue (), ($($rest)*), [$macro_name $( [$($macro_args)*] )?], [$next[$next_args]]]
394 ], input=($input) $(, args=[$($macro_args)*])? );
395 };
396 // Continue, still more
397 ( [@continue ($($accum:tt)*), ($input:tt $($rest:tt)*), [$macro_name:path $( [$($macro_args:tt)*] )?], $next:tt], ($($output:tt)*) ) => {
398 $macro_name ! ( @entry next=$crate::__for_each[
399 [@continue ($($accum)* $($output)*), ($($rest)*), [$macro_name $( [$($macro_args)*] )?], $next]
400 ], input=($input) $(, args=[$($macro_args)*])? );
401 };
402 // Continue, done
403 ( [@continue ($($accum:tt)*), (), $macro:tt, [$next:path[$next_args:tt]]], ($($output:tt)*) ) => {
404 $next ! ( $next_args, ($($accum)* $($output)*) );
405 };
406 ( $($input:tt)* ) => {
407 const _: () = { compile_error!(concat!("Unexpected input for __for_each: ", stringify!($($input)*))); };
408 };
409 }
410
411 /// Calls a dynamic macro with the given input and arguments.
412 #[macro_export]
413 #[doc(hidden)]
414 macro_rules! __call {
415 ( @entry next=$next:path[$next_args:tt], input=($($input:tt)*), args=[$macro_name:path $([$($args:tt)*])?] ) => {
416 $macro_name ! ( @entry next=$next[$next_args], input=($($input)*) $(, args=[$($args)*])? );
417 };
418 }
419
420 /// Park the current state of the chain so it can be resumed at a lower stack depth.
421 #[macro_export]
422 #[doc(hidden)]
423 macro_rules! __park {
424 ( @entry next=$next:path[$next_args:tt], input=$input:tt, args=[$macro_name:ident] ) => {
425 macro_rules! $macro_name {
426 () => {
427 $next!($next_args, $input);
428 };
429 }
430 };
431 }