Fix nested builtin(`shift',$@) regression from 2008-02-22.
[m4/ericb.git] / src / macro.c
blob4558e9c3cffbab1ac01342820c53e61931801b5c
1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2006, 2007, 2008 Free
4 Software Foundation, Inc.
6 This file is part of GNU M4.
8 GNU M4 is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU M4 is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* This file contains the functions, that performs the basic argument
23 parsing and macro expansion. */
25 #include "m4.h"
27 #ifndef DEBUG_MACRO
28 # define DEBUG_MACRO 0
29 #endif /* DEBUG_MACRO */
31 /* Opaque structure describing all arguments to a macro, including the
32 macro name at index 0. The lifetime of argv0 is only guaranteed
33 within a call to expand_macro, whereas the lifetime of the array
34 members is guaranteed as long as the input engine can parse text
35 with a reference to $@. */
36 struct macro_arguments
38 /* Number of arguments owned by this object, may be larger than
39 arraylen since the array can refer to multiple arguments via a
40 single $@ reference. */
41 unsigned int argc;
42 /* False unless the macro expansion refers to $@; determines whether
43 this object can be freed immediately at the end of expand_macro,
44 or must wait until all recursion has completed. */
45 bool_bitfield inuse : 1;
46 /* False if all arguments are just text or func, true if this argv
47 refers to another one. */
48 bool_bitfield wrapper : 1;
49 /* False if all arguments belong to this argv, true if some of them
50 include references to another. */
51 bool_bitfield has_ref : 1;
52 /* True to flatten builtins contained in references. */
53 bool_bitfield flatten : 1;
54 /* True if any token contains builtins. */
55 bool_bitfield has_func : 1;
56 const char *argv0; /* The macro name being expanded. */
57 size_t argv0_len; /* Length of argv0. */
58 /* The value of quote_age used when parsing all arguments in this
59 object, or 0 if quote_age changed during parsing or if any of the
60 arguments might contain content that can affect rescan. */
61 unsigned int quote_age;
62 int level; /* Which obstack owns this argv. */
63 unsigned int arraylen; /* True length of allocated elements in array. */
64 /* Used as a variable-length array, storing information about each
65 argument. */
66 token_data *array[FLEXIBLE_ARRAY_MEMBER];
69 /* Internal struct to maintain list of argument stacks. Within a
70 recursion level, consecutive macros can share a stack, but distinct
71 recursion levels need different stacks since the nested macro is
72 interrupting the argument collection of the outer level. Note that
73 a reference can live as long as the expansion containing the
74 reference can participate as an argument in a future macro call.
76 Therefore, we implement a reference counter for each expansion
77 level, tracking how many references exist into the obstack, as well
78 as associate a level with each reference. Of course, expand_macro
79 is actively using argv, so it increments the refcount on entry and
80 decrements it on exit. Additionally, any time the input engine is
81 handed a reference that it does not inline, it increases the
82 refcount in push_token, then decreases it in pop_input once the
83 reference has been rescanned. Finally, when the input engine hands
84 a reference back to expand_argument, the refcount increases, which
85 is then cleaned up at the end of expand_macro.
87 For a running example, consider this input:
89 define(a,A)define(b,`a(`$1')')define(c,$*)dnl
90 define(x,`a(1)`'c($@')define(y,`$@)')dnl
91 x(a(`b')``a'')y(`b')(`a')
92 => AAaA
94 Assuming all arguments are large enough to exceed the inlining
95 thresholds of the input engine, the interesting sequence of events
96 is as follows:
98 stacks[0] refs stacks[1] refs
99 after second dnl ends: `' 0 `' 0
100 expand_macro for x, level 0: `' 1 `' 0
101 expand_macro for a, level 1: `' 1 `' 1
102 after collect_arguments for a: `' 1 `b' 1
103 push `A' to input stack: `' 1 `b' 1
104 exit expand_macro for a: `' 1 `' 0
105 after collect_arguments for x: `A`a'' 1 `' 0
106 push `a(1)`'c(' to input stack: `A`a'' 1 `' 0
107 push_token saves $@(x) ref: `A`a'' 2 `' 0
108 exit expand_macro for x: `A`a'' 1 `' 0
109 expand_macro for a, level 0: `A`a'' 2 `' 0
110 after collect_arguments for a: `A`a''`1' 2 `' 0
111 push `A' to input stack: `A`a''`1' 2 `' 0
112 exit expand_macro for a: `A`a'' 1 `' 0
113 output `A': `A`a'' 1 `' 0
114 expand_macro for c, level 0: `A`a'' 2 `' 0
115 expand_argument gets $@(x) ref: `A`a''`$@(x)' 3 `' 0
116 pop_input ends $@(x) ref: `A`a''`$@(x)' 2 `' 0
117 expand_macro for y, level 1: `A`a''`$@(x)' 2 `' 1
118 after collect_arguments for y: `A`a''`$@(x)' 2 `b' 1
119 push_token saves $@(y) ref: `A`a''`$@(x)' 2 `b' 2
120 push `)' to input stack: `A`a''`$@(x)' 2 `b' 2
121 exit expand_macro for y: `A`a''`$@(x)' 2 `b' 1
122 expand_argument gets $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 2
123 pop_input ends $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 1
124 after collect_arguments for c: `A`a''`$@(x)$@(y)' 2 `b' 1
125 push_token saves $*(c) ref: `A`a''`$@(x)$@(y)' 3 `b' 2
126 expand_macro frees $@(x) ref: `A`a''`$@(x)$@(y)' 2 `b' 2
127 expand_macro frees $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 1
128 exit expand_macro for c: `A`a''`$@(x)$@(y)' 1 `b' 1
129 output `Aa': `A`a''`$@(x)$@(y)' 0 `b' 1
130 pop_input ends $*(c)$@(x) ref: `' 0 `b' 1
131 expand_macro for b, level 0: `' 1 `b' 1
132 pop_input ends $*(c)$@(y) ref: `' 1 `' 0
133 after collect_arguments for b: `a' 1 `' 0
134 push `a(`' to input stack: `a' 1 `' 0
135 push_token saves $1(b) ref: `a' 2 `' 0
136 push `')' to input stack: `a' 2 `' 0
137 exit expand_macro for b: `a' 1 `' 0
138 expand_macro for a, level 0 : `a' 2 `' 0
139 expand_argument gets $1(b) ref: `a'`$1(b)' 3 `' 0
140 pop_input ends $1(b) ref: `a'`$1(b)' 2 `' 0
141 after collect_arguments for a: `a'`$1(b)' 2 `' 0
142 push `A' to input stack: `a'`$1(b)' 2 `' 0
143 expand_macro frees $1(b) ref: `a'`$1(b)' 1 `' 0
144 exit expand_macro for a: `' 0 `' 0
145 output `A': `' 0 `' 0
147 An obstack is only completely cleared when its refcount reaches
148 zero. However, as an optimization, expand_macro also frees
149 anything that it added to the obstack if no additional references
150 were added at the current expansion level, to reduce the amount of
151 memory left on the obstack while waiting for refcounts to drop.
153 struct macro_arg_stacks
155 size_t refcount; /* Number of active $@ references at this level. */
156 size_t argcount; /* Number of argv at this level. */
157 struct obstack *args; /* Content of arguments. */
158 struct obstack *argv; /* Argv pointers into args. */
159 void *args_base; /* Location for clearing the args obstack. */
160 void *argv_base; /* Location for clearing the argv obstack. */
163 typedef struct macro_arg_stacks macro_arg_stacks;
165 static void expand_macro (symbol *);
166 static bool expand_token (struct obstack *, token_type, token_data *, int,
167 bool);
169 /* Array of stacks, one element per macro recursion level. */
170 static macro_arg_stacks *stacks;
172 /* Current size of stacks array. */
173 static size_t stacks_count;
175 /* Current recursion level in expand_macro (). */
176 int expansion_level = 0;
178 /* The number of the current call of expand_macro (). */
179 static int macro_call_id = 0;
181 /* The empty string token. */
182 static token_data empty_token;
184 #if DEBUG_MACRO
185 /* True if significant changes to stacks should be printed to the
186 trace stream. Primarily useful for debugging $@ ref memory leaks,
187 and controlled by M4_DEBUG_MACRO environment variable. */
188 static int debug_macro_level;
189 #else
190 # define debug_macro_level 0
191 #endif /* !DEBUG_MACRO */
192 #define PRINT_ARGCOUNT_CHANGES 1
193 #define PRINT_REFCOUNT_INCREASE 2
194 #define PRINT_REFCOUNT_DECREASE 4
197 /*----------------------------------------------------------------.
198 | This function reads all input, and expands each token, one at a |
199 | time. |
200 `----------------------------------------------------------------*/
202 void
203 expand_input (void)
205 token_type t;
206 token_data td;
207 int line;
208 size_t i;
210 #if DEBUG_MACRO
211 const char *s = getenv ("M4_DEBUG_MACRO");
212 if (s)
213 debug_macro_level = strtol (s, NULL, 0);
214 #endif /* DEBUG_MACRO */
216 TOKEN_DATA_TYPE (&empty_token) = TOKEN_TEXT;
217 TOKEN_DATA_TEXT (&empty_token) = "";
218 TOKEN_DATA_LEN (&empty_token) = 0;
219 #ifdef ENABLE_CHANGEWORD
220 TOKEN_DATA_ORIG_TEXT (&empty_token) = "";
221 #endif
223 while ((t = next_token (&td, &line, NULL, false, NULL)) != TOKEN_EOF)
224 expand_token (NULL, t, &td, line, true);
226 for (i = 0; i < stacks_count; i++)
228 assert (stacks[i].refcount == 0 && stacks[i].argcount == 0);
229 if (stacks[i].args)
231 obstack_free (stacks[i].args, NULL);
232 free (stacks[i].args);
233 obstack_free (stacks[i].argv, NULL);
234 free (stacks[i].argv);
237 free (stacks);
238 stacks = NULL;
239 stacks_count = 0;
243 /*-------------------------------------------------------------------.
244 | Expand one token TD onto the stack OBS, according to its type T, |
245 | which began parsing on the specified LINE. If OBS is NULL, output |
246 | the data. If FIRST, there is no previous text in the current |
247 | argument. Potential macro names (TOKEN_WORD) are looked up in the |
248 | symbol table, to see if they have a macro definition. If they |
249 | have, they are expanded as macros, otherwise the text is just |
250 | copied to the output. Return true if the result is guaranteed to |
251 | give the same parse on rescan in a quoted context, provided |
252 | quoting doesn't change. Returning false is always safe, although |
253 | it may lead to slower performance. |
254 `-------------------------------------------------------------------*/
256 static bool
257 expand_token (struct obstack *obs, token_type t, token_data *td, int line,
258 bool first)
260 symbol *sym;
261 bool result;
262 int ch;
264 switch (t)
265 { /* TOKSW */
266 case TOKEN_EOF:
267 case TOKEN_MACDEF:
268 /* Always safe, since there is no text to rescan. */
269 return true;
271 case TOKEN_STRING:
272 /* Tokens and comments are safe in isolation (since quote_age()
273 detects any change in delimiters). But if other text is
274 already present, multi-character delimiters could be an
275 issue, so use a conservative heuristic. If obstack is
276 provided, the string was already expanded into it during
277 next_token. */
278 result = first || safe_quotes ();
279 if (obs)
280 return result;
281 break;
283 case TOKEN_OPEN:
284 case TOKEN_COMMA:
285 case TOKEN_CLOSE:
286 /* Conservative heuristic; thanks to multi-character delimiter
287 concatenation. */
288 result = safe_quotes ();
289 break;
291 case TOKEN_SIMPLE:
292 /* Conservative heuristic; if these characters are whitespace or
293 numeric, then behavior of safe_quotes is applicable.
294 Otherwise, assume these characters have a high likelihood of
295 use in quote delimiters. */
296 ch = to_uchar (*TOKEN_DATA_TEXT (td));
297 result = (isspace (ch) || isdigit (ch)) && safe_quotes ();
298 break;
300 case TOKEN_WORD:
301 sym = lookup_symbol (TOKEN_DATA_TEXT (td), SYMBOL_LOOKUP);
302 if (sym == NULL || SYMBOL_TYPE (sym) == TOKEN_VOID
303 || (SYMBOL_TYPE (sym) == TOKEN_FUNC
304 && SYMBOL_BLIND_NO_ARGS (sym)
305 && peek_token () != TOKEN_OPEN))
307 #ifdef ENABLE_CHANGEWORD
308 divert_text (obs, TOKEN_DATA_ORIG_TEXT (td),
309 TOKEN_DATA_LEN (td), line);
310 #else
311 divert_text (obs, TOKEN_DATA_TEXT (td), TOKEN_DATA_LEN (td), line);
312 #endif /* !ENABLE_CHANGEWORD */
313 /* The word just appended is unquoted, but the heuristics of
314 safe_quote are applicable. */
315 return safe_quotes();
317 expand_macro (sym);
318 /* Expanding a macro creates new tokens to scan, and those new
319 tokens may append unsafe text later; but we did not append
320 any text now. */
321 return true;
323 default:
324 assert (!"expand_token");
325 abort ();
327 divert_text (obs, TOKEN_DATA_TEXT (td), TOKEN_DATA_LEN (td), line);
328 return result;
332 /*---------------------------------------------------------------.
333 | Helper function to print warning about concatenating FUNC with |
334 | text. |
335 `---------------------------------------------------------------*/
336 static void
337 warn_builtin_concat (const char *caller, builtin_func *func)
339 const builtin *bp = find_builtin_by_addr (func);
340 assert (bp);
341 m4_warn (0, caller, _("cannot concatenate builtin `%s'"), bp->name);
344 /*-------------------------------------------------------------------.
345 | This function parses one argument to a macro call. It expects the |
346 | first left parenthesis or the separating comma to have been read |
347 | by the caller. It skips leading whitespace, then reads and |
348 | expands tokens, until it finds a comma or right parenthesis at the |
349 | same level of parentheses. It returns a flag indicating whether |
350 | the argument read is the last for the active macro call. The |
351 | argument is built on the obstack OBS, indirectly through |
352 | expand_token (). Report errors on behalf of CALLER. |
353 `-------------------------------------------------------------------*/
355 static bool
356 expand_argument (struct obstack *obs, token_data *argp, const char *caller)
358 token_type t;
359 token_data td;
360 int paren_level;
361 const char *file = current_file;
362 int line = current_line;
363 unsigned int age = quote_age ();
364 bool first = true;
366 TOKEN_DATA_TYPE (argp) = TOKEN_VOID;
368 /* Skip leading white space. */
371 t = next_token (&td, NULL, obs, true, caller);
373 while (t == TOKEN_SIMPLE && isspace (to_uchar (*TOKEN_DATA_TEXT (&td))));
375 paren_level = 0;
377 while (1)
380 switch (t)
381 { /* TOKSW */
382 case TOKEN_COMMA:
383 case TOKEN_CLOSE:
384 if (paren_level == 0)
386 size_t len = obstack_object_size (obs);
387 if (TOKEN_DATA_TYPE (argp) == TOKEN_FUNC)
389 if (!len)
390 return t == TOKEN_COMMA;
391 warn_builtin_concat (caller, TOKEN_DATA_FUNC (argp));
393 if (TOKEN_DATA_TYPE (argp) != TOKEN_COMP)
395 TOKEN_DATA_TYPE (argp) = TOKEN_TEXT;
396 if (len)
398 obstack_1grow (obs, '\0');
399 TOKEN_DATA_TEXT (argp) = (char *) obstack_finish (obs);
401 else
402 TOKEN_DATA_TEXT (argp) = NULL;
403 TOKEN_DATA_LEN (argp) = len;
404 TOKEN_DATA_QUOTE_AGE (argp) = age;
406 else
407 make_text_link (obs, NULL, &argp->u.u_c.end);
408 return t == TOKEN_COMMA;
410 /* fallthru */
411 case TOKEN_OPEN:
412 case TOKEN_SIMPLE:
413 if (t == TOKEN_OPEN)
414 paren_level++;
415 else if (t == TOKEN_CLOSE)
416 paren_level--;
417 if (!expand_token (obs, t, &td, line, first))
418 age = 0;
419 break;
421 case TOKEN_EOF:
422 /* Current_file changed to "" if we see TOKEN_EOF, use the
423 previous value we stored earlier. */
424 m4_error_at_line (EXIT_FAILURE, 0, file, line, caller,
425 _("end of file in argument list"));
426 break;
428 case TOKEN_WORD:
429 case TOKEN_STRING:
430 if (!expand_token (obs, t, &td, line, first))
431 age = 0;
432 if (TOKEN_DATA_TYPE (&td) == TOKEN_COMP)
434 if (TOKEN_DATA_TYPE (argp) != TOKEN_COMP)
436 if (TOKEN_DATA_TYPE (argp) == TOKEN_FUNC)
437 warn_builtin_concat (caller, TOKEN_DATA_FUNC (argp));
438 TOKEN_DATA_TYPE (argp) = TOKEN_COMP;
439 argp->u.u_c.chain = td.u.u_c.chain;
440 argp->u.u_c.wrapper = argp->u.u_c.has_func = false;
442 else
444 assert (argp->u.u_c.end);
445 argp->u.u_c.end->next = td.u.u_c.chain;
447 argp->u.u_c.end = td.u.u_c.end;
448 if (td.u.u_c.has_func)
449 argp->u.u_c.has_func = true;
451 break;
453 case TOKEN_MACDEF:
454 if (TOKEN_DATA_TYPE (argp) == TOKEN_VOID
455 && obstack_object_size (obs) == 0)
457 TOKEN_DATA_TYPE (argp) = TOKEN_FUNC;
458 TOKEN_DATA_FUNC (argp) = TOKEN_DATA_FUNC (&td);
460 else
462 if (TOKEN_DATA_TYPE (argp) == TOKEN_FUNC)
463 warn_builtin_concat (caller, TOKEN_DATA_FUNC (argp));
464 warn_builtin_concat (caller, TOKEN_DATA_FUNC (&td));
465 TOKEN_DATA_TYPE (argp) = TOKEN_TEXT;
467 break;
469 case TOKEN_ARGV:
470 assert (paren_level == 0 && TOKEN_DATA_TYPE (argp) == TOKEN_VOID
471 && obstack_object_size (obs) == 0
472 && td.u.u_c.chain == td.u.u_c.end
473 && td.u.u_c.chain->type == CHAIN_ARGV);
474 TOKEN_DATA_TYPE (argp) = TOKEN_COMP;
475 argp->u.u_c.chain = argp->u.u_c.end = td.u.u_c.chain;
476 argp->u.u_c.wrapper = true;
477 argp->u.u_c.has_func = td.u.u_c.has_func;
478 t = next_token (&td, NULL, NULL, false, caller);
479 if (argp->u.u_c.chain->u.u_a.skip_last)
480 assert (t == TOKEN_COMMA);
481 else
482 assert (t == TOKEN_COMMA || t == TOKEN_CLOSE);
483 return t == TOKEN_COMMA;
485 default:
486 assert (!"expand_argument");
487 abort ();
490 if (TOKEN_DATA_TYPE (argp) != TOKEN_VOID || obstack_object_size (obs))
491 first = false;
492 t = next_token (&td, NULL, obs, first, caller);
496 /*-------------------------------------------------------------------------.
497 | Collect all the arguments to a call of the macro SYM. The arguments are |
498 | stored on the obstack ARGUMENTS and a table of pointers to the arguments |
499 | on the obstack argv_stack. |
500 `-------------------------------------------------------------------------*/
502 static macro_arguments *
503 collect_arguments (symbol *sym, struct obstack *arguments,
504 struct obstack *argv_stack)
506 token_data td;
507 token_data *tdp;
508 bool more_args;
509 bool groks_macro_args = SYMBOL_MACRO_ARGS (sym);
510 macro_arguments args;
511 macro_arguments *argv;
513 args.argc = 1;
514 args.inuse = false;
515 args.wrapper = false;
516 args.has_ref = false;
517 args.flatten = !groks_macro_args;
518 args.has_func = false;
519 args.argv0 = SYMBOL_NAME (sym);
520 args.argv0_len = strlen (args.argv0);
521 args.quote_age = quote_age ();
522 args.level = expansion_level - 1;
523 args.arraylen = 0;
524 obstack_grow (argv_stack, &args, offsetof (macro_arguments, array));
526 if (peek_token () == TOKEN_OPEN)
528 /* gobble parenthesis */
529 next_token (&td, NULL, NULL, false, SYMBOL_NAME (sym));
532 tdp = (token_data *) obstack_alloc (arguments, sizeof *tdp);
533 more_args = expand_argument (arguments, tdp, SYMBOL_NAME (sym));
535 if ((TOKEN_DATA_TYPE (tdp) == TOKEN_TEXT && !TOKEN_DATA_LEN (tdp))
536 || (!groks_macro_args && TOKEN_DATA_TYPE (tdp) == TOKEN_FUNC))
538 obstack_free (arguments, tdp);
539 tdp = &empty_token;
541 obstack_ptr_grow (argv_stack, tdp);
542 args.arraylen++;
543 args.argc++;
544 switch (TOKEN_DATA_TYPE (tdp))
546 case TOKEN_TEXT:
547 /* Be conservative - any change in quoting while
548 collecting arguments, or any argument that consists
549 of unsafe text, will require a rescan if $@ is
550 reused. */
551 if (TOKEN_DATA_LEN (tdp) > 0
552 && TOKEN_DATA_QUOTE_AGE (tdp) != args.quote_age)
553 args.quote_age = 0;
554 break;
555 case TOKEN_FUNC:
556 args.has_func = true;
557 break;
558 case TOKEN_COMP:
559 args.has_ref = true;
560 if (tdp->u.u_c.wrapper)
562 assert (tdp->u.u_c.chain->type == CHAIN_ARGV
563 && !tdp->u.u_c.chain->next);
564 args.argc += (tdp->u.u_c.chain->u.u_a.argv->argc
565 - tdp->u.u_c.chain->u.u_a.index
566 - tdp->u.u_c.chain->u.u_a.skip_last - 1);
567 args.wrapper = true;
569 if (tdp->u.u_c.has_func)
570 args.has_func = true;
571 break;
572 default:
573 assert (!"expand_argument");
574 abort ();
577 while (more_args);
579 argv = (macro_arguments *) obstack_finish (argv_stack);
580 argv->argc = args.argc;
581 argv->wrapper = args.wrapper;
582 argv->has_ref = args.has_ref;
583 argv->has_func = args.has_func;
584 if (args.quote_age != quote_age ())
585 argv->quote_age = 0;
586 argv->arraylen = args.arraylen;
587 return argv;
591 /*-----------------------------------------------------------------.
592 | Call the macro SYM, which is either a builtin function or a user |
593 | macro (via the expansion function expand_user_macro () in |
594 | builtin.c). There are ARGC arguments to the call, stored in the |
595 | ARGV table. The expansion is left on the obstack EXPANSION. |
596 `-----------------------------------------------------------------*/
598 void
599 call_macro (symbol *sym, int argc, macro_arguments *argv,
600 struct obstack *expansion)
602 switch (SYMBOL_TYPE (sym))
604 case TOKEN_FUNC:
605 SYMBOL_FUNC (sym) (expansion, argc, argv);
606 break;
608 case TOKEN_TEXT:
609 expand_user_macro (expansion, sym, argc, argv);
610 break;
612 default:
613 assert (!"call_macro");
614 abort ();
618 /*-------------------------------------------------------------------------.
619 | The macro expansion is handled by expand_macro (). It parses the |
620 | arguments, using collect_arguments (), and builds a table of pointers to |
621 | the arguments. The arguments themselves are stored on a local obstack. |
622 | Expand_macro () uses call_macro () to do the call of the macro. |
624 | Expand_macro () is potentially recursive, since it calls expand_argument |
625 | (), which might call expand_token (), which might call expand_macro (). |
626 `-------------------------------------------------------------------------*/
628 static void
629 expand_macro (symbol *sym)
631 void *args_base; /* Base of stacks[i].args on entry. */
632 void *args_scratch; /* Base of scratch space for call_macro. */
633 void *argv_base; /* Base of stacks[i].argv on entry. */
634 macro_arguments *argv; /* Arguments to the called macro. */
635 struct obstack *expansion; /* Collects the macro's expansion. */
636 const input_block *expanded; /* The resulting expansion, for tracing. */
637 bool traced; /* True if this macro is traced. */
638 int my_call_id; /* Sequence id for this macro. */
639 int level = expansion_level; /* Expansion level of this macro. */
641 /* Report errors at the location where the open parenthesis (if any)
642 was found, but after expansion, restore global state back to the
643 location of the close parenthesis. This is safe since we
644 guarantee that macro expansion does not alter the state of
645 current_file/current_line (dnl, include, and sinclude are special
646 cased in the input engine to ensure this fact). */
647 const char *loc_open_file = current_file;
648 int loc_open_line = current_line;
649 const char *loc_close_file;
650 int loc_close_line;
652 /* Obstack preparation. */
653 if (level >= stacks_count)
655 size_t old_count = stacks_count;
656 stacks = (macro_arg_stacks *) x2nrealloc (stacks, &stacks_count,
657 sizeof *stacks);
658 memset (&stacks[old_count], 0,
659 sizeof *stacks * (stacks_count - old_count));
661 if (!stacks[level].args)
663 assert (!stacks[level].refcount);
664 stacks[level].args = xmalloc (sizeof (struct obstack));
665 stacks[level].argv = xmalloc (sizeof (struct obstack));
666 obstack_init (stacks[level].args);
667 obstack_init (stacks[level].argv);
668 stacks[level].args_base = obstack_finish (stacks[level].args);
669 stacks[level].argv_base = obstack_finish (stacks[level].argv);
671 assert (obstack_object_size (stacks[level].args) == 0
672 && obstack_object_size (stacks[level].argv) == 0);
673 args_base = obstack_finish (stacks[level].args);
674 argv_base = obstack_finish (stacks[level].argv);
675 adjust_refcount (level, true);
676 stacks[level].argcount++;
678 /* Prepare for argument collection. */
679 SYMBOL_PENDING_EXPANSIONS (sym)++;
680 expansion_level++;
681 if (nesting_limit > 0 && expansion_level > nesting_limit)
682 m4_error (EXIT_FAILURE, 0, NULL,
683 _("recursion limit of %d exceeded, use -L<N> to change it"),
684 nesting_limit);
686 macro_call_id++;
687 my_call_id = macro_call_id;
689 traced = (debug_level & DEBUG_TRACE_ALL) || SYMBOL_TRACED (sym);
690 if (traced && (debug_level & DEBUG_TRACE_CALL))
691 trace_prepre (SYMBOL_NAME (sym), my_call_id);
693 argv = collect_arguments (sym, stacks[level].args, stacks[level].argv);
694 args_scratch = obstack_finish (stacks[level].args);
696 /* The actual macro call. */
697 loc_close_file = current_file;
698 loc_close_line = current_line;
699 current_file = loc_open_file;
700 current_line = loc_open_line;
702 if (traced)
703 trace_pre (SYMBOL_NAME (sym), my_call_id, argv);
705 expansion = push_string_init ();
706 call_macro (sym, argv->argc, argv, expansion);
707 expanded = push_string_finish ();
709 if (traced)
710 trace_post (SYMBOL_NAME (sym), my_call_id, argv, expanded);
712 /* Cleanup. */
713 current_file = loc_close_file;
714 current_line = loc_close_line;
716 --expansion_level;
717 --SYMBOL_PENDING_EXPANSIONS (sym);
719 if (SYMBOL_DELETED (sym))
720 free_symbol (sym);
722 /* We no longer need argv, so reduce the refcount. Additionally, if
723 no other references to argv were created, we can free our portion
724 of the obstack, although we must leave earlier content alone. A
725 refcount of 0 implies that adjust_refcount already freed the
726 entire stack. */
727 arg_adjust_refcount (argv, false);
728 if (stacks[level].refcount)
730 if (argv->inuse)
732 obstack_free (stacks[level].args, args_scratch);
733 if (debug_macro_level & PRINT_ARGCOUNT_CHANGES)
734 xfprintf (debug, "m4debug: -%d- `%s' in use, level=%d, "
735 "refcount=%zu, argcount=%zu\n", my_call_id, argv->argv0,
736 level, stacks[level].refcount, stacks[level].argcount);
738 else
740 obstack_free (stacks[level].args, args_base);
741 obstack_free (stacks[level].argv, argv_base);
742 stacks[level].argcount--;
747 /* Adjust the refcount of argument stack LEVEL. If INCREASE, then
748 increase the count, otherwise decrease the count and clear the
749 entire stack if the new count is zero. Return the new
750 refcount. */
751 size_t
752 adjust_refcount (int level, bool increase)
754 assert (level >= 0 && level < stacks_count && stacks[level].args);
755 assert (increase || stacks[level].refcount);
756 if (increase)
757 stacks[level].refcount++;
758 else if (--stacks[level].refcount == 0)
760 obstack_free (stacks[level].args, stacks[level].args_base);
761 obstack_free (stacks[level].argv, stacks[level].argv_base);
762 if ((debug_macro_level & PRINT_ARGCOUNT_CHANGES)
763 && stacks[level].argcount > 1)
764 xfprintf (debug, "m4debug: -%d- freeing %zu args, level=%d\n",
765 macro_call_id, stacks[level].argcount, level);
766 stacks[level].argcount = 0;
768 if (debug_macro_level
769 & (increase ? PRINT_REFCOUNT_INCREASE : PRINT_REFCOUNT_DECREASE))
770 xfprintf (debug, "m4debug: level %d refcount=%d\n", level,
771 stacks[level].refcount);
772 return stacks[level].refcount;
775 /* Given ARGV, adjust the refcount of every reference it contains in
776 the direction decided by INCREASE. Return true if increasing
777 references to ARGV implies the first use of ARGV. */
778 bool
779 arg_adjust_refcount (macro_arguments *argv, bool increase)
781 unsigned int i;
782 token_chain *chain;
783 bool result = !argv->inuse;
785 if (argv->has_ref)
786 for (i = 0; i < argv->arraylen; i++)
787 if (TOKEN_DATA_TYPE (argv->array[i]) == TOKEN_COMP)
789 chain = argv->array[i]->u.u_c.chain;
790 while (chain)
792 switch (chain->type)
794 case CHAIN_STR:
795 if (chain->u.u_s.level >= 0)
796 adjust_refcount (chain->u.u_s.level, increase);
797 break;
798 case CHAIN_ARGV:
799 assert (chain->u.u_a.argv->inuse);
800 arg_adjust_refcount (chain->u.u_a.argv, increase);
801 break;
802 default:
803 assert (!"arg_adjust_refcount");
804 abort ();
806 chain = chain->next;
809 adjust_refcount (argv->level, increase);
810 return result;
814 /* Given ARGV, return the token_data that contains argument INDEX;
815 INDEX must be > 0, < argv->argc. If LEVEL is non-NULL, *LEVEL is
816 set to the obstack level that contains the token (which is not
817 necessarily the level of ARGV). */
818 static token_data *
819 arg_token (macro_arguments *argv, unsigned int index, int *level)
821 unsigned int i;
822 token_data *token;
824 assert (index && index < argv->argc);
825 if (level)
826 *level = argv->level;
827 if (!argv->wrapper)
828 return argv->array[index - 1];
830 /* Must cycle through all tokens, until we find index, since a ref
831 may occupy multiple indices. */
832 for (i = 0; i < argv->arraylen; i++)
834 token = argv->array[i];
835 if (TOKEN_DATA_TYPE (token) == TOKEN_COMP && token->u.u_c.wrapper)
837 token_chain *chain = token->u.u_c.chain;
838 assert (!chain->next && chain->type == CHAIN_ARGV);
839 if (index <= (chain->u.u_a.argv->argc - chain->u.u_a.index
840 - chain->u.u_a.skip_last))
842 token = arg_token (chain->u.u_a.argv,
843 chain->u.u_a.index - 1 + index, level);
844 if (chain->u.u_a.flatten
845 && TOKEN_DATA_TYPE (token) == TOKEN_FUNC)
846 token = &empty_token;
847 break;
849 index -= (chain->u.u_a.argv->argc - chain->u.u_a.index
850 - chain->u.u_a.skip_last);
852 else if (--index == 0)
853 break;
855 return token;
858 /* Mark ARGV as being in use, along with any $@ references that it
859 wraps. */
860 static void
861 arg_mark (macro_arguments *argv)
863 unsigned int i;
864 token_chain *chain;
866 if (argv->inuse)
867 return;
868 argv->inuse = true;
869 if (argv->wrapper)
870 for (i = 0; i < argv->arraylen; i++)
871 if (TOKEN_DATA_TYPE (argv->array[i]) == TOKEN_COMP
872 && argv->array[i]->u.u_c.wrapper)
874 chain = argv->array[i]->u.u_c.chain;
875 assert (!chain->next && chain->type == CHAIN_ARGV);
876 if (!chain->u.u_a.argv->inuse)
877 arg_mark (chain->u.u_a.argv);
881 /* Given ARGV, return how many arguments it refers to. */
882 unsigned int
883 arg_argc (macro_arguments *argv)
885 return argv->argc;
888 /* Given ARGV, return the type of argument INDEX. Index 0 is always
889 text, and indices beyond argc are likewise treated as text. */
890 token_data_type
891 arg_type (macro_arguments *argv, unsigned int index)
893 token_data_type type;
894 token_data *token;
896 if (argv->flatten || !argv->has_func || index == 0 || index >= argv->argc)
897 return TOKEN_TEXT;
898 token = arg_token (argv, index, NULL);
899 type = TOKEN_DATA_TYPE (token);
900 if (type == TOKEN_COMP && !token->u.u_c.has_func)
901 type = TOKEN_TEXT;
902 if (type != TOKEN_TEXT)
903 assert (argv->has_func);
904 /* TODO support TOKEN_COMP meaning concatenation of builtins. */
905 assert (type != TOKEN_COMP);
906 return type;
909 /* Given ARGV, return the text at argument INDEX. Abort if the
910 argument is not text. Index 0 is always text, and indices beyond
911 argc return the empty string. The result is always NUL-terminated,
912 even if it includes embedded NUL characters. */
913 const char *
914 arg_text (macro_arguments *argv, unsigned int index)
916 token_data *token;
917 token_chain *chain;
918 struct obstack *obs; /* Scratch space; cleaned at end of macro_expand. */
920 if (index == 0)
921 return argv->argv0;
922 if (index >= argv->argc)
923 return "";
924 token = arg_token (argv, index, NULL);
925 switch (TOKEN_DATA_TYPE (token))
927 case TOKEN_TEXT:
928 return TOKEN_DATA_TEXT (token);
929 case TOKEN_COMP:
930 /* TODO - concatenate functions. */
931 chain = token->u.u_c.chain;
932 obs = arg_scratch ();
933 while (chain)
935 switch (chain->type)
937 case CHAIN_STR:
938 obstack_grow (obs, chain->u.u_s.str, chain->u.u_s.len);
939 break;
940 case CHAIN_ARGV:
941 arg_print (obs, chain->u.u_a.argv, chain->u.u_a.index,
942 quote_cache (NULL, chain->quote_age,
943 chain->u.u_a.quotes),
944 chain->u.u_a.flatten, NULL, NULL, false);
945 break;
946 default:
947 assert (!"arg_text");
948 abort ();
950 chain = chain->next;
952 obstack_1grow (obs, '\0');
953 return (char *) obstack_finish (obs);
954 default:
955 break;
957 assert (!"arg_text");
958 abort ();
961 /* Given ARGV, compare text arguments INDEXA and INDEXB for equality.
962 Both indices must be non-zero and less than argc. Return true if
963 the arguments contain the same contents; often more efficient than
964 strcmp (arg_text (argv, indexa), arg_text (argv, indexb)) == 0. */
965 bool
966 arg_equal (macro_arguments *argv, unsigned int indexa, unsigned int indexb)
968 token_data *ta = arg_token (argv, indexa, NULL);
969 token_data *tb = arg_token (argv, indexb, NULL);
970 token_chain tmpa;
971 token_chain tmpb;
972 token_chain *ca = &tmpa;
973 token_chain *cb = &tmpb;
974 struct obstack *obs = arg_scratch ();
976 /* Quick tests. */
977 if (ta == &empty_token || tb == &empty_token)
978 return ta == tb;
979 if (TOKEN_DATA_TYPE (ta) == TOKEN_TEXT
980 && TOKEN_DATA_TYPE (tb) == TOKEN_TEXT)
981 return (TOKEN_DATA_LEN (ta) == TOKEN_DATA_LEN (tb)
982 && memcmp (TOKEN_DATA_TEXT (ta), TOKEN_DATA_TEXT (tb),
983 TOKEN_DATA_LEN (ta)) == 0);
985 /* Convert both arguments to chains, if not one already. */
986 /* TODO - allow builtin tokens in the comparison? */
987 if (TOKEN_DATA_TYPE (ta) == TOKEN_TEXT)
989 tmpa.next = NULL;
990 tmpa.type = CHAIN_STR;
991 tmpa.u.u_s.str = TOKEN_DATA_TEXT (ta);
992 tmpa.u.u_s.len = TOKEN_DATA_LEN (ta);
994 else
996 assert (TOKEN_DATA_TYPE (ta) == TOKEN_COMP);
997 ca = ta->u.u_c.chain;
999 if (TOKEN_DATA_TYPE (tb) == TOKEN_TEXT)
1001 tmpb.next = NULL;
1002 tmpb.type = CHAIN_STR;
1003 tmpb.u.u_s.str = TOKEN_DATA_TEXT (tb);
1004 tmpb.u.u_s.len = TOKEN_DATA_LEN (tb);
1006 else
1008 assert (TOKEN_DATA_TYPE (tb) == TOKEN_COMP);
1009 cb = tb->u.u_c.chain;
1012 /* Compare each link of the chain. */
1013 while (ca && cb)
1015 if (ca->type == CHAIN_ARGV)
1017 tmpa.next = ca->next;
1018 tmpa.type = CHAIN_STR;
1019 /* TODO support $@ with funcs. */
1020 assert (!ca->u.u_a.has_func || argv->flatten || ca->u.u_a.flatten);
1021 arg_print (obs, ca->u.u_a.argv, ca->u.u_a.index,
1022 quote_cache (NULL, ca->quote_age, ca->u.u_a.quotes),
1023 argv->flatten || ca->u.u_a.flatten, NULL, NULL, false);
1024 tmpa.u.u_s.len = obstack_object_size (obs);
1025 tmpa.u.u_s.str = (char *) obstack_finish (obs);
1026 ca = &tmpa;
1027 continue;
1029 if (cb->type == CHAIN_ARGV)
1031 tmpb.next = cb->next;
1032 tmpb.type = CHAIN_STR;
1033 /* TODO support $@ with funcs. */
1034 assert (!cb->u.u_a.has_func || argv->flatten || cb->u.u_a.flatten);
1035 arg_print (obs, cb->u.u_a.argv, cb->u.u_a.index,
1036 quote_cache (NULL, cb->quote_age, cb->u.u_a.quotes),
1037 argv->flatten || cb->u.u_a.flatten, NULL, NULL, false);
1038 tmpb.u.u_s.len = obstack_object_size (obs);
1039 tmpb.u.u_s.str = (char *) obstack_finish (obs);
1040 cb = &tmpb;
1041 continue;
1043 assert (ca->type == CHAIN_STR && cb->type == CHAIN_STR);
1044 if (ca->u.u_s.len == cb->u.u_s.len)
1046 if (memcmp (ca->u.u_s.str, cb->u.u_s.str, ca->u.u_s.len) != 0)
1047 return false;
1048 ca = ca->next;
1049 cb = cb->next;
1051 else if (ca->u.u_s.len < cb->u.u_s.len)
1053 if (memcmp (ca->u.u_s.str, cb->u.u_s.str, ca->u.u_s.len) != 0)
1054 return false;
1055 tmpb.next = cb->next;
1056 tmpb.type = CHAIN_STR;
1057 tmpb.u.u_s.str = cb->u.u_s.str + ca->u.u_s.len;
1058 tmpb.u.u_s.len = cb->u.u_s.len - ca->u.u_s.len;
1059 ca = ca->next;
1060 cb = &tmpb;
1062 else
1064 assert (ca->u.u_s.len > cb->u.u_s.len);
1065 if (memcmp (ca->u.u_s.str, cb->u.u_s.str, cb->u.u_s.len) != 0)
1066 return false;
1067 tmpa.next = ca->next;
1068 tmpa.type = CHAIN_STR;
1069 tmpa.u.u_s.str = ca->u.u_s.str + cb->u.u_s.len;
1070 tmpa.u.u_s.len = ca->u.u_s.len - cb->u.u_s.len;
1071 ca = &tmpa;
1072 cb = cb->next;
1076 /* If we get this far, the two tokens are equal only if both chains
1077 are exhausted. */
1078 assert (ca != cb || ca == NULL);
1079 return ca == cb;
1082 /* Given ARGV, return true if argument INDEX is the empty string.
1083 This gives the same result as comparing arg_len against 0, but is
1084 often faster. */
1085 bool
1086 arg_empty (macro_arguments *argv, unsigned int index)
1088 if (index == 0)
1089 return argv->argv0_len == 0;
1090 if (index >= argv->argc)
1091 return true;
1092 return arg_token (argv, index, NULL) == &empty_token;
1095 /* Given ARGV, return the length of argument INDEX. Abort if the
1096 argument is not text. Indices beyond argc return 0. */
1097 size_t
1098 arg_len (macro_arguments *argv, unsigned int index)
1100 token_data *token;
1101 token_chain *chain;
1102 size_t len;
1104 if (index == 0)
1105 return argv->argv0_len;
1106 if (index >= argv->argc)
1107 return 0;
1108 token = arg_token (argv, index, NULL);
1109 switch (TOKEN_DATA_TYPE (token))
1111 case TOKEN_TEXT:
1112 assert ((token == &empty_token) == (TOKEN_DATA_LEN (token) == 0));
1113 return TOKEN_DATA_LEN (token);
1114 case TOKEN_COMP:
1115 chain = token->u.u_c.chain;
1116 len = 0;
1117 while (chain)
1119 unsigned int i;
1120 unsigned int limit;
1121 const string_pair *quotes;
1122 switch (chain->type)
1124 case CHAIN_STR:
1125 len += chain->u.u_s.len;
1126 break;
1127 case CHAIN_ARGV:
1128 i = chain->u.u_a.index;
1129 limit = chain->u.u_a.argv->argc - i - chain->u.u_a.skip_last;
1130 quotes = quote_cache (NULL, chain->quote_age,
1131 chain->u.u_a.quotes);
1132 assert (limit);
1133 if (quotes)
1134 len += (quotes->len1 + quotes->len2) * limit;
1135 len += limit - 1;
1136 while (limit--)
1138 /* TODO handle builtin concatenation. */
1139 if (TOKEN_DATA_TYPE (arg_token (chain->u.u_a.argv, i,
1140 NULL)) == TOKEN_FUNC)
1141 assert (argv->flatten);
1142 else
1143 len += arg_len (chain->u.u_a.argv, i);
1144 i++;
1146 break;
1147 default:
1148 assert (!"arg_len");
1149 abort ();
1151 chain = chain->next;
1153 assert (len);
1154 return len;
1155 default:
1156 break;
1158 assert (!"arg_len");
1159 abort ();
1162 /* Given ARGV, return the builtin function referenced by argument
1163 INDEX. Abort if it is not a builtin in isolation. */
1164 builtin_func *
1165 arg_func (macro_arguments *argv, unsigned int index)
1167 token_data *token;
1169 token = arg_token (argv, index, NULL);
1170 assert (TOKEN_DATA_TYPE (token) == TOKEN_FUNC);
1171 return TOKEN_DATA_FUNC (token);
1174 /* Return an obstack useful for scratch calculations that will not
1175 interfere with macro expansion. The obstack will be reset when
1176 expand_macro completes. */
1177 struct obstack *
1178 arg_scratch (void)
1180 assert (obstack_object_size (stacks[expansion_level - 1].args) == 0);
1181 return stacks[expansion_level - 1].args;
1184 /* Dump a representation of ARGV to the obstack OBS, starting with
1185 argument INDEX. If QUOTES is non-NULL, each argument is displayed
1186 with those quotes. If FLATTEN, builtins are ignored. Separate
1187 arguments with SEP, which defaults to a comma. If MAX_LEN is
1188 non-NULL, truncate the output after *MAX_LEN bytes are output and
1189 return true; otherwise, return false, and reduce *MAX_LEN by the
1190 number of bytes output. If QUOTE_EACH, the truncation length is
1191 reset for each argument, quotes do not count against length, and
1192 all arguments are printed; otherwise, quotes count against the
1193 length and trailing arguments may be discarded. */
1194 bool
1195 arg_print (struct obstack *obs, macro_arguments *argv, unsigned int index,
1196 const string_pair *quotes, bool flatten, const char *sep,
1197 size_t *max_len, bool quote_each)
1199 size_t len = max_len ? *max_len : INT_MAX;
1200 unsigned int i;
1201 token_data *token;
1202 token_chain *chain;
1203 bool use_sep = false;
1204 bool done;
1205 size_t sep_len;
1206 size_t *plen = quote_each ? NULL : &len;
1208 if (!sep)
1209 sep = ",";
1210 sep_len = strlen (sep);
1211 for (i = index; i < argv->argc; i++)
1213 if (quote_each && max_len)
1214 len = *max_len;
1215 if (use_sep && shipout_string_trunc (obs, sep, sep_len, plen))
1216 return true;
1217 use_sep = true;
1218 token = arg_token (argv, i, NULL);
1219 switch (TOKEN_DATA_TYPE (token))
1221 case TOKEN_TEXT:
1222 if (quotes && shipout_string_trunc (obs, quotes->str1, quotes->len1,
1223 plen))
1224 return true;
1225 if (shipout_string_trunc (obs, TOKEN_DATA_TEXT (token),
1226 TOKEN_DATA_LEN (token), &len)
1227 && !quote_each)
1228 return true;
1229 if (quotes && shipout_string_trunc (obs, quotes->str2, quotes->len2,
1230 plen))
1231 return true;
1232 break;
1233 case TOKEN_COMP:
1234 if (quotes && shipout_string_trunc (obs, quotes->str1, quotes->len1,
1235 plen))
1236 return true;
1237 chain = token->u.u_c.chain;
1238 done = false;
1239 while (chain && !done)
1241 switch (chain->type)
1243 case CHAIN_STR:
1244 if (shipout_string_trunc (obs, chain->u.u_s.str,
1245 chain->u.u_s.len, &len))
1246 done = true;
1247 break;
1248 case CHAIN_ARGV:
1249 if (arg_print (obs, chain->u.u_a.argv, chain->u.u_a.index,
1250 quote_cache (NULL, chain->quote_age,
1251 chain->u.u_a.quotes),
1252 flatten, NULL, &len, false))
1253 done = true;
1254 break;
1255 default:
1256 assert (!"arg_print");
1257 abort ();
1259 chain = chain->next;
1261 if (done && !quote_each)
1262 return true;
1263 if (quotes && shipout_string_trunc (obs, quotes->str2, quotes->len2,
1264 plen))
1265 return true;
1266 break;
1267 case TOKEN_FUNC:
1268 func_print (obs, find_builtin_by_addr (TOKEN_DATA_FUNC (token)),
1269 flatten, quotes);
1270 break;
1271 default:
1272 assert (!"arg_print");
1273 abort ();
1276 if (max_len)
1277 *max_len = len;
1278 return false;
1281 /* Populate the new TOKEN as a wrapper to ARGV, starting with argument
1282 INDEX. Allocate any data on OBS, owned by a given expansion LEVEL.
1283 FLATTEN determines whether to allow builtins, and QUOTES determines
1284 whether all arguments are quoted. Return TOKEN when successful,
1285 NULL when wrapping ARGV is trivially empty. */
1286 static token_data *
1287 make_argv_ref_token (token_data *token, struct obstack *obs, int level,
1288 macro_arguments *argv, unsigned int index, bool flatten,
1289 const string_pair *quotes)
1291 token_chain *chain;
1293 if (index >= argv->argc)
1294 return NULL;
1295 TOKEN_DATA_TYPE (token) = TOKEN_COMP;
1296 token->u.u_c.chain = token->u.u_c.end = NULL;
1298 /* Cater to the common idiom of $0(`$1',shift(shift($@))), by
1299 inlining the first few arguments and reusing the original $@ ref,
1300 rather than creating another layer of wrappers. */
1301 while (argv->wrapper)
1303 unsigned int i;
1304 for (i = 0; i < argv->arraylen; i++)
1306 if ((TOKEN_DATA_TYPE (argv->array[i]) == TOKEN_COMP
1307 && argv->array[i]->u.u_c.wrapper)
1308 || level >= 0)
1309 break;
1310 if (index == 1)
1312 push_arg_quote (obs, argv, i + 1, quotes);
1313 obstack_1grow (obs, ',');
1315 else
1316 index--;
1318 assert (i < argv->arraylen);
1319 if (i + 1 == argv->arraylen)
1321 assert (TOKEN_DATA_TYPE (argv->array[i]) == TOKEN_COMP
1322 && argv->array[i]->u.u_c.wrapper);
1323 chain = argv->array[i]->u.u_c.chain;
1324 assert (!chain->next && chain->type == CHAIN_ARGV
1325 && !chain->u.u_a.skip_last);
1326 argv = chain->u.u_a.argv;
1327 index += chain->u.u_a.index - 1;
1329 else
1331 index += i;
1332 break;
1336 make_text_link (obs, &token->u.u_c.chain, &token->u.u_c.end);
1337 chain = (token_chain *) obstack_alloc (obs, sizeof *chain);
1338 if (token->u.u_c.end)
1339 token->u.u_c.end->next = chain;
1340 else
1341 token->u.u_c.chain = chain;
1342 token->u.u_c.end = chain;
1343 token->u.u_c.wrapper = true;
1344 token->u.u_c.has_func = argv->has_func;
1345 chain->next = NULL;
1346 chain->type = CHAIN_ARGV;
1347 chain->quote_age = argv->quote_age;
1348 chain->u.u_a.argv = argv;
1349 chain->u.u_a.index = index;
1350 chain->u.u_a.flatten = flatten;
1351 chain->u.u_a.has_func = argv->has_func;
1352 chain->u.u_a.comma = false;
1353 chain->u.u_a.skip_last = false;
1354 chain->u.u_a.quotes = quote_cache (obs, chain->quote_age, quotes);
1355 return token;
1358 /* Create a new argument object using the same obstack as ARGV; thus,
1359 the new object will automatically be freed when the original is
1360 freed. Explicitly set the macro name (argv[0]) from ARGV0 with
1361 length ARGV0_LEN. If SKIP, set argv[1] of the new object to
1362 argv[2] of the old, otherwise the objects share all arguments. If
1363 FLATTEN, any non-text in ARGV is flattened to an empty string when
1364 referenced through the new object. */
1365 macro_arguments *
1366 make_argv_ref (macro_arguments *argv, const char *argv0, size_t argv0_len,
1367 bool skip, bool flatten)
1369 macro_arguments *new_argv;
1370 token_data *token;
1371 token_data *new_token;
1372 unsigned int index = skip ? 2 : 1;
1373 struct obstack *obs = arg_scratch ();
1375 new_token = (token_data *) obstack_alloc (obs, sizeof *token);
1376 token = make_argv_ref_token (new_token, obs, expansion_level - 1, argv,
1377 index, flatten, NULL);
1378 if (!token)
1380 obstack_free (obs, new_token);
1381 new_argv = (macro_arguments *)
1382 obstack_alloc (obs, offsetof (macro_arguments, array));
1383 new_argv->arraylen = 0;
1384 new_argv->wrapper = false;
1385 new_argv->has_ref = false;
1386 new_argv->flatten = false;
1387 new_argv->has_func = false;
1389 else
1391 new_argv = (macro_arguments *)
1392 obstack_alloc (obs, offsetof (macro_arguments, array) + sizeof token);
1393 new_argv->arraylen = 1;
1394 new_argv->array[0] = token;
1395 new_argv->wrapper = true;
1396 new_argv->has_ref = argv->has_ref;
1397 new_argv->flatten = flatten;
1398 new_argv->has_func = argv->has_func;
1400 new_argv->argc = argv->argc - (index - 1);
1401 new_argv->inuse = false;
1402 new_argv->argv0 = argv0;
1403 new_argv->argv0_len = argv0_len;
1404 new_argv->quote_age = argv->quote_age;
1405 new_argv->level = argv->level;
1406 return new_argv;
1409 /* Push argument INDEX from ARGV, which must be a text token, onto the
1410 expansion stack OBS for rescanning. */
1411 void
1412 push_arg (struct obstack *obs, macro_arguments *argv, unsigned int index)
1414 if (index == 0)
1416 /* Always push copy of arg 0, since its lifetime is not
1417 guaranteed beyond expand_macro. */
1418 obstack_grow (obs, argv->argv0, argv->argv0_len);
1419 return;
1421 if (index >= argv->argc)
1422 return;
1423 push_arg_quote (obs, argv, index, NULL);
1426 /* Push argument INDEX from ARGV, which must be a text token, onto the
1427 expansion stack OBS for rescanning. INDEX must be > 0, < argc.
1428 QUOTES determines any quote delimiters that were in effect when the
1429 reference was created. */
1430 void
1431 push_arg_quote (struct obstack *obs, macro_arguments *argv, unsigned int index,
1432 const string_pair *quotes)
1434 int level;
1435 token_data *token = arg_token (argv, index, &level);
1437 /* TODO handle func tokens. */
1438 if (quotes)
1439 obstack_grow (obs, quotes->str1, quotes->len1);
1440 if (push_token (token, level, argv->inuse))
1441 arg_mark (argv);
1442 if (quotes)
1443 obstack_grow (obs, quotes->str2, quotes->len2);
1446 /* Push series of comma-separated arguments from ARGV, which can
1447 include builtins, onto the expansion stack OBS for rescanning. If
1448 SKIP, then don't push the first argument. If QUOTE, the rescan
1449 also includes quoting around each arg. */
1450 void
1451 push_args (struct obstack *obs, macro_arguments *argv, bool skip, bool quote)
1453 unsigned int i = skip ? 2 : 1;
1454 token_data td;
1455 token_data *token;
1457 if (i >= argv->argc)
1458 return;
1460 if (i + 1 == argv->argc)
1462 push_arg_quote (obs, argv, i, quote ? &curr_quote : NULL);
1463 return;
1466 /* TODO allow shift, $@, to push builtins without flatten. */
1467 token = make_argv_ref_token (&td, obs, -1, argv, i, true,
1468 quote ? &curr_quote : NULL);
1469 assert (token);
1470 if (push_token (token, -1, argv->inuse))
1471 arg_mark (argv);