foreach.m4 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. # -*- Autoconf -*-
  2. # This file is part of Autoconf.
  3. # foreach-based replacements for recursive functions.
  4. # Speeds up GNU M4 1.4.x by avoiding quadratic $@ recursion, but penalizes
  5. # GNU M4 1.6 by requiring more memory and macro expansions.
  6. #
  7. # Copyright (C) 2008-2017, 2020 Free Software Foundation, Inc.
  8. # This file is part of Autoconf. This program is free
  9. # software; you can redistribute it and/or modify it under the
  10. # terms of the GNU General Public License as published by the
  11. # Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # Under Section 7 of GPL version 3, you are granted additional
  20. # permissions described in the Autoconf Configure Script Exception,
  21. # version 3.0, as published by the Free Software Foundation.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # and a copy of the Autoconf Configure Script Exception along with
  25. # this program; see the files COPYINGv3 and COPYING.EXCEPTION
  26. # respectively. If not, see <https://www.gnu.org/licenses/>.
  27. # Written by Eric Blake.
  28. # In M4 1.4.x, every byte of $@ is rescanned. This means that an
  29. # algorithm on n arguments that recurses with one less argument each
  30. # iteration will scan n * (n + 1) / 2 arguments, for O(n^2) time. In
  31. # M4 1.6, this was fixed so that $@ is only scanned once, then
  32. # back-references are made to information stored about the scan.
  33. # Thus, n iterations need only scan n arguments, for O(n) time.
  34. # Additionally, in M4 1.4.x, recursive algorithms did not clean up
  35. # memory very well, requiring O(n^2) memory rather than O(n) for n
  36. # iterations.
  37. #
  38. # This file is designed to overcome the quadratic nature of $@
  39. # recursion by writing a variant of m4_foreach that uses m4_for rather
  40. # than $@ recursion to operate on the list. This involves more macro
  41. # expansions, but avoids the need to rescan a quadratic number of
  42. # arguments, making these replacements very attractive for M4 1.4.x.
  43. # On the other hand, in any version of M4, expanding additional macros
  44. # costs additional time; therefore, in M4 1.6, where $@ recursion uses
  45. # fewer macros, these replacements actually pessimize performance.
  46. # Additionally, the use of $10 to mean the tenth argument violates
  47. # POSIX; although all versions of m4 1.4.x support this meaning, a
  48. # future m4 version may switch to take it as the first argument
  49. # concatenated with a literal 0, so the implementations in this file
  50. # are not future-proof. Thus, this file is conditionally included as
  51. # part of m4_init(), only when it is detected that M4 probably has
  52. # quadratic behavior (ie. it lacks the macro __m4_version__).
  53. #
  54. # Please keep this file in sync with m4sugar.m4.
  55. # _m4_foreach(PRE, POST, IGNORED, ARG...)
  56. # ---------------------------------------
  57. # Form the common basis of the m4_foreach and m4_map macros. For each
  58. # ARG, expand PRE[ARG]POST[]. The IGNORED argument makes recursion
  59. # easier, and must be supplied rather than implicit.
  60. #
  61. # This version minimizes the number of times that $@ is evaluated by
  62. # using m4_for to generate a boilerplate into _m4_f then passing $@ to
  63. # that temporary macro. Thus, the recursion is done in m4_for without
  64. # reparsing any user input, and is not quadratic. For an idea of how
  65. # this works, note that m4_foreach(i,[1,2],[i]) calls
  66. # _m4_foreach([m4_define([i],],[)i],[],[1],[2])
  67. # which defines _m4_f:
  68. # $1[$4]$2[]$1[$5]$2[]_m4_popdef([_m4_f])
  69. # then calls _m4_f([m4_define([i],],[)i],[],[1],[2]) for a net result:
  70. # m4_define([i],[1])i[]m4_define([i],[2])i[]_m4_popdef([_m4_f]).
  71. m4_define([_m4_foreach],
  72. [m4_if([$#], [3], [],
  73. [m4_pushdef([_m4_f], _m4_for([4], [$#], [1],
  74. [$0_([1], [2],], [)])[_m4_popdef([_m4_f])])_m4_f($@)])])
  75. m4_define([_m4_foreach_],
  76. [[$$1[$$3]$$2[]]])
  77. # m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
  78. # -----------------------------------------------------------
  79. # Find the first VAL that SWITCH matches, and expand the corresponding
  80. # IF-VAL. If there are no matches, expand DEFAULT.
  81. #
  82. # Use m4_for to create a temporary macro in terms of a boilerplate
  83. # m4_if with final cleanup. If $# is even, we have DEFAULT; if it is
  84. # odd, then rounding the last $# up in the temporary macro is
  85. # harmless. For example, both m4_case(1,2,3,4,5) and
  86. # m4_case(1,2,3,4,5,6) result in the intermediate _m4_case being
  87. # m4_if([$1],[$2],[$3],[$1],[$4],[$5],_m4_popdef([_m4_case])[$6])
  88. m4_define([m4_case],
  89. [m4_if(m4_eval([$# <= 2]), [1], [$2],
  90. [m4_pushdef([_$0], [m4_if(]_m4_for([2], m4_eval([($# - 1) / 2 * 2]), [2],
  91. [_$0_(], [)])[_m4_popdef(
  92. [_$0])]m4_dquote($m4_eval([($# + 1) & ~1]))[)])_$0($@)])])
  93. m4_define([_m4_case_],
  94. [$0_([1], [$1], m4_incr([$1]))])
  95. m4_define([_m4_case__],
  96. [[[$$1],[$$2],[$$3],]])
  97. # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
  98. # -----------------------------------------------------
  99. # m4 equivalent of
  100. #
  101. # if (SWITCH =~ RE1)
  102. # VAL1;
  103. # elif (SWITCH =~ RE2)
  104. # VAL2;
  105. # elif ...
  106. # ...
  107. # else
  108. # DEFAULT
  109. #
  110. # We build the temporary macro _m4_b:
  111. # m4_define([_m4_b], _m4_defn([_m4_bmatch]))_m4_b([$1], [$2], [$3])...
  112. # _m4_b([$1], [$m-1], [$m])_m4_b([], [], [$m+1]_m4_popdef([_m4_b]))
  113. # then invoke m4_unquote(_m4_b($@)), for concatenation with later text.
  114. m4_define([m4_bmatch],
  115. [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
  116. [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
  117. [$#], 2, [$2],
  118. [m4_pushdef([_m4_b], [m4_define([_m4_b],
  119. _m4_defn([_$0]))]_m4_for([3], m4_eval([($# + 1) / 2 * 2 - 1]),
  120. [2], [_$0_(], [)])[_m4_b([], [],]m4_dquote([$]m4_eval(
  121. [($# + 1) / 2 * 2]))[_m4_popdef([_m4_b]))])m4_unquote(_m4_b($@))])])
  122. m4_define([_m4_bmatch],
  123. [m4_if(m4_bregexp([$1], [$2]), [-1], [], [[$3]m4_define([$0])])])
  124. m4_define([_m4_bmatch_],
  125. [$0_([1], m4_decr([$1]), [$1])])
  126. m4_define([_m4_bmatch__],
  127. [[_m4_b([$$1], [$$2], [$$3])]])
  128. # m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
  129. # -------------------------------------------------------------------
  130. # Similar to m4_if, except that each TEST is expanded when encountered.
  131. # If the expansion of TESTn matches the string VALn, the result is IF-VALn.
  132. # The result is DEFAULT if no tests passed. This macro allows
  133. # short-circuiting of expensive tests, where it pays to arrange quick
  134. # filter tests to run first.
  135. #
  136. # m4_cond already guarantees either 3*n or 3*n + 1 arguments, 1 <= n.
  137. # We only have to speed up _m4_cond, by building the temporary _m4_c:
  138. # m4_define([_m4_c], _m4_defn([m4_unquote]))_m4_c([m4_if(($1), [($2)],
  139. # [[$3]m4_define([_m4_c])])])_m4_c([m4_if(($4), [($5)],
  140. # [[$6]m4_define([_m4_c])])])..._m4_c([m4_if(($m-2), [($m-1)],
  141. # [[$m]m4_define([_m4_c])])])_m4_c([[$m+1]]_m4_popdef([_m4_c]))
  142. # We invoke m4_unquote(_m4_c($@)), for concatenation with later text.
  143. m4_define([_m4_cond],
  144. [m4_pushdef([_m4_c], [m4_define([_m4_c],
  145. _m4_defn([m4_unquote]))]_m4_for([2], m4_eval([$# / 3 * 3 - 1]), [3],
  146. [$0_(], [)])[_m4_c(]m4_dquote(m4_dquote(
  147. [$]m4_eval([$# / 3 * 3 + 1])))[_m4_popdef([_m4_c]))])m4_unquote(_m4_c($@))])
  148. m4_define([_m4_cond_],
  149. [$0_(m4_decr([$1]), [$1], m4_incr([$1]))])
  150. m4_define([_m4_cond__],
  151. [[_m4_c([m4_if(($$1), [($$2)], [[$$3]m4_define([_m4_c])])])]])
  152. # m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
  153. # ----------------------------------------------------
  154. # m4 equivalent of
  155. #
  156. # $_ = STRING;
  157. # s/RE1/SUBST1/g;
  158. # s/RE2/SUBST2/g;
  159. # ...
  160. #
  161. # m4_bpatsubsts already validated an odd number of arguments; we only
  162. # need to speed up _m4_bpatsubsts. To avoid nesting, we build the
  163. # temporary _m4_p:
  164. # m4_define([_m4_p], [$1])m4_define([_m4_p],
  165. # m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$2], [$3]))m4_define([_m4_p],
  166. # m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$4], [$5]))m4_define([_m4_p],...
  167. # m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$m-1], [$m]))m4_unquote(
  168. # _m4_defn([_m4_p])_m4_popdef([_m4_p]))
  169. m4_define([_m4_bpatsubsts],
  170. [m4_pushdef([_m4_p], [m4_define([_m4_p],
  171. ]m4_dquote([$]1)[)]_m4_for([3], [$#], [2], [$0_(],
  172. [)])[m4_unquote(_m4_defn([_m4_p])_m4_popdef([_m4_p]))])_m4_p($@)])
  173. m4_define([_m4_bpatsubsts_],
  174. [$0_(m4_decr([$1]), [$1])])
  175. m4_define([_m4_bpatsubsts__],
  176. [[m4_define([_m4_p],
  177. m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$$1], [$$2]))]])
  178. # m4_shiftn(N, ...)
  179. # -----------------
  180. # Returns ... shifted N times. Useful for recursive "varargs" constructs.
  181. #
  182. # m4_shiftn already validated arguments; we only need to speed up
  183. # _m4_shiftn. If N is 3, then we build the temporary _m4_s, defined as
  184. # ,[$5],[$6],...,[$m]_m4_popdef([_m4_s])
  185. # before calling m4_shift(_m4_s($@)).
  186. m4_define([_m4_shiftn],
  187. [m4_if(m4_incr([$1]), [$#], [], [m4_pushdef([_m4_s],
  188. _m4_for(m4_eval([$1 + 2]), [$#], [1],
  189. [[,]m4_dquote($], [)])[_m4_popdef([_m4_s])])m4_shift(_m4_s($@))])])
  190. # m4_do(STRING, ...)
  191. # ------------------
  192. # This macro invokes all its arguments (in sequence, of course). It is
  193. # useful for making your macros more structured and readable by dropping
  194. # unnecessary dnl's and have the macros indented properly.
  195. #
  196. # Here, we use the temporary macro _m4_do, defined as
  197. # $1[]$2[]...[]$n[]_m4_popdef([_m4_do])
  198. m4_define([m4_do],
  199. [m4_if([$#], [0], [],
  200. [m4_pushdef([_$0], _m4_for([1], [$#], [1],
  201. [$], [[[]]])[_m4_popdef([_$0])])_$0($@)])])
  202. # m4_dquote_elt(ARGS)
  203. # -------------------
  204. # Return ARGS as an unquoted list of double-quoted arguments.
  205. #
  206. # _m4_foreach to the rescue.
  207. m4_define([m4_dquote_elt],
  208. [m4_if([$#], [0], [], [[[$1]]_m4_foreach([,m4_dquote(], [)], $@)])])
  209. # m4_reverse(ARGS)
  210. # ----------------
  211. # Output ARGS in reverse order.
  212. #
  213. # Invoke _m4_r($@) with the temporary _m4_r built as
  214. # [$m], [$m-1], ..., [$2], [$1]_m4_popdef([_m4_r])
  215. m4_define([m4_reverse],
  216. [m4_if([$#], [0], [], [$#], [1], [[$1]],
  217. [m4_pushdef([_m4_r], [[$$#]]_m4_for(m4_decr([$#]), [1], [-1],
  218. [[, ]m4_dquote($], [)])[_m4_popdef([_m4_r])])_m4_r($@)])])
  219. # m4_map_args_pair(EXPRESSION, [END-EXPR = EXPRESSION], ARG...)
  220. # -------------------------------------------------------------
  221. # Perform a pairwise grouping of consecutive ARGs, by expanding
  222. # EXPRESSION([ARG1], [ARG2]). If there are an odd number of ARGs, the
  223. # final argument is expanded with END-EXPR([ARGn]).
  224. #
  225. # Build the temporary macro _m4_map_args_pair, with the $2([$m+1])
  226. # only output if $# is odd:
  227. # $1([$3], [$4])[]$1([$5], [$6])[]...$1([$m-1],
  228. # [$m])[]m4_default([$2], [$1])([$m+1])[]_m4_popdef([_m4_map_args_pair])
  229. m4_define([m4_map_args_pair],
  230. [m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
  231. [$#], [1], [m4_fatal([$0: too few arguments: $#: $1])],
  232. [$#], [2], [],
  233. [$#], [3], [m4_default([$2], [$1])([$3])[]],
  234. [m4_pushdef([_$0], _m4_for([3],
  235. m4_eval([$# / 2 * 2 - 1]), [2], [_$0_(], [)])_$0_end(
  236. [1], [2], [$#])[_m4_popdef([_$0])])_$0($@)])])
  237. m4_define([_m4_map_args_pair_],
  238. [$0_([1], [$1], m4_incr([$1]))])
  239. m4_define([_m4_map_args_pair__],
  240. [[$$1([$$2], [$$3])[]]])
  241. m4_define([_m4_map_args_pair_end],
  242. [m4_if(m4_eval([$3 & 1]), [1], [[m4_default([$$2], [$$1])([$$3])[]]])])
  243. # m4_join(SEP, ARG1, ARG2...)
  244. # ---------------------------
  245. # Produce ARG1SEPARG2...SEPARGn. Avoid back-to-back SEP when a given ARG
  246. # is the empty string. No expansion is performed on SEP or ARGs.
  247. #
  248. # Use a self-modifying separator, since we don't know how many
  249. # arguments might be skipped before a separator is first printed, but
  250. # be careful if the separator contains $. _m4_foreach to the rescue.
  251. m4_define([m4_join],
  252. [m4_pushdef([_m4_sep], [m4_define([_m4_sep], _m4_defn([m4_echo]))])]dnl
  253. [_m4_foreach([_$0([$1],], [)], $@)_m4_popdef([_m4_sep])])
  254. m4_define([_m4_join],
  255. [m4_if([$2], [], [], [_m4_sep([$1])[$2]])])
  256. # m4_joinall(SEP, ARG1, ARG2...)
  257. # ------------------------------
  258. # Produce ARG1SEPARG2...SEPARGn. An empty ARG results in back-to-back SEP.
  259. # No expansion is performed on SEP or ARGs.
  260. #
  261. # A bit easier than m4_join. _m4_foreach to the rescue.
  262. m4_define([m4_joinall],
  263. [[$2]m4_if(m4_eval([$# <= 2]), [1], [],
  264. [_m4_foreach([$1], [], m4_shift($@))])])
  265. # m4_list_cmp(A, B)
  266. # -----------------
  267. # Compare the two lists of integer expressions A and B.
  268. #
  269. # m4_list_cmp takes care of any side effects; we only override
  270. # _m4_list_cmp_raw, where we can safely expand lists multiple times.
  271. # First, insert padding so that both lists are the same length; the
  272. # trailing +0 is necessary to handle a missing list. Next, create a
  273. # temporary macro to perform pairwise comparisons until an inequality
  274. # is found. For example, m4_list_cmp([1], [1,2]) creates _m4_cmp as
  275. # m4_if(m4_eval([($1) != ($3)]), [1], [m4_cmp([$1], [$3])],
  276. # m4_eval([($2) != ($4)]), [1], [m4_cmp([$2], [$4])],
  277. # [0]_m4_popdef([_m4_cmp]))
  278. # then calls _m4_cmp([1+0], [0*2], [1], [2+0])
  279. m4_define([_m4_list_cmp_raw],
  280. [m4_if([$1], [$2], 0,
  281. [_m4_list_cmp($1+0_m4_list_pad(m4_count($1), m4_count($2)),
  282. $2+0_m4_list_pad(m4_count($2), m4_count($1)))])])
  283. m4_define([_m4_list_pad],
  284. [m4_if(m4_eval($1 < $2), [1],
  285. [_m4_for(m4_incr([$1]), [$2], [1], [,0*])])])
  286. m4_define([_m4_list_cmp],
  287. [m4_pushdef([_m4_cmp], [m4_if(]_m4_for(
  288. [1], m4_eval([$# >> 1]), [1], [$0_(], [,]m4_eval([$# >> 1])[)])[
  289. [0]_m4_popdef([_m4_cmp]))])_m4_cmp($@)])
  290. m4_define([_m4_list_cmp_],
  291. [$0_([$1], m4_eval([$1 + $2]))])
  292. m4_define([_m4_list_cmp__],
  293. [[m4_eval([($$1) != ($$2)]), [1], [m4_cmp([$$1], [$$2])],
  294. ]])
  295. # m4_max(EXPR, ...)
  296. # m4_min(EXPR, ...)
  297. # -----------------
  298. # Return the decimal value of the maximum (or minimum) in a series of
  299. # integer expressions.
  300. #
  301. # _m4_foreach to the rescue; we only need to replace _m4_minmax. Here,
  302. # we need a temporary macro to track the best answer so far, so that
  303. # the foreach expression is tractable.
  304. m4_define([_m4_minmax],
  305. [m4_pushdef([_m4_best], m4_eval([$2]))_m4_foreach(
  306. [m4_define([_m4_best], $1(_m4_best,], [))], m4_shift($@))]dnl
  307. [_m4_best[]_m4_popdef([_m4_best])])
  308. # m4_set_add_all(SET, VALUE...)
  309. # -----------------------------
  310. # Add each VALUE into SET. This is O(n) in the number of VALUEs, and
  311. # can be faster than calling m4_set_add for each VALUE.
  312. #
  313. # _m4_foreach to the rescue. If no deletions have occurred, then
  314. # avoid the speed penalty of m4_set_add.
  315. m4_define([m4_set_add_all],
  316. [m4_if([$#], [0], [], [$#], [1], [],
  317. [m4_define([_m4_set_size($1)], m4_eval(m4_set_size([$1])
  318. + m4_len(_m4_foreach(m4_ifdef([_m4_set_cleanup($1)],
  319. [[m4_set_add]], [[_$0]])[([$1],], [)], $@))))])])
  320. m4_define([_m4_set_add_all],
  321. [m4_ifdef([_m4_set([$1],$2)], [],
  322. [m4_define([_m4_set([$1],$2)],
  323. [1])m4_pushdef([_m4_set([$1])], [$2])-])])