14 .TH control 3 "23 April 2023" "Straylight/Edgeware" "mLib utilities library"
17 control \- control structure metaprogramming
36 .B "#include <mLib/control.h>"
38 .BI MC_BEFORE( tag ", " stmts ") " body
39 .BI MC_AFTER( tag ", " stmts ") " body
40 .BI MC_WRAP( tag ", " before_stmt ", " onend_stmt ", " onbreak_stmt ") " body
41 .BI MC_FINALLY( tag ", " cleanup ") " body
42 .BI MC_DOWHILE( tag ", " cond ") " body
43 .BI MC_DECL( tag ", " decl ") " body
44 .BI MC_LOOPELSE( tag ", " head ") " loop_body " \fR[\fBelse " else_body \fR]
45 .BI MC_LOOPBETWEEN( tag ", " setup ", " cond ", " step ") " loop_body " \fR[\fBelse " else_body \fR]
47 .BI MC_TARGET( tag ", " stmt ") " body
48 .BI MC_GOTARGET( tag );
49 .BI MC_ALLOWELSE( tag ") " main_body " \fR[\fBelse " else_body \fR]
60 defines a number of macros which are useful
61 when defining new control structures for C.
62 They are inspired by Simon Tatham's article
63 .IR "Metaprogramming custom control structures in C",
64 though these macros differ from Tatham's in a few respects.
67 Each of these macros takes a
72 is lexically like an identifier,
73 except that it may begin with a digit,
74 so, for example, plain integers are acceptable tags.
75 Each use of an action macro by a user-level macro
78 If you're writing a new prefix action macro
79 written in terms of these existing actions,
80 your macro should receive a
84 along with a distinctive component of its own,
85 down to any prefix actions that it calls;
88 from each layer should be separated by a pair of underscores.
90 Some of these macros work by wrapping a loop around the
93 This interferes with the way that `free'
100 we say that these statements are
109 if it doesn't appear lexically within a loop or
113 statement that is part of the
125 for (i = 0; i < n; i++)
126 if (interestingp(i)) break;
130 Some of these macros take special care
131 to give you control over what happens when a captured
134 Alas, proper handling of
136 doesn't seem possible.
143 arguments to these macros are never captured.
145 .SS "Prefix action macros"
147 macro is the simplest to understand. Executing
149 .BI MC_BEFORE( tag ", " stmt ") " body
151 has the same effect as executing
155 except that the whole thing is syntactically a single statement,
156 so, for example, it doesn't need to be enclosed in braces
161 does not capture free
169 .BI MC_AFTER( tag ", " stmt ") " body
173 the same effect as executing
177 Again, the whole thing is syntactically a single statement.
184 statements within the
190 abruptly ends execution of the
192 immediately transferring control to the
197 .BI MC_WRAP( tag ", " before ", " onend ", " onbreak ") " body
201 the same effect as executing
210 statement, then control abruptly continues with the
220 then control abruptly continues with the
223 but this behaviour is a bug and may be fixed in the future.
227 .BI MC_FINALLY( tag ", " cleanup ") " body
229 has the same effect as executing
239 before propagating the
241 to the enclosing context.
244 statement currently causes control to continue abruptly with
246 but this behaviour is a bug and may be fixed in the future.
249 code is textually duplicated,
250 so there'll be some code bloat if this is very complex.
251 If it arranges to have private long-term state
252 then the two copies will not share this state,
253 so probably don't do this.
257 .BI MC_DOWHILE( tag ", " cond ") " body
259 has exactly the same effect as
260 .BI "do " body " while (" cond ); \fR,
261 the only difference being that the
263 appears in tail position rather than sandwiched in the middle.
266 .BI MC_DECL( tag ", " decl ") " body
267 has the same effect as
268 .BI "{ " decl "; " body " }" \fR,
273 statements are captured.
276 statement will simply abruptly terminate execution of the
284 without leaving the scope of the
286 but these behaviours are bugs and may be fixed in the future.
290 macro makes use of the fact that a
292 statement can introduce a declaration
293 into its body's scope in C99 and C++;
294 the macro is not available in C89.
300 .BI MC_LOOPELSE( head ", " tag ") "
306 results in Python-like loop behaviour.
309 must be a valid loop head with one of the forms
312 .BI "while (" cond ")"
313 .BI "for (" decl "; " cond "; " next_expr ")"
314 .BI "MC_DOWHILE(" tag ", " cond ")"
317 The resulting loop executes the same as
325 If the loop ends abruptly, as a result of
327 then control is passed to the statement following the loop
329 However, if the loop completes naturally,
345 statements within the
353 .BI MC_LOOPBETWEEN( tag ", " setup ", " cond ", " step ") "
359 is similar to executing the
365 .BI "for (" setup "; " cond "; " step ") "
369 except that, once the
374 expression evaluated,
377 evaluated and determined to be nonzero,
380 (if any) is executed before re-entering the
382 This makes it a useful place to insert
383 any kind of interstitial material,
384 e.g., printing commas between list items.
385 Note that by the time the
388 the decision has already been made
389 that another iteration will be performed,
390 and, in particular, the
394 is therefore looking at the next item to be processed,
395 not the item that has just finished being processed.
398 is textually duplicated,
399 so there'll be some code bloat if this is very complex.
400 If it somehow manages to have private long-term state
401 (e.g., as a result of declaring static variables
402 inside GCC statement expressions)
403 then the two copies will not share this state,
404 so probably don't do this.
406 .SS "Lower-level machinery"
409 .BI MC_TARGET( tag ", " stmt ") " body
411 has exactly the same effect as simply executing
415 immediately transfers control to
417 with control continuing with the following statement,
428 This is most commonly useful in loops
429 in order to arrange the correct behaviour of a free
431 within the loop body.
432 See the example below,
433 which shows the definition
441 .BI MC_ALLOWELSE( tag ") "
447 has exactly the same effect as just
451 .BI MC_GOELSE( tag );
453 transfers control immediately to
456 control then naturally transfers to the following statement as usual.
461 statements in either of
469 works by secretly inserting an
471 statement head before the
473 so things will likely to wrong if
487 will be mis-associated;
494 the resulting program text is likely to provoke a compiler warning
499 it's relatively straightforward to define a macro like
503 .ta 4n 4n+\w'\fBMC_WRAP(tag##__body, 'u \n(.lu-\n(.iu-4n
504 #define MC_LOOPELSE(tag, head) \e
505 MC_TARGET(tag##__exit, { ; }) \e
506 MC_ALLOWELSE(tag##__else) \e
507 MC_AFTER(tag##__after, { MC_GOELSE(tag##__else); }) \e
509 MC_WRAP(tag##__body, { ; }, { ; }, \e
510 { MC_GOTARGET(tag##__exit); })
512 The main `trick' for these control-flow macros is
514 which wraps up a statement as an
517 .IR "statement head" ,
522 i.e., it must be completed by following it with a
527 .BI MC_ACT( stmt ") " body
529 has the same effect as simply executing
536 is a valid statement which does nothing,
539 is also a valid statement with the same effect as
541 The only way to cause
543 to be executed is to attach a label to it and transfer control using
548 .BI MC_LABEL( tag ") " body
550 has the same effect as
556 immediately transfers control to the
560 is syntactically an action,
561 i.e., it's wrapped in
565 here are scoped to the top-level source line,
568 in this macro package.
570 All of the control-flow macros in this package are mainly constructed from
575 sometimes with one or two other statement heads thrown into the mix.
580 .ta 4n 28n 30n \n(.lu-\n(.iu-4n
581 #define MC_AFTER(tag, stmt) \e
582 MC_GOTO(tag##__body) \e
583 MC_LABEL(tag##__end) \e
586 MC_GOTO(tag##__end) \e
587 MC_LABEL(tag##__body)
589 (The unusual layout is conventional,
590 to make the overall structure of the code clear
591 despite visual interference from the labels.)
597 Control enters at the start,
598 and is immediately transferred to the
606 completes, the loop restarts,
607 transferring control to
611 Since it is enclosed in
616 control transfers to the following statement.
619 Some macros cause free
623 statements to behave in unexpected ways.
625 It's rather hard to use
627 in practice without provoking
628 .RB `dangling- else '
631 The need for tagging is ugly,
632 and the restriction on having two
633 user-facing control-flow macros on the same line is objectionable.
634 The latter could be avoided
635 by using nonstandard features such as GCC's
638 but adopting that would do programmers a disservice
639 by introducing a hazard for those
640 trying to port code to other compilers which lack any such feature.
647 .IR "Metaprogramming custom control structures in C",
648 .BR "https://www.chiark.greenend.org.uk/~sgtatham/mp/" .
651 Mark Wooding, <mdw@distorted.org.uk>