]> git.ipfire.org Git - thirdparty/autoconf.git/commitdiff
add m4_stack_foreach and m4_stack_foreach_lifo
authorPaolo Bonzini <bonzini@gnu.org>
Tue, 28 Oct 2008 10:27:31 +0000 (11:27 +0100)
committerPaolo Bonzini <bonzini@gnu.org>
Tue, 28 Oct 2008 17:29:25 +0000 (18:29 +0100)
* lib/m4sugar/m4sugar.m4 (_m4_stack_reverse): New from _m4_copy.
(m4_stack_foreach, m4_stack_foreach_lifo): New.
(m4_copy): Use m4_stack_foreach and m4_curry.
(_m4_dumpdefs_down, _m4_dumpdefs_up): Remove.
(m4_dumpdefs): Rewrite using m4_stack_foreach_lifo.
* tests/m4sugar.at (m4_stack_foreach): New test.

ChangeLog
lib/m4sugar/m4sugar.m4
tests/m4sugar.at

index 4e8fdd1b876ef00b158145dd86df35dc7cab70d7..d0296c06c8f6ef07e787b0cda2657ab3e560be2f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2008-10-28  Paolo Bonzini  <bonzini@gnu.org>
+
+       Add m4_stack_foreach and m4_stack_foreach_lifo.
+       * lib/m4sugar/m4sugar.m4 (_m4_stack_reverse): New from _m4_copy.
+       (m4_stack_foreach, m4_stack_foreach_lifo): New.
+       (m4_copy): Use m4_stack_foreach and m4_curry.
+       (_m4_dumpdefs_down, _m4_dumpdefs_up): Remove.
+       (m4_dumpdefs): Rewrite using m4_stack_foreach_lifo.
+       * tests/m4sugar.at (m4_stack_foreach): New test.
+
 2008-10-28  Paolo Bonzini  <bonzini@gnu.org>
     
        use a shell function for AC_TYPE_INTx_T
index 22c38e8d0df1c9a2d92b989d4d6e38f00feed4c9..eb5d4260cf695f270f378edf8242694a95bdff08 100644 (file)
@@ -535,19 +535,12 @@ m4_define([_m4_bpatsubsts],
 # includes one-shot initialization that is later popped to the normal
 # definition.
 #
-# The recursive worker destructively swaps the order of a stack.  We
-# use a temporary stack, and swap directions twice, using the third
-# argument to restore the original stack.
-#
-# Some macros simply can't be renamed with this method: namely,
-# anything involved in the implementation of _m4_copy.
+# Some macros simply can't be renamed with this method: namely, anything
+# involved in the implementation of m4_stack_foreach and m4_curry.
 m4_define([m4_copy],
 [m4_ifdef([$2], [m4_fatal([$0: won't overwrite defined macro: $2])],
-         [_$0([$1], [m4_tmp])_$0([m4_tmp], [$2],
-  [m4_pushdef([$1], _m4_defn([m4_tmp]))])m4_ifdef([m4_location($1)],
-  [m4_define([m4_location($2)], m4_location)])])])
-m4_define([_m4_copy],
-[m4_ifdef([$1], [m4_pushdef([$2], _m4_defn([$1]))$3[]_m4_popdef([$1])$0($@)])])
+         [m4_stack_foreach([$1], [m4_curry([m4_pushdef], [$2])])m4_ifdef([m4_location($1)],
+[m4_define([m4_location($2)], m4_location)])])])
 
 
 # m4_define_default(MACRO, VALUE)
@@ -603,32 +596,17 @@ m4_define([m4_defn],
        [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])
 
 
-# _m4_dumpdefs_up(NAME)
-# ---------------------
-m4_define([_m4_dumpdefs_up],
-[m4_ifdef([$1],
-         [m4_pushdef([_m4_dumpdefs], _m4_defn([$1]))dnl
-m4_dumpdef([$1])dnl
-_m4_popdef([$1])dnl
-_m4_dumpdefs_up([$1])])])
-
-
-# _m4_dumpdefs_down(NAME)
-# -----------------------
-m4_define([_m4_dumpdefs_down],
-[m4_ifdef([_m4_dumpdefs],
-         [m4_pushdef([$1], _m4_defn([_m4_dumpdefs]))dnl
-_m4_popdef([_m4_dumpdefs])dnl
-_m4_dumpdefs_down([$1])])])
-
-
 # m4_dumpdefs(NAME)
 # -----------------
 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
 # value stack (most recent displayed first).
+#
+# This macro cheats, because it relies on the current definition of NAME
+# while the second argument of m4_stack_foreach_lifo is evaluated (which
+# would be undefined according to the API).  If m4_dumpdef is ever rewritten
+# not to use the builtin, revisit this.
 m4_define([m4_dumpdefs],
-[_m4_dumpdefs_up([$1])dnl
-_m4_dumpdefs_down([$1])])
+[m4_stack_foreach_lifo([$1], [m4_dumpdef([$1])m4_ignore])])
 
 
 # m4_popdef(NAME)
@@ -1189,6 +1167,32 @@ m4_define([m4_map_args_pair],
        [$1([$3], [$4])[]$0([$1], [$2], m4_shift(m4_shift3($@)))])])
 
 
+# m4_stack_foreach(MACRO, FUNC)
+# m4_stack_foreach_lifo(MACRO, FUNC)
+# ----------------------------------
+# Pass each stacked definition of MACRO to the one-argument macro FUNC.
+# m4_stack_foreach proceeds in FIFO order, while m4_stack_foreach_lifo
+# processes the topmost definitions first.  In addition, FUNC should
+# not push or pop definitions of MACRO, and should not expect anything about
+# the active definition of MACRO (it will not be the topmost, and may not
+# be the one passed to FUNC either).
+#
+# The recursive worker _m4_stack_reverse destructively swaps the order of a
+# stack.  We use a temporary stack, and swap directions twice.  Some macros
+# simply can't be examined with this method: namely, anything involved
+# in the implementation of _m4_stack_reverse.
+m4_define([_m4_stack_reverse],
+[m4_ifdef([$1], [m4_pushdef([$2], _m4_defn([$1]))$3[]_m4_popdef([$1])$0($@)])])
+
+m4_define([m4_stack_foreach],
+[_m4_stack_reverse([$1], [m4_tmp-$1])]dnl
+[_m4_stack_reverse([m4_tmp-$1], [$1], [$2(_m4_defn([m4_tmp-$1]))])])
+
+m4_define([m4_stack_foreach_lifo],
+[_m4_stack_reverse([$1], [m4_tmp-$1], [$2(_m4_defn([m4_tmp-$1]))])]dnl
+[_m4_stack_reverse([m4_tmp-$1], [$1])])
+
+
 ## --------------------------- ##
 ## 9. More diversion support.  ##
 ## --------------------------- ##
index 959b209daf5018bddac47c54eeaf3e66e7cfc13a..10ebbd8be45a84fa1e8ca3eafcf5c8877c623415 100644 (file)
@@ -37,6 +37,60 @@ AT_CHECK_M4SUGAR([-o-],, [$2], [$3])
 ])# AT_CHECK_M4SUGAR_TEXT
 
 
+## ------------------ ##
+## m4_stack_foreach.  ##
+## ------------------ ##
+
+AT_SETUP([m4@&t@_stack_foreach])
+
+AT_KEYWORDS([m4@&t@_stack_foreach_lifo m4@&t@_copy m4@&t@_n])
+
+# Test the semantics of macros to walk stacked macro definitions.
+AT_CHECK_M4SUGAR_TEXT([[dnl
+m4_pushdef([abc], [def])dnl
+m4_pushdef([abc], [ghi])dnl
+m4_pushdef([abc], [jkl])dnl
+m4_stack_foreach([abc], [m4_n])
+abc
+m4_stack_foreach_lifo([abc], [m4_n])
+m4_stack_foreach([abc], [m4_n])
+m4_copy([abc], [foo])dnl
+m4_stack_foreach([foo], [m4_n])
+m4_stack_foreach_lifo([foo], [m4_n])
+m4_pushdef([xyz], [123])dnl
+m4_pushdef([xyz], [456])dnl
+m4_define([doit], [[$1](m4_shift(m4_stack_foreach([xyz], [,m4_echo])))
+])dnl
+m4_stack_foreach([abc], [doit])]],
+[[def
+ghi
+jkl
+
+jkl
+jkl
+ghi
+def
+
+def
+ghi
+jkl
+
+def
+ghi
+jkl
+
+jkl
+ghi
+def
+
+def(123,456)
+ghi(123,456)
+jkl(123,456)
+]])
+
+AT_CLEANUP
+
+
 ## --------- ##
 ## m4_defn.  ##
 ## --------- ##